var gLoadStatusText = "";
var gBustCacheParameter = "";
var gServURL = "";//The background service URL;
var gLoadedObjects = "";//The loaded fileName;
var gPageLoadedFlag = false;//Set the flag mark that if the backURL loaded complete!
/**
*post the data to server.
*@param postDataStr the param that need post to server side .
*@param servURL the server side url .
*@param reqMethod the method is "post" or "get" .
*@param asyncFlag the flag that synchrouse or asynchrouse.
*@param promoteFlag the flag that if show the promote info.
*@param nextActionID appointed the next action ID.
*
*@author:junny
*@version:1.0
*@since:2006-07-17
*/

function doNextAction(responseText,nextActionID) {
	gResultData = responseText;	
	eventManagerDoNextAction(nextActionID);
}

function postXMLHttp(postDataStr,servURL,reqMethod,asyncFlag,promoteFlag,nextActionID) {	
	//the background servURL.	
	gServURL = servURL;	
	var ajaxReq = newXMLHttpRequest();	
	ajaxReq.open(reqMethod, servURL, asyncFlag);
	ajaxReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	if (promoteFlag) {	
		setTimeout("showLoadingLater()",1000);		
	}
	ajaxReq.send(postDataStr);				
	ajaxReq.onreadystatechange = getReadyStateHandler(ajaxReq,doNextAction,promoteFlag,nextActionID);						
}


/*
 * Returns an new XMLHttpRequest object, or false if the browser
 * doesn't support it
 */
function newXMLHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");      
	    } catch (e1) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				xmlreq = false;
			}
		}
	}
	return xmlreq;
}

/**
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes it XML response to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 * @param req the xmlHttpRequest object.
 * @param responseXmlHandler the callback function.
 * @param promoteFlag if show the posting status.true show,otherwise hidden.
 * @param nextActionID do the next action.
 */
 function getReadyStateHandler(req, responseXmlHandler, promoteFlag,nextActionID) {
	return function () {				
		if (req.readyState == 4) {
			if (req.status == 200) {
				gPageLoadedFlag = true;
				Element.hide('loadingDiv');				
				//need the extended file (ie:listMail.js or listMail.css)				
				loadExtendedFile();							
				responseXmlHandler(req.responseText,nextActionID);
				dhtmlHistory.add("a"+new Date().getTime(),req.responseText);
			} else {
				alert("HTTP error "+req.status+": "+req.statusText);
			}
		}
	}
}
 
 
 /**
  *Load the extended js file and css file.
  *when the "gSubURL" is not null ,and the "gLoadedObjects" is not null;
  */
 function loadExtendedFile(){ 
	var fileName = gServURL.split(".")[0];
	if (gServURL != "" && gServURL.indexOf(".") != -1 && gLoadedObjects.search(fileName) == -1) {
		var head = document.getElementsByTagName("head")[0];
		var ajaxReq = newXMLHttpRequest();	
		var scriptText;
		ajaxReq.open("GET", gUrlInfoObj.mainURL+"js/"+fileName+".js", false);
		ajaxReq.send(null);
		if (ajaxReq.readyState == 4) {
			if (ajaxReq.status == 200) {
				scriptText = ajaxReq.responseText;		
			} 
		} 			

		var jsFileItem = document.createElement("script");
		jsFileItem.language = "javascript"; 
		jsFileItem.type = "text/javascript"; 
		jsFileItem.id = "listMailJSaaa"; 		
		jsFileItem.text = scriptText; 
		//alert(jsFileItem.text);
		document.body.appendChild(jsFileItem);
	
		var cssFileItem = document.createElement("link");
		cssFileItem.setAttribute("rel", "stylesheet");
		cssFileItem.setAttribute("type", "text/css");
		cssFileItem.setAttribute("href", gUrlInfoObj.mainURL+"css/mode_"+gUrlInfoObj.userStyle+"/"+fileName+".css");

		document.getElementsByTagName("head").item(0).appendChild(cssFileItem);
		gLoadedObjects+=fileName+" "; //Remember this object as being already added to page		
	}	

}

/**
 * After post the data,2 second later,show the "loadingDiv".
 */
function showLoadingLater() {
	var loadingDivObj = window.document.getElementById("loadingDiv");
	if (loadingDivObj.style.display == "none" && !gPageLoadedFlag)
		Element.show('loadingDiv');	
}
 

