// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

function validateRequired(field, infoFieldId) {
    with(field) {
        value = trim(value);
        if (value==null || value=="") {
            if (infoFieldId != null) {
                var infoFld = document.getElementById(infoFieldId);
                infoFld.className = "formErr";
                infoFld.firstChild.nodeValue = "Please complete this field.";
            }
            return false;
        } else {
            if (infoFieldId != null) {
                var infoFld = document.getElementById(infoFieldId);
                infoFld.className = "formOK";
                infoFld.firstChild.nodeValue = "OK";
            }
            return true;
        }
    }
};

function subjectChange(field, emailFieldId, phoneFieldId) {
    with(field) {
        var emailFld = document.getElementById(emailFieldId);
        var phoneFld = document.getElementById(phoneFieldId);
        if (selectedIndex == 0) {
            emailFld.className = "formReq";
            emailFld.firstChild.nodeValue = "*";
            phoneFld.className = "formReq";
            phoneFld.firstChild.nodeValue = "*";
        } else {
            emailFld.className = "formReq";
            emailFld.firstChild.nodeValue = " ";
            phoneFld.className = "formReq";
            phoneFld.firstChild.nodeValue = " ";
        }
    }
};

function validateEmail(field, infoFieldId, required) {
    with(field) {
        var value = trim(value);
        var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
        if (value==null || value=="") {
            if (required) {            
                if (infoFieldId != null) {
                    var infoFld = document.getElementById(infoFieldId);
                    infoFld.className = "formErr";
                    infoFld.firstChild.nodeValue = "Please enter your e-mail address";
                }
                return false;
            } else {
                if (infoFieldId != null) {
                    var infoFld = document.getElementById(infoFieldId);
                    infoFld.className = "formOK";
                    infoFld.firstChild.nodeValue = "";
                }
                return true;
            }
        } else if (!email.test(value)) {
            if (infoFieldId != null) {
                var infoFld = document.getElementById(infoFieldId);
                infoFld.className = "formErr";
                infoFld.firstChild.nodeValue = "Please check your e-mail address";
            }
            return false;
        } else {
            if (infoFieldId != null) {
                var infoFld = document.getElementById(infoFieldId);
                infoFld.className = "formOK";
                infoFld.firstChild.nodeValue = "OK";
            }
            return true;
        }

    }
};

function validatePhone(field, infoFieldId, required) {
    with(field) {
        var value = trim(value);
        var infoFld = document.getElementById(infoFieldId);
        var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
        if (value == null || value == "") {
            if (required) {
                infoFld.className = "formErr";
                infoFld.firstChild.nodeValue = "Please enter your phone number.";
                return false;
            } else {
                infoFld.className = "formOK";
                infoFld.firstChild.nodeValue = "";
                return true;
            }
        } else if (!telnr.test(value)) {
            infoFld.className = "formErr";
            infoFld.firstChild.nodeValue = "Phone number should be numeric.";
            return false;
        } else if (value.length < 10) {
            infoFld.className = "formErr";
            infoFld.firstChild.nodeValue = "Please enter your full phone number including area code.";
            return false;
        } else {
            infoFld.className = "formOK";
            infoFld.firstChild.nodeValue = "OK";
            return true;
        }
    }
};


