function validate_Currency(strCur,fldName,fldLabel) {

	if (!strCur == "") {
		if (isCurrency(strCur) == false ) {
			window.alert(fldLabel + " must be an Currency.");
			return false;
		}

	}
	return true;

}


function validate_num(strNum, fldName, fldLabel)
{

	if(!strNum == "")
	{
		if(strNum.indexOf("$") > -1 || strNum.indexOf(",") > 0)
		{
			window.alert(fldLabel + " must be a Number, No $ or ,")
			return false;
		}
		if(isNaN(strNum)) {
			window.alert(fldLabel + " must be a Number.")
			return false;
		}
	}
	return true;
}


function validate_memo(strMemo, fldName, fldLabel, fldSize)
{
	if(!strMemo == "")
	{
		if(strMemo.length > fldSize)
		{
			alert(fldLabel + " must be Less than " + fldSize )
			return false;
		}
	}
	return true;
}


function validate_date(strDate, fldName, fldLabel)
{
	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)
		{
			
			alert(fldLabel + " must be a date.")
			return false;
		}
		
		if(date_array[0].length < 1 || date_array[0].length > 2)
		{
			alert(fldLabel + " has an invalid month.")
			return false;
		}

		if(date_array[2].length > 4 || date_array[2].valueOf() < 0)
		{
			alert(fldLabel + " has an invalid year.")
			return false;
		}


		
		testDate = new Date(date_array[2], date_array[0] - 1, date_array[1]);
		if (testDate.getMonth() != date_array[0] - 1)
		{
			alert(fldLabel + " must be a date.")
			return false;
		}
	}
	
	return true;
}


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)
		{
			alert(fldLabel + " must be a date.")

			hidBox.value = "";
			return false;
		}
		hidBox.value = strDate;
	}
	else
	{
		if(strDate.length != 8) {
			alert("Invalid " + fldLabel)
			hidBox.value = "";
			return false;
		}
		if(required == true) {
			alert("You must enter a " + fldLabel)
			hidBox.value = "";
			return false;
		}
	}

	return true;
}

function validate_date_dd(strMonth, strDay, strYear, fldLabel, hidBox, required)
{
	var x
	var y
	var strDate = new String(strMonth + "/" + strDay + "/" + strYear)
	// alert(strDate)
	if(strDate.length == 10)
	{

		x = new Date(strYear, strMonth - 1, strDay)

		if(x.getMonth() != strMonth - 1)
		{
			
			alert(fldLabel + " must be a date.")
			hidBox.value = "";
			return false;
		}
		hidBox.value = strDate;
	}
	else
	{
		if(strDate.length != 2) {

			alert("Invalid " + fldLabel)
			hidBox.value = "";
			return false;
		}
		
		if(required == true) {
			alert("You must enter a " + fldLabel)
			hidBox.value = "";
			return false;
		}
	}

	return true;
}



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 textSelect_autoscroll(textElement, selectElement)
{


	var lngMatches = 0;
	var lngIndex = -1;
	var strOption = new String("");
	var strValue = new String("");

	if(window.event.keyCode == 8 || window.event.keyCode == 16)
	{
		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;
		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)
			tRange.select();
		}
		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();
	}
}

