function validateContact(objForm)
{
    var blnProceed = true;
    var objError =  document.getElementById('errorMsg');
    var strMessage = '';
    if (!checkEmail(objForm.email)) {
        blnProceed = false;
        strMessage += '* That wasn\'t a real email address. Check your spelling and try again.';
    }

    if (objForm.category) {
        if (objForm.category.value != 'null') {
            objForm.category.style.backgroundColor = "white";            
        } else {
            objForm.category.style.backgroundColor = "red";
            blnProceed = false;
            strMessage += strMessage ? '<br/>' : '';
            strMessage += '* Please select a topic.';
        }
    }

    if (!checkInput(objForm.message)){
        blnProceed = false;
        strMessage += strMessage ? '<br/>' : '';
        strMessage += '* You left out your message. Think of something to say, and try again.';
    }

    objError.innerHTML = strMessage;

    if (blnProceed){
        hideError(objError);
    } else {
        showError(objError);
    }

    return blnProceed;
}


/*
    Refactoring to make a generic validaton script.  This should validate any form when finished.
*/
function validate(formId) {
    
   /*   
        This is a generic validation fucntion that will validate 
        any form provided it follows the following guidlines:
        ---------------------------------------------------------
        1)input tag must contain the class of inputrequired
        2)input tag must contain one of the following class attributes 
            ** emailtype
            ** inputtype
            ** checkboxtype
            ** radiotype
            ** cctype // credit card
        3)Should have a dom object with the id of "errorMsg"
            ** this obj should contain the error to display 
            and should be styled "display: none;"
        4)This file is depended on the protoype.js file
            ** If we Choose not to use protoype We'll need to rethink this
   */
   alert(formId);
   objForm = $(formId);
   var lstFormEle = objForm.getElements();
   var objError =  document.getElementById('errorMsg');
   var blnProceed = true;
   
   for (var x=0; x < lstFormEle.length; x++) {
       if (lstFormEle[x].hasClassName('inputrequired')) {
           if (lstFormEle[x].hasClassName('emailtype')) {
               // NOTE: validate for email
               if (!checkEmail(lstFormEle[x])) {
                   blnProceed = false;
               }
           } else if (lstFormEle[x].hasClassName('inputtype')) {    
               // NOTE: validate for inputbox
               if (!checkInput(lstFormEle[x])){
                   blnProceed = false;
               }
           } else if (lstFormEle[x].hasClassName('radiotype')) {
               // TODO: validate for radio
           } else if (lstFormEle[x].hasClassName('checkboxtype')) {
               // TODO: validate for checkbox
           } else if (lstFormEle[x].hasClassName('cctype')) {
               // TODO: validate for checkbox
           } 
       }
   }
   
   if (blnProceed) {
       hideError(objError);
   } else { 
       showError(objError);
   }  
   
   return blnProceed;    
}

function findElement(varElement) {
    var objEl;
    if (typeof(varElement) == "object") {
        if (varElement.nodeType) {
            objEl = varElement;
        }
    } else if (typeof(varElement) == "string") {
        objEl = document.getElementById(varElement);
    }
    return objEl;
}

function hideError(objErr) {
    objErr.style.display = 'none';
}

function showError(objErr) {
    objErr.style.display = 'inline';
}

function highlightError(varElement) {
    var element = findElement(varElement);
    try { 
        element.style.border = "1px solid #ff0000";
    } catch (ex) {}
}

function clearError(varElement) {
    var element = findElement(varElement);
    try {
        element.style.border = "1px solid gray";
    } catch(e) { }
}

function highlight_error(varElement) {
    return highlightError(varElement);
}

function clear_error(varElement) {
    return clearError(varElement);
}

function checkEmail(tagname) {
	if(!tagname) tagname = "email";
    var strEmail = checkInput(tagname);
    var intAmpersand;
    var strUser;
    var strDomain;
    var arrDomainParts = Array();
    var intDomainLength;

    clearError(tagname);

    intAmpersand  = strEmail.indexOf("@");
    if (intAmpersand < 1) // no "@" in email (or "@" first char)
    {
        highlightError(tagname);
        return false;
    }
    if (strEmail.length == intAmpersand + 1) // "@" is last char
    {
        highlightError(tagname);
        return false;
    }
    if (strEmail.lastIndexOf("@") != intAmpersand) // multiple "@"'s in email
    {
        highlightError(tagname);
        return false;
    }
    strUser = strEmail.split("@")[ 0 ];
    strDomain = strEmail.split("@")[ 1 ];
    if (strUser == "" | strDomain == "") // missing part of email
    {
        highlightError(tagname);
        return false;
    }
    // user shouldn't contain '()<>:;,[ ]\" ' (delivermail.py)
    if (
        (strUser.indexOf("(") >= 0) | (strUser.indexOf(")") >= 0) |
        (strUser.indexOf("<") >= 0) | (strUser.indexOf(">") >= 0) |
        (strUser.indexOf(";") >= 0) | (strUser.indexOf(":") >= 0) |
        (strUser.indexOf(",") >= 0) | (strUser.indexOf("[") >= 0) |
        (strUser.indexOf("]") >= 0) | (strUser.indexOf('"') >= 0) |
        (strUser.indexOf("\\\\") >= 0) | (strUser.indexOf(" ") >= 0)
    )
    {
        highlightError(tagname);
        return false;
    }
    // domain should be text.text[ .text ]*
    if (strDomain.indexOf("..") != -1) // part of domain missing
    {
        highlightError(tagname);
        return false;
    }
    arrDomainParts = strDomain.split(".");
    intDomainLength = arrDomainParts.length;
    if (intDomainLength == 1)
    {
        highlightError(tagname);
        return false;
    }
    if (arrDomainParts[ intDomainLength-1 ].length < 2 | arrDomainParts[ intDomainLength-1 ].length > 6)
    {
        // top level is wrong length (2-char country code, or 3-6 char whatchallit)
        highlightError(tagname);
        return false;
    }
    if (strDomain == "aol.com")
    {
		if(strUser.length < 3 || strUser.length >16)
		{
			highlightError(tagname);
			return false;
		}
    }
    return true; // passed all the tests
}

function checkInput(varElement, blnHighlightError) {
    if (blnHighlightError == undefined) {
        blnHighlightError = true;
    }
    clearError(varElement);
    el = findElement(varElement);
    if (el) {
        var value = el.value;
        if ((value == "" || value == undefined) && blnHighlightError) {
            highlightError(varElement);
            blnProceed = false;
        }
        return el.value;
    } else {
        return "";
    }
}
