function getSelectedCountryCode(){

	var shippingCountry = document.frmDelivery.countryCode.value;
	
	
	 if(shippingCountry=="826"){
		document.getElementById("postCodeRow").style.visibility="visible";
		document.getElementById("postCodeBoxLabel").innerHTML = "Post Code:";
	 }
	 else if(shippingCountry=="372")
	 {	  
		 document.getElementById("postCodeRow").style.visibility="visible";
		 document.getElementById("postCodeBoxLabel").innerHTML = "City:";
	 }
	 else{	   
		 document.getElementById("postCodeRow").style.visibility="hidden";
		 document.frmDelivery.postalCode.value = "";
		 displayDeliveryMethods("",shippingCountry);		 
	 }
}


function clearDisplay(){
	var objPostCode = document.getElementById("postCodeRow");
	var objShippingMethods= document.getElementById("shippingMethods");
		if(objShippingMethods != null){
				objShippingMethods.style.visibility="hidden";
		}
		if(objPostCode != null){
			objPostCode.style.visibility="hidden";
		}
		$("#deliverySaveDiv").hide();
}

function getShippingMethodForCityIreland(shippingCountry,postalCodeOrCity,validationNeeded)
{	
	var city = document.frmDelivery.city.value;	
	if(shippingCountry == "372")
	{
		getShippingMethods(shippingCountry,postalCodeOrCity,validationNeeded);
	}
}

function getShippingMethods(shippingCountry,postalCodeOrCity,validationNeeded){
	
	//--	trim the postal code
	postalCodeOrCity = postalCodeOrCity.replace(/\s/g, "");
	if( document.frmDelivery != undefined && document.frmDelivery.postalCode != undefined ){
		document.frmDelivery.postalCode.value = document.frmDelivery.postalCode.value.replace(/\s/g, "");
	}
	
		
	if(postalCodeOrCity==''){
		var city = "";
		if(typeof(document.frmDelivery.city) != "undefined")
		{
			city = document.frmDelivery.city.value;			
		}
		
		var postalCode = document.frmDelivery.postalCode.value;
		
		if(shippingCountry == "372"){
			postalCodeOrCity = city;
		}
		else{
			postalCodeOrCity = postalCode;	
		}
	}	
	//var shippingCountry = frmObj.country.value;	
	var postCodeOutBound = "";
	var postCodeInBound = "";
	//var postalCodeOrCity = "";
	//if(shippingCountry=='372'){
		//postalCodeOrCity  = frmObj.city.value;
	//}
	//else{
		//postalCodeOrCity  = frmObj.postalCode.value;
	//}
	var displayDeliveryMethod = true;
	if(validationNeeded=="yes"){	
		if((shippingCountry=='826')&&(postalCodeOrCity=='')){
			alert("Please enter Postal Code");
			hideDeliveryMethods();
			displayDeliveryMethod = false;
		}
		else if ((shippingCountry=='826')&&(postalCodeOrCity!='')&& (checkPostCode(postalCodeOrCity)!= true)) {
			alert ("Postcode has invalid format");
			hideDeliveryMethods();
			displayDeliveryMethod = false;
		}
	}

	if(displayDeliveryMethod == true)
	{
		if(shippingCountry=='826')
		{
			if(postalCodeOrCity != "" && postalCodeOrCity.length > 5)
			{
				postalCodeOrCity = postalCodeOrCity.toUpperCase();
				postCodeVal = postalCodeOrCity;
				if(postCodeVal.charAt(postCodeVal.length-4)!=" ")
				{	
					postCodeOutBound = postCodeVal.substring(0,postCodeVal.length-3);
					postCodeInBound = postCodeVal.substring(postCodeVal.length-3);
					postalCodeOrCity = postCodeOutBound + " " + postCodeInBound;
					document.forms["frmDelivery"].postalCode.value = postalCodeOrCity;				
				}		
			}			
		}
		displayDeliveryMethods(postalCodeOrCity,shippingCountry);
	}
	

}


function getShippingMethodsTemp(frmName,frmObj)
{

	var shippingCountry = frmObj.countryCode.value;	
	var postCodeOutBound = "";
	var postCodeInBound = "";
	var postCode = frmObj.postCode.value;
	var displayDeliveryMethod = true;
		
	if((shippingCountry=='826')&&(postCode=='')){
		alert("Please enter Postal Code");
		displayDeliveryMethod = false;
	}
	else if ((shippingCountry=='826')&&(postCode!='')&& (checkPostCode(postCode)!= true)) {
		alert ("Postcode has invalid format");
		displayDeliveryMethod = false;
	}

	if(displayDeliveryMethod == true)
	{
		if(shippingCountry=='826')
		{
			if(frmObj.postCode.value != "" && frmObj.postCode.value.length > 5)
			{
				frmObj.postCode.value = frmObj.postCode.value.toUpperCase();
				postCodeVal = frmObj.postCode.value;
				if(postCodeVal.charAt(postCodeVal.length-4)!=" ")
				{	
					postCodeOutBound = postCodeVal.substring(0,postCodeVal.length-3);
					postCodeInBound = postCodeVal.substring(postCodeVal.length-3);
					frmObj.postCode.value = postCodeOutBound + " " + postCodeInBound;
				}		
			}			
		}	
		displayDeliveryMethods(frmObj.postCode.value,shippingCountry);
	}
	

}

function hideDeliveryMethods()
{	
	if(document.getElementById("shippingMethods") != null)
	{
		var fromPage = document.getElementById("frompage").innerHTML;
		if(fromPage == "deliverydetails")
		{
			document.getElementById("shippingMethods").innerHTML = document.getElementById("shippingMethodsTitle").innerHTML;
		}
		else
		{
			$("#shippingMethods").hide();
			document.getElementById("shippingMethods").style.visibility="hidden";
			document.getElementById("shippingMethods").innerHTML = ""; 
		}
	}
	if(document.getElementById("deliverySaveDiv") != null) {
		$("#deliverySaveDiv").hide();	
	}
}

function displayDeliveryMethods(postCode,shippingCountry)
{

	$.post(sitePath+"getdeliverymethods.htm",{postCode:postCode,countryCode:shippingCountry},function(data){	 	
		document.getElementById("shippingMethods").style.visibility="visible";
		document.getElementById("shippingMethods").innerHTML = data; 
		if(document.getElementById("shippingMethods") != null)
		{
			$("#shippingMethods").show();
		}
		if(document.getElementById("deliverySaveDiv") != null) {
			$("#deliverySaveDiv").show();	
		}		
	},"html");

}

function checkPostCode(toCheck) {
	
	if(toCheck.length < 6)
		return false;
		
	// Permitted letters depend upon their position in the postcode.
	var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
	var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
	var alpha3 = "[abcdefghjkstuw]";                                // Character 3
	var alpha4 = "[abehmnprvwxy]";                                  // Character 4
	var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5

	// Array holds the regular expressions for the valid postcodes
	var pcexp = new Array ();

	// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Expression for postcodes: ANA NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Expression for postcodes: AANA  NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Exception for the special postcode GIR 0AA
	pcexp.push (/^(GIR)(\s*)(0AA)$/i);

	// Standard BFPO numbers
	pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);

	// c/o BFPO numbers
	pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

	// Load up the string to check
	var postCode = toCheck;

	// Assume we're not going to find a valid postcode
	var valid = false;

	// Check the string against the types of post codes
	for ( var i=0; i<pcexp.length; i++) {
		if (pcexp[i].test(postCode)) {
			// The post code is valid - split the post code into component parts
			pcexp[i].exec(postCode);
  
			// Copy it back into the original string, converting it to uppercase and
			// inserting a space between the inward and outward codes
			postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
  
			// If it is a BFPO c/o type postcode, tidy up the "c/o" part
			postCode = postCode.replace (/C\/O\s*/,"c/o ");
  
			// Load new postcode back into the form element
			valid = true;
  
			// Remember that we have found that the code is valid and break from loop
			break;
		}
	}

	// Return with either the reformatted valid postcode or the original invalid 
	// postcode
	if (valid) {
		return true;
	} else {
		return false;
	}
}
