// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
 {
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;

  // this should work for all browsers except IE6 and older
  try
   {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
   }
  catch(e)
   {
    // assume IE6 or older
	var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
									"MSXML2.XMLHTTP.5.0",
									"MSXML2.XMLHTTP.4.0",
									"MSXML2.XMLHTTP.3.0",
									"MSXML2.XMLHTTP",
									"Microsoft.XMLHTTP");
    // try every prog id until one works
	for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
	 {
	  try
	   {
	    // try to create XMLHttpRequest object
		xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
	   }
	  catch (e) {}
	 }
   }
  // return the created object or display an error message
  if (!xmlHttp) return false;
    else return xmlHttp;
 }

function inArray(value, arr)
{
  for (x in arr)
   if (x == value) return true;

  return false;
}

// called to read a file from the server
function process(mainForm, urlHandler)
{

	// only continue if xmlHttp isn't void
  if (xmlHttp)
  {
  	// try to connect to the server
		try
	 	{
    	usedParams = new Array();
	  	p = urlHandler.search(/\?/ig);
      if (p>0 && ((p+1) < urlHandler.length))
      {
      	paramStr = urlHandler.substr(p+1);
        paramArr = paramStr.split('&');
        for(i=0; i<paramArr.length;i++)
        {
        	pvar = paramArr[i].split('=');
         	usedParams[pvar[0]] = pvar[1];
        }
      }

	  	inputArray = new Array();
	  	inputArray = mainForm.elements;

	  	inputValue = new Array();

	  	for(i=0; i<inputArray.length;i++)
	   	{
	    	//if ( inputArray[i].type == 'file' )
	    	if (((inputArray[i].type != 'checkbox') && (inputArray[i].type != 'radio')) || (inputArray[i].checked))
        {
        	if (!inArray(inputArray[i].name, usedParams))
          	inputValue[i] = encodeURIComponent(inputArray[i].name)+'='+encodeURIComponent(inputArray[i].value);
        }
      }
	  	var query = inputValue.join('&');
	  	//alert(query);
      //for (x in inputValue){alert(inputValue[x]);}
      if (mainForm.method == 'post')
      {
	    	xmlHttp.open("POST", urlHandler, false);
	    	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    	xmlHttp.send(query);
      }
      else
      {
      	p = urlHandler.search(/\?/ig);
        if (p > 0)
        	urlHandler += '&'+query;
        else
        	urlHandler += '?'+query;
	      xmlHttp.open("GET", urlHandler, false);
        xmlHttp.send(null);
      }
			return handleRequestStateChange(mainForm);
		}
		// display the error in case of failure
		catch (e)
		{
			//window.event.returnValue=false;
			return true;
		}
	}
	else
	{
  	return true;
	}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange(mainForm)
 {
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
   {
    // continue only if HTTP status is "OK"
	if (xmlHttp.status == 200)
	 {
	  try
	   {
	    // do something with the response from the server
		return handleServerResponse(mainForm);
	   }
	  catch(e)
	   {
	    // display error message
			return true;
	   }
	 }
	  else
	    {
		 // display status message
		 return true;
		}
	}
	 else
	   {
	    return false;
	   }
 }

// handles the response received from the server

function handleServerResponse(mainForm)
{
try
{  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement) return true;
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror") return true;

  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;

  // testing that we received the XML document we expect
  if (rootNodeName != "response" || !xmlRoot.firstChild) return true;

  status = xmlRoot.childNodes[0].firstChild.data;
  if (status == 'failed')
	{
  	message = xmlRoot.childNodes[1].firstChild.data;
    field = xmlRoot.childNodes[2].firstChild.data;
    alert(message);
		fieldFocus = mainForm[field];
		fieldFocus.focus();
    return false;
	}
  else
	{
		return true;
	}

}
catch(e)
{
	// display error message
	return false;
}
}

/*
function validator_init(formName, urlHandler)
{
 frmE = document.getElementById(formName);
 frmObj = document.forms[formName];

 if (frmE.addEventListener)
     frmE.addEventListener('submit', function () {return process(frmObj, urlHandler);}, false);
 else
     frmE.attachEvent('onsubmit', function () {return process(frmObj, urlHandler);});

}
*/