// JavaScript functions created and collected  by Suman
//all functions return true if conditions are satisfied else false

//<------------starts-------------------->
//Check valid phone numbers
//Name of the function: function test_phone(elmnt)
//elmnt : set of numbers (0 to 9) with special character hyphen(-) after 3 numbers . 
//return value: returns  true if input parameter  string satisfied the input criteria else returns false.
function test_phone(elmnt)
{
	var strx=/^[0-9][0-9][0-9][0-9-]*[0-9]$/;
	return strx.test(elmnt);
}
//<------------ends---------------------->

//Checks valid cell -phone numbers
//Name of the function: function test_cell_phone(elmnt){}
//elmnt : set of numbers (0 to 9) only  
//return value: returns  true if input parameter  string satisfied the input criteria else returns false.

function test_cell_phone(elmnt)
{
	var strx=/^[0-9]*$/;
	return strx.test(elmnt);
}
//<---------------ends------------------->
//<------------starts-------------------->
//Checks valid date entry in “mm/dd/yyyy” format
//Name of the function: function test_date(date1){}
//date1 : date argument that should be supplied in “mm/dd/yyyy” format.
//return value: returns  true if input parameter  string satisfied the input criteria including leap year consideration else returns false.
function test_date(date1)
{

		//var strx=/^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
		//var strx=/^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d/;
		var strx=/^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)[0-9][0-9]$/;
		var dt,mm,yy;
		if(strx.test(date1)==true)
	  		{
 				 mm=parseInt(date1.substr(0,2));
				 dt=parseInt(date1.substr(3,2));
				 yy=parseInt(date1.substr(6,4));
				 if(((mm==1)||(mm==3)||(mm==5)||(mm==7)||(mm==8)||(mm==10)||(mm==12))&&(dt<=31))
				   {
						return true;
				   }
				  if(((mm==4||mm==6||mm==9||mm==11))&&(dt<=30))
				   {
					return true;  
				   }
				   if(mm==2)
				   {
					 if((dt==29)&&((yy%4==0)&&((yy%100!=0)||yy%400==0)))
				      {
					  return true;
				      }
				     if(dt<=28)
				      {
					   return true;  
				      }
				   }
				   return false;
			}
			else
			{
				return false;
			}
}
//<---------------ends------------------->

//<------------starts-------------------->
//Comparison of two dates 
//Name of the function: function date comp(date1,date2,typex){}
//date1: date argument that should be supplied in “mm/dd/yyyy” format.
//date2: date argument that should be supplied in “mm/dd/yyyy”format.
//typex: types of comparison as follows:
//return value: returns  true if input parameter  string satisfied the input criteria else returns false.
//typex=0--->date1 equals date2
//typex=1--->date1 greaterthan date2
//typex=2--->date1 less date2
//typex=3--->date1 greaterthan or equals to date2
//typex=4--->date1 lessthan or equals to date2
function date_comp(date1,date2,typex)
{
//alert(date1);
//alert(date2);
if((test_date(date1)==true)&&(test_date(date2)==true))
	 {
		 var dt1=new Date(date1);
 		 var dt2=new Date(date2);
		   if((typex==0)) //(dt1==dt2)&&
		    {
			  if(dt1.toString()==dt2.toString() )
				{
				return true; 
				}
				else
				{
					return false;
				}
		    }
		 if(typex==1)
		   {
			   if(dt1>dt2)
		         {
				  return true; 
			     }
				else
			     {
					return false;
			     }
		   }

	if(typex==2)
		 {
			 if(dt1<dt2)
			   {
			   return true; 
		       }
			   else
			   {
				 return false;
			   }
		 }
		 if(typex==3)
		 {
			 if(dt1>=dt2)
			   {
			    return true; 
		       }
			   else
			   {
				 return false;
			   }
		 }
		 if(typex==4)
		 {
			 if(dt1<=dt2)
			   {
			    return true; 
		       }
			   else
			   {
				 return false;
			   }
		 }
	 }
	 else
	 {
		  return false; 
	 }
	
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks valid numbers only
//Name of the function:function test_num(elmnt)
//elmnt : set of positive or negative numbers (0 to 9) 
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_num(elmnt1)
{
	//var strx = /^-{0,1}\d*\.{0,1}\d+$/;
	var strx=/^-{0,1}\d+$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks valid numbers with decimal
//Name of the function: function test_num_dec(elmnt)
//elmnt : set of positive or negative  numbers (0 to 9) with or without decimal 
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_num_dec(elmnt1)
{
	var strx = /^-{0,1}\d*\.{0,1}\d+$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks of alphabets 
//Name of the function: function test_alphabet(elmnt)
//elmnt : set of English  alphabets including small and capital letters only. 
//With out special characters and white spaces.
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_alphabet(elmnt1)
{
	var strx = /^[a-zA-Z]+$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks of alphabets with space
//Name of the function: function test_alphabet_wsp(elmnt)
//elmnt : set of English  alphabets including small and capital letters with 
//white spaces only. No special characters considered
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_alphabet_wsp(elmnt1)
{
	var strx = /^[a-zA-Z ]+$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks of alphabets and numeric with space
//Name of the function: function test_alpha_num_wsp (elmnt)
//elmnt : set of English  alphabets including small and capital letters and numbers(0 to 9)  with white spaces. No special characters considered
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_alpha_num_wsp(elmnt1)
{
	var strx = /^[a-zA-Z 0-9]+$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->

//<------------starts-------------------->
//Checks of alphabets,numeric(except zero) and space
//Name of the function: function test_alpha_num_wsp_withoutzero(elmnt1)
//elmnt : set of English  alphabets including small and capital letters and numbers(1 to 9)  with white spaces. No special characters and zero considered
//return value: returns  true if input parameter string satisfied the input criterion else returns false.
function test_alpha_num_wsp_withoutzero(elmnt1)
{
	var strx = /^[a-zA-Z 1-9]+$/;
	return strx.test(elmnt1);
}


//<------------starts-------------------->
//Checks of alphabets,numeric(except zero) and space but with one alphabet or numeric valurs atleast
//Name of the function: function test_alpha_num_one_chr_atleast(elmnt1)
//elmnt1 : set of English  alphabets including small and capital letters and numbers(1 to 9)  with white spaces. No special characters and zero considered
//return value: returns  true if input parameter string satisfied the input criterion(atleast one alphabet or anumber at the string) else returns false.
function test_alpha_num_one_chr_atleast(elmnt1)
{
	//var strx = /^[a-zA-Z0-9][a-zA-Z 0-9,-.;\[\](){}]+$/;
	var strx = /[a-zA-Z][a-zA-Z 0-9-]*$/;
	return strx.test(elmnt1);
}
//<---------------ends------------------->
//<------------starts-------------------->
//Checks of alphabets,numeric and space
//Name of the function: function test_alpha_num_wsp_char(elmnt1)
//elmnt1 : set of English  alphabets including small and capital letters and numbers(1 to 9)  with white spaces. No special characters and zero considered
//return value: returns  true if input parameter string satisfied the input criterion else returns false.

function test_alpha_num_wsp_char(elmnt1)
{
	var strx = /^[a-zA-Z 0-9]+$/;
	if (strx.test(elmnt1))
	{
		var strx= /^[0-9]+$/;
		return strx.test(elmnt1);
	}
}
//<---------------ends------------------->

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;
    }
}
//<------------starts-------------------->
//Checks for empty and blank space
//Name of the function: function test_empty (elmnt)
//elmnt : can be empty or space only
//return value: returns  true if input parameter string satisfied the input criterion else returns false.

function test_empty(strValue)
{
	var strTemp = strValue;
   if(strTemp== 0)
	return true;
   else		
   	return false;
}
//<---------------ends------------------->
//<------------starts-------------------->
//Checks for valid e-mail format
//Name of the function: function test_email (elmnt)
//elmnt : can be a string containing valid e-mail id
//return value: returns  true if input parameter string satisfied the input criterion else returns false.

function test_email(strValue)
{
  //var objRegExp  =/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
    //var objRegExp  =/(^[a-z]([a-z_\.][a-z]*)@([a-z_\.]*)([.][a-z]{2,3})$)|(^[a-z]([a-z_\.][a-z]*)@([a-z_\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
    
	 var objRegExp=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	return objRegExp.test(strValue);
}
//<---------------ends------------------->
//<------------starts-------------------->
//Check for equality of values
//Name of the function: function test_equals(elmt1,elmt2,case_type)
//elmt1: string of any value;
//elmt2: string of any value;
//case_type: zero for case independent comparison else say one for case dependent comparison.
//return value: returns  true if both the parameter values are same else returns false.
function test_equals(elmt1,elmt2,case_type)
{		
		if(case_type==0)//zero for case independent comparison,other than zero say 1 is case dependent
		{
			elmt1=elmt1.toUpperCase();
			elmt2=elmt2.toUpperCase();
		}
				
		elmt1=strtrim(elmt1);
		elmt2=strtrim(elmt2);
		
		if(elmt1==elmt2)
		  {	   
		  return true;
		  }
		  else
		  {
		  return false;
		  }
}
//<---------------ends------------------->

//<------------starts-------------------->
//Check for multiple checkbox selection
//Name of the function: function test_select_n(chk_name,nos)
//chk_name: name of the checkbox array
//nos: indicates minimum no. of checkboxes to be selected
//return value: returns  true if at least ‘nos’ numbers of checkboxes are selected else returns false.

function test_select_n(chk_name,nos)
{
	//return chk_name.length;
	var lv_nos=0;
	for($i=0;$i<chk_name.length;$i++)
	{
		if(chk_name[$i].checked==true)
		{
			++lv_nos;
			//alert(chk_name[$i].checked);
		}
	}
	if(lv_nos>=nos)
	{
		return true;
	}
	else
	{
		return false;
	}
		
}
//<---------------ends------------------->

//<------------starts-------------------->
//Check for one radio button selection
//Name of the function: function test_select_one(radiox)
//radiox: name of the radio button group
//return value: returns  true if one radio button is selected else returns false.
function test_select_one(radiox)
{
	//alert("hello1");
	var lv_nos=0;
	for($i=0;$i<radiox.length;$i++)
	{
		if(radiox[$i].checked==true)
		{
			lv_nos++;
			break;
		}
	}
	if(lv_nos>0)
	{
		return false;
	}
	else
	{
		return true;
	}
		
}
//<---------------ends------------------->

//<------------starts-------------------->
//opens/redirectscontroll to different page
//Name of the function: function test_reset(formname)
//filename: name of the file where controll is redirected
function test_reset(filename)
{
window.location.href=filename;
}
//<---------------ends------------------->

//<------------starts-------------------->
//checks for valid percentage up to 100%
//Name of the function: function test_percentage(strValue)
//strValue: name of the string ,containing percentage figure
//return value: returns  true if percentage formt is valid else returns false.
function test_percentage(strValue)
{
	var len=2;
	var a=strValue;
	if (strValue>100)
	{
		return false;
	}
	else
	{
		if (strValue.indexOf(".")>=0)
		{
			//var strx = /^[0-9]{1,2}\.[0-9]{1,2}$/;
			var strx = /^[0-9]{1,2}\.[0-9]{1,2}$/;			
			return strx.test(strValue);
		}
		else
		{
			strx = /^[0-9]{1,3}$/;
			return strx.test(a);
		}
	}

}
//<---------------ends------------------->

//<------------starts-------------------->
//checks for valid time entry(in terms of hours andminutes)
//Name of the function: function  test_time(strValue)
//strValue: name of the string ,containing hours and minutes figure
//return value: returns  true if time formt is valid else returns false.

function test_time(strValue)
{
	var len=2;
	var a=strValue;
	if (isNaN(strValue))
	{
		alert("Please Enter only Number");
		return false;
	}
	if (strValue>24)
	{
		alert("Please Enter the Valid Time <=23");
		return false;
	}
	else
	{
		if (strValue.indexOf(".")>=0)
		{
			var strx = /^[0-9]{1,2}\.[0-9]{1,2}$/;
			if (strx.test(strValue))
			{
				var values = a.split(".");
				 if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) 
				 { 
				 	alert("Please Enter No. of hours <23");
				 	return false; 
				}
				if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) 
				{ 
					alert("Please Enter No. of Minutes <60");
					return false; 
				}
				else 
					return true;
			}
			
		}
		else
		{
			strx = /^[0-9]{1,2}$/;
			return strx.test(a);
		}
	}

}
//<---------------ends------------------->

//<------------starts-------------------->
//Ads a day to a given date in mm/dd/yyyy format
//Name of the function: test_add_day(date1)
//date1: date in mm/dd/yyyy format
//return value: returns  the same input date adding one date with it.

function test_add_day(date1)
{
	arr_date=date1.split("/");
	mm=arr_date[0];
	dt=arr_date[1];
	yy=arr_date[2];
   //newdate=new Date(yy, parseInt(mm)-1, parseInt(dt,10)+1);
     newdate=new Date(yy, parseInt(mm)-1, parseInt(dt,10)+1);
	 //str_new_date=newdate.getMonth()+'/'+newdate.getDate()+'/'+newdate.getYear();
	 newdate=newdate.getMonth()+'/'+newdate.getDate()+'/'+newdate.getYear();
   return newdate.toString();
  //return str_new_date.toString();
   
}
//<------------starts-------------------->
//Trims spaces from both the sides of a string
//Name of the function: strtrim(mystr)
//return value: returns  the same input string without spaces from both the sides.
function strtrim(mystr) 
{
	return mystr.replace(/^\s+/,'').replace(/\s+$/,'');
}
//<---------------ends------------------->







