/*

This function checks wheather the date given by dd mm yyyy is a valid date and
returns appropriate status

Input Parameters
	
	dd		:	Date of the month ( Numeric value )
	
	mm		:	Month ( Numeric value )
	
	yyyy	:	Year ( Numeric value )
				
	
Output Parameters

	status	:	Boolean value indicating the Validity of the Date
	
					True if valid
					
					false if not valid

Author		:	Karunakaran

Name of Module	:	Customer Administration ( Test Module )

Start Date	:	18/05/2000

End Date	:	19/05/2000

Revision	:	2

*/

function checkValidDate(dd,mm,yyyy)
{
	var upperLimit;
	
	if(mm<1 || mm>12)
		return false;
	
	if (mm==2)
	{
		if((yyyy % 4) ==0)
			upperLimit=29;
		else
			upperLimit=28;
	}
	else if (mm>=1 && mm<=7)
	{
		if((mm%2) == 0)
			upperLimit=30;
		else
			upperLimit=31;
	}
	else if (mm>7 && mm<=12)
	{
		if((mm%2) == 0)
			upperLimit=31;
		else
			upperLimit=30;
	}
	
	if (dd > upperLimit || dd <1) 
		return false;
	else
		return true;
}

/*

This function checks for the presence of given characters in the given string and
returns appropriate status

Input Parameters

	str	:Input string in which to search for characters ( Type : String )
	
	chrs	:	String of characters which are to be checked ( Type :String )
	
Output Parameters

	status	:	Boolean value indicating the Presence of Character
	
					True when any one is Present
					
					false when Nothing is Present

Author		:	Karunakaran

Start Date	:	18/05/2000

End Date	:	18/05/2000

Revision	:	1

*/

function checkChars(str,chrs)
{
	var count;
	for(count=0;count<chrs.length;count++)
	{
		if(str.indexOf(chrs.charAt(count)) != -1)
			return true;
	}
	return false;
}
	

function checkSpace(txtField,errMsg)
{
	var strField,i;
	strField=txtField.value;
	if(isSpace(strField))
	{
		alert(errMsg);
		txtField.focus();
		return false
	}
	else 
		return true;
}	

function isSpace(strField)
{
	for(i=0;i<strField.length;i++)
	{
		if(strField.charAt(i) != ' ')
		{
			return false
		}
	}
	return true
}

function checkMail(txtEmail,errMsg)
{
    var strEmail,tempStr,countI,countJ;
	strEmail="";
	tempStr=txtEmail.value;
	
	for(countI=0;countI<tempStr.length;countI++)
	{
		if(tempStr.charAt(countI)==' ') 
			continue;
		else
			break
	}
	
	for(countJ=tempStr.length-1;countJ>=0;countJ--)
	{
		if(tempStr.charAt(countJ)==' ') 
			continue;
		else
			break
	}
	strEmail=tempStr.substring(countI,countJ+1);
	
	var SPLCHARS;
	SPLCHARS="'~!#$%^&*()`:\"<>?,/|\\"
	if (checkChars(txtEmail.value,SPLCHARS) == true || countChar(txtEmail.value,"@") >1)
	{
		alert(errMsg);
		txtEmail.focus();
		return false;
	}
	

	if(strEmail.indexOf("@") == -1 || strEmail.indexOf(".") == -1 || strEmail.indexOf(" ") != -1 || strEmail.indexOf("@") > strEmail.lastIndexOf(".") || strEmail.lastIndexOf(".")==strEmail.length-1) 
	{
		alert(errMsg);
		txtEmail.focus();
		return false;
	}
	return true;
}

function checkName(txtName,errMsg)
{
	var SPLCHARS;
	SPLCHARS="'~!@#$%^&*()`1234567890:\"<>?,/|\\"
	
	if (checkChars(txtName.value,SPLCHARS) == true || isSpace(txtName.value))
	{
		alert(errMsg + "\nIt should not contain " + SPLCHARS);
		txtName.focus();
		return false;
	}
	return true;
}
		
function checkDate(txtDate,errMsg)
{
	var dtField,i,cnt;
	var dtArray;
	cnt=0;
	dtField=txtDate.value;
	dtArray=dtField.split("/");
	//alert(isNaN(dtArray[0]))
	if(isNaN(dtArray[0]) || isNaN(dtArray[1]) || isNaN(dtArray[2]) || (dtArray.length != 3)|| (dtField.length > 10) ||(dtArray[0]<1900) ||(dtArray[0]>2100)|| !checkValidDate(dtArray[2],dtArray[1],dtArray[0]))
	{
		//Invalid date
		alert(errMsg);
		txtDate.focus();
		return false;
	}
	return true;
}

function countChar(str,chr)
{
	var pos,cnt;
	cnt=0;
	while((pos=str.indexOf(chr)) != -1)
	{
		cnt++;
		str=str.substring(pos+1);
	}
	return cnt;
}

function checkLength(txtField,length,errMsg)
{
	if(txtField.value.length>length)
	{
		alert(errMsg);
		txtField.focus();
		return false;
	}
	return true;
}

function checkPresence(txtField,errMsg)
{
    var strField,i;	
	strField=txtField.value;
	if(trim(strField) == "")
	{
		alert(errMsg);
		txtField.focus();
		return false;
	}
	else 
		return true;
}	

function checkAlpha(txtField,errMsg)
{
	rgExp=/[^A-Za-z\s]/g
	if(!checkExpr(txtField.value,rgExp))
	{
	 alert(errMsg);
	 txtField.focus(); 	 
	 return false
	}
	else
	 return true;
}

function checkAlphaNumeric(txtField,errMsg)
{
	rgExp=/[^A-Za-z0-9]/g
	if(!checkExpr(txtField.value,rgExp))
	{
	 alert(errMsg);
	 txtField.focus(); 	 
	 return false
	}
	else
	 return true;
}

function checkUserName(txtField,errMsg)
{
	rgExp=/[^A-Za-z0-9_]/g
	if(!checkExpr(txtField.value,rgExp))
	{
	 alert(errMsg);
	 txtField.focus(); 	 
	 return false
	}
	else
	 return true;
}


// Evaluates the expression on the string "str" and returns 
// true if the validation passes
// false if it fails

function checkExpr(str,rgExp)
{
	var tmpStr
	tmpStr=str.replace(rgExp,"")
	if(str.length > tmpStr.length)
	 return false
	else
	 return true;
}

function checkDecimalNumber(txtField,errMsg)
	{		
		rgExp=/[^.0-9]/g
		if(!checkExpr(txtField.value,rgExp))
		{
		 	alert(errMsg);
		 	txtField.focus(); 	 
		 	return false;
		}
		else
		{
			return true;
		}
	}

// Evaluates whether the number entered is Negative decimal  or decimal number
// true if the validation passes
// false if it fails

function checkNegativeNumber(txtField,errMsg)
	{		
		rgExp=/[^-.0-9]/g
		if(!checkExpr(txtField.value,rgExp))
		{
		 	alert(errMsg);
		 	txtField.focus(); 	 
		 	return false;
		}
		else
		{
			return true;
		}
	}


function checkNumber(txtField,errMsg)
{
	rgExp=/\D/g
	if(!checkExpr(txtField.value,rgExp))
	{
	 alert(errMsg);
	 txtField.focus();
	 return false
	}
	else
	 return true;
}

function checkNumberval(txtField,errMsg)
{
	rgExp=/\D/g
	var SPLCHARS;
	SPLCHARS="'~!#$%^&*()`:\"<>?,/|\\"
	if ((checkChars(txtField.value,SPLCHARS) == true) &&	(!checkExpr(txtField.value,rgExp)))
	{
	 alert(errMsg);
	 txtField.focus(); 	 
	 return false
	}
	else
	 return true;
}

/* The following function can be used to test whether the given field is empty or if not emptry checks whether
it contains numbers 
  1) if the text field is empty and it returns true
  2) if the textfield has any content, then it checks whether it is number 
        i) if not a number , displayes the message.

Author:  KumaRaveL .A
Date:  18-04-2003
*/

function checkNullorContentIsOnlyNumber(txtField,errMsg)
{
    var strField,i;	
	strField=txtField.value;
	if(trim(strField) == "")
	{
		return true;
	}
	else 
	{
		rgExp=/\D/g
		if(!checkExpr(txtField.value,rgExp))
		{
		 alert(errMsg);
		 txtField.focus(); 	 
		 return false;
		}
		else
			 return true;
	
	}
}

/*
 The following function can be used to test whether the textfield is empty
  1) if  the text field is empty then it returns true
  2) else it returns false		
Author:  KumaRaveL .A
Date:  24-05-2003
*/

function checkEmpty(txtField)
{
    var strField,i;	
	strField=txtField.value;
	if(trim(strField) == "")
		return true;
	else 
		return false;
}	

/*
 The following function can be used to test whether the first date is greater than the second Date
  1) if the date1 is greater than date2 then the function returns true
  2) else it returns false		
Author:  KumaRaveL .A
Date:  24-05-2003
*/
function checkDate1GreaterthanDate2(txtD1,txtD2,errMsg)
{
	var	dtD1=txtD1.value;
		arrD1=dtD1.split("/");
	var dtD2=txtD2.value;
		arrD2=dtD2.split("/");	
		
	var year1=parseInt(arrD1[2]);
	var year2=parseInt(arrD2[2]);

	var month1=parseInt(arrD1[1]);
	var month2=parseInt(arrD2[1]);

	var day1=parseInt(arrD1[0]);
	var day2=parseInt(arrD2[0]);
	
	
		if (year1 < year2)
		{
			alert(errMsg);
			txtD1.focus();
			return false;	
		}
		else
		{
			if (year1 > year2)
			{}
			else if (year1 == year2)
			{						
				if (month1 > month2)
				{}
				else if (month1< month2)
				{
					alert(errMsg);
					txtD1.focus();
					return false;
				}
				else if (month1== month2)
				{
					if (day1>day2)
					{}
					else if (day1<=day2)
					{								
						alert(errMsg);
						txtD1.focus();
						return false;
					}
					
				}
			}
		return true;
		}
	}

/*
 The following function can be used to test whether the entered date is later than today
  1) if  the entered date is later than today it returs true
  2) else it returns false		
Author:  KumaRaveL .A
Date:  24-05-2003
*/
function checkDate1GreaterThanToday(txtD1,errMsg)
{
	
	datV1=txtD1.value;
	dtV1=datV1.split("/");
	var obj=new Date();
	
	var year=parseInt(dtV1[2]);
	var month=parseInt(dtV1[1]);
	var day=parseInt(dtV1[0]);
	
	var yearToday=parseInt(obj.getYear());
	var monthToday=parseInt(obj.getMonth());
	var dayToday=parseInt(obj.getDate());
	
		if (dtV1[2] < yearToday)
		{
			alert(errMsg);
			txtD1.focus();
			return false;	
		}
		else
		{
			if (dtV1[2] > yearToday)
			{			
			}
			else if (dtV1[2] ==yearToday)
			{						
				if (dtV1[1]>(monthToday+1))
				{				
				}
				else if (dtV1[1]<(monthToday+1))
				{
					alert(errMsg);
					txtD1.focus();
					return false;
				}
				else if (dtV1[1]==(monthToday+1))
				{
					if (dtV1[0]>(dayToday))
					{				
					}
					else if (dtV1[0]<=(dayToday))
					{								
						alert(errMsg);
						txtD1.focus();
						return false;
					}
				}
			}
		}	
	return true;
}

/*
 The following function can be used to test whether the entered date is later than today
  1) if  the entered date is later than today it returs true
  2) else it returns false		
Author:  KumaRaveL .A
Date:  24-05-2003
*/
function checkDate1GEToday(txtD1,errMsg)
{
	
	datV1=txtD1.value;
	dtV1=datV1.split("/");
	var obj=new Date();
	
	var year=parseInt(dtV1[2]);
	var month=parseInt(dtV1[1]);
	var day=parseInt(dtV1[0]);
	
	var yearToday=parseInt(obj.getYear());
	var monthToday=parseInt(obj.getMonth());
	var dayToday=parseInt(obj.getDate());
	
		if (dtV1[2] < yearToday)
		{
			alert(errMsg);
			txtD1.focus();
			return false;	
		}
		else
		{
			if (year > yearToday)
			{			
			}
			else if (year==yearToday)
			{						
				if (month>(monthToday+1))
				{				
				}
				else if (month<(monthToday+1))
				{
					alert(errMsg);
					txtD1.focus();
					return false;
				}
				else if (month==(monthToday+1))
				{
					if (day>=(dayToday))
					{				
					}
					else if (day<(dayToday))
					{								
						alert(errMsg);
						txtD1.focus();
						return false;
					}
				}
			}
		}	
	return true;
}


/*
 The following function can be used to test whether the textfield contains the value 0
  1) if  the text field contains 0,it displays the given message and returns false 
  2) else it returns true
Author:  KumaRaveL .A
Date:  28-05-2003
*/

function checkContentIsZero(txtField,errMsg)
{
    var strField,i;	
	strField=txtField.value;
	if(parseFloat(trim(strField)) == 0)
	{
		alert(errMsg);
		txtField.focus();
		return false;
	}
	else 
		return true;
}	


function checkNum()
		{
		  rgExp=/\D/g
		  if((!checkExpr(document.frmTestText.txt1.value,rgExp))&&(!checkExpr(document.frmTestText.txt2.value,rgExp)))
	 	    {   
			   return false 
			}
	      else
		    {
		       return true;
	        }  
		 }
		 
function checkPhoneNum(txtPhone,errMsg)
{
	var SPLCHARS;
	SPLCHARS="'~!@#$%^&*`abcdefghijklmnopqrstuvwxyzABCDEVGHIJKLMNOPQRSTUVWXYZ:\"<>?,/|\\"
	
	if (checkChars(txtPhone.value,SPLCHARS) == true || isSpace(txtPhone.value))
	{
		alert(errMsg + "\nIt should not contain '~!@#$%^&*`:\"<>?,/|\\ and alphabets");
		txtPhone.focus();
		return false;
	}
	return true;
}

function checkPhoneNumItaly(txtPhone,errMsg)
{
	var SPLCHARS;
	SPLCHARS="'~!@#$%^&*`abcdefghijklmnopqrstuvwxyzABCDEVGHIJKLMNOPQRSTUVWXYZ:\"<>?,/|\\"
	
	if (checkChars(txtPhone.value,SPLCHARS) == true || isSpace(txtPhone.value))
	{
		alert(errMsg + "\nNon dovrebbe contenere '~!@#$%^&*`:\"<>?,/|\ \ ed alfabeti");
		txtPhone.focus();
		return false;
	}
	return true;
}


function checkDOA(txtDOA,errMsg)
{
	var dtField,i,cnt;
	var dtArray;
	cnt=0;
	today = new Date () ;
	year=today.getYear()
	dtField=txtDOA.value;
	dtArray=dtField.split("/");
	//alert(isNaN(dtArray[0]))
	if(isNaN(dtArray[0]) || isNaN(dtArray[1]) || isNaN(dtArray[2]) || (dtArray.length != 3)|| (dtField.length > 10) ||(dtArray[2]<1900) ||(dtArray[2]>2100)|| !checkValidDate(dtArray[0],dtArray[1],dtArray[2]) || (dtArray[2]>year))
	{	
		alert(errMsg);
		txtDOA.focus();
		return false;
	}
	return true;
}

function checkEuroCurrency(txtField,errMsg)
{		
	rgExp=/^([0-9]+)(\,{1})([0-9]{2})$/
	if(!rgExp.test(txtField.value))
	{
		alert(errMsg);
		txtField.focus(); 	 
		return false;
	}
	else
	return true;
}
