var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function numbersOnly(element)
{
	element.value = element.value.replace(/[^0-9]/g, "");
}

function autoTab(input,len, e) {

numbersOnly(input)

var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if (e.keyCode==9 || e.keyCode==16)
{
	return true;
}

if (input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
	found = true;
	else
	index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
}
return true;
}

function checkspecial(keyCode)
{
	alert("inside check special");
	var var_return = false;
	return var_return;
}

function checkMaxLength(input, maxlen, e, statusLabelID)
{
    if(statusLabelID == undefined)
    {
     statusLabelID='textBoxStatus';
    }
	var var_return = true;
	if (input.value.length > maxlen)
	{
		var_return = false;
		var var_aKey_Special = [8,16,17,18,27,33,34,35,36,37,38,39,40,45,46,114];
		
		for (i=0; i< var_aKey_Special.length; i++)
		{ 
			if (e.keyCode == var_aKey_Special[i])
				{
				return true;  
				}
		}
		if (var_return == false)
		{
			var alertString = "max length of " + maxlen + " characters has been reached."
			if (e.keyCode == 13)
			{
				input.value = input.value.replace(new RegExp("\\n", "g"),"");
				return false;
			}
			else
			{
				input.value = input.value.substr(0, maxlen);
				alert(alertString);
				return false;
			}
		}
	}
	else
	{
		if (input.value.length == 0)
		{
			var statusHtml = "maximum of " + maxlen + " characters)"; 
			document.getElementById(statusLabelID).innerHTML= statusHtml;
		}
		else
		{
			var charLeft = (maxlen - input.value.length);
			var statusHtml = "maximum of " + maxlen + " characters, " + charLeft + " left)"; 
			document.getElementById(statusLabelID).innerHTML= statusHtml;
		}	
		return true;
	}
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function MM_openBrWindow(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function leapYearCheck(yearDDLId, monthDDLId, dayDDLId)
{
	var yearDDL = document.getElementById(yearDDLId);
	var monthDDL = document.getElementById(monthDDLId);
	var dayDDL = document.getElementById(dayDDLId);
	
	var year = yearDDL.value;
	var month = monthDDL.value;
	var isLeapYear = false;
	var Result = 0
	
	if ((year % 4) == 0)
	{
		// It is exactly divisible by 4
		if ((year % 100) == 0)
			{
			// It is exactly divisible by 100
			// Is it also exactly divisible by 400?
			Result = ( (year % 400) == 0);
			}
		else
			{
			Result = 1;
			}
	}
	else
	{
		// It is not exactly divisible by 4
		// It is not a leap year
		Result = 0;
	}
	return (Result);
}

function setDayDDLDays(yearDDLId, monthDDLId, dayDDLId)
{
	var yearDDL = document.getElementById(yearDDLId);
	var monthDDL = document.getElementById(monthDDLId);
	var dayDDL = document.getElementById(dayDDLId);
	
	var year = yearDDL.value;
	var month = monthDDL.value;
	var maxDays = 31;

	if (year == "") return;
	
	if (month == "") return;
					
	switch (month)
	{
		case "1": 
				maxDays = 31;
				break;
		case "2": 
				var isLeapYear = leapYearCheck(yearDDLId, monthDDLId, dayDDLId);
				if (isLeapYear == 0)
					{ maxDays = 28; break; }
				else
					{ maxDays = 29; break; }
				break;
		case "3": 
				maxDays = 31;
				break;
		case "4": 
				maxDays = 30;
				break;
		case "5":	
				maxDays = 31;
				break;
		case "6": 
				maxDays = 30;
				break;
		case "7": 
				maxDays = 31;
				break;
		case "8": 
				maxDays = 31;
				break;
		case "9": 
				maxDays = 30;
				break;
		case "10": 
				maxDays = 31;
				break;
		case "11": 
				maxDays = 30;
				break;
		case "12": 
				maxDays = 31;
				break;
		default:
			maxDays = 31;
			break;
	}

	//get total number of current options			
	var totOpt = dayDDL.options.length;
	//check to see if the list needs recreated
	if (totOpt != maxDays+1)
	{
		//clear the options
		for (j=0;j<totOpt;j++)
		{
		dayDDL.options[j] = null;
		}
		dayDDL.options.length = 0;
		
		
		//reload the options
		//add in the blank one first
		var optBlank = document.createElement("option");
		dayDDL.options.add(optBlank);
		optBlank.text = "DD";
		optBlank.value = "";
		
		//add in the days
		for (i=1; i <= maxDays; i++)
		{
			var opt = document.createElement("option");
			dayDDL.options.add(opt);
			opt.text = i;
			opt.value = i;
		}
	}
}