var popUp; 

// global storage object for type-ahead info, including reset() method
var typeAheadInfo = {last:0, 
                     accumString:"", 
                     delay:1000,
                     timeout:null, 
                     reset:function() {this.last=0; this.accumString=""}
                    };
// function invoked by select element's onkeydown event handler
function typeAhead() {
	//Bail out on tab/enter
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13 || keyCode == 9) 
    {
		return false;
    }

   // limit processing to IE event model supporter; don't trap Ctrl+keys
   if (window.event && !window.event.ctrlKey) {
      // timer for current event
      var now = new Date();
      // process for an empty accumString or an event within [delay] ms of last
      if (typeAheadInfo.accumString == "" || now - typeAheadInfo.last < typeAheadInfo.delay) {
         // make shortcut event object reference
         var evt = window.event;
         // get reference to the select element
         var selectElem = evt.srcElement;
         // get typed character ASCII value
         var charCode = evt.keyCode;
         // correct for NumPad digits
 		 if (charCode >= 96 && charCode <=105) {
			charCode = charCode - 48;
		 }
         // get the actual character, converted to uppercase
         var newChar =  String.fromCharCode(charCode).toUpperCase();
         // append new character to accumString storage
         typeAheadInfo.accumString += newChar;
         // grab all select element option objects as an array
         var selectOptions = selectElem.options;
         // prepare local variables for use inside loop
         var txt, nearest;
         // look through all options for a match starting with accumString
         for (var i = 0; i < selectOptions.length; i++) {
            // convert each item's text to uppercase to facilitate comparison
            // (use value property if you want match to be for hidden option value)
            txt = selectOptions[i].text.toUpperCase();
            // record nearest lowest index, if applicable
            nearest = (typeAheadInfo.accumString > txt.substr(0, typeAheadInfo.accumString.length)) ? i : nearest;
            // process if accumString is at start of option text
            if (txt.indexOf(typeAheadInfo.accumString) == 0) {
               // stop any previous timeout timer
               clearTimeout(typeAheadInfo.timeout);
               // store current event's time in object 
               typeAheadInfo.last = now;
               // reset typeAhead properties in [delay] ms unless cleared beforehand
               typeAheadInfo.timeout = setTimeout("typeAheadInfo.reset()", typeAheadInfo.delay);
               // visibly select the matching item
               selectElem.selectedIndex = i;
               // prevent default event actions and propagation
               evt.cancelBubble = true;
               evt.returnValue = false;
               // exit function
               return false;   
            }            
         }
         // if a next lowest match exists, select it
         if (nearest != null) {
			selectElem.selectedIndex = nearest;
         }
      } else {
         // not a desired event, so clear timeout
         clearTimeout(typeAheadInfo.timeout);
      }
      // reset global object
      typeAheadInfo.reset();
   }
   return true;
}

function checkRecordPrompt(objSource,target)
{
	if (objSource.value != '')
	{
		var objTarget = eval(document.all.item(target));
		objTarget.value = '';
	}
}

function checkDigitPrompts(objSource,target0,target1,target2,target3,target4,target5,target6,target7,target8,target9)
{
	if (objSource.value != '')
	{
		var objTarget
		objTarget = eval(document.all.item(target0));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target1));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target2));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target3));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target4));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target5));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target6));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target7));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target8));
		objTarget.value = '';		
		objTarget = eval(document.all.item(target9));
		objTarget.value = '';		
	}
}

function buildLocationList(target)
{
	var targetSel = target.options[target.selectedIndex].value;
	var sel = -1;

	if (target.options.length == 1) 
	{
		target.options.length = 0;
		for (loop=0; loop < aryLocation[0].length; loop++)
			{
				var element = new Option(aryLocation[1][loop],aryLocation[0][loop]);
				target.options[loop] = element;
				if (target.options[loop].value == targetSel) sel = loop;
			}
			if (sel == -1) target.options[0].selected = true;
			else target.options[sel].selected = true;
	}

	return true;
}

function buildVehicleList(target)
{
	var targetSel = target.options[target.selectedIndex].value;
	var sel = -1;

	if (target.options.length == 1) 
	{
		/*target.options.length = 0;*/
		for (loop=0; loop < aryVehicle[0].length; loop++)
			{
				var element = new Option(aryVehicle[1][loop],aryVehicle[0][loop]);
				target.options[loop+1] = element;
				if (target.options[loop+1].value == targetSel) sel = loop+1;
			}
			if (sel == -1) target.options[1].selected = true;
			else target.options[sel].selected = true;
			target.options[0] = null;
			return true;
			
	}

	return true;
}

function OpenCalendar(idname, postBack)
{
	popUp = window.open('/VSSWeb/Utils/Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'titlebar=0,width=170,height=235,left=200,top=250');
}

function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (postBack)
		__doPostBack(id,'');
}		

function SoundWindowOpen(wav)
{
	SoundPopUp = window.open('/Messages/' + wav,'SoundPopup','titlebar=0,width=400,height=250,left=200,top=250');
}

function OpenUserGuide()
{
	UGPopUp = window.open('../../VSSWeb/ScheduleComUsersGuide.pdf','SoundPopup','titlebar=0,resizable,left=0,top=0');
}

function OpenReportWindow(url)
{
	ReportPopUp = window.open('../../VSSWeb/WebModules/Report/' + url,'ReportPopup','toolbar=no,resizable=yes,left=0,top=0');
}

function OpenExcelWindow(url)
{
	ReportPopUp = window.open('../../VSSWeb/WebModules/Report/' + url,'ReportPopup','menubar,resizable=yes,left=0,top=0');
}

function OpenAnswerWindow(url)
{
	AnswerPopUp = window.open('../../VSSWeb/WebModules/Tranaction/' + url,'AnswerPopUp','toolbar=no,resizable=yes,width=800,height=500,left=100,top=150');
}

function OpenNewWindow(winName,target,id,tabindex,tabid,sortselection,locationselection,emptypeselection)
{
	if (winName = 'EditTrainee')
	{
		newWindow = window.open('../../WebModules/Transaction/TraineeEdit.aspx?id=' + id + '&sched=' + target + '&tabindex=' + tabindex + '&tabid=' + tabid + '&scrollpos=' + Form1.SchedGrid_saveScrollPos.value + '&sortselection=' + Form1.SchedGrid_ddlSort.value + '&locationselection=' + Form1.SchedGrid_ddlLocationFilter.value + '&emptypeselection=' + Form1.SchedGrid_ddlEmpTypeFilter.value,'popup','titlebar=0,width=400,height=250,left=200,top=250');
	}
	newWindow.focus();
}

function deleteError(text)
{
	alert(text);
}

/*function timeFormat(txt, control, txtDate)*/
function timeFormat(txt)
{
	var txtTime = txt.value;
	var chgDate = false;
	
	if (txtTime.length > 0)
	{
		if (!validateTime(txt.value))		
		{
			alert('Invalid time entered.');
			txt.focus();
			return false;
		}
		else
		{
			if (txt.value.indexOf(':') == -1)
			{
				if (txt.value.length == 4)
				{
					txt.value = txt.value.substring(0,2) + ':' + txt.value.substring(2);
				}
				else
				{
					txt.value = txt.value.substring(0,1) + ':' + txt.value.substring(1);
				}
			}
			return true;
		}
	}
	return true;
}


function validateTime ( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid 12 hour time format. Seconds are optional.
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

REMARKS: Returns True for time formats such as:
  HH:MM or HH:MM:SS or HH:MM:SS.mmm (where the
  .mmm is milliseconds as used in SQL Server 
  datetime datatype.  Also, the .mmm portion will 
  accept 1 to 3 digits after the period)
  var objRegExp = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
  var objRegExp = /((?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9])/;
  var objRegExp = /^(20|21|22|23|[0-1]\d)[0-5]\d$/;
  var objRegExp = /var objRegExp = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;/
  var objRegExp = /((?:0?[0-9]|1[0-9]|2[0-3]):?[0-5][0-9])/;

  var objRegExp = /^((?:0?[0-9]{1}|1[0-9]{1}|2[0-3]{1}):?[0-5]{1}[0-9]{1})$/;

*************************************************/
  
  var objRegExp = /^((0?[0-9]{1}|1[0-9]{1}|2[0-3]{1}):?[0-5]{1}[0-9]{1})$/;
  
  return objRegExp.test( strValue );

}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}
function confirmDelete() 
{
	return confirm('Continue with Delete?');
}

