	/**
	*
	* 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  = '/-/insao-play/publisher/-/xsl/'+_file+'/';
		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);
	}
	
	/** -------------------------------------------------------
	*
	* 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('GET', 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_generic_page() {
		
		try {
			var xmlHttp 	= 	prepare_www_ajax_process_action('www','','');
			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 {
						alert("foo")
					}
				}
			}
  		} catch(e) {
			alert(e);
		}
	}