function notEmpty( field, msg, issetfocus )
{
	if(trimAll(field.value)=='' ) {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	return true;
}

function notEmptyCond( field )
{
	if( field.value=='' )
		return false;
	
	return true;
}

function trimAll(sString) //trimall function
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function rightTrim(sString) //righttrim function
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function leftTrim(sString) //lefttrim function
{
		while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
		return sString;
}
/*
var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
*/
/*function checkEmail(field, msg,issetfocus)
{
	val=field.value;
	if(val!="")
	{
	   Email = val;
       if ((Email.indexOf('@') < 0) || ((Email.charAt(Email.length-4) != '.') && (Email.charAt(Email.length-3) != '.')))
		{			
			alert(msg);
			field.focus();
			return false;
		}
	}
	return true;
}*/
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please Enter Valid Email Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please Enter Valid Email Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please Enter Valid Email Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please Enter Valid Email Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please Enter Valid Email Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please Enter Valid Email Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please Enter Valid Email Address")
		    return false
		 }

 		 return true					
	}
	
function checkEmail(field, msg,issetfocus)
{	
	//val=field.value;
	var emailID=field

	/*if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter Email ID")
		emailID.focus()
		return false
	}*/
	if (echeck(emailID.value)==false){
		//emailID.value=""
		//alert("Please Enter Valid Email Address");
		emailID.focus()
		return false
	}
	return true
}

function isValue( field, value )
{
	if( field.value==value )
		return true;
	return false;
}

function isNumber( field, reg, msg, issetfocus )
{
	var reg_exp = new RegExp(reg);
	if( field.value.search(reg_exp) < 0 ) {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	return true;
}

function isValideStr( field, reg, min, max, msg, issetfocus )
{
	var reg_exp = new RegExp(reg);
	
	var val = trim(field.value);
	
	if( val.length < min ) {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	if(  max>0 && val.length>max ) {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	//alert(reg);
	
	var regexp;
	
	regexp = new RegExp(reg,"i");
	//alert( val.match(regexp) );
	//return false;

	if( val.match(regexp)==null ) {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	return true;
}


function get_date_object(value, reg, format)
{
	var regexp;
	
	var val = trim(value);

	regexp = new RegExp(reg);
	var ro = val.match(regexp);
	
	if( ro==null ) {
		return null;
	}
	
	var d, m, y;
	
	if(format=="dd/mm/yyyy") {
		d = ro[1];
		m = ro[2];
		y = ro[3];
	}
	else if(format=="mm/dd/yyyy") {
		d = ro[2];
		m = ro[1];
		y = ro[3];
	}
	else {
		return null;
	}

	
	m = m - 1;
	var date = new Date(y,m,d);
	
	//alert( y + "   " + date.getFullYear() );
	//alert( m + "   " + date.getMonth() );
	//alert( d + "   " + date.getDate() );
	//alert( typeof(date) );
	
	if( typeof(date)!='object' || date.getFullYear()!=y || date.getMonth()!=m || date.getDate()!=d ) {
		return null;
	}
	
	return date;
}

function isDate( field, reg, format, msg, issetfocus)
{
	var date = get_date_object(field.value,reg,format);

	if( date==null)  {
		if( msg!=null ) {
			
			alert(msg);
			if( issetfocus )
				field.focus();
		}
		return false;
	}
	
	return true;
}

function compare_fileds(field, withf, type, reg, format, msg, issetfocus)
{
	if(type=='notmatch') {
		if(field.value!=withf.value) {
			return true;
		}
	}
	else if(type=='datematch' || type=='dateless' || type=='dategreater' ) {
		var date1 = get_date_object(field.value,reg,format);
		var date2 = get_date_object(withf.value,reg,format);
		
		if(date1!=null && date2!=null) {
			
			//alert(date1.toString()==date2.toString());
			//alert(date1 < date2);
			//alert(date1 > date2);
			
			if(type=='datematch' && date1.toString()==date2.toString()) {
				return true;
			}
			else if(type=='dateless' && date1 < date2) {
				return true;
			}
			else if(type=='dategreater' && date1 > date2) {
				return true;
			}			
		}
		else {
			//alert('failed');
			//alert(date1);
			//alert(date2);
		}
	}
	else {
		if(field.value==withf.value) {
			return true;
		}
	}	

	
	
	
	alert(msg);
	if( issetfocus )
		field.focus();
	return false;
}

function datevalidation(frmObj)
{

	if(frmObj.startdate.value != "" && frmObj.enddate.value != "")
	{
		
		var sdate = frmObj.startdate.value.split('/');
		var edate = frmObj.enddate.value.split('/');
		
		var statdate = sdate[2]+sdate[0]+sdate[1];
		var enddate = edate[2]+edate[0]+edate[1];
		
		var diff = (enddate - statdate);
		if(diff < 0)
		{
			alert("Please Select Proper Start And End Date");
			frmObj.startdate.focus();
			return false;
		}
	}
	else
	{		
		frmObj.submit();	
	}
		
	
}


