/**
 * @author Sebastian Romero - Andres Garcia - March-10-2008
 * @projectDescription Class which allow to manipulate async. server requests
 * @version 1.0
 * @example 
 * var ajax = new AjaxRequest();
	ajax.onComplete = function(data){ this.ignoreWhite(data.xml)...};
	ajax.readXML(path);
 */
function AjaxRequest(){
	this.onComplete;
	
	this.objectToQueryString = function (obj){
		var str_toQueryString = "";
		
		for (var i in obj){
			
			if (obj[i].constructor.toString().indexOf("Array") != -1){
				for(var k = 0; k < obj[i].length; k++) {
					
					obj[i][k] = encodeURIComponent(obj[i][k]);
					
					//obj[i][k] = replaceExtChars(obj[i][k]);	
					//obj[i][k]  = String(obj[i][k]).split("%").join(escape("%")).split("&").join(escape("&"));
					str_toQueryString += String("&" + i + "=" + obj[i][k]);
				}
			} else {
				obj[i] = encodeURIComponent(obj[i]);
				//obj[i] = replaceExtChars(obj[i]);
				//obj[i]  = String(obj[i]).split("%").join(escape("%")).split("&").join(escape("&"));
				str_toQueryString += String("&" + i + "=" + obj[i]);
			}
		}
		return str_toQueryString.substring(1, str_toQueryString.length);
	}
	
	

	/**
	 * 
	 * @param {String} url
	 * @param {String} method
	 * @param {Object} objParams
	 * @param {Function} handler
	 */
	this.readXML = function (url, method, objParams, handler){
		
		var STR_NO_AJAX_ERROR = "Please upgrade your browser since its version is too old.";
		
		
		
		var strParams = this.objectToQueryString(objParams);
		
		
		var ajaxRequest = false;
		var fnc_onFinished = (!handler) ? this.onComplete : handler;
		var str_method = AjaxMethod.GET;
		
		var xml_response;
		var str_response;
		
		if (window.XMLHttpRequest){
			ajaxRequest = new XMLHttpRequest ();
		} else if (window.ActiveXObject){
			try{
				ajaxRequest = new ActiveXObject ("Msxml2.XMLHTTP");
			}catch (e){
				try{
					ajaxRequest = new ActiveXObject ("Microsoft.XMLHTTP");
				} catch(e){
					showAlert(STR_NO_AJAX_ERROR, "Ajax Error");
					return false;
				}
			}
		} else {
			showAlert(STR_NO_AJAX_ERROR, "Ajax Error");
			return false;
		}
		

		
		ajaxRequest.onreadystatechange = function (){
			if(ajaxRequest.readyState == 4){
				if(ajaxRequest.status == 200 || window.location.href.indexOf ("http")){
					xml_response = ajaxRequest.responseXML;
						str_response = ajaxRequest.responseText;
						//xml_response = ignoreWhite(xml_response);
						fnc_onFinished({xml:xml_response, txt:str_response});
					
				} else {
					//error http 
				}
			} else {
				//progress
				
			}
		}
		
		ajaxRequest.open((method == AjaxMethod.POST) ? AjaxMethod.POST : AjaxMethod.GET, url, true);
		
		if(method == AjaxMethod.POST){
			ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("charset", "utf-8");
			//ajaxRequest.setRequestHeader("Content-length", strParams.length);
			ajaxRequest.setRequestHeader("Connection", "close");
			str_method = AjaxMethod.POST;
		}
		
		
		
		ajaxRequest.send(strParams);
	}
	/*	
	ignoreWhite Method
	Take the XML object and removes the Text and Empty nodes
	@Params:
		xml:Object -> XMLObject
	@Returns a XML Object
	*/
	this.ignoreWhite = function (xmlObject){
		for(var i = 0; i< xmlObject.childNodes[0].childNodes.length; i++){
			switch (Number(xmlObject.childNodes[0].childNodes[i].nodeType)){
				case 3:
					xmlObject.childNodes[0].removeChild(xmlObject.childNodes[0].childNodes[i]);
				break;
			}
		}
		return xmlObject;
	}
}

AjaxMethod = {
	POST : "POST",
	GET : "GET"
}

HTMLEncode = {
	/**
	* @desc HTML Encode
	* @param {String} str
	*/
	encode : function (str){
	   var div = document.createElement("div");
	   //var text = document.createTextNode(str);
	   //div.appendChild(text);
	   div.innerHTML = str;
	   alert(div.innerHTML);
	   return div.innerHTML;
	}
}


/*
function replaces extended characters of 'text' with its character entities.
call the function by default with output = false. if you switch it to true it will format the source code for output in a textarea.
*/
function replaceExtChars(text, output) {
	
	if(typeof(text) != "string"){
		return text;
	}
	
	text = text.replace(eval('/&/g'), '&amp;');
	var fromTo = new Array('&AElig;','Æ','&Aacute;','Á','&Acirc;','Â','&Agrave;','À','&Aring;','Å','&Atilde;', 'Ã','&Auml;','Ä','&Ccedil;','Ç','&ETH;','Ð','&Eacute;','É','&Ecirc;','Ê','&Egrave;','È ','&Euml;','Ë','&Iacute;','Í','&Icirc;','Î','&Igrave;','Ì','&Iuml;','Ï','&Ntilde;','Ñ', '&Oacute;','Ó','&Ocirc;','Ô','&Ograve;','Ò','&Oslash;','Ø','&Otilde;','Õ','&Ouml;','Ö','&THORN; ','Þ','&Uacute;','Ú','&Ucirc;','Û','&Ugrave;','Ù','&Uuml;','Ü','&Yacute;','Ý','&aacute;', 'á','&acirc;','â','&aelig;','æ','&agrave;','à','&aring;','å','&atilde;','ã','&auml;','ä ','&brvbar;','¦','&ccedil;','ç','&cent;','¢','&copy;','©','&deg;','°','&eacute;','é', '&ecirc;','ê','&egrave;','è','&eth;','ð','&euml;','ë','&frac12;','½','&frac14;','¼','&frac34; ','¾','&gt;','>','&gt','>','&iacute;','í','&icirc;','î','&iexcl;','¡','&igrave;','ì','&iquest;','¿','&iuml;','ï', '&laquo;','«','&lt;','<','&lt','<','&mdash;','—','&micro;','µ','&middot;','·','&ndash;','–','&not;','¬','&ntilde;','ñ', '&oacute;','ó','&ocirc;','ô','&ograve;','ò','&oslash;','ø','&otilde;','õ','&ouml;','ö','&para;','¶','&plusmn;','±','&pound;',' £','&quot;','\"','&raquo;','»','&reg;','®','&sect;','§','&shy;','\\*','&sup1;','¹','&sup2;','²', '&sup3;','³','&szlig;','ß','&thorn;','þ','&tilde;','˜','&trade;','™','&uacute;','ú','&ucirc; ','û','&ugrave;','ù','&uuml;','ü','&yacute;','ý','&yen;','¥','&yuml;','ÿ');
	
	if (output) {
		fromTo[fromTo.length] = '&amp;';
		fromTo[fromTo.length] = '&';
	}
	
	for (var i = 0; i < fromTo.length; i = i + 2){
		text = text.replace(eval("/" + fromTo[i + 1] + "/g"), fromTo[i]);
	}	
	return (text);
}



