function validate_Currency(strCur,fldName,fldLabel) {

	if (!strCur == "") {
		if (isCurrency(strCur) == false ) {

			return fldLabel + " must be an Currency.\n";
		}

	}
	return "";

}


function validate_num(strNum, fldName, fldLabel, minValue, maxValue, decimalPlaces)
{
	var decimalIndex;
	if(!(strNum == ""))
	{
		// CHECK TO MAKE SURE VALUE IS A NUMBER
		if(strNum.indexOf("$") > -1 || strNum.indexOf(",") > 0)
		{
		
			return fldLabel + " must be a Number, No $ or ,\n"
		}
		if(isNaN(strNum)) {

			return fldLabel + " must be a Number.\n";
		}
		
		//CHECK MIN AND MAX VALUES
		if(minValue != "")
		{
			if(parseFloat(minValue) > parseFloat(strNum))
			{
				return fldLabel + " must be greater than " + minValue + ".\n";
			}
		}
		
		if(maxValue != "")
		{
			if(parseFloat(maxValue) < parseFloat(strNum))
			{
				return fldLabel + " must be smaller than " + maxValue + ".\n";
			}
		}
		
		decimalIndex = strNum.indexOf(".")
		if(decimalIndex > -1)
		{
			if(decimalPlaces == "0")
			{
				return fldLabel + " must be an integer.\n";
			}
	
			if(parseInt(decimalPlaces) < strNum.length - decimalIndex - 1)
			{
				return fldLabel + " has too many decimal places.\n";
			}
		}		

		
	}
	return "";
}


function validate_memo(strMemo, fldName, fldLabel, fldSize)
{
	if(!strMemo == "")
	{
		if(strMemo.length > fldSize)
		{
			return fldLabel + " must be Less than " + fldSize  + "\n";
		}
	}
	return "";
}


function validate_date(strDate, fldName, fldLabel, minValue, maxValue)
{
	var x
	var strSplit = new String(strDate)
	var date_array = new Array();
	var testDate;
	
	date_array = strSplit.split(" ")
	date_array = date_array[0].split("/")

	if(!strDate == "")
	{
		
		x = Date.parse(strDate);
		

		if(isNaN(x) || date_array.length != 3)
		{
			
			return fldLabel + " must be a date.\n";
		}
		
		if(date_array[0].length < 1 || date_array[0].length > 2)
		{
			return fldLabel + " has an invalid month.\n";
		}

		if(date_array[2].length > 4 || date_array[2].valueOf() < 0)
		{
			return fldLabel + " has an invalid year.\n";
		}
		
		testDate = new Date(date_array[2], date_array[0] - 1, date_array[1]);
		if (testDate.getMonth() != date_array[0] - 1)
		{
			return fldLabel + " must be a date.\n";
		}
		
		
		if(x < Date.parse(minValue))
		{
			return fldLabel + " must be on or after " + minValue + ".\n";
		}

		if(x > Date.parse(maxValue))
		{
			return fldLabel + " must be on or before " + maxValue + ".\n";
		}
		
		
		
	}
	
	return "";
}


function validate_date_time(strMonth, strDay, strYear, strHour, strMinute, strAMPM, fldLabel, hidBox, required)
{
	var x
	var y
	var strDate = new String(strMonth + "/" + strDay + "/" + strYear + " " + strHour + ":" + strMinute + ":00 " + strAMPM)

	if(strDate.length == 22)
	{

		x = new Date(strYear, strMonth - 1, strDay)

		if(x.getMonth() != strMonth - 1)
		{
			hidBox.value = "";
			return fldLabel + " must be a date.\n";
		}
		hidBox.value = strDate;
	}
	else
	{
		if(strDate.length != 8) {
			hidBox.value = "";
			return "Invalid " + fldLabel + "\n";
		}
		if(required == true) {
			hidBox.value = "";
			return "You must enter a " + fldLabel + "\n";
		}
	}

	return "";
}

function validate_date_dd(strMonth, strDay, strYear, fldLabel, hidBox, required, minValue, maxValue)
{
	var x
	var y
	var strDate = new String(strMonth + "/" + strDay + "/" + strYear)
	
	var z = Date.parse(strDate);
	
	// alert(strDate)
	if(strDate.length == 10)
	{

		x = new Date(strYear, strMonth - 1, strDay)

		if(x.getMonth() != strMonth - 1)
		{
			hidBox.value = "";
			return fldLabel + " must be a date.\n";
		}
		
		// CHECK MIN/MAX VALUES
		if(z < Date.parse(minValue))
		{
			hidBox.value = "";
			return fldLabel + " must be on or after " + minValue + ".\n";
		}

		if(z > Date.parse(maxValue))
		{
			hidBox.value = "";
			return fldLabel + " must be on or before " + maxValue + ".\n";
		}		

		
		hidBox.value = strDate;
		
		
		
		
	}
	else
	{
		if(strDate.length != 2) {

			hidBox.value = "";
			return "Invalid " + fldLabel + "\n";
		}
		
		if(required == true) {
			hidBox.value = "";
			return "You must enter a " + fldLabel + "\n";
		}
	}


	return "";
}



function ltrim(strTrim)
{
	var str = new String(strTrim);
	var retstr = new String("");
	for (k = 0; k < str.length; k++)
	{
		if (str.charAt(k) != " ")
   		{
			retstr = str.substr(k);
			break;
 		}
	}
	return retstr;
}


function rtrim(strTrim)
{
	var str = new String(strTrim);
	var retstr = new String("");
	for (k = str.length - 1; k >= 0; k--)
	{
		if (str.charAt(k) != " ")
   		{
			retstr = str.substr(0,k+1);
			break;
 		}
	}
	return retstr;
}

function trim(strTrim)
{
	var x = new String(strTrim)
	x = ltrim(x)
	x = ytrim(x)
	return x;
}



function replace_norm(ser, strFind, rep)
{
	var y = new Array()
	var i
	var mainString = new String(ser)
	var newStr = new String("")
	var strSearch = new String(strFind)
	var strReplace = new String(rep)
	
	y = mainString.split(strSearch)

	for(i = 0; i < y.length; i++)
	{
		newStr = newStr + y[i] + strReplace

	}
	newStr = newStr.substr(0, newStr.length - strReplace.length)

	return newStr;

}


function isCurrency(str){
	var isFlag=0;
    var isDollarSign=0;
    var isPeriod = 0;
	var isStr=str;
	
	for (var i=0; i <isStr.length ; i++) {
		if (isStr.charAt(i) == "$" ) {
			isDollarSign=isDollarSign + 1;
		}else if (isStr.charAt(i) == "." ) {
			isPeriod=isPeriod + 1;
		}else if (isStr.charAt(i) == "," ) {
			//do nothing
		} else {
			if (isNumber(isStr.charAt(i)) == false ) {
				isFlag=1;
			}
		}
	}	
	if (isFlag == 0 && isDollarSign < 2 && isPeriod < 2 ) { 
		return true;
	} else {
		return false
	}
}

function isNumber (tmp) {  
	var i; 
	for (i=0;i<tmp.length;i++) { 
		c = tmp.charAt(i) 
		if (c < "0" || c > "9") return false; 
	} 
	return true; 
} 

function isNumberic (tmp) {  
	var i;
	var sample="0123456789.";
	var result = false; 
	for (i=0;i<tmp.length;i++) { 
		c = tmp.charAt(i); 
		result = false;
		for (j=0; j < sample.length; j++) {
			if (c == sample.charAt(j)){
			   result=true;
			}
		}
		if (result == false) return false;
	} 
	return true; 
} 

function getCurrencyNumber(sstr) {

	sstr = replace_norm(sstr,'$','');
	sstr=sstr.replace(/,/gi,'');
	return sstr;

}


function formatToMask(strValue, strMask)
{
	var i;
	
	var intValueIndex = 0;
	var intMaskIndex = 0;
	
	var sample="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	var strValueComplete = new String(strValue);
	var strValueStripped = new String("");
	var strOutput = new String("");
	
	var strCharacter = new String("");
	
	// This part strips out all un-numeric characters
	for(i = 0; i < strValueComplete.length; i++)
	{
		
		if(sample.indexOf(strValueComplete.substr(i,1)) != -1)
		{
			strValueStripped += strValueComplete.substr(i,1);
		}
		
	}

	if(strValueStripped == "")
	{
		return "";
	}
	
	
	for(i = 0; i < strMask.length; i++)
	{
		strCharacter = strMask.substr(i,1)
		if(strCharacter != "^" && strCharacter != "~" && strCharacter != "*")
		{

			strOutput += strCharacter;

		}
		
		// Output a character from the value stripped string
		if(strCharacter == "^")
		{	
		
			if(intValueIndex >= strValueStripped.length)
			{
				return strOutput;
			}
			
			strOutput += strValueStripped.substr(intValueIndex, 1);
			intValueIndex++;

		}
		
		// If the value stripped string has been run through then
		// stop processing the mask
		if(strCharacter == "*")
		{

			if(intValueIndex >= strValueStripped.length)
			{
				return strOutput;
			}

		}
		
		// Write out the rest of the value stripped string
		if(strCharacter == "~")
		{
		
		
			for(i = intValueIndex; i < strValueStripped.length; i++)
			{
				strOutput += strValueStripped.substr(i,1);
			}
			return strOutput;
		}

	}

	return strOutput;
}



function textSelect_autoscroll(textElement, selectElement, prevH, prevK)
{


	var lngMatches = 0;
	var lngIndex = -1;
	var strOption = new String("");
	var strValue = new String("");
	var strElValue = new String(textElement.value);
	
	
	
	if(window.event)
	{
		
		var strKeyCode = new String(event.keyCode)
		
		if(document.getElementById(prevK).value != strKeyCode)
		{

			return false;
		}
	
	
		if(window.event.keyCode == 16)
		{
			return false;
		}	
		if(window.event.keyCode == 8)
		{

			if(document.getElementById(prevH).value == "true")
			{
				textElement.value = strElValue.substr(0, strElValue.length - 1);
			}
			// return false;
		}
	}
	if(textElement.value == "") 
	{	
		selectElement.options[0].selected = true;
		return false;
	}
	
	for(i = 0; i < selectElement.options.length; i++)
	{
		strOption = selectElement.options[i].text
		strOption = strOption.toUpperCase();
		strValue = textElement.value;
		strValue = strValue.toUpperCase();
		
		if(strOption.indexOf(strValue) == 0)
		{
			if(lngIndex == -1)
			{
				selectElement.options[i].selected = true;
				lngIndex = i;
			}
			lngMatches++;
		}
	}

	if(lngMatches == 0)
	{
		selectElement.options[0].selected = true;
		document.getElementById(prevH).value = "false";
		return false;
		
	}
	else
	{
		if(lngMatches == 1)
		{
			strValue = textElement.value;
			var oldLength = strValue.length
			
			textElement.value = selectElement.options[lngIndex].text;
			
			strValue = textElement.value;
			var newLength = strValue.length
			
			var tRange = textElement.createTextRange();



			tRange.moveStart("character", oldLength)
			if(tRange.text != "")
			{
				document.getElementById(prevH).value = "true";
			}
			else
			{
				document.getElementById(prevH).value = "false";
			}
			
			tRange.select();
		}
		else
		{
			document.getElementById(prevH).value = "false";
		}
		return true;
	}
}	

function textSelect_select2text(textElement, selectElement)
{

	if(selectElement.selectedIndex > 0)
	{	

		textElement.value = selectElement.options[selectElement.selectedIndex].text
		selectElement.focus();	
	}
	else
	{

		textElement.value = "";
		selectElement.focus();
	}
}

function clearField(theField)
{

	if(!theField.length)
	{

		if(theField.checked)
			theField.checked = false;
		

		if(theField.value)
			theField.value = '';
	}
	else
	{
		for(var i = 0; i < theField.length; i++)
		{
			if(theField[i].checked)
				theField[i].checked == false;
		}
	
		if(theField.selectedIndex)
		{
			theField.selectedIndex = 0;
		}

	
	}

}

function validateSearchField(y, theform, ssid, fldname, fldlbl, fldtype, fldsize, flddisplaytype)
{

   if(fldtype == "Numeric")
   {
   	if(flddisplaytype == "Text" || flddisplaytype == "Currency")
	{

		xx = theform["f1_"+ ssid].value
		xx2 = theform["f2_" + ssid].value
		xxst = theform["st_" + ssid].options[theform["st_" + ssid].selectedIndex].value
		
		xx = ltrim(xx)
		xx = rtrim(xx)
					
		xx2 = ltrim(xx2)
		xx2 = rtrim(xx2)
		
	       if(xx != "")
		{
		
			if(isNaN(xx))
			{
				alert(fldlbl + " must be a Number.")
				y = 1;
			}
	
			if(xxst == "notbetween" || xxst == "between")
			{
				if(isNaN(xx2) || xx2 == "")
				{
					alert("The second value for " + fldlbl + " must be a Number.")
					y = 1;
				}
			}
		}
	}
	else
	{

		if(!theform[fldname].value == "")
		{
			xx = theform[fldname].value
			
			xx = ltrim(xx)
		
			if(isNaN(xx))
			{
				alert(fldlbl + " must be a Number.")
				y = 1;
			}
		       xx = theform[fldname].value;
			theform[fldname].value = replace_norm(xx, " ", "")
		}
	}
   }

   
   if(fldtype == "Date")
   {
	xx = theform["f1_" + ssid].value
	xx2 = theform["f2_" + ssid].value
	xxst = theform["st_" + ssid].options[theform["st_" + ssid].selectedIndex].value
	
	xx = ltrim(xx)
	xx = rtrim(xx)
				
	xx2 = ltrim(xx2)
	xx2 = rtrim(xx2)
	
       if(xx != "")
	{
		
		x = validate_date(xx, fldname,  fldlbl)
		if(x != "")
		{
			window.alert(x);
			y = 1;
					
		}

		if(xxst == "notbetween" || xxst == "between")
		{
			x = validate_date(xx2, fldname, 'The second value for ' + fldlbl)
			if(x != "")
			{
				window.alert(x);
				y = 1;
						
			}
		}
	}
    }

    return y;
}

function validateIsEmpty(passed_form, fldname, displaytype, boolEmpty)
{
	
	if(displaytype == "Select" || displaytype == "Select Type-In")
	{
		if(passed_form[fldname].selectedIndex != 0) boolEmpty = false;
	}
	
	else if(displaytype == "Radio")
	{
		radioFound = false;

		for(var i = 0; i < passed_form[fldname].length; i++)
		{

			if(passed_form[fldname][i].checked) radioFound = true;
		}

		if(radioFound == true) boolEmpty = false;
	}
	
	else if(displaytype == "Checkbox")
	{
		checkFound = false;
		if(passed_form[fldname].length)
		{
			for(var i = 0; i < passed_form[fldname].length; i++)
			{
				if(passed_form[fldname][i].checked) checkFound = true;
			}
		}
		else
		{
			if(passed_form[fldname].checked) checkFound = true;
		}
		
		
		if(checkFound == true) boolEmpty = false;
	}
	
	else if(displaytype == "Date/Time")
	{
	
	}
	
	else if(displaytype == "Date")
	{
		if(passed_form[fldname + "_month"].selectedIndex != 0 || passed_form[fldname + "_day"].selectedIndex != 0 || passed_form[fldname + "_year"].selectedIndex != 0)
		{
			boolEmpty = false;
		}
	}
	
	else
	{
		
		if(passed_form[fldname].value != "")
		{
			boolEmpty = false;
		}
	}
	return boolEmpty;
}