function submitPage(linkid) {
var ProductName;
var location_of_dot;
var ProductCategory;
var SubmitString;
var rExp = / /g;
with (document.forms["contactus"]) {
ProductName = productname.options[productname.selectedIndex].value;
location_of_dot = ProductName.indexOf(".");
ProductCategory =  ProductName.substr(0,location_of_dot);
SubmitString ="contactus.frp?id="+linkid+"&prod="+ProductName+"&cat="+ProductCategory;
SubmitString = SubmitString.replace(rExp,"%20");
action=SubmitString;
if (validate())
submit();

}
}

function update(linkid) {
var ProductName;
var location_of_dot;
var ProductCategory;
var SubmitString;
var Province;
var rExp = / /g;
with (document.forms["contactus"]) {
ProductName = productname.options[productname.selectedIndex].value;
if (ProductName == "")
return false;

location_of_dot = ProductName.indexOf(".");
ProductCategory =  ProductName.substr(0,location_of_dot);
productcat.value=ProductCategory;
Province = province.options[province.selectedIndex].value;
SubmitString = "/web/link?id="+linkid+"&prod="+ProductName+"&cat="+ProductCategory+"&telephone="+telephone.value+"&emailaddress="+submitteremail.value+"&fname="+submitterfirstname.value+"&lname="+submitterlastname.value+"&address="+submitteraddress.value+"&pcode="+zipcode.value+"&company="+company.value+"&city="+city.value+"&prov="+Province;
SubmitString = SubmitString.replace(rExp,"%20");
document.location.href=SubmitString;

}
}

function resetFields() {
	with (document.forms["contactus"]) {
		submitterfirstname.value = "";
		submitterlastname.value = "";
		submitteremail.value = "";
		submitteraddress.value = "";
		city.value = "";
		zipcode.value = "";
		telephone.value = "";
		modelnumber.value = "";
		serialnumber.value = "";
		province.value = "0";
		contactreason.value = "";
		purchasemonth.selectedIndex = 0;
		purchaseday.selectedIndex = 0;
		purchaseyear.selectedIndex = 0;
		productname.value = "";
		dealer.value = "";
		submittercomment.value = "";
		company.value = "";
	}
}

function validDate(year, month, day) {
	dt = new Date(year,month-1,day);
	return (dt.getDate() == day && dt.getMonth() == month-1 && ((year < 2000?1900:0)+dt.getYear() == year));
}

function validZipCode(zipcode, allowempty) {
	if (zipcode.length < 6 || zipcode.length > 7) 
		return (zipcode.length == 0 && allowempty);
	zipcode = zipcode.toUpperCase();
	zipcode=zipcode.replace(" ","");
	for(j=0; j<6; j++) {
		ch = zipcode.charAt(j);
		if (j%2==0) {	// this digit must be alpha
			if(!(ch >= 'A' && ch <= 'Z')) {
				return false;
			}
		}
		if (j%2==1) { // this digit must be numeric
			if (!(ch >= '0' && ch <= '9')) {
				return false;
			}
		}
	}
	return true;
}

function validCanadaPhone(phone, allowempty) {
	cleanPhone = "";
	
	for(i=0; i<phone.length; i++) {
		ch = phone.charAt(i);
		if (ch != '-' && ch != ' ') {
			if(!(ch >= '0' && ch <= '9'))
				return false;
			cleanPhone = cleanPhone + ch;
		} 
       }
	return ((cleanPhone.length > 0  && cleanPhone.length <=20) || (cleanPhone.length==0 && allowempty));
}

function validEmailAddress(email, allowempty) {
	if (email.length == 0)
		return allowempty;
	a = email.split('@');
	if (a.length == 2) {
		if (a[0].length >= 2 && a[1].length >= 2) {
			//a = email.split('.');
			//if (a.length >= 2) {
				//for(i=0; i < a.length; i++) {
					//if (a[i].length < 2)
						//return false;
				//}
				return true;
			//}
		}
	}
	return false;
}

function validModelSerial(number, allowempty) {
	number = number.toUpperCase();
	cleanNumber = "";
	for(i=0; i<number.length; i++) {
		ch = number.charAt(i);
		if (ch != '-' && ch != ' ') {
			if((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z'))
				cleanNumber = cleanNumber + ch;
			else
				return false;
		} 
       }
	return (cleanNumber.length <= 20) || (cleanNumber.length == 0 && allowempty);
}


function validate() {
	strErrors="";
	with (document.forms["contactus"]) {
		if (submitterfirstname.value.length == 0) // submitterFirstName is required
			strErrors = "First Name must be provided\n";
		if (submitterlastname.value.length == 0) // submitterLastName is required
			strErrors = strErrors + "Last Name must be provided\n";
		//if 	(city.value.length == 0) // city is required
			//strErrors = strErrors + "City must be provided\n";	
		//if 	(zipcode.value.length == 0) // zipcode is required
			//strErrors = strErrors + "Postal Code must be provided\n";
		//if (!validZipCode(zipcode.value, true)) //zipcode
			//strErrors = strErrors + "Invalid postal code\n";
		if (!validEmailAddress(submitteremail.value, false)) //submitterEmail is required
			strErrors = strErrors + "Invalid email address\n";
		//if 	(telephone.value.length == 0) // telephone is required
			//strErrors = strErrors + "Phone number must be provided\n";		
		//if (!validCanadaPhone(telephone.value, true)) //telephone
			//strErrors = strErrors + "Invalid phone number\n";
		if (!validModelSerial(modelnumber.value, true)) //modelnumber
			strErrors = strErrors + "Invalid model number\n";
		if (!validModelSerial(serialnumber.value, true)) //serialnumber
			strErrors = strErrors + "Invalid serial number\n";
		//if (province.value == "0") //province is required
			//strErrors = strErrors + "Province must be provided\n";
		if ((contactreason.value.length == 0) && (contactreason.type != "hidden")) //contact reason is required
			strErrors = strErrors + "Reason of inquire must be provided\n";
		if (purchasemonth.selectedIndex != 0 && purchaseday.selectedIndex != 0 && purchaseyear.selectedIndex != 0) {
			if (!validDate(purchaseyear.options[purchaseyear.selectedIndex].value,purchasemonth.options[purchasemonth.selectedIndex].value,purchaseday.options[purchaseday.selectedIndex].value))
				strErrors = strErrors + "Invalid purchase date\n";
		}
		if (submittercomment.value.length > 3200) // too long
			strErrors = "Comment size too big\n";
	}
	if (strErrors.length>0)
		alert(strErrors);
	return (strErrors.length==0);
}
