function check_country(){
	if(document.getElementById('country_id_search').options[document.getElementById('country_id_search').selectedIndex].value ==""){
		document.getElementById('postcode_example').innerHTML = 'Please select a country.';
		document.input_search.postcode.disabled = true;
	}else{
		switch(document.input_search.country.options[document.input_search.country.selectedIndex].value){
			case "au":
			document.getElementById('postcode_example').innerHTML = 'Enter your 4 digit postcode.';
			break;
			case "ca":
			document.getElementById('postcode_example').innerHTML = 'Enter the first part of your postal code. e.g. K1A';
			break;
			case "ie":
			document.getElementById('postcode_example').innerHTML = '';
			break;
			case "nz":
			document.getElementById('postcode_example').innerHTML = 'Enter your 4 digit postcode.';
			break;
			case "za":
			document.getElementById('postcode_example').innerHTML = 'Enter your 4 digit post code.';
			break;
			case "gb":
			document.getElementById('postcode_example').innerHTML = 'Enter the first part of your post code. e.g. SS2';
			break;
			case "us":
			document.getElementById('postcode_example').innerHTML = 'Enter your 5 digit zip code.';
			break;
			
			default:
			document.getElementById('postcode_example').innerHTML = '';
			break;		
		}
		document.input_search.postcode.disabled = false;		
	}
}

function check_search(){
	cont=true;
	if(document.getElementById('country_id_search').options[document.getElementById('country_id_search').selectedIndex].value ==""){
		alert_text = "Please select a country and a postcode.";
		cont=false;
	}else if(document.getElementById('postcode_selected_search').value == ""){
		alert_text = "You have entered an invalid postcode / zipcode."; 
		cont=false;
	}
	
	if(cont==false){
		alert(alert_text);					
	}else{
		checkPostcode(document.getElementById('postcode_selected_search').value, document.getElementById('country_id_search').options[document.getElementById('country_id_search').selectedIndex].value);
	}
}

var xmlHttp;
function getPostcode_search(postcode,country){ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="get_postcode.php";
	url=url+"?p="+postcode;
	url=url+"&c="+country;
	url=url+"&f=small";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=postcodeChanged_search;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function postcodeChanged_search(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	 	outputText=xmlHttp.responseText;
	 	if(outputText==''){	
	 	}else{
	 		document.getElementById("postcode_found_content_search").innerHTML='';
		  document.getElementById("postcode_found_content_search").innerHTML=xmlHttp.responseText;
		  document.getElementById('postcode_found_content_search').style.visibility = 'visible';
			document.getElementById('postcode_found_content_search').style.display = 'block';
		}
	} 
}

var xmlHttp;
function checkPostcode(postcode,country){ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="check_postcode.php";
	url=url+"?p="+postcode;
	url=url+"&c="+country;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=checkPostcode_search;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function checkPostcode_search(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	 	outputText=xmlHttp.responseText;
	 	if(outputText=='false'){	
	 		alert("You have entered an invalid postcode / zipcode.");
	 	}else{
	 		document.input_search.submit();
	 	}
	 	
	} 
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	//Internet Explorer
 		try{
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
		catch (e){
		 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	 }
	}
	return xmlHttp;
}

function select_postcode_search(selectedItem){
	document.getElementById('postcode_selected_search').value = document.getElementById('postcode_found_search').options[selectedItem].value;		
	document.getElementById('postcode_found_content_search').style.display = 'none';
	document.getElementById('postcode_found_content_search').style.visibility = 'hidden';	
}

window.onload = function() {
	check_country();
}