/**
 * AJaX handler
 * create object by calling Ajax() and trigger HTTP request by calling prototype doSend();
 * @author V. Perez
 * @copyright Copyright (c) 2008
 * @version 1.0.1 16 apr 2008 10:54:33
 */


/**
 * Ajax Object
 */
function Ajax(){
	/**
	 * @access private
	 * @var string contains the url for the request
	 */
	this.m_sUrl = '';
	
	/**
	 * @access private
	 * @var object contains the form object from the DOM, if not null the ajax object will read all values from the form
	 */	
	this.m_oForm = null;
	
	/**
	 * @access private
	 * @var string sMethod, contains the request method ( post or get )
	 */		
	this.m_sMethod = '';
	
	/**
	 * @access private
	 * @var string contains the send query
	 */	
	this.m_sQuery = '';
	
	/**
	 * @access private
	 * @var string contains the function that will be calld on return data from the request
	 */	
	this.m_sReturnDataTo = '';
	
	/**
	 * @access private
	 * @var string sSendCode, contains a sendcode
	 */	
	this.m_sSendCode = '';

	/**
	 * @access private
	 * @var object contains the HTTP Request object
	 */	
	this.m_oXmlHttp = null;
	
	/**
	 * @access private
	 * @var string contains a temp. query string
	 */	
	this.m_sQuerySet = '';
	
	/**
	 * @access private
	 * @var object contains return object
	 */
	this.m_oReturnObject = null;
	
	/**
	 * @access public
	 * 
	 */
	this.sRequestedData = '';
	
	this.oReturnAjax = null;
}

/**
 * The setter
 * @param string p_sKey, contains the property that you want to set see
 * @param mixed p_vValue contains the value for the key
 * @property-write string sUrl, contains the url for the request
 * @property-write object oForm, contains the form object from the DOM, if not null the ajax object will read all values from the form ( data is not supported )
 * @property-write string sMethod, contains the request method ( post or get )
 * @property-write string sQuery, contains the send query, warning if oForm is set all data from de sQuery will by killd
 * @property-write string sReturnDataTo, contains the function that will be calld on return data from the request
 * @property-write string sSendCode, contains a sendcode
 */
Ajax.prototype.set = function(p_sKey, p_vValue){
	switch(p_sKey){
		case 'sUrl':{
			this.m_sUrl = p_vValue;
		}
		break;
		
		case 'oForm':{
			this.m_oForm = p_vValue;
		}
		break;
		
		case 'sMethod':{
			var sMethod = p_vValue.toUpperCase();
			if(sMethod == 'POST' || sMethod == 'GET'){
				this.m_sMethod = sMethod;
			}else{
				throw 'property sMethod must be POST or GET\nYou\'r value is:' + p_vValue;
			}
		}
		break;
		
		case 'sQuery':{
			this.m_sQuery = p_vValue;
		}
		break;
		
		case 'sReturnDataTo':{
			this.m_sReturnDataTo = p_vValue;
		}
		break;
		
		case 'sSendCode':{
			this.m_sSendCode = p_vValue;
		}
		break;
		
		case 'oReturnObject':{
			this.m_oReturnObject = p_vValue;
		}
		break;
		
		case 'oAjax':{
			this.oReturnAjax = p_vValue;
		}
		break;
	}
}
 

/**
 * Ajax prototype createXMLHttpRequest
 * This will create a HTTP Request
 */
Ajax.prototype.createXMLHttpRequest = function(){
	if(window.XMLHttpRequest){
		this.m_oXmlHttp = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		var aIe_versions = new Array("MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp","Microsoft.XMLHTTP");
		for(var i=0; i < aIe_versions.length; i++) {
			try {
				this.m_oXmlHttp = new ActiveXObject(aIe_versions[i]);
			}catch (error){}
		}
	}
}

/**
 * Ajax prototype handleStateChange
 * This will handle the HTTP request states
 */
Ajax.prototype.handleStateChange = function(){

	if(this.m_oXmlHttp.readyState == 4 ){
		if(this.m_oXmlHttp.status == 200){
			if(this.m_oReturnObject){
				this.sRequestedData = this.m_oXmlHttp.responseText;
				var oThis = this;
				with(this.m_oReturnObject){
					eval("(" + oThis.m_sReturnDataTo + "())");
				}
			}else{
				var sReturn = this.m_sReturnDataTo+"('"+this.m_oXmlHttp.responseText+"')";
				setTimeout(sReturn, 0);
			}
		}else{
			throw this.m_oXmlHttp.status;
		}
	}
}

/**
 * Ajax prototype doSend
 * This will start the Ajax request
 */
Ajax.prototype.doSend = function(){
	if((this.m_sMethod == 'POST' || this.m_sMethod == 'GET')
		&& this.m_sUrl != ''
	){
		this.createXMLHttpRequest();
		if (this.m_sReturnDataTo != '') {
			var oAjax = this;	
			this.m_oXmlHttp.onreadystatechange = function(){
				oAjax.handleStateChange();
			}
		}
		if(this.m_oForm != null){
			this.readForm();
		}
		if(this.m_sMethod == "POST"){
			this.m_oXmlHttp.open("POST", this.m_sUrl, true);
			this.m_oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			this.m_oXmlHttp.setRequestHeader("Cache-Control", "no-cache");
			this.m_oXmlHttp.setRequestHeader("Content-length", this.m_sQuery.length);
			this.m_oXmlHttp.setRequestHeader("Connection", "close");
			this.m_oXmlHttp.setRequestHeader("X_USERAGENT", this.m_sSendCode);
			this.m_oXmlHttp.send(this.m_sQuery);
		}else{
			if(this.m_sQuery != ''){
				URL = URL+'?'+this.m_sQuery;
			}
			this.m_oXmlHttp.open("GET", this.m_sUrl, true);
			this.m_oXmlHttp.setRequestHeader("Cache-Control", "no-cache");
			this.m_oXmlHttp.setRequestHeader("X_USERAGENT", this.m_sSendCode);	
			this.m_oXmlHttp.send(null);
		}
	}else{
		throw 'Please set sMethod, sReturnDataTo and sUrl for the request';
	}
}


Ajax.prototype.readForm = function(){
	if(this.m_oForm != null){
		for(var i=0; this.m_oForm.elements[i]; i++){
			switch(this.m_oForm.elements[i].type){
				case 'radio':{
					if(this.m_oForm.elements[i].checked == true){
						this.m_sQuerySet = this.m_oForm.elements[i].name+'='+m_oForm.elements[i].value;
					}else{
						this.m_sQuerySet = '';
					}
				}
				break;
					
				case 'textarea':{
					this.m_sQuerySet = this.m_oForm.elements[i].name+'='+encodeURIComponent(this.m_oForm.elements[i].value);
				}
				break;

				case 'text':{
					this.m_sQuerySet = this.m_oForm.elements[i].name+'='+encodeURIComponent(this.m_oForm.elements[i].value);
				}
				break;
				
				case 'select-one':{
						this.m_sQuerySet = this.m_oForm.elements[i].name+'='+encodeURIComponent(this.m_oForm.elements[i][this.m_oForm.elements[i].selectedIndex].value);
				}
				break;

				case 'hidden':{
					this.m_sQuerySet = this.m_oForm.elements[i].name+'='+encodeURIComponent(this.m_oForm.elements[i].value);
				}
				break;
				
				case 'checkbox':{
					if(this.m_oForm.elements[i].checked == true){
						this.m_sQuerySet = this.m_oForm.elements[i].name+'=1';
					}else{
						this.m_sQuerySet = this.m_oForm.elements[i].name+'=0';
					}
				}
				break;
				
				case 'button':{
					this.m_sQuerySet = this.m_oForm.elements[i].name+'='+encodeURIComponent(this.oSelectedForm.elements[i].value);
				}
				break;
				
				case 'select-multiple':{
					this.m_sQuerySet = this.m_oForm.elements[i].name+'=';
					for(var j =0; j < this.m_oForm.elements[i].options.length; j++){
						if(this.m_oForm.elements[i].options[j].selected == true){
							this.m_sQuerySet += encodeURIComponent(this.m_oForm.elements[i].options[j].value+';');
						}
					}
				}
				break;
				
				default:{
					//alert(this.m_oForm.elements[i].type);
				}
			}
			
			if(this.m_sQuerySet){
				if(this.m_sQuery){
					this.m_sQuery += '&'+ this.m_sQuerySet;
				}else{
					this.m_sQuery += this.m_sQuerySet;
				}
			}
		}
	}else{
		throw 'Please set first a Form object for running readForm prototype';
	}
}
