function isEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return true;
    }
    return false;
}

function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

/* Check if the element value is a numeric and <= max*/
function isMaxNum(elem, helperMsg, max){
    if(!isNumeric(elem, helperMsg)){
        return false;
    }
    if(elem.value > max){
        alert(helperMsg);
        elem.focus();
        return false;	
    }
    return true;
}

/* Check if the element value is a numeric and >= min*/
function isMinNum(elem, helperMsg, min){
    if(!isNumeric(elem, helperMsg)){
        return false;
    }
    if(elem.value < min){
        alert(helperMsg);
        elem.focus();
        return false;
    }
    return true;
}
function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function isAlphanumeric(elem, helperMsg){
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function lengthRestriction(elem, min, max){
    var uInput = elem.value;
    if(uInput.length >= min && uInput.length <= max){
        return true;
    }else{
        alert("Please enter between " +min+ " and " +max+ " characters");
        elem.focus();
        return false;
    }
}

function checkedRadio(elem, helperMsg){
    var checked = false;
    
    for(count = 0; count < elem.length; count++){
        if(elem[count].checked){
            checked = true;
            break;
        }
    }
    if(!checked){
        alert(helperMsg);
        elem[elem.length-1].focus();
        return false;
    }else{
        return true;
    }
}
/**
 * elem is an array
 */
function checkedCheckboxes(elem, helperMsg){
    var checked = false;
    for(count = 0; count < elem.length; count++){
        if(elem[count].checked){
            checked = true;
            break;
        }
    }
    if(!checked){
        alert(helperMsg);
        elem[0].focus();
        return false;
    }else{
        return true;
    }
}

function madeSelection(elem, helperMsg){
    if(elem.value == "Please Choose"){
        alert(helperMsg);
        elem.focus();
        return false;
    }else{
        return true;
    }
}

function emailValidator(elem, helperMsg){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.match(emailExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

/**
 *US Phone Number: This regular expression for US phone numbers conforms to NANP A-digit and D-digit requirments (ANN-DNN-NNNN). Area *Codes 001-199 are not permitted; Central Office Codes 001-199 are not permitted. Format validation accepts 10-digits without  
 *delimiters, optional parens on area code, and optional spaces or dashes between area code, central office code and station code. 
 *Acceptable formats include 2225551212, 222 555 1212, 222-555-1212, (222) 555 1212, (222) 555-1212, etc. You can add/remove formatting *options to meet your needs.
 */
function phoneValidator(elem, helperMsg){
    var phoneExp = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/;
    if(elem.value.match(phoneExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }	
}

/**
 * This regular expressions matches dates of the form:
 * code = 0 : XX/XX/YYYY where XX can be 1 or 2 digits long and YYYY is always 4 digits long.
 * code = 1 : XX/XX/YYYY where XX is always 2 digits long and YYYY is always 4 digits long.
 * code = 2 : XX/XX/YY where XX can be 1 or 2 digits long and YY is always 4 digits long.
 * code = 3 : XX/XX/YY where XX is always 2 digits long and YYYY is always 4 digits long.
 */
function dateValidator(elem, helperMsg, code){
    var dateExp = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    switch(code){
        case 0:
            dateExp = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
            break;
        case 1:
            dateExp = /^\d{2}\/\d{2}\/\d{4}$/;
            break;
        case 2:
            dateExp = /^\d{1,2}\/\d{1,2}\/\d{2}$/;
            break;
        case 3:
            dateExp = /^\d{2}\/\d{2}\/\d{2}$/;
            break;
        case 4:
            dateExp = /^\d{4}[-]\d{2}[-]\d{2}$/;
            break;
        default:
    }
    if(elem.value.match(dateExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }	
}
function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
                if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
                    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
                } else if (test!='R') { num = parseFloat(val);
                    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
                    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
                        min=test.substring(8,p); max=test.substring(p+1);
                        if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
                    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
}
