/*
' ***********************************************************************************************
'
' File Name		:	functions.js
' File Type		:	javascript
' Description	:	javascript validation file
' Author		:	Wipro Infotech
' Created On	:	05/03/2002
' Version		:	1.0 		
' Notices		:	Legal stuff Copyright(c) 2002 Wipro Infotech all rights reserved
'
' Modified On	Version		Modified By			Purpose of Modification 
' ===========	=======		==========			====================
' 00/00/0000	0.0			Name				Purpose
'
'
' ***********************************************************************************************
*/var decimalPointDelimiter = "."
// whitespace characters
var whitespace = " \t\n\r";


function checkDateObjs(ar, desc, reqF)
{
    for (var xx=0; xx<ar.length; xx++)
    {
    	if (! checkDate(ar[xx], desc[xx], reqF[xx]))
		{
			ar[xx].focus();
			return false;
		}
    }
    return true;
}


//checks that date is in correct DDMMYYYY format
function checkDate(date, msg, req)
{
	date.value=trim(date.value);
    
    //field need to be checked whether required or not
    if (isEmpty(date.value))
    {
    	if(req>0)
    	    return warnEmpty(date,msg);
    	else
    	    return true;
    }	

    //dates need to be 8 characters or we don't need to do any other verification
    if (!(date.value.length == 10))
	return warnWrongDateFormat(date, msg);
  

    //break up date into components
    day = date.value.substring(0,2);
    month = date.value.substring(3,5);
    year = date.value.substring(6,10);
    
    //Check that year month and date are numbers that fall within expected limits
    if (! (isYear(year) && isMonth(month) && isDay(day)))
	return warnWrongDateFormat(date, msg);

    // catch invalid days, except for February
    if (day > daysInMonth(month)) 
	return warnWrongDateFormat(date, msg);

    //check february dates 
    if ((month == 02) && (day > daysInFebruary(year)))
	return warnWrongDateFormat(date, msg);

    return true;
}

function warnWrongDateFormat(theField, msg)
{
    if (msg == null)
    	alert("The format is not correct for this field : " + theField.value + "\nDate field format is DD/MM/YYYY." );
    else
    	alert ( msg + " is of wrong format : " + theField.value + "\nDate field format is DD/MM/YYYY.");
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}

//Utility functions
function isYear (s)
{   
    for(i=0;i<s.length;i++)
    {
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
    }   
    return (s.length == 4);
}

function isMonth (s)
{   
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{     
    return isIntegerInRange (s, 1, 31);
}

function daysInMonth(x)
{
	x=parseInt(x)
	switch (x)
	{
		 case 1 : return 31;
		 case 2 : return 29;
		 case 3 : return 31;
		 case 4 : return 30;
		 case 5 : return 31;
		 case 6 : return 30;
		 case 7 : return 31;
		 case 8 : return 31;
		 case 9 : return 30;
		 case 10 : return 31;
		 case 11 : return 30;
		 case 12 : return 31;
	}
}

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isIntegerInRange (s, a, b)
{   
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

function isDigit (d)
{
   return (( d>="0") && (d <= "9"))
}

function isEmpty(x)
{
   return ((x == null) || (x.length == 0));
}


function warnEmpty (theField, msg)
{   
    theField.focus()
    if (msg == null)
    	alert("This is a mandatory field, please fill in a value.");
    else
    	alert(msg + " is a mandatory field, please fill in a value.");
    return false;
}

function checkReqObjs(ar, desc)
{
    var ix;
    for (ix=0; ix<ar.length; ix++)
    {
		if (! checkValue(ar[ix], desc[ix]))
		{
			ar[ix].focus();
			return false;
		}
    }
    return true;
}

// checks whether the value is filled or not
function checkValue(fie, msg)
{
	
    if (isEmpty(fie.value))
    	return warnEmpty(fie, msg);
    if (isWhitespace(fie.value.substring(0,1)))
    	return warnEmpty(fie, msg);
    return true;
}

function isWhitespace(s)
{
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function checkIntObjs(ar, desc, flag)
{
    for (var i=0; i<ar.length; i++)
    {
		if (! checkInt(ar[i], desc[i], flag[i]))
		{
			ar[i].focus();
			return false;
		}
    }
    return true;
}

// checks whether the value is integer
function checkInt(s, msg, req)
{	
    s.value=trim(s.value);
    if (isEmpty(s.value))
    {
		if(req>0)
			return warnEmpty(s,msg);
		else
			return true;
    }

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.value.length; i++)
    {   
        // Check that current character is number.
        var c = s.value.charAt(i);

        if (!isDigit(c)) return warnWrongIntFormat(s, msg);
    }

    // All characters are numbers.
    return true;
}

function warnWrongIntFormat(theField, msg)
{
    if (msg == null) {
       alert("The data type is not correct for this field : " + theField.value
		+ "\nIt should be an integer/number \n");
    }else {
       alert( msg + " is of wrong format : " + theField.value + "\nIt should be an integer." );
    }
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}

function checkFloatObjs(ar, desc,num, deci, flag)
{
    for (var i=0; i<ar.length; i++)
    {
		if (! checkFloat(ar[i], desc[i], num[i], deci[i], flag[i]))
		{
			ar[i].focus();
			return false;
		}
	}
    return true;
}

//checks for float format
function checkFloat (s, msg, num, deci, req)
{
	s.value=trim(s.value);
    var ins = s.value;
    
    if (isEmpty(s.value))
    {
    	if(req>0)
    	    return warnEmpty(s,msg);
    	else
    	    return true;
    }

    // check for all chars or digits .
    for (var i = 0; i < ins.length; i++)
    {
		var d = ins.charAt(i);
		if (!((d ==".") || (( d>="0") && (d <= "9"))))
			return warnWrongFloatFormat(s, msg, deci);
	}

    var ix = ins.indexOf(".");
    
	// if its a whole no add decimal places        
	if (ix == -1)   	// whole number	
	{
		if(ins.length>(num))
	    	return warnWrongFloatFormat(s, msg, deci);
	    else
	    {
			ins=ins+".";
			for(i=0; i<deci ; i++)
				ins=ins+"0";				
			
			s.value=ins;
		}
		return true;
    }
    if ((ins.substr(ix+1).indexOf(".") != -1)  // more than 1 dec. point
	    	|| (ins.substring(0,ix).length<1)	// no digit before dec. point
			|| (ins == "."))
		return warnWrongFloatFormat(s, msg, deci);
	
	if(ix > num)
		return warnWrongFloatFormat(s, msg, deci);
	
	var deciDiff = deci-(ins.length - ix-1);
	// fill digits for remaining places after decimal
	if(deciDiff < 1)
	{
		s.value=ins.substring(0,(ins.substring(0,(ix+1)).length+deci));
		return true;
	}
	for(i=0;i<deciDiff;i++)
		ins=ins+"0";
	s.value=ins;

	return true;
}
/*
function checkFloat (s, msg, deci, req)
{
	
    var ins = s.value;
    
    s.value=trim(s.value);
    
    if (isEmpty(s.value))
    {
    	if(req>0)
    	    return warnEmpty(s,msg);
    	else
    	    return true;
    }

    // check for all chars or digits .
    for (var i = 0; i < ins.length; i++)
    {
		var d = ins.charAt(i);
		if (!((d ==".") || (( d>="0") && (d <= "9"))))
			return warnWrongFloatFormat(s, msg, deci);
	}

    var ix = ins.indexOf(".");    
	
    if ((ix == -1) // if its a whole no
    		|| (ins == ".") // if only '.' is present.
    		|| (ins.substr(ix+1).indexOf(".") != -1)  // more than 1 dec. point
	    	|| (ins.substring(0,ix).length<1)	// no digit before dec. point
	    	|| (ins.substr(ix+1).length > deci)) // if decimal places are greater than required.
		return warnWrongFloatFormat(s, msg, deci);
	    
	return true;
}
*/

function warnWrongFloatFormat(theField, msg, deci)
{
    if (msg == null)
    	alert("The format is not correct for this field : " + theField.value + "\nPrice field format is dddddd.dd with\n"+deci+" decimals.");
    else
    	alert ( msg + " is of wrong format : " + theField.value + "\nFor this field format is dddddd.dd with\n"+deci+" decimals." );
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}

function checkTimeObjs(ar, desc, reqF)
{
    for (var xx=0; xx<ar.length; xx++)
    {
    	if (! checkTime(ar[xx], desc[xx], reqF[xx]))
		{
			ar[xx].focus();
			return false;
		}
    }
    return true;
}


//checks that Time is in correct HH:MM format
function checkTime(time, msg, req)
{
    
    //field need to be checked whether required or not
    
    time.value=trim(time.value);
    
    if (isEmpty(time.value))
    {
    	if(req>0)
    	    return warnEmpty(time,msg);
    	else
    	    return true;
    }

    //time need to be 5 characters or we don't need to do any other verification
    if (!(time.value.length == 5))
		return warnWrongTimeFormat(time, msg);
  
    //break up time into components
    var hour = time.value.substring(0,2);
    var col = time.value.substring(2,3);
    var min = time.value.substring(3,5);
    
    //Check that hour and min are numbers that fall within expected limits
    if (! (isHour(hour) && isCol(col) && isMin(min)))
		return warnWrongTimeFormat(time, msg);
    
    return true;
}

function warnWrongTimeFormat(theField, msg)
{
    if (msg == null)
    	alert("The format is not correct for this field : " + theField.value + "\nTime field format is HH:MM." );
    else
    	alert ( msg + " is of wrong format : " + theField.value + "\nTime field format is HH:MM.");
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}

//Utility functions
function isHour (s)
{   
    for(i=0;i<s.length;i++)
    {
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
    }
    return ((s.length == 2) && (parseInt(s)<24) && (parseInt(s)>=0));
}

function isMin (s)
{   
    for(i=0;i<s.length;i++)
    {
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
    }
    return ((s.length == 2) && (parseInt(s)<60) && (parseInt(s)>=0));
}

function isCol (s)
{   
    return ((s.length == 1) && (s==":"));
}

function checkAlphaObjs(ar, desc, flag)
{
    for (var i=0; i < ar.length; i++)
    {
		if (! checkAlpha(ar[i], desc[i], flag[i]))
		{
			ar[i].focus();
			return false;
		}
    }
    return true;
}

// check for characters
function checkAlpha(s, msg, req)
{
	s.value=trim(s.value);
    
    if (isEmpty(s.value))
    {
		if(req>0)
			return warnEmpty(s,msg);
		else
			return true;
    }

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.value.length; i++)
    {   
        // Check that current character is alphanumeric.
        var c = s.value.charAt(i);

        if(i==0)
        {
            if (!isAlpha(c))
            	return warnWrongAlphaFormat(s, msg);
        }
        else
        {
			if(!isAlphaOrSpace(c))
				return warnWrongAlphaFormat(s, msg);
   		}
    }

    // All characters are alphanumeric.
    return true;
}

function warnWrongAlphaFormat(theField, msg)
{
    if (msg == null)
    	alert("The data type is not correct for this field : " + theField.value	+ "\nIt should be an alphabetic value.\n");
    else
       alert( msg + " is of wrong format : " + theField.value + "\nIt should be an alphabetic value." );    
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}

function isAlpha (c)
{
   return ((( c>="a") && (c <= "z")) || (( c>="A") && (c <= "Z")))
}

function isAlphaOrSpace (c)
{
   return ((( c>="a") && (c <= "z")) || (( c>="A") && (c <= "Z")) || c==" ")
}

function checkAlphaNumObjs(ar, desc, flag)
{
    for (var i=0; i<ar.length; i++)
    {
		if (! checkAlphaNum(ar[i], desc[i], flag[i]))
		{
			ar[i].focus();
			return false;
		}
    }
    return true;
}

// check for characters
function checkAlphaNum(s, msg, req)
{
	s.value=trim(s.value);
    
    if (isEmpty(s.value))
    {
		if(req>0)
			return warnEmpty(s,msg);
		else
			return true;
    }

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < s.value.length; i++)
    {   
        // Check that current character is alphanumeric.
        var c = s.value.charAt(i);

        if(i==0)
        {
            if (!(isAlpha(c) || isDigit(c)))
            	return warnWrongAlphaNumFormat(s, msg);
        }        
        else
        {
			if(!(isAlphaOrSpace(c) || isDigit(c)))
				return warnWrongAlphaNumFormat(s, msg);
   		}
    }

    // All characters are alphanumeric.
    return true;
}

function warnWrongAlphaNumFormat(theField, msg)
{
    if (msg == null)
    	alert("The data type is not correct for this field : " + theField.value	+ "\nIt should be an alphanumeric value.\n");
    else
       alert( msg + " is of wrong format : " + theField.value + "\nIt should be an alphanumeric value." );    
    theField.value="";
    theField.select()
    theField.focus()
    return false;
}


function makeTextObjsBlank(ar)
{
	for (var i=0; i<ar.length; i++)
		makeTextBlank(ar[i]);
    return true;
}

// remove values of text fields
function makeTextBlank (s)
{
   s.value="";
   return true;
}

function makeListObjsBlank(ar)
{
    for (var i=0; i<ar.length; i++)
    	makeListBlank(ar[i]);
    return true;
}

// remove value of list objects
function makeListBlank(s)
{
   s.options[s.options.selectedIndex].value="";
   return true;
}


// remove spaces from the string
function trim(fil)
{
	if ((fil.length==0) || (fil==null))
	  return fil;
	
	var start=0;
	var end=fil.length;
	
	for (var i = 0; i < end; i++)
	{   
		// Check that current character isn't whitespace.
		var c = fil.charAt(i);

		if (whitespace.indexOf(c) == -1) 
		{
			start=i;
			break;
		}
	}
	if(i==end) 
	{
		fil="";
		return fil;
	}
	for (i = (end-1); i >0; i--)
	{   
		// Check that current character isn't whitespace.
		var c = fil.charAt(i);

		if (whitespace.indexOf(c) == -1) 
		{
			end = i+1;
			break;
		}
	}
	
	return fil.substring(start,end);
}


// function to remove new line from String 's'.
function removeNewLine(s)
{
	s.value=removeEqual(s.value);
	s.value=trim(s.value);
	var str = s.value;
	var regExp = /\r\n/g;
	var newStr=str.replace(regExp," ");
	s.value = newStr + ' ';	
	s.value=trim(s.value);
}

function removeEqual(str)
{
	var regExp = /=/g;
	var newStr=str.replace(regExp,":");
	regExp = /;/g;
	newStr=newStr.replace(regExp,".");
	return newStr;
}

function checkEmailId(emailStr,msg,req)
{
	var s=emailStr.value;
	
	if (isEmpty(s))
   	{
   		if(req>0)
   			return warnEmpty(emailStr,msg);
   		else
   			return true;
    }
    // is s whitespace?
    if (isWhitespace(s)) return warnWrongEmailFormat(emailStr,msg);
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return warnWrongEmailFormat(emailStr,msg);
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return warnWrongEmailFormat(emailStr,msg);
    else return true;
}

function warnWrongEmailFormat(theField,msg)
{
	alert("Please enter proper email address");
	theField.value="";
	theField.select()
	theField.focus()
    return false;
}

function checkPhone(s, msg, req)
{
	s.value=trim(s.value);
    
    if (isEmpty(s.value))
    {
		if(req>0)
			return warnEmpty(s,msg);
		else
			return true;
    }

    for (var i = 0; i < s.value.length; i++)
    {   
        // Check that current character is alphanumeric.
        var c = s.value.charAt(i);

        if(i==0)
        {
            if (!(isDigit(c)))
            	return warnWrongPhoneFormat(s, msg);
        }        
        else
        {
			if(!(isDigit(c) || (c=="-") || (c==",")))
				return warnWrongPhoneFormat(s, msg);
   		}
    }

    // All characters are alphanumeric.
    return true;
}

function warnWrongPhoneFormat(theField,msg)
{
	alert("Please enter proper " + msg + ". It can include any digit and '-' and ','");
	theField.value="";
	theField.select()
	theField.focus()
    return false;
}


