//----------------------------------------------------------------------//
//  FormCheck.js
//----------------------------------------------------------------------//
function ShowHowdy()
{
	alert('Howdy');
}

function removeBlanks(s)
{
	s = s.replace(/\s/g,''); // remove all whitespace
	return s;
}

function trimBlanks(fieldValue)
{
	while (fieldValue.charAt(0) == ' ')
		fieldValue = fieldValue.substring(1,fieldValue.length)
	return fieldValue;
}

function checkDecimals(fieldName, fieldValue)
{
	decimals = 2;  // how many decimals are allowed?
	
	if (isNaN(fieldValue))
	{
		alert("Enter a valid amount with " + decimals + " decimals.");
		fieldName.select();
		fieldName.focus();
		return false;
	}
	else
	{
		timeshundred = parseFloat(fieldValue * Math.pow(10, decimals));
		integervalue = parseInt(parseFloat(fieldValue) * Math.pow(10, decimals));
		if (timeshundred != integervalue)
		{
			alert("Enter a valid amount with " + decimals + " decimals.");
			fieldName.select();
			fieldName.focus();
			return false;
		}
	}
	return true;
}

function checkEmailAddress(fieldValue)
{
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

	if (!reg1.test(fieldValue) && reg2.test(fieldValue))
	{
		return true;
	}

	return false;
}

function checkTelNumber(fieldValue)
{
	var reg = /^\d{3}-?\d{3}-?\d{4}$/;	//valid phone number.

	if (!reg.test(fieldValue)) return false;
	return true;
}

function formatTelNumber(inVal)
{
	if (!checkTelNumber(inVal)) return inVal;
	var parts = inVal.split('-')
	switch (parts.length) {
		case 1:	return inVal.substr(0,3) + "-" + inVal.substr(3,3) + "-" + inVal.substr(6,4);
		case 2:	if (inVal.charAt(3)=="-")
					return inVal.substr(0,7) + "-" + inVal.substr(7,4)
				else
					return inVal.substr(0,3) + "-" + inVal.substr(3,8)
		case 3:	return inVal
	}
}

function checkZipCode(fieldValue)
{
	var reg = /^\d{5}$/;			//valid 5-digit zip code.

	if (!reg.test(fieldValue)) return false;
	return true;
}

function formatNumeric(num, decimals, commas, prefix, suffix)
{
	num = num.toString().replace(/\s/g,''); //remove all blanks.
	num = num.toString().replace(/\%|\$|\,/g,''); //remove all '$', '%', ','

	if (num.length == 0) return num;
	if (isNaN(num)) return num;

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	decs = num % 100;
	num = Math.floor(num/100).toString();
	if (decs < 10) decs = "0" + decs;
	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 (decimals)
		num = (((sign)?'':'-') + num + '.' + decs);
	else
		num = (((sign)?'':'-') + num);
	if (!commas)
		num = num.replace(/\,/g,'');
	if (prefix.length > 0)
		num = prefix + num;
	if (suffix.length > 0)
		num = num + suffix;
	return num;
}

function formatPercent(num, decimals)
{
	num = num.toString().replace(/\s/g,''); //remove all blanks.
	num = num.toString().replace(/\%|\$|\-|\,/g,''); //remove all '$', '%', ','

	if (num.length == 0) return num;
	if (isNaN(num)) return num;

	return parseFloat(num).toFixed(decimals);
}

function formatNumber(num, includeDecs)
{
	num = num.toString().replace(/\$|\%|\,/g,'');
	if (num.length == 0) return num;
	if (isNaN(num)) return num;

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	decs = num % 100;
	num = Math.floor(num/100).toString();
	if (decs < 10) decs = "0" + decs;
	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 (includeDecs)
		return (((sign)?'':'-') + num + '.' + decs);
	else
		return (((sign)?'':'-') + num);
}

function formatCurrency(num, includeCents)
{
	num = num.toString().replace(/\$|\,/g,'');
	if (num.length == 0) return num;
	if (isNaN(num)) return num;

	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 (includeCents)
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	else
		return (((sign)?'':'-') + '$' + num);
}

function checkDateOLD(inVal)
{
	var DateValue = inVal;
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;

	/* If a forward slash is used, there must be exactly 2 of them */
	t = DateValue.replace(/[^\/]/g,'');
	if (t.length != 0 && t.length != 2) return false;

	/* If foward slashes were used, reformat to mmddyyyy format. */
	if (t.length == 2)
	{
		parts = DateValue.split("/");
		if (parts[0].length == 1) {parts[0] = '0' + parts[0]};
		if (parts[1].length == 1) {parts[1] = '0' + parts[1]};
		DateValue = parts.join('');
	}

	/* Remove all chars except 0..9 */
	DateValue = DateValue.replace(/[^\d]/g,'');

	/* Always change date to 8 digits - string*/
	if (DateValue.length == 5)
	{
		if (DateValue.substr(3,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = '0' + DateValue.substr(0,3) + year + DateValue.substr(3,2);
	}
	if (DateValue.length == 6)
	{
		if (DateValue.substr(4,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = DateValue.substr(0,4) + year + DateValue.substr(4,2);
	}
	if (DateValue.length == 7)
	{
		DateValue = '0' + DateValue;
	}
	if (DateValue.length != 8)
	{
		err = 19;
	}

	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0)
	{
		err = 20;
	}

	/* Validation of month*/
	month = DateValue.substr(0,2);
	if ((month < 1) || (month > 12))
	{
		err = 21;
	}

	/* Validation of day*/
	day = DateValue.substr(2,2);
	if (day < 1)
	{
		err = 22;
	}

	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
	{
		leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29))
	{
		err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28))
	{
		err = 24;
	}
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12")))
	{
		err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11")))
	{
		err = 26;
	}

	/* if 00 ist entered, no error, deleting the entry */
	if ((day == 0) && (month == 0) && (year == 00))
	{
		err = 0; day = ""; month = ""; year = ""; seperator = "";
	}

	if (err == 0)
		return true;
	else
		return false;
}

function checkDate(inVal)
{
	var DateValue = inVal;
	var day;
	var month;
	var year;
	var leap = 0;

	/* If '/' is used, there must be exactly 2 of them */
	t = DateValue.replace(/[^\/]/g,'');
	if (t.length != 0 && t.length != 2) return false;

	/* If foward slashes were used, reformat to mmddyyyy format. */
	if (t.length == 2)
	{
		parts = DateValue.split("/");
		if (parts[0].length == 1) {parts[0] = '0' + parts[0]};
		if (parts[1].length == 1) {parts[1] = '0' + parts[1]};
		DateValue = parts.join('');
	}

	/* Remove all chars except 0..9 */
	DateValue = DateValue.replace(/[^\d]/g,'');

	/* Always change date to 8-digit string */
	if (DateValue.length == 5)
	{
		if (DateValue.substr(3,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = '0' + DateValue.substr(0,3) + year + DateValue.substr(3,2);
	}
	if (DateValue.length == 6)
	{
		if (DateValue.substr(4,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = DateValue.substr(0,4) + year + DateValue.substr(4,2);
	}
	if (DateValue.length == 7)
		DateValue = '0' + DateValue;

	if (DateValue.length != 8)
		return false;

	/* Get date parts */
	month = DateValue.substr(0,2);
	day = DateValue.substr(2,2);
	year = DateValue.substr(4,4);

	/* Check the year */
	if (year == 0)
		return false;

	/* Check the month. */
	if ((month < 1) || (month > 12))
		return false;
	if ((day == 31) && ((month == "04") || (month == "06") || (month == "09") || (month == "11")))
		return false;

	/* Check the day. */
	if ((day < 1) || (day > 31))
		return false;

	/* Check February  */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
		leap = 1;
	if ((month == 2) && (leap == 1) && (day > 29))
		return false;
	if ((month == 2) && (leap != 1) && (day > 28))
		return false;

	return true;
}

function formatDate(inVal, sep, zpad, yr4)
{
	var DateValue = inVal;
	var day;
	var month;
	var year;
	var leap = 0;

	/* If '/' is used, there must be exactly 2 of them */
	t = DateValue.replace(/[^\/]/g,'');
	if (t.length != 0 && t.length != 2) return inVal;

	/* If foward slashes were used, reformat to mmddyyyy format. */
	if (t.length == 2)
	{
		parts = DateValue.split("/");
		if (parts[0].length == 1) {parts[0] = '0' + parts[0]};
		if (parts[1].length == 1) {parts[1] = '0' + parts[1]};
		DateValue = parts.join('');
	}

	/* Remove all chars except 0..9 */
	DateValue = DateValue.replace(/[^\d]/g,'');

	/* Always change date to 8-digit string */
	if (DateValue.length == 5)
	{
		if (DateValue.substr(3,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = '0' + DateValue.substr(0,3) + year + DateValue.substr(3,2);
	}
	if (DateValue.length == 6)
	{
		if (DateValue.substr(4,2) > 60)
			year = '19'
		else
			year = '20'
		DateValue = DateValue.substr(0,4) + year + DateValue.substr(4,2);
	}
	if (DateValue.length == 7)
		DateValue = '0' + DateValue;

	if (DateValue.length != 8)
		return inVal;

	/* Get date parts */
	month = DateValue.substr(0,2);
	day = DateValue.substr(2,2);
	year = DateValue.substr(4,4);

	/* Check the year */
	if (year == 0)
		return inVal;

	/* Check the month. */
	if ((month < 1) || (month > 12))
		return inVal;
	if ((day == 31) && ((month == "04") || (month == "06") || (month == "09") || (month == "11")))
		return inVal;

	/* Check the day. */
	if ((day < 1) || (day > 31))
		return inVal;

	/* Check February  */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
		leap = 1;
	if ((month == 2) && (leap == 1) && (day > 29))
		return inVal;
	if ((month == 2) && (leap != 1) && (day > 28))
		return inVal;

	/* Pad single digit days & months with '0'? */
	if (!zpad && (month.substr(0,1) == '0'))
			month = month.substr(1,1);
	if (!zpad && (day.substr(0,1) == '0'))
			day = day.substr(1,1);

	/* 4-digit year? */
	if (!yr4)
		year = year.substr(2,2);

	return month + sep + day + sep + year;
}

function CheckTextLength(field, maxlimit)
{
	if (field.value.length > maxlimit)
	{
		alert('You have exceeded the maximum number of characters allowed in this field.\nThe maximum number is ' + maxlimit + '.');
		field.value = field.value.substring(0, maxlimit);
	}
}