var validateMsg = "";
var validateCount = 0;
var petRadioClicked = false;
var gateRadioClicked = false;
var fsForm = document.forms[0];

// ============================================================
// Function to get only the digits in a string
// ============================================================

function getDigits(str) {

	var digits = new String("");

	for ( var i = 0; i < str.length; i++ ) {
		if ( str.charAt(i) >= '0' && str.charAt(i) <= '9' ) {
			digits = digits.concat(str.charAt(i));
		}
	}
	return digits;
}

// ============================================================
// Function to check if character is whitespace
// ============================================================

function isWhitespace(c) {
    whitespace = " \t\n\r";
    if (whitespace.indexOf(c) < 0) {
	return false;
    }
    return true;
}

// ============================================================
// Function to remove leading and trailing whitespace
// ============================================================

function trim(s) {

   var sBuff = s;
   var sLength = s.length;

// Remove leading whitespace

    for (var sPos = 0; sPos < sLength; sPos++) {
	if (!isWhitespace(s.charAt(sPos))) {
	    sBuff = s.substr(sPos);
	    break;
	}
    }

// Remove trailing whitespace

    var bLength = sBuff.length;
    for (sPos = bLength - 1; sPos >= 0; sPos--) {
	if (isWhitespace(sBuff.charAt(sPos))) {
	    return sBuff.substr(0, (sPos + 1));
	    break;
	}
    }
    return sBuff;
}

function formatSSN(ssnInput) {

	var digits = getDigits(ssnInput.value);

	if (digits.length == 0 && ssnInput.length != 0) {
		alert("Please enter a valid Social Security Number.");
		ssnInput.focus();
		return false;
	}

// If 9 digits long, format ###-##-####

	if ( digits.length == 9 ) {
		ssnInput.value = digits.substr(0,3) + "-" + digits.substr(3,2) + "-" + digits.substr(5);
	}
	return true;
}

function formatZipCode(zipInput) {

	var digits = getDigits(zipInput.value);

	if (digits.length == 0 && zipInput.value.length > 0) {
		alert("Please enter a valid US Zip Code.");
		zipInput.focus();
		return false;
	}
	if (digits.length != 0 && digits.length != 5 && digits.length != 9) {
		alert("US Zip Code should be 5 or 9 digits long.");
		zipInput.focus();
		return false;
	}

// If 9 digits long, format #####-####

	if ( digits.length == 9 ) {
		zipInput.value = digits.substr(0,5) + "-" + digits.substr(5);
	}
	return true;
}

function formatPhoneNumber(phoneInput) {

	var s = "";
	var digits = getDigits(phoneInput.value);

	if (digits.length == 0 && phoneInput.value.length > 0) {
		alert("Please enter a valid Phone Number.");
		phoneInput.focus();
		return false;
	}

// Require area code (North American standard)

	if ( digits.length < 10 && digits.length != 0) {
		alert("Please provide area code and phone number");
		phoneInput.focus();
		return false;
	}

// If 10 digits long, format (npa) nxx-line

	if ( digits.length == 10 ) {
		phoneInput.value = digits.substr(0,3) + "-" + digits.substr(3,3) + "-" + digits.substr(6);
	}
	return true;
}

function validateDate(t) {
    var dateChars  = "01234567890/-";
    var s = trim(t.value);

    if (s.length == 0) return false;
    if (s.length != 10 && s.length != 0) {
	alert("Invalid date format:  Use the form mm/dd/yyyy, with no spaces.");
//	alert("Invalid date format:  Use the form mm-dd-yyyy or mm/dd/yyyy, with no spaces.");
	t.focus();
	return false;
    }
    for (var i = 0; i < s.length; i++) {
	if (dateChars.indexOf(s.charAt(i)) < 0) {
	    alert("Invalid characters in date:  Use only digits and slashes in the form mm/dd/yyyy.");
//	    alert("Invalid characters in date:  Use only digits and slashes in the form mm-dd-yyyy or mm/dd/yyyy.");
	    t.focus();
	    return false;
	}
    }
    var yr = s.substr(6, 4) - 0;
    var month = s.substr(0, 2) - 0;
    var day  = s.substr(3, 2) - 0;
    if (yr < 1900) {
	alert("Check year:  Minimum allowed is 1900.");
	t.focus();
	return false;
    }
    if (month < 1 || month > 12) {
	alert("Invalid month: Should be between 01 and 12.");
	t.focus();
	return false;
    }
    switch(month) {
	case  1 : 
	case  3 : 
	case  5 : 
	case  7 : 
	case  8 : 
	case 10 : 
	case 12 : if (day > 0 && day <= 31) return true; break;
	case  4 : 
	case  6 : 
	case  9 : 
	case 11 : if (day > 0 && day <= 30) return true; break;
	default : if ((yr % 4 == 0 && day <= 29) || (yr % 4 != 0 && day <= 28)) return true; break;
    }
    alert("Check day " + day + ": Should be between 1 and appropriate End-of-Month (30, 31, 28 or 29).");
    t.focus();
    return false;
}

function validateEmail(emailInput) {
	var email_pattern = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]+$/;
	if((result = email_pattern.exec(emailInput.value)) == null) {
		alert("Please check for a valid e-mail address format\nExamples: john@yahoo.com, jane.doe@my-isp.net");
		emailInput.focus();
		return false;
	}
	return true;	
}

// ============================================================
// Correct amount text value to specified precision
// ============================================================

function fixAmountLength(p, t) {
	
	var periodPos = t.value.indexOf(".");

	if (t.value == "") return;

	if ( periodPos < 0 ) {
		t.value += ".00";
	}
	else if ( periodPos < t.value.length - p ) {
/*		alert("Corrected check amount to " + p + " decimal places"); */
		t.value = t.value.substr(0, periodPos + p + 1);
	}
	else 
		while ( t.value.indexOf(".") != t.value.length - p - 1 ) t.value += "0";
}

// ============================================================
// Restrict typed characters to digits and specified precision
// ============================================================

function numbersOnly(e, p, t) {

	var keyCode;
	var periodPos;

	if ( window.event )
		keyCode = window.event.keyCode;
	else if ( e )
		keyCode = e.which;
	else
		return true;

	periodPos = t.value.indexOf(".");

	if ( p > 0 && keyCode == 46 && periodPos < 0 ) {	// decimal point
		return true;
	}
	else if ( keyCode >= 48 && keyCode <= 57 ) { 		// digits
		if ( periodPos >= 0 ) 
			t.value = t.value.substr(0, periodPos + p);
		return true;
	}
	else
		return false;
}

// ============================================================
