function SetSelectedComboItemByIndex(combo, idx)
{
   combo.options[idx].selected = true;
}

function SetSelectedComboItemByValue(combo, val)
{
  for (i=0;i<combo.options.length;i++)
  {
    if (combo.options[i].value == val)
    {
      combo.options[i].selected = true;
    }
  }
}

function showMessage(aMessage)
{
  alert(aMessage);
}

function returnFalse()
{
  return false;
}

  function SetCalendarParms(FormField, Button)
  {
  Calendar.setup(
    {
      inputField  : FormField,        // ID of the input field
      ifFormat    : '%d/%b/%Y',    // the date format
      button      : Button       // ID of the button
    }
  );
  }
  
function ToggleVisibility(element)
{
  if(element.style.display == 'none')
  {element.style.display='';}
  else
  {element.style.display='none';}
}  
  
function GetDateDifferenceAsDays(date1, date2)
{
  try
  {
    var int1;
    var int2;
    int1 = Date.parse(date1);
    int2 = Date.parse(date2);
    alert(date1);
    alert(date2);
    return (int2-int1)*360000;
  }
  catch(ex)
  {
    return 0
  }

}


function ValidateVehicleBlockingSubmission()
{
var submitIt;
submitIt = true;
var submitValue;

if (form.ToggleAuth != undefined)
{
for (var i = 0; i < form.ToggleAuth.length; i++) 
{
		if(form.ToggleAuth[i].checked) 
		{
			submitValue = form.ToggleAuth[i].value;
		}
}

// value of 2 = accept and submit
if (submitValue == 2 && form.VehicleBlockingDeclaration1 != undefined && form.VehicleBlockingDeclaration2 != undefined && form.VehicleBlockingDeclaration3 != undefined )
{
if (!form.VehicleBlockingDeclaration1.checked || !form.VehicleBlockingDeclaration2.checked || !form.VehicleBlockingDeclaration3.checked)
{
  submitIt = false;
  alert('Please accept all the declarations');
}
}
}

if(submitIt)
{
  if (form.UnBlockOption != undefined)
    if (form.UnBlockOption.checked)
      submitIt = confirm("Are you sure you want to unblock this vehicle?");
      
  if (form.DeleteOption != undefined)
    if (form.DeleteOption.checked)
      submitIt = confirm("Are you sure you want to delete this blocking request?");
}

if (submitIt)
  {
  form.Reloading.value='N';  
  form.submit();
}
}

function SetDateInControl(setDate, aControl)
{
  if (setDate)
  {
  var d = new Date();
  var dd = d.getDate();
  var mm = d.getMonth();
  var yy = d.getFullYear();
  var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
  var mmstr = months[mm];
  
  aControl.value = dd+"/"+mmstr+"/"+yy;
  }
  else
  {
  aControl.value = "";
  }
}

function groupSelect_OnSelectedIndexChanged()
{
    var theForm = document.getElementById("menuForm");
    var nextActionHdn = document.getElementById("NextAction");
    nextActionHdn.value = "Select";
    theForm.submit();
}

function addMenuGroup()

{
    if (confirm("Are you sure you want to add a new menu group below the selected menu group?"))
    {
        var groupName = prompt("Please enter the name for the new group:");
        var groupNameHdn = document.getElementById("GroupName");    
        var theForm = document.getElementById("menuForm");
        var nextActionHdn = document.getElementById("NextAction");
        
        nextActionHdn.value = "AddGroup";
        groupNameHdn.value = groupName;
        theForm.submit();
    }
}

function deleteMenuGroup()
{
    if (confirm("Deleting this menu group will remove it permanently and all menu items contained within the group. Are you sure you want to delete this menu group?"))
    {
        var theForm = document.getElementById("menuForm");
        var nextActionHdn = document.getElementById("NextAction");
        var menuIdHdn = document.getElementById("MenuId");
        var menuId = document.getElementById("groupSelect")
        
        nextActionHdn.value = "DeleteGroup";
        menuIdHdn.value = menuId[menuId.selectedIndex].value;
        
        theForm.submit();
    }
}

function addMenuItem()
{
    var editRow = document.getElementById("editRow");
    var menuIdHdn = document.getElementById("MenuId");
    var nextActionHdn = document.getElementById("NextAction");
    
    nextActionHdn.value = "Update";
    editRow.style.display = 'block';
    menuIdHdn.value = '0';
}

function editMenuItem(menuId, isEditable, menuText, menuSecurity, menuUrl, openInNewWindow)
{
    var theForm = document.getElementById("menuForm");
    var editRow = document.getElementById("editRow");
    var menuIdHdn = document.getElementById("MenuId");
    var isEditableHdn = document.getElementById("IsEditable");
    var nextActionHdn = document.getElementById("NextAction");

    
    var menuTextTxt = document.getElementById("inputMenuText");
    var menuUrlTxt = document.getElementById("inputMenuUrl");
    var menuFormSecurity = document.getElementById("inputMenuSecurity");
    var openInNewWindowChk = document.getElementById("chkOpenInNewWindow");
    
    editRow.style.display = 'block';
    menuIdHdn.value = menuId;
    isEditableHdn.value = isEditable;
    nextActionHdn.value = "Update";
    
    //Populate text boxes with values from table
    menuTextTxt.value = menuText;
    menuUrlTxt.value = menuUrl;
    menuFormSecurity.value = menuSecurity;
    openInNewWindowChk.checked = openInNewWindow;
    
    //Set url and privileges textboxes to disabled if no editable
    menuUrlTxt.disabled = openInNewWindowChk.disabled = !isEditable;
}

function deleteMenuItem(menuId)
{
    if (confirm("Deleting this menu item will remove it permanently. Are you sure you want to delete this menu item?"))
    {
        var theForm = document.getElementById("menuForm");
        var editRow = document.getElementById("editRow");
        var menuIdHdn = document.getElementById("MenuId");
        var nextActionHdn = document.getElementById("NextAction");
        
        nextActionHdn.value = "Delete";
        menuIdHdn.value = menuId;
        theForm.submit();
    }
}

function verifyAndSubmitMenuItem()
{
    var returnValue = true;
    var menuText = document.getElementById("inputMenuText");
    var menuUrl = document.getElementById("inputMenuUrl");
    var openInNewWindowChk = document.getElementById("chkOpenInNewWindow");
    
    //validators
    var vldMenuText = document.getElementById("vldMenuText");
    var vldMenuUrl = document.getElementById("vldMenuUrl");

    
    if (menuText.value == '')
    {
        vldMenuText.style.display = 'block';
        returnValue = false;
    }
    else
        vldMenuText.style.display = 'none';

    if (menuUrl.value == '')
    {
        vldMenuUrl.style.display = 'block';
        returnValue = false;
    }
    else
        vldMenuUrl.style.display = 'none';

    return returnValue;
}

// Following block autocompletes drop down lists as the user types 
// Tracks key presses and builds up a string to compare against the items in the list and sets the new selected index if found
// auto-completes as a user is typing in a combo. Tracks key presses and builds up a string
// to compare against the items in the list and sets the new selected index if found
var lastList;
var currWord;

// stops backspace from doing history.back when entered in the control
function handleBackspace()
{
  var code;
  var e = window.event;

  if (e.keyCode) 
    code = e.keyCode;
  else if (e.which) 
    code = e.which;

  if (code == 8)
  {        
    e.cancelBubble = true;
    e.returnValue = false;
    return false;
  }
}

function clearcurrWord()
{
  currWord="";
  lastList = null;
}

function fillin(selectList)
{
  // check we are still recording input from the same drop down
  if (lastList != selectList)
  {
    currWord = "";
    lastList = selectList;
  }

  // get the key pressed
  var code;
  var e = window.event;
  if (e.keyCode) 
    code = e.keyCode;
  else if (e.which) 
    code = e.which;
  
  var character = String.fromCharCode(code);

  if (code==8) // if backspace, drop the last character
  {
    if (currWord.length > 0)
    {
      currWord = currWord.substr(0,currWord.length-1)
    }
  }
  else // else build up the string
  {
  currWord = currWord+character;
  }

  // try and find the current word in the list
  var mytext = currWord;
  sellength = selectList.length;
  var i;
 
  for(i = 0; i<sellength; i++)
  {
    if (selectList.options[i].text.toLowerCase().indexOf(mytext.toLowerCase(),0) == 0)
    {
      selectList.options[i].selected = true;
      break;
    }
  }
}

function showPopupHelp(popupHelpId)
{
  var iMyWidth;
  var iMyHeight;
  //half the screen width minus half the new window width (plus 5 pixel borders).
  iMyWidth = (window.screen.width/2) - (200 + 10);
  //half the screen height minus half the new window height (plus title and status bars).
  iMyHeight = (window.screen.height/2) - (100 + 50);


  window.open('popuphelp.asp?id=' + popupHelpId, 'popupHelp', 
    'height=200,width=400,status=no,toolbar=no,menubar=no,location=no, scrollbars=yes, left=' 
      + iMyWidth + ',top=' + iMyHeight + ',screenX=' + iMyWidth + ',screenY=' + iMyHeight)
}

function focusControl(aControlName)
{
  
	        try
	        {
    	        theElement =  document.getElementById(aControlName);
	            if (theElement != null)
	            {
	    	        theElement.focus();
	            }
            }
	        catch(e) {}
}
