var xmlHttp
var url = null;	//string variable used to hold post to url
var AjaxSpan = "AJAXSection";
var postChange = false;
var evalString = false;

function sendFormSpan(SFSform, SFSspan)
{

	AjaxSpan = SFSspan;
	sendForm (SFSform);
}

function sendFormSpan2(SFSform, SFSspan, SFURL)
{
	url=SFURL;
	
	AjaxSpan = SFSspan;
	
	sendForm (SFSform);
}


function sendForm(form)
{
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"../ajax-loader.gif\">
	
	
	document.getElementById('LoadImg').innerHTML="<img src=\"../ajax-loader2.gif\">";
	//if valid form is passed pull the parameter from that form and same into variable parms
	if (form != null)
	{
		//alert("init");
		var parms = getFormValues(form,"validate")
	}
	else
	{
		//alert ("form is null");
	}
	
	
	//gets http object for users browser (ie, firefox, safari)
	xmlHttp=GetXmlHttpObject()

	//checks to see if the browser support http request.  If it does not exti script
	if (xmlHttp==null)
  	{
  		alert ("Browser does not support HTTP Request")
  		return
  	}
	
	//check that the url variable has been set.  if it has not exit script.
	if (url==null)
	{
		alert ("Destination url not set")
		return
	}  	 
		
	//send xmlhttp request
	xmlHttp.onreadystatechange=stateChanged	//register the function to use when state has changed
	xmlHttp.open("POST",url,true)	//open url and set data pass method to post
	
	//if a valid form has been past to this method the parameter option will be set and posted to the url set above
	if (form != null)
	{

		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", parms.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parms)	//send parameters to the url set above
	}
	//if a null form has been passed to this method null parameters will be posted to the url set above
	else
	{
		xmlHttp.send(null)	//send null to the url set above
	}
} 

//function to call when state has changed
function stateChanged() 
{ 
	//when the url has finished processing the output will be posted in the span tag "AJAXSection"
	//This tag must be setup in the html
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 		document.getElementById("LoadImg").innerHTML= "";
 				
 		if(postChange)
		{
			alert(xmlHttp.responseText); 
			postChange=false;
		}
		else
		{
	 		try 
	 		{
	 			//alert (AjaxSpan);
	 			document.getElementById(AjaxSpan).innerHTML=xmlHttp.responseText;
	 		}
	 		catch (err)
	 		{
				alert ("ajax document writer failed \n "+err+xmlHttp.responseText);
	 		}
	 		
	 		if(evalString != false)
	 		{
	 			eval(evalString);
	 			evalString = false;
	 		}
		}
 	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
 	{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e)
 	{
 		// Internet Explorer
 		try
  		{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e)
  		{
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	
	return xmlHttp;
}

function getFormValues(fobj,valFunc) 
{ 
   var str = ""; 
   var valueArr = null; 
   var val = ""; 
   var cmd = ""; 
      
   for(var i = 0;i < fobj.elements.length;i++) 
   { 
   		
       switch(fobj.elements[i].type) 
       { 
           case "password": 
           case "text": 
           
                if(valFunc) 
                { 
                    //use single quotes for argument so that the value of 
                    //fobj.elements[i].value is treated as a string not a literal 
                    cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
                    
                    //val = eval(cmd)
                    val = cmd; 
                }
                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
                 break; 
           case "select-one": 
                str += fobj.elements[i].name + 
                "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&"; 
                break;
           case "hidden": 
          
                if(valFunc) 
                { 
                    //use single quotes for argument so that the value of 
                    //fobj.elements[i].value is treated as a string not a literal 
                    cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
                    //val = eval(cmd)
                    val = cmd; 
                }
                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
                 break; 
            case "select-multiple": 
            	
               if (!fobj.elements[i].disabled) {
               
                     for (var j = 0; j < fobj.elements[i].length; j++) {
                         var optElem = fobj.elements[i].options[j];
                         if (optElem.selected == true) {
                         
                             str += fobj.elements[i].name + "=" + encodeURIComponent(optElem.value) + "&";
                             alert (str);
                         }
                     }
                 }
                
                 break;
           case "radio":
           
           		if (fobj.elements[i].checked == true)
           		{
           			if(valFunc) 
	                { 
	                    //use single quotes for argument so that the value of 
	                    //fobj.elements[i].value is treated as a string not a literal 
	                    cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
	                    //val = eval(cmd)
	                    val = cmd; 
	                }
	                
	                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
           		}
           		
           		break;
           case "checkbox": 
        	   
               if(fobj.elements[i].checked == true)
               {
	                if(valFunc) 
	                { 
	                    //use single quotes for argument so that the value of 
	                    //fobj.elements[i].value is treated as a string not a literal 
	                    cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
	                    //val = eval(cmd)
	                    //alert (fobj.elements[i].value+" "+fobj.elements[i].name);
	                    val = cmd; 
	                }
	                
	                str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
               }
                
       } 
   } 

   str = str.substr(0,(str.length - 1));
   return str; 
}
