/*
 * showing suburbs with the help of postcode Form JavaScript
 *
 * Written by Muhammad Hassan Faroooqi @ COMSYS
 * Date:   06/02/2007
 * Client: K5
 */

  

function countryChange(){
		if(document.getElementById("currentCountry").value=='AUS')
			{
				//$("select#homeCode").removeAttr("disabled");
				//alert(document.getElementById("currentCountry").value);
				var x = document.getElementById("homeCode");
				
				x.style.visibility = "visible";
				var y = document.getElementById("homeLab");
				
				y.style.visibility = "visible";
			}
			else
			{
				var x = document.getElementById("homeCode");
			x.style.visibility = "hidden";
			var y = document.getElementById("homeLab");
			y.style.visibility = "hidden";
			}
		
	}
	

function suburbChange(){
						  	var x = document.getElementById("Suburb");
							x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
							
							
							
					  
						  }
						  
function mailSuburbChange(){
						  	var x = document.getElementById("Mail_Suburb");
							x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
							
					  
						  }
						  
function billingSuburbChange(){
						  	var x = document.getElementById("Billing_Suburb");
							x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
							
					  
						  }
						  

function removeOption(){
		var x = document.getElementById("Suburb")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}
	function removeOptionMail(){
		var x = document.getElementById("Mail_Suburb")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}

function removeOptionBilling(){
		var x = document.getElementById("Billing_Suburb")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}
///////////////////states handling///////////
function removeStateOption(){
		var x = document.getElementById("State")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}

function removeStateOptionMail(){
		var x = document.getElementById("mailState")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}
	
	function removeStateOptionBilling(){
		var x = document.getElementById("billingState")
		for(var i =0;i<x.length;i++)
		{
			x.remove(i);	
		}
	}

/////////////////////

$(document).ready(function(){
						    
							
							
							$("input#PostCode").change(function(){
																					
											$.post("postcode.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
													//loading of suburbs in Mailing Address  section
												$("input#Mail_PostCode").attr("value",$("input#PostCode").attr("value"));
												$("input#Billing_PostCode").attr("value",$("input#PostCode").attr("value"));
												$("select#Billing_Suburb > option").each(function(i) {
														removeOptionBilling();
																							  }
																							  
																			   );
												
												$("select#Mail_Suburb > option").each(function(i) {
														removeOptionMail();
																							    }
																							  
																			        );
																
																$("select#Mail_Suburb").append(data);
																$("select#Billing_Suburb").append(data);
																
																//loading of suburbs in Address section
								   				$("select#Suburb > option").each(function(i) {
														removeOption();
																							  }
																							  
																			   );
												
																$("select#Suburb").append(data);
																var x = document.getElementById("Suburb");
											x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
											var x = document.getElementById("Mail_Suburb");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																var x = document.getElementById("Billing_Suburb");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
											
																}
											);
											
										/////////////////
										       $.post("state.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
											
											
																
																
											
													//loading of suburbs in Mailing Address  section
												
								   				$("select#State > option").each(function(i) {
														removeStateOption();
																							  }
																							  
																			   );
												
																$("select#State").append(data);
												
											
																				
																
												$("select#mailState > option").each(function(i) {
														removeStateOptionMail();
																							    }
																							  
																			        );
												
												
																$("select#mailState").append(data);
																
																$("select#billingState > option").each(function(i) {
														removeStateOptionBilling();
																							    }
																							  
																			        );
												
												
																$("select#billingState").append(data);
																
																var x = document.getElementById("State");
											x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
											var x = document.getElementById("mailState");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																var x = document.getElementById("billingState");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
											
																}
											);
										///////////////////
							
							
						   }
				 );
							$("input#Mail_PostCode").change(function(){
											$.post("postcode.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
											$("input#Billing_PostCode").attr("value",$("input#Mail_PostCode").attr("value"));
								   				$("select#Mail_Suburb > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeOptionMail();
																							    }
																							  
																			        );
												$("select#Billing_Suburb > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeOptionBilling();
																							    }
																							  
																			        );
												$("select#Billing_Suburb").append(data);
																$("select#Mail_Suburb").append(data);
																var x = document.getElementById("Mail_Suburb");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																var x = document.getElementById("Billing_Suburb");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																
																}
											);
											
											//////////////////////////////////////////
											$.post("state.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
											
												$("select#billingState > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeStateOptionBilling();
																							    }
																							  
																			        );
												
												
																$("select#billingState").append(data);
																
																
																
																
												$("select#mailState > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeStateOptionMail();
																							    }
																							  
																			        );
												
												
																$("select#mailState").append(data);
																
																var x = document.getElementById("mailState");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																var x = document.getElementById("billingState");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																
																}
											);
							
							
						   }
				 );

				
				$("input#Billing_PostCode").change(function(){
											$.post("postcode.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
								   				$("select#Billing_Suburb > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeOptionBilling();
																							    }
																							  
																			        );
																$("select#Billing_Suburb").append(data);
																var x = document.getElementById("Billing_Suburb");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																}
											);
											///////////////////
											$.post("state.php"
								            ,{pcode : $(this).val()}
								            ,function(data){ 
												$("select#billingState > option").each(function(i) {
												//if(i) {//the first value = 0,, i.e. select city
													//$(this).remove();
														//}
														removeStateOptionBilling();
																							    }
																							  
																			        );
												
												
																$("select#billingState").append(data);
																var x = document.getElementById("billingState");
																x.options[x.selectedIndex].value = x.options[x.selectedIndex].text;
																
																}
											);
											//////////////////////
											
							
							
						   }
				 );
				//end of Biling _ post code
				});