﻿/**
 * The Validator class
 *
 * @author Scott Noring
 * @author Mandarin Drummond
 */
 
/**
 * MD, 09132007, these global vars are deprecated and should be phased out of existing code when time permits
 */
var _emailFilter=/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
var _zipFilter =/(^\d{5}$)|(^\d{5}-\d{4}$)/;
var _phoneAreaCodeFilter = /^\d{3}$/; 
var _phonePrefixFilter = /^\d{3}$/; 
var _phoneSuffixFilter =/^\d{4}$/; 
//var _dateSingleField = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;  
var _dateSingleField = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
var _flag = true;
var _msg = "";

RP.Validator = {}

RP.Validator.testEqual = function(val1, val2) {  
    return (val1 != val2) ? false : true;
}

RP.Validator.testRadio = function(){
  var args = RP.Validator.testRadio.arguments;
  for(i=0;i < args.length;i++){
    if(args[i].checked == 1){
     return true;
    }
   }
}

RP.Validator.testCheckbox = function(){
  var args = RP.Validator.testCheckbox.arguments;
  for(i=0;i < args.length;i++){
    if(!args[i].checked == 1){
     return true;
    }
   }
}

RP.Validator.testEmail = function(str){
    //var regex = /^([0-9a-zA-Z]([+-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    // Changed by McQuaid Feb 2009 - from above to allow a single character domain name
var regex = /^([0-9a-zA-Z]([+-.\w]*[0-9a-zA-Z])*@[0-9a-zA-Z.-]+\.[a-zA-Z]{2,9})$/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testPassword = function(str) {
    var regex = /^([a-zA-Z0-9_]{5,16})$/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testFullName = function(str) {
   var regex = /^[a-zA-Z]{5,40} ?[a-zA-Z]{0,40}$/;
   return (!str ) ? false : true;
}

RP.Validator.testNickname = function(str) {
    var regex = /^[a-zA-Z]([a-zA-Z0-9_]{5,15})$/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testZip = function(str) {
    var regex = /\d{5}(-\d{4})?/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testPhoneNumber = function(str) {
    var regex = /[0-9]{10}/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testSecurityAnswer = function(str){
    var regex = /.{4,100}/;
    return (!str || !str.match(regex)) ? false : true;
}

RP.Validator.testGender = function(obj){
    return (obj.selectedIndex == 0) ? false : true;
}
 

   RP.Validator.testCurrentDate = function(str){
    var strMonth = str.charAt(0) + str.charAt(1);
    // convert str to single digit if less than 10
    if(strMonth.charAt(0) == '0') 
    {
        strMonth = str.charAt(1);
    }
    else
    {
        strMonth = str.charAt(0) + str.charAt(1);
    }
    
    var strDay = str.charAt(3) + str.charAt(4);
    // convert str to single digit if less than 10
    if(strDay.charAt(0) == '0') 
    {
        strDay = str.charAt(4);
    }
    else
    {
        strDay = str.charAt(3) + str.charAt(4);
    }
    var strYear = str.charAt(6) + str.charAt(7) + str.charAt(8) + str.charAt(9);
    if(strMonth.toString().length < 2)
    {
    strMonth = "0" + strMonth + "";
    }

    if(strDay.toString().length < 2)
    {
    strDay = "0" + strDay + "";
    }

    var currentDate = new Date();
    var currentMonth = currentDate.getMonth() + 1;
    var currentDay = currentDate.getDate();
    var currentYear = currentDate.getFullYear();
    if(currentMonth.toString().length < 2)
    {
    currentMonth = "0" + currentMonth + "";
    }

    if(currentDay.toString().length < 2)
    {
    currentDay = "0" + currentDay + "";
    }
    
//  SAW 10/29/2007  Added the following lines to convert the passed string and the current date back to dates
	strStrYYYYMMDD = strYear + "." + strMonth + "." + strDay;
	strCurrentYYYYMMDD = currentYear + "." + currentMonth + "." + currentDay;
    
//  SAW 10/29/2007  Commented out the following line and changed so that the passed date is compared against the current date
//                  It was raising a false error because, using 11/5/2007 as str and 10/29/2007 as current, 11 + 5 + 2007 < 10 + 29 + 2007 = 2023 < 2046 = True;
//                  The code was attempting to make sure that the str date was >= today's date
//                  The new code below fixes this issue
//    return(parseInt(strMonth) + parseInt(strDay) +  parseInt(strYear) < currentMonth + currentDay + currentYear) ? false : true;
    return(strStrYYYYMMDD >= strCurrentYYYYMMDD);
  }
   
   