//BASIC AJAX SCRIPT

//Make sure the browser is not completely ancient
var isvalidbrowser = (document.getElementById) ? true:false;
var emailurl = "http://" + window.location.host + "/EmailOther.aspx";

//**************************************************************
//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();


//**************************************************************
//A function to abort the request
//Have to set onreadystatechange to null or it still runs off
//To the event handler. Abort apparently sets the readystate to 4
//And then the event handler bombs because there are no headers
//in the non-response.
//**************************************************************
function ClearRequest() {
	if(connection.readyState != 4 && connection.readyState != 0) {
		connection.onreadystatechange=null;
		connection.abort();
	}
}

//**************************************************************
//Function to use when sending out GET requests
//Send in the connection object, the url to hit
//**************************************************************
function SendGETRequest(urlstring) {
	if(!isvalidbrowser || connection==null) return;
	connection.open("GET", urlstring, true);
	connection.onreadystatechange=ResponseHandler;
	connection.send(null);
}


//**************************************************************
//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;
	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.");
		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.");
    }

   //Server side execution error
    if(connection.readyState == 4 && connection.status==500){
 		ErrorHandler("The server is busy and cannot complete your request. Please try again.");
    }
}


//**************************************************************
//Handle XML document. Probably punt various response types off to their own
//handler functions. The xmldoc is a fully qualified xml doc that can be manipulated
//with the DOM.
//**************************************************************
function XMLHandler(xmldoc) {
	//Do xml stuff here

}


//**************************************************************
//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) {
    document.getElementById("Wait").innerHTML = txt;
}

//**************************************************************
//Handle errors
//**************************************************************
function ErrorHandler(txt) {
	alert(txt);
}

//**************************************************************
//Utility function to create a querystring from a form. To be 
//used for the Ajax GET function which takes the a name=value&
//type string in the send parameter. Assumes 1 form on the page.
//**************************************************************
function buildQueryString() {
  theForm = document.forms[0];
  var qs = ''
  for (e=0;e<theForm.elements.length;e++) {
	  if(theForm.elements[e].type == "radio") {
	  		if(theForm.elements[e].checked==true) {
	      		qs+=(qs=='')?'':'&'
      			qs+=theForm.elements[e].name+'='+ encodeURIComponent(theForm.elements[e].value)  		
	  		}
		}
	else {
      qs+=(qs=='')?'':'&'
      qs+=theForm.elements[e].name+'='+ encodeURIComponent(theForm.elements[e].value)
     }
    }
  return qs
}

function buildEmailString() {
    qs = "DataTitle=" + document.forms[0].DataTitle.value;
    qs+= "&DataText=" +  document.forms[0].DataText.value;
    qs+= "&email=" + document.forms[0].email.value;
    
    return qs;
}

//**************************************************************
//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 {
      ps+=(ps=='')?'':'&';
      ps+=theForm.elements[e].name+'='+ theForm.elements[e].value;
    }
   }
  return ps
}
