//----------------------------------------------------------------------------
// Title:		Utilities script
// Filename:	Utilities.js
// Purpose:		To contain all javascript functions for centralized management
// Created:		2/5/2004
// Updated:		2/5/2004
// Owner:		National Elevator Industry Education Program
//				Copyright © 2004
//----------------------------------------------------------------------------

//function to change 2-digit year to 4 digits
function formatClassDate(txtClassDate) {
	var txtClassDate=document.getElementById(txtClassDate);
	var thisClassDate=new Date(txtClassDate.value);
	var theYear=thisClassDate.getFullYear();
	var now=new Date();
	var currentYear=now.getFullYear();
	if(theYear<currentYear-50){
		theYear=theYear + 100;
	}
	txtClassDate.value=(thisClassDate.getMonth()+1) + "/" + thisClassDate.getDate() + "/" + theYear;
}

//function to return the next class date for a weekday
function updateClassDate(txtClassDate, ddlWeekday) {
	var txtClassDate=document.getElementById(txtClassDate);
	var ddlWeekday=document.getElementById(ddlWeekday);
	var thisClassDate=new Date(txtClassDate.value);
	if(((String)(thisClassDate.toString()).indexOf("NaN"))==-1) {
		var thisDay=thisClassDate.getDay();
		//start with Sunday to check for weekday
		var thisClassDate=new Date(thisClassDate.valueOf()-(thisDay*1000*60*60*24))
		var c=0;
		while(ddlWeekday.selectedIndex!=thisClassDate.getDay() && c<8) {
			thisClassDate=new Date(thisClassDate.valueOf() + (1000*60*60*24));
			c++;
		}
		txtClassDate.value=(thisClassDate.getMonth()+1) + "/" + thisClassDate.getDate() + "/" + thisClassDate.getFullYear();
	}
}

//function to return the weekday for any date
function updateWeekday(txtClassDate, ddlWeekday) {
	var txtClassDate=document.getElementById(txtClassDate);
	var ddlWeekday=document.getElementById(ddlWeekday);
	var thisClassDate=new Date(txtClassDate.value);
	ddlWeekday.selectedIndex=thisClassDate.getDay();
}

//function to select the local from a shortcut textbox
function selectLocal(ddlLocal, txtShortcut) 
{
	var ddlLocal=document.getElementById(ddlLocal);
	var txtShortcut=document.getElementById(txtShortcut);
	var strLocal=txtShortcut.value
	var c;
	for (c=ddlLocal.options.length-1; c>=0; c--) 
	{
		if (strLocal==ddlLocal.options[c].value.substring(0,strLocal.length)) 
			ddlLocal.options[c].selected = true;
	}
}
function selectLocalByText(ddlLocal, txtShortcut) 
{
	var ddlLocal = document.getElementById(ddlLocal);
	var txtShortcut = document.getElementById(txtShortcut);
	var strLocal = txtShortcut.value
	var c;
	for (c = ddlLocal.options.length - 1; c >= 0; c--) 
	{
		if (strLocal == ddlLocal.options[c].text.substring(0, strLocal.length)) 
			ddlLocal.options[c].selected = true;
	}
}

//function to prevent users from checking / unchecking checkboxes
//i.e., for display only purposes
function noCheck(ckb){
	if(ckb.checked)
		ckb.checked = false;
	else
		ckb.checked = true;
}

//----------------------------------------------------
// Pop-up window function
//----------------------------------------------------
function popup(url, width, height, windowName, scroll, menubar, resizable)
{
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;

  // default window name if not passed to function
  if (!windowName) { windowName = "popup_window"; }
  if((!scroll) || (scroll=="")) { scroll="yes"; }
  if((!menubar) || (menubar=="")) { menubar="yes"; }
  if((!resizable) || (resizable=="")) { resizable="yes"; }
  
  var popupWindow = window.open ("", windowName,
        "toolbar=no,width=" + width + ",height=" + height +
        ",left=" + left + ",top=" + top +
        ",directories=no,status=no,scrollbars="+scroll+",resizable=" + resizable + ",menubar=" + menubar);
  
  if(popupWindow) 
  { 
    popupWindow.focus();
    popupWindow.document.write("<h1 style='font-family:Arial;color:DimGray;position:relative;left=" + (width/2-(width/10)) + ";top=" + (height/2-(height/10)) + ";'>Loading...</h1>");
    popupWindow.document.location.replace(url);
  }
}

// Disable ENTER button when requested ...
// In any input tags place the following code
// onKeyPress="return noenter();"
function noenter() 
{
   return !(window.event && window.event.keyCode == 13);
}

function submitButton(btnId)
{
    if(window.event && window.event.keyCode == 13)
    { 
        var btn = document.getElementById(btnId); 
        if(btn) btn.click();
    }
}