// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

function isName(elem) {
    var str = elem.value;
    var re = /^\s*[\w'-]+[\s\w'-]+\s*$/;
    str = str.toString( );
    if (!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

//validates that the entry is a positive or negative number
function isPhoneNumber(elem) {
    var str = elem.value;
    var re = /^[ 0-9+()-]{7,30}$/;
    str = str.toString( );
    if (!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

//validates that the entry is a valid password format
function isPassword(elem) {
    var str = elem.value;
    var re = /^.{3,16}$/;
    str = str.toString( );
    if (!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }   
}       

function isPassEqual() {
    if (document.registration.pass1.value != document.registration.pass2.value) {
        document.registration.pass2.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        document.registration.pass2.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

//validates that the entry is a username must be between 3 and 16 characters
//only contain lowercase letters, numbers and the underscore character
function isUserName(elem) {
    var str = elem.value;
    var re = /^[a-z0-9_]{3,16}$/;
    str = str.toString( );
    if (!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}
   
// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
    var str = elem.value;
    var re = /^[A-Za-z0-9][A-Za-z0-9+._-]*@([A-Za-z0-9-]+\.)+[A-Za-z]+$/;
    if (!str.match(re)) {
        elem.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        elem.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        } 
    }
    return false;
}

function isInErrorMode(elem) {
    elem.style.backgroundColor = "rgb( 255, 204, 204)";
    return false;
}

// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        select.style.backgroundColor = "rgb( 255, 204, 204)";
        return false;
    } else {
        select.style.backgroundColor = "rgb( 204, 255, 204)";
        return true;
    }
}

function validateReg1() {
    var valid = true;
    var errorCont = document.getElementById("errorBox");
    var errorHead = document.createElement("div");
    var errorBox = document.createElement("div");
    while (errorCont.childNodes.length > 0) {
        errorCont.removeChild(errorCont.childNodes[0]);
    }
    errorHead.style.backgroundColor = "rgb( 0, 0, 0)";
    errorHead.style.border = "0px solid #fff";
    errorHead.style.padding = "0px";

    if (!isNotEmpty(document.registration.name)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.name.focus();";
        errorTxt = document.createTextNode("Please provide your first name.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP); 
        valid = false;
    }
    if (!isNotEmpty(document.registration.lastname)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.lastname.focus();";
        errorTxt = document.createTextNode("Please provide your last name.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP);
        valid = false;
    }
    if (!isEMailAddr(document.registration.mail)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.mail.focus();";
        errorTxt = document.createTextNode("A valid email address is required.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP); 
        valid = false;
    }
    if (!isPhoneNumber(document.registration.contact)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.contact.focus();";
        errorTxt = document.createTextNode("Please provide your phone number. No letters (a-z) can be used.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP);
        valid = false;
    }
    if (!isNotEmpty(document.registration.addr1)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.addr1.focus();";
        errorTxt = document.createTextNode("Please provide the first line of your address.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP); 
        valid = false;
    }
    if (!isNotEmpty(document.registration.addrcity)) {
        errorTxtP = document.createElement("p");
        errorTxtA = document.createElement("a");
        errorTxtA.href = "javascript:document.registration.addrcity.focus();";
        errorTxt = document.createTextNode("Please provide your city, town or county.");
        errorTxtA.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtA);
        errorBox.appendChild(errorTxtP); 
        valid = false;
    }
    if (valid != true) {
        errorTxtP = document.createElement("p");
        errorTxtB = document.createElement("b");
        errorTxt = document.createTextNode("Please fix the following error(s) and then click on Register.");
        errorTxtB.appendChild(errorTxt);
        errorTxtP.appendChild(errorTxtB);
        errorHead.appendChild(errorTxtP);
        errorHead.style.backgroundColor = "rgb( 255, 204, 204)";
        errorHead.style.border = "2px solid #f00";
        errorHead.style.padding = "3px";
    }
    errorCont.appendChild(errorHead);
    errorHead.appendChild(errorBox);
    return valid;
}


function getWidth() {
    regTable = document.getElementById("registrationTable");
    var intWidth = regTable.offsetWidth;
    return intWidth;
}