var gUrl="";
var gCtrl="";
var gTimerId=0;
var gIndex=0;
var gSub="";
var gSequence=0;
var gFocus="";
var gWait="";
var gFunction="";

var NavType2="E";
if (navigator.appName == "Netscape") {NavType2="N"};
if (navigator.appName == "Mozilla") {NavType2="N"};

function makeRequest(nIndex,sCtrl,sSub,sDelay,sFocus,sMinChars,sWait,sFunction) {
      
     		var nSequence=0;
 
     		if (sMinChars > 0)
     		{
     			var fld=document.getElementById(sFocus);
     		//	alert("MinChars= " + sMinChars + " " + sFocus + " Length= " + fld.value.length);
     			if (fld.value.length > 0 && fld.value.length < sMinChars)
     			{
     				return false;
     			}
     		}
     		
    		if (sWait != null)
     		{
     			var cWait=document.getElementById(sWait);
     			var sControl=document.getElementById(sFocus);
     			if (cWait != null)
     			{
     				cWait.style.display='block';
     			}
     		}

	      if (sDelay == ""){sDelay=300};
	      if (NavType2 == "E" && sDelay != 0)
	     	{
		       if (gTimerId > 0){clearTimeout(gTimerId)};
		       gIndex=nIndex
		       gCtrl=sCtrl;
		       gSub=sSub;
		       gWait=sWait;
		       gSequence++;
		       nSequence=gSequence;
		       gFocus="";
		       gFunction="";
		       if (sFunction != null){gFunction=sFunction};
		       if (sFocus){gFocus=sFocus}
		       var sString="setTimeout('RunRequest(" + gIndex + ",\"" + gCtrl + "\",\"" + gSub + "\"," + nSequence + ",\"" + gFocus + "\",\"" + gWait + "\",\"" + gFunction + "\")'," + sDelay + ")";
		       gTimerId=eval(sString);
	      }
	      else
	    	{
	  			// firefox 1.0 -> 1.5 doesn't like setTimeout requests......
	  			gSequence++;
		      nSequence=gSequence;
	  			RunRequest(nIndex,sCtrl,sSub,nSequence,sFocus,sWait,sFunction);
	   		}
   }
   
 function RunRequest(nIndex,sCtrl,sSub,nSequence,sFocus,sWait,sFunction) {
 				var sUrlString=window.location.pathname;
       	if (nIndex > 0)
 				{
       		sUrlString=LData[nIndex];
        }
        var UrlData=sUrlString.split("\?");
	 			var sUrl=UrlData[0];
	 			var sParams=UrlData[1];
       	
        var httpRequest;
			

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
               // httpRequest.overrideMimeType('text/xml');
                httpRequest.overrideMimeType('text/plain');
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        var PostData=createpoststring(sCtrl,sSub,sParams);
        httpRequest.onreadystatechange = function() { alertContents(httpRequest,sCtrl,nSequence,sFocus,sWait,sFunction); };
        httpRequest.open('POST',sUrl,true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.send(PostData);
}

function RunManRequest(sUrl,sParams,sCtrl,sWait) {
       // var sCtrl='';
        var sSub='';
        var nSequence=1;
        var sFocus='';

        var httpRequest;
        
     		if (sWait != null)
     		{
     			var cWait=document.getElementById(sWait);
     			if (cWait !=null)
     			{
     				cWait.style.display='block';
     			}
     		}
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
               // httpRequest.overrideMimeType('text/xml');
                httpRequest.overrideMimeType('text/plain');
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        var PostData=sParams;
        httpRequest.onreadystatechange = function() { alertContents(httpRequest,sCtrl,nSequence,sFocus,sWait); };
        httpRequest.open('POST',sUrl,true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.send(PostData);
}

function alertContents(httpRequest,sCtrl,nSequence,sFocus,sWait,sFunction) {

   try {
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
	
           			
           			if (nSequence >= gSequence) // only return the latest request 
           			{
           				var aControls = sCtrl.split('|');
           				var sText = httpRequest.responseText;
           				sText=sText.replace('<?xml version="1.0" ?>','');
           				var aHtml = sText.split('<ajaxrjbw>');
           				if (sCtrl > '')
           				{
	           				for (nItem in aControls)
	           				{
	           					  
	           					  var DivName=aControls[nItem] + "_Div1";
	           					  var sStartDiv="<div id='" + DivName + "'>";
	           					// alert("Control=" + DivName);
	           					  aHtml[nItem]=aHtml[nItem].replace(sStartDiv,'');
	           					  aHtml[nItem]=aHtml[nItem].replace(/<\/div>$/,'');
	           					 // alert ("Ajax=" + aHtml[nItem]);
	           				 	 // alert ("Div= " + DivName + " Add Ajax " + aHtml[nItem]);
	           					 	document.getElementById(DivName).innerHTML =aHtml[nItem];
	           				}

 									}
           				if (sFunction !=null)
           				{
										if (sFunction > "")
										{
											//alert ("Function = " + sFunction);
											eval(sFunction + "()");
										}
           				}           		
           				if (NavType2 == "E" && sFocus )
           				{
           					var sControl=document.getElementById(sFocus);
           					if (sControl)
           					{
           						eval("window.setTimeout('document.getElementById(\"" + sFocus + "\").focus()', 1)");

           					}
           				}

           			}
           			UnSetWait(sWait);
            }
            else
            {
              alert('There was a problem with the request. ' + httpRequest.status ); // + ' ' + httpRequest.responseText );
            	UnSetWait(sWait);
            }
        }

   	}     
     catch( e )
     {
           alert('Caught Exception: ' + e.deFunctionion);
           UnSetWait(sWait);
     }
}

function UnSetWait(sWait)
{
		if (sWait != null)
		{
			var cWait=document.getElementById(sWait);
			if (cWait !=null)
			{
				cWait.style.display='none';
			}
		}	
}
  
function createpoststring(sCtrl,sSub,sParams)
{
	var strArr = "Ajax=" + sCtrl + ":" + sSub + "&";
	for(i=0; i< document.cgiform.elements.length; i++)
	{
			strArr += document.cgiform.elements[i].name + '=' + encodeURI(document.cgiform.elements[i].value) + '&';
	}
	if (sParams)
	{
		strArr += sParams + '&';
	}
	var sEvent=document.getElementById('event');	
	if (sEvent != null)
	{
		sEvent.value="";
	}	
	return strArr 
}

