﻿//    Function Name : LTrim 
//    Argument      : String value
//    Description   : the LTrim function removes all spaces from the left-hand side of a string.

     
 
LTrim = function ( p_strValue )
{
    var objRegExp = /^(\s*)(\b[\w\W]*)$/;

    //checking for valid string 

    if(objRegExp.test(p_strValue))
    {
        //remove leading a whitespace characters 
        p_strValue = p_strValue.replace(objRegExp, '$2');

    }

    return p_strValue;
}
 
 

//    Function Name : RTrim 
//    Argument      : String value
//    Description   : RTrim removes blanks from the end of a character string expression.
     
 
 RTrim = function ( p_strValue ) 
{
    var objRegExp = /^([\w\W]*)(\b\s*)$/;

    //checking for valid string 

    if(objRegExp.test(p_strValue))
    {
        //remove trailing a whitespace characters 
        p_strValue = p_strValue.replace(objRegExp, '$1');
    }    

    return p_strValue;
}
 
 



String.prototype.trim = Trim;

function Trim(str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


//Function Name : Trim 
//    Argument      : String value
//    Description   : Trim function returns a string without leading and trailing spaces.
     
 
 Trim = function ( p_strValue )
{
    var objRegExp = /^(\s*)$/;
    //check for a blank string 
    if(objRegExp.test(p_strValue))
    {
        p_strValue = p_strValue.replace(objRegExp, '');
        if( p_strValue.length == 0) 
        {
            return p_strValue;
        }
    }

    //check for leading & trailing spaces 

    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;

    if(objRegExp.test(p_strValue))
    {
       //remove leading and trailing whitespace characters 
        p_strValue = p_strValue.replace(objRegExp, '$2');
    }

    return p_strValue;
}
 
 
 
//    Function Name : removeCommas 
//    Argument      : String value
//    Description   : removes comma from a given value, this is usefull when we need retrive 
//                    value from a comma formatted value like currency.., for calc
    
 
removeCommas = function ( p_strValue )
{
    //search for commas globally 
    var objRegExp = /,/g; 
     //replace all matches with empty strings 
    return p_strValue.replace(objRegExp,'');
}
 
//  Function Name : validateEmail
//    Argument      : Email Id
//    Description   : Simple email validation function, using RegExp
     
 
function validateEmail(p_Value)
{  
    if(p_Value.length>0)
    {
        var objExpr=/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
        if(!objExpr.test(p_Value))
        {
            return false;
        }
        else 
        {
             return true;
        }
    }
}
 
// 
//  Function Name : Round 
//    Argument      : 
//    Description   : This function will return a rounded figure of the given value
//                     
 
 Round = function (value, decimalplaces)
{
    return value.toFixed(decimalplaces);
}
 
 
//Function Name : isNumeric 
//    Argument      : 
//    Description   : 
//                     
 
 isNumeric = function  ( p_strValue )
{
    var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
    //check for numeric characters 
    return objRegExp.test(p_strValue);
}
 
 
//   Function Name : isInteger 
//    Argument      : 
//    Description   : 
 
 isInteger = function ( p_strValue ) 
{
    var objRegExp = /(^-?\d\d*$)/;

    //check for integer characters 

    return objRegExp.test(p_strValue);
}
function validateEmailall(comp)
{
    if(comp.value.length>0)
    {
        var str=comp.value;
        var objExpr=/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
        if(!objExpr.test(str))
        {        
            alert("Please enter a valid email Id. \nFor eg. somebody@somesite.com / somebody@somesite.co.in / somebody@somesite.info");
            comp.value="";
            //alert(comp.id);
            comp.focus();
            return false;
        }       
        else
        {
             comp.value=str.toLowerCase();
             return true;
        }
    }
    else
    {
    return true;
    }
    
}





function onlyInteger(evt)
{
    var e=evt?evt:window.event;
    
    if(e.type=="paste")
    {
        if(e.target)
        {
            return false;
        }
        else
        {
            if(isNaN(window.clipboardData.getData("Text")))
            {
                return false;
            }
        }
    }
    else
    {
        if(!window.event)
        {
           
           if((e.charCode<48 || e.charCode>57) && e.charCode!=0)
            {
                return false;
            } 
        }
        else
        {
            //var keyValue = String.fromCharCode(e.keyCode);
            var keyValue = e.keyCode;
           // alert(keyValue);
            if (keyValue != 8 && !(keyValue >= 48 && keyValue <= 57))
            {
                return false;
            }
        }
    }
}


function checkMaxLength(control,maxLength)
{
    if ( control.value != undefined)
    {
       if( control.value.length > maxLength )
       {
         control.value = control.value.substring(0,maxLength);
       }
    }
}


function toYYYYMMDD(date)
{
    var outDate = "";
    if ( date != undefined && date != "")
    {
       if( date.length == 10 )
       {
           outDate =  date.substring(6,10) + date.substring(0,2) + date.substring(3,5);
           return outDate;
       }
    }
}


function toSQLFormat(date)
{
    var outDate = "";
    if ( date != undefined && date != "")
    {
       if( date.length == 10 )
       {
           outDate =  date.substring(6,10) + '/' + date.substring(0,2) + '/' + date.substring(3,5);
       }
    }
     return outDate;
}



function popUp(URL) 
{
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 112,top = 84');");
}






function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

/*-------------------------------------------------------------------------------------------
function Name   : onKeyPressBlockNumbers
Description     : allows only non numeric values to be entered.
Argument        : take event on the component as argument 
                    eg . <input type="text" onkeypress="return onKeyPressBlockNumbers(event);" />
Return value    : returns false when numeric values are entered.
fired on Event  : onKeyPress
---------------------------------------------------------------------------------------------*/
function onKeyPressBlockNumbers(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return !reg.test(keychar);
}

function getText(el)
    {
        if ('string' == typeof el.textContent) return el.textContent;
        if ('string' == typeof el.innerText) return el.innerText;
        return el.innerHTML.replace(/<[^>]*>/g,'');
    }
    
function Highlight(strScreenName)
{

    //alert(window.parent.parent.frames[0].name);
    var slmTemplate = window.parent.frames[0].document.getElementById("slmTemplate");
        
    var spans = slmTemplate.getElementsByTagName('span');
    for (index=0;index<spans.length; index++ )
    {    
            if(spans[index].id == strScreenName)
           window.parent.frames[0].ChildNode_onClick(spans[index].parentNode.parentNode );
    }
}


