var bValid; //give global scope

function submithandle(f) {
    bValid = true; //initilize - assume form is valid		
    for (var i = 0; i <= f.elements.length - 1; i++)//	Check all fields begining with '*'
    {
        if (f.elements[i].name.substring(0, 1) == '*') {
            bValid = CheckSpecified(f.elements[f.elements[i].name], "Warning\n==================\nA Required Field was not entered.\nPlease return and fix.");
        }

        if (f.elements[i].name.substring(0, 1) == '#') {
            bValid = CheckNumeric(f.elements[f.elements[i].name], "Warning\n==================\nA Required Numeric field was not entered properly.\nPlease return and fix.");
        }

        if (f.elements[i].name.substring(0, 1) == '@') {
            bValid = CheckEmail(f.elements[f.elements[i].name], "Warning\n==================\nA Required Email Address was not entered properly.\nPlease return and fix.");
        }


    }

    //JR: Spam Check
    //var sCheck = document.getElementById("SFil");
    //if (sCheck) {
    //    if (sCheck.value != "") {
    //        alert("A spam attempt may have been detected. Please re-set your form.");
    //        return false;
    //    }
    //}

    return bValid;
}

function CheckSpecified(oinpt, strMsg) {
    var strFieldValue = getValue(oinpt);
    if (strFieldValue == "") {
        if (bValid) {
            alert(strMsg)
            bValid = false
        }
    }
    return bValid;
}

function CheckPositive(oinpt, strMsg) {
    var val = replaceChars(getValue(oinpt), ',', '');
    var temp = oinpt.value;
    if (val.indexOf('$') == 0) {
        val = val.substring(1, val.length)
    }
    if (val.length != 0) {
        if (isNaN(val) || val < 1) {
            oinpt.value = "";
        }
        CheckSpecified(oinpt, strMsg);
        oinpt.value = temp;
    }
    return bValid;
}


function CheckNumeric(oinpt, strMsg) {

    var val = replaceChars(getValue(oinpt), ',', '');
    var temp = oinpt.value;
    if (val.indexOf('$') == 0) {
        val = val.substring(1, val.length)
    }


    if (val.length != 0) {
        if (isNaN(val)) {
            oinpt.value = "";
        }
        bValid = CheckSpecified(oinpt, strMsg);
        oinpt.value = temp;
    }
    else {
        bValid = CheckSpecified(oinpt, strMsg);
    }
    return bValid;
}



function CheckDate(oinpt, fieldLabel) {
    var result = true;
    if (result) {
        var elems = oinpt.value.split("/");
        result = (elems.length == 3); // should be three components     
        if (result) {
            var month = parseFloat(elems[0]);
            var day = parseFloat(elems[1]);
            var year = parseFloat(elems[2]);
            result = !isNaN(month) && (month > 0) && (month < 13) &&
		       !isNaN(day) && (day > 0) && (day < 32) &&
		       !isNaN(year) && (elems[2].length == 4 || elems[2].length == 2);
        }

        if (!result) {
            var temp = oinpt.value
            oinpt.value = "";
            CheckSpecified(oinpt, 'Please enter a date in the format MM/DD/YYYY for the ' + fieldLabel + '.');
            oinpt.value = temp;
        }
        return result;
    }

}

function CheckEmail(oinpt, strMsg) {
    var result = validateemail_nonrequired(oinpt.value);

    if (!result) {
        var temp = oinpt.value
        oinpt.value = "";
        bValid = CheckSpecified(oinpt, strMsg);
        oinpt.value = temp;
    }

    return bValid;
}





function getValue(oinpt) {
    var val = "";
    var type;

    if (oinpt.type) {
        type = oinpt.type;
    }
    else {
        type = oinpt[0].type;
    }

    if ((type == 'radio' || type == 'checkbox')) {
        for (var i = 0; i <= oinpt.length - 1; i++) {
            if (oinpt[i].checked) {
                val = oinpt[i].value;
            }
        }
    }

    else if (type.indexOf('select') != -1) {
        val = "";
        if (oinpt.selectedIndex != -1) {
            val = oinpt.options[oinpt.selectedIndex].value;
        }
    }
    else {
        val = oinpt.value;
    }
    return val;

}




//Validation functions 
function validateemail_required(email) {
    // IF There is a '.' in it  AND   the dot comes after the @ symbol  AND    there is an @ symbol and it is not the first character(indexOf returns 0 if not in string)
    if ((email.indexOf('.') != -1 && email.lastIndexOf('.') > email.indexOf('@')) && email.indexOf('@') > 0) {
        return true;
    }
    else {
        return false;
    }

}

function validateemail_nonrequired(email) {
    email = Trim(email)
    if (email != '') { //only validate if not blank
        // IF There is a '.' in it  AND   the dot comes after the @ symbol  AND    there is an @ symbol and it is not the first character(indexOf returns 0 if not in string)			
        if ((email.indexOf('.') != -1 && email.lastIndexOf('.') > email.indexOf('@')) && email.indexOf('@') > 0) {
            return true;
        }
        else {

            return false;
        }
    }
    else {
        return true;
    }
}

////String Handling functions
function replaceChars(entry, out, add) {
    var temp = "" + entry; // temporary holder
    while (temp.indexOf(out) > -1) {
        pos = temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add +
			temp.substring((pos + out.length), temp.length));
    }
    return temp;
}




function LTrim(str) {
    var whitespace = new String(" \t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(0)) != -1) {
        // We have a string with leading blank(s)...

        var j = 0, i = s.length;

        // Iterate from the far left of string until we
        // don't have any more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;


        // Get the substring from the first non-whitespace
        // character to the end of the string...
        s = s.substring(j, i);
    }

    return s;
}

function RTrim(str) {
    // We don't want to trip JUST spaces, but also tabs,
    // line feeds, etc.  Add anything else you want to
    // "trim" here in Whitespace
    var whitespace = new String(" \t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        // We have a string with trailing blank(s)...

        var i = s.length - 1;       // Get length of string

        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;


        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring(0, i + 1);
    }

    return s;
}





function Trim(str) {
    return RTrim(LTrim(str));
}

function UpdateMallUser(newVal) { if (document.frm) { document.frm.MALLUSER.value = newVal } }
