//*********************************************************************
//
// Modified Versions of .Net Validator Methods
//
//*********************************************************************

function RangeValidatorEvaluateIsValid2(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
            ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
}

// this was modified so that it will do a compare regardless of the fact
// the control contains empty contents. The reason for this is to allow 
// the compare control to dictate whether the confirm control is required
function CompareValidatorEvaluateIsValid2(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    //if (ValidatorTrim(value).length == 0)
    //    return true;
    var compareTo = "";
    if ((typeof(val.controltocompare) != "string") ||
        (typeof(document.getElementById(val.controltocompare)) == "undefined") ||
        (null == document.getElementById(val.controltocompare))) {
        if (typeof(val.valuetocompare) == "string") {
            compareTo = val.valuetocompare;
        }
    }
    else {
        compareTo = ValidatorGetValue(val.controltocompare);
    }
    var operator = "Equal";
    if (typeof(val.operator) == "string") {
        operator = val.operator;
    }
    return ValidatorCompare(value, compareTo, operator, val);
}

function Validate_ABANumber(s, args)
{
   var str = document.getElementById(s.controltovalidate).value;
   var iTotal = 0;
   var iNum =0;
   args.IsValid = false;

   if (str.length == 9)
   {
        iNum = parseInt(str.charAt(0)); 
        iTotal += iNum * 3; 
       
        iNum = parseInt(str.charAt(1)); 
        iTotal += iNum * 7; 

        iNum = parseInt(str.charAt(2)); 
        iTotal += iNum; 

        iNum = parseInt(str.charAt(3)); 
        iTotal += iNum * 3; 

        iNum = parseInt(str.charAt(4)); 
        iTotal += iNum * 7; 

        iNum = parseInt(str.charAt(5)); 
        iTotal += iNum; 

        iNum = parseInt(str.charAt(6)); 
        iTotal += iNum * 3; 

        iNum = parseInt(str.charAt(7)); 
        iTotal += iNum * 7; 

        iNum = parseInt(str.charAt(8)); 
        iTotal += iNum; 

        if ((iTotal % 10) == 0) 
            args.IsValid = true;
    }
    return args.IsValid;
 }
        


//*********************************************************************
//
// isCharCode Methods
//
//*********************************************************************

function isCharAlpha(c)
{
	if (c > 122 && c < 65)	
		return false;
	return true;
}
function isCharAlphaNumeric(c)
{
	if (c > 122 && c < 48)	
		return false;
	return true;
}

function isCharNumeric(keyCode)
{
    // Same as isCharNumeric, except no minus char
    // 45='-', 46='.', 57='9', 48='0'
    if (keyCode != 46 && 
        keyCode != 45 &&
       (keyCode > 57 | keyCode < 48))
	{
		return false;
	}
	return true;
}

function isCharNumber(keyCode)
{
    // 57='9', 48='0'
    if (keyCode > 57 | keyCode < 48)
		return false;
	return true;
}

//*********************************************************************
//
// is keycode Methods
//
//*********************************************************************

function isstring(e)
{
	if (e.keyCode > 122)		
		e.keyCode=0;
	if (e.keyCode == 39)
		e.keyCode=34;
}
function isnumber(e)
{
    // 45='-', 46='.', 57='9', 48='0'
    if (e.keyCode != 46 && 
        e.keyCode != 45 && 
       (e.keyCode > 57 | e.keyCode < 48))
	{
		e.keyCode = 0;
	}
}
function isnumeric(e)
{
    if (e.keyCode != 46 && 
       (e.keyCode > 57 | e.keyCode < 48))
	{
		e.keyCode = 0;
	}
}

//*********************************************************************
//
// is string Methods
//
//*********************************************************************

function isEmpty(s) 
{   
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s) 
{   
	var i;
    var whitespace = " \t\n\r";
    if (isEmpty(s)) 
        return true;
    
    for (i = 0; i < s.length; i++) 
    {   
		var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) 
            return false;
    }
    return true;    
}

function isNumeric(s)
{
    var i;
    var c;
    if (isWhitespace(s)) 
        return false;
    for (i=0; i<s.length; i++)
    {
 		c = s.charAt(i);
		if (!((c >= "0") && (c <= "9")))
			return false;
	}
	return true;
}

function isBool(s)
{
    s = s.toUpperCase();
    if (s == "TRUE" || s == "FALSE" )
	    return true;
    else
	    return false;
}

function isDecimal(s)
{
    var i,dotCounter;
    var c;
    dotCounter = 0;
    if (isWhitespace(s)) 
        return false;
    for (i=0; i<s.length; i++)
    {
 		c = s.charAt(i);
		if (!((c >= "0") && (c <= "9")))
		   if ( c !=".")
			  return false; 
		   else
		   	  ++dotCounter;
	}
	if (dotCounter > 1)
		return false;
	else if (dotCounter == s.length)
		return false;
	return true;
}

function isDateValid(s)
{
	var i,j,k;
    var c;
    j = 0;
    k = 0;
    if (isWhitespace(s)) return false;
	   for (i=0; i<s.length; i++){
 		c = s.charAt(i);
 		if (!((c >= "0") && (c <= "9")))
			if (c!="/")
				return false;
			else
				if (k == 0)
					if ((i < 1) || (i > 2))
						return false;
					else
					{	
						var No;
						k = i;
						No = GetValue(s,0,i);
						if (!((No > 0) && (No <= 12)))
							return false;
						else
							j +=1;	
					}	
				else
					if ((i-k-1)>2)
						return false;
					else
					{
						var No;
						No = GetValue(s,k+1,i);
						if (!((No > 0) && (No<=31)))
							return false;
						else
						{
							j +=1;
							if (j != 2)
								return false;
							k = i;
						}
					}
		}
		
		if (j != 2)
			return false;
			
		j = s.substring(k+1,s.length);
		if (j.length > 4 || j.length < 4 )
			return false;
	return true;
}

function IsDate(strDate) 
{
    if (strDate.length == 0)
        return false;

    var strDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = strDate.match(strDatePattern); // is the format ok?

    if (matchArray == null) {
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];
    if (month < 1 || month > 12) { // check month range
        return false;
    }

    if (day < 1 || day > 31) {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true; // date is valid
}

function IsMoney(strMoney)
{
    if (!isDecimal(strMoney))
        return false;
    /*
    aryMoney = strMoney.split(".");
    if (aryMoney[1].length>2)
        return false;
    */
    return true;
}


function CleanWholeNumber(s)
{
    var s2 = "";
    for (i = 0; i < s.length; i++)
    {
       if (isCharNumber(s.charCodeAt(i)))
           s2 += s.charAt(i);
    }
    return s2;
}

function CleanDecimalNumber(s)
{
    var s2 = "";
    for (i = 0; i < s.length; i++)
    {
       if (isCharNumeric(s.charCodeAt(i)))
           s2 += s.charAt(i);
    }
    return s2;
}

function IsPhone(strPhone)
 {
    return (CleanWholeNumber(strPhone).length == 10);
}
function IsZip(strZip)
{
    var s = CleanWholeNumber(strZip);
    return (s.length == 5 || s.length == 9);
}

//*********************************************************************
//
// Format Methods
//
//*********************************************************************

function FormatPhone(strPhone)
{
    var s = CleanWholeNumber(strPhone);
    if (s.length != 10)
        return strPhone;
    return '(' + s.substring(0,3) + ') ' + 
                 s.substring(3,6) + '-' + 
                 s.substring(6,10);
}
function FormatDate(strDate)
{
    if (!IsDate(strDate))
        return strDate;

    var strDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = strDate.match(strDatePattern); // is the format ok?

    if (matchArray == null) 
        return strDate;
    
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    return month + "/" + day + "/" + year;
}


function ApplyCleanWholeNumber(arg)
{
    var newval = CleanWholeNumber(arg.value);
    if (newval != arg.value)
    {
        arg.value = newval;
        arg.select();
        arg.focus();
    }
}

function ApplyFormatPhone(arg)
{
    arg.value = FormatPhone(arg.value);
}

function ApplyFormatDate(arg)
{
    arg.value = FormatDate(arg.value);
}

//*********************************************************************
//
// Validate Methods
//
//*********************************************************************

var valid = new Object();

valid.zipCode = /\d{5}/;    // /\d{5}(-\d{4})?/;      
valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 
valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;		
valid.phoneNumberNumericOnly = /^\d{10}$/;		

function validateNumeric(value,minVal,maxVal,maxDecimal){
	var num;
	var val = CleanDecimalNumber(value);

	if (maxDecimal!=0)
		eval("num = /^\\d{"+minVal.toString().split(".")[0].length.toString()+","
		                   +maxVal.toString().split(".")[0].length.toString()+"}.?\\d{0,"+maxDecimal+"}?$/");
	else
		eval("num = /^\\d{"+minVal.toString().split(".")[0].length.toString()+","
		                   +maxVal.toString().split(".")[0].length.toString()+"}?$/");

	var result = num.exec(val);

	if(!(result))
	{
		return false;
    }
	else
	{
	    var resultLI = (result!=null) ? val.indexOf(result[0])+result[0].length : 0;
		if(resultLI != val.length) 
		{
		    return false;
		}
	}
	if(parseFloat(val) < parseFloat(minVal) || parseFloat(val) > parseFloat(maxVal))
	{
	    return false;
    }
    return true;
}

function validateString(value, minLength, maxLength)
{
	value = ValidatorTrim(value);
	if(value.length<minLength||value.length>maxLength)
	{
		return false;
	}
	return true;
}

function validateFormat(value, formatName) 
{
	switch (formatName)
	{
		case "zip":
			var result = valid.zipCode.exec(value);				
			break;
		case "date":
			return IsDate(value);
		case "phone":
			var result = valid.phoneNumber.exec(value);
			break;
		case "phonenumeric":
		    var value = CleanWholeNumber(value);
			var result = valid.phoneNumberNumericOnly.exec(value);
			break;
		case "email":
			var result = valid.emailAddress.exec(value);
			break;
	}
	
	if(!(result) || 
	    result.lastIndex != value.length)
	{
		return false;
	}
	return true;
}    


//*********************************************************************
//
// Behaviour Methods
//
//*********************************************************************

function autoTab2(input,len, e) 
{
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if (input.value.length >= len && !containsElement(filter,keyCode)) {
			input.value = input.value.slice(0, len);
			input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	return true;

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while (!found && index < arr.length)
			if (arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input) 
				index = i;
			else 
				i++;
		return index;
	}
}

