﻿
function H2TextBoxGotFocus(fld, cssNameFocus, selectonfocus)
{   
    fld.className = cssNameFocus;
    
    if (selectonfocus == true)
    {
        fld.focus();
        fld.select();
    }
}

function H2TextBoxDoFocus(fld, cssNameFocus, selectonfocus)
{   
    fld.className = cssNameFocus;
    if (selectonfocus == true)
    {
        fld.focus();
        fld.select();
    }
}


function H2TextBoxLostFocus(fld, textBoxType, cssName, cssNameError)
{
    //check if the input is valid
    var valid = true;
    var userChars = '';
            
    if (textBoxType.match(/^UserDefined:/))
    {
        userChars = textBoxType.substr(11);
        textBoxType = "USERDEFINED";            
    }
        
    switch (textBoxType.toUpperCase())
    {
        case "ALL":
            fld.value = AllTrim(fld.value);     //trim the value
            valid = true;
            break;

        case "ALLNOTRIM":
            valid = true;
            break;

        case "ALPHANUMERIC":
            valid = ValidateAlphaNumeric(fld);
            break;

        case "ALPHABETS":
            valid = ValidateAlphabets(fld);
            break;

        case "DIGITS":
            valid = ValidateDigits(fld);
            break;

		case "POSATIVENUMBERS":
            valid = ValidatePosativeNumbers(fld);
            break;

		case "NUMBERS":
            valid = ValidateNumbers(fld);
            break;

        case "MONEY":
            valid = ValidateMoney(fld);
            break;

        case "SINGLE":
            valid = ValidateSingle(fld);
            break;

		case "DATE":
            valid = ValidateDate(fld);
            break;

    	case "PHONE":
            valid = ValidatePhone(fld);
            break;

    	case "USERDEFINED":
            valid = ValidateUserDefined(fld, userChars);
            break;  
            
        case "SALARY":
            valid = ValidateSalary(fld);      
            if(valid)
            {
                fld.value = "$" + fld.value;
            }    
    }

    if (valid)
    {
        fld.className = cssName;
    }
    else
    {
        fld.className = cssNameError;
    }

}


//Allow Alphabets, Underscore, Space and Numbers only
function H2TextBoxAlphaNumericKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which

    if ((key >= 65 && key <= 90) || key == 32 || (key >= 97 && key <= 122) || (key >= 48 && key <= 57) ||  key == 0 || key == 8)
    {
         return true
    }
    else
    {
         return false;
    }

}

//Allow Alphabets and Space only
function H2TextBoxAlphabetKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which;
    
    if ((key >= 65 && key <= 90) || key==32 || (key >= 97 && key <= 122) || key == 0 || key == 8 )
    {
        return true;
    }
    else
    {
        return false;
    }
}


//Allow only Digits
function H2TextBoxDigitKeyPress(e)
{  
    var key = window.event ? e.keyCode : e.which;

    if ((key >= 48 && key <= 57) || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow Digits and Minus sign
function H2TextBoxNumberKeyPress(e)
{   
    var key = window.event ? e.keyCode : e.which;    
    if ((key >= 48 && key <= 57)  || key == 45 || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow Digits and Decimal Key
function H2TextBoxITMoneyKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which

    if((key >= 48 && key <= 57) || key == 46 || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow Digits and Decimal Key
function H2TextBoxSingleKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which

    if((key >= 48 && key <= 57) || key == 46 || key == 45 || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow Digits and slash sign
function H2TextBoxDateKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which;

    if ((key >= 48 && key <= 57)  || key == 47 || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow Digits, "(", ")" and "-"
function H2TextBoxPhoneKeyPress(e)
{
    var key = window.event ? e.keyCode : e.which;

    if ((key >= 48 && key <= 57)  || key == 40 || key == 41 || key == 45 || key == 0 || key == 8)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Allow characters as per users definition
function H2TextBoxUserKeyPress(e, userChars)
{
    var key = window.event ? e.keyCode : e.which;

    if (userChars.indexOf(key) > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Validate Alpha Numeric values and space
function ValidateAlphaNumeric(fld)
{
    var result = false;
    var RegExPattern = /^[A-Za-z0-9 ]+$/;
    
    if (fld.value.match(RegExPattern) && fld.value != '') 
    {
        result = true;   
    } 
    else 
    {
        result = false;
    } 
    
    return result;
}


function ValidateAlphabets(fld)
{
    fld.value = AllTrim(fld.value);
    
	var result = fld.value.match(/^[A-Za-z ]+$/);

   	return result;
}


//validate that the user has only entered digits
function ValidateDigits(fld)
{
    var result = false;
    
    fld.value = AllTrim(fld.value);
    
    if (fld.value != "")
    {
        result = fld.value.match(/^\d+$/);
    }
    else
    {
        result = true;
    }

   	return result;
}


//validate that the user has only entered posative numbers
function ValidatePosativeNumbers(fld)
{
    var result = false;
    
    fld.value = AllTrim(fld.value);
    
    if (fld.value != "")
    {
        result = fld.value.match(/^\d+$/);
    }
    else
    {
        result = true;
    }

   	return result;

}

//validate that the user has entered an integer +ve or -ve
function ValidateNumbers(fld)
{
    var result = false;
    
    fld.value = AllTrim(fld.value);
    
    if (fld.value != "")
    {
        result = fld.value.match(/^[-+]?\d+$/);
    }
    else
    {
        result = true;
    }

    return result;
}

//validate that the user has entered a money value
//also round off the value
function ValidateMoney(fld)
{
    var valNumber;
    var sign;
    var paise;
    
    fld.value = AllTrim(fld.value);
    var result = fld.value.match(/^[-+]?[0-9]*(\.[0-9]*)?$/);
    
    if (result)
    {
        //uncomment if we wish to remove $ and comma
        //valNumber = valNumber.toString().replace(/$|\,/g,'');
        valNumber = fld.value;
            
        if (isNaN(valNumber))
        {
            valNumber = "0";
        }
        
        sign = (valNumber == (valNumber = Math.abs(valNumber)));
        valNumber = Math.floor(valNumber*100+0.50000000001);
        paise = valNumber%100;
        valNumber = Math.floor(valNumber/100).toString();
        if (paise < 10)
        {
            paise = "0" + paise;
        }
        fld.value = (((sign)?'':'-') + valNumber + '.' + paise);
    }    

    return result;
}

//validate that the user has entered a money value
//also round off the value
function ValidateSingle(fld)
{
    var valNumber;
    var sign;
    var paise;
    
    fld.value = AllTrim(fld.value);
    var result = fld.value.match(/^[-+]?[0-9]*(\.[0-9]*)?$/);
    
    return result;
}

//validate that the user has entered a date in dd/MM/yyyy format
function ValidateDate(fld)
{
    var result = false;
    //Britsih var RegExPattern = /^((((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 RegExPattern = /^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/;
    var RegExPattern = /^([1-9]|0[1-9]|1[012])[\.\-\/]([1-9]|0[1-9]|[12][0-9]|3[01])[\.\-\/][0-9]{4}$/;

    if(fld.value != '__/__/____')
    {
        if (fld.value.match(RegExPattern) && fld.value != '') 
        {
            result = true;   
        } 
        else 
        {
            result = false;
        } 
    }
    else
         result = true;  
         
    return result; 
}

//validate that the user has entered a phone number
function ValidatePhone(fld)
{
    var result = false;
    fld.value = AllTrim(fld.value);
    
    if (fld.value != "" && fld.value != "(___)-___-____")
    {
        result = fld.value.match(/^[\d\(\)\-]+$/);
    }
    else
    {
        result = true;
    }

    return result;
}

//validate that the user has entered only the user defined characters
function ValidateUserDefined(fld, userChars)
{
    var result = true;
    var fldValue = AllTrim(fld.value);
    
    //loop thrun the field and test that the characters 
    //are present in the user defined characters
    for (index = 0; index <= fldValue.length && result; index++)
    {
        var c = fldValue.substr(index,1)
        if (userChars.indexOf(c) == -1)     
        {
            result = false;
        }
    }

    return result;
}

//validate salary type
//salary type is equal to money type. only difference is the $ sign before the value.
function ValidateSalary(fld)
{
    var valNumber;
    var sign;
    var paise;
    
    fld.value = AllTrim(fld.value);
    fld.value = fld.value.replace('$','');
    var result = fld.value.match(/^[-+]?[0-9]*(\.[0-9]*)?$/);
    
    if (result)
    {
        //uncomment if we wish to remove $ and comma
        //valNumber = valNumber.toString().replace(/$|\,/g,'');
        valNumber = fld.value;
            
        if (isNaN(valNumber))
        {
            valNumber = "0";
        }
        
        sign = (valNumber == (valNumber = Math.abs(valNumber)));
        valNumber = Math.floor(valNumber*100+0.50000000001);
        paise = valNumber%100;
        valNumber = Math.floor(valNumber/100).toString();
        if (paise < 10)
        {
            paise = "0" + paise;
        }
        fld.value = (((sign)?'':'-') + valNumber + '.' + paise);
    }    

    return result;
}

//All Trim
function AllTrim(strValue) 
{
    return strValue.replace(/^\s+|\s+$/g, '');
}

//Left Trim
function LTrim(strValue)
{
    return strValue.replace(/^\s+/, '');
}

//Right Trim
function RTrim(strValue) 
{
    return strValue.replace(/\s+$/, '');
}


function validateLength(fld,limit)
{
	var len = fld.value.length;
	if (len > limit -2) {
	    fld.value = fld.value.substring(0, limit-1);
                }
}

