//====================================================================================================
//	File Name		:	validate.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Client side validation in JavaScript.
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//	Copyright		:	Copyrights © 2005 
//	Email			:	adnan10@gmail.com
//	History			:
//						Date				Author					Remark
//						07-Oct-2005			Adnan Sarela		Initial Release
//
//====================================================================================================

//====================================================================================================
//	Function Name	:	IsEmpty
//	Purpose			:	checks whether a field has value or is blank, it returns false if a field
//						is empty otherwise true.
//	Parameters		:	fld	-	field name to be checked
//					    msg -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
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	:	IsEmail
//	Purpose			:	checks Email validity. Email must have character @ followed by one or more
//						dots. It returns flase if Email is invalid otherwise true.
//	Parameters		:	fld	-	field name to be checked
//					    msg -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
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	:	IsEmails
//	Purpose			:	checks Email validity. Email must have character @ followed by one or more
//						dots. It returns flase if Email is invalid otherwise true.
//	Parameters		:	fld	-	field name to be checked
//					    msg -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
function IsEmails(fld,msg)
{
	var emails_array = fld.value.split(',');
	var x=0;
	var regex = /^[\w]+(\.[\w]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/ ;
	
	for (x=0; x < emails_array.length ; x++)
	{
		if(!regex.test(emails_array[x]))
		{
			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.
//	Parameters		:	fld	-	field name to be checked
//					    msg -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
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	:	IsAlphaString
//----------------------------------------------------------------------------------------------------
function IsAlphaString(fld,msg)
{
	var regex = /^[a-zA-Z]+$/;
	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.
//	Parameters		:	fld	-	field name to be checked
//					    msg -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
function IsPassword(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	:	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.
//	Parameters		:	fld	   - field name to be checked
//						minlen - minimum length of a field
//						maxlen - maximum length of a field
//					    msg    -   error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
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	:	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
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	07-Oct-2005
//----------------------------------------------------------------------------------------------------
function IsDate(m,d,y,msg)
{
  var val1= m.value;
  var val2= d.value;
  var val3= y.value;

	if(val2 > daysInFebruary(val3) && val1 == 2)
	{
		alert(msg);
		d.focus();
		return false;
	}
	if((val1 == 4 || val1 == 6 || val1 == 9 || val1 == 11 ) && (val2 > 30))
	{
		alert(msg);
		d.focus();
		return false;
	}

  dt= val1 + '/' + val2 + '/' + val3;
  return true;
}

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 allDigits(fld, msg)
{
	var regex = /^[0-9]+$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

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	:	hasQuotes
//	Purpose			:	It ckecks the Dimensions of the image file.
//	Parameters		:	fld -  field name containig image file name
//	Return			:	None
//	Author			:	Adnan Sarela
//	Creation Date	:	29-Apr-2003
//----------------------------------------------------------------------------------------------------
function hasQuotes(fld,msg)
{
	var regex = /'|"| /;
	if(regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsDimensions
//	Purpose			:	It ckecks the Dimensions of the image file.
//	Parameters		:	fld -  field name containig image file name
//	Return			:	None
//	Author			:	Adnan Sarela
//	Creation Date	:	29-Apr-2003
//----------------------------------------------------------------------------------------------------
function IsDimensions(fld,msg)
{
	var img = new Image();
	img.src = fld.value;
	//alert('Dimensions:' + img.width + 'x' + img.height);
	//alert('File Size:' + img.fileSize);
	if((img.width > 85 || img.width < 50) && (img.height > 100 || img.height < 75))
	{
		alert(msg);
		fld.focus();
		return false;
	}

	return true;
}

//====================================================================================================
//	Function Name	:	checkImageType
//	Purpose			:	It checks the image type. It must be either jpg or gif.
//	Parameters		:	fld -  field name containig image file name
//						msg -  error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	29-Apr-2003
//----------------------------------------------------------------------------------------------------
function checkImageType(fld,msg)
{
	//var regex = /(.jpg|.jpeg|.gif)$/;
	var regex = /(.jpg|.jpeg)$/;
	if(!regex.test(fld.value.toLowerCase()))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	checkMediaType
//	Purpose			:	It checks the media type. It must be either jpg or gif.
//	Parameters		:	fld -  field name containig image file name
//						msg -  error message to be displayed
//	Return			:	true or false
//	Author			:	Adnan Sarela
//	Creation Date	:	29-Apr-2003
//----------------------------------------------------------------------------------------------------
function checkMediaType(fld,msg)
{
	var regex = /(.mpe|.mpg|.mpeg|.mp3|.wma|.wmv|.ra|.rm|.ram)$/;
	if(!regex.test(fld.value.toLowerCase()))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function checkImage(fld,msg)
{
	var regex = /(.jpg|.jpeg)$/;
	if(!regex.test(fld.value.toLowerCase()))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function isNumericKey()
{
	if (window.event.keyCode < 48 || window.event.keyCode > 57)
	{
		window.event.keyCode = 0;
	} 
}
