function round(s)
{
	var amount=new String(s);
	var index=amount.indexOf('.');
	if(index > 0)
	amount=amount.substring(0,index+3);
	return(amount);
}

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>3) {
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}
return true;
}

function trim(val)
{
	var a=new String(val.value);
	for(i=0;i < a.length;i++)
	{
		if(a.charAt(i)!=' ' && a.charAt(i)!=0) return;
	}
	val.value='';
}

function rtrim(val)
{
	var a=new String(val.value);
	for(i=a.length;i>0;i--)
	{
		if(a.charAt(i-1)==' ') { a=a.substring(0,(i-1)); val.value=a;}
		else return;
	}
}

function checkselect(f,fieldName)
{
	if(f.selectedIndex==0 || f.selectedIndex==-1)
	{
		alert('Please Select '+fieldName);
		f.focus();
		return false;
	}
	return true;
}

/*
function check_checkbox(f,g) validates whether any checkbox is checked
or not?F:is the reference of checkbox and g:is the message which
is to be displayed to the user.
*/

function check_checkbox(f,g)
{
var a=f.length;
var s=new String(a);
var flag=0;
if (s=='undefined')
	{
		if(f.checked==true) return true;
		else
		{
		alert(g+" should be checked");
		return false;
		}
	}
	else
	{
		for(i=0;i< a;i++)
		{
			if(f[i].checked==true)flag=1;
		}
	if(flag!=1)
		{
		alert(g+" should be checked");
		return false;
		}
	else
		return true;
	}
}

/*
function isDate(f) is the function to validate the date.
The only parameter f is the reference of select box

*/

function isDate(f) {

var dateStr='';
dateStr = f.value;

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/
var matchArray = dateStr.match(datePat)
if (matchArray == null) {
alert('Date is not in a valid format.')
return false
}
day = matchArray[1]
month = matchArray[3]
year = matchArray[4]
if (month < 1 || month > 12) {
alert('Month must be between 1 and 12.')
return false
}
if (day < 1 || day > 31) {
alert('Day must be between 1 and 31.')
return false
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!")
return false
   }
}
return true
}

/*

This function checkMaxPriceValue(f,s,fieldName) just checks the textbox for
maximum value.First parameter is the name of textbox and second is the max value size
is the message which you want to display to user.

*/

function checkMaxPriceValue(f,s,fieldName)
{
	var maxRupValue="";
	
	if(isNaN(f.value))
	{
		alert('Field ' + fieldName + ' should be numeric');
		f.focus();
		return false;
	}
	
	for(var iIndex=1;iIndex<=s;iIndex++)maxRupValue = maxRupValue + "9";
	maxRupValue = parseFloat(maxRupValue);
	maxRupValue = maxRupValue + 1;
	
	trim(f);
	if(f.value>maxRupValue)
	{
		alert('Field ' + fieldName + ' value should be less than ' + maxRupValue);
		f.focus();
		return false;
	}
	return true;
}

/*
This function checkEmpty(f,fieldName) just checks the textbox for
null value.First parameter is the name of textbox and second
is the message which you want to display to user.

*/

function checkEmpty(f,fieldName)
{
	trim(f);
	if(f.value=='')return false;
	
	return true;
}

/*
This function checknull(f,fieldName) validates the textbox for
null value.First parameter is the name of textbox and second
is the message which you want to display to user.

*/

function checkNull(f,fieldName)
{
	trim(f);
	if(f.value=='')
	{
		alert('Field '+fieldName+' should be specified');
		f.focus();
		return false;
	}
	return true;
}

/* This function is used for only accept Number Without Decimal */

function numberwithoutdec(f,fieldName)
{
	trim(f);
	var checkOK = "0123456789";
	var checkStr = f.value;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
					{	alert('Field '+fieldName+' Should be a Number Without Decimal')
						f.focus();
						return false;
						break;
					}
	}
	return true;
}

function alphanumeric_spaceless(f,fieldName)
{
	trim(f);
	var checkOK = "0123456789abcdefghijklmnopqrstuvwxyz";
	var checkStr = f.value;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		ch = ch.toLowerCase();
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
					{	alert('Field '+fieldName+' is Invalid')
						f.focus();
						return false;
						break;
					}
	}
	return true;
}

function alphanumeric(f,fieldName)
{
	trim(f);
	var checkOK = "0123456789abcdefghijklmnopqrstuvwxyz ";
	var checkStr = f.value;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		ch = ch.toLowerCase();
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
					{	alert('Field '+fieldName+' is Invalid')
						f.focus();
						return false;
						break;
					}
	}
	return true;
}



/*	This function is used for edit mode and it selects the correct date in select box
	Function ComboDateSelect is takes first parameter comboname:select name
	and other parameter is the value which is obtained from database
	using getDate method.

*/


N = (document.all) ? 0 : 1;
if (N) {
document.captureEvents(Event.KEYPRESS)
}
var ob;
var ty;
var len;


function textareavalidation(val,i)
{
	ty=val;
	len=i;
	ty.onkeydown=MY;
	ty.onkeypress=MY;
	ty.onmousedown=MY;
}

function MY(e)
{
	if(N)
	X=e.which
	else
	X=event.keyCode
	var v=new String(ty.value);
	if(X==0)
	{
		ty.value=v.substring(0,len-1);
		return false;
	}


	//if(X==222)return false;
	if(X==8 || X==9 || (X>36 && X<41) || X==46)return true;

	if(v.length>=len-1)
	{
		ty.value=v.substring(0,len-1);
		return false;
	}
	return true;
}

/*
This function LimitTextArea(txtareaname,len) checks the maximum length
of textarea.
*/

function LimitTextArea(txtareaname,len)
{
	var p = new String(txtareaname.value);
	var leng=p.length;
	if(leng>len)
	{
		alert("The maximum limit of this text area is "+len+ " characters and You have entered "+leng+" characters");
		return false;
	}
	return true;
}


function selection(comboname,val)
{
	v=comboname.options.length
	for(i=0;i < v;i++)
	{
		if(comboname.options[i].value==val)
			comboname.options[i].selected=true;
	}
}



function compareDate(d1,d2)
{
	var date1=d1[1].value+'/'+d1[0].value+'/'+d1[2].value;
	var date2=d2[1].value+'/'+d2[0].value+'/'+d2[2].value;
	var firstdate=new Date(date1);
	var seconddate=new Date(date2);
	if(firstdate>seconddate) return false;
	else
	return true;
}

function isEqual(d1,d2)
{
	var date1=d1[1].value+'/'+d1[0].value+'/'+d1[2].value;
	var date2=d2[1].value+'/'+d2[0].value+'/'+d2[2].value;
	var firstdate=new Date(date1);
	var seconddate=new Date(date2);
	//alert(firstdate);
	//alert(seconddate);
	if(firstdate!=seconddate) return false;
	else
	return true;
}
function isGreater(d1,d2)
{
	var date1=d1;
	var date2=d2;
	var firstdate=new Date(date1);
	var seconddate=new Date(date2);
	if(firstdate>seconddate) return true;
	else return false;
}


function checkdate(d,m,y) {

var day =  d;
var month= m;
var year = y;
if (month < 1 || month > 12) {
alert('Month must be between 1 and 12.')
return false
}
if (day < 1 || day > 31) {
alert('Day must be between 1 and 31.')
return false
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!")
return false
   }
}
return true
}




// Move value one list box into another list box
function MoveOption(objSourceElement, objTargetElement)
     {
   var aryTempSourceOptions = new Array();
   var aryTempTargetOptions = new Array();
   var x = 0;
   
   //looping through source element to find selected options
   for (var i = 0; i < objSourceElement.length; i++) {
    if (objSourceElement.options[i].selected) {
     //need to move this option to target element
     var intTargetLen = objTargetElement.length++;
     objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
     objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
    }
    else {
     //storing options that stay to recreate select element
     var objTempValues = new Object();
     objTempValues.text = objSourceElement.options[i].text;
     objTempValues.value = objSourceElement.options[i].value;
     aryTempSourceOptions[x] = objTempValues;
     x++;
    }
   }
   //sorting and refilling target list
   for (var i = 0; i < objTargetElement.length; i++) {
    var objTempValues = new Object();
    objTempValues.text = objTargetElement.options[i].text;
    objTempValues.value = objTargetElement.options[i].value;
    aryTempTargetOptions[i] = objTempValues;
   }
   aryTempTargetOptions.sort(sortByText);
   for (var i = 0; i < objTargetElement.length; i++) {
    objTargetElement.options[i].text = aryTempTargetOptions[i].text;
    objTargetElement.options[i].value = aryTempTargetOptions[i].value;
    objTargetElement.options[i].selected = false;
   }
   
   //resetting length of source
   objSourceElement.length = aryTempSourceOptions.length;
   //looping through temp array to recreate source select element
   for (var i = 0; i < aryTempSourceOptions.length; i++) {
    objSourceElement.options[i].text = aryTempSourceOptions[i].text;
    objSourceElement.options[i].value = aryTempSourceOptions[i].value;
    objSourceElement.options[i].selected = false;
   }
     }
     function sortByText(a, b)
     {
   if (a.text < b.text) {return -1}
   if (a.text > b.text) {return 1}
   return 0;
     }
     function selectAll(objTargetElement)
     {
   for (var i = 0; i < objTargetElement.length; i++) {
    objTargetElement.options[i].selected = true;
   }
   return false;
  }



