﻿/*====================================================================
 * Post a form using AJAX
 * url - the page to handle submit.
 * parameters - the parameters to submit
 * funStr - a script to handle the XMLHttpRequest state change, the XMLHttpRequest Object named "http_request".
 * useHTML - [optional] a boolean determine is output html or not, default is true
 *====================================================================*/
function makePOSTRequest(url, parameters, funcStr) {
	http_request = false;
	var useHTML = (arguments.length > 3) ? arguments[3] : true;
	var startProcessScript = (arguments.length > 4) ? arguments[4] : '';
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			var mineType = ( useHTML ) ? 'text/html' : "text/xml";
			http_request.overrideMimeType( mineType );
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function(){
		switch ( http_request.readyState ){
			case 0:	// request not initialize
				break;
			case 1:	// request has been setup
				if (startProcessScript != '')
					eval(startProcessScript);
				break;
			case 2:	// request has been sent
				break;
			case 3: // processing
				break;
			case 4:	// complete
				eval(funcStr);
				evalScripts( http_request.responseText );
				break;
		}
	};
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

/*====================================================================
 * Form a parameters string from a Form object.
 * obj - a html form.
 *====================================================================*/
function getParamsStr(obj) {
	var getstr = "";
	
	var objList = obj.getElementsByTagName("input");
	if ( objList ){
		for (i=0; i<objList.length; i++) {
			if ((objList[i].type == "text") || (objList[i].type == "hidden") || 
				(objList[i].type == "password")) {
				getstr += (objList[i].name + "=" + objList[i].value + "&");
			} else if ( (objList[i].type == "checkbox") && objList[i].checked ){
				getstr += (objList[i].name + "=" + objList[i].value + "&");
			} else if ( (objList[i].type == "radio") && (objList[i].checked) ){
				getstr += (objList[i].name + "=" + objList[i].value + "&");
			}
		}
	}
	
	var selectList = obj.getElementsByTagName("select");
	if ( selectList ){
		for (i=0; i<selectList.length; i++) {
			
			if (selectList[i].multiple){
				for ( j=0; j< selectList[i].options.length; j++ ){
					if ( selectList[i].options[j].selected ){
						getstr += (selectList[i].name + "=" + selectList[i].options[j].value + "&");
					}
				}
			}else{
				getstr += (selectList[i].name + "=" + selectList[i].value + "&");	
			}
		}
	}
	
	if(obj.getElementsByTagName("textarea")) {
		for (i=0; i<obj.getElementsByTagName("textarea").length; i++) {
			getstr += obj.getElementsByTagName("textarea")[i].name + "=" + 
			escape(encodeURI(obj.getElementsByTagName("textarea")[i].value)) + "&";
		}
	}
	return getstr;
}


/*====================================================================
 * To Submit a form by Ajax
 * from - is a HTML Form Object
 * funcStr - is a javascript to be execute when callback.
 *====================================================================*/
function ajaxFormSubmit( formObj, funcStr ){
	if ( (formObj) && (formObj.tagName == 'FORM')){
		var params = getParamsStr( formObj );
		makePOSTRequest( formObj.action, params, funcStr );
	}
	
	return false;
}


/*====================================================================
 * Get the Node value from a http_request.responseXML
 *====================================================================*/
function getNodeValue( rootNode, nodeName ){
	var node = rootNode.getElementsByTagName( nodeName );
	if ( node ){
		if ( (node.length > 0) && (node[0].firstChild != null) ){
			return node[0].firstChild.nodeValue;
		}
	}
	return ( (arguments.length > 2) ? arguments[2] : '');
}


/*====================================================================
 * Activate the javascript in the script string
 *====================================================================*/
function evalScripts(scripts)
{	
	try
	{	if(scripts != '')	
		{	var script = "";
			scripts = scripts.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
				if (scripts !== null) script += arguments[1] + '\n'; return '';});
			if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
		}
		return false;
	}
	catch(e)
	{
		alert(e);
	}
}

