function checkMinLength( fieldName, value, minLength )
{	
	var theError="";
	
	if ( value.length < minLength ) {
		if ( value.length == 0 )
			theError = "You must enter something in the " + fieldName + " field.\n";
		else {
			theError="The " + fieldName + " must be at least " + minLength + " character";
			if ( minLength != 1 )
				theError += "s";
			theError += " long.\n";
		}
	}
	return theError;
}

function checkMaxValue( fieldName, value, maxValue )
{	
	var theError="";
	
	if ( value > maxValue ) {
		theError="The " + fieldName + " cannot be more than " + maxValue + ".\n";
	}
	return theError;
}

function checkMinValue( fieldName, value, minValue )
{	
	var theError="";
	
	if ( value < minValue ) {
		theError="The " + fieldName + " cannot be less than " + minValue + ".\n";
	}
	return theError;
}

function checkEMail(address)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(address);
}

function checkPassword ( password, firstName, lastName ) {
	var theError="";
	var letters = /[a-zA-Z]/;
	var numbers = /[0-9]/;
		
	if ( password.length < 8 || !(numbers.test (password) && letters.test (password)) )
		theError = "The password must at least 8 characters long, including at least 1 letter and 1 number.\n";
	else if ( ( firstName.length > 0 && password.toLowerCase().indexOf ( firstName.toLowerCase() ) != -1 ) || ( lastName.length > 0 && password.toLowerCase().indexOf ( lastName.toLowerCase() ) != -1 ) )
		theError = "The password must not contain your name.\n";
	return theError;
}

function trimString(theString) {
  theString = theString.replace( /^\s+/g, "" );// strip leading
  return theString.replace( /\s+$/g, "" );// strip trailing
}

function uppercaseFirstLetter ( theField ) {
	theField.value = theField.value.substring(0,1).toUpperCase().concat(theField.value.substring(1));
}

function uppercase ( theField ) {
	theField.value = theField.value.toUpperCase();
}

function numbersOnly ( theString, allowSpaces ) {
	var filter;
	if ( allowSpaces )
		filter = /[^0-9 ]/;
	else
		filter = /[^0-9]/;
		
	return !filter.test(theString);
}

function checkCreditCardNumber( fieldName, value, allowSpaces )
{	
	var theError=checkMinLength( "credit card number", value, 16 );
	
	if ( !numbersOnly( value, allowSpaces ) ) {
		theError="The " + fieldName + " number can only contain numbers";
		if ( allowSpaces ) 
			theError += " and spaces";
		theError += ".\n";
	}
	return theError;
}

function checkNumbersOnly( fieldName, value )
{		
	var theError = "";
	
	if ( !numbersOnly( value, false ) ) {
		theError="The " + fieldName + " can only contain numbers.\n";
	}
	return theError;
}

function validExpirationDate ( theString ) {
	var filter = /[^0-9 \/]/;
		
	return !filter.test(theString);
}

function checkExpirationDate( value )
{	
	var theError=checkMinLength( "expiration date", value, 4 );
	
	if ( !validExpirationDate( value ) ) {
		theError="The expiration date can only contain numbers and a slash.\n";
	}
	return theError;
}

function getRadioValue (radioObject) {
	var	value = "";
	
	for ( var i=0; i<radioObject.length; i++ ) {
		if ( radioObject[i].checked ) {
			value = radioObject[i].value;
			break;
		}
	}
	
	return value;
}

function getSelectObjectValue (theObject) 
{
	return theObject.options[theObject.selectedIndex].value;
}

function getSelectObjectText (theObject) 
{
	return theObject.options[theObject.selectedIndex].text;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function scrollToAnchor ( anchorName, hOffset, vOffset ) {
	if ( anchorName ) {
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name == anchorName) 
			{ 
				window.scrollTo ( hOffset, findPosY(document.anchors[i]) + vOffset );
				break; 
			}
		}
	}
}
