
/*

<script language="JavaScript" src="../include/validation.js"></script>
<script Language="JavaScript">

function frmindex_Validate(frm)
{
	with (frm)
	{
		if
		(
			isNotEmpty(frm,txtCCNumber,'Please Enter Number !') && 
			isNumeric(frm,txtCCNumber,'Please Enter a Numeric Number !') && 
			isOfLength(frm,txtCCNumber,'Please Enter a Numeric 16-digit Number !',16) && 
			isValidEmail(frm,txtEmail,'Please Enter a Valid E-mail Address !') &&
			isSingleSel(frm,selAlpha,'Please Select Alphabet !') && 
			isMultipleSel(frm,selState,'Please Select atleast one State !')
		)
			return true;
		return false;
	}
}

</script>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="SetFocus('txtName')">

<form name="frmindex" method="post" action="" onSubmit="return frmindex_Validate(this);">

*/
// Discard space at starting and ending if any
function trim(str) 
{ 
    return str.replace(/^\s+|\s+$/g,''); 
}

function SetFocus(element)
{
	document.getElementById(element).focus();
}

//Level indepenncy
function levelInDep(le,en)
{
//alert('document.'+le.name+'.'+en.name);
	var res = eval('document.'+le.name+'.'+en.name);
	//alert(res);
	return res;	
}

// Check whether the value of an object is empty/null 
function isNotEmpty(frm,ctrl,msg)
{
	
	var obj = levelInDep(frm,ctrl);
	with (obj)
	{
		if (value==null || trim(value)=="")
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}
/*function isNotEmptyMultiple(frm,ctrls,msg)
{
	controls=ctrls.split(',');
	alert (ctrls);
	for(i=0;i<controls.lenghth;i++)
	{
		var obj = levelInDep(frm,controls[i]);
		with (obj)
		{
			if (value==null || trim(value)=="")
			{
				if (msg!="") 
					alert(msg);
				else
					alert("Validation Error ! ");
				focus();
				return false;
			}
			return true;
		}
	}
	
}
*/
// Check whether the value of an object is numeric
function isNumeric(frm,ctrl,msg)
{	
	var obj = levelInDep(frm,ctrl);
	with (obj)
	{
		if (isNaN(value) == true)
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}
// Check whether the value of an object is not numeric

function isNotNumeric(frm,ctrl,msg)
{	
	var obj = levelInDep(frm,ctrl);
	with (obj)
	{
		if (isNaN(value) != true)
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
	//return true;
}

// Check whether the value of an object is numeric and positive
function isPositiveNum(frm,ctrl,msg)
             {
				var obj = levelInDep(frm,ctrl);
				with (obj)
				{
					var regexp =  /^[0-9]+$/;
					if (regexp.test(trim(value)) != true)
					{
						if (msg!="") 
							alert(msg);
						else
							alert("Validation Error ! ");
						focus();
						return false;
					}
					return true;
				}
              }
			  
// Check whether the value of an object is numeric
function isOfMinLength(level,entered, alertbox,num)
{	
	var obj = levelInDep(level,entered);
	with (obj)
	{
		if (value.length < num)
		{
			if (alertbox!="") 
			{
				alert(alertbox);
			}
			focus();
			return false;
		}
		else 
		{
			return true;
		}
	}
}

// Check whether the value of an object is numeric
function isOfMaxLength(level,entered, alertbox,num)
{	
	var obj = levelInDep(level,entered);
	with (obj)
	{
		if (value.length > num)
		{
			if (alertbox!="") 
			{
				alert(alertbox);
			}
			focus();
			return false;
		}
		else 
		{
			return true;
		}
	}
}


// Check whether the value of an object is numeric
function isOfLength(level,entered, alertbox,num)
{	
	var obj = levelInDep(level,entered);
	with (obj)
	{
		if (value.length < num || value.length > num)
		{
			if (alertbox!="") 
			{
				alert(alertbox);
			}
			focus();
			return false;
		}
		else 
		{
			return true;
		}
	}
}

// Check whether the value of either of the two control blank or not
function isAtleastOneNotEmpty(frm,ctrl1,ctrl2,msg)
{
	var obj1 = levelInDep(frm,ctrl1);
	var obj2 = levelInDep(frm,ctrl2);
	with (obj1)
	{
		if (value=="" && obj2.value=="")
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}

// match control's value with another control's value.
function isMatched(frm,ctrl1,ctrl2,msg)
{
	var obj1 = levelInDep(frm,ctrl1);
	var obj2 = levelInDep(frm,ctrl2);
	with (obj1)
	{
		if (value!=obj2.value)
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}

// Check whether an Email address is valid
function isValidEmail(frm,ctrl,msg)
{	
	var obj = levelInDep(frm,ctrl);
	with (obj)
	{
		var regexp =  /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
		//var regexp = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
		//var regexp=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
		//var regexp =  /^(\d{5}(-\d{4})?|[a-z][a-z]?\d\d? ?\d[a-z][a-z])$/i;
		if (regexp.test(trim(value)) != true)
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}

// Check whether the something is selected in the list or not
function isSingleSel(level,entered, alertbox) 
{ 
//alert(level + " " + entered + " " + alertbox);
	var obj = levelInDep(level,entered);
	//alert(obj);
	with (obj)
	{
		if (selectedIndex != 0)
		{
			return true;
		}
		else 
		{
			if (alertbox!="") 
			{
				alert(alertbox);
			}
			focus();
			return false;
		}
	}
} 

// Check whether the something is selected in the multi select list or not
function isMultipleSel(level,entered, alertbox) 
{ 
	var obj = eval("document.getElementById('"+entered+"')");
	//var obj = levelInDep(level,entered);
	//obj+=+"[]";
	//alert(obj.value);
	//obj=obj.replace('[','');
	//obj=obj.replace(']','');
	with (obj)
	{
		if (selectedIndex != -1)
		{
			return true;
		}
		else 
		{
			if (alertbox!="") 
			{
				alert(alertbox);
			}
			focus();
			return false;
		}
	}
} 
// checks a uploadd file should be in specified extension
function get_ext(str)
{
	if(str != "")
	{
		len = str.length;
		dot = str.lastIndexOf('.');
		ext = str.substr(dot+1,len);
		return ext;
	}
	else
		return '';
}

function isNotEqual(frm,ctrl1,ctrl2,msg)
{
	var obj1 = levelInDep(frm,ctrl1);
	var obj2 = levelInDep(frm,ctrl2);
	with (obj2)
	{
		if (value!=obj1.value)
		{
			if (msg!="") 
				alert(msg);
			else
				alert("Validation Error ! ");
			focus();
			return false;
		}
		return true;
	}
}
// Check whether the uploaded file is in specified extension
function isUploadedFileValid(frm,ctrl1,str,msg)
{
	var obj1 = levelInDep(frm,ctrl1);
	//alert(obj1.value);
	if(obj1.value!="")
	{
		var type=get_ext(obj1.value);
		type=type.toLowerCase();
		var obj2 = str.split(',');
		for(i=0;i<obj2.length;i++)
		{
			if (trim(obj2[i])==trim(type))
			{
				return true;
			}
		}
		if (msg!="") 
			alert(msg);
		else
			alert("Validation Error ! ");
		focus();
		return false;
	}
	return true;
}
