/*******************************************************************************
FILE: RegExpValidate.js

DESCRIPTION: This file contains a library of validation functions
  using javascript regular expressions.  Library also contains functions that re-
  format fields for display or for storage.
  
 
  VALIDATION FUNCTIONS:
  
  validateEmail - checks format of email address
    validateUSPhone - checks format of US phone number
    validateNumeric - checks for valid numeric value 
  validateInteger - checks for valid integer value
    validateNotEmpty - checks for blank form field
  validateUSZip - checks for valid US zip code 
  validateUSDate - checks for valid date in US format
  validateValue - checks a string against supplied pattern
  
  FORMAT FUNCTIONS:
  
  rightTrim - removes trailing spaces from a string
  leftTrim - removes leading spaces from a string
  trimAll - removes leading and trailing spaces from a string
  removeCurrency - removes currency formatting characters (), $ 
  addCurrency - inserts currency formatting characters
  removeCommas - removes comma separators from a number
  addCommas - adds comma separators to a number
  removeCharacters - removes characters from a string that match passed pattern
  
  
AUTHOR: Karen Gayda

DATE: 03/24/2000
*******************************************************************************/

function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid email pattern. 
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp  = /(^[a-z0-9]([a-z0-9_\.]*)@([a-z])([a-z\.]*)([.][a-z]{3})$)|(^[a-z0-9]([a-z0-9_\.]*)@([a-z_])([a-z\.]*)([.][a-z]{2})$)/i;

 
  //check for valid email
  return objRegExp.test(strValue);
}

function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern. 
  Ex. (999) 999-9999 or (999)999-9999
  
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}



function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 
  //check for valid US Zipcode
  return objRegExp.test(strValue);
}
function validatePhone(strValue){
var objRegExp  = /(^\d{2}-\d*$)|(^\d*$)/;
 
  //check for valid US Zipcode
  return objRegExp.test(strValue);

}

function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1]); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February
    var intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[0]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

function validateValue( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Validates that a string a matches
  a valid regular expression value.
    
PARAMETERS:
   strValue - String to be tested for validity
   strMatchPattern - String containing a valid
      regular expression match pattern.
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp = new RegExp( strMatchPattern);
 
 //check if string matches pattern
 return objRegExp.test(strValue);
}


function rightTrim( strValue ) {
/************************************************
DESCRIPTION: Trims trailing whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed.  
      
RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed
   
RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from 
  source string.
  
PARAMETERS: 
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';
 
  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }
  
  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS: 
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid 
  numeric value in the rounded to 2 decimal 
  places.  If not, returns original value.
*************************************************/
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;
   
    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else
      return strValue;
}

function removeCommas( strValue ) {
/************************************************
DESCRIPTION: Removes commas from source string.

PARAMETERS: 
  strValue - Source string from which commas will 
    be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /,/g; //search for commas globally
 
  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS: 
  strValue - source string containing commas.
  
RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is 
  returned.
  
REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})'); 

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match, 
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}

function removeCharacters( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Removes characters from a source string
  based upon matches of the supplied pattern.

PARAMETERS: 
  strValue - source string containing number.
  
RETURNS: String modified with characters
  matching search pattern removed
  
USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd', 
                                '\s*')
*************************************************/
 var objRegExp =  new RegExp( strMatchPattern, 'gi' );
 
 //replace passed pattern matches with blanks
  return strValue.replace(objRegExp,'');
} 

function CheckFor18Yrs(strFormName,strfldDate,strfldDummyDate,strfldDOB,strDesc)
{
	var strjoindate;
	var dtjoindate;
	var strdob;
	var dtdob;

	var strFormDoc = "document." + strFormName + ".";

	strjoindate = eval(strFormDoc + strfldDate + ".value");
	strdob = eval(strFormDoc + strfldDOB + ".value");

	if (strjoindate != '' && strdob != '')
	{
		dtjoindate = new Date(strjoindate);
		dtdob =  new Date(strdob);
		var intage = dtjoindate.getFullYear() - dtdob.getFullYear();

		if ( intage < 18 ) 
		{
			alert( strDesc + " [ " + strjoindate + " ] should be 18 yrs after the DOB [ " + strdob + " ] ");
			eval(strFormDoc + strfldDate + ".value = ''");
			eval(strFormDoc + strfldDummyDate + ".value = ''");
		}
	}
}


function validatemaxlen(frm,strfld,intmaxlen)
{
	var intLen;
	var strFldValue;
	
	strFldValue = eval("frm." + strfld + ".value");
	intLen = strFldValue.length;
	if ( intLen > intmaxlen )
	{
		alert("Please note that the value is exceeding the maximum length of " + intmaxlen + " characters");
		eval("frm." + strfld + ".value='" + strFldValue.substring(0,intmaxlen) + "'");
	}
}

function validateforsysdate(strFormName,strfldDate,strfldDummyDate,strDateDesc)
{


    var strDate = document[getNetuiTagName(strFormName,this)][getNetuiTagName(strfldDate,this)].value;

	if ( strDate != '')
	{
    	var date = strDate.substring(0,2);
        var month = strDate.substring(3,6);
        var year = strDate.substring(7,11);
        var parsDate = date + ' ' + month + ' ' + year;
		dtDate = Date.parse(parsDate);
		dtSysDate = new Date();
		if ( dtDate > dtSysDate ) 
		{   alert( strDateDesc + " cannot be greater than the System date");		
			//eval(strFormDoc + strfldDate + ".value = ''");
		}
	}
}

function validateforNetuisysdate(strFormName,strfldDate,strfldDummyDate,strDateDesc)
{
    var strDate = document[getNetuiTagName(strFormName,this)][getNetuiTagName(strfldDate,this)].value;
    
	if ( strDate != '')
	{
    	var date = strDate.substring(0,2);
        var month = strDate.substring(3,6);
        var year = strDate.substring(7,11);
        var parsDate = date + ' ' + month + ' ' + year;
		dtDate = Date.parse(parsDate);
		dtSysDate = new Date();
		if ( dtDate > dtSysDate ) 
		{   alert( strDateDesc + " cannot be greater than the System date");
        document[getNetuiTagName(strFormName,this)][getNetuiTagName(strfldDate,this)].value = " ";		
        //document[getNetuiTagName(strFormName,this)][getNetuiTagName(strfldDate,this)].focus = true;
			//eval(strFormDoc + strfldDate + ".value = ''");
            return false;
		}
        //document[getNetuiTagName(strFormName,this)].submit();
	}
    return true;
}
function validateNetuiFROMTO(frmName,strFrom,strTo,strFromDesc,strToDesc)
{
    
	var dtFromDate;
	var dtToDate;
	var strFromVal;
	var strToVal;
	var strDocForm;
    strFromVal = document[getNetuiTagName(frmName,this)][getNetuiTagName(strFrom,this)].value;

    strToVal = document[getNetuiTagName(frmName,this)][getNetuiTagName(strTo,this)].value;

	if (  strFromVal != '' && strToVal != '' )
	{

        var frmDate = strFromVal.substring(0,2);
        var frmMonth = strFromVal.substring(3,6);
        var frmYear = strFromVal.substring(7,11);
        var prsFromVal = frmDate + ' ' + frmMonth + ' ' + frmYear;
		dtFromDate = Date.parse(prsFromVal);

        
        var toDate = strToVal.substring(0,2);
        var toMonth = strToVal.substring(3,6);
        var toYear = strToVal.substring(7,11);
        var prsToVal = toDate + ' ' + toMonth + ' ' + toYear;	        
		dtToDate = Date.parse(prsToVal);
		if ( dtFromDate > dtToDate )
		{
			alert(strFromDesc + " date [ " + strFromVal + " ] cannot be \n greater than " + strToDesc + " date [ " + strToVal + " ] ");
			//eval(strDocForm + strFrom + ".value = ''");
			return false; 
		}
        //document[getNetuiTagName(frmName,this)].submit();
        
	}
    return true;
}
function NotLessThanSysdate(strFormName,strfldDate,strfldDummyDate,strfldrDate,strfldrDummyDate,strDateDesc,strid)
{

	var dtSysDate;
	var dtDate;
      var dtrdate

	var strFormDoc = "document." + strFormName + ".";
	var strDate = eval(strFormDoc + strfldDate + ".value");

 if (strid == '1')
   {
    
     var strrDate = eval(strFormDoc + strfldrDate + ".value");
}
else
     var strrDate = strfldrDate;

 
   
	

	if ( strDate != '')
	{
		dtDate = Date.parse(strDate);
            dtrdate = Date.parse(strrDate);
              
		
		if ( dtDate <  dtrdate  )
               

		{
			alert( strDateDesc + " should be greater than release date ");
			
			eval(strFormDoc + strfldDate + ".value = ''");
		}
	}
}


function validateNumber(fld)
{
	var blnFlag;
	if ( trimAll(fld.value) != '' )
	{
		blnFlag = IsNumber(fld.value);
		if ( blnFlag == false )
		{
			alert("Invalid characters or a Negative value entered ! ");
			fld.value = "";
		}
	}
}

function IsNumber(strValue)
{
	var objRegExp = /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
	return objRegExp.test(strValue);
}

function validEmail(frm,strfld)
{
	var strValue = eval("frm." + strfld + ".value");

	if ( trimAll(strValue) != '' )
	{
		if ( validateEmail(strValue) == false )
		{
			alert("Invalid email entered !");
			eval("frm." + strfld + ".focus()");
		}
	}
}


function validateInteger(fld)
{
	var blnFlag;
	blnFlag = IsInteger(fld.value);
	if ( blnFlag == false )
	{
		alert("Invalid characters or a Negative value entered ! ");
		fld.value = "";
	}
}

function IsInteger(strValue)
{
	var objRegExp = /(^\d\d*$)/;
	return objRegExp.test(strValue);
}

function validatePercent(fld)
{
	var value; 
	value = parseInt(fld.value);
	if ( value > 100 )
	{
		fld.value = 100;
		alert("Percentage value cannot exceed a value of 100");
	}
}

function validateMonths(fld)
{
	var intYears;
	var fltMonths;
	var fltValue;

	if ( fld.value != '' )
	{
		fltValue = parseFloat(fld.value);
		intYears = parseInt(fld.value);
		fltMonths = ( fltValue - intYears );

		if ( ( 1 - fltMonths ) < 0.89 )
		{
			alert("Invalid Months entered ! ");
			fld.value = intYears;
		}
	}
}


function fallsbetween(xval,yval,fld)
{
	var intxval,intyval;
	var intval;
	intxval = parseInt(xval);
	intyval = parseInt(yval);
	intval = parseInt(fld.value);

	if ( intval < intxval && intyval == 0 )
	{
		alert( intval + " should be greater than or equal to " + intxval );
		fld.value = intxval;
	}
	else if ( intval > intyval && intxval == 0 )
     {
		alert( intval + " should be less than or equal to " + intyval );
		fld.value = intyval;
     }
	else if ( ( intval < intxval || intval > intyval ) && ( intxval != -1 && intyval != 0 )  ) 
     {
		alert( intval + " should fall between " + intxval + " and " + intyval );
		fld.value = intxval;
     }
}

function checkifblank(fld,defvalue)
{
	if (fld.value == '')
	{
		fld.value = defvalue;
	}
}

function validateFROMTO(frmName,strFrom,strTo,strFromDesc,strToDesc)
{

	var dtFromDate;
	var dtToDate;
	var strFromVal;
	var strToVal;
	var strDocForm;

	strDocForm = "document." + frmName + ".";

	strFromVal = eval(strDocForm + strFrom + ".value");
	strToVal = eval(strDocForm + strTo + ".value");

	if (  strFromVal != '' && strToVal != '' )
	{
		dtFromDate = Date.parse(strFromVal);
		dtToDate = Date.parse(strToVal);
		if ( dtFromDate > dtToDate )
		{
			alert(strFromDesc + " date [ " + strFromVal + " ] cannot be \n greater than " + strToDesc + " date [ " + strToVal + " ] ");
			eval(strDocForm + strFrom + ".value = ''");
			 
		}
	}
}

function validateGridFROMTO(frmName,strFrom,strTo,strDummyFrom,strFromDesc,strToDesc)
{

	var dtFromDate;
	var dtToDate;
	var strFromVal;
	var strToVal;
	var strDocForm;

	strDocForm = "document." + frmName + ".";

	strFromVal = eval(strDocForm + strFrom + ".value");
	strToVal = eval(strDocForm + strTo + ".value");

	if (  strFromVal != '' && strToVal != '' )
	{
		dtFromDate = Date.parse(strFromVal);
		dtToDate = Date.parse(strToVal);
		if ( dtFromDate > dtToDate )
		{
			alert(strFromDesc + " date [ " + strFromVal + " ] cannot be \n greater than " + strToDesc + " date [ " + strToVal + " ] ");
			eval(strDocForm + strFrom + ".value = ''");
			eval(strDocForm + strDummyFrom + ".value = ''");
		}
	}
} 

function DateFallsBetween(frmName,strFrom,strTo,strFld,strDummyFld,strFallsDesc,strFldDesc)
{
	var strDocForm;
	var strFromVal;
	var strToVal;
	var strFldVal;
	var dtFromDate,dtToDate,dtFldDate;
	var blnFlag;

	strDocForm = "document." + frmName + ".";

	strFromVal = eval(strDocForm + strFrom + ".value");
	strToVal = eval(strDocForm + strTo + ".value");
	strFldVal = eval(strDocForm + strFld + ".value");

	if ( strFromVal != '' && strToVal != '' && strFldVal != '' )
	{
		dtFromDate = Date.parse(strFromVal);
		dtToDate = Date.parse(strToVal);
		dtFldDate = Date.parse(strFldVal);
		
		if ( dtFldDate < dtFromDate || dtFldDate > dtToDate )
		{
			alert(strFldDesc + " [ " + strFldVal + " ] should fall between \n " + strFallsDesc + " Start date [ " + strFromVal + " ] and End date [ " + strToVal + " ]");
			blnFlag = true;
		}
	}
	else if( strFromVal != '' && strToVal == '' && strFldVal != '' )
	{
		dtFromDate = Date.parse(strFromVal);
		dtFldDate = Date.parse(strFldVal);
		
		if ( dtFldDate < dtFromDate )
		{
			alert(strFldDesc + " [ " + strFldVal + " ] should be greater than \n " + strFallsDesc + " Start date [ " + strFromVal + " ]" );
			blnFlag = true;
		}
	}
	else if( strFromVal == '' && strToVal != '' && strFldVal != '' )
	{
		dtToDate = Date.parse(strToVal);
		dtFldDate = Date.parse(strFldVal);
		
		if ( dtFldDate > dtToDate )
		{
			alert(strFldDesc + " [ " + strFldVal + " ] should be less than \n " + strFallsDesc + " End date [ " + strToVal + " ]");
			blnFlag = true;
		}
	}

	if ( blnFlag == true )
	{
		eval(strDocForm + strFld + ".value = ''");
		eval(strDocForm + strDummyFld + ".value = ''");
	}

}


function newpagesubmitform(strFormName,strMode, strPageNo) 
{	
	if (navigator.appName != 'Netscape')
	{
		window.event.returnValue = false;
	}
	eval("document." + strFormName + ".mode.value = " + strMode);
	eval("document." + strFormName + ".pageno.value = " +  strPageNo);
	eval("document." + strFormName + ".submit()");
}


function validateXYAmt(frm,fldx,fldy,strXDesc,strYDesc)
{
	var strFldX = 'frm.' + fldx + '.value';
	var strFldY = 'frm.' + fldy + '.value';

	
	if ( eval(strFldX) != '' && eval(strFldY) != '' )
	{
		var fltX = parseFloat(eval(strFldX));
		var fltY = parseFloat(eval(strFldY));
		if ( fltX > fltY )
		{
			alert(strXDesc + ' of [ Rs. ' + eval(strFldX) + ']' + ' cannot exceed ' + strYDesc + ' of [ Rs. ' + eval(strFldY) + ']');
			eval(strFldX + " = '' ");
		}
	}
}


function ControlExists(ctlName)
{
	var blnExists;
	blnExists = false;
	
	for( var intIndex=0; intIndex < document.forms[0].elements.length; intIndex++ )
	{
	
		if ( ctlName == trimAll(document.forms[0].elements[intIndex].name) )
		{
			blnExists = true;
			break;
		}
		
	}
	
	return blnExists;
}



function NewControlExists(strfrm,ctlName)
{
	var blnExists;
	var intTotallength;
	var strFormDoc = "document." + strfrm + ".";
	blnExists = false;
	
	intTotallength = eval(strFormDoc + "elements.length");
	
	for( var intIndex=0; intIndex < intTotallength; intIndex++ )
	{
	
		if ( ctlName == trimAll(eval(strFormDoc + "elements[" + intIndex + "].name" )) )
		{
			blnExists = true;
			break;
		}
		
	}
	
	return blnExists;
}

function submitme(strform,straction,strcid)
{
	eval("document." + strform + ".hdrowid.value = '" +  strcid + "'");
	eval("document." + strform + ".hdaction.value = '" +  straction + "'");
	eval("document." + strform + ".submit()");
}



function printme(strfrm,straction,strrowid,strbentype,struserlock)
{

	var strlock = '1';	

	if ( eval("document." + strfrm + ".hdunlock.value") == 1 && struserlock == '1')
	{
		if ( confirm("Are you sure of unlocking this transaction ? ") == true )
			strlock = '0';
	}
	eval("document." + strfrm + ".target = '_new'");
	eval("document." + strfrm + ".hdaction.value = '" + straction + "'");
	eval("document." + strfrm + ".hdrowid.value = '" + strrowid + "'");
	eval("document." + strfrm + ".hdbentype.value = '" + strbentype + "'");
	eval("document." + strfrm + ".hduserlock.value = '" + strlock + "'");
	eval("document." + strfrm + ".submit()");
}


function suycDateDiff( start, end, interval, rounding ) {

    var iOut = 0;
    
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    if ( interval.charAt == 'undefined' ) {
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA+86400000 ;
    
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}

function Calculatedatediff(frm,fromdate,todate,result,format) {
var iout;
var ierr = 1 ;
if(document[frm].elements[fromdate].value != '' && document[frm].elements[todate].value != '' )
	{
	if(document[frm].elements[fromdate].value !='')
		{
    var ierr = 1 ;
    
   var roundDays = 'true' ;
   
    if(document[frm].elements[fromdate].value != '') {
        if(!isNaN(Date.parse( document[frm].elements[fromdate].value ))) {
            var s = new Date(Date.parse(document[frm].elements[fromdate].value)) ;
            ierr = 0 ;
        }
    }
    
    if(document[frm].elements[todate].value != '') 
    	{
        if(!isNaN(Date.parse( document[frm].elements[todate].value )))
         {
            var e = new Date(Date.parse(document[frm].elements[todate].value)) ;
            
            var temp = suycDateDiff( s, e, 'd', roundDays ) ;
        }else{
            ierr = 1;
        }
    }else{
        ierr = 1;
    }
    
    if ( temp != null ) 

	var yano=temp + 1;
	var mono=temp + 1;

	yano=Math.floor(Math.abs(yano / 365));
	var mono=temp.toString();
	
	mono=mono - (yano * 365);
	var Dano=mono;
	mono=Math.floor(Math.abs(mono / 30));
	Dano=Dano - (mono * 30);

	var adj;
	var FinalData;
	FinalData='';
	
	if (yano > 0)
	{
		if(yano < 10)
		{
			FinalData= '0'+ yano + '.';
		}else
			FinalData= yano + '.';

		
	}else
		FinalData='0' + '.';

	if(Dano >15)
		mono=mono+1;
	else
		adj=1;
		
	if(mono > 0)
		{
			if(mono < 10)
			{
				FinalData=FinalData +'0'+ mono + '';
			}else
				FinalData=FinalData + mono + '';

		}else
			FinalData=FinalData + '00'+ '';
	if (format=='yy.mm')
	{
	document[frm].elements[result].value= FinalData;
	return iout;
	}
	if (format=='yy.mm.dd.')
	{
	if (Dano > 0)
		{
		FinalData= FinalData + Dano + '.';
		}else
			FinalData='0'+sysbolm;
	document[frm].elements[result].value= FinalData;
	return iout;

	
	}
	}	
	}

}


function trimAll( strValue ) 
	{
 		var objRegExp = /^(\s*$)/;

	    if(objRegExp.test(strValue)) {
           strValue = strValue.replace(objRegExp, '');
           if( strValue.length == 0)
    		      return strValue;
	      }
    
	   	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	  	if(objRegExp.test(strValue)) {
	      strValue = strValue.replace(objRegExp, '$2');
      		}
  return strValue;
	}


// Validate User Name 

    function validateUser(frmName, fieldName){  
        var userChars = '!@#$%^&*()+=-[]\\\';,./{}|\":<>? ';    
        var username = document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value;
        if(username != ''){
            for (i = 0; i < username.length; i++) {
                if (userChars.indexOf(username.charAt(i)) != -1) {
                    alert('User Name has special characters');
                    document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value='';                    
                    document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].focus();
                    return;
                }
           }
        }
    }
    
    
// Validate Password

function validatePassword(frmName, fieldName){
    var num_valid = '123456789';
    var alph_valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var iChars = '%&()+=-[]\\\';,./{}|\":<>?';    
    var password = document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value;
    if(password.length != '' && password.length <8 ){
        alert('Password should not be less than 8 characters long');
        document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value='';                    
        document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].focus();
        return;
     }else if(password.length != ''){    
        for ( i = 0; i < password.length; i++) {
            if (iChars.indexOf(password.charAt(i)) != -1) {
                alert('Password has special characters');
                document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value='';                    
                document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].focus();
                return;
            }
        }                
        var cnt = 0;
        for ( i=0; i<password.length; i++) {
            if (num_valid.indexOf(password.charAt(i)) < 0) {
                cnt ++;
            }
        }
        if(cnt == password.length){
            alert('Password contains only characters. Please enter an alphanumeric value like alpha123 ');
            document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value='';                    
            document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].focus();
            return;
        }                
        cnt = 0;
        for ( i=0; i<password.length; i++) {
            if (alph_valid.indexOf(password.charAt(i)) < 0) {
                cnt ++;
            }
        }
        if(cnt == password.length){
            alert('Password contains only numbers. Please enter an alphanumeric value like alpha123 ');
            document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].value='';                    
            document[getNetuiTagName(frmName,this)][getNetuiTagName(fieldName,this)].focus();
            return;
        }               
          
    }
}    


// Added by prashanth to check only string,number and -

    function validateStringInteger(strValue) {
		 
		var objStr =/^[a-zA-Z0-9]*$|^[a-zA-Z\-\a-zA-Z0-9]*$/;
		if(objStr.test(strValue)) {
				return true;
		} else {
			return false;
		}
	}
    
    // Checks only String and space
    
    function validateOnlyString (strValue) {
        var objStr =/^[a-zA-Z]*$|[a-zA-Z\s\[a-zA-Z]]*$/ ;
		if(objStr.test(strValue)) {
				return true;
		} else {
			return false;
		}
    }
    
    /*********************************************************
        validating HH:MI format;
        input: HH:MI
        return: true/false
        author: JEEVA.P
        modified : Muthu Lakshmi V.
    *********************************************************/
     function validateHourMinute(obj)
    {
        var strValue = obj.value;
        var objRegExp = /^\d{2}:\d{2}$/;
        if(strValue.length>0)
        {
            if(objRegExp.test(strValue))
            {
                var time = strValue.split(":");
                if(time[0]>23)
                {
                    alert(timeAlertMsg_[0]);
                    obj.value="00:00";
                    obj.focus();
                    return false;
                }
                if(time[1]>59)
                {
                    alert(timeAlertMsg_[1]);
                    obj.value="00:00";
                    obj.focus();
                    return false;
                }
                
                return true;
            }
            else
            {
                alert(timeAlertMsg_[2]);
                obj.value="00:00";
                obj.focus();
                return false;
            }
        }
        else
        {
            return false;
        } 
        return true;           
    }
    
    if( document.captureEvents && Event.KEYDOWN) 
    {
       document.captureEvents(Event.KEYDOWN);
    }
    
    /*********************************************************
        getting cusrsor position;
        input: Objest Id
        return: position
        author: Muthu Lakshmi V.
    *********************************************************/
    
    function GetCursorLocation(CurrentTextBox)
    {
        var CurrentSelection, FullRange, SelectedRange, LocationIndex = -1;
        if (typeof CurrentTextBox.selectionStart == "number")
        {
            LocationIndex = CurrentTextBox.selectionStart;
        }
        else if (document.selection && CurrentTextBox.createTextRange)
        {
            CurrentSelection = document.selection;
            if (CurrentSelection)
            {
                SelectedRange = CurrentSelection.createRange();
                FullRange = CurrentTextBox.createTextRange();
                FullRange.setEndPoint("EndToStart", SelectedRange);
                LocationIndex = FullRange.text.length;
            }
        }
        return LocationIndex;
    }
    
    /*********************************************************
        validates time
        input: event
        return: true/false
        author: Muthu Lakshmi V.
    *********************************************************/
    
    function validateTime(e)
    {
        var code = 0;
        
        if(window.event )
        {
              e = window.event;
              code = e.keyCode;
              shiftPressed=e.shiftKey;
              shiftcode=186;
        }
        else
        {
              code = e.which;
              shiftPressed=e.shiftKey;
              shiftcode=59;
        }
        
        var index = GetCursorLocation(this);
        var txt = this.value;
        var a = txt.charAt(index-1);
        var b = txt.charAt(index);
        
        if(shiftPressed && code==shiftcode && index==2 && b!=":")
            return true;
        
        if((a==":" && code==8) ||(b==":" && code==46))
        
            return false;
        
        if(e && code != 13 && code!=0 && code!=8 && code!=37 && code!=38 && code!=39 && code!=40 && code!=46)
        {
             if(code != 44)
             {
                 if(shiftPressed)
                 {
                    return false;
                 }
                 if(e && code < 48 ||  code >= 58 )
                 {
                    return false;
                 }
                 else if(index == 2)
                 {
                        return false;
                 }
             }
             else
             {
                return true;
             }
        }
        else
        {
          return true;
        }
    
    }
