function doslice(arg, idx) {
    var ret = Array();
    for (var i = idx; i < arg.length; i++) {
        ret.push(arg[i]);
    }
    return ret;
}

function Check(theForm, what, regexp, indices) {
    for (var i = 0; i < indices.length; i++) {
        var el = theForm.elements[indices[i]];
        if (el.value == "") continue;
        var avalue = el.value;
        if (!regexp.test(avalue)) {
            alert("Please correct the incorrect entry in the " + what +" field");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckEmail(theForm) {
    var regexp = /^[0-9a-z\.\-_]+@[0-9a-z\-\_]+\..+$/i;    
    return Check(theForm, "email", regexp, doslice(arguments, 1));
}

function CheckAlpha(theForm) {
    var regexp = /^[a-z]*$/i;
    return Check(theForm, "alpha value", regexp, doslice(arguments, 1));
}

function CheckAlphaNum(theForm) {
    var regexp = /^[a-z0-9]*$/i;
    return Check(theForm, "alphanumeric value", regexp, doslice(arguments, 1));
}

function CheckNumeric(theForm) {
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i] - 1];
        if (el.value == "") continue;
        var avalue = parseInt(el.value);
        if (isNaN(avalue)) {
            alert("Cell number and Landline number must not have spaces.");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckFloat(theForm) {
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = parseFloat(el.value);
        if (isNaN(avalue)) {
            alert("Field is not a valid floating point number");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckDate(theForm) {
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = el.value;
        if (isNaN(Date.parse(avalue))) {
            alert("Field is not a valid date");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckTime(theForm) {
    var regexp = /^[0-9]+:[0-9]+:[0-9]+/i;    
    if (!Check(theForm, "time", regexp,  doslice(arguments, 1)))
        return false;                 
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value == "") continue;
        var avalue = el.value;
        if (isNaN(Date.parse("1/1/1970 " + avalue))) {
            alert("Field is not a valid time");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckRequiredFields(theForm) {    
    for (var i = 1; i < arguments.length; i++) {
        var el = theForm.elements[arguments[i]];
        if (el.value=="") {
            alert("Please supply information for all fields");
            el.focus();
            return false;
        }
    }
    return true;
}

function CheckForm(theForm) {
    return (
        CheckRequiredFields(theForm, 0, 1, 2, 3, 4, 5) &&
        CheckEmail(theForm, 2) &&
        CheckNumeric(theForm, 4, 5)
    )
}
