var popUp;
var popUpRes;

function getThisYear()
{
	var yr = new Date();
	var y = yr.getYear();
	if (y < 100)
		y = y + 1900;
	return y;
}

function OpenCalendarRecent(idname, postBack)
{
	var yrStart = getThisYear() - 10;
	var yrEnd   = getThisYear() + 10;
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&yrStart=' + eval(yrStart) + '&yrEnd=' + eval(yrEnd) + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=165,height=218,left=200,top=250');
}

function OpenCalendar(idname, yrStart, yrEnd, postBack)
{
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&yrStart=' + eval(yrStart) + '&yrEnd=' + eval(yrEnd) + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=165,height=218,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 OpenResourceCalendar(eventdate, outofservicedate)
{
	popUpRes = window.open('resourceCalendar.aspx?event=' + eventdate + '&outofservice=' + outofservicedate, 
		'popuprescal', 
		'width=165,height=190,left=200,top=250');
}

function setExpiryVisibility()
{
	alert(0);
	if (document.all.cBoxExpires)
	{
		alert(1);
		if (document.all.cBoxExpires.checked)
		{
			alert(2);
			Form1.elements["TableReminderDetails"].style.display = inline;
		}
		else
		{
			alert(3);
			Form1.elements["TableReminderDetails"].style.display = none;
		}
	}
}

function convertToLbs(kilos)
{
	if ("NaN" == Math.round(parseFloat(kilos)))
		kilos = 0;
	//alert(kilos + "kilos to lbs=" + Math.round(parseFloat(kilos)* 100 * 2.205) / 100);
	var weight = Math.round(parseFloat(kilos)* 100 * 2.205) / 100;
	if (Math.abs(weight - Math.round(weight)) < 0.03)
		weight = Math.round(weight);
	
	return eval(weight) + "";
}
function convertToKilos(lbs)
{
	if ("NaN" == Math.round(parseFloat(lbs)))
		lbs = 0;
	//alert(lbs + "lbs to kilos=" + Math.round(parseFloat(lbs)* 100 / 2.205) / 100);
	var weight = Math.round(parseFloat(lbs)* 100 / 2.205) / 100;
	if (Math.abs(weight - Math.round(weight)) < 0.03)
		weight = Math.round(weight);
	
	return eval(weight) + "";
}

function convertToInches(cms)
{
	cms = eval(cms);
	//alert(cms + "cms to Inches=" + Math.round(parseFloat(cms)* 100 / 2.54) / 100);
	//return eval(Math.round(parseFloat(cms)* 100 / 2.54) / 100) + "";

	var height = Math.round(parseFloat(cms)* 100 / 2.54) / 100;
	if (Math.abs(height - Math.round(height)) < 0.03)
		height = Math.round(height);
	
	return eval(height) + "";
}
function convertToCms(inches)
{
	inches = eval(inches);
	//alert(inches + "Inches to cms=" + Math.round(parseFloat(inches)* 100 * 2.54) / 100);
	//return eval(Math.round(parseFloat(inches)* 100 * 2.54) / 100) + "";

	var height = Math.round(parseFloat(inches)* 100 * 2.54) / 100;
	if (Math.abs(height - Math.round(height)) < 0.03)
		height = Math.round(height);
	
	return eval(height) + "";
}

function setTableWidths()
{
	var widest = Table1.offsetWidth;
	if (widest < Table2.offsetWidth)
		widest = Table2.offsetWidth;
	if (widest < Table3.offsetWidth)
		widest = Table3.offsetWidth;
	if (widest < Table4.offsetWidth)
		widest = Table4.offsetWidth;
	if (widest < Table5.offsetWidth)
		widest = Table5.offsetWidth;
	Table1.width = widest;
	Table2.width = widest;
}
function CloseWindow()
{
	if (popUpOriginal)
		popUpOriginal.close();
	if (parent.name == "DocumentViewer")
		parent.close;
	else
		self.close();
}
// Start of Ray Scott Copyright protected script
// Copyright 1999 - 2002 by Ray Stott, Pop-up Windows Script ver 2.0
// OK to use if this copyright is included
// Script is available at http://www.crays.com/jsc
// modified by Peak Performance Software Corp. for customized usage          

var popWin = null    // use this when referring to pop-up window
var winCount = 0
var winName = "rsvpPop"
function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
  var d_winLeft = 20  // default, pixels from screen left to window left
  var d_winTop = 20   // default, pixels from screen top to window top
  winName = "rsvpPop"// + winCount++ //unique name for each pop-up window
  closePopWin()           // close any previously opened pop-up window
  if (openPopWin.arguments.length >= 4)  // any additional features? 
    winFeatures = "," + winFeatures
  else 
    winFeatures = "" 
  if (openPopWin.arguments.length == 6)  // location specified
    winFeatures += getLocation(winWidth, winHeight, winLeft, winTop)
  else
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
  popWin = window.open(winURL, winName, "width=" + winWidth 
           + ",height=" + winHeight + winFeatures)
  }
function closePopWin(){    // close pop-up window if it is open 
  if (navigator.appName != "Microsoft Internet Explorer" 
      || parseInt(navigator.appVersion) >=4) //do not close if early IE
     if(popWin != null) if(!popWin.closed) popWin.close(); 
  }
function getLocation(winWidth, winHeight, winLeft, winTop){
  var winLocation = ""
  if (navigator.appName != "Microsoft Internet Explorer" 
      || parseInt(navigator.appVersion) >=4) {
    if (winLeft < 0)
      winLeft = screen.width - winWidth + winLeft
    if (winTop < 0)
      winTop = screen.height - winHeight + winTop
    if (winTop == "cen")
      winTop = (screen.height - winHeight)/2 - 20
    if (winLeft == "cen")
      winLeft = (screen.width - winWidth)/2
    if (winLeft>0 & winTop>0)
      winLocation =  ",screenX=" + winLeft + ",left=" + winLeft	
                  + ",screenY=" + winTop + ",top=" + winTop
    else
      winLocation = ""
    }
  return winLocation
  }
// End of Ray Scott Copyright protected script

function popCourseRequest(courseType)
{
	openPopWin("requestCourse.aspx?Type=" + courseType, 780, 500, "resizable=1,dependent=1", -10, 1)
}

function popFleetRegistration(courseID)
{
	openPopWin("fleetRegistration.aspx?ID=" + courseID, screen.width-20, screen.height-20, "resizable=1,scrollbars=1,dependent=1", -10, 1)
}

function popOpenRegistration(courseID)
{
	openPopWin("openRegistration.aspx?ID=" + courseID, screen.height-20, 750, "resizable=1,scrollbars=1,dependent=1", -10, 1)
}

function popOpenManager(courseID)
{
    if (popWin != null && !popWin.closed)
		alert("Please save/close current manager window first.");
	else
		openPopWin("manager.aspx?ID=" + courseID, 780, 550, "resizable=1,scrollbars=1,dependent=1", -10, 1);
}

function openOriginal(update)
{
	if (update == "update")
	{
		if (document.all["originalfilename"].value.length < 1)
		{
			popWin = openPopWin("viewOriginal.aspx?Close=true", 200, 100, "resizable", -1, 1)
		}
		else
		{
			if (popWin != null)
			{
				if (popWin.location != document.all["originalfilename"].value)
					popWin.location=document.all["originalfilename"].value;
			}
			else
			{
				popWin = openPopWin(document.all["originalfilename"].value, 1024, 750, "resizable", -1, 1)
			}
		}
	}
	//alert("out of openOriginal");
}

function setTemplateViewer(newURL)
{
	//alert("Setting TemplateViewer to URL:" + newURL);
	if (newURL != "dont")
	{
		if ((parent) && (parent.frames("TemplateViewer")))
		{
			//alert("TemplateViewer URL:" + newURL);
			//if (newURL == "update")
			//	parent.frames("TemplateViewer").location="getFDF.aspx";
			//else
				parent.frames("TemplateViewer").location=newURL;
			//alert("changed");
		}
	}
	//alert("out of setTemplateViewer");
}

function setDocViewerTableWidths()
{
	var widest = Table1.offsetWidth;
	if (widest < Table2.offsetWidth)
		widest = Table2.offsetWidth;
	//if (widest < Table3.offsetWidth)
	//	widest = Table3.offsetWidth;
	Table1.width = widest;
	Table2.width = widest;
	//Table3.width = widest;
}

function saveTabStripIndex()
{
	if (document.all["oTSIndexSave"] && document.all["oTS"])
		document.all["oTSIndexSave"].value = document.all["oTS"].selectedIndex;
}

function loadTabStripIndex()
{
	if (document.all["oTSIndexSave"] && document.all["oTS"])
	{
		if (eval(document.all["oTSIndexSave"].value) != "undefined")
			document.all["oTS"].selectedIndex = document.all["oTSIndexSave"].value;
		else
			document.all["oTS"].selectedIndex = 0;
	}
}
			
var initialSelectedIndex = 0;
var userEntry = "";
var postBackDone = false;

function ddlAutoSelector()
{
	this.oSelect = document.all[arguments[0]];
	if (this.oSelect)
	{
		userEntry = "";

		if (this.oSelect.attachEvent)
		{
			this.oSelect.attachEvent("onkeypress", handleKeyPress);
			this.oSelect.attachEvent("onfocus",		handleOnFocus);
			this.oSelect.attachEvent("onblur",		handleOnBlur);
		}
		if (arguments && (arguments.length > 1))
			this.oSelect.setAttribute("ccsPostBack",arguments[1]);
		
		initialSelectedIndex = this.oSelect.selectedIndex;
	}
}

function handleOnFocus()
{
	userEntry = "";
	initialSelectedIndex = document.all[event.srcElement.id].selectedIndex;
	postBackDone = false;
}

function handleOnBlur()
{
	if (!postBackDone && (document.all[event.srcElement.id].selectedIndex != initialSelectedIndex) && (document.all[event.srcElement.id].getAttribute("ccsPostBack") == true))
	{
		postBackDone = true;
		saveTabStripIndex();
		__doPostBack(event.srcElement.id,'');
	}
}

function isDigit(a)
{
	return ((a=="1") || (a=="2") || (a=="3") || (a=="4") || (a=="5") || (a=="6") || (a=="7") || (a=="8") || (a=="9") || (a=="0"))
}

function handleKeyPress()
{
	//alert(event.keyCode);
	this.oSelect = document.all[event.srcElement.id];

	if (event.keyCode == 27)
	{
		userEntry = "";
		this.oSelect.selectedIndex = initialSelectedIndex;
		this.oSelect.options(initialSelectedIndex).focus();
		this.oSelect.click();
	}
	else if ((event.keyCode > 96) && (event.keyCode < 123))
	{
		userEntry = userEntry + String.fromCharCode(event.keyCode - 32);
	}
	else if (event.keyCode > 31)
	{
		userEntry = userEntry + String.fromCharCode(event.keyCode);
	}
		
	window.status = userEntry;
	var i = -1;
	if (userEntry && userEntry.length > 0)
	{
		var isNumber = isDigit(userEntry.substring(0,1));
		if (!isNumber)
		{
			for (i=0; i < this.oSelect.options.length; i++)
			{
				if ((this.oSelect.options(i).text.length >= userEntry.length) && (this.oSelect.options(i).text.toUpperCase().substring(0,userEntry.length) == userEntry))
				{
					this.oSelect.options(i).focus();
					this.oSelect.selectedIndex = i;
					break;
				}
			}
		}
		if (isNumber || (i == this.oSelect.options.length))
		{
			for (i=0; i < this.oSelect.options.length; i++)
			{
				if (this.oSelect.options(i).text.toUpperCase().indexOf(userEntry) != -1)
				{
					this.oSelect.options(i).focus();
					this.oSelect.selectedIndex = i;
					break;
				}
			}
		}
	}
	var done = false;
	if (event.keyCode == 13)
	{
		userEntry = "";
		if ((i > -1) && (i < this.oSelect.options.length))
		{
			document.all[event.srcElement.id].selectedIndex = i;
			document.all[event.srcElement.id].options(i).focus();
		}
		done = true;
	}
	event.returnValue = done;
	if (done && (document.all[event.srcElement.id].selectedIndex != initialSelectedIndex) && (document.all[event.srcElement.id].getAttribute("ccsPostBack") == true))
	{
		postBackDone = true;
		__doPostBack(event.srcElement.id,'');
	}
}

function toggleCollapsable(handleId,itemID)
{
	h = MM_findObj(handleId);
	i = MM_findObj(itemID);
	if (!h || !i) return;
	if (i.className == "navSideCollapsed")
	{
		i.className = "navSideCollapsableItem";
		h.src = "Images/BulletOpen.GIF";
	}
	else
	{
		i.className = "navSideCollapsed";
		h.src = "Images/BulletCollapsed.GIF";
	}
}

// from Macromedia Studio:
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
