﻿var xmlHttp, errorReportDiv

function callServer(cMethod, cURL, cFunction, cPara)
	//cMethod: GET or POST
	//cURL: The server's URL
	//cFunction: Function to run upon reply from server.
	//cPara: The parameters to send to server
	{
	var cBrowser = navigator.userAgent.toLowerCase()
	try
		{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
	catch (e)
		{
		// Internet Explorer
		try
			{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e)
			{
			try
				{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e)
				{
				alert("Your browser does not support AJAX!");
				return false;
				}
			}
		}

		xmlHttp.onreadystatechange=function()
			{
			if(xmlHttp.readyState==4)
				{
				var cText = xmlHttp.responseText, nVFP = cText.indexOf("VFP_Error@"), 
					nFOX = cText.indexOf("FOXISAPI call failed"), nLength
				if (nVFP > -1 || nFOX > -1)
					{
					alert(cText)
					}
					else
					{
					if (cFunction)
						{
						eval(cFunction)
						}
						else
						{
						alert(cText);
						}
					}
				}
			}

	if (cMethod.toLowerCase() == "get")
		{
		cURL = cURL + "?" + cPara + "&isAJAX=1"
		xmlHttp.open(cMethod,cURL,true);
		//IE, when under facebook application, will not pass any cookie and document.cookie is empty. This generate error io setRequestHeader.
		//So !document.cookie, no setRequestHeader.
		if (document.cookie)
			{
			xmlHttp.setRequestHeader("COOKIE",document.cookie)
			}
		xmlHttp.setRequestHeader("AJAX","YES")
		xmlHttp.send(null);
		}
		else
		{
		cPara = cPara + "&isAJAX=1"
		//Safari needs to have false cBrowser.indexOf("safari") == - 1
		xmlHttp.open(cMethod,cURL,cBrowser.indexOf("safari") == - 1);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", cPara.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.setRequestHeader("AJAX","YES")
		//IE, when under facebook application, will not pass any cookie and document.cookie is empty. This generate error io setRequestHeader.
		//So !document.cookie, no setRequestHeader.
		if (document.cookie)
			{
			xmlHttp.setRequestHeader("COOKIE",document.cookie)
			}
		xmlHttp.send(cPara);
		}

	}

