var thisURL = location.toString();
var thisLang = "en";
if ((thisURL.indexOf('/french') != -1)|| (thisURL.indexOf('fr_CA') != -1)) {
	thisLang = "fr";
}


function checkJQueryUIDialogloaded () {
	var style = document.createElement('link');
	style.type = "text/css";
	style.rel = "stylesheet";
	style.href = "/css/common/showPostalCodeDialog.css"; // if you change this CSS, you'll have to clear your cache
	document.getElementsByTagName("head")[0].appendChild(style);
	$.getScript("/js/common/jquery-ui-1.7.2.custom.core_and_dialog.min.js", function(){
		showPostalCodeDialog()
	});
}

function showPostalCodeDialog () {

	// Need to put in some logic now that this code runs on the homepage, which may or may not say "english/french" in the URL
	// The lightbox is expecting "english/french" in the URL as it makes a relative AJAX call by default
	var ajaxURL = "postal-code-form";
	var regex = /(english|french)/;
	if (!regex.test(thisURL)) {
		if ($("body").hasClass("lang-fr")) ajaxURL = "/gm/french/" + ajaxURL;
		if ($("body").hasClass("lang-en")) ajaxURL = "/gm/english/" + ajaxURL;
	}

	$("#postalCodeForm #postalCodeError").hide();
	$("body").append("<div id=\"postalCodeDialog\"></div>");
	$.ui.dialog.defaults.bgiframe = true; // is this actually working? documentation says it needs a plugin...
	$("#postalCodeDialog").dialog({ 
		close: function(event, ui) {
			$("#postalCodeDialog").remove(); // remove dom elements upon closing the dialog
		},
		dialogClass: 'postalCodeDialog',
		modal:true,
		width:480
	});
	$("#postalCodeDialog").html('<img src="/images/common/postalCodeLoader.gif" alt="loading" id="postalCodeLoader" />');
	$.ajax({
		url: ajaxURL,
		cache: false,
		success: function(html){
			$("#postalCodeDialog").html(html);
			$("#postalCodeDialog a#closeLabel, #postalCodeDialog a#closeX").click(function() { 
				$("#postalCodeDialog").dialog('close');
			});
			$.getScript("/js/common/showPostalCodeDialog.js"); // re-bind jQuery elements since dialog wasn't part of DOM initially
		},
		error: function(request, data) {
			$("#postalCodeDialog").html("An unexpected error has occurred: <b>" + data + "</b>.<br/><br/>Please try again in a few minutes.");
			$('#postalCodeDialog').dialog('option', 'buttons', { 
				"OK": function() { $("#postalCodeDialog").dialog('close'); }
			});
		}
			
	}); 
}

function validatePostalCode () {
	var postalCode = $("#postalCodeForm input[type=text]").get(0).value.toUpperCase().replace(/\s/,''); // get postal code input, strip whitespace
	var today = new Date();
	today.setTime(today.getTime());
	//Get 1 day in milliseconds
	var one_day=1000*60*60*24
	
	var cookieExpiresDate = new Date (today.getTime() + (30 * one_day));
	var pcRegExp = /(^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$)/;
	if (pcRegExp.test(postalCode)) {
		$("#postalCodeForm input[type=text]").get(0).value = postalCode;
		setCookie("pc",postalCode, cookieExpiresDate, "/");
		$("#postalCodeDialog #postalCodeError").hide();
		return true;
	} 
	else {
		$("#postalCodeForm #postalCodeError").show();
		$("#postalCodeForm input[type=text]").focus();
		return false;
	}
}

function validatePostalCode_lw() {
	var errorCode;
	document.getElementById("pc").value= document.getElementById("pc").value.toUpperCase();
	var postalCode = document.getElementById("pc").value.toUpperCase().replace(/\s/,''); // get postal code input, strip whitespace
	var today = new Date();
	today.setTime(today.getTime());
	//Get 1 day in milliseconds
	var one_day=1000*60*60*24
	
	var cookieExpiresDate = new Date (today.getTime() + (30 * one_day));
	var pcRegExp = /(^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$)/;
	if (pcRegExp.test(postalCode)) {
		setCookie("pc",postalCode, cookieExpiresDate, "/");
		return true; // submit the form and hence refresh the page
	}	
	else {
		if(location.href.indexOf("french")!= -1) {
			errorCode = "D\u00E9sol\u00E9! Le code postal vous avez entr\u00E9 n'est pas valide. Veuillez re-entrez votre code postal et essayez encore.";
		} 
		else {
			errorCode = "<p>Sorry! The postal code you have entered is not valid. Please re-enter your postal code and try again.</p>";
		}
		document.getElementById("postalCodeError").innerHTML = errorCode;
		return false;
	};
}
