//BASIC AJAX SCRIPT

//**************************************************************
//Creates an xml connection object based on browser (Microsoft vs. Everyone Else)
//**************************************************************
function CreateConnectionObject(){
	var co = null;
	if(IsValidBrowser) {
		co=(window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	return co;
}

//Create our Ajax object
var connection = CreateConnectionObject();

//**************************************************************
//Function to use when sending out POST requests
//Send in the connection object, the url to hit, and the paramstring
//You can use the included
//**************************************************************
function SendPOSTRequest(urlstring, paramstring) {
	if(!IsValidBrowser || connection==null) return;
	//prompt("STRING", paramstring);
	connection.open("POST", urlstring, true);
	connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	connection.onreadystatechange=ResponseHandler;
 	connection.send(paramstring);
}


//**************************************************************
//Handles the various states of the connection
//**************************************************************
function ResponseHandler() {
    //A valid response has come back.
   
    var doctype="None";
    if(connection.readyState == 4 && connection.status==200){
		doctype = connection.getResponseHeader("content-type");
		if(doctype==null) doctype="TEXT/HTML";
		if(doctype.toUpperCase().indexOf("TEXT/HTML") >= 0) {
      		TextHandler(connection.responseText);
			return;
		}
		if(doctype.toUpperCase().indexOf("TEXT/XML") >= 0) {
			XMLHandler(connection.responseXML);
			return;
		}
		//Fail if returned document is not one of these two types
		ErrorHandler("The server is busy and cannot complete your request. Please try again. UNK");
		return;
    }

   //The url did not exist
    if(connection.readyState == 4 && connection.status==404){
    	ErrorHandler("The server is busy and cannot complete your request. Please try again. 404");
    	return;
    }

   //Server side execution error
    if(connection.readyState == 4 && connection.status==500){
 		ErrorHandler("The server is busy and cannot complete your request. Please try again. 500");
 		return;
    }
    
    //alert("DBG: " + connection.readyState + ":" + connection.status);
}

//**************************************************************
//Handle a text response. This is just "text." If you have an html fragment
//you could just stuff the response into a  container with InnerHTML 
//**************************************************************
function TextHandler(txt) {
switch(sMode) {
    case "EMAILTRIP":
    finishEmail(txt);
    break;
    
    case "TS":
    $("PopUp").innerHTML = txt;
    break;
    
    case "ZOOM":
    processZoom(txt);
    break;
}
}


//**************************************************************
//Handle errors
//**************************************************************
function ErrorHandler(txt) {
    isSending = false;
	//alert("A problem occurred while processing your request. Please refresh the page.");
}

//**************************************************************
//Utility function to create a paramstring to send to the AJAX 
//POST function. Removes URIEncoding each value forthe GET. Is to be 
//used for the Ajax POST function which takes the a name=value&
//type string in the send parameter. Assumes 1 form on the page.
//**************************************************************
function buildPostString() {
  theForm = document.forms[0];
  var ps = ''
  for (e=0;e<theForm.elements.length;e++) {
  	  if(theForm.elements[e].type == "radio") {
	  		if(theForm.elements[e].checked==true) {
	      		ps+=(ps=='')?'':'&';
      			ps+=theForm.elements[e].name+'='+ theForm.elements[e].value;  		
	  		}
		}
	else {
	  if(theForm.elements[e].name != "__VIEWSTATE" & theForm.elements[e].name !="__EVENTVALIDATION") {
        ps+=(ps=='')?'':'&';
        ps+=theForm.elements[e].name+'='+ theForm.elements[e].value;
      }
    }
   }
  return ps
}
