// JavaScript Document
image1 = new Object();
image1.src = "/inc/udm-resources/down-blue.gif";
image2 = new Object();
image2.src = "/inc/udm-resources/down-yellow.gif";
// FORM VALIDATION 
// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
	var str = elem.value;
	var fieldname = elem.id;
    var re = /.+/;
    if(!str.match(re)) {
        alert("Please fill in the " + fieldname + " field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}

//validates that the entry is a positive or negative number
function isNumber(elem) {
	var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
        alert("Please enter only numbers into the " + elem.id +" field (without spaces).");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}
// validates that the entry is 16 characters long
function isLen16(elem) {
	var str = elem.value;
    var re = /\b.{16}\b/;
    if (!str.match(re)) {
        alert("Entry does not contain the required 16 characters.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        alert("Please enter a valid email address.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validate that the user made a selection other than default
function isChosen(select) {
	var fieldname = select.id;
    if (select.selectedIndex == 0) {
        alert("Please make a choice from the '" +fieldname+ "' list.");
        return false;
    } else {
        return true;
    }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
		var fieldname = radio[i].id;
        if (radio[i].checked) {
            return true;
        }
    }
	
    alert("Make a choice for " + fieldname + ".");
    return false;
}

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}

function validateSignup(form) {									
	if (isEMailAddr(form.FQS_email)) {
    	return true;                  
    }                    	
   	return false;
}
function checkDate(fld) {
    var day, mo, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "/";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(delim1+1, delim2), 10);
		day = parseInt(entry.substring(0, delim1), 10);
		//mo = parseInt(entry.substring(0, delim1), 10);  
		//day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getYear() == yr) {
                    return true;
                } else {
                    alert("There is a problem with the birth year entry.");
                }
            } else {
                alert("There is a problem with the birth month entry.");
            }
        } else {
            alert("There is a problem with the birth date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yy.");
    }
    return false;
}

function validateDate(fld) {
    if (!checkDate(fld)) {
        // focus if validation fails
        fld.focus();
        fld.select();
    }
}
