//This file contains the functions for input data validation
//at client side with the help of java script.

//  this function checks the email format is correct or not
//  and return true or false accordingly.
	function is_email(email)
	{
		if(!email.match(/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/))
			return false;
		return true;
	}
// End of is_email Function

//  this function checks the given number is signed/unsigned number
//  and return true or false accordingly.
	function is_number(number)
	{
		if(!number.match(/^[\-\+0-9e1-9]+$/))
			return false;
		return true;
	}
// End of is_number Function

// function used for phone number validation
	function is_validPhone(number)
	{
		if(!number.match(/^[0-9-]+$/))
			return false;
		return true;
	}
	// for valid zip code
	// function used for phone number validation
	function is_validZip(number)
	{
		if(!number.match(/^[0-9]+$/))
			return false;
		return true;
	}


	function is_validCost(number)
	{
		if(!number.match(/^[0-9.]+$/))
			return false;
		return true;
	}



//  this function checks the given number is unsigned number
//  and return true or false accordingly.
	function is_unsign_number(number)
	{
		if(!number.match(/^[\+0-9]+$/))
			return false;
		return true;
	}
	function is_double(number)
	{
		if(!number.match(/^[0-9]*\.?[0-9]*$/))
			return false;
		return true;
	}
// End of is_unsign_number Function
	
//  this function checks the given string is alphanumeric word or not
//  and return true or false accordingly.
	function is_alpha_numeric(str)
	{
		if(!str.match(/^[A-Za-z0-9 ]+$/))
			return false;
		return true;
	}
// End of is_alpha_numeric Function

//  this function checks the given string is empty or not
//  and return true or false accordingly.
	function is_empty(str)
	{
  		 str=trim(str);
		 if ((str.length==0)||(str==null))
			return true;
		 return false;
	}
// End of is_empty Function

// date format validation
function dateValidate(str) {
    var regStr = /^\d{4,4}-\d{1,2}-\d{1,2}$/; 
    if(str.match(regStr)) {
        return true;     
    } else {
        return false;
    }
}
	
	function trim(inputString) 
	{
	   inputString=inputString.replace(/^\s+/g,"");
	   inputString=inputString.replace(/\s+$/g,"");
	   return inputString;
	} // Ends the "trim" function

	function convertDate(d,dateformat)
	{
		if(dateformat==null)
			dateformat='dd-mm-yyyy';

		if(dateformat.match(/^dd[-\/]{1}mm[-\/]{1}yyyy$/i))
		{
			var T = d.split(/[-\/]/);
			var M = T[1];
			var D = T[0];
			var	Y = T[2];
		}
		else if(dateformat.match(/^yyyy[-\/]{1}mm[-\/]{1}dd$/i))
		{
			var T = d.split(/[-\/]/);
			var M = T[1];
			var D = T[2];
			var	Y = T[0];
		}
		else
			return d;

		return (M+"-"+D+"-"+Y);
	}

	function is_date(d,dateformat)
	{
		if(dateformat==null)
			dateformat='dd-mm-yyyy';

		if(!dateformat.match(/^mm[-\/]{1}dd[-\/]{1}yyyy$/i))
			d=convertDate(d,dateformat);

		if(d.search(/^(\d){1,2}[-\/\\](\d){1,2}[-\/\\]\d{4}$/)!=0)
			return -1;//Bad Date Format
		
		var T = d.split(/[-\/]/);
		var M = eval(T[0]);
		var D = T[1];
		var	Y = T[2];
	
		return D>0 && (D<=[,31,28,31,30,31,30,31,31,30,31,30,31][M] ||	D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) 
	}

	/// Usage : daetDiif(FirstDate,SecondDate,dateformat,returnas)
	/// returnas=null or 0 //Difrence will return in days
	/// returnas=null or 1 //Difrence will return in hours;
	/// returnas=null or 2 //Difrence will return in mins;
	/// returnas=null or 3 //Difrence will return in secs;
	/// returnas=null or 4 //Difrence will return in weeks;
	/// returnas=null or 5 //An array will return;


	function dateDiff(firstdate,secondate,dateformat,returnas)
	{
		date1 = new Date();
		date2 = new Date();
		diff  = new Date();
		
		firstdate=convertDate(firstdate);
		secondate=convertDate(secondate);

		if(is_date(firstdate,'mm-dd-yyyy')) 
		{ // Validates first date 
			date1temp = new Date(firstdate);
			date1.setTime(date1temp.getTime());
		}
		else
			return false; // otherwise exits

		if(is_date(secondate,'mm-dd-yyyy')) 
		{ // Validates second date 
			date2temp = new Date(secondate);
			date2.setTime(date2temp.getTime());
		}
		else
			return false; // otherwise exits

		// sets difference date to difference of first date and second date

		diff.setTime(date1.getTime() - date2.getTime());

		timediff = diff.getTime();
		
		if(returnas==null || returnas==0)
			return Math.floor(timediff / (1000 * 60 * 60 * 24)); 
		else if(returnas==1)
			return Math.floor(timediff / (1000 * 60 * 60)); 
		else if(returnas==2)
			return Math.floor(timediff / (1000 * 60)); 
		else if(returnas==3)
			return Math.floor(timediff / 1000); 
		else if(returnas==4)
			return Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
		else if(returnas==5)
		{
			weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
			timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

			days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
			timediff -= days * (1000 * 60 * 60 * 24);

			hours = Math.floor(timediff / (1000 * 60 * 60)); 
			timediff -= hours * (1000 * 60 * 60);

			mins = Math.floor(timediff / (1000 * 60)); 
			timediff -= mins * (1000 * 60);

			secs = Math.floor(timediff / 1000); 
			timediff -= secs * 1000;

			retval=new Array(weeks,days,hours,mins,secs);

			return retval; // form should never submit, returns false
		}
	}

	function isPastDate(firstdate,secondate,dateformat)
	{

		diff=dateDiff(firstdate,secondate);
		
		if(diff<0)
			return true;
		return false;
	}

	function isValidCreditCard(type, ccnum) 
	{
	   if (type == "Visa" || type == "VI") {
		  // Visa: length 16, prefix 4, dashes optional.
		  var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (type == "MasterCard" || type == "MC") {
		  // Mastercard: length 16, prefix 51-55, dashes optional.
		  var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (type == "Discover"  || type == "NO") {
		  // Discover: length 16, prefix 6011, dashes optional.
		  var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (type == "AmEx" || type == "AX") {
		  // American Express: length 15, prefix 34 or 37.
		  var re = /^3[4,7]\d{13}$/;
	   } else if (type == "Diners") {
		  // Diners: length 14, prefix 30, 36, or 38.
		  var re = /^3[0,6,8]\d{12}$/;
	   } else if (type == "Bankcard") {
		  // Bankcard: length 16, prefix 5610 dashes optional.
		  var re = /^5610-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (type == "JCB") {
		  // Bankcard: length 16, prefix 5610 dashes optional.
		  var re = /^[3088|3096|3112|3158|3337|3528]\d{12}$/;
	   } else if (type == "EnRoute") {
		  // Bankcard: length 15, prefix 5610 dashes optional.
		  var re = /^[2014|2149]\d{11}$/;
	   } else if (type == "Switch") {
		  // Bankcard: length 16, prefix 5610 dashes optional.
		  var re = /^[4903|4911|4936|5641|6333|6759|6334|6767]\d{12}$/;
	   }

	   if (!re.test(ccnum)) return false;
	   // Checksum ("Mod 10")
	   // Add even digits in even length strings or odd digits in odd length strings.
	   var checksum = 0;
	   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
		  checksum += parseInt(ccnum.charAt(i-1));
	   }
	   // Analyze odd digits in even length strings or even digits in odd length strings.
	   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
		  var digit = parseInt(ccnum.charAt(i-1)) * 2;
		  if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
	   }
	   if ((checksum % 10) == 0) return true; else return false;
	}

	//Checks the phone number like (001)-330-330 OR 9992592892
	///Start Function
	function is_phone(varphone)
	{
		if(!varphone.match(/^(\(?[0-9]*[-#\*\s]*[0-9]+\)?)+$/))
			return false;
		return true
	}
	//End Function

function value_length(obj,min,msg)
{
	var objvalue=trim(obj.value);
				if(objvalue.length<min)
				{
					alert(msg);
					obj.focus();
					return false;
				}

}
function mf_validation(obj,type,msg)
{	

			if (type=="P")
			{
		
				if(trim(obj.value)!="")
				{
					alert(msg);
					obj.focus();
					return false;
				}
			} // end if type B
		
			if (type=="B")
			{
		
				if(trim(obj.value)=="")
				{
					alert(msg);
					obj.focus();
					return false;
				}
			} // end if type B
			if (type=="N")
			{
				if(obj.value=="" || obj.value<=0 || isNaN(obj.value)==true)
				{
					alert(msg);
					obj.focus();
					return false;
				}

			} // end if type N

			if (type=="E")
			{
				if(emailCheck(obj.value)==false)
					{
						alert(msg);
						obj.focus();
						return false;
					}

			} // end if type E

} // END FUNCTION 

//END  V A L I D A T I O N    F U N T I O N 

// --- E M A I L  F U N C T I O N 
function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if(emailStr=="")
{ return true}
if (matchArray==null) {
 	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
     alert("The username doesn't seem to be valid.")
    return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
 	  for (var i=1;i<=4;i++){
	    	if (IPArray[i]>255) 	{
	        	alert("Destination IP address is invalid!")
			return false
	    				}
				}
    		return true	
		}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>7) {
   alert("The address must end in a valid domain, or two letter country.")
   return false
}
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}
return true;
}