//Gets the browser specific XmlHttpRequest Object 
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();
var reqtext = '';
//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;
   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");
//alert(updatePage());
   //Make the request
   receiveReq.send(param);
	 //alert(param);
	 //alert('s-'+window.reqtext);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
		//return false;
	 reqtext = receiveReq.responseText;
	 if(reqtext == 'ok')
   {
	 	 document.getElementById('result').innerHTML = '';
		 document.form_contact.submit();
	 }
	 else
	 {
		 document.getElementById('txtCaptcha').value = '';
		 document.getElementById('result').innerHTML = receiveReq.responseText;
	 }
   //return receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
	 //alert('s-'+receiveReq.responseText);
	 return receiveReq.responseText;
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha2.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 return makeRequest(url, postStr);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function loadXMLDoc(url, funcProcess) {
   req = false;
   if(window.XMLHttpRequest) {
      try {
         req = new XMLHttpRequest();
      } catch(e) {
         req = false;
      }
   } else if(window.ActiveXObject) {
      try {
         req = new ActiveXObject('Msxml2.XMLHTTP');
      } catch(e) {
         try {
            req = new ActiveXObject('Microsoft.XMLHTTP');
         } catch(e) {
            req = false;
         }
      }
   }
   if(req) {
      req.onreadystatechange = funcProcess;
      req.open('GET', url, true);
      req.send(null);
   }
}

function show_aff(val, td_id, m, i)
{
	loadXMLDoc(host+"js/affiliate_respond.php?a=-2&val="+val+"&td_id="+td_id+"&m="+m+"&i="+i, aff_back);
}

function aff_back()
{
	if(req.readyState == 0) { document.getElementById('loading').style.display = "block"; } 
	if(req.readyState == 1) { document.getElementById('loading').style.display = "block"; } 
	if(req.readyState == 2) { document.getElementById('loading').style.display = "block"; } 
	if(req.readyState == 3) { document.getElementById('loading').style.display = "block"; } 
	if (req.readyState == 4)
   {
		if (req.status == 200)
      {
         if (req.responseText.length)
         {
            if (req.responseText != '1')
            {
							document.getElementById('loading').style.display = "none";
							var resp=req.responseText.split("|::|");
							show_hide(resp[0], resp[1]);
            }
         }
      }
      else
      {
         alert('There was a problem retrieving the XML data:n' + req.statusText);
      }
   } 
}