﻿var form = "";
var submitted = false;
var error = false;
var error_message = "";

function check_input(field_name, field_size, message) {
	if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
		var field_value = form.elements[field_name].value;
		if (field_value == '' || field_value.length < field_size) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_radio(field_name, message) {
	var isChecked = false;
	
	if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
		var radio = form.elements[field_name];
		
		for (var i=0; i<radio.length; i++) {
			if (radio[i].checked == true) {
				isChecked = true;
				break;
			}
		}
		
		if (isChecked == false) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_select(field_name, field_default, message) {
	if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
		var field_value = form.elements[field_name].value;
		
		if (field_value == field_default) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_password(field_name_1, field_name_2, field_size, message_1, message_2) {
	if (form.elements[field_name_1] && (form.elements[field_name_1].type != "hidden")) {
		var password = form.elements[field_name_1].value;
		var confirmation = form.elements[field_name_2].value;
		
		if (password == '' || password.length < field_size) {
			error_message = error_message + "* " + message_1 + "\n";
			error = true;
		} else if (password != confirmation) {
			error_message = error_message + "* " + message_2 + "\n";
			error = true;
		}
	}
}

function check_password_new(field_name_1, field_name_2, field_name_3, field_size, message_1, message_2, message_3) {
	if (form.elements[field_name_1] && (form.elements[field_name_1].type != "hidden")) {
		var password_current = form.elements[field_name_1].value;
		var password_new = form.elements[field_name_2].value;
		var password_confirmation = form.elements[field_name_3].value;
		
		if (password_current == '' || password_current.length < field_size) {
			error_message = error_message + "* " + message_1 + "\n";
			error = true;
		} else if (password_new == '' || password_new.length < field_size) {
			error_message = error_message + "* " + message_2 + "\n";
			error = true;
		} else if (password_new != password_confirmation) {
			error_message = error_message + "* " + message_3 + "\n";
			error = true;
		}
	}
}

function check_date(field_name, message) {
	if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
		var field_value = form.elements[field_name].value;
		if (!isDate(field_value)) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}


function check_form(form_name) {
	if (submitted == true) {
		alert("This form has already been submitted. Please press OK and wait for this process to be completed.");
		return false;
	}
	
	error = false;
	form = form_name;
	error_message = "Errors have occurred during the processing of your form.\n\nPlease make the following corrections:\n\n";
	
	check_radio("gender", "Please choose a salutation.");
	
	check_input("firstname", 2, "Is your first name correct? Our system requires a minimum of 2 characters. Please try again.");
	check_input("lastname", 2, "Is your last name correct? Our system requires a minimum of 2 characters. Please try again.");
	
	check_date("dob", "Is your birth date correct? Our system requires the date in this format: MM/DD/YYYY (eg 05/21/1970)");
	
	check_input("email_address", 6, "Is your email address correct? It should contain at least 6 characters. Please try again.");
	check_input("street_address", 5, "Your Street Address must contain a minimum of 5 characters.");
	check_input("postcode", 4, "Your Post/ZIP Code must contain a minimum of 4 characters.");
	check_input("city", 2, "Your City must contain a minimum of 2 characters.");
	
	if (form.elements['state']) {
		if (!form.state.disabled && form.zone_id.value == "") check_input("state", 2, "Your State must contain a minimum of 2 characters.")
		else if (form.state.disabled) check_select("zone_id", "", "Please select a state from the States pull down menu.");
	} else check_select("zone_id", "", "Please select a state from the States pull down menu.");
	
	check_select("country", "", "You must select a country from the Countries pull down menu.");
	
	check_input("telephone", 3, "Your Telephone Number must contain a minimum of 3 characters.");
	
	check_password("password", "confirmation", 5, "Your Password must contain a minimum of 5 characters.", "The Password Confirmation must match your Password.");
	check_password_new("password_current", "password_new", "password_confirmation", 5, "Your Password must contain a minimum of 5 characters.", "Your new Password must contain a minimum of 5 characters.", "The Password Confirmation must match your new Password.");
	
	if (error == true) {
		alert(error_message);
		return false;
	} else {
		submitted = true;
		return true;
	}
}
