/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

/*
 * AJAX functions
 *
 * These functions processes the XMLHTTPrequests
 *
 */

var xHr = {
	/*
	 * processes a form and generates a URL string
	 */
	stringFormData: function(oFrm){
		var i, s;
		s='';
		for (i=0; i<oFrm.length; i++) {
			if (oFrm[i].name) {
				switch(oFrm[i].type){
					case 'checkbox':
						s += oFrm[i].name + '=' + (oFrm[i].checked ? '1' : '0') + '&';
						break;
					case 'radio':
						if (oFrm[i].checked) {
							s += oFrm[i].name + '=' + encodeURIComponent(oFrm[i].value) + '&';
						}
						break;
					default:
						s += oFrm[i].name + '=' + encodeURIComponent(oFrm[i].value) + '&';
						break;
				 } // switch
			}
		}
		s = s.substr(0, s.length-1);
		return s;
	},

	/* send request string and pass it to the closure function
	 *
	 * pString: URL request string e.g. 'first_name=John&last_name=Miller'
	 * pUrl:    URL which processes request
	 * pFunc:   closure function to be processed and to which the response
	 *          will be passed.
     */
	getStringResponse: function(pString, pUrl, pFunc) {
		var oRq = new XMLHttpRequest();
		oRq.open('POST', pUrl, true);
		oRq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//oRq.setRequestHeader("Content-length", pString.length);
		oRq.onreadystatechange = function () {
			if (oRq.readyState === 4) {
				// Show again the default cursor
				document.body.style.cursor = 'default';
				if (oRq.status === 200) {
					pFunc(oRq.responseText);
				}
			} else {
				// Show a waiting cursor
				document.body.style.cursor = 'wait';
			}
		};
		oRq.send(pString);
	},


	/* send request string and put HTML response into the inner HTML
	 * of the destination element, usually a <div>
	 *
	 * pString: URL request string e.g. 'first_name=John&last_name=Miller'
	 * pUrl:    URL which processes request
	 * pDest:   String id of Object or Object where response will be put into, usually a <div>.
	 *          The innerHTML of the object will be replaced by the response.
	 * pFunc:   optional closure function to be processed after the response.
     */
	getHTMLResponse: function(pString, pUrl, pDest, pFunc){
		this.getStringResponse(pString, pUrl, function(cResp) {
			if (pDest) {
				var oDest;
				if (typeof(pDest)==='object') {
					oDest=pDest;
				} else {
					oDest = document.getElementById(pDest);
				}
				oDest.innerHTML = cResp;
/*				var re = /<script.*?>(.*?)<\//igm;
				var match;
				while (match = re.exec(cResp)) {
					document.write(match[1]);
				} */
			}
			if (pFunc) {
				pFunc();
			}
		});
	},

	/* send request string and pass the JSON response to the supplied function
	 *
	 * pString: URL request string e.g. 'first_name=John&last_name=Miller'
	 * pUrl:    URL which processes request
	 * pFunc:   closure function to which the response is passed to. The response
	 *          is passed as a parameter of type object.
     */
	getJsonResponse: function(pString, pUrl, pFunc){
		this.getStringResponse(pString, pUrl, function(cResp) {
			if (JSON) {
				var oJson=JSON.parse(cResp);
			} else {
				var oJson = eval('('+cResp+')');
			}
			pFunc(oJson);
		});
	},

	/* send form data and put response into the inner html of the
	 * supplied element
	 *
	 * pForm:   Form to be submitted
	 * pUrl:    URL which processes request
	 * pDest:	Object where response will be put into, usually a <div>.
	 *          The innerHTML of the object will be replaced by the response.
	 * pFunc:   optional closure function to be processed after the response.
     */
	getHTMLfromForm: function(pForm, pUrl, pDest, pFunc) {
		var s = this.stringFormData(pForm);
		this.getHTMLResponse(s, pUrl, pDest, function() {
			if (pFunc) {
				pFunc();
			}
		});
	},

	/* send form data and replace the form element values with the response
	 * data. The response must contain the field data in JSON format
	 *
	 * pForm:   Form to be processed
	 * pUrl:    URL which processes request
	 * pFunc:   optional closure function to be processed after the response.
     */
	replaceFormData: function(pForm, pUrl, pFunc) {
		var dt, d, i, type;
		var s = this.stringFormData(pForm);
		this.getJsonResponse(s, pUrl, function(oData) {
			for (i=0; i<pForm.length; i++) {
				if (typeof(oData[pForm[i].name]) !== 'undefined') {
					pForm[i].value = oData[pForm[i].name];
					if (oData[pForm[i].name]===null) {
						pForm[i].value = '';
					}
					dt = pForm[i].getAttribute('dt');
					if (dt) {
						if (dt === 'date') {
							d = new Date(pForm[i].value);
							pForm[i].value = d.toHumandate();
						}
					}
					type = pForm[i].getAttribute('type');
					if (type === 'checkbox') {
						pForm[i].checked = (Number(pForm[i].value) === 0 ? false : true);
					}
				}
			}
			if (pFunc) {
				pFunc();
			}
		});
	},

	loadJscript: function(pSource) {
		var i, cSrc;
		var oScripts = document.getElementsByTagName('script');
		var found = false;
		for (i=0; i<oScripts.length; i++) {
			cSrc = oScripts[i].getAttribute('src');
			if (cSrc === pSource) {
				found = true;
				break;
			}
		}
		if (!found) {
			var oHead = document.getElementsByTagName('HEAD').item(0);
			var oScript = document.createElement("script");
			oScript.type = "text/javascript";
			oScript.src = pSource;
			oHead.appendChild( oScript);
		}
	},

	unloadJscript: function(pSource) {
		var i;
		var oHead = document.getElementsByTagName('HEAD').item(0);
		var oScripts = document.getElementsByTagName('script');
		for (i=0; i<oScripts.length; i++) {
			if (oScripts[i].getAttribute('src') === pSource) {
				oHead.removeChild(oScripts[i]);
				break;
			}
		}
	}
};


