//This file stores all the common functions 

//Function to Validate Number
//Parameters: Takes 3 Parameteres
//		1 - The number to check
//		2 - Maximum number of digits allowed before decimal
//		3 - Maximum number of digits allowed after decimal

//To open window - on 8/2/2005 - yogesh
function mywin(url) 
{
var newwin
newwin = window.open(url,'newwin');
}  
//To open window - End
function IsValidNumber(Num,precision,scale)
{
    var Number
    Number=trim(Num)
	if(trim(Number)=="")
		return false;
	
	//Check For Valid Number
	if (isNaN(Number)==true)    
		return false;
	
	//Check if More Than One Decimal 
	if(Number.split(".").length > 2) 
		return false;
		
	
	if(Number.indexOf(".") > 0)
	{
		//If the number contains decimal point
		if((Number.split(".")[0]).length > precision)
			return false;
		
		if((Number.split(".")[1]).length > scale)
			return false;	
		return true;
	}
	else
	{
		//If the number doesn't contain decimal point
		if(Number.length > precision)
			return false;
		else
			return true;
	}
	
}

//Function to Validate Percentage
//Parameters: Takes 3 Parameteres
//		1 - The number to check
//		2 - Maximum Number of digits before decimal
//		3 - Maximum Number of digits after decimal
function ValidPercent(dblNum,intDigitsBeforeDecimal,intDigitsAfterDecimal)
{  
       if (IsValidNumber(dblNum,intDigitsBeforeDecimal,intDigitsAfterDecimal) == true)
       {
			    if(dblNum<=0)
				   return false;
				else
					return true;				   
       }
       else
			return false;	
} 

//Function to Return the trimmed value passed as argument
//Parameters: Takes 1 Parameteres
//		1 - The value to trim
function trim(st) 
{
	var len = st.length;
	var begin = 0, end = len-1;
	while (st.charAt(begin) == " " && begin < len) 
	{
		begin++;
	}
	while (st.charAt(end) == " " && begin < end) 
	{
		end--;
	}
	return st.substring(begin, end+1);
}


//modification done and modified by Liju Mathew And Lakhnesh Pandey

//Function to Validate date
//Parameters: Takes 2 Parameteres
//		1 - The value to validate

function IsValidDate(strDate)
{
   
	//First Parameter is Date String
	//date Format Assumed is mm/dd/yyyy (Ex : 01/01/2000 is 1st Jan 2000)
	//date has only the seperators (/) or (-)
	var year,month,day,strSep,i;
	//strDate=trim(strDate)
	//assigning the seperator
	for(i=0;i<strDate.length;i++)
	   {
	    if(strDate.charAt(i)=="/" || strDate.charAt(i)=="-")
	    {
	     strSep=strDate.charAt(i)
	     break;
	    }
	   }  
  //Seperate Year,Month,Day
	day=strDate.split(strSep)[0];
	month=strDate.split(strSep)[1];
	year=strDate.split(strSep)[2];

	var months=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	//Check for Leap Year & Adjust The days in Feb accordingly
		if(year % 4 == 0)
		{
			months[1]=29;
		}

	    
	//Check for valid Numbers(Day,month,year)
	if(isNaN(day) || isNaN(month) || isNaN(year))
     {
		return false;
	     }
	//Added by yogesh 28-7-2004    
	//Check For Length of Date String   		
	 if(day.length == 2 || day.length == 1 )
	  {}
     else 
       return false;
                        
	 if(month.length == 2 || month.length == 1 )
	  {}
     else
       return false;
      
     if(year.length == 4)
      {}
     else
       return false;
     
       		     
     //code added by vidya
	 
	//Check for Valid Month
	if(month > 12 || month < 1)
{
		return false;
	
	}
	//Check For Valid Days
	if(day < 1 || day > months[month-1])
{
		return false;
	}	
		for(var i=1;i<=strDate.length;i++)
		{
			if(!((i==3) ||( i==6)))
			{
				num=strDate.substring(i,i-1)
	
				if(isNaN(num)==true)
				{	
					return false;
				}
			}
			if(((i==3) ||( i==6)))
			{
				if(strDate.substring(i,i-1)!= strSep) 
				{
				return false
				}
			}
			return true
		}
		
	//added by abhay for more accuracy
	if ((strDate.charAt(8) == "/") || (strDate.charAt(10) == "/"))
	{
		return false
	}
	
	
	//	end of ad's code
	
	//added here an another condition for seperator
	//Check For Seperators
	if ((strDate.charAt(2) == "/") && (strDate.charAt(5) == "/"))
	{
		return true
	}
	else
	{
		if ((strDate.charAt(2) == "-") || (strDate.charAt(5) == "-"))
		{
			return true;
		}
		else{

			return false;
}
	}

}


//Function to Validate Email
//Parameters: Takes 1 Parameteres
//		1 - The value to validate
	//email validation
	function emailValidation(entered)
	{
		var intCnt
		intCnt = 0;
		
		apos=entered.indexOf("@"); 
		dotpos=entered.lastIndexOf(".");
		lastpos=entered.length-1;
		if (apos < 1 || (dotpos-apos) < 2 || lastpos-dotpos > 3 || (lastpos-dotpos) < 2){
			return false
		}
		
		//no dots continuous
		if (entered.charAt(dotpos-1) == "."){
			return false
		}
		
		//counter for @
		for (var j=0; j<entered.length; j++){
			if (entered.charAt(j) == "@"){
				intCnt++;
			}
		}
		
		//only one @ allowed
		if (intCnt != 1){
			return false
		}
		
		//checking for speacial characters
		for (var i=0; i<entered.length; i++){
				//ascii from 33 to 45, 33- 45, 58-63, 123-126 are checked
			//if (((entered.charAt(i) >= "!") && (entered.charAt(i) <= "-")) ||
			
			if (((entered.charAt(i) >= "!") && (entered.charAt(i) < "-")) ||
			 ((entered.charAt(i) >= "[") && (entered.charAt(i) <= "^")) ||
			 ((entered.charAt(i) >= ":") && (entered.charAt(i) <= "?")) ||
			 ((entered.charAt(i) >= "{") && (entered.charAt(i) <= "~")) ) {
				return false
			}
		}
		
		return true
	} 

//Function to Clear all Fields of Form Passed as Parameter
//Parameters: Takes 1 Parametere
//			  The parameter is the form name whose fields are to be cleared
function fClearForm(formName)
{
	for(var i=0;i<formName.elements.length;i++)
	{
		if(formName.elements(i).type == "text" || formName.elements(i).type == "textarea")
			formName.elements(i).value="";
		else
		{
			if(formName.elements(i).type == "select-one")
				formName.elements(i).selectedIndex = 0;
			else
			{
				if(formName.elements(i).type == "radio" || formName.elements(i).type == "checkbox")
					formName.elements(i).checked = false;
			}
		}
	}
}

function checkPhone(value)
{
	for(var i = 0;i < value.length;i++)
	{
		if(!((value.charAt(i) >= "0" && value.charAt(i) <= "9") ||
			   (value.charAt(i) == "(" ) || 
			   (value.charAt(i) == ")") ||
			   (value.charAt(i) == "-") ||
			   (value.charAt(i) == " ") ||
			   (value.charAt(i) == ",") || 
			   (value.charAt(i) == " ")
			))
		{ 
			return false		
		}
	}
	return true
}

function checkForAlphabets(value)
{
	for(var i = 0;i < value.length;i++)
	{
		if( !( (value.charAt(i) >= "A" && value.charAt(i) <= "Z") ||
			   (value.charAt(i) >= "a" && value.charAt(i) <= "z")
		     )
		  )
		{ 
			return false				
		}
	}
	return true
}


/**************************************************************
 Split: Returns a zero-based, one-dimensional array containing 
        a specified number of substrings

 Parameters:
      Expression = String expression containing substrings and 
                   delimiters. If expression is a zero-length 
                   string(""), Split returns an empty array, 
                   that is, an array with no elements and no 
                   data.
      Delimiter  = String character used to identify substring 
                   limits. If delimiter is a zero-length 
                   string (""), a single-element array 
                   containing the entire expression string 
                   is returned.

 Returns: String
***************************************************************/
function Split(Expression, Delimiter)
{
	var temp = Expression;
	var a, b = 0;
	var array = new Array();

	if (Delimiter.length == 0)
	{
		array[0] = Expression;
		return (array);
	}

	if (Expression.length == '')
	{
		array[0] = Expression;
		return (array);
	}

	Delimiter = Delimiter.charAt(0);

	for (var i = 0; i < Expression.length; i++) 
	{
		a = temp.indexOf(Delimiter);
		if (a == -1)
		{
			array[i] = temp;
			break;
		}
		else
		{
			b = (b + a) + 1;
			var temp2 = temp.substring(0, a);
			array[i] = temp2;
			temp = Expression.substr(b, Expression.length - temp2.length);
		}
	}

	return (array);
}
// Added by yogesh
//      Comparing Current Date and User Date
// cday =current day
// uday =user day
function IsDateGreater(cday,cmonth,cyear,uday,umonth,uyear)
 {
   var cday = cday;
   var cmonth = cmonth;
   var cyear = cyear;
   var uday = uday;
   var umonth = umonth;
   var uyear = uyear;
   if ((1*(cyear)) <= (1*(uyear)))
    {
      if ((1*(cyear)) == (1*(uyear)))
        {
         
           if((1*(cmonth))<=(1*(umonth)))
            {
              if ((1*(cmonth)) == (1*(umonth)))
                {
                  if ((1*(cday)) < (1*(uday)))
                    {
                      return true;
                    }
                  else
                    {
                      return false;
                    }  
                }
              else
                {
                  return true;
                }  
            }
          else
            {
              return false;
            }  
        }
      else
        {
          return true;
        }  
    }
   else
    {
      return false;
    } 
 }