	/**
	*
	* INSAO PLAY
	* 
	* @copyright	(c) 2007-2010 PR23 Limited. All Rights Reserved
	* @author 		PR23 :: Paul Winterhalder
	*
	* @category 	INSAO PLAY 1.0.0.1
	* @package 		/insao-play/
	* @subpackage 	/www/
	* @name 		ajaxhandler.js
	*
	*
	*/

	/** -------------------------------------------------------
	*
	* XMLHTTP call
	*
	**/
	function GetXmlHttpObject(){
		var objXMLHttp=null
	
		if (window.XMLHttpRequest) {
			objXMLHttp=new XMLHttpRequest();
			
		} else if (window.ActiveXObject) {
			objXMLHttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
		return objXMLHttp
	} 
	
	/** -------------------------------------------------------
	*
	*
	*/	
	function pathToXsl(_file) {
		var url  = '/-/xsl/'+_file+'/';
		return url;
	}
	
	/** -------------------------------------------------------
	*
	* Add the loading status bar
	*/
	function wwwAddLoadingStatusBar() {
		var eul  					= 	document.createElement("div");
		var popupimg 				= 	document.createElement("img");
		popupimg.src				=	getImagePath();
		popupimg.height				=	"30";
		popupimg.width				=	"30";
		popupimg.style.marginTop 	=	"10px";
		popupimg.style.marginRight 	=	"10px";
		
		eul.appendChild(popupimg);

		return eul;		 	 
	}
	
	/** -------------------------------------------------------
	*
	*
	*/
 	function getImagePath() {
		var url  = '/-/images/loading/gif/';
		return url;
	}
	
	/** -------------------------------------------------------
	*
	*	
	*/
	function loadXMLDoc(fname) {
		
		var xmlDoc;
		
		try {
			// IE
			if (window.ActiveXObject){
		 		 xmlDoc 	= new ActiveXObject("Microsoft.XMLDOM");
				
		 	//Firefox, Mozilla, Opera, etc.
			} else if (document.implementation && document.implementation.createDocument) {
	 			 xmlDoc		=	document.implementation.createDocument("","", null);
			
			}
			
			xmlDoc.async			=	false;
			xmlDoc.validateOnParse 	= 	false;
			xmlDoc.resolveExternals = 	false;
			xmlDoc.load(fname);

		} catch(e) {
			// Google Chrome
			try {
			 	var xmlhttp = new window.XMLHttpRequest();
			    xmlhttp.open("GET",fname,false);
			    xmlhttp.send(null);
			    xmlDoc = xmlhttp.responseXML.documentElement; 
			} catch(e) {
				
			}
		}
		return(xmlDoc);
	}
	
	/** -------------------------------------------------------
	*
	*	
	*/
	function serveWWWXslt(theXML, theXSL) {

		xml 	= 	theXML;
		xsl		=	loadXMLDoc(theXSL);
		
		if (window.ActiveXObject) {
	  		resultDocument		=	xml.transformNode(xsl);
	  		
	  	} else if (document.implementation && document.implementation.createDocument) {
		  xsltProcessor		=	new XSLTProcessor();
		  xsltProcessor.importStylesheet(xsl);
		  resultDocument 	= 	xsltProcessor.transformToFragment(xml,document);
	  	}

	  	return resultDocument;
	}
	
	/** -------------------------------------------------------
	*
	* Alert any error
	* @param (str)	status
	* @param (str)	msg
	*
	*/
	function alertError(status,msg,close) {
		var	close 	= 	((typeof(close) != 'undefined'))					? 	false 	: 	true;
		var	status 	=  	((typeof(status) != 'undefined') && (status != ''))	? 	status 	: 	'Warning';
		var msg	 	= 	(typeof(msg) != 'undefined')						? 	msg 	: 	'Unknown error';
		
		if(close == true) {
			false;
		}
		
		alert(status + '\r\n' + msg);
	}
	
	/** -------------------------------------------------------
	*
	* Catch exception error
	* @param 	(str)	msg
	* @return array
	*
	*/
	function catchError(msg) {
		if(_InsaoiwmDebug = true) {
			var msg	 			=	 typeof(msg) 		!= 'undefined' 		? 	msg 			: 	'Unknown error';
			alert(msg);
		}
	}
	
	/** -------------------------------------------------------
	*
	*  Display any errors
	*
	* @param (arr)	xmlDocumentErrors
	* @return null
	*
	*/
	function displayErrorBox(xmlDocumentErrors) {
		alert(xmlDocumentErrors.status + '\r\n' + xmlDocumentErrors.etext)
	}
	
	/** -------------------------------------------------------
	*
	* FIND && SORT ERRORS
	* We only return the first error - we do not want to overload the user with errors 
	* @param (xml)	xmlDocument
	*
	*/
	function checkForAjaxErrors(xmlDocument) {
		
		try {
			var numoferrors 	= 	xmlDocument.getElementsByTagName('error').length
			if(numoferrors >= 1) {
				var seterrorText 		= 	xmlDocument.getElementsByTagName('error')[0].childNodes[0].nodeValue
				return {set:true, status:false, etext:seterrorText};
				
			} else {
				return {set:false, status:false, etext:false};
			}
		} catch(e) {
			return {set:true, status:false, etext:e};
		}
	}
	
	/** -------------------------------------------------------
	*
	*	
	*
	*
	*/
	function prepare_www_ajax_cmd(cmdCall,cmd,appendposts) {
		var	cmdCall			=	 typeof(cmdCall)		!= 'undefined' 		? 	cmdCall 		: 	'null';
		var	cmd				=	 typeof(cmd)			!= 'undefined' 		? 	cmd 			: 	'null';
		var	appendposts		=	 typeof(appendposts)	!= 'undefined' 		? 	appendposts 	: 	'';
		var xmlDocument		=	 null;
		
		xmlHttp=GetXmlHttpObject()
		url="/-/ajax/" + cmdCall + "/";
		
		if ( xmlHttp==null ) {
			alert ('Browser does not support HTTP Request')
			return
		} 
		
		xmlHttp.open('POST', url, true)
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
		xmlHttp.send('action='+cmd+''+appendposts)
		return xmlHttp;
	}
	
	/** -------------------------------------------------------
	*
	*	
	* @param (str)	instance
	* @param (str)	cmd
	* @param (str)	appendposts
	*
	*/
	function prepare_www_ajax_process_action(instance,cmd,appendposts) {

		var instance	 	=	 typeof(instance)		!= 'undefined' 		? 	instance 		: 	'home';
		var	cmd				=	 typeof(cmd)			!= 'undefined' 		? 	cmd 			: 	'null';
		var	appendposts		=	 typeof(appendposts)	!= 'undefined' 		? 	appendposts 	: 	'';
		
		return prepare_www_ajax_cmd(instance,cmd,appendposts);
	}
	
	
	/** -----------------------------------------------------
	*
	* (int) fid
	* (str) fcmd
	*
	*/
	function build_gallery(mid,intwidth) {
		
		try {
			sendparams		=	'&mid='+escape(mid)+''
			var xmlHttp 	= 	prepare_www_ajax_process_action('www','getGalleryImage',sendparams);
			xmlHttp.onreadystatechange=function() {
				if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete') {
		
					var xmlDocument		=	xmlHttp.responseXML;
					var anyErrors		=	checkForAjaxErrors(xmlDocument);
										
					if(anyErrors.set == true) {
						alertError(anyErrors.status, anyErrors.etext, false);
					
					} else {
						popupgallery(intwidth);
						toAppend	=	document.getElementById('popbox_ui_inner');
					
						try {
							var xslpath		=	pathToXsl('gallery');
							var transform	=	serveWWWXslt(xmlDocument.getElementsByTagName('response')[0], xslpath);
						
							if (window.ActiveXObject) {
								toAppend.innerHTML=transform;
									
						  	} else if (document.implementation && document.implementation.createDocument) {
							 	toAppend.appendChild(transform);
						 	
							 	if(document.getElementById("gallery_right_btm_inner") != null) {
						  			var textgg	=	xmlDocument.getElementsByTagName('altext')[0]
						  			textgg		=	textgg.textContent
									document.getElementById("gallery_right_btm_inner").innerHTML	=	textgg
						  		}
						  	}
						  	
				  		} catch(e) {
							catchError(e);
						}
						
					}
				}
			}
  		} catch(e) {
			catchError(e);
		}
	}
	
	/** -----------------------------------------------------
	*
	* (int) fid
	* (str) fcmd
	*
	*/
	function build_swf_popup(mid,intwidth) {
		
		try {
			sendparams		=	'&mid='+escape(mid)+''
			var xmlHttp 	= 	prepare_www_ajax_process_action('www','getSWF',sendparams);
			xmlHttp.onreadystatechange=function() {
				if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete') {
		
					var xmlDocument		=	xmlHttp.responseXML;
					var anyErrors		=	checkForAjaxErrors(xmlDocument);
										
					if(anyErrors.set == true) {
						alertError(anyErrors.status, anyErrors.etext, false);
					
					} else {
						popupgallery(intwidth);
						toAppend					=	document.getElementById('popbox_ui_inner');
						
						toAppend.style.textAlign 	= 	'center';
						var loadTxt 		 		=  	wwwAddLoadingStatusBar();
						toAppend.appendChild(loadTxt);

						
					
						try {
							var xslpath		=	pathToXsl('swf_popup');
							var transform	=	serveWWWXslt(xmlDocument.getElementsByTagName('response')[0], xslpath);
						
							toAppend.removeChild(loadTxt)
							toAppend.style.textAlign = 'left';

							if (window.ActiveXObject) {
								toAppend.innerHTML=transform;
									
						  	} else if (document.implementation && document.implementation.createDocument) {
							 	toAppend.appendChild(transform);
						 	
							 	if(document.getElementById("gallery_right_btm_inner") != null) {
						  			var textgg	=	xmlDocument.getElementsByTagName('altext')[0]
						  			textgg		=	textgg.textContent
									document.getElementById("gallery_right_btm_inner").innerHTML	=	textgg
						  		}
						  	}
						  	
						  	
						  	hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
							if(hasRightVersion) { 
								AC_FL_RunContent(
									'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,2,0',
									'width', xmlDocument.getElementsByTagName('swf_width')[0].childNodes[0].nodeValue,
									'height', xmlDocument.getElementsByTagName('swf_height')[0].childNodes[0].nodeValue,
									'src', xmlDocument.getElementsByTagName('swf_src')[0].childNodes[0].nodeValue,
									'quality', 'high',
									'pluginspage', 'http://www.adobe.com/go/getflashplayer',
									'align', 'middle',
									'play', 'true',
									'loop', 'false',
									'scale', 'showall',
									'devicefont', 'false',
									'id', 'swf_popup_' + xmlDocument.getElementsByTagName('swf_id')[0].childNodes[0].nodeValue,
									'bgcolor', '#ffffff',
									'name', 'swf_popup_' + xmlDocument.getElementsByTagName('swf_id')[0].childNodes[0].nodeValue,
									'menu', 'true',
									'allowFullScreen', 'false',
									'allowScriptAccess','always',
									'movie', xmlDocument.getElementsByTagName('swf_src')[0].childNodes[0].nodeValue,
									'salign', '',
									'objecttoappend', 'gallery_left_inner'
									); //end AC code
									
							} else {  
								var alternateContent = ''
									+ 'This content requires the Adobe Flash Player.'
									+ '<a href="http://www.adobe.com/go/getflashplayer/">Get Flash</a>';
								document.write(alternateContent);  
							}
						
				  		} catch(e) {
							catchError(e);
						}
						
					}
				}
			}
  		} catch(e) {
			catchError(e);
		}
	}
