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 validateOffersPostal(obj) {
	if(validatePostalCode()) {
		if(obj==null) { document.getElementById('regform').submit(); }
		return true;
	}
	return false;
}	

function validatePostalCode() {
   var zip = document.getElementById('pc').value;
   var zipRegExp = /(^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$)/;
   if (zipRegExp.test(zip)) {
   		return true;
   } else {
		if(location.href.indexOf("french")!= -1) {
			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#)");
		} else {
			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#)");
			}
   		return false;
   };
}


/*NOTE: The following submit1() should be removed whenever time permits and the validateOffersPostal() above should be used instead */
function submit1(){
	var formObject = getRef('regform');
	var formLang = getRef('lang').value;
	var formBrandCode = CCNMMDIdivision;
	var formPC = getRef('pc').value;
	if (document.getElementById && validatePostalCode()) {
		location.href = getMyLoc() +  "lang=" + formLang + "&f=" + formBrandCode + '&pc=' + formPC ; 
	}
}


/* make sure we are not compounding a postal code search url */

function getMyLoc(){
	var myLoc = location.href; //create this with reasonable value in case something goes wrong, we can fail somewhat gracefully
	if(myLoc.indexOf('?pc=') != -1  || myLoc.indexOf('?lang=') != -1  ){ //we seem to have query string values associate with the offers widget
		myLoc = document.location.href.substring(0,document.location.href.indexOf("?")) + "?";	
	}
	else if (myLoc.indexOf('&pc=') != -1  && myLoc.indexOf('&lang=') != -1) {
		if (myLoc.indexOf('&pc=') < myLoc.indexOf('&lang=')){
			myLoc = document.location.href.substring(0,document.location.href.indexOf('pc='));  
		}
		else{
			myLoc = document.location.href.substring(0,document.location.href.indexOf('lang='));  
		}
	} 
	else if(myLoc.indexOf('?') == -1){ //we no query string so let's add the delimiter
		myLoc = myLoc + "?";
	}
	else{myLoc = myLoc + "&";}  //we have a query string unrelated to the offers widget so add the additive delimiter instead
	return myLoc;
}

