// JavaScript Document
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,j,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

    for (i=0; i<(args.length-2); i+=3) {
		test=args[i+2];
		valEl=document.getElementById(args[i]);
		if (valEl) {
			nm=valEl.name;
			if ((val=valEl.value)!="") {
				valEl.className='none';
        		if (test.indexOf('isEmail')!=-1) {
					p=val.indexOf('@');
          			if (p<1 || p==(val.length-1)) {
						errors+='- E-mail field must contain an e-mail address.\n';
						valEl.className='error';
					}
        		} else if(test.indexOf('isPostal')!=-1){
					if(!isPostCode(args[i])){
						errors+='- Postal Code field must contain a valid Canadian Postal Code (X1X1X1)\n';
						valEl.className='error';
					}
				} else if (test!='R') {
					num = parseFloat(val);
          			if (isNaN(val)){
						errors+='- '+nm+' must contain a number.\n';
					}
          			if (test.indexOf('inRange') != -1) {
						p=test.indexOf(':');
            			min=test.substring(8,p);
						max=test.substring(p+1);
            			if (num<min || max<num) {
							errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
						}
      				}
				}
			} else if (test.charAt(0) == 'R') {
				switch(nm) {
					case 'first':
						errors += '- First Name is required.\n';
						break;
					case 'last':
						errors += '- Last Name is required.\n';
						break;
					case 'zip':
						errors += '- Valid Postal Code is required.\n';
						break;
					case 'custom_homePhone':
						errors += '- Evening phone number is required.\n';
						break;
					case 'email':
						errors += '- E-mail field must contain an e-mail address.\n';
						break;
					case 'phone':
						errors += '- Daytime phone Number is required. \n';
						break;
					case 'address_1':
						errors += '- Mailing Address is required. \n';
						break;
					case 'city':
						errors += '- City name is required. \n';
						break;
					case 'custom_Province':
						errors += '- Province name is required. \n';
						break;
					case 'postalCode':
						errors += '- Valid Postal Code is required.\n';
						break;
					default:
  					errors += '- '+nm+' is required.\n';
				}			
				valEl.className='error';
			}
		}
    }
	x=document.getElementById('errorIndicator').childNodes[0];
	if (errors)
		{
			x.nodeValue='Please fill in all required fields.';
			alert('The following error(s) occurred:\n'+errors)
		} else {
			x.nodeValue='Please wait...';
		};
    document.MM_returnValue = (errors == '');
} }
function isPostCode(id){ // checks cdn codes only
	entry=document.getElementById(id).value;
	strlen=entry.length;if(strlen!==6){return false;}
	entry=entry.toUpperCase(); //in case of lowercase
	//Check for legal characters,index starts at zero
	s1='ABCEGHJKLMNPRSTVXY';
	s2=s1+'WZ';
	d3='0123456789';
	if(s1.indexOf(entry.charAt(0))<0){return false;}
	if(d3.indexOf(entry.charAt(1))<0){return false;}
	if(s2.indexOf(entry.charAt(2))<0){return false;}
	if(d3.indexOf(entry.charAt(3))<0){return false;}
	if(s2.indexOf(entry.charAt(4))<0){return false;}
	if(d3.indexOf(entry.charAt(5))<0){return false;}
	document.getElementById(id).value = entry;
	return true;
}