/**
 * Validates the phone number. The number can be digits, or a + followed by
 * digits.
 *
 * @param field - the phone number containing field.
 * @return true if the phone number is valid, false otherwise.
 */
function valPhoneNumber(field)
{
    var phoneReg = /^[+]?[0-9 ]*$/;
    var strValue = field.value;
    var blnCorrect = true;
    
    if(null == strValue || 6 > strValue.length || "" == strValue ||
       !phoneReg.test(strValue))
    {
        blnCorrect = false;
    }
    
    return blnCorrect;
    
}

/**
 * Validates the email. The email can be at least 1 letter or digit followed by
 * letters digits . _ or @ and must end with at least one dot followed by 2
 * characters.
 * 
 * @param field - the email containing field.
 * @return true if the email is valid, false otherwise.
 */
function valEmail(field)
{
    //var emailReg = /^[a-zA-Z0-9].+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
    var emailReg = /^[a-zA-Z0-9]{1}[a-zA-Z0-9\.\-_]+\@[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,3}){1,2}$/;
    var strValue = field.value;
    var blnCorrect = true;

    if(null == strValue || 0 == strValue.length || "" == strValue ||
       !emailReg.test(strValue))    
    {
        blnCorrect = false;
    }
    
    return blnCorrect;
}

/**
 * Ensures that the supplied field contains numeric information.
 *
 * @param field - the field to check.
 * @return true if the field contains numeriuc information, false otherwise.
 */
function valNumeric(field)
{
    var numericReg = /^[0-9]*$/;
    var strValue = field.value;
    var blnCorrect = true;

    if(null == strValue || 0 == strValue.length || "" == strValue ||
       !numericReg.test(strValue))    
    {
        blnCorrect = false;
    }
    
    return blnCorrect;
}

/**
 * Ensures that the supplied field contains one of the supplied possible values.
 *
 * @param field - the field to check.
 * @param strPossibilities - an array of the possible values
 * @return true if the field contains one of the supplied possibilities, false otherwise.
 */
function valOneOf(field, strPossibilities)
{
    var strValue = field.value;
    var blnCorrect = false;
    var i;

    for(i=0; i<strPossibilities.length; i++)
    {
        if(strValue == strPossibilities[i])
        {
            blnCorrect = true;
            break;
        }
    }
    
    return blnCorrect;
}

/**
 * Ensures that the supplied field contains information.
 *
 * @param field - the field to check.
 * @return true if the field contains information, false otherwise.
 */
function valNonEmpty(field)
{
    var strValue = field.value;
    var blnReturn = true;
    
    blnReturn = (!(null == strValue || 0 == strValue.length || "" == strValue));
    
    /*Since the smartText puts in two characters when the form is first loaded (ASCII
      13 followed by 10) make sure that the user has updated this to something
      reasonable*/
    if(blnReturn)
    {
        if(2 == strValue.length)
        {
            if((13 == strValue.charCodeAt(0)) && (10 == strValue.charCodeAt(1)))
                blnReturn = false;
        }
    }
   
    return blnReturn;
}

/**
 * Ensures that the supplied select field's selection contains valid information.
 *
 * @param field - the field to check.
 * @return true if the field contains information, false otherwise.
 */
function valNonEmptySelection(field)
{
    var strValue = field[field.selectedIndex].value;
    
    blnReturn = (!(null == strValue || 0 == strValue.length || "" == strValue));
   
    return blnReturn;
}

/**
 * Validates a date in the format DD/MM/YYYY.
 * The date is tested for the correct format, and then the days in the month are
 * tested, keeping leap year validation in mind. Years 1900-2099 allowed
 *
 * @param field - the field containing the date to be validated.
 * @return - true if the date is valid, false otherwise.
 */
function validDate(field)
{
    var strValue = field.value;
    return(validDateString(strValue));
}

/**
 * Validates a date in the format DD/MM/YYYY.
 * The date is tested for the correct format, and then the days in the month are
 * tested, keeping leap year validation in mind. Years 1900-2099 allowed
 *
 * @param strValue - the string date to be validated.
 * @return - true if the date is valid, false otherwise.
 */
function validDateString(strValue)
{
    var dateReg = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/(19|20)(\d\d)$/;
    var blnReturn = true;
    var valDays = (0,31,29,31,30,31,30,31,31,30,31,30,31);
    var iMonth, iDay;
    var blnLeapYear = false;

    /*Check that we have something, and that the pattern is valid*/
    if(null == strValue || 0 == strValue.length || "" == strValue ||
       !dateReg.test(strValue))
    {
        blnReturn = false;
    }
    else
    {
        /*Since there is something, and the pattern is valid, ensure that
          the days are within the correct range for the month*/
        iYear = strValue.substr(6,4);
        iMonth = strValue.substr(3,2);
        iDay = strValue.substr(0,2);

        if(iMonth == 2)
        {
            /* Validation leap-year */
            if ((iYear % 4 == 0) || (iYear % 100 == 0) || (iYear % 400 == 0))
            {
                blnLeapYear = true;
            }
            if (blnLeapYear && iDay > 29)
            {
                blnReturn = false;
            }
            if (!blnLeapYear && iDay > 28)
            {
                blnReturn = false;
            }
        }
        else
        {
            /*Month Validation for all other months*/
            if(iDay > valDays[iMonth])
                blnReturn = false;
        }
   }
    return blnReturn;
}

/**
 * Validates a time in the format HH:MM:SS.
 * This allows times in the range 00:00:00 - 23:59:59.
 *
 * @param strValue - the string time to be validated.
 * @return - true if the time is valid, false otherwise.
 */
function validTimeString(strValue)
{
    var timeReg = /^([01]\d|2[0-3])(:)([0-5]\d)(:)([0-5]\d)$/;
    
    if(null == strValue || 0 == strValue.length || "" == strValue ||
       !timeReg.test(strValue))
        return false;
        
    return true;
}

/**
 * Validates a date-time in the format HH:MM:SS DD/MM/YYYY.
 *
 * @param field - the field containing the date time to be validated.
 * @return - true if the date time is valid, false otherwise.
 */ 
function validDateTime(field)
{
    /*Firstly we have to ensure that there is something here, then try
      spliting it before passing it to the validDate and validTime functions*/
    var strValue = field.value;
    
    if(null == strValue)
        return false;
        
    var strSplit = strValue.split(" ");
    
    if(2 != strSplit.length)
        return false;
        
    return(validDateString(strSplit[1]) && validTimeString(strSplit[0]));
}

/**
 * Ensures that the input is a valid UK post code.
 *
 * @param field - the field containing the post code to be validated.
 * @return - true if the post code is valid, false otherwise.
 */
function valPostCode(field)
{
    var strValue = field.value;
    var pcReg = /^[A-Za-z]{1,2}[\d]{1,2}([A-Za-z])?\s?[\d][A-Za-z]{2}$/;
    
    if(null == strValue || 0 == strValue.length || !pcReg.test(strValue))
        return false;
        
    return true;
}

/**
 * Ensures that the input is a valid US zip code.
 *
 * @param field - the field containing the zip code to be validated.
 * @return - true if the zip code is valid, false otherwise.
 */
function valZipCode(field)
{
    var strValue = field.value;
    var zipReg = /^[0-9]{5}$/;
    
    if(null == strValue || 0 == strValue.length || !zipReg.test(strValue))
        return false;
        
    return true;
}


/**
 * Ensures that the password is a valid format.
 *
 * @param field - the field containing the password to be validated.
 * @return - true if the password is valid, false otherwise.
 */
function valPassword(field)
{
    var strValue = field.value;
    //Password must consist of at least 1 lower case letter, 1 upper case letter and 1 number
    var passReg = /^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/;
    
    //Also it must be between 8 and 20 characters
    if(null == strValue || 8 > strValue.length || 20 < strValue.length || !passReg.test(strValue))
        return false;
        
    return true;
}

/**
 * Ensures that the IP address is a valid format "#.#.#.#".
 *
 * @param field - the field containing the IP Address to be validated.
 * @return - true if the IP is valid, false otherwise.
 */
function valIP(field)
{
    var strValue;
    
    if(field.value)
    {
        strValue = field.value;
    }
    else
    {
        strValue = field;
    }
        
    var ipReg = /^((([0-9][0-9]?)|([01][0-9]?[0-9]?)|([2][0-9]?)|([2][0-4][0-9])|([2][5][0-4]))\.){3}(([0-9][0-9]?)|([01][0-9]?[0-9]?)|([2][0-9]?)|([2][0-4][0-9])|([2][5][0-5]))$/;
    
    if(null == strValue || 0 == strValue.length || !ipReg.test(strValue))
        return false;
        
    return true;
}

/**
 * Ensures that the Subnet is a valid format 255s followed by 0s.
 *
 * @param field - the field containing the subnet mask to be validated.
 * @return - true if the mask is valid, false otherwise.
 */
function valSubnet(field)
{
    var strValue = field.value;
    var subnetReg = /^((([2][5][5]\.){3}([0]))|(([2][5][5]\.){2}([0][\.][0]))|(([2][5][5]\.)([0][\.]){2}[0]))$/;
    
    if(null == strValue || 0 == strValue.length || !subnetReg.test(strValue))
        return false;
        
    return true;
}
