// ValidateDate.js

// returns whether the given string represents a valid date
// two-digit years (with 00=2000) and slashes/hyphens are supported
function isDate(strdt)
{
	var dt = new Date(strdt);

	var elem = strdt.match(/^\s*(\d+)\/(\d+)\/(\d+)\s*$/) ;
	
	if(elem == null) return false;
	
	// 2/29/00 means leap day 2000, not 1900 as JavaScript may think
	if(0 == elem[3] && 2 == elem[1] && 29 == elem[2] && dt.getDate() > 0) return true;
	
	// if the date parser and the text parser get the same month, it's all good.
	// otherwise we'll get NaN or the wrong month (e.g. 2/29/99 -> 3/1/1999)	
	return dt.getMonth()+1 == elem[1];
} 


function FormatAsDate(strValue)
{
    var Month;
    var Day;
    var Year;
    var Index;
    
	var Values = GetInteger(strValue, 0);
    if(Values == null)
    {
		return null;
    }
    Month = Values[0];    
    Index = parseInt(Values[1]);
    
    // For now assume it either 6 or 8 characters.
	if(Index == strValue.length)
    {
        // Assume it is this format MMDDYY
		if(strValue.length == 6)	
		{
		  Month = strValue.substr(0,2);
		  Day = strValue.substr(2,2);
		  Year = strValue.substr(4,2);

		}
		else 
		// Assume it is this format MMDDYYYY
		if(strValue.length == 8)	
		{
		  Month = strValue.substr(0,2);
		  Day = strValue.substr(2,2);
		  Year = strValue.substr(4,4);
		}		
		else
		  return null;
    }
    else 
    {
		Values = GetInteger(strValue, Index);
		if(Values == null)
		{
			return null;
		}
		
		Day = Values[0];
		Index = Values[1];		

		Values = GetInteger(strValue, Index);
		if(Values == null)
		{
			return null;
		}

		Year = Values[0];
    }
    
    // For now we will use MM/DD/YYYY
    return Month + "/" + Day + "/" + Year;
}


function GetInteger(s, startpos)
{
    var foundnumber = false;
	var i;
	var j = "";
	var charcount = 0;
	
    for (i = startpos; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c >= "0") && (c <= "9"))
        {
           j = j + c;
           foundnumber = true;
           charcount = 0;
        }
		else
        if ((c == "-") || (c == "/") || (c == "."))
        {
		    if(charcount > 1)
			{
				return null;
			}
			else
			if(foundnumber == true)
			{
				if(j.length > 0)
				{
					return [ j , i ];
				}
				else
				{
					return null;
				}
			}
			else
			if(startpos == 0)
			{
				return null;
			}
	        else			
			{
			  charcount++;
			}
        }  
        else
        {
           return null;
		}        
    }
    
	if(foundnumber == true)
	{
		x = i;
		if(j.length > 0)
		{
			return [ j , i ];
	    }
    }
	else
	{
		return null;
	}
}

var ErrorInvalidID = "";
function OnError(criterionCntrl, ErrorNo, ErrorCntrlFormat)
{
	var errorMessageCntrl = document.getElementById("ErrorMessage");	

    var strError = "ERROR - ";
	switch(ErrorNo)
	{
		case 1: // Invalid Date Format
		{
		    strError += "Invalid Date";		
			break;
		}

		case 2: // Invalid Date
		{
		    strError += "Invalid Date Format";
			break;
		}
			
		default:
		{
			break;
		}
	}
	errorMessageCntrl.style.display="block";
	errorMessageCntrl.style.color="red";
	errorMessageCntrl.innerText= strError;
	criterionCntrl.value = "";
	ErrorInvalidID = ErrorCntrlFormat;
	setTimeout("SetFocusViaTimer();", 1000);							
}

function SetFocusViaTimer()
{
    if (ErrorInvalidID != null)
    {
		document.getElementById(ErrorInvalidID).focus();
    }
}


