
//====================================================================================================
//	File Name		:	validate.js
//----------------------------------------------------------------------------------------------------
//====================================================================================================
//	Function Name	:	IsEmpty
//	Purpose			:	checks whether a field has value or is blank, it returns false if a field
//						is empty otherwise true.
//----------------------------------------------------------------------------------------------------
function IsEmpty(fld,msg)
{
	if((fld.value == "" || fld.value.length == 0) && (msg == ''))
	{
		return false;
	}
	if(fld.value == "" || fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsSelected
//	Purpose			:	checks whether a option is selected, it returns false if a option
//						is selected otherwise true.
//----------------------------------------------------------------------------------------------------
function IsSelected(fld,msg)
{
	if((fld.value == "" || fld.value == "0" || fld.value.length == 0) && (msg == ''))
	{
		return false;
	}
	if(fld.value == "" || fld.value == "0" || fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsEmail
//	Purpose			:	checks Email validity. Email must have character @ followed by one or more
//						dots. It returns flase if Email is invalid otherwise true.
//----------------------------------------------------------------------------------------------------
function IsEmail(fld,msg)
{
	var regex = /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-zA-Z]{2,7}$/ ;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsValidString
//	Purpose			:	checks if field value contains only alphanumeric and '_' charactes. Also checks
//						that alphabetical chars. and '_' must have to be come first and followed by
//						numbers. It returns false if above conditions will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsValidString(fld,msg)
{
	var regex = /^[_]*[a-zA-Z_]+[a-zA-Z0-9_]+$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsPassword
//----------------------------------------------------------------------------------------------------
function IsPassword(fld,msg)
{
	var regex = /^[_]*[a-zA-Z]+[0-9]+[a-zA-Z0-9]*$/;
	if(!regex.test(fld.value))
  	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsLen
//	Purpose			:	checks if field value has number of characters between two specified limits.
//						It returns false if no. of chars. is < min. length or > max. length
//						otherwise true.
//----------------------------------------------------------------------------------------------------
function IsLen(fld, minlen, maxlen, msg)
{
	if(fld.value.length < minlen || fld.value.length > maxlen)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsCurrency
//	Purpose			:	checks if Currency value is in proper format i.e. ',' must be after 1(at first place)
//						or 3 digits also dot '.' must be followed by ',' . '$' is optinal as a first char.
//						It returns false if above condition will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsCurrency(fld,msg)
{
    val = fld.value.replace(/\s/g, "");

	regex = /^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$/;

    if(!regex.test(val)) {
        // alert(msg);
		 fld.focus();
		 return false;
    }
	return true;
}

//====================================================================================================
//	Function Name	:	IsPhone
//	Purpose			:	checks if phone field has following characters : 0-9, '-', '+', '(' , ')' .
//						It returns false if there are other than above characters otherwise true .
//	Parameters		:	fld1	-  area code to be checked
//					:	fld2	-  city code to be checked
//					:	fld3	-  actual phone no to be checked
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsPhone(fld1,fld2,fld3,msg)
{
	var regex = /^[\d-+()]+$/;

	var phone = "(" + fld1.value + ")" + fld2.value + "-" + fld3.value;
	if(!regex.test(phone))
	{
		//alert(msg);
		fld1.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsPhone
//	Purpose			:	checks if phone field has following characters : 0-9, '-', '+', '(' , ')' .
//						It returns false if there are other than above characters otherwise true .
//	Parameters		:	fld1	-  area code to be checked
//					:	fld2	-  city code to be checked
//					:	fld3	-  actual phone no to be checked
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsMyPhone(fld,msg)
{
	phone = fld.value; 
	phone = phone.split("-") ;
	fld1  = phone[0] ;
	fld2  = phone[1] ;
	fld3  = phone[2] ;

    if(fld1 == "undefined") {
		 fld1 ="";
	}

    if(fld2 == "undefined") {
		 fld2 ="";
	}

    if(fld3 == "undefined") {
		 fld3 ="";
	}

	//var regex = /^[\d-+()]+$/;
	var regex = /^\d{3}\-\d{3}\-\d{4}$/; 
	
	var phone =  fld1 + "-" + fld2 + "-" + fld3;

	if(!regex.test(phone))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsFax
//	Purpose			:	checks if phone field has following characters : 0-9, '-', '+', '(' , ')' .
//						It returns false if there are other than above characters otherwise true .
//	Parameters		:	fld1	-  area code to be checked
//					:	fld2	-  city code to be checked
//					:	fld3	-  actual phone no to be checked
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsFax(fld1,fld2,fld3,msg,fld)
{
	//var regex = /^[\d-+()]+$/;
	var regex = /^\d{3}\-\d{3}\-\d{4}$/; 
	
	var fax =  fld1 + "-" + fld2 + "-" + fld3;
	//alert(fld1);
	if(!regex.test(fax))
	{
		//alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsMobile
//	Purpose			:	checks if phone field has following characters : 0-9, '-', '+', '(' , ')' .
//						It returns false if there are other than above characters otherwise true .
//	Parameters		:	fld1	-  area code to be checked
//					:	fld2	-  city code to be checked
//					:	fld3	-  actual phone no to be checked
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsMobile(fld1,fld2,fld3,msg,fld)
{
	//var regex = /^[\d-+()]+$/;
	var regex = /^\d{3}\-\d{3}\-\d{4}$/; 
	
	var mobile =  fld1 + "-" + fld2 + "-" + fld3;
	//alert(fld1);
	if(!regex.test(mobile))
	{
		//alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsZip
//	Purpose			:	checks if zip field value is of length 5 or 9 . (for U.S. zip code).
//						It returns false if it contains alphabetic chars. or length is not as
//						specified.
//----------------------------------------------------------------------------------------------------
function IsZip(fld,msg)
{
	var num = /^[\d]+$/;

	if(!num.test(fld.value) || (fld.value.length !=5 && fld.value.length !=9))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsDate
//	Purpose			:	checks if date is valid according to month selected.
//						i.e. Feb must have 28 or 29 days and also April, June, Sept. and Nov. have
//						30 days. It returns false if above condition will not satisfy otherwise true.
//	Parameters		:	m	-  month field
//						d   -  day field
//						y   -  year field
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsDate(m,d,y,msg)
{
  var val1= m.value;
  var val2= d.value;
  var val3= y.value;
	if(val2 > daysInFebruary(val3) && val1 == 02)
	{
		alert(msg);
		d.focus();
		return false;
	}
	if((val1 == '04'|| val1 == '06' || val1 == '09' || val1 == '11' ) && (val2 > '30'))
	{
		alert(msg);
		d.focus();
		return false;
	}
  dt= val1 + '/' + val2 + '/' + val3;
  return true;
}
//====================================================================================================
//	Function Name	:	IsDate
//	Purpose			:	checks if date is valid according to month selected.
//						i.e. Feb must have 28 or 29 days and also April, June, Sept. and Nov. have
//						30 days. It returns false if above condition will not satisfy otherwise true.
//	Parameters		:	m	-  month field
//						d   -  day field
//						y   -  year field
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsMyDate(m,d,y,msg,fld)
{
  var val1= m;
  var val2= d;
  var val3= y;
 
	if(val2 > daysInFebruary(val3) && val1 == 02)
	{
	 
	//	alert(msg);
		fld.focus();
		return false;
	}
	if((val2 > 31)||(val1 >12))
	{
		//alert(msg);
		fld.focus();
		return false;
	}
	if((val1 == '04') || (val1 == '06') || (val1 == '09') || (val1 == '11' ) && (val2 > '30'))
	{
 
		//alert(msg);
		fld.focus();
		return false;
	}

  dt= val1 + '/' + val2 + '/' + val3;
  return true;
}
//====================================================================================================
//	Function Name	:	allDigits
//----------------------------------------------------------------------------------------------------
function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}
//====================================================================================================
//	Function Name	:	inValidCharSet
//----------------------------------------------------------------------------------------------------
function inValidCharSet(str,charset)
{
	var result = true;
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}
//====================================================================================================
//	Function Name	:	daysInFebruary
//	Purpose			:	To check days in Feb
//----------------------------------------------------------------------------------------------------
function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
//====================================================================================================
//	Function Name	:	checkExpDate
//	Purpose			:	Also it checks whether card is expired or not. It returns false if card is
//						expired otherwise true.
//----------------------------------------------------------------------------------------------------
function checkExpDate(fldmonth,fldyear,msg)
{
		var result = true;
 		var expired = false;
 		if (result)
 		{
 			var month = fldmonth.value;
 			var year = fldyear.value;

 			var now = new Date();
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
		}
		if (expired)
		{
 			result = false;
			fldmonth.focus();
 			alert(msg);
		}

	return result;
}
//====================================================================================================
//	Function Name	:	checkFileType
//	Purpose			:	It checks the file type. It must be either doc or pdf.
//----------------------------------------------------------------------------------------------------
function checkFileType(fld,msg)
{
	var regex = /(.doc|.pdf)$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	checkImageType
//	Purpose			:	It checks the image type. It must be either jpg or gif.
//----------------------------------------------------------------------------------------------------
function checkImageType(fld,msg)
{
	var regex = /(.jpg|.jpeg|.JPG.|JPEG|.gif.|.GIF)$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
s}

//====================================================================================================
//	Function Name	:	IsUrl
//	Purpose			:	It check that if url starts with http://
//----------------------------------------------------------------------------------------------------
function IsUrl(fld,msg)
{
	var regex = /^(http:\/\/|https:\/\/)/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsFileSize
//	Purpose			:	It ckecks the size of the image file.
//----------------------------------------------------------------------------------------------------
function IsFileSize(fld,msg)
{
	var img = new Image();
	img.src = fld.value;
//	alert('Dimensions:' + img.width + 'x' + img.height);
//	alert('File Size:' + img.fileSize);
	if(img.fileSize > 102400)
	{
		alert(msg);
		fld.focus();
		return false;
	}

	return true;
}
//====================================================================================================
//	Function Name	:	IsValidColor
//	Purpose			:	checks if field value contains only alphanumeric(a to f & 0 to 9). 
//						It returns false if above conditions will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsValidColor(fld,msg)
{
//	var regex = /[a-fA-F0-9][a-fA-F0-9]*/;
	var regex = /[a-fA-F0-9]+[a-fA-F0-9]*$/;
//	var regex = /*[a-zA-Z0-9]+[a-zA-Z0-9_]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsRadioBtnChecked
//----------------------------------------------------------------------------------------------------
function IsRadioBtnChecked(fld,msg)
{
	if(fld.length)
	{
		for(var i=0 ; i<fld.length ; i++ )
			if(fld[i].checked)
				return true;
		alert(msg);
		return false;
	}
	else
	{
		if(fld.checked)
			return true;
		alert(msg);
		return false;	
	}
	
}
//====================================================================================================
//	Function Name	:	IsCheckBoxChecked
//----------------------------------------------------------------------------------------------------
function IsCheckBoxChecked(fld,msg)
{
	if(fld.length)
	{
		for(var i=0 ; i<fld.length ; i++ )
			if(fld[i].checked)
				return true;
		alert(msg);
		return false;
	}
	else
	{
		if(fld.checked)
			return true;
		alert(msg);
		return false;	
	}
}
//====================================================================================================
//	Function Name	:	getcheckFieldLength
//----------------------------------------------------------------------------------------------------
function getcheckFieldLength(fld)
{
		var counter = 0;
		
		if(fld.length)
		{
			for(i=0 ; i<fld.length ; i++ )
				if(fld[i].checked)
					counter++;
		}
		else 	
		{
			if(fld.checked)
				counter++;
		}
		
	 return counter;	
}
//====================================================================================================
//	Function Name	:	unCheck
//----------------------------------------------------------------------------------------------------
function unCheck(fld)
{
	if(fld.length)
		{
			for(i=0 ; i<fld.length ; i++ )
				if(fld[i].checked)
					fld[i].checked = false;
		}
		else 	
		{
			if(fld.checked)
				fld.checked = false;
		}

		return true;
}
//====================================================================================================
//	Function Name	:	IsPriordate
//	Purpose			:	checks whether a given date is previos date
//----------------------------------------------------------------------------------------------------
function IsPriordate(fld,msg)
{
	today          = new Date();
	date           = today.getDate();
	month          = today.getMonth() + 1;
	year           = today.getFullYear();
	if(date < 10)
		  date = '0'+date;
	if(month < 10)
		  month = '0'+month;
		  
	var checkdate = '';
	checkdate = year+'-'+month+'-'+date;	  	
	
	if(fld.value < checkdate)
	{
		 alert(msg);
		 fld.focus();	
		 return false;
	}	
	return true;
}

