//=========================================================
//        Name:        verifyEmail
//        Desc:        Validates an email address.
//=========================================================
function verifyEmail(emailStr)
{
        if(emailStr.length == 0)
                return true;

        var emailPat=/^(.+)@(.+)$/;

        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]";
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=emailStr.match(emailPat);

        if (matchArray==null)
        {
                alert("Email address seems incorrect (check @ and .'s)");
                return false;
        }

        var user=matchArray[1];
        var domain=matchArray[2];

        if (user.match(userPat)==null)
        {
            alert("The username doesn't seem to be valid.");
            return false;
        }

        var IPArray=domain.match(ipDomainPat);

        if (IPArray!=null)
        {
          for (var i=1;i<=4;i++)
          {
            if (IPArray[i]>255)
            {
                        alert("Destination IP address is invalid!");
                        return false;
            }
    }
    return true;
        }

        var domainArray=domain.match(domainPat);
        if (domainArray==null)
        {
                alert("The domain name doesn't seem to be valid.");
            return false;
        }

        var atomPat=new RegExp(atom,"g");
        var domArr=domain.match(atomPat);
        var len=domArr.length;
        if (domArr[domArr.length-1].length<2 ||
            domArr[domArr.length-1].length>3)
        {
           alert("The address must end in a three-letter domain, or two letter country.");
           return false;
        }

        if (len<2)
        {
           var errStr="This address is missing a hostname!";
           alert(errStr);
           return false;
        }

        return true;
}

//=========================================================
//        Name:        verifyPhone
//        Desc:        Validates a phone number to north american
//                        standards.
//=========================================================
function verifyPhone(phoneStr)
{
        var strFormat = /^([0-9]{3})-([0-9]{3})-([0-9]{4})$/;
        var splitPhoneStr = phoneStr.value.match(strFormat);

        if(splitPhoneStr == null)
                return false;

        var phoneNumber = parseInt(splitPhoneStr[1] + splitPhoneStr[2] + splitPhoneStr[3], 10);

        if(phoneNumber == NaN)
                return false;

        if(phoneNumber.toString(10).length != 10)
                return false;

        return true;
}

//=========================================================
//        Name:        verifyAddr
//        Desc:        Validates a street address.
//=========================================================
function verifyAddr(addrStr)
{
        var strFormat = /^([0-9])+/;
        var splitAddrStr = addrStr.value.match(strFormat);

        if(splitAddrStr == null)
                return false;

        return true;
}

//=========================================================
//        Name:        verifyName
//        Desc:        Validates a persons name.
//=========================================================
function verifyName(nameStr)
{
        var strFormat = /^[a-zA-Z\s]+/;
        var splitNameStr = nameStr.value.match(strFormat);

        if(splitNameStr == null)
                return false;

        return true;
}

//=========================================================
//        Name:        verifyUser
//        Desc:        Validates a username.
//=========================================================
/*function verifyUser(userStr)
{
        var strFormat = /^[a-zA-Z][0-9a-zA-Z]+/;
        var splitUserStr = userStr.value.match(strFormat);

        if(splitUserStr == null)
                return false;

        return true;
}*/
function verifyUser(userStr) {

    var goodChars = /\W/; // allow letters, numbers, and underscores

    var yes = goodChars.test(userStr.value);
      if(yes)
       return false;

return true;
}

//=========================================================
//        Name: verifyEmpty
//        Desc: Checks if a field is empty.
//=========================================================
function verifyEmpty(str)
{
        alert(str.value.length);
        if(str.value.length == 0)
                return false;
        return true;
}

//=========================================================
//        Name:        verifyDomain
//        Desc:        Checks if a domain name is valid.
//=========================================================
function verifyDomain(domainStr)
{
        var strFormat = /^([A-Za-z0-9-]{1,68})\.(com|net|org)/i;
        var splitDomainStr = domainStr.value.match(strFormat);

        if(splitDomainStr == null)
                return false;

        return true;
}

//=========================================================
//        Name:        verifyDomain
//        Desc:        Checks if a domain name is valid.
//=========================================================
function verifyState(stateStr)
{
        var strFormat = /^[A-Za-z]/;
        var splitStateStr = stateStr.value.match(strFormat);

        if(splitStateStr == null)
                return false;

        return true;
}

//=========================================================
//        Name: verifyAlpha
//        Desc: Checks if a field is of alphanumeric value.
//=========================================================
function verifyAlpha(alphaStr)
{
        var strFormat = /[A-Za-z0-9\-\+\.]+/;
        var splitAlphaStr = alphaStr.value.match(strFormat);

        if(splitAlphaStr == null)
                return false;

        return true;
}

//=========================================================
//        Name: verifyNumber
//        Desc: Checks if a field is of numeric value.
//=========================================================
function verifyNumber(numberStr)
{
        var strFormat = /^[0-9]+$/;
        var splitNumberStr = numberStr.value.match(strFormat);

        if(splitNumberStr == null)
                return false;

        return true;
}

//=========================================================
//        Name: verifyCcNumber
//        Desc: Checks if a field is of numeric value.
//=========================================================
function verifyCcNumber(numberStr)
{
        var strFormat = /^[0-9]{15,16}$/;
        var splitNumberStr = numberStr.value.match(strFormat);

        if(splitNumberStr == null)
                return false;

        return true;
}

//=========================================================
//        Name: verifyZipcode
//        Desc:        Checks a US or canadian zipcode for validity.
//=========================================================
function verifyZipcode(zipcodeStr)
{
        var strFormat = /^[0-9]{5}$|^[A-Za-z]{1}\d{1}[A-Za-z]{1}\s{1}\d{1}[A-Za-z]{1}\d{1}$/;
        var splitZipcodeStr = zipcodeStr.value.match(strFormat);

        if(splitZipcodeStr == null)
                return false;

        return true;
}

//=========================================================
//        Name:        verifyReq
//        Desc:        Checks a form to make sure required fields are
//                        filled.
//=========================================================
function verifyReq(form, fields, errors)
{
        var frmFields, frmErrors, len;

        frmFields = fields.split(",");
        frmErrors = errors.split(",");
        len = frmFields.length;



        if(frmFields[0] == "" || frmErrors[0] == "")
                return true;

        for(i = 0; i < len; i++)
        {
	
	        if(form.elements(frmFields[i]) == null)
                {
                        alert(frmErrors[i]);
                        return false;
                }
                if(form.elements(frmFields[i]).value.length == 0)
                {
                        alert(frmErrors[i]);
                        return false;
                }
        }

        return true;
}


//=========================================================
//        Name: verifyPassword
//        Desc:        Validates a password so that it matches and
//                        has only valid chars.
//=========================================================
function verifyPassword(pwdObj1, pwdObj2)
{
        if(pwdObj1.value.length == 0)
        {
                pwdObj2.value = "";
                return true;
        }

        if(pwdObj1.value != pwdObj2.value)
        {
                pwdObj1.value = "";
                pwdObj2.value = "";
                alert('The passwords do not match, please enter them again.');
                return false;
        }

        return true;
}

//=========================================================
//        Name:        verifyForm
//        Desc:        Validates any type of field.
//=========================================================
function verifyForm(obj, type)
{
        var errorStr = "";
        var verified = false;

        switch(type)
        {
                case "phone":
                        if(!(verified = verifyPhone(obj)))
                                errorStr = "The phone number you've entered isn't valid. (Format - '555-555-5555', no spaces.)";
                break;

                case "number":
                        if(!(verified = verifyNumber(obj)))
                                errorStr = "The number you've entered isn't valid. (Format - '12', no letters or spaces.)";
                break;

                case "address":
                        if(!(verified = verifyAddr(obj)))
                                errorStr = "The address you've entered isn't valid. (Format - '20 Street st [, suite 1]', the suite or apt number is optional, no periods.)";
                break;

                case "email":
                        if(!(verified = verifyEmail(obj.value)))
                                errorStr = "The email address you've entered isn't valid. (Format - 'user@host.com')";
                break;

                case "user":
                        if(!(verified = verifyUser(obj)))
                                errorStr = "The Username you've entered isn't valid. (Format - 'user', no spaces or special characters.)";
                break;

                case "name":
                        if(!(verified = verifyName(obj)))
                                errorStr = "The Name you've entered isn't valid. (Format - letters only.)";
                break;

                case "alpha":
                        if(!(verified = verifyAlpha(obj)))
                                errorStr = "The value you've entered isn't valid. (Format - '123 abc', any combination of letters and numbers)";
                break;

                case "domain":
                        if(!(verified = verifyDomain(obj)))
                                errorStr = "The domain name you've entered isn't valid. (Format - 'domain.com', valid extensions: .com, .net, .org)";
                break;

                case "state":
                        if(!(verified = verifyState(obj)))
                                errorStr = "The state you've entered isn't valid. (Format - letters only.)";
                break;

                case "ccnumber":
                        if(!(verified = verifyCcNumber(obj)))
                                errorStr = "The credit card number you've entered isn't valid. (Format '1234123412341234', digits only, no spaces, no dashes.)";
                break;

                case "zipcode":
                        if(!(verified = verifyZipcode(obj)))
                                errorStr = "The zipcode you've entered isn't valid. (Format - '12345' or 'A1A 2B2' (Canada).)";
                break;

        }

        if((!verified) && (obj.value.length > 0))
        {
                alert(errorStr);
                obj.value = '';
                obj.focus();
        }

        return verified;
}

