function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
checkLogin(receiveReq.responseText); 
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
 }
}

function checkLogin(response)
{
 if (response == "Success!") {
   window.location='contact_thankyou.html';
 } else {
  //alert("You have something wrong in your form.");
 }
}
//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';
 //Set up the parameters of our AJAX call
if (theForm.E_TimeofDay1.checked == '1') {theForm.E_TimeofDay1.value ='1'}
if (theForm.E_TimeofDay2.checked == '1') {theForm.E_TimeofDay2.value ='1'}
if (theForm.E_TimeofDay3.checked == '1') {theForm.E_TimeofDay3.value ='1'}
if (theForm.E_TimeofDay4.checked == '1') {theForm.E_TimeofDay4.value ='1'}

if (theForm.F_BestMethod1.checked == '1') {theForm.F_BestMethod1.value ='1'}
if (theForm.F_BestMethod2.checked == '1') {theForm.F_BestMethod2.value ='1'}
if (theForm.F_BestMethod3.checked == '1') {theForm.F_BestMethod3.value ='1'}
if (theForm.F_BestMethod4.checked == '1') {theForm.F_BestMethod4.value ='1'}

if (theForm.K_TypeofService.checked == '1') {theForm.K_TypeofService.value ='1'}
if (theForm.K_TypeofService1.checked == '1') {theForm.K_TypeofService1.value ='1'}
if (theForm.K_TypeofService2.checked == '1') {theForm.K_TypeofService2.value ='1'}
if (theForm.K_TypeofService3.checked == '1') {theForm.K_TypeofService3.value ='1'}
if (theForm.K_TypeofService4.checked == '1') {theForm.K_TypeofService4.value ='1'}
if (theForm.K_TypeofService5.checked == '1') {theForm.K_TypeofService5.value ='1'}
if (theForm.K_TypeofService6.checked == '1') {theForm.K_TypeofService6.value ='1'}
if (theForm.K_TypeofService7.checked == '1') {theForm.K_TypeofService7.value ='1'}
 
 var postStr = theForm.A_FirstName.name + "=" + encodeURIComponent( theForm.A_FirstName.value ) + "&" + theForm.B_LastName.name + "=" + encodeURIComponent( theForm.B_LastName.value ) + "&" + theForm.C_PrimaryPhone.name + "=" + encodeURIComponent( theForm.C_PrimaryPhone.value ) + "&" + theForm.D_EmailAddr.name + "=" + encodeURIComponent( theForm.D_EmailAddr.value ) + "&" + theForm.E_TimeofDay1.name + "=" + encodeURIComponent( theForm.E_TimeofDay1.value ) + "&" + theForm.E_TimeofDay2.name + "=" + encodeURIComponent( theForm.E_TimeofDay2.value ) + "&" + theForm.E_TimeofDay3.name + "=" + encodeURIComponent( theForm.E_TimeofDay3.value ) + "&" + theForm.E_TimeofDay4.name + "=" + encodeURIComponent( theForm.E_TimeofDay4.value ) + "&" + theForm.F_BestMethod1.name + "=" + encodeURIComponent( theForm.F_BestMethod1.value ) + "&" + theForm.F_BestMethod2.name + "=" + encodeURIComponent( theForm.F_BestMethod2.value ) + "&" + theForm.F_BestMethod3.name + "=" + encodeURIComponent( theForm.F_BestMethod3.value ) + "&" + theForm.F_BestMethod4.name + "=" + encodeURIComponent( theForm.F_BestMethod4.value ) + "&" + theForm.G_PreferredCompanion.name + "=" + encodeURIComponent( theForm.G_PreferredCompanion.value ) + "&" + theForm.H_Month2StartService.name + "=" + encodeURIComponent( theForm.H_Month2StartService.value ) + "&" + theForm.H_Day2StartService.name + "=" + encodeURIComponent( theForm.H_Day2StartService.value ) + "&" + theForm.TimeStartService.name + "=" + encodeURIComponent( theForm.TimeStartService.value ) + "&" +theForm.I_Month2EndService.name + "=" + encodeURIComponent( theForm.I_Month2EndService.value ) + "&" + theForm.I_Day2EndService.name + "=" + encodeURIComponent( theForm.I_Day2EndService.value ) + "&" +theForm.TimeEndService0.name + "=" + encodeURIComponent( theForm.TimeEndService0.value ) + "&" + theForm.K_TypeofService.name + "=" + encodeURIComponent( theForm.K_TypeofService.value ) + "&" +theForm.K_TypeofService1.name + "=" + encodeURIComponent( theForm.K_TypeofService1.value ) + "&" +theForm.K_TypeofService2.name + "=" + encodeURIComponent( theForm.K_TypeofService2.value ) + "&" + theForm.TimeStartService1.name + "=" + encodeURIComponent( theForm.TimeStartService1.value ) + "&" +theForm.K_TypeofService3.name + "=" + encodeURIComponent( theForm.K_TypeofService3.value ) + "&" + theForm.TimeStartService2.name + "=" + encodeURIComponent( theForm.TimeStartService2.value ) + "&" +theForm.K_TypeofService4.name + "=" + encodeURIComponent( theForm.K_TypeofService4.value ) + "&" + theForm.K_TypeofService5.name + "=" + encodeURIComponent( theForm.K_TypeofService5.value ) + "&" +theForm.K_TypeofService6.name + "=" + encodeURIComponent( theForm.K_TypeofService6.value ) + "&" + theForm.K_TypeofService7.name + "=" + encodeURIComponent( theForm.K_TypeofService7.value ) + "&" + theForm.data1.name + "=" + encodeURIComponent( theForm.data1.value ) + "&" + theForm.Time1.name + "=" + encodeURIComponent( theForm.Time1.value ) + "&" + theForm.data2.name + "=" + encodeURIComponent( theForm.data2.value ) + "&" + theForm.Time2.name + "=" + encodeURIComponent( theForm.Time2.value ) + "&" + theForm.L_AnythingChanged.name + "=" + encodeURIComponent( theForm.L_AnythingChanged.value ) + "&"+ theForm.TimeEndService1.name + "=" + encodeURIComponent( theForm.TimeEndService1.value ) + "&"+ theForm.TimeEndService01.name + "=" + encodeURIComponent( theForm.TimeEndService01.value ) + "&" + theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //var postStr = theForm.name.name + "=" + encodeURIComponent( theForm.name.value ) + "&" + theForm.email.name + "=" + encodeURIComponent( theForm.email.value ) + "&" + theForm.name1.name + "=" + encodeURIComponent( theForm.name1.value ) + "&" + theForm.email1.name + "=" + encodeURIComponent( theForm.email1.value ) + "&" + theForm.name2.name + "=" + encodeURIComponent( theForm.name2.value ) + "&" + theForm.email2.name + "=" + encodeURIComponent( theForm.email2.value ) + "&" + theForm.name3.name + "=" + encodeURIComponent( theForm.name3.value ) + "&" + theForm.email3.name + "=" + encodeURIComponent( theForm.email3.value ) + "&" + theForm.message.name + "=" + encodeURIComponent( theForm.message.value ) + "&" + theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}
