var timeoutId, request;
var box_element;
var queryString;
var which_get = "search"
old_results = "";
function handleCheck(){
	  if(request.readyState == 4){
   		 clearTimeout(timeoutId);
    	if(request.status == 200){
      		//Implement document object in DOM
      		xmlReturnVal = request.responseText;
					document.getElementById("mail_box").innerHTML = xmlReturnVal;
					document.getElementById("loading_box").style.display = "none";
					document.getElementById("mail_box").style.display = "block";
     } else {   
      alert("A problem occurred with communicating between the XMLHttpRequest object and the server program. " + request.status);
    }
  }//end outer if
}
 function warn(){
   request.abort();
   alert("A problem occurred with communicating with the server program. Please make sure you are connected to the Internet and try again in a few moments.");
 }

function initReq(reqType,url,bool){
    try{
        /* Specify the function that will handle the HTTP response */
       request.onreadystatechange=handleCheck; 
        request.open(reqType,url,bool);
		 request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    	request.send(null);
    } catch (errv) {

        alert(
                "The application cannot contact the server at the moment. "+
                "It may be a problem with cross-domain restrictions." );
    }
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
function httpRequest(reqType,url,asynch) {
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initReq(reqType,url,asynch);
    }  else {
       alert("Your browser does not permit the use of all "+
       "of this application's features!");
			 }
}

function ajax_mail() {
subject = "Big O Contact (mytirestore.com)";
name =  document.frmContact.Name.value;
address = document.frmContact.Address.value;
city = document.frmContact.City.value;
zip = document.frmContact.Zip.value;
state = document.frmContact.State.value;
phone = document.frmContact.Phone.value;
fax = document.frmContact.Fax.value;
comments = document.frmContact.Comments.value;
storename = document.frmContact.Storename.value;
mailto = "info@mytirestore.com,bizseln7@frontier.com";
mailfrom = document.frmContact.Email.value;
queryString = "&name="+encodeURIComponent(name)+"&address="+encodeURIComponent(address)+"&city="+encodeURIComponent(city)+"&zip="+encodeURIComponent(zip)+"&phone="+encodeURIComponent(phone)+"&fax="+encodeURIComponent(fax)+"&comments="+encodeURIComponent(comments)+"&storename="+encodeURIComponent(storename)+"&subject="+encodeURIComponent(subject)+"&mailfrom="+encodeURIComponent(mailfrom)+"&state="+encodeURIComponent(state);
httpRequest("GET","includes/sndmail.php?mailto="+encodeURIComponent(mailto)+queryString,true);
document.getElementById("mail_box").style.display = "none";
document.getElementById("loading_box").style.display = "block";
}

