//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//PRE-FILL FROM MARKETO SCRIPT --------------------------------------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function preFill(){
	// IF THE MARKETO PRE_LOAD SCRIPT IS PRESENT ON THE PAGE---------------------------------------
	if(typeof mktoPreFillFields != 'undefined'){
	
		//PREEFILL EMAIL------------------------------------------
		if(document.getElementById('Session.user.email')){
			if(mktoPreFillFields.Email != "" && typeof mktoPreFillFields.Email != 'undefined' && mktoPreFillFields.Email != null){ // 
					document.getElementById('Session.user.email').value = mktoPreFillFields.Email;
				}else{
					document.getElementById('Session.user.email').value = "";
			}
		}
			
			//PREEFILL NAME------------------------------------------
		if(document.getElementById('Session.user.name')){
			var firstUserName = "";
			var secondUserName = "";
			var fullUserName = "";
			if (mktoPreFillFields.FirstName != "" && typeof mktoPreFillFields.FirstName != 'undefined' && mktoPreFillFields.FirstName!= null){
				firstUserName = mktoPreFillFields.FirstName;
			}
			if(mktoPreFillFields.LastName != "" && typeof mktoPreFillFields.LastName != 'undefined' && mktoPreFillFields.LastName != null){
				secondUserName = mktoPreFillFields.LastName;
			}
			if(firstUserName != "" && secondUserName!= ""){
				fullUserName = firstUserName + " " + secondUserName;
			}
			if(firstUserName == ""){
				fullUserName = secondUserName;
			}
			if(secondUserName == ""){
				fullUserName = firstUserName;
			}
			document.getElementById('Session.user.name').value = fullUserName;
		}
			
			//PREEFILL COMPANY------------------------------------------
		if(document.getElementById('Session.user.companyName')){		
			if(mktoPreFillFields.Company != "" && typeof mktoPreFillFields.Company != 'undefined' && mktoPreFillFields.Company != null ){
				document.getElementById('Session.user.companyName').value = mktoPreFillFields.Company;
			}else{
				document.getElementById('Session.user.companyName').value = "";
			}
		}
		
		
		//PREEFILL CONTACT FORM MESSAGE------------------------------------------
		if(document.getElementById('Session.user.message')){		
			if(mktoPreFillFields.contactFormMessage != "" && typeof mktoPreFillFields.contactFormMessage != 'undefined' && mktoPreFillFields.contactFormMessage != null ){
				document.getElementById('Session.user.message').value = mktoPreFillFields.contactFormMessage;
			}else{
				document.getElementById('Session.user.message').value = "";
			}
		}
		
			//PREEFILL INDUSTRY------------------------------------------
		if(document.getElementById('Session.user.industry')){		
			if(mktoPreFillFields.Industry != "" && typeof mktoPreFillFields.Industry != 'undefined' && mktoPreFillFields.Industry != null ){
				document.getElementById('Session.user.industry').value = mktoPreFillFields.Industry;
				questionIndustry();
				if(mktoPreFillFields['Industry Other'] != "" && typeof mktoPreFillFields['Industry Other'] != 'undefined' && mktoPreFillFields['Industry Other'] != null ){
					document.getElementById('Session.user.industry.specify').value = mktoPreFillFields['Industry Other'];
				}
			}else{
				document.getElementById('Session.user.industry').value = "";
			}
		}
		
		
		//PREEFILL JOB FUNCTION------------------------------------------
		if(document.getElementById('Session.user.jobFunction')){	
			if(mktoPreFillFields['Job Function'] != "" && typeof mktoPreFillFields['Job Function'] != 'undefined' && mktoPreFillFields['Job Function'] != null ){
				document.getElementById('Session.user.jobFunction').value = mktoPreFillFields['Job Function'];
				questionJobFunction();
				if(mktoPreFillFields['Job Function Other'] != "" && typeof mktoPreFillFields['Job Function Other'] != 'undefined' && mktoPreFillFields['Job Function Other'] != null ){
					document.getElementById('Session.user.jobFunction.specify').value = mktoPreFillFields['Job Function Other'];
				}
			}else{
				document.getElementById('Session.user.jobFunction.specify').value = "";
			}
		}

			//PREEFILL PHONE------------------------------------------
		if(document.getElementById('Session.user.phone')){
			if(mktoPreFillFields.Phone != "" && typeof mktoPreFillFields.Phone != 'undefined' && mktoPreFillFields.Phone != null){
				document.getElementById('Session.user.phone').value = mktoPreFillFields.Phone;
			}else{
				document.getElementById('Session.user.phone').value = "";
			}
		}
		
		if(document.getElementById('countrySelect')){
			//PREEFILL COUNTRY------------------------------------------
			if(mktoPreFillFields.Country != "" && mktoPreFillFields.Country != null){
					document.getElementById('countrySelect').value = mktoPreFillFields.Country;	 
					checkForUK()
				}else{
					document.getElementById('countrySelect').options[0].selected = true;
			}
			
			//PREEFILL STATE------------------------------------------
			if(mktoPreFillFields.Country == "USA" || mktoPreFillFields.Country == "Canada"){
				if (mktoPreFillFields.State__c != "" && typeof mktoPreFillFields.State__c != 'undefined'){
						document.getElementById('stateSelect').value = mktoPreFillFields.State__c;	 
					}else{
						document.getElementById('stateSelect').options[0].selected = true;
				}
			}
			
			//PREEFILL UK POSTALCODE------------------------------------------
			if(mktoPreFillFields.Country == "United Kingdom" ){
				if (mktoPreFillFields.PostalCode != "" && typeof mktoPreFillFields.PostalCode != 'undefined'){
					document.getElementById('Session.user.postalCode').value = mktoPreFillFields.PostalCode;	 
				}else{
					document.getElementById('Session.user.postalCode').value = "";
				}
			}
		}
	}
}




//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//CHANGE DIV ON COMPLETE SCRIPT --------------------------------------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function showthx(){
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('ftform').style.display = 'none';
	}else{
		if (document.layers){ // Netscape 4
			document.ftform.display = 'none';
		}else{ // IE 4
			document.all.ftform.style.display = 'none';
		}
	}
	if (document.getElementById){ // DOM3 = IE5, NS6
		document.getElementById('ftthx').style.display = 'block';
	}else{
		if (document.layers){ // Netscape 4
			document.ftthx.display = 'block';
		}else{ // IE 4
			document.all.ftform.style.display = 'block';
		}
	}
	
}



//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//CONTACT FORM SPECIFICATION FIELDS --------------------------------------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function questionIndustry() {
	if(document.getElementById('Session.user.industry').value=="Other"){
		document.getElementById('otherOne').style.display='block';
		//alert("Others");
	}else{
		document.getElementById('otherOne').style.display='none';
		document.getElementById('Session.user.industry.specify').value='';
		//alert("Something else");
	}
}

function questionJobFunction() {
	if(document.getElementById('Session.user.jobFunction').value=="Other"){
		document.getElementById('otherJobFunction').style.display='block';
		//alert("Others");
	}else{
		document.getElementById('otherJobFunction').style.display='none';
		document.getElementById('Session.user.jobFunction.specify').value='';
		//alert("Something else");
	}
}



//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//This function has contain all allowed UK postal Codes, and are called in the form postal input field
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function PostalCodes(){
var f14 = new LiveValidation('Session.user.postalCode');
		     				f14.add(Validate.Inclusion, { within: [ 'BB' , 'BH' , 'BS' , 'DT' , 'EX' , 'GU' , 'K0' , 'K1' , 'K2' , 'K3' , 'K4' , 'K5' , 'K6' , 'K7' , 'K8' , 'K9' , 'PL' , 'PO' , 'SN' , 'SO' , 'SP' , 'TA' , 'TQ' , 'TR' , 'B0' , 'B1' , 'B2' , 'B3' , 'B4' , 'B5' , 'B6' , 'B7' , 'B8' , 'B9' , 'CF' , 'DY' , 'GL' , 'HR' , 'LD' , 'NP' , 'OX' , 'RG' , 'SA' , 'SY' , 'TF' , 'WR' , 'WS' , 'BN' , 'BR' , 'CR' , 'CT' , 'DA' , 'KT' , 'ME' , 'RH' , 'SE' , 'SM' , 'SW' , 'TN' , 'TW' , 'W0' , 'W1' , 'W2' , 'W3' , 'W4' , 'W5' , 'W6' , 'W7' , 'W8' , 'W9' , 'AL' , 'CM' , 'CO' , 'E0' , 'E1' , 'E2' , 'E3' , 'E4' , 'E5' , 'E6' , 'E7' , 'E8' , 'E9' , 'EC' , 'EN' , 'HA' , 'IG' , 'N0' , 'N1' , 'N2' , 'N3' , 'N4' , 'N5' , 'N6' , 'N7' , 'N8' , 'N9' , 'NW' , 'RM' , 'SG' , 'SL' , 'SS' , 'UB' , 'WC' , 'WD' , 'BB' , 'BL' , 'CH' , 'CW' , 'FY' , 'HX' , 'L0' , 'L1' , 'L2' , 'L3' , 'L4' , 'L5' , 'L6' , 'L7' , 'L8' , 'L9' , 'LL' , 'M0' , 'M1' , 'M2' , 'M3' , 'M4' , 'M5' , 'M6' , 'M7' , 'M8' , 'M9' , 'OL' , 'PR' , 'SK' , 'ST' , 'WA' , 'WN' , 'WV' , 'CB' , 'CV' , 'DE' , 'HP' , 'IP' , 'LE' , 'LU' , 'MK' , 'NG' , 'NN' , 'NR' , 'PE' , 'BD' , 'DH' , 'DL' , 'DN' , 'HD' , 'HG' , 'HU' , 'LA' , 'LN' , 'LS' , 'NE' , 'S0' , 'S1' , 'S2' , 'S3' , 'S4' , 'S5' , 'S6' , 'S7' , 'S8' , 'S9' , 'SR' , 'TS' , 'WF' , 'YO' , 'AB' , 'BT' , 'CA' , 'DD' , 'DG' , 'EH' , 'FK' , 'G0' , 'G1' , 'G2' , 'G3' , 'G4' , 'G5' , 'G6' , 'G7' , 'G8' , 'G9' , 'HS' , 'IM' , 'IV' , 'KA' , 'KY' , 'ML' , 'PA' , 'PH' , 'TD' , 'ZE' ],caseSensitive: false } );
}
	
	
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//STEP ONE: Validating fields and if oki submitting email for a token.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var realselcountry;


function dostuff(){
    document.getElementById('submitbut').value = "Please wait";
	document.getElementById('loaderbar').style.display = 'block';
	var msg = "";
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	
	
	//EMAIL VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('required.email')){
		if (!emailFilter.test(document.getElementById('Session.user.email').value))
			msg += "Email address is invalid\n";
			
		if(document.getElementById('Session.user.email').value.match(illegalChars))
		msg += "Email address is invalid\n";
	}
		
	
	
	
	//NAME VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('required.name')){
		if(document.getElementById('Session.user.name').value.length<1)
			msg += "Please enter a username\n";
	}
	
	//COMPANY VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('required.company')){
		if(document.getElementById('Session.user.companyName').value.length<1)
			msg += "Please enter a company\n"
	}
	
	
	//PHONE VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	//If Country is DK, NO or SE then do not validate phone
	if(document.getElementById('required.phone')){
			if(document.getElementById('Session.user.phone').value.length<5)
			msg += "Please enter a correct phone number\n";
	}
	
	
	//CONTACT MESSAGE VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	//If Country is DK, NO or SE then do not validate phone
	if(document.getElementById('required.contactmessage')){
			if(document.getElementById('Session.user.message').value.length<5)
			msg += "Please enter a message\n";
	}
	
	
	//JOB FUNCTION VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('required.jobFunction')){
		if(document.getElementById('Session.user.jobFunction').value.length<1)
			msg += "Please choose a job function\n";
			
		if(document.getElementById('Session.user.jobFunction').value == "Other" && document.getElementById('Session.user.jobFunction.specify').value.length<1)
			msg += "Please specify your job function\n";
	}
	
	
	//INDUSTRY VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('required.industry')){
		if(document.getElementById('Session.user.industry').value.length<1)
			msg += "Please choose an industry\n";
			
		if(document.getElementById('Session.user.industry').value == "Other" && document.getElementById('Session.user.industry.specify').value.length<1)
			msg += "Please specify your industry\n";
	}
			
	//COUNTRY VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(document.getElementById('countrySelect')){
		if(document.getElementById('countrySelect').options[document.getElementById('countrySelect').selectedIndex].value=="")
			msg += "Please select a country\n";

		//STATE VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		tmpcountry = document.getElementById('countrySelect').options[document.getElementById('countrySelect').selectedIndex].value;
			
		if((tmpcountry=='Canada'||tmpcountry=='USA')&&document.getElementById('stateSelect')){
			selectedstate =	document.getElementById('stateSelect').value;
			if(document.getElementById('stateSelect').value.length<2)
			msg += "Please select a state\n";
	
			//alert("IS US OR CA");
		}else{
			selectedstate =	'';		
			//alert("IS NOT US OR CA");
		}//IF Country is not USA or Canada then set State__c/stateSelect to "" 
	
		//UNITED KINGDOM POSTAL CODE VALIDATION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	
		if(document.getElementById('countrySelect').value!='United Kingdom'){
			//Postal code only relevant for the UK, set to empty before submit
			//This value can only be set if uk is choosen from the dropdown and the field is filled and then another country is selected from the dropdown
			document.getElementById('Session.user.postalCode').value = "";
		}
			
		/*if(document.getElementById('countrySelect').value=='United Kingdom' && document.getElementById('Session.user.postalCode').value.length<1)
			msg += "Please enter a postal code\n";
		
		if(document.getElementById('countrySelect').value=='United Kingdom' && document.getElementById('Session.user.postalCode').value.length>2)
			msg += "Postal code can only be 1-2 digits\n";
		
		if(document.getElementById('countrySelect').value=='United Kingdom' && document.getElementById('Session.user.postalCode').value.length>0){
				
			res = false;
	
			for(i=0;i<pcode.length;i++){
			
			
				if(pcode[i]==document.getElementById('Session.user.postalCode').value.toUpperCase()){
				
					res = true;
					break;
				}
			}
			
			if(!res){
				
				msg += "Please enter a valid postal code\n";			
			}
		}*/
	}
	
	//IF ANY ERRORS - RESET BUTTON VALUE TO "GO!" AND ALERT ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	if(msg.length>0){
		document.getElementById('submitbut').value = "Go!";
		document.getElementById('loaderbar').style.display = 'none';
		alert("Please fill in the form correctly!\n\nThe following errors where detected:\n"+ msg);
		return;
	}
	//DISABLES THE SUBMIT BUTTON TO PREVENT MULTIPLE SUBMISSIONS
	document.getElementById('submitbut').disabled=true;
	
	if(FreeTrialForm == "true" ){//IF it is a free trial form
		//The webshop only supports a limited set of countries, if the country falls outside this category we need to change it to row before we submit to the shop
		if(document.getElementById('Session.user.phone').style.display == 'none'){
			document.getElementById('Session.user.phone').value = 'Null';
		}
		
		res=false;
		cselect = document.getElementById('countrySelect');
		realselcountry = cselect.selectedIndex;
		for(i=0;i<countries.length;i++){
		
			if(countries[i]==cselect.options[cselect.selectedIndex].value.toUpperCase()){
				res=true;
				break;
			}
		}
		if(!res){
			//Create a new option element
			newopt = document.createElement('option');
			
			//Add value and text 
			newopt.text  = cselect.options[cselect.selectedIndex].text;
			newopt.value  = "Rest Of The World";
		
			//Add the new options to position 0
			cselect.options.add(newopt,0);
		
			//Make sure option 0 (the one just created) is selected before we submit
			cselect.selectedIndex=0;
			}
	
		setTimeout(complete,4000);
	}else{//IF it is NOT a free trial form
		setTimeout(redir,4000);
	}
}


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//STEP TWO - Submits the form to Form action and resets the country value to original country after rewriting it for shop
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function complete(){
	document.getElementById('freetrial').submit();
	
	//This resets the country and passing it to the shop
	var countryoffset = 0;
	for(i=0;i<document.getElementById('countrySelect').options.length;i++){
		if(document.getElementById('countrySelect').options[i].value=="Rest Of The World"){
		 countryoffset = 1;
		 break;
		}
	}
	document.getElementById('countrySelect').selectedIndex=realselcountry+countryoffset;
	//showthx();//This function will switch over to the "thank you" div on submit
	setTimeout(redir,4000);//CALLS FUNCTION redir, IF YOU WANT A REDIRECT AFTER SIGNUP
}


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//ALLOWED COUNTRIES IN SHOP, OTHER COUNTRIES ARE SET REST OF THE WORLD IN FUNCTION DOSTUFF  ---------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	var countries = new Array();
	countries[1] = "UNITED KINGDOM";
	countries[2] = "SWEDEN";
	countries[3] = "NORWAY";
	countries[4] = "CANADA";
	countries[5] = "USA";
	countries[6] = "DENMARK";
	

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//UK POSTAL CODES ALLOWED  --------------------------------------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var pcode = new Array();
	//TERRITORY 1 START
	pcode[0]="BB";
	pcode[1]="BH";
	pcode[2]="BS";
	pcode[3]="DT";
	pcode[4]="EX";
	pcode[5]="GU";
	pcode[6]="K0";
	pcode[7]="K1";
	pcode[8]="K2";
	pcode[9]="K3";
	pcode[10]="K4";
	pcode[11]="K5";
	pcode[12]="K6";
	pcode[13]="K7";
	pcode[14]="K8";
	pcode[15]="K9";
	pcode[16]="PL";
	pcode[17]="PO";
	pcode[18]="SN";
	pcode[19]="SO";
	pcode[20]="SP";
	pcode[21]="TA";
	pcode[22]="TQ";
	pcode[23]="TR";//TERRITORY 2 START
	pcode[24]="B0";
	pcode[25]="B1";
	pcode[26]="B2";
	pcode[27]="B3";
	pcode[28]="B4";
	pcode[29]="B5";
	pcode[30]="B6";
	pcode[31]="B7";
	pcode[32]="B8";
	pcode[33]="B9";
	pcode[34]="CF";
	pcode[35]="DY";
	pcode[36]="GL";
	pcode[37]="HR";
	pcode[38]="LD";
	pcode[39]="NP";
	pcode[40]="OX";
	pcode[41]="RG";
	pcode[42]="SA";
	pcode[43]="SY";
	pcode[44]="TF";
	pcode[45]="WR";
	pcode[46]="WS";//TERRITORY 3 START
	pcode[47]="BN";
	pcode[48]="BR";
	pcode[49]="CR";
	pcode[50]="CT";
	pcode[51]="DA";
	pcode[52]="KT";
	pcode[53]="ME";
	pcode[54]="RH";
	pcode[55]="SE";
	pcode[56]="SM";
	pcode[57]="SW"
	pcode[58]="TN";
	pcode[59]="TW";
	pcode[60]="W0";//TERRITORY 4 START
	pcode[61]="W1";
	pcode[62]="W2";
	pcode[63]="W3";
	pcode[64]="W4";
	pcode[65]="W5";
	pcode[66]="W6";
	pcode[67]="W7";
	pcode[68]="W8";
	pcode[69]="W9";
	pcode[70]="AL";
	pcode[71]="CM";
	pcode[72]="CO";
	pcode[73]="E0";
	pcode[74]="E1";
	pcode[75]="E2";
	pcode[76]="E3";
	pcode[77]="E4";
	pcode[78]="E5";
	pcode[79]="E6";
	pcode[80]="E7";
	pcode[81]="E8";
	pcode[82]="E9";
	pcode[83]="EC";
	pcode[84]="EN";
	pcode[85]="HA";
	pcode[86]="IG";
	pcode[87]="N0";
	pcode[88]="N1";
	pcode[89]="N2";
	pcode[90]="N3";
	pcode[91]="N4";
	pcode[92]="N5";
	pcode[93]="N6";
	pcode[94]="N7";
	pcode[95]="N8";
	pcode[96]="N9";
	pcode[97]="NW";
	pcode[98]="RM";
	pcode[99]="SG";
	pcode[100]="SL";
	pcode[101]="SS";
	pcode[102]="UB";
	pcode[103]="WC";
	pcode[104]="WD";//TERRITORY 5 START
	pcode[105]="BB";
	pcode[106]="BL";
	pcode[107]="CH";
	pcode[108]="CW";
	pcode[109]="FY";
	pcode[110]="HX";
	pcode[111]="L0";
	pcode[112]="L1";
	pcode[113]="L2";
	pcode[114]="L3";
	pcode[115]="L4";
	pcode[116]="L5";
	pcode[117]="L6";
	pcode[118]="L7";
	pcode[119]="L8";
	pcode[120]="L9";
	pcode[121]="LL";
	pcode[122]="M0";
	pcode[123]="M1";
	pcode[124]="M2";
	pcode[125]="M3";
	pcode[126]="M4";
	pcode[127]="M5";
	pcode[128]="M6";
	pcode[129]="M7";
	pcode[130]="M8";
	pcode[131]="M9";
	pcode[132]="OL";
	pcode[133]="PR";
	pcode[134]="SK";
	pcode[135]="ST";
	pcode[136]="WA";
	pcode[137]="WN";
	pcode[138]="WV";//TERRITORY 6 START
	pcode[139]="CB";
	pcode[140]="CV";
	pcode[141]="DE";
	pcode[142]="HP";
	pcode[143]="IP";
	pcode[144]="LE";
	pcode[145]="LU";
	pcode[146]="MK";
	pcode[147]="NG";
	pcode[148]="NN";
	pcode[149]="NR";
	pcode[150]="PE";//TERRITORY 7 START
	pcode[151]="BD";
	pcode[152]="DH";
	pcode[153]="DL";
	pcode[154]="DN";
	pcode[155]="HD";
	pcode[156]="HG";
	pcode[157]="HU";
	pcode[158]="LA";
	pcode[159]="LN";
	pcode[160]="LS";
	pcode[161]="NE";
	pcode[162]="S0";
	pcode[163]="S1";
	pcode[164]="S2";
	pcode[165]="S3";
	pcode[166]="S4";
	pcode[167]="S5";
	pcode[168]="S6";
	pcode[169]="S7";
	pcode[170]="S8";
	pcode[171]="S9";
	pcode[172]="SR";
	pcode[173]="TS";
	pcode[174]="WF";
	pcode[175]="YO";//TERRITORY 8 START
	pcode[176]="AB";
	pcode[177]="BT";
	pcode[178]="CA";
	pcode[179]="DD";
	pcode[180]="DG";
	pcode[181]="EH";
	pcode[182]="FK";
	pcode[183]="G0";
	pcode[184]="G1";
	pcode[185]="G2";
	pcode[186]="G3";
	pcode[187]="G4";
	pcode[188]="G5";
	pcode[189]="G6";
	pcode[190]="G7";
	pcode[191]="G8";
	pcode[192]="G9";
	pcode[193]="HS";
	pcode[194]="IM";
	pcode[195]="IV";
	pcode[196]="KA";
	pcode[197]="KY";
	pcode[198]="ML";
	pcode[199]="PA";
	pcode[200]="PH";
	pcode[201]="TD";
	pcode[202]="ZE";




//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//COUNTRY AND STATE SCRIPT --------------------------------------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


var postState = '';
var postCountry = '';

var state = '\
USA:AK:Alaska|\
USA:AL:Alabama|\
USA:AR:Arkansas|\
USA:AZ:Arizona|\
USA:CA:California|\
USA:CO:Colorado|\
USA:CT:Connecticut|\
USA:DC:District of Columbia|\
USA:DE:Delaware|\
USA:FL:Florida|\
USA:GA:Georgia|\
USA:HI:Hawaii|\
USA:IA:Iowa|\
USA:ID:Idaho|\
USA:IL:Illinois|\
USA:IN:Indiana|\
USA:KS:Kansas|\
USA:KY:Kentucky|\
USA:LA:Louisiana|\
USA:MA:Massachusetts|\
USA:MD:Maryland|\
USA:ME:Maine|\
USA:MI:Michigan|\
USA:MN:Minnesota|\
USA:MO:Missouri|\
USA:MS:Mississippi|\
USA:MT:Montana|\
USA:NC:North Carolina|\
USA:ND:North Dakota|\
USA:NE:Nebraska|\
USA:NH:New Hampshire|\
USA:NJ:New Jersey|\
USA:NM:New Mexico|\
USA:NV:Nevada|\
USA:NY:New York|\
USA:OH:Ohio|\
USA:OK:Oklahoma|\
USA:OR:Oregon|\
USA:PA:Pennsylvania|\
USA:RI:Rhode Island|\
USA:SC:South Carolina|\
USA:SD:South Dakota|\
USA:TN:Tennessee|\
USA:TX:Texas|\
USA:UT:Utah|\
USA:VA:Virginia|\
USA:VT:Vermont|\
USA:WA:Washington|\
USA:WI:Wisconsin|\
USA:WV:West Virginia|\
USA:WY:Wyoming|\
Canada:AB:Alberta|\
Canada:BC:British Columbia|\
Canada:MB:Manitoba|\
Canada:NB:New Brunswick|\
Canada:NF:Newfoundland and Labrador|\
Canada:ON:Ontario|\
Canada:SK:Saskatchewan|\
';


var country = "\
Afghanistan:Afghanistan|\
Albania:Albania|\
Algeria:Algeria|\
Andorra:Andorra|\
Angola:Angola|\
Antarctica:Antarctica|\
Antigua and Barbuda:Antigua and Barbuda|\
Argentina:Argentina|\
Armenia:Armenia|\
Australia:Australia|\
Austria:Austria|\
Azerbaijan:Azerbaijan|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Bangladesh:Bangladesh|\
Barbados:Barbados|\
Belarus:Belarus|\
Belgium:Belgium|\
Belize:Belize|\
Benin:Benin|\
Bermuda:Bermuda|\
Bhutan:Bhutan|\
Bolivia:Bolivia|\
Bosnia and Herzegovina:Bosnia and Herzegovina|\
Botswana:Botswana|\
Brazil:Brazil|\
Brunei:Brunei|\
Bulgaria:Bulgaria|\
Burkina Faso:Burkina Faso|\
Burma:Burma|\
Burundi:Burundi|\
Cambodia:Cambodia|\
Cameroon:Cameroon|\
Canada:Canada|\
Cape Verde:Cape Verde|\
Central African Republic:Central African Republic|\
Chad:Chad|\
Chile:Chile|\
China:China|\
Colombia:Colombia|\
Comoros:Comoros|\
Congo, Democratic Republic:Congo, Democratic Republic|\
Congo, Republic of the:Congo, Republic of the|\
Costa Rica:Costa Rica|\
Cote d'Ivoire:Cote d'Ivoire|\
Croatia:Croatia|\
Cuba:Cuba|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Denmark:Denmark|\
Djibouti:Djibouti|\
Dominica:Dominica|\
Dominican Republic:Dominican Republic|\
East Timor:East Timor|\
Ecuador:Ecuador|\
Egypt:Egypt|\
El Salvador:El Salvador|\
Equatorial Guinea:Equatorial Guinea|\
Eritrea:Eritrea|\
Estonia:Estonia|\
Ethiopia:Ethiopia|\
Fiji:Fiji|\
Finland:Finland|\
France:France|\
Gabon:Gabon|\
Gambia:Gambia|\
Georgia:Georgia|\
Germany:Germany|\
Ghana:Ghana|\
Greece:Greece|\
Greenland:Greenland|\
Grenada:Grenada|\
Guatemala:Guatemala|\
Guinea:Guinea|\
Guinea-Bissau:Guinea-Bissau|\
Guyana:Guyana|\
Haiti:Haiti|\
Honduras:Honduras|\
Hong Kong:Hong Kong|\
Hungary:Hungary|\
Iceland:Iceland|\
India:India|\
Indonesia:Indonesia|\
Iran:Iran|\
Iraq:Iraq|\
Ireland, Northern:Ireland, Northern|\
Ireland:Ireland|\
Israel:Israel|\
Italy:Italy|\
Jamaica:Jamaica|\
Japan:Japan|\
Jordan:Jordan|\
Kazakhstan:Kazakhstan|\
Kenya:Kenya|\
Kiribati:Kiribati|\
Korea, North:Korea, North|\
Korea, South:Korea, South|\
Kuwait:Kuwait|\
Kyrgyzstan:Kyrgyzstan|\
Laos:Laos|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Lesotho:Lesotho|\
Liberia:Liberia|\
Libya:Libya|\
Liechtenstein:Liechtenstein|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Macedonia:Macedonia|\
Madagascar:Madagascar|\
Malawi:Malawi|\
Malaysia:Malaysia|\
Maldives:Maldives|\
Mali:Mali|\
Malta:Malta|\
Marshall Islands:Marshall Islands|\
Mauritania:Mauritania|\
Mauritius:Mauritius|\
Mexico:Mexico|\
Micronesia:Micronesia|\
Moldova:Moldova|\
Mongolia:Mongolia|\
Morocco:Morocco|\
Monaco:Monaco|\
Mozambique:Mozambique|\
Namibia:Namibia|\
Nauru:Nauru|\
Nepal:Nepal|\
Netherlands:Netherlands|\
New Zealand:New Zealand|\
Nicaragua:Nicaragua|\
Niger:Niger|\
Nigeria:Nigeria|\
Norway:Norway|\
Oman:Oman|\
Pakistan:Pakistan|\
Panama:Panama|\
Papua New Guinea:Papua New Guinea|\
Paraguay:Paraguay|\
Peru:Peru|\
Philippines:Philippines|\
Poland:Poland|\
Portugal:Portugal|\
Qatar:Qatar|\
Romania:Romania|\
Russia:Russia|\
Rwanda:Rwanda|\
Samoa:Samoa|\
San Marino:San Marino|\
Sao Tome:Sao Tome|\
Saudi Arabia:Saudi Arabia|\
Senegal:Senegal|\
Serbia and Montenegro:Serbia and Montenegro|\
Seychelles:Seychelles|\
Sierra Leone:Sierra Leone|\
Singapore:Singapore|\
Slovakia:Slovakia|\
Slovenia:Slovenia|\
Solomon Islands:Solomon Islands|\
Somalia:Somalia|\
South Africa:South Africa|\
Spain:Spain|\
Sri Lanka:Sri Lanka|\
Sudan:Sudan|\
Suriname:Suriname|\
Swaziland:Swaziland|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Syria:Syria|\
Taiwan:Taiwan|\
Tajikistan:Tajikistan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Togo, Tonga:Togo, Tonga|\
Trinidad and Tobago:Trinidad and Tobago|\
Tunisia:Tunisia|\
Turkey:Turkey|\
Turkmenistan:Turkmenistan|\
Uganda:Uganda|\
Ukraine:Ukraine|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom|\
Uruguay:Uruguay|\
USA:USA|\
Uzbekistan:Uzbekistan|\
Vanuatu:Vanuatu|\
Venezuela:Venezuela|\
Vietnam:Vietnam|\
Yemen:Yemen|\
Zambia:Zambia|\
Zimbabwe:Zimbabwe|\
";

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Select Country','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' && countryName) {
//		alert(countryName);
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function checkForUK() {
	countryValue = document.getElementById('countrySelect').value
	if(countryValue == "United Kingdom"){
		document.getElementById('StateField').style.display = 'none';
		document.getElementById('PostalCodeField').style.display = 'none';
	}else{
		document.getElementById('Session.user.postalCode').value = "";
		if(document.getElementById('postalCodeRespondsHolder')){
		document.getElementById('postalCodeRespondsHolder').style.display = 'none';
		}
		document.getElementById('PostalCodeField').style.display = 'none';
		document.getElementById('StateField').style.display = 'block';
		populateState();
	}	
} 

function populateState() {
  document.getElementById('StateField').style.visibility = 'visible';
  document.getElementById('StateField').innerHTML='<strong style=\'padding-right:30px; font-family:Verdana, Arial; font-size:12px; font-weight:bold; color:#666666;\'>State:</strong>        <select style=\'width:195px; margin-left:75px\' id=\'stateSelect\' name=\'state\'></select>		<img style=\'padding-left:3px\' src=\'http://www.zmags.com/Media/required.gif\' width=\'10\' height=\'10\' />	   <script type=\'text/javascript\'>initCountry(\'USA\'); </script>';
	   
	   
//  alert(document.getElementById('StateField').innerHTML);
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Please select','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);
  if (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select

if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
		inputSel.setAttribute("length", 150);
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State','');
        selObj.selectedIndex = 0;

      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
	inputEl.setAttribute("width", 150);
    inputEl.setAttribute("value", postState);
	document.getElementById('StateField').style.visibility = 'hidden';
    //parentObj.appendChild(inputEl) ;
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}









