function isnumeric(strString) {
	// check for valid numeric strings	
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	var i;

	if (strString.length == 0) return false;
	// test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
function isposnumeric(strString) {
	// check for valid POSITIVE numeric strings
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	var i;

	if (strString.length == 0) return false;
	// test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
function isphonenumber(s) {
	// validates format: 123-123-1234
	return (s.match(/[0-9]{3}\-[0-9]{3}\-[0-9]{4}/));
}
function isemailaddress(s) {
	return (s.match(/^.+@.+\..+$/));
}
function is_alphanum(s) {
	return (s.match(/^[0-9,a-z,A-Z]+$/));
}

function ismoney(s) {
	// If the use entered dollar signs or commas, remove them and
	// re-test. If the dollar signs or commas should not be there,
	// then this method can not be used.
	s = s.replace("$","")
	s = s.replace(",","");
	if (!isnumeric(s)) return false;
	return true;
}

function isdate(strString) {
	// this will check the date to be sure it was input in MM/DD/YYYY format
	arDate = strString.split("/");
	if (arDate.length != 3)
		return false;
  
	// This instruction will create a date object
	source_date = new Date(arDate[2], arDate[0]-1, arDate[1]);
	
	if (arDate[0] != source_date.getMonth()+1)
		return false;
		
	if (arDate[1] != source_date.getDate())
		return false;
  
	if (arDate[2] != source_date.getFullYear())
		return false;

	return true;
}

function isblank(strString) {
	var i=0;
	if (strString == null) return true;
	for (i=0; i < strString.length; i++) {
		var c = strString.charAt(i);
		if ((c != " ") && (c != "\n") && (c != "")) return false;
	}
	return true;
}
function indexSelected(obj, index) {
	if (obj.selectedIndex == index) return true;
	return false;
}

function alertNFocus(input,name) {
	alert("Please enter a " + name + " before clicking the submit button.");
	return false;
}
function alertToBlank(input,name) {
	alert("A value is required for " + name + " and can not be left blank.");
	return false;
}
function alertToInvalidValue(input,name) {
	alert("Invalid characters were found in " + name + ". Please delete these and enter a correct value for this field.");
	return false;
}
function alertNotMoneyFormat(input,name) {
	alert(name + " does not represent a money format. Correct and resubmit.");
	return false;
}


function validate_addalert(frm) {
	if (isblank(frm.alert_name.value)) {
		return alertNFocus(frm.alert_name,"Alert Name");
	} else if (frm.alerttype_id.value == '1' || frm.alerttype_id.value == '3') {
		return confirm("PaddleSwap is not liable for any charges associated with receiving text messages. Do you understand and agree?");
	}
	return true;
}
function validate_addcomment(frm) {
	if (isblank(frm.comment.value)) {
		return alertNFocus(frm.comment,"Comment");
	}
	return true;
}
function validate_signup(frm) {
	/* username */
	if (isblank(frm.username.value)) {
		return alertToBlank(frm.username,"Username");
	} else {
		if (!is_alphanum(frm.username.value)) {
			return alertToInvalidValue(frm.username,"Username");
		}
	}
	/* Email address */ 
	if (isblank(frm.email.value)) {
		return alertToBlank(frm.email,"Email");
	} else {
		if (!isemailaddress(frm.email.value)) {
			return alertNFocus(frm.email, "Email"); 
		}
	}
	if (isblank(frm.email2.value)) {
		return alertToBlank(frm.email2,"Confirm Email");
	} else {
		if (!isemailaddress(frm.email2.value)) {
			return alertNFocus(frm.email2, "Confirm Email"); 
		}
	} 
	if (frm.email.value != frm.email2.value) {
		alert("Email addresses do not match! Retype them both to confirm, and submit.");
		return false;
	}

	/* Passwords */ 
	if (isblank(frm.password.value)) {
		return alertToBlank(frm.password,"Password");
	}
	if (isblank(frm.password2.value)) {
		return alertToBlank(frm.password2,"Confirm Password");
	}
	if (frm.password.value != frm.password2.value) {
		frm.password.value = '';
		frm.password2.value = '';
		alert("Passwords do not match! Retype them both to confirm, and submit.");
		return false;
	}
	/* Locations */

	return true;
}


// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        } else if (!e.optional) {
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }
        }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

function validate_sendemail(frm) {
	if (isblank(frm.from.value)) {
		return alertNFocus(frm.from, "From");
	} else if (!isemailaddress(frm.from.value)) {
		alert("Please enter a valid From addrss.");
		return false;
	}
	if (isblank(frm.subject.value)) {
		return alertNFocus(frm.subject, "Subject");
	}
	if (isblank(frm.message_body.value)) {
		return alertNFocus(frm.message_body, "Message");
	}
	return true;
}

function validate_signup_confirm(frm) {
	if (!frm.tos_agree.checked) {
		alert("You must agree to the Terms of Service before submitting this form.");
		return false;
	}
	return true;
}
function validate_boat_edit(frm) {
	if (isNaN(parseInt(frm.askprice.value))) {
		alert("You must enter a numeric price.");
		return false;
	} else {
		frm.askprice.value = parseInt(frm.askprice.value);
	}
	return true;
}
function validate_equip_edit(frm) {
	if (isNaN(parseInt(frm.askprice.value))) {
		alert("You must enter a numeric price.");
		return false;
	} else {
		frm.askprice.value = parseInt(frm.askprice.value);
	}
	return true;
}



