function getSelectedValue(vObject){
	if (vObject.tagName.toUpperCase() != 'SELECT'){
		alert('ERROR: [' + vObject.name + '] is not a SELECT List.');
		return false;
	}
	
	return vObject.options[vObject.selectedIndex].value;
}

function getSelectedText(vObject){
	if (vObject.tagName.toUpperCase() != 'SELECT'){
		alert('ERROR: [' + vObject.name + '] is not a SELECT List.');
		return false;
	}
	
	return vObject.options[vObject.selectedIndex].text;
}

function selectValue(vObject, vStr){
	if (vObject.tagName.toUpperCase() != 'SELECT'){
		alert('ERROR: [' + vObject.name + '] is not a SELECT List.');
		return false;
	}
	
	for (i=0; i < vObject.options.length; i++){
		if (vObject.options[i].value == vStr){
			vObject.options[i].selected = true;
			return true;
		}
	}
}

function disable(object, keepvalue){
	if (object.type == 'text'){
		if (!keepvalue){ object.value = ''; }
		object.style.background = '#CCCCCC';
		object.style.color = '#000000';
		object.disabled = true;
		return;
	}
	
	if (object.type == 'button'){
		object.style.background = '#999999;';
		object.style.color = '#CCCCCC';
		return;
	}
	
	if (object.type == 'checkbox'){
		object.disabled = true;
		if (!keepvalue) { object.checked = false; }
		return;
	}
	
	if (object.type == 'select-one' || object.type == 'select-multiple'){
		if (!keepvalue){ object.selectedIndex = -1; }
		object.style.background = '#CCCCCC';
		object.disabled = true;
		return;
	}
}

function enable(object, keepvalue, datetime){
	if (object.type == 'text'){
		if (!keepvalue){
			if (object.name.toUpperCase().indexOf("SSN") >= 0 ){	//reset SSN to ___-__-____
				object.value = '___-__-____';
			}else if (object.name.toUpperCase().indexOf("PHONE") >= 0){	//reset Phone to ___-____
				object.value = '___-____';
			}else if (object.name.toUpperCase().indexOf("TIME") >= 0){	//reset Time to __:__ __
				object.value = '__:__ __';
			}else if (object.name.toUpperCase().indexOf("DATE") >= 0){	//reset Date to mm/dd/yyyy
				object.value = 'mm/dd/yyyy';
			}else{
				object.value = '';
			}
		}
		object.style.background = '#FFFFFF';
		object.disabled = false;
		return;
	}
	
	if (object.type == 'button'){
		object.style.background = '#000099;'
		object.style.color = '#EFEFDE';
		return;
	}

	if (object.type == 'checkbox'){
		object.disabled = false;
		if (!keepvalue) { object.checked = false; }
		return;
	}

	if (object.type == 'select-one' || object.type == 'select-multiple'){
		if (keepvalue && object.selectedIndex < 0){ object.selectedIndex = 0; }
		if (!keepvalue){ object.selectedIndex = 0; }
		object.style.background = '#FFFFFF';
		object.disabled = false;
		if (!keepvalue) { object.selectedIndex = 0; }
		return;
	}
}

function isNumber(vStr,bAllowNull,vLen){
	var n;
	//check for blank field
	if (!bAllowNull && vStr.length == 0){ return false; }
	if (bAllowNull && vStr.length == 0){ return true; }
	
	if (vStr.length != vLen){ return false; }

	//loop through all characters to see if they're 0-9
	for (i=0; i < vStr.length; i++){
		n = vStr.charCodeAt(i);
		if (n < 48 || n > 57){ return false; }
	}

	//return true if we made it this far
	return true;
}

function LTrim(vStr){
	if (typeof(vStr) == 'object'){ vStr = vStr.value; }
	if (vStr.length != 0){
		while (vStr.charAt(0) == ' '){
			vStr = vStr.substring(1,vStr.length);
		}
	}
	return vStr;
}

function RTrim(vStr){
	if (typeof(vStr) == 'object'){ vStr = vStr.value; }
	if (vStr.length != 0){
		while (vStr.charAt(vStr.length-1) == ' '){
			vStr = vStr.substring(0,(vStr.length-1));
		}
	}
	return vStr;
}

function trim(vStr){
	if (typeof(vStr) == 'object'){ vStr = vStr.value; }
	vStr = LTrim(vStr);
	vStr = RTrim(vStr);
	return vStr;
}

function checkComment(form){
	var string = form.txtComment.innerText;
//	var string = vbTrim(form.txtComment.innerText);
//	if (string == ''){ return false; }
	form.comment.value = string;
	form.txtComment.innerText = '';
	return true;
}

function checkTime(object, on, validate, dObject){
	var x,hmt;
	var t1,t2,t3,d1,d2,d3;
	var now = new Date();
		
//	if (is_Disabled(object)){ object.blur(); return; }
		
	object.value = object.value.toUpperCase();
	if (on){	//FOCUS
		while (object.value.indexOf("_") >= 0){ x = object.value.indexOf("_");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		while (object.value.indexOf(":") >= 0){ x = object.value.indexOf(":");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		while (object.value.indexOf(" ") >= 0){ x = object.value.indexOf(" ");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		t1 = object.value.substring(0,2);
		t2 = object.value.substring(2,4);
		t3 = object.value.substring(4,6);
		object.value = t1 + t2 + t3;
	}else{		//BLUR
		hmt = "______";
		object.value = object.value + hmt.substring(object.value.length, hmt.length);
		t1 = object.value.substring(0,2);
		t2 = object.value.substring(2,4);
		t3 = object.value.substring(4,6);
		object.value = t1 + ":" + t2 + " " + t3;
		if (object.value == "__:__ __"){ return; }
		if (object.value.indexOf("_") > -1){
			alert("Enter a valid date. The date must be in \"hhmmtt\" format.");
			object.focus();
			return;
		}
		
		if (t1 < 1 || t1 > 12){
			alert('Enter a valid hour. (01 to 12)');
			object.focus();
			return;
		}
		if (t2 < 0 || t1 > 59){
			alert('Enter a valid minute. (00 to 59)');
			object.focus();
			return;
		}
		
		if (validate){
			d1 = dObject.value.substring(0,2);
			d2 = dObject.value.substring(3,5);
			d3 = dObject.value.substring(6,10);
			if (t3 == "PM"){
				if (t1 != 12){ x = (t1-12)+24; }
				else { x = t1; }
			}else{
				if (t1 != 12){ x = t1; }
				else { x = 00; }
			}
			var t_date = new Date(d3, (d1 - 1), d2, x, t2, 00);
			if (t_date > now){
				vbAlert('Enter a valid Time.  This date/time is in the future.');
				object.focus();
				return;
			}
		}
	}
}

function checkDate(object, on, validate){
	var x,mdy;
	var d1,d2,d3;

//	if (is_Disabled(object)){ object.blur(); return; }
	object.value = object.value.toLowerCase();
	if (on){		//FOCUS
		while (object.value.indexOf("m") >= 0){ x = object.value.indexOf("m");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		while (object.value.indexOf("d") >= 0){ x = object.value.indexOf("d");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		while (object.value.indexOf("y") >= 0){ x = object.value.indexOf("y");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		while (object.value.indexOf("/") >= 0){ x = object.value.indexOf("/");
			object.value = object.value.substring(0,x) + object.value.substring(x+1,object.value.length); }
		d1 = object.value.substring(0,2);
		d2 = object.value.substring(2,4);
		d3 = object.value.substring(4,8);
		object.value = d1 + d2 + d3;
	}else{			//BLUR
		mdy = "mmddyyyy";
		object.value = object.value + mdy.substring(object.value.length, mdy.length);
		d1 = object.value.substring(0,2);
		d2 = object.value.substring(2,4);
		d3 = object.value.substring(4,8);
		object.value = d1 + "/" + d2 + "/" + d3;
		if (object.value == "mm/dd/yyyy"){ return; }
		if (object.value.indexOf("m") > -1 || object.value.indexOf("d") > -1 || object.value.indexOf("y") > -1){
			alert("Enter a valid date.  The date must be in \"mmddyyyy\" format.");
			object.focus();
			return;
		}

		if (d1 < 1 || d1 > 12){
			alert('Enter a valid month. (01 - 12)');
			object.focus();
			return false;
		}
		if (!checkday(d3, d1, d2)){
			alert('Enter a valid day. (01 - 28,29,30,31 depending)');
			object.focus();
			return false;
		}

		if (d3.substring(0,2) != 19 && d3.substring(0,2) != 20){
			alert('Enter a valid year.\nThe year must be 19__ or 20__.');
			object.focus();
			return false;
		}
		if (validate){
			var tdate = new Date(d3, (d1 - 1), d2);
			
			d1 = object.minDate.substring(0,2);
			d2 = object.minDate.substring(3,5);
			d3 = object.minDate.substring(6,10);
			var now = new Date(d3, (d1 - 1), d2);
				if (tdate < now){
				vbAlert('You can not set the order date back to an earlier date than the last one saved in the database.\n\nPlease enter a date greater than or equal to the last saved date.');
				object.focus();
				return false;
			}
		}
	}
}

function checkday(checkYear, checkMonth, checkDay){
	var maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	if (checkDay <= maxDay){ return true; }
}

function checkDollar(object, on, roundonly){
	if (object.value.length <= 0){ object.value = 0; }
	
	if (on){		//FOCUS
		takeCommas(object);
	}else{			//BLUR
		addCommas(object);
	}
}

function fixInput(object, type){ // numbers, caps, time, money, money_allow_negative){
	var intKeyCode;
	var eventWindow = this;

	if (!event){
		eventWindow = document.fMain;
	}
	intKeyCode = eventWindow.event.keyCode;

	if (type == 'numbers'){
		if (intKeyCode < 48 || intKeyCode > 57){
//			if (intKeyCode != 45){ window.event.returnValue = false; }
			eventWindow.event.returnValue = false;
		}
	}

	if (type == 'money' | type == 'money_allow_negative'){
		if (intKeyCode == 46 && object.value.indexOf('.') > 0){ eventWindow.event.returnValue = false; }
		if (intKeyCode == 45 && (object.value.indexOf('-') > 0 || type != 'money_allow_negative')){ eventWindow.event.returnValue = false; }
		if ((intKeyCode < 48 || intKeyCode > 57) && intKeyCode != 46 && intKeyCode != 45){
				eventWindow.event.returnValue = false;
		}
	}
		
	if (type == 'caps'){
		object.value = object.value.toUpperCase();
	}
		
	if (type == 'time'){
		if ( (intKeyCode < 48 || intKeyCode > 57) && intKeyCode != 97 && intKeyCode != 65 && intKeyCode != 112 && intKeyCode != 80 && intKeyCode != 77 && intKeyCode != 109){
			eventWindow.event.returnValue = false;
		}
	}
}

// --------------  Highlight Functions  -------------- //
aH1 = new Array();
aH2 = new Array();
aH3 = new Array();
aH4 = new Array();
aH5 = new Array();
aH6 = new Array();
aHighlighted = new Array(aH1,aH2,aH3,aH4,aH5,aH6);

function checkHighlighted(which,allowMultiple){
	if (!which){ which = 0; }

	if (aHighlighted[which].length < 1){
		alert("Please make a selection from the list before clicking this button.");
		return false;
	}
	if (!allowMultiple && aHighlighted[which].length > 1){
		alert("You have more than one record highlighted.  Please highlight only one file from the list.");
		return false;
	}
	return true;
}

function clearHighlighted(which){
	if (!which){ which = 0; }
	for (i=0; i < aHighlighted[which].length; i++){
		aHighlighted[which][i].style.background='';
		aHighlighted[which][i].style.color='#000000';
	}
	aHighlighted[which].length=0;
}

// if scriptedFlag is TRUE, then the object is being passed in, otherwise, just capture the object from the event
// vEvent is passed in from objects that had the "highlight" function attached as an event behavior
//function highlight(event,scriptedFlag,object){
function highlight(vEvent,vScriptedFlag,vObject){
	var which, evalme;

	if (vScriptedFlag){
		// the script passed in the object
		object = vObject
	}else{
		// the script did not pass in the object, so find it based on the event
		object = vEvent.srcElement;
		//the event could be a <td>.  if so, then grab it's parent element
		if (object.tagName == 'TD'){ object = object.parentElement; }
	}

	if (!object){ return; }

	which = object.which;
	if (!which){ which = 0; }

	if ( (!event) || (!object.multi) || (event && !event.ctrlKey)){
		//Ctrl not held down.  unhighlight all
		for (i=0; i < aHighlighted[which].length; i++){
			aHighlighted[which][i].style.background='';
			aHighlighted[which][i].style.color='#000000';
		}
		aHighlighted[which].length = 0;	//clear the highlighted array
	}

	//now add this row to the highlighted array
	//check if it's already in the array. if so, then take it out.
	for (i=0; i < aHighlighted[which].length; i++){
		if (aHighlighted[which][i] == object) {clipHighlighted(which,i); return; }
	}

	object.style.background='#000099';
	object.style.color='#ffffff';
	
	aHighlighted[which].length++;
	aHighlighted[which][aHighlighted[which].length - 1] = object;
}

function clipHighlighted(which,i){
	aHighlighted[which][i].style.background='';
	aHighlighted[which][i].style.color='#000000';
	
	for (x=i; x < aHighlighted[which].length; x++){
		aHighlighted[which][x] = aHighlighted[which][x+1];
	}
	aHighlighted[which].length--;
}

// --------------  Tab Functions  -------------- //
function initilize(startTab){
	for (i=0; i<num_of_tabs; i++){
		aTabs[i] = eval('document.all.tab' + i);
		aButtons[i] = eval('document.all.button' + i);
	}

	if (!startTab || startTab >= num_of_tabs){
		startTab=0;
	}
	showTab(startTab);
}

function showTab(which){
	for (i=0; i < num_of_tabs; i++){
		if (i == which){
			aButtons[i].className = 'tabOn';
			aTabs[i].style.display = '';
		}else{
			aButtons[i].className = 'tabOff';
			aTabs[i].style.display = 'none';
		}
	}
	
	if (typeof(document.all.tab) == 'object' && document.all.tab.type.toUpperCase() == 'HIDDEN'){
		document.all.tab.value = which;
	}
	current_tab = which;
}

function highlightTab(object,turnOn){
	if (object.className == 'tabOn'){ return; }
	if (turnOn){
//		object.style.background = '#cccccc';
	}else{
//		object.style.background = '#ababab';
	}
}
// -------------- Calendar Functions ------------- //
function gotoMonth(vMonth, vYear, vURL){
	vURL += '?displayMonth=' + vMonth + '&displayYear=' + vYear;
	window.location = vURL;
}

function highlightDate(){
	var vObject = event.srcElement;
	var vType = event.type;

	if (vType == 'mouseover'){
		vObject.lastClass = vObject.className;
		vObject.className = 'calMouseover';
	}else{
		vObject.className = vObject.lastClass;
	}
}

// --------------  Debug Functions  -------------- //
function re(){ document.location = location; }
//document.ondblclick = re;
