// Valid canadian postal code
var pcodeexp = /^[g,h,j,k,l,m,n,p,t,v][0-9][a-z][0-9][a-z][0-9]$/i ;
var iens6 = document.all || document.getElementById ? true : false;
var ns4 = document.layers ? true : false;
var preloadFlag = false;
function preloadImages()
{
if (document.images)
{
	pre_btn_order_on = newImage('../images/btn_order-on.gif');
	pre_btn_ctgiftcards_on = newImage('../images/btn_ctgiftcards-on.gif');
	pre_btn_enter_on = newImage('../images/btn_enter-on.gif');
	preloadFlag = true;
}
}
// Code for the popup windows
var popup;
function launchwindow(winURL,winName,w,h,winSpecs)
{
popup = window.open(winURL,winName,
		"Width=" + w + ",Height=" + h + "," + winSpecs);
popup.focus();
}
function BNB_mouseon(n)
	{
	if(browsok)
		{
		imageON = eval(n + "1.src");
		document [n].src = imageON;
		}
	}

function BNB_mouseoff(n)
	{
	if(browsok)
		{
		imageOFF = eval(n + "0.src");
		document [n].src = imageOFF;
		}
	}

//Place focus on the first form element
function placeFocus()
{
	if (document.forms.length > 0)
	{
		var field = document.forms[0];
		for (i = 0; i < field.length; i++)
		{
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
			document.forms[0].elements[i].focus();
			break;
         }
      }
   }
}
function Login_onsubmit() 
{
 	//Verify the login from the entry form.
	if ( document.Login.Email.value == "")
	{
		window.alert("Please enter a valid email address.");
		document.Login.Email.focus();
		return false;
	}
 	//Verify the form fields
	if ( document.Login.Password.value == "")
	{
		window.alert("Please enter your password.");
		document.Login.Password.focus();
		return false;
	}
}
function ClientLogin_onsubmit() 
{
 	//Verify the login from the entry form.
	if ( document.Login.UserName.value == "")
	{
		window.alert("Please enter a valid username.");
		document.Login.UserName.focus();
		return false;
	}
 	//Verify the form fields
	if ( document.Login.Password.value == "")
	{
		window.alert("Please enter your password.");
		document.Login.Password.focus();
		return false;
	}
}
function Login2_onsubmit() 
{
 	//Verify the login from the entry form.
	if ( document.Login.Email.value == "")
	{
		window.alert("Please enter a valid email address.");
		document.Login.Email.focus();
		return false;
	}
}
function NameNews_onsubmit() 
{
 	//Verify the login from the entry form.
	if ( document.NameNews.NameNews.value == "")
	{
		window.alert("Please enter a name for the newsletter.");
		document.NameNews.NameNews.focus();
		return false;
	}
}
function Entry_onsubmit() 
{
 	//Verify the form fields
	if ( document.Entry.FName.value == "")
	{
		window.alert("Please enter your first name.");
		document.Entry.FName.focus();
		return false;
	}
	if ( document.Entry.LName.value == "")
	{
		window.alert("Please enter your last name.");
		document.Entry.LName.focus();
		return false;
	}
	if ( document.Entry.Address.value == "")
	{
		window.alert("Please enter your address.");
		document.Entry.Address.focus();
		return false;
	}
	if ( document.Entry.City.value == "" )
	{
		window.alert("Please enter your city.");
		document.Entry.City.focus();
		return false;
	}
	if ( document.Entry.Province.selectedIndex == 0 )
	{
		window.alert("Please select your province.");
		document.Entry.Province.focus();
		return false;
	}
	if ( document.Entry.PostalCode.value == "" )
	{
		window.alert("Please enter a valid Postal or Zip Code.");
		document.Entry.PostalCode.focus();
		return false;
	}
	if ( document.Entry.Email.value == "" )
	{
		window.alert("Please enter a valid email address.");
		document.Entry.Email.focus();
		return false;
	}
	var atsymbol = parseInt(document.Entry.Email.value.indexOf("@"));
	var periodchar = parseInt(document.Entry.Email.value.indexOf("."));
	var courriel_len = parseInt(document.Entry.Email.value.length);
	if (document.Entry.Email.value != "")
	{
		if (atsymbol < 0 || periodchar < 0 || periodchar == (atsymbol + 1) || (courriel_len - 1) == periodchar )  
		{
			alert("Please enter a valid email address");
			document.Entry.Email.focus();
			return (false);
		}
	}
	if ( document.Entry.Rules.checked == false)
	{
		alert('Before entering the contest you must read, agree and accept the contest Rules and Regulations.');
		document.Entry.Rules.focus();
		return false;
	}	
	if ( document.Entry.OptIn.checked == true)
	{
		if ( document.Entry.Password.value == "" )
		{
			window.alert("Please enter your password.");
			document.Entry.Password.focus();
			return false;
		}
		if ( document.Entry.Password2.value == "" )
		{
			window.alert("Please confirm your password.");
			document.Entry.Password2.focus();
			return false;
		}
		if ( document.Entry.Password.value != document.Entry.Password2.value )
		{
			window.alert("Entered passwords do not match.");
			document.Entry.Password2.focus();
			return false;
		}
	}  
}
function Register_onsubmit() 
{
 	//Verify the form fields
	if ( document.Entry.FName.value == "")
	{
		window.alert("Please enter your first name.");
		document.Entry.FName.focus();
		return false;
	}
	if ( document.Entry.LName.value == "")
	{
		window.alert("Please enter your last name.");
		document.Entry.LName.focus();
		return false;
	}
	if ( document.Entry.Address.value == "")
	{
		window.alert("Please enter your address.");
		document.Entry.Address.focus();
		return false;
	}
	if ( document.Entry.City.value == "" )
	{
		window.alert("Please enter your city.");
		document.Entry.City.focus();
		return false;
	}
	if ( document.Entry.Province.selectedIndex == 0 )
	{
		window.alert("Please select your province.");
		document.Entry.Province.focus();
		return false;
	}
	if ( document.Entry.PostalCode.value == "" )
	{
		window.alert("Please enter a valid Postal or Zip Code.");
		document.Entry.PostalCode.focus();
		return false;
	}
	if ( document.Entry.Email.value == "" )
	{
		window.alert("Please enter a valid email address.");
		document.Entry.Email.focus();
		return false;
	}
	var atsymbol = parseInt(document.Entry.Email.value.indexOf("@"));
	var periodchar = parseInt(document.Entry.Email.value.indexOf("."));
	var courriel_len = parseInt(document.Entry.Email.value.length);
	if (document.Entry.Email.value != "")
	{
		if (atsymbol < 0 || periodchar < 0 || periodchar == (atsymbol + 1) || (courriel_len - 1) == periodchar )  
		{
			alert("Please enter a valid email address");
			document.Entry.Email.focus();
			return (false);
		}
	}	
}
function Entry_fr_onsubmit() 
{
 	//Verify the form fields
	if ( document.Entry.FName.value == "")
	{
		window.alert("Veuillez entrer votre prénom.");
		document.Entry.FName.focus();
		return false;
	}
	if ( document.Entry.LName.value == "")
	{
		window.alert("Veuillez entrer votre nom de famille.");
		document.Entry.LName.focus();
		return false;
	}
	if ( document.Entry.Email.value == "" )
	{
		window.alert("Veuillez entrer votre courriel.");
		document.Entry.Email.focus();
		return false;
	}
	var atsymbol = parseInt(document.Entry.Email.value.indexOf("@"));
	var periodchar = parseInt(document.Entry.Email.value.indexOf("."));
	var courriel_len = parseInt(document.Entry.Email.value.length);
	if (document.Entry.Email.value != "")
	{
		if (atsymbol < 0 || periodchar < 0 || periodchar == (atsymbol + 1) || (courriel_len - 1) == periodchar )  
		{
			alert("Veuillez entrer votre courriel.");
			document.Entry.Email.focus();
			return (false);
		}
	}
	if ( document.Entry.Address.value == "")
	{
		window.alert("Veuillez entrer votre adresse.");
		document.Entry.Address.focus();
		return false;
	}
	if ( document.Entry.City.value == "" )
	{
		window.alert("Veuillez entrer le nom de votre ville.");
		document.Entry.City.focus();
		return false;
	}
	if ( document.Entry.Province.selectedIndex == 0 )
	{
		window.alert("Veuillez indiquer votre province.");
		document.Entry.Province.focus();
		return false;
	}
	if ( document.Entry.Postal.value == "" )
	{
		window.alert("Veuillez entrer votre code postal.");
		document.Entry.Postal.focus();
		return false;
	}
	else
	{
        if (!/[A-Z][0-9][A-Z][\s-]?[0-9][A-Z][0-9]/i.test(document.Entry.Postal.value))
        {
			alert("Le code postal n’est pas valide. Veuillez l’entrer de l’une des façons suivantes : M4S 2G9  M4S2G9 M4S-2G9 ");
			document.Entry.Postal.focus();
			return false;
        }
    } 
	if ( String(document.Entry.AreaCode.value + document.Entry.Phone1.value + document.Entry.Phone2.value ).length != 10 || !(/\d{10}/.test(String(document.Entry.AreaCode.value + document.Entry.Phone1.value + document.Entry.Phone2.value))))
	{
		window.alert("Veuillez entrer les dix chiffres de votre numéro de téléphone.");
		document.Entry.AreaCode.focus();
		return false;
	}
	if ( document.Entry.Rules.checked == false)
	{
		alert('Pour continuer, vous devez lire et accepter le règlement du concours.');
		document.Entry.Rules.focus();
		return false;
	} 	
}
function Confirm_onsubmit() 
{
	var ThisAns = false;
	for (counter = 0;  counter < Confirm.eventID.length; counter++)
	{

	if (Confirm.eventID[counter].checked)
	ThisAns = true; 
	}

	if (!ThisAns)
	{
		alert("Please select the event you wish to attend.");
		return (false);
	}
}
function Confirm_fr_onsubmit() 
{
	var ThisAns = false;
	for (counter = 0;  counter < Confirm.eventID.length; counter++)
	{

	if (Confirm.eventID[counter].checked)
	ThisAns = true; 
	}

	if (!ThisAns)
	{
		alert("Please select the event you wish to attend.");
		return (false);
	}
}
function Dealer_onsubmit() 
{
	var ThisAns = false;
	for (counter = 0;  counter < Dealers.DealerCode.length; counter++)
	{

	if (Dealers.DealerCode[counter].checked)
	ThisAns = true; 
	}

	if (!ThisAns)
	{
		alert("Which dealer event would you like to attend?");
		return (false);
	}
}
function Dealer_fr_onsubmit() 
{
	var ThisAns = false;
	for (counter = 0;  counter < Dealers.DealerCode.length; counter++)
	{

	if (Dealers.DealerCode[counter].checked)
	ThisAns = true; 
	}

	if (!ThisAns)
	{
		alert("Which dealer event would you like to attend?");
		return (false);
	}
}
browsok = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 3)) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=3)));
if(browsok)
	{
	
	// MouseOn 
	tr1 = new Image();
	tr1.src = "images/btn_ctgiftcards-on.gif";
	by1 = new Image();
	by1.src = "images/btn_order-on.gif";
	fr1 = new Image();
	fr1.src = "images/btn_enter-on.gif";

	// MouseOver
	tr0 = new Image();
	tr0.src = "images/btn_ctgiftcards.gif";
	by0 = new Image();
	by0.src = "images/btn_order.gif";
	fr0 = new Image();
	fr0.src = "images/btn_enter.gif";

	//dropdown toggle
	toggleImg1 = new Image();
	toggleImg1.src = "images/toggle_close.jpg";
	toggleImg0 = new Image();
	toggleImg0.src = "images/toggle_open.jpg";
	}

function BNB_mouseon(n)
	{
	if(browsok)
		{
		imageON = eval(n + "1.src");
		document [n].src = imageON;
		}
	}

function BNB_mouseoff(n)
	{
	if(browsok)
		{
		imageOFF = eval(n + "0.src");
		document [n].src = imageOFF;
		}
	}

function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
/* Functions that swaps images. */
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function isValid(pattern, str) 
{
    if ( !pattern.test(str) )
       {
            return false;
       }
    else
            return true;
}
	                
function ltrim( cStr )
{
	cStr	=	String(cStr);
	if ( cStr == "0" || cStr == 'null' || cStr.substr(0,3) == '000' )
		return "";
	var oReg = /^(\s+)/;
	return cStr.replace(oReg,'');
}


function rtrim( cStr )
{
	cStr	=	String(cStr);
	var oReg = /(\s+)$/;
	return cStr.replace(oReg,'');
}


function alltrim( cStr )
{
	if (String(cStr) == 'null' )
		return "";
	else
		return ltrim(rtrim(cStr));
}

function strtran( cStr )
{
	cStr	=	String(cStr);
	if ( cStr == '')
		return "";
		
	var oReg = /\s/g;
	return cStr.replace(oReg,'');

}

function checkLength(curr, next)
{
if (curr.value.length == curr.maxLength && iens6)
	{
	next.focus();
	}
}
//-->
