function change_vals() {
	var f = document.form_warranty;
	change_values(f.how_heard.value, 2);
}

function change_password(f) {
	var err = new Array();
	if (f.passwd_n.value == '') {
		err.push('You must enter a valid password.')
		highlight(f.passwd_n, 1);
	} else if (f.passwd_n.value.length < 7) {
		err.push('Your password must be at least 7 characters long.');
		highlight(f.passwd_n, 1);
	} else if (f.passwd_c.value == '') {
		err.push('You must confirm your password.');
		highlight(f.passwd_n, 0);
		highlight(f.passwd_c, 1);
	} else if (f.passwd_n.value != f.passwd_c.value) {
		err.push('Your passwords do not match.');
		highlight(f.passwd_n, 1);
		highlight(f.passwd_c, 1);
	} else {
		highlight(f.passwd_n, 0);
		highlight(f.passwd_c, 0);
	}
	if (err.length > 0) {
		show_dialog("Change Password Errors", "The following fields had errors:<ul><li>" + err.join("</li><li>") + "</ul>", 360, 200);
	} else {
		var params = $("#form_change_password").serialize();
		ajax_updater("/functions/cb_support_change_password.php", params, "message_form_password", "post");
	}
	return false;
}

function change_values(opt, v) {
	if (parseInt(v) == 2) {
		var f = document.form_warranty;
	} else {
		var f = document.form_contact;
	}
	var div_id1 = document.getElementById('li_where_heard' + v);
	var div_id2 = document.getElementById('li_what_issue' + v);
	var s = f.where_heard.options;
	if (where_values[opt].length != undefined) {
		s.length = where_values[opt].length + 1;
		if (s.length > 2) {
			div_id1.style.display = 'block';
			s[0].value = 0;
			s[0].text = '-- Select --';
			for (i = 1; i < (s.length); i++) {
				s[i].value = where_values[opt][(i - 1)];
				s[i].text = where_values[opt][(i - 1)];
			}
			check_hidden = true;
		} else {
			div_id1.style.display = 'none';
			check_hidden = false;
		}
	
		if (parseInt(opt) == 1 || parseInt(opt) == 7)
			div_id2.style.display = 'block';
		else
			div_id2.style.display = 'none';
	}
	f.where_heard.selectedIndex = 0;
}

function check_state_zip(s, z, callback) {
	$.ajax({
		type: "get",
		async: false,
		processData: true,
		url: "functions/cb_check_state_zip.php",
		data: "s=" + s + "&z=" + z,
		cache: false,
		success: function(d) {
			callback(d);
		}
	});
}

// Clean serial removes unneccesary characters.
function clean_serial(s) {
	var x = s.replace(/-/g, "");
	x = x.replace(/_/g, "");
	x = x.replace(/\*/g, "");
	x = x.replace(/\//g, "");
	return x;
}

function open_units(c, v) {
	var block = document.getElementById('div_units_purchased');
	if (v == "N") {
		block.style.display = "none";
	} else {
		block.style.display = "block";
	}
}

function validate_contact(f) {
	var err = new Array();
	
	if (f.company_name.value == "") {
		err.push("Company Name is required.");
		highlight(f.company_name, 1);
	} else {
		highlight(f.company_name, 0);
	}
	if (f.title.value == "") {
		err.push("Title is required.");
		highlight(f.title, 1);
	} else {
		highlight(f.title, 0);
	}
	if (f.first_name.value == "") {
		err.push("First Name is required.");
		highlight(f.first_name, 1);
	} else {
		highlight(f.first_name, 0);
	}
	if (f.last_name.value == "") {
		err.push("Last Name is required.");
		highlight(f.last_name, 1);
	} else {
		highlight(f.last_name, 0);
	}
	if (f.address1.value == "") {
		err.push("Street Address is required.");
		highlight(f.address1, 1);
	} else {
		highlight(f.address1, 0);
	}
	if (f.city.value == "") {
		err.push("City is required.");
		highlight(f.city, 1);
	} else {
		highlight(f.city, 0);
	}
	if (f.state.selectedIndex == 0) {
		err.push("State is required.");
		highlight(f.state, 1);
	} else {
		highlight(f.state, 0);
	}
	if (f.postal_code.value == "") {
		err.push("Zip Code is required.");
		highlight(f.postal_code, 1);
	} else {
		highlight(f.postal_code, 0);
	}
	if (f.country_code.selectedIndex == 0) {
		err.push("Country is required.");
		highlight(f.country_code, 1);
	} else {
		highlight(f.country_code, 0);
	}
	/*if (f.country_code.value == "US" && f.state.value != "" && f.postal_code.value != "") {
		check_state_zip(f.state.value, f.postal_code.value, function(status) {
			if (status.toString() != "1") {
				err.push("The zip code and state you've selected do not match.");
			}
		});
	}*/
	if (f.email.value == "") {
		err.push("Email Address is required.");
		highlight(f.email, 1);
	} else if (!is_email(f.email.value)) {
		err.push("Email Address is not in the correct format");
		highlight(f.email, 1);
	} else {
		highlight(f.email, 0);
	}
	if (f.phone.value == "") {
		err.push("Phone number is required.");
		highlight(f.phone, 1);
	} else {
		highlight(f.phone, 0);
	}
	if (f.how_heard.selectedIndex == 0) {
		err.push("How did you hear about MovinCool is required.");
		highlight(f.how_heard, 1);
	} else {
		highlight(f.how_heard, 0);
		var hh = f.how_heard.value;
		if (where_values[hh].length > 1 && f.where_heard.selectedIndex == 0) {
			err.push("Please select where you heard about us.");
			highlight(f.where_heard, 1);
		} else {
			highlight(f.where_heard, 0);
		}
		if (f.how_heard.selectedIndex == 1 || f.how_heard.selectedIndex == 7) {
			if (f.source_month.selectedIndex == 0) {
				err.push("Please select the date of the source.");
				highlight(f.source_month, 1);
			} else {
				highlight(f.source_month, 0);
			}
		}
	}
	if (f.primary_use.selectedIndex == 0) {
		err.push("Primary Use of the Unit? is required.");
		highlight(f.primary_use, 1);
	} else {
		highlight(f.primary_use, 0);
	}
	if (f.where_used.selectedIndex == 0) {
		err.push("What is the Unit Cooling? is required.");
		highlight(f.where_used, 1);
	} else {
		highlight(f.where_used, 0);
	}
	if (f.purchase_factor.selectedIndex == 0) {
		err.push('Primary Purchase Factor is required.');
		highlight(f.purchase_factor, 1);
	} else {
		highlight(f.purchase_factor, 0);
	}
	if (f.purchase_timeframe.selectedIndex == 0) {
		err.push('Purchase Timeframe is required.');
		highlight(f.purchase_timeframe, 1);
	} else {
		highlight(f.purchase_timeframe, 0);
	}
	if (f.business_type.selectedIndex == 0) {
		err.push("Business Type is required.");
		highlight(f.business_type, 1);
	} else {
		highlight(f.business_type, 0);
	}
	if (err.length > 0) {
		show_dialog("Contact Form Submission Errors", "The following fields had errors:<ul><li>" + err.join("</li><li>") + "</ul>", 400);
		return false;
	} else {
		return true;
	}
}

function validate_login(f) {
	var err = new Array();
	if (f.email.value == "") {
		err.push("Email Address is required.");
		highlight(f.email, 1);
	} else if (!is_email(f.email.value)) {
		err.push("Email Address is not in the correct format.");
		highlight(f.email, 1);
	} else {
		highlight(f.email, 0);
	}
	if (f.passwd.value == "") {
		err.push("Password is required.");
		highlight(f.passwd, 1);
	} else {
		highlight(f.passwd, 0);
	}
	if (err.length > 0) {
		show_dialog("Download Login Form Errors", "The following fields had errors:<ul><li>" + err.join("</li><li>") + "</ul>");
	} else {
		ajax_updater("/functions/cb_login_download.php", "e=" + f.email.value + "&p=" + f.passwd.value, "login_message", "post");
	}
	return false;
}

function validate_promotion(f) {
	var err = validate_registration(f);
	
	if (!f.accept_rules.checked) {
		err.push("You must accept the Official Rules.");
		highlight(document.getElementById("div_accept_rules"), 1);
	} else {
		highlight(document.getElementById("div_accept_rules"), 0);
	}
	if (err.length > 0) {
		show_dialog("Warranty Submission Errors", "The following fields had errors:<ul><li>" + err.join("</li><li>") + "</ul>", 350);
		return false;
	} else {
		return true;		
	}
}

function validate_registration(f) {
	var err = new Array();
	
	if (f.company_name.value == "") {
		err.push("Company Name is required.");
		highlight(f.company_name, 1);
	} else {
		highlight(f.company_name, 0);
	}
	if (f.title.value == "") {
		err.push("Title is required.");
		highlight(f.title, 1);
	} else {
		highlight(f.title, 0);
	}
	if (f.first_name.value == "") {
		err.push("First Name is required.");
		highlight(f.first_name, 1);
	} else {
		highlight(f.first_name, 0);
	}
	if (f.last_name.value == "") {
		err.push("Last Name is required.");
		highlight(f.last_name, 1);
	} else {
		highlight(f.last_name, 0);
	}
	if (f.address1.value == "") {
		err.push("Street Address is required.");
		highlight(f.address1, 1);
	} else {
		highlight(f.address1, 0);
	}
	if (f.city.value == "") {
		err.push("City is required.");
		highlight(f.city, 1);
	} else {
		highlight(f.city, 0);
	}
	if (f.state.selectedIndex == 0) {
		err.push("State is required.");
		highlight(f.state, 1);
	} else {
		highlight(f.state, 0);
	}
	if (f.postal_code.value == "") {
		err.push("Zip Code is required.");
		highlight(f.postal_code, 1);
	} else {
		highlight(f.postal_code, 0);
	}
	if (f.country_code.selectedIndex == 0) {
		err.push("Country is required.");
		highlight(f.country_code, 1);
	} else {
		highlight(f.country_code, 0);
	}
	if (f.dealer_name.selectedIndex == 0) {
		err.push("Dealer Name is required.");
		highlight(f.dealer_name, 1);
	} else {
		highlight(f.dealer_name, 0);
	}
	if (f.dealer_state.selectedIndex == 0) {
		err.push("Dealer Location is required.");
		highlight(f.dealer_state, 1);
	} else {
		highlight(f.dealer_state, 0);
	}
	if (f.business_type.selectedIndex == 0) {
		err.push("Business Type is required.");
		highlight(f.business_type, 1);
	} else {
		highlight(f.business_type, 0);
	}
	if (f.how_heard.selectedIndex == 0) {
		err.push("How did you hear about MovinCool is required.");
		highlight(f.how_heard, 1);
	} else {
		highlight(f.how_heard, 0);
		var hh = f.how_heard.value;
		if (where_values[hh].length > 1 && f.where_heard.selectedIndex == 0) {
			err.push("Please select where you heard about us.");
			highlight(f.where_heard, 1);
		} else {
			highlight(f.where_heard, 0);
		}
		if (f.how_heard.selectedIndex == 1 || f.how_heard.selectedIndex == 7) {
			if (f.source_month.selectedIndex == 0) {
				err.push("Please select the date of the source.");
				highlight(f.source_month, 1);
			} else {
				highlight(f.source_month, 0);
			}
		}
	}
	if (f.phone.value == "") {
		err.push("Phone number is required.");
		highlight(f.phone, 1);
	} else {
		highlight(f.phone, 0);
	}
	if (f.email.value == "") {
		err.push("Email Address is required.");
		highlight(f.email, 1);
	} else if (!is_email(f.email.value)) {
		err.push("Email Address is not in the correct format");
		highlight(f.email, 1);
	} else {
		highlight(f.email, 0);
	}
	// Check all of the serial numbers if they have the right number.
	var model_id = 0;
	var serial = 0;
	var atleastone = false;
	var serial_test = false;
	var serial_array = new Array();
	for (i = 1; i <= 5; i++) {
		if (f.elements["model" + i].selectedIndex != 0) {
			highlight(f.elements["model" + i], 0);
			atleastone = true;
			if (f.elements["serial_number" + i].value == "") {
				err.push("Serial #" + i + " is required.");
				highlight(f.elements["serial_number" + i], 1);
			} else {
				model_id = parseInt(f.elements["model" + i].value);
				serial = clean_serial(f.elements["serial_number" + i].value);
				serial_array.push(serial);
				f.elements["serial_number" + i].value = serial;
				// Check that the length of the serial number is correct.
				if (serials[model_id].length > 0) {
					serial_test = false;
					for (j = 0; j < serials[model_id].length; j++) {
						if (serial.length == parseInt(serials[model_id][j])) {
							serial_test = true;
						}
					}
					// If the item doesn't pass the test, show an error.
					if (!serial_test) {
						err.push("Serial #" + i + " must be " + serials[model_id] + " characters long.");
						highlight(f.elements["serial_number" + i], 1);
					} else {
						highlight(f.elements["serial_number" + i], 0);
					}
				}
			}
		}
	}
	if (!atleastone) {
		err.push("At least one MovinCool model is required.");
		highlight(f.model1, 1);
		err.push("At least one Serial Number is required.");
		highlight(f.serial_number1, 1);
	}
	// Check that the serials are all unique.
	var unique_array = array_unique(serial_array);
	if (serial_array.length != unique_array.length) {
		err.push("All serial numbers must be unique.");
		for (i = 0; i < serial_array.length; i++) {
			if (in_array_count(serial_array[i], serial_array) > 1) {
				highlight(f.elements["serial_number" + (i + 1)], 1);
			}
		}
	}
	if (f.purchase_date.value == "") {
		err.push("Date of Purchase is required.");
		highlight(f.purchase_date, 1);
	} else if (!is_valid_date(f.purchase_date.value)) {
		err.push("Date of purchase is not in the format mm/dd/yyyy");
		highlight(f.purchase_date, 1);
	} else {
		highlight(f.purchase_date, 0);
	}
	if (!f.purchased_before[0].checked && !f.purchased_before[1].checked) {
		err.push('Have you previously purchased a MovinCool? is required');
	}
	if (f.purchased_before[0].checked == true && f.units_purchased.value == '') {
		err.push("How many units you've purchased before is required.");
		highlight(f.units_purchased, 1);
	} else {
		highlight(f.units_purchased, 0);
	}
	if (f.use_type.selectedIndex == 0) {
		err.push('Primary use of the unit is required');
		highlight(f.use_type, 1);
	} else {
		highlight(f.use_type, 0);
	}
	if (f.primary_use.selectedIndex == 0) {
		err.push('Where is the unit used is required');
		highlight(f.primary_use, 1);
	} else {
		highlight(f.primary_use, 0);
	}
	if (f.purchase_factor.selectedIndex == 0) {
		err.push('Factor that influenced purchase is required');
		highlight(f.purchase_factor, 1);
	} else {
		highlight(f.purchase_factor, 0);
	}
	return err;
}

function validate_warranty(f) {
	var err = validate_registration(f);
	if (err.length > 0) {
		show_dialog("Warranty Submission Errors", "The following fields had errors:<ul><li>" + err.join("</li><li>") + "</ul>", 350);
		return false;
	} else {
		return true;		
	}
}