function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	for(i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if(ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function validateTimeEntry(theControl)
{
	theNum = parseFloat(theControl.value);
	if(isNaN(theNum))
		theNum = 0.0;
	theControl.value = Math.round(theNum*100)/100;
}

function validateTimeEntryHours(theControl)
{
	theNum = parseFloat(theControl.value);
	if(isNaN(theNum))
		theNum = 0.0;
	if(theNum > 24.0)
	{
		theNum = 24.0;
		alert('Daily time must be 24 hours or less.');
	}
	theControl.value = Math.round(theNum*100)/100;
}

function ValNumber(val)
{
	var strlist = /[.0123456789]/;
	var strval = val.value;
	var strLen = strval.length;
	var lastchar = val.value.charAt((strLen) - 1);

	if(lastchar.search(strlist) == -1)
	{
	var test = val.value.substring(0, (strLen) - 1);
	val.value = test;
	}
}

function ValInt(val)
{
	var strlist = /[0123456789]/;
	var strval = val.value;
	var strLen = strval.length;
	var lastchar = val.value.charAt((strLen) - 1);

	if(lastchar.search(strlist) == -1)
	{
	var test = val.value.substring(0, (strLen) - 1);
	val.value = test;
	}
}

function deleteCharge()
{
	return confirm("Are you sure you want to delete this charge and all of its entries?");
}

function deleteEntry()
{
	return confirm("Are you sure you want to delete this entry?");
}

function copyToCrew()
{
	return confirm("Are sure you wish to copy this to your crew?");
}

function validSignComment(val,signText)
{
	if(val == "")
	{
		alert("You must enter a comment before signing this timesheet.");
		return false;
	}
	return confirm(signText);
}

function validEditComment(val)
{
	if(val == "")
	{
		alert("You must enter a comment before editing this time pair.");
		return false;
	}
	return true;
}

function UpdateTime(sender) {    
    var text = sender.value;
    
    if(text.length == 0)
        return;
        
    var numericPart = '';
    var characterPart = '';
    var i;
    var current;
    
    // Break the text input into numeric and character parts for easier parsing			    
    for(i = 0; i < text.length; i++) {  
        current = text.charAt(i);			        
        
        if(IsNumeric(current))
            numericPart = numericPart + current;
            
        if(IsCharacter(current))
            characterPart = characterPart + current;
   }
   					   
   var formattedDate;
   var hour;
   var minute;
   var dayPart;
   
   // Handle AM/PM by looking for an a, otherwise treat as PM
    if(characterPart.indexOf('a') > -1 || characterPart.indexOf('A') > -1)
        dayPart = 'AM';
    else
        dayPart = 'PM';
     
    if(numericPart.length >= 4) {
		hour = numericPart.substring(0, 2);
		minute = numericPart.substring(2, 4);
	} else if(numericPart.length == 3) {	 
		hour = numericPart.substring(0, 1);
		minute = numericPart.substring(1, 3);
	} else if(numericPart.length == 2) {	 
		hour = numericPart.substring(0, 2);
		minute = '00';		
	} else if(numericPart.length == 1) {	 
		hour = numericPart.substring(0, 1);
		minute = '00';									
	} else {	 
		// Just use the current hour
		var d = new Date();
		hour = d.getHours();	
		
		minute = '00';
	}	
	
	// Apply 24 hour logic
	if(hour > 12) {
		if(hour <= 24) {
			hour -= 12;
			dayPart = 'PM';
		} else {
			// If the hour is still > 12 then the user has inputed something that doesn't
			// exist, so just use current hour
			
			hour = (new Date()).getHours();				
					
			if(hour > 12) {		
				hour -= 12;
				dayPart = 'PM';
			} else {		
				dayPart = 'AM';
			}
		}
	}	
	
	if(hour == 0) {
		hour = 12;
		dayPart = 'AM';
	}
		 
	 sender.value = hour + ':' + minute + ' ' + dayPart;				 
}

function IsNumeric(text) {
   var validChars = '0123456789';
   return (validChars.indexOf(text) > -1)                   
}

function IsCharacter(text) {
   var validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';   
   return (validChars.indexOf(text) > -1)                   
}	