﻿
function validateField(field, datatype, caption, required, isarray) {
    
    var field_type;
    
    if (isarray) {
        field_type = field[0].type;
    } else {
        field_type = field.type;
    }
    
    if (required) {
        switch (field_type) {
            case "radio":
                if (isarray) {
                    isblank = true;
                    for (i=0; i<field.length; i++) {
                        if (field[i].checked) {
                            isblank = false;
                            continue;
                        }
                    }
                } else {
                    if (field.checked == false) {
                        isblank = true;
                    }
                }
                if (isblank) {
                    alert(caption + " is a required field.");
                    if (isarray) {
                        field[0].focus();
                    } else {
                        field.focus();
                    }
                    return false;
                }
                break;
            default:
                if (field.value == "") {
                    alert(caption + " is a required field.");
                    field.focus();
                    return false;
                }
                break;
        }
    }
    
    switch (datatype) {
        case "email":
            if (validateEmail(field.value) == false) {
                alert(caption + " is not a valid email address.");
                field.focus();
                field.select();
                return false;
            }
            break;
        case "number":
            if (validateType("0123456789", field.value) == false) {
                alert(caption + " is not a valid number.\n\nOnly characters 0 - 9 may be entered in this field.");
                field.focus();
                field.select();
                return false;
            }
            break;
        case "hex":
            if (validateType("0123456789ABCDEF", field.value.toUpperCase()) == false) {
                alert(caption + " is not valid.\n\nOnly characters 0 - 9 and A - F may be entered in this field.");
                field.focus();
                field.select();
                return false;
            }
            break;
        case "phone_intl":
            if (validateType("()-. 0123456789", field.value) == false) {
                alert(caption + " is not a valid phone number.\n\nOnly characters 0 - 9 may be entered in this field.");
                field.focus();
                field.select();
                return false;
            }
            break;
    }
    
}

function validatePhone(country_code, phone_no) {
    
    var remove_chars = "()9-. ";
    var toRemove = new Array("\\(", "\\)", "-", "\\.", " "); // Regular Expressions.
    
    for (i = 0; i < toRemove.length; i++) {
        phone_no = phone_no.replace( new RegExp(toRemove[i], "gi"), "");
    }
    
    switch (country_code) {
        case "1":
            // ~~~~~ number cannot start with zero
            if (phone_no.charAt(0) == "0") { alertInvalidPhoneFormat(); return false; }
            // ~~~~~ length should be 10.
            if (phone_no.length != 10) { alertInvalidPhoneFormat(); return false; }
            break;
        case "44":
            // ~~~~~ drop the leading zero for international calling.
            if (phone_no.charAt(0) == "0") {
                phone_no = phone_no.substring(1, phone_no.length);
            }
            // ~~~~~ without the leading zero, length should be 10.
            if (phone_no.length != 10) { alertInvalidPhoneFormat(); return false; }
            break;
        default:
            break;
    }
    
    return true;
    
}

function alertInvalidPhoneFormat() {
    alert("The Phone Number you entered is not in a valid format for the Country Code you selected.");
}

function validateEmail(email) {
    
    var at_pos = email.indexOf("@");
    var last_dot_pos = email.lastIndexOf(".");
    
    if (at_pos <= 0) { return false; }
    if (last_dot_pos < at_pos) { return false; }
    
}

function validateType(valid_chars, input) {

    var cchar;

    for (i = 0; i < input.length; i++)  { 
        cchar = input.charAt(i); 
        if (valid_chars.indexOf(cchar) == -1)  { return false; }
    }

    return true;

}

function comparePasswords(pass1_field, pass2_field) {
    
    if (pass1_field.value != pass2_field.value) {
        alert("The entered passwords do not match.");
        pass1_field.value = "";
        pass2_field.value = "";
        pass1_field.focus();
        return false;
    }
    
}