// function to check whether the value is blank
// return true if value is blank
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
function isBlank(val) {
	if (val == null) { return true; }
	for (var i=0; i < val.length; i++) {
		if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n")) { return false; }
		}
	return true;
	}

///function to check whether the value is amount
function isAmount(num) {
	var string="1234567890.,";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}

///function to check whether the value is amount
function isDigit(num) {
	var string="1234567890,";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}

///function to check the whether the value is Integer
function isInteger(val,param1) 
{
	for (var i=0; i < val.length; i++) 
	{
		if(param1=="amount")
		{
			if (!isAmount(val.charAt(i))) 
			{ 
				return false; 
			}
		}
		else
		{
			if (!isDigit(val.charAt(i))) 
			{ 
				return false; 
			}
		}
	}
	return true;
}

// function to check the value is integer
function checkInt(val,param1)
{
	value=val.value
	if(param1=="amount")
	{
		ret=isInteger(value,"amount");
	}
	else
	{
		ret=isInteger(value)	;
	}
	ret1=isBlank(value);
	/////if value is left blank
	if(ret1==true)
	{
		if(param1=="amount")
		{
			val.value="0.00";
		}
		else
		{	
			val.value="0";	
		}
		val.focus();
	}
	else
	{
		//////else show the error
		if(ret==false)
		{
			if(param1=="amount")
			{			
				alert("Please enter Amount value");
				val.value="0.00";
			}
			else
			{	
				alert("Please enter numeric value");
				val.value="0";	
			}
			val.focus();
		}
		if(param1=="amount")
		{			
			value=formatCurrency(value,'amount');
			val.value=value;
		}
		else
		{
			val.value=value;
		}
	}		
}
///////function to make an Array
function makeArray(IntarrSize) {

  for (var n = 0; n < IntarrSize; n++)
    this[n] = "";

  return this;

}
/////////function equivalent to explode
function customSplit(strvalue, separator, arrayName) 
{
  var n = 0;

  if (separator.length != 0) {
    while (strvalue.indexOf(separator) != -1) {
      eval("arr"+n+" = strvalue.substring(0, strvalue.indexOf(separator));");
      strvalue = strvalue.substring(strvalue.indexOf(separator)+separator.length,
          strvalue.length+1);
      n++;
    }
    eval("arr" + n + " = strvalue;");
    arraySize = n+1;
  }
  else {
    for (var x = 0; x < strvalue.length; x++) {
      eval("arr"+n+" = \"" + strvalue.substring(x, x+1) + "\";");
      n++;
    }
    arraySize = n;
  }

  eval(arrayName + " = new makeArray(arraySize);");

  for (var i = 0; i < arraySize; i++)
    eval(arrayName + "[" + i + "] = arr" + i + ";");

  return arraySize;
}

/////FUNCTION TO FORMAT THE CURRENCY
function formatCurrency(num,param) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	if(param=='amount')
	{
		return (((sign)?'':'-') + num + '.' + cents);
	}
	else
	{
		return (((sign)?'':'-') + num);
	}
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
function isValidDate(dateObj) {

dateStr=dateObj.value;
if(dateStr=="")
	{
	return false;
	}
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("`"+dateStr + "` is not a valid date format.")
	dateObj.select();
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
dateObj.select();
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
dateObj.select();
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
dateObj.select();
return false;
}
if (month == 2) { // check for february 29th
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!");
dateObj.select();
return false;
   }
}
return true;
}


function dateDiff(first, second) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

size=customSplit(first, "/", "First") ;
size1=customSplit(second, "/", "Second") ;
date1temp = new Date(First[1]+"/"+First[0]+"/"+First[2]+" 00:00:00AM");
date1.setTime(date1temp.getTime());

date2temp = new Date(Second[1]+"/"+Second[0]+"/"+Second[2]+" 00:00:00AM");
//date2temp = new Date("10/10/2002 00:00:00AM");
date2.setTime(date2temp.getTime());

// sets difference date to difference of first date and second date

diff.setTime(date1.getTime() - date2.getTime());

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

//dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";
total=eval(eval(weeks * 7) + days);

return total; // form should never submit, returns false
}

