﻿function checkUsername(strng, which) {
    var error = "";
    var illegalChars = /\W/;
    if (strng == "") {
        error = "You didn't enter a "+which+".\n";
        if ((strng.length < 4) || (strng.length > 10)) {
            error = "The " + which + " is the wrong length.\n";
        }
    }
    // allow only letters, numbers, and underscores
    if (illegalChars.test(strng)) {
        error = "The " + which + " contains illegal characters.\n";
    }

    return error;
}

function checkString(strng, which, max) {
    var error = "";
    if (strng == "") {
        error = "You didn't enter a " + which + ".\n";
    }
    else if (strng.length > max) {
        error = "The " + which + " is too long.  It should be less than " + max + " characters long.\n";
    }

    return error;
}

function checkStringLength(strng, which, max) {
    var error = "";
    if (strng.length > max) {
        error = "The " + which + " is too long.  It should be less than " + max + " characters long.\n";
    }

    return error;
}

function checkPassword(strng, which) {
    var error = "";
    if (strng == "") {
        error = "You have not entered a " + which + ".\n";
    }
    var illegalChars = /[\W_]/; // allow only letters and numbers
    if ((strng.length < 5) || (strng.length > 8)) {
        error = "The " + which + " should be between 5 and 8 characters long.\n";
    }
    else if (illegalChars.test(strng)) {
        error = "The " + which + " contains illegal characters.\n";
    }
    else if (!((strng.search(/[a-z]+/) > -1) && (strng.search(/[A-Z]+/) > -1) && (strng.search(/[0-9]+/) > -1))) {
        error = "The " + which + " must contain at least one uppercase letter, one lowercase letter and one numeral.\n";
    }

    return error;
}

function checkEmail(strng, which) {
    var error = "";
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(strng) == false) {
        error = "Please enter a valid " + which + " e.g. info@torch.co.uk";
    }
    
    return error;
}

function checkPhone(strng, which) {
    var error = "";
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
    //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
        error = "The " + which + " contains illegal characters.";
    }
    else {
        if (!(stripped.length == 11)) {
            error = "The " + which + " is the wrong length.  Please ensure that you include the area code.\n";
        }
    }

    return error;
}

function isDifferent(strng1, strng2, which) {
    var error = "";
    if (strng1 != strng2) {
        error = "The " + which + "'s are not the same.\n";
    }

    return error;
}

function checkRadio() {
    var checkValue = "";
    var error = "";
    for (i = 0, n = aForm.aRadio.length; i < n; i++) {
        if (aForm.aRadio[i].checked) {
            checkValue = aForm.aRadio[i].value;
            break;
        }
    }

    if (checkValue == "") {
        error = "Please check a radio button.\n";
    }

    return error;
}

function checkDropdown(choice, which) {
    var error = "";
    if (choice == 0) {
        error = "You have not selected a "+ which +" from the list.\n";
    }

    return error;
}

function checkNumber(nmber, which) {
    var error = "";
    if (isNaN(nmber)) {
        error = "The " + which + " contains illegal characters.";
    }
    else {
        if ((nmber.length < 6) || (nmber.length > 10)) {
            error = "The " + which + " is the wrong length.\n";
        }
    }

    return error;
}

function checkBoxChecked(checkedYN, which) {
    var error = "";
    if (!checkedYN) {
        error = "The " + which + " check box needs to be checked.\n";
    }
    
    return error;
}

function clearFormFields(oForm, exclusion1, exclusion2, exclusion3, exclusion4) {
    var frm_elements = oForm.elements;

    for (i = 0; i < frm_elements.length; i++) {
        if ((frm_elements[i].id != exclusion1) && (frm_elements[i].id != exclusion2) && (frm_elements[i].id != exclusion3) && (frm_elements[i].id != exclusion4))
            frm_elements[i].value = "";
    }
}

function fieldLimit(countObject, maxLength) {
    if (countObject.value.length == maxLength)
        return false;
    return true;
}

function fieldCount(countObject, countDisplay, maxLength) {
    var bName = navigator.appName;
    objcountDisplay = createObject(countDisplay);
    objVal = countObject.value;
    if (objVal.length > maxLength) objVal = objVal.substring(0, maxLength);
    if (objcountDisplay) {
        if (bName == "Netscape") {
            objcountDisplay.textContent = maxLength - objVal.length;
        }
        else { objcountDisplay.innerText = maxLength - objVal.length; }
    }
    return true;
}

function createObject(objId) {
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.getElementById) return eval("document.getElementById." + objId);
    else return eval("document." + objId);
}
