/* urls where app can be found */
// concat "gaps" put in urls so that global search/replace won't mess with'em - kn Jan11/08
var urlItl = "http://apps.itl" + ".gm.ca";
var urlPreprod = "http://apps.preprod" + ".gm.ca";
var urlBase = "http://apps" + ".gm.ca";


//urlBase = (location.href.indexOf('itl.')>-1) ? urlItl: urlBase;
//urlBase = (location.href.indexOf('preprod.')>-1) ? urlPreprod: urlBase;

function getRef(id) {
   return (document.getElementById ? document.getElementById(id) : document.all[id]);
}

var details = false;
function drawSubmitBtn(imgLocation) {
    document.write('<img src=' + imgLocation +  ' onclick="submit1()"');
}


function submit1_fjord(){
	//alert("in");
	var formObject = getRef('regform');
	//if (formObject == null) return;
	//var formLang = (getRef('lang') ==  null) ? "en" : getRef('lang').value;
	var formLang = (getRef('langShort') ==  null) ? "en" : getRef('langShort').value;
	var formBrandCode = (getRef('f') == null) ? "8" : getRef('f').value;
	var formPC = (getRef('pc') == null) ? "" : getRef('pc').value;

	if (document.getElementById && validatePostalCode()) {
		//alert("looking for url");
		var url = urlBase + '/app/GMCanada/offersearch/ThumbnailDisplay.do?lang=' + formLang + '&f=' + formBrandCode + '&req=S&sdesc=All+Vehicle+Types&s=0&pc=' + formPC + '&x=17&y=15';
		//alert("success! " + url);
		location.href = url;
		/*
		var url = urlBase + '/app/GMCanada/offersearch/ThumbnailDisplay.do?lang=' + formLang + '&f=' + formBrandCode + '&req=S&sdesc=All+Vehicle+Types&s=0&pc=' + formPC + '&x=17&y=15';
		var oRequest = new Ajax.Request(
			url,
			{
				method: "get",
				//onComplete: HandleResponse,
				onSuccess: HandleSuccess
			}
		);
		*/
		//return true;
	}

	//return false;
}

// offers postalcode handler
// Dec 11 - sample locater link: http://apps.gm.ca/app/GMCanada/offersearch/ThumbnailDisplay.do?lang=en&f=H&req=S&sdesc=All+Vehicle+Types&s=0&pc=M5V+1L7
function submit1(){
	var formObject = getRef('regform');
	
	var formLang = (getRef('langShort') ==  null) ? "en" : getRef('langShort').value;
	var formBrandCode = (getRef('f') == null) ? "8" : getRef('f').value;
	var formPC = (getRef('pc') == null) ? "" : getRef('pc').value.toUpperCase();

	//alert("lang=" + formLang + " bCode=" + formBrandCode + " pCode=" + formPC);

	//if (document.getElementById && validatePostalCode()) {
	if (document.getElementById && isValidPC(formPC)) {
		var url = urlBase + '/app/GMCanada/offersearch/ThumbnailDisplay.do?lang=' + formLang + '&f=' + formBrandCode + '&req=S&sdesc=All+Vehicle+Types&s=0&pc=' + formPC + '&x=17&y=15'
		//alert("base="+url);
		//alert("success! " + url);		
		//document.cookie = 'pc=' + formPC + '; domain=.gm.ca; path=/';
		document.cookie = 'pc=' + formPC + '; path=/';
		//document.location.href = url;
		window.location = url;
		//alert("success! went to " + url);
		return false;
	}
	else {	
		langPath = document.location.href;
		if(langPath.indexOf("fr_CA") == -1 && langPath.indexOf("/french") == -1) {
			alert("Sorry!The postal code you have entered is not valid. Please re-enter your postal code and try again.(A valid Canadian postal code includes letters (A) and numbers (#) in the following sequence: A#A #A#)");
		} else {
			alert("D\u00E9sol\u00E9! Le code postal vous avez entr\u00E9 n'est pas valide. Veuillez re-entrez votre code postal et essayez encore. (Un code postal canadien valide doit inclure des lettres (a) et des nombres (#) dans l'ordre suivant : a#a #a#)");
		}
		return false;
	}
}

function HandleSuccess() {
	alert("offersLookup: success");
}

// try 2 at a PC validator
//http://www.webdeveloper.com/forum/showthread.php?t=95583
function isValidPC(postCode)
{
	// 5-digit US zip validator
	//if (zip.match(/^[0-9]{5}$/)) {
	//return true;
	//}
	
	if (postCode.length > 7 || postCode.length < 6) {
		//alert('*** wrong length PC!');
		return false;
	}
	
	postCode=postCode.toUpperCase();
	
	//6-char checker
	if (postCode.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
		return true;
	}
	//7-char checker
	//if (postCode.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
	if (postCode.match(/^[A-Z][0-9][A-Z][ ][0-9][A-Z][0-9]$/)) {
		return true;
	}
	//alert('*** Bad PC!');
	return false;
}

// tweaked... nay - abused! kn Dec 11/08
function validatePostalCode() {

	var theObject = getRef('pc');

    var testResult = false;

	var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	var num = '0123456789';
	
	var code = "hey now";

	if (document.getElementById) {
		code = theObject.value;
		code = code.replace(/^\s*/, "").replace(/\s*$/, ""); // explicit trim. This ain't Java...
	}

	//alert("into pc validation of '" + code + "'");
	
	langPath = document.location.href;

	if (code.length > 7 || code.length < 6) {
		alert("length Fail! L=" + code.length);
		// Update -- insert language specific message

		if(langPath.indexOf("fr_CA") == -1 && langPath.indexOf("/french") == -1) {
			alert("Sorry!The postal code you have entered is not valid. Please re-enter your postal code and try again.(A valid Canadian postal code includes letters (A) and numbers (#) in the following sequence: A#A #A#)");
		} else {
			alert("D\u00E9sol\u00E9! Le code postal vous avez entr\u00E9 n'est pas valide. Veuillez re-entrez votre code postal et essayez encore. (Un code postal canadien valide doit inclure des lettres (a) et des nombres (#) dans l'ordre suivant : a#a #a#)");
		}

		theObject.focus();
		// and return a bad flag value
		return false;
		
	} else {
		if (code.length == 7) {
			var loc = code.indexOf(" ");
			if (loc != -1) {
				code = code.substring(0, loc) + code.substring(loc + 1, 7);
                // may not be true yet, but it might be
                testResult = true;
			}
		}
		
		for (var i=0; i < code.length; i++) {
			ch = "" + code.substring(i, i+1);
			if ((((i == 0) || (i == 2) || (i == 4)) && (alpha.indexOf(ch) == -1)) ||
			    (((i == 1) || (i == 3) || (i == 5)) && (num.indexOf(ch) == -1))) {
			    
			    alert("validate Fail! code=" + code);	
			
				if(langPath.indexOf("fr_CA") == -1 && langPath.indexOf("/french") == -1) {
					alert("Sorry!The postal code you have entered is not valid. Please re-enter your postal code and try again.(A valid Canadian postal code includes letters (A) and numbers (#) in the following sequence: A#A #A#)");
				} else {
					alert("D\u00E9sol\u00E9! Le code postal vous avez entr\u00E9 n'est pas valide. Veuillez re-entrez votre code postal et essayez encore. (Un code postal canadien valide doit inclure des lettres (a) et des nombres (#) dans l'ordre suivant : a#a #a#)");
				}
				theObject.focus();
                testResult = false;
			}
		}
	}
    return testResult;
}


