// JavaScript Document

// Begin function replaceCharacter
// This function will replace the_search_character with the_replace_character.
// You can specify where you want to replace this character.
// If the_location = start, the function will replace all matching characters 
// at the beginning of the string until if locates a non-matching character.
// If the_location = end, the function will replace all matching characters at
// the end of the string and will continue backwards until it locates a 
// non-matching character. If the_location = start_end, the function will do
// both the actions described above in start and end. If the_location = all,
// the function will replace every occurence of the_search_character with
// the_replace_character. This sample call would remove all leading and trailing
// spaces from a string: trimSpaces(this, 'start_end')

function replaceCharacter(the_field_name, the_search_character, the_replace_character, the_location) {
	var the_string = the_field_name.value;
	var string_start = 0;
	var string_end = (the_string.length - 1);
	var starting_string = "";
	var ending_string = "";
	var revised_string = "";

	if (the_location == "start_end" || (the_location == "start" || the_location == "end")) {
		if (the_location == "start_end" || the_location == "start") {
			for (var start_loop = 0; start_loop < the_string.length; start_loop++) {
				if (the_string.charAt(start_loop) == the_search_character) {
					starting_string += the_replace_character;
				} else {
					string_start = start_loop;
					break;
				}
			}
		}
		if (the_location == "start_end" || the_location == "end") {
			for (var end_loop = (the_string.length - 1); end_loop >= 0; end_loop --) {
				if (the_string.charAt(end_loop) == the_search_character) {
					ending_string = the_replace_character + ending_string;
				} else {
					string_end = (end_loop);
					break;
				}
			}
		}
	} else if (the_location == "all") {
		for (var start_loop = 0; start_loop < the_string.length; start_loop ++) {
			var string_start = start_loop;
			if (the_string.charAt(start_loop) == the_search_character) {
				revised_string += the_replace_character;
			} else {
				revised_string += the_string.charAt(start_loop);
			}
		}
	} else {
		// Unexpected location - do nothing
	}
	if (the_location != "all") {
		revised_string = starting_string;
		if (the_string.length != 1) {
			for (var new_string_loop = string_start; new_string_loop <= string_end; new_string_loop++) {
				revised_string += the_string.charAt(new_string_loop);
			}
		}
		revised_string += ending_string;
	}
	the_field_name.value = revised_string;
}
// End function replaceCharacter

// Begin function checkForm
// This function accepts an array named "required" which contains any number of pairs of fields 
// and error messages. The function will automatically determine the field type and check to see
// if the user has completed it. If not, the error message will be displayed and focus will be 
// set on the required field. If all of the required fields have been completed, the function will
// return true. I generally call this function using a validate funtion. The simplest version would
// look like:
// function validate() {
// 	 var required_fields = new Array()
// 	 required_fields[0] = document.the_form.username;
// 	 required_fields[1] = "Please enter a value in the Username field.";
// 	 if (checkForm(required_fields)) {
// 		document.the_form.submit();
// 		return true;
//	 } else {
// 		return false;
// 	 }
// }

function checkForm(required) {
	for (var loop = 0; loop < required.length; loop += 2) {
		var the_field = required[loop];
		if ((the_field.type == "text" || the_field.type == "password" || the_field.type == "textarea") && the_field.value == "") {
			window.alert(required[loop + 1]);
			the_field.focus();
			return false;
		} else
		if (the_field.type == "select-one" && the_field.options[the_field.selectedIndex].value == "") {
			window.alert(required[loop + 1]);
			the_field.focus();
			return false;
		} else 
		if (the_field.type == "select-multiple") {
			for (var multiple_loop = 0; multiple_loop < the_field.length; multiple_loop ++) {
				if (multiple_loop == the_field.length - 1 && !the_field.options[multiple_loop].selected) {
					window.alert(required[loop + 1]);
					the_field.focus();
					return false;
				} else 
				if (the_field.options[multiple_loop].selected) {
					break;
				}
			}
		} else
		if (the_field[0]) {
			if (the_field[0].type == "radio" || the_field[0].type == "checkbox") {
				for (var rc_loop = 0; rc_loop < the_field.length; rc_loop ++) {
					if (rc_loop == the_field.length - 1 && !the_field[rc_loop].checked) {
						window.alert(required[loop + 1]);
						the_field[0].focus();
						return false;
					} else 
					if (the_field[rc_loop].checked) {
						break;
					}
				}
			}
		}
	}
	return true;
}
// End function checkForm

// Begin function checkMax
// This function will chop a string containing more characters then the limit set in the max variable.
// Since you can set a limit on input type=text with maxlength this really only applies to textareas.
// A sample call would be checkMax(this, 'Comments', '200')
function checkMax(the_field_name, the_field_handle, max) {
	var the_string = the_field_name.value;
	if (the_string != "") {
		if (the_string.length > max) {
			var new_string = "";
			for (var new_loop = 0; new_loop < (max - 3); new_loop ++) {
				new_string += the_string.charAt(new_loop);
			}
			new_string += "...";
			the_field_name.value = new_string;
			window.alert("You can only enter " + max + " characters in the " + the_field_handle + " field. Your entry has been shortened to meet this limit.")
		}
	}
}
// End function checkMax

// Begin function validateDate
function validateDate(the_field, return_value) {
	var inserted_date = the_field.value;
	var date_array = new Array();
	var date_numbers = new Array();
	var continue_check = true;
	var date_changed = false;
	var alert_message = "";
	
	if (inserted_date != "") {
		date_array = inserted_date.split(/-/);
		if (date_array.length == 3) {
			for (var date_loop = 0; date_loop < date_array.length; date_loop++) {
				if (date_array[date_loop].match(/^\d+$/)) {
					var temp_number = parseInt(date_array[date_loop], 10);
					date_numbers[date_loop] = temp_number;
				} else {
					continue_check = false;
					alert_message = ("Please enter a date in the format MM-DD-YYYY. Use a two-digit number for the month, a two-digit number for the day, and a four-digit number for the year.");
					break;
				}
			}

			if (continue_check == true) {
				if (date_numbers[0] > 0 && date_numbers[0] <= 12) {
					if (date_array[0].length == 1) {
						date_array[0] = ("0" + date_array[0]);
						date_changed = true;
						continue_check = true;
					}
				} else {
					continue_check = false;
					alert_message = ("Please enter a valid month.");
				}
			}
			if (continue_check == true) {
				if (date_numbers[1] > 0 && date_numbers[1] <= 31) {
					var month_names = new Array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
					var months_invalid_31days = new Array(2, 4, 6, 9, 11);
					
					if (date_numbers[1] == 31) {
						for (var invalid_loop = 0; invalid_loop < months_invalid_31days.length; invalid_loop++) {
							if (date_numbers[0] == months_invalid_31days[invalid_loop]) {
								alert_message = (month_names[date_numbers[0]] + " does\'t have 31 days. Please enter a valid date.");
							}
						}					
					} else if (date_numbers[1] == 30 && date_numbers[0] == 2) {
						alert_message = ("February doesn\'t have 30 days.");
					} else if (date_numbers[1] == 29 && date_numbers[0] == 2) {
						var base_year = 1900;
						for (var leap_loop = 0; leap_loop <= 50; leap_loop++) {
							if (base_year == date_numbers[2]) {
								break;
							} else if (base_year > date_numbers[2] || leap_loop == 50) {
								alert_message = ("February doesn\'t have 29 days in " + date_numbers[2] + ". The year " + date_numbers[2] + " is not a leap year. Please enter a valid date.");
								break;
							} else {
								base_year += 4;
							}
						}
					}
					
					if (date_array[1].length == 1) {
						date_array[1] = ("0" + date_array[1]);
						date_changed = true;
						continue_check = true;
					}
				} else {
					continue_check = false;
					alert_message = ("Please enter a valid day.");
				}
			}
			if (continue_check == true) {
				if (date_numbers[2] >= 0 && date_numbers[2] <= 69) {
					date_array[2] = ("20" + date_array[2]);
					date_changed = true;
					continue_check = true;
				} else if (date_numbers[2] >= 70 && date_numbers[2] <= 99) {
					date_array[2] = ("19" + date_array[2]);
					date_changed = true;
					continue_check = true;
				} else if (date_numbers[2] >= 1900 && date_numbers[2] < 2100) {

				} else {
					continue_check = false;
					alert_message = ("Please enter a valid year.");
				}
			}
			if (continue_check == true && date_changed == true) {
				the_field.value = (date_array[0] + "-" + date_array[1] + "-" + date_array[2]);
			}
		} else {
			alert_message = ("Please enter a date in the format MM-DD-YYYY.");
		}
	}
	if (alert_message != "") {
		window.alert(alert_message);
		the_field.focus();
		if (return_value != "no") {
			return false;
		}
	} else {
		if (return_value != "no") {
			return true;
		}
	}
}
// End function validateDate

// Begin function validatePrice
function validatePrice(the_field, the_field_title, return_value) {
	var the_entered_price = the_field.value;
	var error_message = "";
	if (the_field_title == "" || the_field_title == undefined) {
		var the_title = "Price";
	} else {
		var the_title = the_field_title;
	}
	if (the_entered_price.match(/^\d+$/)) {
		var parsed_price = parseInt(the_entered_price, 10);
	} else if (the_entered_price.match(/^\d+.\d\d$/)) {
		var parsed_price = parseFloat(the_entered_price);
	} else {
		error_message = ("Please enter a valid entry in the " + the_title + " field.");
	}
	if (error_message != "") {
		window.alert(error_message);
		the_field.focus;
		if (return_value != "no") {
			return false;
		}
	} else {
		if (return_value != "no") {
			return true;
		}
	}
}
// End function validatePrice

// Begin function setHiddenDate
function setHiddenDate(the_display_date_field, the_hidden_field) {
	var inserted_date = the_display_date_field.value;
	var input_date_array = inserted_date.split(/-/);
	the_hidden_field.value = (input_date_array[2] + "-" + input_date_array[0] + "-" + input_date_array[1]);
}
// End function setHiddenDate

// This function will remove spaces from a string.
// If the_location == start, all leading spaces will be removed.
// If the_location == end, all trailing spaces will be removed.
// If the_location == start_end, all leading & trailing spaces will be removed.
// If the_loaction == all, all spaces will be removed.
function trimSpaces(the_field, the_location) {
	var the_string = the_field.value;
	var temp_string = "";
	if (the_location == "start") {
		temp_string = the_string.replace(/^\s+/, "");
	} else if (the_location == "end") {
		temp_string = the_string.replace(/\s+$/, "");
	} else if (the_location == "start_end") {
		var temp_string2 = the_string.replace(/^\s+/, "");
		temp_string = temp_string2.replace(/\s+$/, "");
	} else if (the_location == "all") {
		temp_string = the_string.replace(/\s+/g, "");
	}
	if (temp_string != "") {
		the_field.value = temp_string;
	}
}
// End function trimSpaces

function checkZipPostal(country_field, zip_postal_field) {
	var the_selected_country = country_field.options[country_field.selectedIndex].text;
	var the_zip_postal_code = zip_postal_field.value;
	if (the_selected_country == "United States of America" || the_selected_country == "USA") {
		if (the_zip_postal_code.match(/^\d{5}$/) || the_zip_postal_code.match(/^\d{5}-\d{4}$/)) {
			return true;
		} else {
			window.alert("You selected a USA state. Please enter a valid zip code in the format: 99999 or 99999-9999");
			zip_postal_field.value = "";
			zip_postal_field.focus();
			return false();
		}
	} else if (the_selected_country == "Canada" || the_selected_country == "CAN") {
		if (the_zip_postal_code.match(/^[A-Za-z]{1}\d{1}[A-Za-z]{1}\s{1}\d{1}[A-Za-z]{1}\d{1}$/)) {
			return true;
		} else {
			window.alert("You selected a Canadian Province / Territory. Please enter a valid postal code in the format: A2A 2A2");
			zip_postal_field.value = "";
			zip_postal_field.focus();
			return false;
		}
	}
}

