
//checks the validity of dates
function isDate(Ctrl) 
{
	Time = Date.parse(Ctrl.value);
	if(!Time)
	{
		Ctrl.focus();
		return false;
	}
	else
		return true;
}

// Returns true if the input is empty
function isEmpty(Ctrl)
{
	var val;
	val = Ctrl.value;

	// Strip all the whitespace from the input
	var buf;
	buf = "";
	var j;
	for(j=0; j < val.length; j ++)
	{
		var c;
		c = val.charAt(j);
		if( c==' ') {}
		else if( c=='\t') {}
		else if( c=='\r') {}
		else if( c=='\n') {}
		else buf += c;
	}

	if(buf.length == 0)
	{
		Ctrl.focus();
		return true;
	}
	else
		return false;
}

//checks to see if item is selected from the the drop down
function isSelected(Ctrl)
{
	if (Ctrl[0].selected)
	{
		return false;
	}
	else
		return true;
}



function makeDecimal (amount) 
{
	if (amount != null && amount != 0) 
	{
		var x = Math.round(parseFloat(amount) * 100); // round and make it an integer
		if (x < 100)
		{
			var d=0;
		}
		else
		{
			var d = parseInt(x / 100);     // the dollars amount
			var c = x % 100;               // the cents amount
			var g = (c >= 10)?"":"0";      // need extra "0" if cents is a single digit number
			amount = "" + d + "." + g + c  // construct the string thing
		}
		return amount
	}
}


// Returns true if the input contains a parsable Currency
function isCurrency(Ctrl) 
{
	var val;
	val = Ctrl.value;

	// Strip all the whitespace from the input
	var buf;
	buf = "";
	var j;
	for(j=0; j < val.length; j ++)
	{
		var c;
		c = val.charAt(j);
		if( c==' ') {}
		else if( c=='\t') {}
		else if( c=='\r') {}
		else if( c=='\n') {}
		else buf += c;
	}

	//If buf is empty then dump
	if(buf.length == 0)
	{
		Ctrl.focus();
		return false;
	}

	// Loop through all the characters 
	var i;
	for(i=0; i < buf.length; i ++)
	{
		var c;
		c = buf.charAt(i);
		
		// if the '$' is not at the begining then we've got a problem
		if(c=='$' && i != 0)
		{
			Ctrl.focus();
			return false;
		}
		
		// Make sure only valid currency characters are in our string
		if(!(c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' || c=='0' || c=='.' || c==',' || c=='$'))
		{
			Ctrl.focus();
			return false;
		}
	}
	return true;
}


// Returns true if the input contains a parsable number
function isNumber(Ctrl) 
{	
	var val;
	val = Ctrl.value;
	
	// Strip all the whitespace from the input
	var buf;
	buf = "";
	var j;
	for(j=0; j < val.length; j ++)
	{
	
		var c;
		c = val.charAt(j);
		if( c==' ') {}
		else if( c=='\t') {}
		else if( c=='\r') {}
		else if( c=='\n') {}
		else buf += c;
	}
	

	 if (isNaN(buf)==false) 
	{
		
		return true;	
	}
	else
	{
		Ctrl.focus();
		return false;
	}
}
//Checking to see if a check box has been checked or not
function isChecked(Ctrl)
{
	var bTemp = false;
	if (Ctrl.length > 0) 
	{
		for ( Count = 0; Count< Ctrl.length; Count ++) 
		{
			if (Ctrl[Count].checked)
			{	
				bTemp = true;
				break;
			}
		}
	}
	else if (Ctrl.checked == true) 
	{
		bTemp = true;
		return bTemp;
	}
		
	return bTemp;
}

//Check to see if the input contains a valid email address
function isEmail(Ctrl)
{
	var val;
	val = Ctrl.value;

	var atIndex;
	atIndex = val.indexOf("@",1);

	var lastDotIndex;
	lastDotIndex = val.lastIndexOf(".");


	if(val.length < 6) return false;	//must be at least 6 chars to be valid
	if(atIndex < 1) return false;		//'@' must be at least the second character
	if(lastDotIndex == -1)	return false;	//must have a dot
	if(lastDotIndex < atIndex) return false;// '.' must be after '@'

	return true;
}

// Validates date values entered from drop-down menus
function date_validate(monthControl, dayControl, yearControl, bRequired)
{
// It returns true if a date is valid and false otherwise (for example,
// June 31, Feb 30)
		 
// It is assumed that three different dropdown menus will be used... for the month, day, and year.
		 
// Function Arguments
// monthControl is the control field holding the integer value for the month
// dayControl is the control field holding the value for the day of the month
// yearControl is the control field holding the 'full' integer value for the year (ie, 2001 MUST be entered instead of 01)
// bRequired is a boolean value that is set to true if the date is required... otherwise it's set to false
		 
var nNumericMonth;
var nDateInMonth;
var nYear;
var nFebruary;
var nApril;
var nJune;
var nSeptember;
var nNovember;
var bLeapYear;

nFebruary = 2;
nApril = 4;
nJune = 6;
nSeptember = 9;
nNovember = 11;			
						
nNumericMonth = monthControl.value;
nDateInMonth = dayControl.value;
nYear = yearControl.value;								
		
//Check for null date values (or default selection)						
if (bRequired == true)	//if the date is required, check if the month, day, or year are NOT selected
{
	if (monthControl[0].selected || dayControl[0].selected || yearControl[0].selected)
	{
		return false;
	}				
}
else	//if the date is not required from the user, check for the following....
{
	// when no month, day, or year are selected... input is okay				
	if (monthControl[0].selected && dayControl[0].selected && yearControl[0].selected)
	{
		return true;
	}
	else //check if anyone of the controls is on the default choice
	{
		if (monthControl[0].selected || dayControl[0].selected || yearControl[0].selected)
		{
			return false;
		}				
	}
}			
// For Apr, Jun, Sept, Nov... check if date entered is 31
if (nNumericMonth == nApril || nNumericMonth == nJune || nNumericMonth == nSeptember || nNumericMonth == nNovember)
{
	if (nDateInMonth > 30) 
	{
		//alert('Invalid date');
		return false;
	}
}

// For feb, check if date entered is valid		
			
if (nNumericMonth == nFebruary)
{
	//check for a leap year
	if ((nYear % 4) == 0)
	{
		// Is it a century?
		if ((nYear % 100) == 0)
		{// If a century, must be evenly divisible by 400.
				if ((nYear % 400) == 0)
				{
				  bLeapYear = true;
				}
				else
				{
					bLeapYear = false;
				}
		}	
		else
		{
			bLeapYear = true;
		}
	}
	else
	{
		bLeapYear = false;
	}
	
	// check for valid feb dates, with or without leap year
	if (bLeapYear)
	{
		if (nDateInMonth > 29)					
		{
			return false;
		}
	}
	else
	{
		if (nDateInMonth > 28)					
		{
			return false;
		}
	}				
}
			
return true;	
}
