//This file stores all the common functions 


// *****   to check if Number entered is Valid or not 		*********

//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

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;
	}
	
}


// *****   to check if Percentage Entered is Valid or not 		*********

//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


// *****   to check if Date  Entered is Valid or not 		*********

//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 Length of Date String
	if(strDate.length < 8 || strDate.length > 10 )
{
		 
		return false;
}		
	
	//Check for valid Numbers(Day,month,year)
	if(isNaN(day) || isNaN(month) || isNaN(year))
{
		return false;
	}
	//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 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;
}
	}

}

// *****   to check if Email is Valid or not 		*********

//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
	} 

// *****   Clear all fields of the FORM 		*********

//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;
			}
		}
	}
}

// *****   to check if Phone Number is Valid or not 		*********

function checkPhone(value)
{
	for(var i = 0;i < value.length;i++)
	{
		if(!(value.charAt(i) >= "0" && value.charAt(i) <= "9"))
		{ 
			if(!(value.charAt(i) == "-"))
			{ 
				return false
			}
		}
	}
	return true
}

// *****   to check if Alphabets are Valid or not 		*********

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);
}

// *****   to check if Pin-code is Valid or not 		*********

function checkingpin(str1)
{
//This function checks if the entered value is a numeric value
        var numstr="1234567890-";
        var intctr,intLen;
            intLen=str1.length;
        for(intCtr=0;intCtr <= intLen && numstr.indexOf(str1.charAt(intCtr))>=0 ;intCtr++);
        if(intCtr > intLen)
        {
            return 1;
        }
        else
        {
        	return 0;
        }

  }	// End fn checkingnumber	   
    
