function formatDate(vDate)
{
	// format date according to system
	vFormat = getDateFormat();
	switch(vFormat)
	{
		case "mm/dd/yyyy":
			var vFormatted = vDate.getMonth() + 1  + "/" + vDate.getDate() + "/" + vDate.getFullYear();
			break;
		case "dd/mm/yyyy":
			var vFormatted = vDate.getDate() + "/" + vDate.getMonth() + 1  + "/" + vDate.getFullYear();
			break;
		case "yyyy/mm/dd":
			var vFormatted = vDate.getFullYear() + "/" + vDate.getMonth() + 1  + "/" + vDate.getDate();
			break;
	}

	return vFormatted;
}

function getDate(vField )
{
	dteToday = new Date() ;
	Today = formatDate(dteToday)

	var a = vField.value ;
	// the - or + key
	if (window.event.keyCode == 45 || window.event.keyCode == 43 )
	{
		if (vField.value =="" )
		{
			vField.value = Today ;
		}
		else
		{

			a = a.split("/");
			if (a.length == 3 )
			{

			 	dteToday = new Date(vField.value);
				//dteToday.setFullYear(a[2], a[0]-1 ,a[1]);

				// the minus key
			 	if (window.event.keyCode == 45 )
			 	{
			 		dteToday.setDate(dteToday.getDate() - 1 );
			 	}
				// the plus key
			 	if (window.event.keyCode ==43 )
			 	{
			 		dteToday.setDate(dteToday.getDate() + 1 );
			 	}
			 	if (!isNaN(dteToday))
			 	{
			 		vField.value= formatDate(dteToday)
			  	}
			 	else
			 	{
			 		vField.value ="";
			 	}

			}
			else
			{
				vField.value ="";
			}
		}
		event.returnValue=false;
	}
}

function CheckValidNumber( vWord, vField)
{
	varCheck = /^\$?-?[0-9]*,?[0-9]*(\.[0-9]+)?$/;
	if (vWord != null)
	{
		//if ( isNaN(vWord))
		if (!varCheck.test(vWord))
		{

			alert("Please enter a numeric value");
			// clear the field
			vClear =  vField + " = ''"   ;
			eval (vClear);
			// set focus to the field
			eval(vField.substring(0,vField.indexOf(".value")) + ".focus()");
		}
	}
}
function IsNumber(obj)
{
	if(obj != null)
	{
		if (obj.value=="")
			return true;

		if(obj.value != "" && (obj.value >= 0 || obj.value <= 0))
		{ return true;  /* Good Value */ }
		else
		{
			alert("Please enter a numeric value.");
			obj.value = "";
			obj.focus();
			return false
		}
	}

	return false;
}
window.status="ToolHound" ;

/*
Validate date library
*/
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)

	// find the separators
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)

	switch(getDateFormat())
	{
		case "mm/dd/yyyy":
			var strMonth=dtStr.substring(0,pos1)
			var strDay=dtStr.substring(pos1+1,pos2)
			var strYear=dtStr.substring(pos2+1,pos2+5)
			break;
		case "dd/mm/yyyy":
			var strDay=dtStr.substring(0,pos1)
			var strMonth=dtStr.substring(pos1+1,pos2)
			var strYear=dtStr.substring(pos2+1,pos2+5)
			break;
		case "yyyy/mm/dd":
			var strYear=dtStr.substring(0,pos1)
			var strMonth=dtStr.substring(pos1+1,pos2)
			var strDay=dtStr.substring(pos2+1)
			break;
	}

	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : " + getDateFormat())
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert(strYear + "- Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
// Public validate date function
function CheckValidDate(vTag){
    if ( vTag.value != "" ) {
	    if ( isDate ( vTag.value ) == false ) {
	        vTag.value = ""; //clean the value to allow select date from the calendar. if the value is not clean the user cannot open calendar window
		    vTag.focus();
	    }
	}
}
//Returns true if first date more that second date and alert error message
function BeginDateMoreThenEndDate(vDateTag1,vDateTag2){
    var value = false;
    if (!( vDateTag1.value == "" && vDateTag2.value == "" )) {
	    // make sure start date is before the end date
        if (Date.parse(vDateTag1.value) > Date.parse(vDateTag2.value)) {
            alert("Invalid Date Range.\nStart date cannot be after end date!");
            value = true;
        }
	}
	return value;
}


/*
End Validate date library
*/





