var self = this;
var ajaxtimer;
var myResendButton='<input class="button" type="button" onclick="doResend();" value="Resend Automatically"/><br />Please, <b> do not</b> do this if you have already tried, but your documents still haven\'t been received - simply continue to fill in the form and our team will help you.';  

function doResend(){
 var xmlHttpReq = false;
  ajaxtimer = document.getElementById('imgAjax');
  ajaxtimer.style.visibility='visible';
  var url = "prox_respool.php?";
  url += "e="+document.getElementById('email').value;
  url += "&q=" + document.getElementById('policyno').value;
  //document.write(url);
  //return false;
// Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open("GET", url, true);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
		if (self.xmlHttpReq.responseText.toLowerCase().indexOf('done')!=-1){
		window.location = 'autoresend_done.php';
	      }
	    else
	     {
		alert('We couldn\'t automatically re-send because of a problem. \n\n Please fill the details below and out customer support team will help you personally.');
	      
	      o=document.getElementById('auto');
	      o.style.visibility='hidden';
	      ajaxtimer.style.visibility='hidden';
	     }
	}
}
  self.xmlHttpReq.send('');
 
}

function doSurname(sPolicy, sSurname, myEmail, divTarget){
  var xmlHttpReq = false;
  ajaxtimer = document.getElementById('imgAjax');
  ajaxtimer.style.visibility='visible';
  var url = "prox_respool.php?a=1";
  url += "&q=" + sPolicy;
// Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open("GET", url, true);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
		handleSurnameResponse(myEmail,sSurname.toLowerCase(),self.xmlHttpReq.responseText);
	}
}
  self.xmlHttpReq.send('');
 
}
 
function handleSurnameResponse(myEmail, mySurname, retVal){
var o=document.getElementById('auto');
retVal = retVal.toLowerCase();
	if (retVal.indexOf('failed')==-1) {
		var items = retVal.split(";");
		if (items.length==2) {
			if (mySurname.indexOf(items[1])!=-1) {
				var theText='Please click if you would like us to try to resend your documents in the name of <b>"'+ items[1].toUpperCase() +'"</b> automatically to <b>' + myEmail +'</b>'+myResendButton;		
			o.innerHTML = theText;
		        o.style.visibility = 'visible';
		
			} else {
			o.innerHTML = '<b>IMPORTANT:</b> Please enter your postcode into your message - it will help the team to locate your records.'; 
			o.style.visibility = 'visible';
			}
		}		
	}
 	
		ajaxtimer.style.visibility = 'hidden';
}

