// JavaScript Document

var img1 = new Image(16, 16);
img1.src="/lib/images/busy.gif";

function check_postcode() {
	
	pc = document.getElementById("postcode").value;

	document.getElementById("pc_go").disabled=true;
		
	var d = Date();
	
	_xmlHttp = getXMLHTTP();
	_xmlHttp.open("GET","/lib/ajax/postcode.php?postcode="+pc+"&time="+d,true);
    
	_xmlHttp.onreadystatechange=function() {
      if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
          // The response text gets executed as javascript... 
          var check = _xmlHttp.responseText;
 		  
		  document.getElementById("pc_go").disabled=false;

 		  document.getElementById("pc_results").style.visibility = 'visible';
		  
		  if(check=="OK") {
			
			 document.getElementById("pc_results").innerHTML = "We deliver to your area!";
		  
		  }else{
			  
			 document.getElementById("pc_results").innerHTML = "Sorry, we don't deliver to your area yet.";
			 
		  }
		  
		  return false;
			  
		  
      }
    }
    ;
    _xmlHttp.send(null)


}
function check_postcode_for_delivery(f) {
	
	
	pc = document.getElementById("post_code").value;
	
	var filter = /[A-z]{1,2}[0-9R][0-9A-z]? [0-9][A-z]{2}/;
	
	var m = filter.test(pc);
	
	if(!m) {
		
		alert("Please provide a valid postcode. Ensure there is a space between the two parts of your postcode.");
		
	}else{
		
		var d = Date();
		
		_xmlHttp = getXMLHTTP();
		_xmlHttp.open("GET","/lib/ajax/postcode.php?postcode="+pc+"&time="+d,true);
		
		_xmlHttp.onreadystatechange=function() {
		  if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
			  // The response text gets executed as javascript... 
			  var check = _xmlHttp.responseText;
			  
			  if(check=="OK") {
				  
				  check_number(f);
	
			  }else{
				  
				  alert("Sorry, we do not delivery to your area, you may not checkout.");
	
			  }
			  
		  }
		}
		;
		_xmlHttp.send(null)
	}

}	

function check_number(f) {
	
	regex = /^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/;
	
	var m = regex.exec(document.getElementById("telephone").value);
	
	if(m==null) {
		
		alert("The phone number you have entered is invalid.\n\nTo ensure your order is processed correctly we require your telephone number.");
		
	}else{
		
		check_time(f);
		
	}
	
}

function check_time(f) {
	
	var d = Date();
	
	var dt = document.getElementById("delivery_date").value;
	
	var hr = document.getElementById("delivery_hour").options[document.getElementById("delivery_hour").selectedIndex].text;
	
	var mn = document.getElementById("delivery_min").options[document.getElementById("delivery_min").selectedIndex].text;
	
	_xmlHttp = getXMLHTTP();

	_xmlHttp.open("GET","/lib/ajax/delivery_time.php?date="+dt+"&time="+hr+":"+mn+"&rand="+d,true);
    
	_xmlHttp.onreadystatechange=function() {
      if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
          // The response text gets executed as javascript... 
          var c2 = _xmlHttp.responseText;

		  if(c2=="check_form(f);") {
			  
			  eval(c2);
			  
		  }else{
			  eval(c2);
			  alert("Sorry we cannot deliver on that date/time because "+err);
			  
		  }
		  
      }
    }
    ;
    _xmlHttp.send(null)

}

function update_clock() {
	
	var d = new Date();
	
	if(document.getElementById("clock_image")) {
		document.getElementById("clock_image").src = "/lib/include/clock.php?d="+d.getTime();
	    window.setTimeout('update_clock()',60000);
	}
}
function change_info() {

	if(document.getElementById("info_hidden")) {
	
		_xmlHttp = getXMLHTTP();
		_xmlHttp.open("GET","/lib/ajax/info.php?next="+document.getElementById("info_hidden").value,true);
		
		_xmlHttp.onreadystatechange=function() {
		  if(_xmlHttp.readyState==4&&_xmlHttp.responseText) {
			  // The response text gets executed as javascript... 
			  var check = _xmlHttp.responseText;
			  
	
	
			  eval(check);
			
			   
			  
			  
			  
		  }
		}
	
		_xmlHttp.send(null)
	}
	
}
