 ENTRY_FIRST_NAME_MIN_LENGTH = 2;

function validateName(formName, fieldName){
    var df = document.forms[formName];
    var fieldIsValid = true;

    //alert(ENTRY_FIRST_NAME_MIN_LENGTH);

    if( df.elements[fieldName].value.length < ENTRY_FIRST_NAME_MIN_LENGTH ){
        alert("Please enter a longer name. [1000]");
        //df.elements[fieldName].select();
        fieldIsValid = false;
        return fieldIsValid;
    }
    return fieldIsValid;
}

function validateEmail(formName, fieldName){
    var df = document.forms[formName];
    var fieldIsValid = true;

    //Check for the minimum requirements for the email address
    if( df.elements[fieldName].value.indexOf("@") == -1 || df.elements[fieldName].value.indexOf(".") == -1 ){
        alert("Please enter a valid email address. [1001]");
        //df.elements[fieldName].select();
        fieldIsValid = false;
        return fieldIsValid;
    }


    //Check for a domain name that is at least 2 characters
    if( df.elements[fieldName].value.indexOf("@") != -1 || df.elements[fieldName].value.indexOf(".") != -1 ){
        atLocation = df.elements[fieldName].value.indexOf("@")+1;
        periodLocation = df.elements[fieldName].value.indexOf(".");
        domainName = df.elements[fieldName].value.substring(atLocation, periodLocation);
        //alert(domainName);
        if( !(domainName.length > 1) ){
            alert("Please enter a valid email address. [1002]");
            //df.elements[fieldName].select();
            fieldIsValid = false;
            return fieldIsValid;
        }
    }

    //Check for an extension name that is at least 2 characters
    if( df.elements[fieldName].value.indexOf("@") != -1 || df.elements[fieldName].value.indexOf(".") != -1 ){
        periodLocation = df.elements[fieldName].value.indexOf(".")+1;
        emailLength = df.elements[fieldName].value.length;
        extensionLength = emailLength - periodLocation;
        //alert(extensionLength);
        if( !(extensionLength > 1) ){
            alert("Please enter a valid email address. [1003]");
            //df.elements[fieldName].select();
            fieldIsValid = false;
            return fieldIsValid;
        }
    }
    return fieldIsValid;
}


function cleanText(formName, fieldName, showAlert){

    var df = document.forms[formName];
    var fieldIsValid = true;
    var msgEnding = " was found and replaced with its encoded replacement, this may alter your expected results if this was used in a search field.";

    text = df.elements[fieldName].value;



    // Encode Ampersand
    if( text.indexOf("&") > -1 ){
        text = text.replace("&", "&#38;");
        if(showAlert) alert("An invalid character (&)" + msgEnding);
    }

    // Encode Single Quote
    if( text.indexOf("'") > -1 ){
        text = text.replace("'", "&#39;")
        if(showAlert) alert("An invalid character (')" + msgEnding);
    }

    // Encode Double Quote
    if( text.indexOf("\"") > -1 ){
        text = text.replace("\"", "&quot;")
        if(showAlert) alert("An invalid character (\")" + msgEnding);
    }

    // Encode Less Than
    if( text.indexOf(">") > -1 ){
        text = text.replace(">", "&gt;")
        if(showAlert) alert("An invalid character (>)" + msgEnding);
    }

    // Encode Greater Than
    if( text.indexOf("<") > -1 ){
        text = text.replace("<", "&lt;")
        if(showAlert) alert("An invalid character (<)" + msgEnding);
    }

    // Encode Close Bracket
    if( text.indexOf(")") > -1 ){
        text = text.replace(")", "&#41;")
        if(showAlert) alert("An invalid character ())" + msgEnding);
    }

    // Encode Open Bracket
    if( text.indexOf("(") > -1 ){
        text = text.replace("(", "&#40;")
        if(showAlert) alert("An invalid character (()" + msgEnding);
    }

    // Encode Close Square Bracket
    if( text.indexOf("]") > -1 ){
        text = text.replace("]", "&#93;")
        if(showAlert) alert("An invalid character (])" + msgEnding);
    }

    // Encode Open Square Bracket
    if( text.indexOf("[") > -1 ){
        text = text.replace("[", "&#91;")
        if(showAlert) alert("An invalid character ([)" + msgEnding);
    }

    // Encode Semicolon
    if( text.indexOf(";") > -1 ){
        text = text.replace(";", "&#59;")
        if(showAlert) alert("An invalid character (;)" + msgEnding);
    }

    // Encode Colon
    if( text.indexOf(":") > -1 ){
        text = text.replace(":", "&#58;")
        if(showAlert) alert("An invalid character (:)" + msgEnding);
    }

    // Encode Forward Slash
    if( text.indexOf("/") > -1 ){
        text = text.replace("/", "&#47;")
        if(showAlert) alert("An invalid character (/)" + msgEnding);
    }

    // Encode Right Brace
    if( text.indexOf("}") > -1 ){
        text = text.replace("}", "&#125;")
        if(showAlert) alert("An invalid character (})" + msgEnding);
    }

    // Encode Left Brace
    if( text.indexOf("{") > -1 ){
        text = text.replace("{", "&#123;")
        if(showAlert) alert("An invalid character ({)" + msgEnding);
    }

    // Encode Exclamation
    if( text.indexOf("!") > -1 ){
        text = text.replace("!", "&#33;")
        if(showAlert) alert("An invalid character (!)" + msgEnding);
    }

    // Encode Double Dash
    if( text.indexOf("--") > -1 ){
        text = text.replace("--", "&#45;&#45;")
        if(showAlert) alert("An invalid character (--)" + msgEnding);
    }

    // Encode Equal Sign
    if( text.indexOf("=") > -1 ){
        text = text.replace("=", "&#61;")
        if(showAlert) alert("An invalid character (=)" + msgEnding);
    }

    // Encode Underscore
    if( text.indexOf("_") > -1 ){
    text = text.replace("_", "&#95;")
        if(showAlert) alert("An invalid character (_)" + msgEnding);
    }

    df.elements[fieldName].value = text;
}
