function isTime(strTime) {
	if (strTime.length !=5) {
		return false;
	} else {
		intHour = parseInt(Left(strTime,2),10);
		intMinute = parseInt(Right(strTime,2),10);
		if (intHour >= 0 && intHour <= 23 && intMinute >= 0 && intMinute <= 59) {
			return true;
		} else {
			return false;
		}
	}
}
function Minute(strTime) {
	if (strTime.length !=5) {
		return -1;
	} else {
		strMinute = Right(strTime,2);
		if (IsNumeric(strMinute)) {
			intMinute = parseInt(strMinute,10);
			if (intMinute >= 0 && intMinute <= 59) {
				return intMinute;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}
}
function Hour(strTime) {
	if (strTime.length !=5) {
		return -1;
	} else {
		strHour = Left(strTime,2);
		if (IsNumeric(strHour)) {
			intHour = parseInt(strHour,10);
			if (intHour >= 0 && intHour <= 23) {
				return intHour;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

}
function MinuteDiff(strTime1, strTime2) {
	if (!(isTime(strTime1) && isTime(strTime2))) {
		return -1;
	} else {
		intHour1 = Hour(strTime1);
		intHour2 = Hour(strTime2);
		intMinute1 = Minute(strTime1);
		intMinute2 = Minute(strTime2);
		intReturn = (60*(intHour2-intHour1));
		intReturn = intReturn + (intMinute2-intMinute1);
		return intReturn;
	}
}

function isDate(strDate) {
	if (strDate.length !=8) {
		return false;
	} else {
		if (Mid(strDate,2,1) == "-" && Mid(strDate,5,1) == "-") {
			var intDay=parseInt(Left(strDate,2),10);
			var intMonth=parseInt(Mid(strDate,3,2),10);
			var intYear=parseInt(Right(strDate,2),10);
			if (intMonth > 0 && intMonth <= 12 && intYear >= 8 && intYear <= 99) {
				if (intMonth == 2) {
					if (intYear == 8 || intYear == 12 || intYear == 16) {
						if (intDay > 0 && intDay <= 29) {
							return true;
						} else {
							return false;
						}
					} else {
						if (intDay > 0 && intDay <= 28) {
							return true;
						} else {
							return false;
						}					
					}
				} else if (intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 8 || intMonth == 8) {
					if (intDay > 0 && intDay <= 31) {
						return true;
					} else {
						return false;
					}					
				} else {
					if (intDay > 0 && intDay <= 30) {
						return true;
					} else {
						return false;
					}					
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	}	
}

function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, charSearchFor.length))
                  {
                        return i;
                  }
            }
            return -1;
}
function Left(str, len) {
	return Mid(str,0,len);
}
function Right(str, len) {
	return Mid(str,(str.length-len), len);
}
function Lcase(str) {
	return str.toLowerCase();
}	
function Ucase(str) {
	return str.toUpperCase();
}	
function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}
function Asc(String)
{

	return String.charCodeAt(0);

}

function Chr(AsciiNum)
{

	return String.fromCharCode(AsciiNum)

}
function HardTrim(strString) {
	var strReturn = "";
	for (i=0;(i < strString.length);i++) {
		strChar = Mid(strString, i, 1);
		if (!(strChar == " ")) {
			strReturn = strReturn + strChar;
		}
	}
	return strReturn;
}
function IsNumeric(strString)
{
	var strChar="";
	var i=0;
	if (strString.length = 0)
	{
		return false;
	} else {
		var bolReturn = true;
		for (i=0;(i < strString.length && bolReturn);i++) {
			strChar = Mid(strString, i, 1);
			if (!IsNumberChar(strChar) && strChar != ",") {
				bolReturn = false;
			}
		}
		return bolReturn;
	}

}
function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}


function IsAlphaNumeric(strString)
{
	var strChar="";
	if (strString.length = 0)
	{
		return false;
	} else {
		var bolReturn = true;
		for (i=0;(i < strString.length && bolReturn);i++) {
			strChar = Mid(strString, i, 1);
			if (!IsValidChar(strChar)) {
				bolReturn = false;
			}
		}
		return bolReturn;
	}

}
function IsValidChar(strChar)
{
	if (Asc(strChar) >= Asc("a") && Asc(strChar) <= Asc("z"))
	{
		return true;
	} else if (Asc(strChar) >= Asc("A") && Asc(strChar) <= Asc("Z")) {
		return true;
	} else if (Asc(strChar) >= Asc("0") && Asc(strChar) <= Asc("9")) {
		return true;
	} else {
		return false;
	}			
}
function IsNumberChar(strChar)
{
	if (Asc(strChar) >= Asc("0") && Asc(strChar) <= Asc("9")) {
		return true;
	} else {
		return false;
	}			
}
function EmailCheck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}
    if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	}
	if (str.indexOf(" ")!=-1){
	    return false;
	}
	return true;
}
function ReplaceAll(strString,strFind,strReplace) 
{
	var i=0;
	var strReturn = strString;
	var intIndexOfMatch = strReturn.indexOf( strFind );
	while (intIndexOfMatch != -1 && i < 100){
		strReturn = strReturn.replace( strFind, strReplace );
		intIndexOfMatch = strReturn.indexOf( strFind );
		i++;
	}
	return( strReturn );
}

function FormatEnteredDate(strFieldName) {
	var currentdate = new Date();
	strEnteredDate=document.getElementById(strFieldName).value;
	strReturn = "";
	intLenEnteredDate = strEnteredDate.length;
	if (intLenEnteredDate == 1) {
		strReturn = "0" + strEnteredDate + "-" + Right("0" + (currentdate.getMonth()+1),2) + "-" + Right("" + currentdate.getFullYear(),2);
	} else if (intLenEnteredDate == 2) {
		strReturn = strEnteredDate + "-" + Right("0" + (currentdate.getMonth()+1),2) + "-" + Right("" + currentdate.getFullYear(),2);
	} else if (intLenEnteredDate == 4) {
		strReturn = Left(strEnteredDate,2) + "-" + Right(strEnteredDate,2) + "-" + Right("" + currentdate.getFullYear(),2);
	} else if (intLenEnteredDate == 5) {
		strReturn = strEnteredDate + "-" + Right("" + currentdate.getFullYear(),2);
	} else if (intLenEnteredDate == 6) {
		strReturn = Left(strEnteredDate,2) + "-" + Mid(strEnteredDate,2,2) + "-" + Right(strEnteredDate,2);
	} else if (intLenEnteredDate == 8) {
		strReturn = strEnteredDate;
	} else if (intLenEnteredDate == 10) {
		strReturn = Left(strEnteredDate,2) + "-" + Mid(strEnteredDate,3,2) + "-" + Right(strEnteredDate,2);
	}
	if (strReturn.length == 8) {
		if (!(IsNumeric(Left(strReturn,2)) && IsNumeric(Mid(strReturn,3,2)) && IsNumeric(Right(strReturn,2)))) {
			strReturn = "";
		} 
	}
	document.getElementById(strFieldName).value = strReturn;
}
function GetTimeValue(strFieldName) {
	var strReturn = "";
	strReturn = document.getElementById(strFieldName).value;
	if (strReturn.length == 4) {
		strReturn = Left(strReturn,2) + ":" + Right(strReturn,2);
	}
	if (strReturn.length == 2) {
		strReturn = strReturn + ":00";
	}
	if (strReturn.length == 1) {
		strReturn = "0" + strReturn + ":00";
	}
	if (strReturn.length == 5) {
		if ((IsNumeric(Left(strReturn,2))) && (IsNumeric(Right(strReturn,2)))) {
			intHour = parseInt(Left(strReturn,2),10);
			intMinute = parseInt(Right(strReturn,2),10);
			if (intMinute>0 && intMinute<15) {
				if (strFieldName=="TimeSheetStart") {
					intMinute = 15;
				} else {
					intMinute = 0;
				}
			} else if (intMinute>15 && intMinute<30) {
				if (strFieldName=="TimeSheetStart") {
					intMinute = 30;
				} else {
					intMinute = 15;
				}
			} else if (intMinute>30 && intMinute<45) {
				if (strFieldName=="TimeSheetStart") {
					intMinute = 45;
				} else {
					intMinute = 30;
				}
			} else if (intMinute>45 && intMinute<60) {
				if (strFieldName=="TimeSheetStart") {
					intMinute = 0;
					intHour = intHour + 1;
				} else {
					intMinute = 45;
				}
			}
			if (intHour>=0 && intHour <= 23 && intMinute>=0 && intMinute <= 59) {
				if (intHour < 10) {
					strReturn = "0" + intHour;
				} else {
					strReturn = "" + intHour;
				}
				strReturn = strReturn + ":";
				if (intMinute < 10) {
					strReturn =  strReturn + "0" + intMinute;
				} else {
					strReturn =  strReturn + intMinute;
				}
			} else {
				strReturn = "";
			}
		} else {
			strReturn = "";
		}
	}
	if (strReturn != document.getElementById(strFieldName).value) {
		document.getElementById(strFieldName).value = strReturn;
	}
	return strReturn;
}
function StripForNonNumChars(strString) {
	var i=0;
	strReturn = "";
	if (strString.length > 0) {
		for (i=0;(i < strString.length);i++) {
			strChar = Mid(strString,i,1);
			if (strChar == "." || strChar == ",") {
				strReturn = strReturn + ",";
			} else if (IsNumeric(strChar)) {
				strReturn = strReturn + strChar;
			}
		}
	}
	if (strReturn != "") {
		if (Left(strReturn,1) == ",") {
			strReturn = "0" + strReturn;
		}
	}
	return strReturn;
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
