/**
 *	This file contains common javascript functions used in the JSPs.
 *
 *  @author:	Dilip Nair, Tata Infotech Ltd.
 *
 *  @Creation Date: 06/04/2002 
 *  
 *  @Last Update Date: 
 *
 *  @Source Id: SPZ/S/1132 1023 100
 *
 *  @Change History: 
 */
 
/**
*	This function checks if a numeric value passed to it is in a proper format.
*	INPUT:-	Numeric value to be checked, Format the value is expected to be in,
*			Name of the numeric field.
*	OUTPUT:-True, if the value matches the given format.
*			False, otherwise.
*/
function validateNumberFormat(strValue, strFormat, strName)
{
      

	if (strValue.length!=strFormat.length || strValue.length ==0)
		return false;
	else
	{
		var strTemp='';
		for (i=0;i<strFormat.length ;i++ )
		{ 
			
			if (strFormat.charAt(i)=='#')
				strTemp +="[0-9]";
			else
				strTemp +=strFormat.charAt(i);
		}
		
		var regExp = new RegExp(strTemp);		
		return(regExp.test(strValue));
	}
}




/**
 * This function checks for null values
 * Input   : strValue - string value to be validated
 *			    strName - field name
 * Output : true - if string value is not null
 *             false - if string value is null
*/
function validateNull(strValue, strName)
{
	if((!strValue) || (strValue==""))
	{
		alert("Please Enter " + strName);
		return false;
	}
	return true;
}

/**
 * This function checks for any space characters
 * Input   : strValue - string value to be validated
 *			    strName - field name
 * Output : true - if string value does not contain any space character
 *             false - if string value contains atleast one space character
*/
function validateAnySpaces(strValue, strName)
{
	iLength=strValue.length;
	   
	for (i=0; i<iLength; i++)
	{
		strChar=strValue.substring(i,i+1);
		if (strChar == " ")
		{
			alert(strName + " must not contain spaces");
            return false;
        }
    }
    return true;
}

/**
 * This function validates number (integer)
 * Input   : strValue - value to be validated
 *			    strName - field name
 * Output : true - if strValue is a valid number (integer)
 *             false - if strValue is null or not a valid number
*/
function validateNumber(strValue, strName)
{
    if (!validateNull(strValue, strName))
		return false;
	if (strValue.match(/^[+-]?[0-9]+$/) == null)
	{
		alert(strName + " must be numeric");
		return false;
	}
	if (strValue.match(/^-0*$/) != null)
	{
		alert(strName + " must be numeric");
		return false;
	}
	return true;
}

/**
 * This function checks whether the input string 
 * contains only alphabetic characters
 * Input   : strValue - string value to be validated
 *			    strName - field name
 * Output : true - if string value contains only alphabetic characters
 *             false - if string value is null or contains atleast one non-alphabetic character
*/
function validateAlphabets (strValue, strName)
{
    if (!validateNull(strValue, strName))
		return false;
	if (strValue.match(/^[a-zA-Z]+$/) == null)
	{
		alert(strName + " must be alphabetic");
		return false;
	}
	return true;
}
/* This function checks if string is null, but does not return any alert statement*/
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
/* This function checks if character is a letter*/
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

/* This function checks if character is a digit*/
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

/**
 * This function checks whether the input string 
 * contains only alphanumeric characters but does not throw any alert statements. 
 * Especially used to check if character is not alphanumeric.
 * Input   : strValue - string value to be validated
 *			    strName - field name
 * Output : true - if string value contains only alphanumeric characters
 *             false - if string value is null or contains atleast one non-alphanumeric character
*/

function isAlphanumeric (s)
{
	var i;

    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return true;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    return true;
}

/*
	This function will return a non alphanumeric delimiter of a string
*/
function getDelim(s) {
	de = ""
	for (i=0;i<s.length;i++) {
		var c = s.charAt(i)
		if (!isAlphanumeric(c)) {	
			de = c
			break
		}
	}
	return de
}

/**
 * This function checks whether the input string 
 * contains only alphanumeric characters
 * Input   : strValue - string value to be validated
 *			    strName - field name
 * Output : true - if string value contains only alphanumeric characters
 *             false - if string value is null or contains atleast one non-alphanumeric character
*/
function validateAlphaNumeric (strValue, strName)
{
    if (!validateNull(strValue, strName))
		return false;
	if (strValue.match(/^[a-zA-Z0-9]+$/) == null)
	{
		alert(strName + " must be alphanumeric");
		return false;
	}
	return true;
}

/*
 * This function returns the numeric value given the string.
 * Parseint returns 0 if you have a string like 00090.
 * This method removes the leading zeros, and then returns the int value using parse int.
*/
function convertToNumber(strValue)
{
	//alert("In convert number");
	var len = strValue.length;
	//alert(" len	"+len);
	for (i=0;i<len;i++)
	{
	  if((strValue.charAt(i)== '0') && ((strValue.charAt(i+1) != '0')||(strValue.charAt(i+1) == '0')))
		strValue=strValue.replace('0',' ');
	  else
	     i=len;	

	  //alert(strValue);
	}
	var iValue = parseInt(strValue);
	
	return iValue;

}

/**
 * This function validates positive number (positive integer)
 * Input   : strValue - value to be validated
 *			    strName - field name
 * Output : true - if strValue is a valid positive number (positive integer)
 *             false - if strValue is null or not a valid positive number
*/
function validatePositiveNumber (strValue, strName)
{
	if (!validateNumber(strValue,strName))
		return false;
/*	alert("In validatePositiveNumber "+strValue);
	var len = strValue.length;
	alert(" len	"+len);
	for (i=0;i<len;i++)
	{
	  if((strValue.charAt(i)== '0') && ((strValue.charAt(i+1) != '0')||(strValue.charAt(i+1) == '0')))
		strValue=strValue.replace('0',' ');
	  else
	     i=len;	

	  alert(strValue);
	}

	alert("In validatePositiveNumber "+parseInt(strValue));	
*/
	if (convertToNumber(strValue) <= 0)
	{
		alert(strName + " must be a positive number");
		return false;
	}
	return true;
}

/**
 * This function validates a number containing decimal point
 * Input   : strValue - value to be validated
 *			    strName - field name
 * Output : true - if strValue is a valid floating point number
 *             false - if strValue is null or not a valid floating point number
*/
function validateFloat (strValue, strName)
{
    if (!validateNull(strValue, strName))
		return false;
	if (strValue.match(/^-?[0-9]+(\.[0-9]+)?$/) == null)
	{
		alert(strName + " must be float");
		return false;
	}
	if (strValue.match(/^-0*(\.0*)?$/) != null)
	{
		alert(strName + " must be float");
		return false;
	}
	//alert(strValue + " VALID");
	return true;
}

/**
 * This function validates a number against a specified format
 * Input   : strValue - value to be validated
 *             strFormat - number format to validate the input value
 *			    strName - field name
 * Output : true - if strValue is in the specified format
 *             false - if strValue is null or not in the specified format
*/
/*function validateNumberFormat (strValue, strFormat, strName)
{
	//alert("strValue : " + strValue);
    if (!validateNull(strValue, strName))
		return false;
	if (strFormat.match(/^#+(\.#+)?$/) == null)
	{
		//alert("Invalid format " + strFormat);
		return false;
	}
	//alert ("VALID FORMAT");

	iPos = strFormat.indexOf(".");
	iLen = strFormat.length;
	//alert("iPos : " + iPos);
	//alert("iLen : " + iLen);

	if (parseInt(iPos) == -1)
	{
		re = "/^[0-9]{" + iLen + "}$/";
		//alert("re : " + re);
		if (strValue.match(eval(re)) == null)
		{
			alert(strName + " is not in " + strFormat + " format");
			return false;
		}
		else 
			return true;
	}
	else
	{
		iLen1 = iPos
		iLen2 = iLen - iPos -1;
		//alert ("iLen1 : " + iLen1 + " iLen2 : " + iLen2);

		re = "/^[0-9]{" + iLen1 + "}\\.[0-9]{" + iLen2 + "}$/";
		//alert("re : " + re);
		if (strValue.match(eval(re)) == null)
		{
			alert(strName + " is not in " + strFormat + " format");
			return false;
		}
		else 
			return true;
	}
}
*/

/**
 * This function validates dates
 * Input   : strDayPart - day part of a date
 *			    strMonthPart - month part of a date
 *             strYearPart - year part of a date
 * Output : true - if input values represent a valid date
 *             false - if input values does not represent a valid date
*/
function checkDate(strDayPart, strMonthPart, strYearPart)
{
	if (  (parseInt(strDayPart,10) <= 0) || (parseInt(strDayPart,10) > 31))
        return false;
	if (  (parseInt(strMonthPart,10) <=0) || (parseInt(strMonthPart,10) > 12))
        return false;

	arrDaysPerMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	if ( ( (parseInt(strYearPart,10)%4 == 0) && (parseInt(strYearPart,10)%100 != 0))
		 || (parseInt(strYearPart,10)%400 == 0))
		arrDaysPerMonth[2] = 29;

	if (parseInt(strDayPart,10) > parseInt(arrDaysPerMonth[parseInt(strMonthPart,10)],10))
		return false;

	return true;
}

/**
 * This function trims spaces from either end of a string
 * Input   : String
 * Output : String with spaces trimmed.
*/
function trim(inword)
{
   word = inword.toString();
   var i=0;
   var j=word.length-1;
   while(word.charAt(i) == " ") i++;
   while(word.charAt(j) == " ") j--;
   if (i > j) 
	{
		return word.substring(i,i);
	} else 
	{
		return word.substring(i,j+1);
	}
}


//**********************************************************
//Function Name    : validateEmailAddress()
//Purpose          : This validates whether the email address is in the correct format.
//Inputs           : The email address textbox object and the the textbox name.
//Output           : Returns Boolean value depending on valid or invalid entry. 
//                   It returns a true value for a valid entry else it returns a false value.
//**********************************************************

function validateEmailAddress(strValue, strName)
{
   
   var aSpecialChar = new Array("+","-","~","`","!","@","#","$","%","^","&","*","(",")","=","{","}","|","[","]","\\",";","'",":","\"","<",">","?",",","/");
                                 
      
   //Check if intial character is a @ or if there is no @
   if(strValue.charAt(0) == "@" || strValue.indexOf("@") == -1)
   {

      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);
   
   }
   
   //Get the string before @
   var iPosAttheRate = strValue.indexOf("@");
   var sStringBeforeAttheRate = strValue.substring(0,iPosAttheRate);
   var iNoOfSpecialChar = 0;
        
   //Check if the string before the @ character has all numbers 
   if(!isNaN(sStringBeforeAttheRate))
   {
   
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);
      
   }
  
   //Check if the string before the @ character has special characters
   for(iIndexOuter=0;iIndexOuter<sStringBeforeAttheRate.length;iIndexOuter++)
   {
   
      for(iIndexInner=0;iIndexInner<31;iIndexInner++)
      {

         if(sStringBeforeAttheRate.charAt(iIndexOuter) == aSpecialChar[iIndexInner])
         {
			if(sStringBeforeAttheRate.charAt(iIndexOuter) != ".")
            iNoOfSpecialChar+=1; 
        
         }
         
      } 
      
   }
   
   if((iNoOfSpecialChar > 0) && (iNoOfSpecialChar <= sStringBeforeAttheRate.length))
   {
   
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);   
   
   }

   //Get the string after @
   var sStringAfterAttheRate = strValue.substring(iPosAttheRate + 1,strValue.length);
   var bFlag = 0; //Flag to indicate consecutive "." in string after @
    
   //Check if the string after @ begins with "." or does not have atleast a single "."
   
   if(sStringAfterAttheRate.charAt(0) == "." || sStringAfterAttheRate.indexOf(".") == -1  )
   {
   
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);
      
   }
   
   //Check if the string after @ has two consecutive "."
   
   for(iIndexOuter=0;iIndexOuter<sStringAfterAttheRate.length;iIndexOuter++)
   {
   
      if(sStringAfterAttheRate.charAt(iIndexOuter) == "." && sStringAfterAttheRate.charAt(iIndexOuter + 1) == "." )
      {
      
            bFlag = 1;
        
      }

   } 
   
   if(bFlag ==1)
   {
   
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);
         
   }
   
   //Check if the email Id ends in "."

   if(sStringAfterAttheRate.charAt(sStringAfterAttheRate.length - 1) == ".")   
   {
      
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);
      
   }
   
    iNoOfSpecialChar = 0;
   //Check if the string after the @ character has special characters
   for(iIndexOuter=0;iIndexOuter<sStringAfterAttheRate.length;iIndexOuter++)
   {
   
      for(iIndexInner=0;iIndexInner<32;iIndexInner++)
      {

         if(sStringAfterAttheRate.charAt(iIndexOuter) == aSpecialChar[iIndexInner])
         {
			if(sStringAfterAttheRate.charAt(iIndexOuter) != ".")
				iNoOfSpecialChar+=1; 
        
         }
         
      } 
      
   }
  
   if(iNoOfSpecialChar > 0) 
   {
   
      alert(strName + " is not in proper format");
      //oTextBox.focus();
      return(false);   
   
   }

 
   return(true);
      
}