/** XHR helper methods **/


var cgxhr = {

	xhr: false,
	responseFunc: null,

	create: function() {
		try {  
			this.xhr = new ActiveXObject('Msxml2.XMLHTTP');   
		} catch (e) {
			try {   
				this.xhr = new ActiveXObject('Microsoft.XMLHTTP');    
			} catch (e2) {
				try {  
					this.xhr = new XMLHttpRequest();     
				}  catch (e3) {  
					this.xhr = false;   
				}
			}
		}
		return this.xhr;
	},
	

	sendForm: function(frmN, processResult) {

		var frm = null;
		if (typeof frmN == 'string')
			frm = document.forms[frmN]; 
		else 
			frm = frmN;

		var action = "";
		try {
			action = frm.action;
		} catch (e) {
 		}
		if (action == "") {
//			action = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + window.location.pathname;
			action = window.location.protocol + '//' + window.location.hostname + window.location.pathname;
		}

		var params = "";
		for (idx = 0; idx < frm.elements.length; idx++) {
			var elm = frm.elements[idx];
		
			var value = this.getInputValue(elm);
			if (value != null)
				params += elm.name + "=" + this.urlencode( value ) + "&";
		}

//alert(  "action : " + action + " - params : " + params );
//		this.debug ( "action : " + action + " - params : " + params );
	
		if (frm.method == "GET") {
			action += "?" + params;
			this.doGET(action, null, processResult);
		} else {
			this.doPOST(action, params, processResult);
		}
	},


	doPOST: function(url, params, processfunction) { 
		this.create();
		if (!this.xhr) this.create();
		if (!this.xhr) return;
		
		try {
		
			this.xhr.onreadystatechange = function()  { return processfunction(this.xhr); }; 

//			this.debug ( "xhr POST : " + url + " - params : " + params );
	

			this.xhr.open("POST", url, true);
			this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
			if (params != null) this.xhr.setRequestHeader("Content-length", params.length);
			this.xhr.setRequestHeader("Connection", "close");             
			this.xhr.send(params);
		} catch ( e ) {
		}
	},
 
	doGET: function(url, params, processfunction) { 
		this.create();
		if (!this.xhr) this.create();
		if (!this.xhr) return;

		try {
			this.xhr.onreadystatechange = function()  { return processfunction(this.xhr); }; 
			this.xhr.open("GET", url + "?" + params, true);
			this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
			//if (params != null) this.xhr.setRequestHeader("Content-length", params.length);
			this.xhr.setRequestHeader("Connection", "close");             
			//this.xhr.send(params);
			this.xhr.send(null);
		} catch ( e ) {
		}
	},
 

	/**
		return body content 
	*/
	getBody: function(content)  {
	   var test = content.toLowerCase();    // to eliminate case sensitivity
	   var x = test.indexOf("<body");
	   if(x == -1) return "";

	   x = test.indexOf(">", x);
	   if(x == -1) return "";

	   var y = test.lastIndexOf("</body>");
	   if(y == -1) y = test.lastIndexOf("</html>");
	   if(y == -1) y = content.length;    // If no HTML then just grab everything till end

	   return content.slice(x + 1, y);   
	}, 

	/** getContentByIds()
	return the content between tags with id1 (included) and id2 (not included)
	*/
	getContentByIds: function(content, id1, id2)  {
		test = content.toLowerCase();    // to eliminate case sensitivity
		var x = test.indexOf('id="' + id1 + '"');
	   	if(x == -1) return "";

		var y = test.lastIndexOf('id="' + id2 + '"');

		if (y > x) {

	   		var test2 = test.slice(x, y);
	   		var x2 = test2.indexOf('>');
	    		var y2 = test2.lastIndexOf('</');

			return content.slice(x+x2+1, x+y2 -2);
		}
		return "";
	},

	urlencode: function(str) {
	    return escape(str.replace(/%/g, '%25').replace(/\+/g, '%2B')).replace(/%25/g, '%');
	},


	getInputValue: function( elem ) {
		if (elem.type == 'checkbox') {
			if (elem.checked)
				return elem.value;
			else 
				return null;
		}
		if (elem.type == 'radio') {
			if (elem.checked)
				return elem.value;
			else 
				return null;
		}

		if (elem.value) return elem.value;
	
	},


	debug: function( txt ) { var divdbg = document.getElementById('cgdebug'); if (divdbg) divdbg.innerHTML += txt + "<br>"; 	}
};







