/*****************************************************************
* Application	: NIC - GeP
* Time			: 13-08-07
* Author		: Venkataramanan K
*****************************************************************
* Desc			It contains the all general functions required for
				the data validations in the UI.
******************************************************************/


/**
* @method	isEmpty
*
* It checks the given value is empty or not
* @param string value
*
*/
function isEmpty(sVal){
	if(sVal == "" || sVal.length == 0){
		return true;
	}	

	return false; // False if the value is not empty.
}


/**
* @method	isEmail
*
* It checks the given value possess the email address heuristics
* @param string value
*
*/
function isEmail(sVal){
	// if the value is empty
	if(isEmpty(sVal))
		return false;

	// Pattern for email heuristics
	var sPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
//	var sPattern = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;

	// Tests the value against the pattern
	if(sPattern.test(sVal)){
		return true; // valid email
	}

	return false; // in-valid email.
}


/**
* @method	isInRange
*
* It checks the given value length is within the specified range.
* @param string value
* @param string length
*
*/
function isValidPassword(sVal){
	// if the value is empty
	if(isEmpty(sVal))
		return false;

	var sPattern = /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!,@,#,$,^,*,_,~])/;

	// Checks the value length is within the specified range.
	if(!sPattern.test(sVal)){
		return false;
	}

	return true; 
}

/**
* @method	isNumeric
*
* It checks the fields is numeric
* @param string value
*
*/
function isNumeric(sVal){
	var sPattern = /^\d+$/;

	if(sPattern.test(sVal)){
		return true;
	}
	
	return false;
}



/**
* @method	compare
*
* It compares the two string values and returns true/false
* @param string value 1
* @param string value 2
*
*/
function compare(sStr1, sStr2){
	// Matches the second value in first 
	if(sStr2.search(sStr1) != -1){
		return true;
	}

	return false;
}

/**
* Checks the input value is a valid PAN number or not
*
* @param string val
*/
function isPAN(sVal){

	// RegEx pattern
	var sPattern = /^\w{5}\d{4}\w{1}$/;

	// Check across the reqex pattern
	if(sPattern.test(sVal)){
		return true;
	}

	return false;
}

	/**
	* Validate the date field. 
	* It validates based on the first date should be less than the second date.
	*
	* @param string Date 1
	* @param string Date 2
	*/
	function compareDate(strDate1, strDate2) {
				
		// Instantiating object of dates with mm/dd/yyyy format.		
		var obDate1 = new Date(formatDate(strDate1));
		var obDate2 = new Date(formatDate(strDate2));
		
		// Date.parse() returns the date in milliseconds
		if(obDate1.getTime() > obDate2.getTime()) {
		
			return false;
		}
		else{
			return true;
		}
	}


	/**
	* It formats the date and returns it in MM/dd/yyyy format
	*/
	function formatDate(strDate){
		var arrPart = strDate.split("/");
		var strReturn;

		if(arrPart){
			strReturn = arrPart[1]+"/"+arrPart[0]+"/"+arrPart[2];
		}

		return strReturn;
	}

/**
* It opens the pop up window based on the given anchor object
*/

	function openPopUp(obElm, sFeature) {
	
		var sLink;
		
		if(typeof(obElm) == 'string') {		
			sLink = obElm;
		}
		else{
			sLink = obElm.href;
			
			var obWin = window.open(sLink, "Popup", sFeature);
			
			obWin.focus();
			
			if(!obWin.opener) obWin.opener = _self;
			
			return false;
		}
	
	}
	
	/**
* It opens the pop up window based on the given anchor object
*/

	function openPopUpWithSubmit(obElm, sFeature, formName, componentName){
		var sLink;
		var filePath = parent.window.document.getElementById(componentName).value;

		if(filePath == '' || filePath == null){
			alert('Please Select File To Sign');
			return false;
		}
		
		var pattern = new RegExp();
		pattern.compile("\&");
		if(pattern.test(filePath)) {
			alert("Special Characters '\&' Not Allowed In File Path");
			return false;
		}
		
		if(typeof(obElm) == 'string'){
			sLink = obElm;
		}
		else{
			sLink = obElm.href;
			sLink = sLink+"&filePath="+filePath;
			var obWin = window.open(sLink, "Popup", sFeature);
			
			obWin.focus();
			
			if(!obWin.opener) obWin.opener = _self;
			
			return false;
		}
	
	}
	
	//To Enable File Read Object form Local Directory from remote
	function filePathRead(Object,componentName){
		 if (Object.files) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege( 'UniversalFileRead' )
			}
			catch (err) {
				//need to set signed.applets.codebase_principal_support to true
				alert(err);
			}
		}
		
		parent.window.document.getElementById(componentName).value = Object.value;
	}
	
	String.prototype.trim = function() {
    	return this.replace(/^\s+|\s+$/g,"");
    }
    
    
	    
	/**
	* It encrypts the given value using hash method.
	* 
	* @param string password
	* @param string salt
	* @return string encrypted password
	*/
	function encryptPwd(strPwd, strSalt){
		
		// Generate hash of the given password
		if(isEmpty(strPwd) || isEmpty(strSalt))
			return null;
		
		var strEncPwd;
			
		//var strPwdHash = hex_md5(strPwd);
		
		//var strSalt = strSalt.substring(1, 5);
		
		// Merge the salt with hash
		//var strMerged = strSalt +""+ strPwdHash;
		
		// Hash again with the salt
		//strEncPwd = hex_md5(strMerged);
		strEncPwd = hex_hmac_md5(strPwd,strSalt)
	
		return strEncPwd;
	}
    
    
    /**
    * Text area text counter method.
    */
	function textCounter(obField,cntfield,maxLimit) {
		if (obField.value.length > maxLimit){ // if too long...trim it!
			obField.value = obField.value.substring(0, maxLimit);
			// otherwise, update 'characters left' counter
		}
		else{
			cntfield.value = maxLimit - obField.value.length;
		}
	}
	
	//Getting Current Date 
	//Written By Jana
	
	function getCurrDate() {
	
		var today = new Date();
		var dd = today.getDate();
		var mm = today.getMonth()+1;//January is 0!
		var yyyy = today.getFullYear();
			
		if(dd<10) {
			dd='0'+dd;
		}
			
		if(mm<10) {
			mm='0'+mm;
		}
			
		var today = dd+'/'+mm+'/'+yyyy;
		
		return today;
	}
	
	
	/**
	* It counts the text area characters and restricts when the maxlimit is reached.
	*/
	function textCounter(field,cntfield,maxlimit) {
		if (field.value.length > maxlimit){
			 // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
		}
		// otherwise, update 'characters left' counter
		else{	
			cntfield.innerText = cntfield.textContent = field.value.length;
		}
	}
		
//Author Prem Prakash
function isNumericData(e,obj,allowdecimal,allowCheck)
{
    var k = e.which ? e.which : e.keyCode
    if(parseInt(k)!=46 && parseInt(k)!=8 && parseInt(k)<48 || parseInt(k)>57)
    {
        return false;
    }
    else
    {
        if(allowCheck=='Y')
        {
            vl=obj.value+String.fromCharCode(k);
            if(parseInt(vl)>100)
            {
                return false;  
            }
	    }
        if(parseInt(k)==46 && allowdecimal=='N')
        {
            return false;
        }
        else
        {  	    	
            if(parseInt(k)==46)
            {
                if(obj.value.split('.').length>1)
                {
                    return false;
                }
   	        }
	    }
    }
    return true;
}	

// Vikas Chaturvedi

 function getKeyCode(e) {
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}

function keyRestrict(e, val) {

var key = '', keychar = '';
key = getKeyCode(e);
if (key == null) return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

var validchars;
validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/";
validchars = validchars.toLowerCase();

if (validchars.indexOf(keychar) != -1)
return true;

if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27 || key == 32)
return true;
return false;
}

function keyRestrictAddress(e, val) {

var key = '', keychar = '';
key = getKeyCode(e);
if (key == null) return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

var validchars;
validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-@&,.+*$";
validchars = validchars.toLowerCase();

if (validchars.indexOf(keychar) != -1)
return true;

if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27 || key == 32)
return true;
return false;
}

function GetSelected(lstObj,defValue)
{
    var ret=false;
    for(i=0;i<lstObj.options.length;i++)
    {
        if(lstObj.options[i].selected == true)
        {
            if(lstObj.options[i].value==defValue)
            {
                ret=false
            }
            else
            {
                ret=true;
                return ret;
            }
        }
    }
    return ret;
}
