//Script validation
/*
Function Name 	: File checking		
Argument 		: filename		
return type 	: boolean.
*/

function chk_file()
{
	var a1=chk_file.arguments;
	var i;
	for(i=0; i<(a1.length-1); i+=2)
	{
		if ((a1[i+1]==1) && (a1[i].value.length==0))
		{
			 alert("Please Upload a File");
			  a1[i].focus();
			 return false;
		}
	}
	source="."
	var j1;
	var cnt=0;
	for(i=0; i<(a1.length-1); i=i+2)
	{
		var a1=a1[i].value;
		for(j1=0;j1<a1.length;j1++)
		{
			c=a1.charAt(j1);
			if(source.indexOf(c)==0)
			{
				cnt=cnt+1;
			}
		}	
		if(cnt==0)
		{
			alert("Enter Proper File Name ");
			return(false);
		}	
		else
		{
			var newval=a1.substring(a1.length-4,a1.length)
			var newval2=a1.substring(a1.length-5,a1.length)
			if(newval==".JPG" || newval==".GIF" || newval==".jpg" || newval==".gif")
			{
				return(true);
			}
			if( newval2==".jpeg" || newval2==".JPEG" )
			{
				return(true);
			}
			else
			{
				alert("Enter Only GIF,JPG or JPEG files");
				return(false);
			}
		}
	}
}
/*
Function Name 	: Trim		
return type 	: boolean.
*/

function trim()
{
	var a=trim.arguments;
	a[0].value=a[0].value.replace(/(^\s*)/,"");   
	a[0].value=a[0].value.replace(/(\s*$)/,""); 
}

/* Function list 
Function Name 	: Email Validation		
Argument 		: Email field,[1/0]		
return type 	: boolean.
*/

function chk_Email()
{
	a=chk_Email.arguments;
	var emailid=a[0].value;
	
	if(emailid.match(/^[a-zA-z0-9\-\_\.]+[a-zA-z0-9\-\_]{1}@[a-zA-z0-9]+[a-zA-z0-9\.\-\_]?[a-zA-z0-9]+\.[a-zA-z]{2,4}$/)==null)
	{
		alert("Please enter valid Email Address");
		a[0].select();
		return false;
	}
	/*for(i=0; i<(a.length-1); i=i+2)
	{
		var emailid=a[i].value;
		if (emailid.length>0 ) 
			{
				var at = emailid.indexOf ('@', 0);
				var dot = emailid.indexOf ('.', 0);
				if ((at == -1) ||(dot == -1) || (dot < at)||(emailid.indexOf ('@', at+1) != -1)|| (emailid.charAt(dot+1)=='.') || (emailid.indexOf (' ', 0) != -1) || (emailid.match("^(.+)@(.+)$")==null) || (emailid.match("^(.+)@(.+)$")[1].match(/^\"?[\w-_\.]*\"?$/)==null)  || (emailid.match("^(.+)@(.+)$")[2].match(/^[\w-\.]*\.[A-Za-z]{2,4}$/)==null))
					{
					 alert("Please enter valid Email Address");
					 a[i].focus();
					 return false;
					} 
				else if ((emailid.substring(0,at).length== 0 ) || (emailid.substring(at+1,dot).length == 0)|| (emailid.substring(dot+1,dot+4).length==0))
				  {
					 alert("Please enter valid Email Address");
					 a[i].focus();
					 return false;
				  } 
				else
				   return true;
			}
		else if (a[i+1]==1)
			{
			  alert("Enter email id in the field");
			  a[i].focus();
			  return false;
			}
	}*/
	return true;	
}

/* Function list 
Function Name 	: Age Validation		
Argument 		: Age field,[1/0]		
return type 	: boolean.
*/

function chk_age()
{
	var a=chk_age.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Please Enter the Age");
			   a[i].focus();
			  return false;
			}
		else
		{		
			a[0].value=a[0].value.toString().replace(/(^\s*)?(\s*$)?/,"");
		}
			if ((a[i].value.match(/(^[0-9]{1,2}$)/)==null) && (a[i].value.length!=0))
			{
				alert("Enter Numbers only and Age should be less than 99");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: Currency		
Argument 		: Currency field,[1/0]		
return type 	: boolean.
*/


function chk_Currency()
{
	a=chk_Currency.arguments;
	for(i=0; i<(a.length-1); i=i+2){
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Amount");
			   a[i].focus();
			  return false;
			}else if (a[i].value.match(/^[0-9]*\.?\d{1,2}$/)==null)
			{
				alert("Enter the Valid Amount format");
			   	a[i].focus();	
			  	return false;
			}
	}
	return true;	
}

/*
Function Name 	: Phone(for those who choose US Country )		
Argument 		: Phone field,[1/0]		
return type 	: boolean.
*/

function chk_phone()
{
	var a=chk_phone.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Phone Number");
			   a[i].focus();
			  return false;
			}
			if ((a[i].value.match(/(^[0-9]{10}$)/)==null) && (a[i].value.length!=0)) 
			{
				alert("Enter the Valid Phone Number");
			 	a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: Number		
Argument 		: Phone field,[1/0]		
return type 	: boolean.
*/

function chk_cvcNumber()
{
	var a=chk_phone.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Number");
			   a[i].focus();
			  return false;
			}
			if ((a[i].value.match(/^[0-9]*$/)==null) && (a[i].value.length!=0)) 
			{
				alert("Enter the Valid Card Code");
			 	a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: Phone		
Argument 		: Phone field,[1/0]		
return type 	: boolean.
*/

function chk_phone1()
{
	var a=chk_phone1.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Phone Number");
			   a[i].focus();
			  return false;
			}
			if ((a[i].value.match(/^[0-9]*$/)==null) && (a[i].value.length!=0)) 
			{
				alert("Enter the Valid Phone Number");
			 a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: Integer Validation		
Argument 		: Text field,[1/0]		
return type 	: boolean.
*/

function chk_int()
{
	var a=chk_int.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Please Enter the Value");
			   a[i].focus();
			  return false;
			}
		else
		{		
			a[0].value=a[0].value.toString().replace(/(^\s*)/,"");   
			a[0].value=a[0].value.toString().replace(/(\s*$)/,""); 
		}
			if ((a[i].value.match(/(^[0-9]{1,2}$)/)==null) && (a[i].value.length!=0))
			{
				alert("Enter Numbers only and should be less than 99");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}
/*
Function Name 	: qty Validation		
Argument 		: Text field,[1/0]		
return type 	: boolean.
*/

function chk_qty()
{
	var a=chk_qty.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Please Enter the Value");
			   a[i].focus();
			  return false;
			}
		else
		{		
			a[0].value=a[0].value.toString().replace(/(^\s*)/,"");   
			a[0].value=a[0].value.toString().replace(/(\s*$)/,""); 
		}
		if(a[i].value==0)
		{
		alert("Please enter in the number of items you are trying to purchase");
			  	 a[i].focus();	
			  	return false;
		}
		else if ((a[i].value.match(/(^[0-9]{1,2}$)/)==null) && (a[i].value.length!=0))
			{
				alert("Please enter in the number of items you are trying to purchase");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: Zipcode Validation for US 		
Argument 		: Text field,[1/0]		
return type 	: boolean.
*/

function chk_zip()
{
	var a=chk_zip.arguments;
	//alert(a)
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Zip Code");
			   a[i].focus();
			  return false;
			}
			if ((a[i].value.match(/(^[0-9]{5}$)|(^[0-9]{9}$)/)==null) && (a[i].value.length!=0))
			{
				alert("Enter the Valid Zip Code");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
Function Name 	: ZipCode Validation		
Argument 		: Text field,[1/0]		
return type 	: boolean.
*/
function chk_zip1()
{
	var a=chk_zip1.arguments;
	//alert(a)
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Zip Code");
			   a[i].focus();
			  return false;
			}
			if ((a[i].value.match(/(^[0-9]{5,15}$)/)==null) && (a[i].value.length!=0))
			{
				alert("Enter the Valid Zip Code");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}
/*
Function Name 	: Date Validate		
Argument 		: Date field,[1/0]		
return type 	: boolean.
*/

function chk_date()
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
	var a=chk_date.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Date");
			  
			  return false;
			}
			
	var matchArray = a[i].value.match(datePat); // is the format ok?

	if (matchArray == null) {

	alert("Date is not in a valid format.")
	
	return false;

	}

	month = matchArray[1]; // parse date into variables

	day = matchArray[3];

	year = matchArray[4];

	if (month < 1 || month > 12) { // check month range

	alert("Month must be between 1 and 12.");
	
	return false;

	}

	if (day < 1 || day > 31) {

	alert("Day must be between 1 and 31.");
	
	return false;

	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {

	alert("Month "+month+" doesn't have 31 days!")
	
	return false;

	}

	if (month == 2) { // check for february 29th

	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

	if (day>29 || (day==29 && !isleap)) {

	alert("February " + year + " doesn't have " + day + " days!");
	
	return false;

	   }

	}
	}
	return true;

}

/*
4
Function Name 	: Positive Number		
Argument 		: Positive Number field,[1/0]		
return type 	: boolean.
*/
function chk_PNumber(){

	a=chk_PNumber.arguments;
	for(i=0; i<(a.length-1); i=i+2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Positive Number");
			  
			  return false;
			}else if (a[i].value.match(/^\+?[0-9]*$/)==null)
			{
				alert("Enter the Valid Positive  Number");
			  	
			  	return false;
			}
	}
	return true;
}

/*
4
Function Name 	: card Number		
Argument 		: card Number field,[1/0]		
return type 	: boolean.
*/
function chk_CNumber(){

	a=chk_CNumber.arguments;
	for(i=0; i<(a.length-1); i=i+2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Card Number");
			  a[i].focus();
			  return false;
			}else if (a[i].value.match(/^\+?[0-9]*$/)==null)
			{
				alert("Enter the Valid Positive  Number");
			  	a[i].focus();	
			  	return false;
			}
	}
	return true;
}
//Expiry date calulation for card
//month and year as parameter
function checkexdate(monthchoosen,yearchoosen)
{
if(monthchoosen.value != "")
	{
		var now=new Date();
		year=now.getYear();
		month=now.getMonth()+1;
		exyear=eval(yearchoosen.value)+2000;
		exmonth=eval(monthchoosen.value);
		if (monthchoosen.value=="")
		{
			alert("Select Month");
			monthchoosen.focus();
			return false;
		}
		if(exyear<year)
		{
			alert("Invalid Expiry date");
			yearchoosen.focus();
			return false;
		}
		if(exyear==year)
		{
			if(exmonth<month)
			{
				alert("Invalid Expiry date");
				monthchoosen.focus();
				return false;
			}
		}
		return true;
	}
}
/*
Function Name 	: Date Compare		
Argument 		: Start Date field,End Date field		
return type 	: boolean.
*/

function date_comp(){
	var a=date_comp.arguments;
	//alert(a[0].value)
	//alert(a[1].value);
	date1 = new Date();
	date2 = new Date();
	date1temp = new Date(a[0].value);
	date2temp = new Date(a[1].value);
	date1.setTime(date1temp.getTime());
	date2.setTime(date2temp.getTime());
	if( (date2.getTime() - date1.getTime()) < 0)
	{
		alert(" Date cannot be Lessthan Today's date");		
		return false;
	}
	else
	return true;
}
/*
Function Name 	: Time		
Argument 		: time field,[1/0]		
return type 	: boolean.
*/
function chk_time()
{
	var a=chk_time.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2)
	{
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Please Enter the time");
			   a[i].focus();
			  return false;
			}
		else
		{		
			a[0].value=a[0].value.toString().replace(/(^\s*)?(\s*$)?/,"");
		}
			/**///if ((a[i].value.match(/^([0-9]{1,2})(\:)(\d{1,2})(\sAM|\sPM|\sam|\spm)$/)==null) && (a[i].value.length!=0))
			if ((a[i].value.match(/^(0[1-9]|[0-9]|1[012])(\:)([0-5][0-9])(\sAM|\sPM|\sam|\spm)$/)==null) && (a[i].value.length!=0))
			{
				alert("Enter correct time in the format [01:15 AM]");
			  	 a[i].focus();	
			  	return false;
			}
	}
	return true;
}

/*
5
Function Name 	: Visa Card		
Argument 		: Positive Number field,[1/0]		
return type 	: boolean.
*/
function chk_visa(){
	var a=chk_visa.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
	alert((a[i].value.length==0));
	return false;
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Visa card Number");
			  return false;
			}
			if (a[i].value.match(/^[0-9]{13,16}$/)==null)
			{
				alert("Enter the Valid Visa card  Number");
			  	
			  	return false;
			}
	}
	return true;
}

/*
6
Function Name 	: Master Card		
Argument 		: Positive Number field,[1/0]		
return type 	: boolean.
*/

function chk_Master(){

	var a=chk_Master.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Master card Number");
			  
			  return false;
			}
			if (a[i].value.match(/^[0-9]{16}$/)==null)
			{
				alert("Enter the Valid Master card  Number");
			  	
			  	return false;
			}
	}
	return true;
}

/*
7
Function Name 	: American Express Card		
Argument 		: Positive Number field,[1/0]		
return type 	: boolean.
*/
function chk_A_Ex(){

	var a=chk_A_Ex.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the  American Express card Number");
			  
			  return false;
			}
			if (a[i].value.match(/^[0-9]{15}$/)==null)
			{
				alert("Enter the Valid American Express card  Number");
			  	
			  	return false;
			}
	}
	return true;	
}

/*
8
Function Name 	: Discover Card		
Argument 		: Positive Number field,[1/0]		
return type 	: boolean.
*/

function chk_Discover(){

	var a=chk_Discover.arguments;
	var i;
	for(i=0; i<(a.length-1); i+=2){
		
		if ((a[i+1]==1) && (a[i].value.length==0))
			{
			  alert("Enter the Discover card Number");
			  
			  return false;
			}
			if (a[i].value.match(/^[0-9]{16}$/)==null)
			{
				alert("Enter the Valid Discover card  Number");
			  	
			  	return false;
			}
	}
	return true;
}


/*
16
Function Name 	: chk_Bkslash		
Argument 		: field		
return type 	: boolean.
*/
function chk_Bkslash(obj)
{
var temp="";
var flg="true";
for(i=0;i<=obj.value.length-1;i++)
{
	if(obj.value.charAt(i)!="\\")
	{
		temp=temp +obj.value.charAt(i);	
	}
	else
			flg="false";
}
if (flg=="false")
	obj.value=temp;
}
