// ENTRY VALIDATION & FORMATTING

// 11/10/97 - TJS Added functions
// 01/21/99 - SCC Added function isUniqueItem
// 02/11/99 - TJS Tweaked number formatting functions
// 03/11/99 - SCC Added param to validateNum; boolean to accept 'none' and change to zero
//*****
// 06/06/99 - SCC - Ported from Pro WAPP. ONLY called by mtg_calc.html.


function isUniqueItem (list,thisItemCode,delimChar,caseSensitive) {
// RETURNS TRUE IF thisItemCode IS NOT IN list, FALSE OTHERWISE. //
// DEFAULT FOR delimChar (OPTIONAL) IS COMMA, DEFAULT FOR caseSensitive (OPTIONAL) is FALSE. //
	if (list == '' || list == undefined || thisItemCode == '' || thisItemCode == undefined)
		return true;
	if (caseSensitive == undefined || caseSensitive == false) { // just convert both to upper case //
		list = list.toUpperCase();
		thisItemCode = thisItemCode.toUpperCase();
	}
	if (delimChar == undefined || delimChar == '')  // Assumes carriage return delim, will replace with ',' //
		delimChar = ',';
	
	if (list.charAt(0) != delimChar)				// In case the delimiter is not at the beginning //
		list = delimChar + list;
	if (list.charAt(list.length - 1) != delimChar)	// or end of the list //
		list = list + delimChar;
		
	thisItemCode = delimChar + thisItemCode + delimChar;
	
	var foundIndex = list.indexOf(thisItemCode);
	if (foundIndex == -1) 
		return true;
	else 
		return false;
}
function scrub_RollNum(theNum,otherAllowed) {
// REMOVE ALL NON-NUMERIC CHARS EXCEPT FOR PROGRAMMER SPECIFIED 'OTHER' ALLOWABLE CHARS
	var result = "";
	var theChar = "";
	var allowed = "";
	otherAllowed +="";
	if (otherAllowed == 'undefined')
		otherAllowed = "";
	allowed = "0123456789"+ otherAllowed;
	for(i=0;i<=theNum.length;i++) {
		theChar = theNum.charAt(i);
		if(allowed.lastIndexOf(theChar) != -1)
 			result = result + theChar;
	}
	// remove commas from the end of the string
	for(i=result.length-1; i>=0; i--) {
		if(result.charAt(i) != ",")
			break;
		result = result.substring(0,i);
	}

	return result;
}

function scrub_Beds_and_Baths(theNum) {
// Check beds and baths entry
	var result = "";
	var theChar = "";
	var allowed = "0123456789";
	for(i=0;i<=theNum.length;i++) {
		theChar = theNum.charAt(i);
		if (theChar=='.') {
			result = result + theChar;
			if(allowed.lastIndexOf(theNum.charAt(i+1)) != -1)
				result = result + theNum.charAt(i+1);
			else
				result = result + '0';
			return result;
		}
		if(allowed.lastIndexOf(theChar) != -1)
 			result = result + theChar;
	}
	return result;
}


function formatNum(theNum,decplaces,addcommas) {
// *** STOP USING THIS ***
// USE format_NUMBER
// FORMAT NUMERIC DATA
	var str = ""+theNum.value;
	if ((str == "") || (str == "null")) {
		theNum.value = "";
		return false;
	}
	if (decplaces == 0) {
		var fmtdnum = formatInteger(theNum,decplaces);
	} else {
		var fmtdnum = formatRealNum(theNum,decplaces);
	}
	if (addcommas) {
		fmtdnum = commaFmt(fmtdnum);
	}
	theNum.value = fmtdnum;
	return true
}

function format_Real(theNum,decplaces) {
// FORMAT A REAL NUMBER TO THE DESIRED DECIMAL PLACE
	var str = filterNum(theNum);
	str = Math.round(parseFloat(str) * Math.pow(10,decplaces));
	str = ""+str;
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function format_Integer(theNum) {
// FORMAT AN INTEGER
	var str = filterNum(theNum);
	str = Math.round(parseFloat(str));
	return ""+str;
}

function format_Number(theNum,decplaces,addcommas) {
// FORMAT NUMERIC DATA
	var str = ""+theNum;
	if ((str == "") || (str == "null"))
		return "";

	if (decplaces == 0)
		var fmtdnum = format_Integer(str,decplaces);
	else
		var fmtdnum = format_Real(str,decplaces);
		
	if (addcommas) 
		fmtdnum = commaFmt(fmtdnum);

	return fmtdnum;
}


// SUBMIT VALIDATION

function str_Empty (dataIn){
	if (dataIn.value+"" == "null")
		return true;
	else if (dataIn.value == "")
		return true;
	return false;
}

function scrub_Text (theStr,makeUpper) {
// strip spaces off ends of string
	theStr += "";
	if (theStr == "null" || theStr == "")
		return "";
	for(var i=0; i<theStr.length; i++) {
		if(theStr.charAt(0) != " ")
			break;
		theStr = theStr.substring(1,theStr.length);
	}
	for(i=theStr.length-1; i>=0; i--) {
		if(theStr.charAt(i) != " ")
			break;
		theStr = theStr.substring(0,i);
	}
	if (makeUpper)
		theStr = theStr.toUpperCase();
	return theStr;
}

function popup_Empty(theMenuObj) {
	if (theMenuObj.options[theMenuObj.selectedIndex].text == '')
		return true;
	else
		return false;
}

function list_Empty(theMenuObj) {
	var isEmpty = true;
	for(i=1;i<theMenuObj.length;i++) {
		if (theMenuObj.options[i].selected)
			isEmpty = false;
	}
	return isEmpty;
}

function dateOK(dayObj,monthObj,yearObj) {
	var dayEmpty = popup_Empty(dayObj);
	var monthEmpty = popup_Empty(monthObj);
	var yearEmpty = popup_Empty(yearObj);
	if (dayEmpty&&monthEmpty&&yearEmpty)
		return true;
	if (!dayEmpty&&!monthEmpty&&!yearEmpty)
		return true;
	return false;
}

function dateRange_OK(fromDayObj,fromMonthObj,fromYearObj,toDayObj,toMonthObj,toYearObj)
{
	var fromDay = fromDayObj.options[fromDayObj.selectedIndex].value;
	var fromMonth =fromMonthObj.options[fromMonthObj.selectedIndex].value;
	var fromYear = fromYearObj.options[fromYearObj.selectedIndex].value;
	var toDay = toDayObj.options[toDayObj.selectedIndex].value;
	var toMonth = toMonthObj.options[toMonthObj.selectedIndex].value;
	var toYear = toYearObj.options[toYearObj.selectedIndex].value;

	if (parseInt(fromYear + fromMonth + fromYear) > parseInt(toYear + toMonth + toYear))
		return false;
	return true;
}

function numberRange_OK(fromNumObj,toNumObj)
{
	if (fromNumObj.value == "" || toNumObj.value == "")
		return true;
		
	var tmpFromFloat = parseFloat(filterNum(fromNumObj.value));
	var tmpToFloat = parseFloat(filterNum(toNumObj.value));
	if (tmpToFloat < tmpFromFloat) {
		alert(toNumObj.name+"  is greater than  "+toNumObj.name+"   Please re-enter");
		fromNumObj.value = "";
		toNumObj.value = "";
		fromNumObj.focus();
		return false;
	}
	return true;
}
