function IsNumeric(sText){
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
function getCheckedValue(radioObj) {
      if(!radioObj)
            return "";
      var radioLength = radioObj.length;
      if(radioLength == undefined)
            if(radioObj.checked)
                  return radioObj.value;
            else
                  return "";
      for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                  return radioObj[i].value;
				  
            }
      }
      return "";
}



function setCheckedValue(radioObj, newValue) {
	if(!radioObj){return;}
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function isValidPostalcode(postalcode) {
	if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
	else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return true;
	else return false;
}

function isValidZipcode(zipcode) {
   if (!(zipcode.length == 5)) return false;
   if (!isNumeric(zipcode)) return false;
   return true;
}

function formatpostal(lang)
{	
	//alert(document.getElementById('country').value);
	if (document.getElementById('country').value == "CA")
	{
		if (document.getElementById('postalCode').value != "")
		{
			var postalcodevalue = document.getElementById('postalCode').value.toUpperCase()
			
			postalcodevalue = postalcodevalue.replace(" ","");
			
			document.getElementById('postalCode').value = postalcodevalue;
			
			if (isValidPostalcode(postalcodevalue) == false )
			{
				if (lang == "English")
				{
					alert("Invalid Canadian postal code format. If you live outside of Canada please select your country from the list below.");
					document.getElementById('postalCode').focus();
					return;
				}
				else
				{
					var y=document.createElement('span');
					y.innerHTML="Format de code postal canadien invalide. Si vous vivez \340 l'\351tranger, veuillez s\351lectionner votre pays dans la liste qui suit.";
					alert(y.innerHTML);
					document.getElementById('postalCode').focus();
					return;
				}
			}
			else
			{
				postalcodevalue = postalcodevalue.substr(0,3) + " " + postalcodevalue.substr(3,3);
				document.getElementById('postalCode').value = postalcodevalue;
	
			}
		}
	}
}

function formatphone(lang)
{
	if ((document.getElementById('country').value == "CA") || (document.getElementById('country').value == "US"))
	{
	if (document.getElementById('telephone').value != "")
	{
		var telephonenum = document.getElementById('telephone').value;
		
		var tstring = "";
		telephonenum = '' + telephonenum;
		splitstring = telephonenum.split(" ");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];		
		telephonenum = tstring;

		var tstring = "";
		telephonenum = '' + telephonenum;
		splitstring = telephonenum.split("(");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];		
		telephonenum = tstring;
		
		var tstring = "";
		telephonenum = '' + telephonenum;
		splitstring = telephonenum.split(")");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];		
		telephonenum = tstring;	
		
		var tstring = "";
		telephonenum = '' + telephonenum;
		splitstring = telephonenum.split("-");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];		
		telephonenum = tstring;	
		
		var tstring = "";
		telephonenum = '' + telephonenum;
		splitstring = telephonenum.split(".");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];		
		telephonenum = tstring;
		
		telephonenum = telephonenum.replace(" ","");
		telephonenum = telephonenum.replace("(","");		
		telephonenum = telephonenum.replace(")","");		
		telephonenum = telephonenum.replace("-","");
		telephonenum = telephonenum.replace(".","");
		document.getElementById('telephone').value = telephonenum;		
		
			if (telephonenum.length > 10 || telephonenum.length < 10)
			{
				if (lang == "English")
				{
					alert("Invalid telephone number format. Please use the following format: (xxx) xxx-xxxx");
					document.getElementById('telephone').focus();
					return;
				}
				else
				{
					alert("Format de num\351ro de t\351l\351phone invalide. Veuillez utiliser le format suivant : (xxx) xxx-xxxx");
					document.getElementById('telephone').focus();
					return;
				}
			}
			else
			{
				telephonenum = "(" + telephonenum.substr(0,3) + ")" + " " + telephonenum.substr(3,3) + "-" + telephonenum.substr(6,4);
				document.getElementById('telephone').value = telephonenum;
	
			}		
	}
	}
}


function validateemail(lang){
	if (document.getElementById('email').value != "")
	{
		if (document.getElementById('email').value != document.getElementById('confirmEmail').value)
		{
		document.getElementById('confirmEmail').value = "";
		}
		var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var re = new RegExp(email_filter);
		if (!document.getElementById('email').value.match(re)) {
			if (lang == "English")
			{			
				alert("Your email addresses is not valid.");
				document.getElementById('email').focus();
				return;
			}
			else
			{
				alert("Votre adresse de courriel n'est pas valide.");
				document.getElementById('email').focus();
				return;			
			}
		}
	}
}

function checkemail(lang)
{
	if (document.getElementById('confirmEmail').value != "")
	{
		if (document.getElementById('email').value != document.getElementById('confirmEmail').value)
		{
			if (lang == "English")
			{			
				alert("Your email addresses do not match.");
				document.getElementById('confirmEmail').focus();
				return;
			}
			else
			{
				alert("Vos adresses de courriel ne correspondent pas.");
				document.getElementById('confirmEmail').focus();
				return;			
			}
		}
	}
}



function saveBtn_onclick(lang) {
/*Validate textarea length before submitting the form*/

var maxChar = 500
if (document.form1.cardMessage.value.length > maxChar) {
diff=document.form1.cardMessage.value.length - maxChar;
if (diff>1)
diff = diff + " characters";
else
diff = diff + " character";

if (lang == "English")
{
	alert("The personal message field is limited to " + maxChar + " characters\n" + "Please reduce the text by " + diff);
}
else
{
	alert("Le champ de message personnel est limit\351 &agrave; " + maxChar + " caract&egrave;res. Veuillez r\351duire votre texte de " + diff + " caract&egrave;res.");
}
document.form1.cardMessage.focus();
return (false);
}
}

