var wwwroot = '';

//Copy to clipboard
function ClipBoard(IDField) 
{
	Copied = document.all[IDField].createTextRange();
	Copied.execCommand("Copy");
	alert('COPY SUCCEEDED\n============\n\nTo paste the url into your email, choose EDIT --> PASTE within your email program.');

}

//Print any page
function print_page ()
	{
		var current_leftstate = '';
		
		current_leftstate = document.all['Left'].style.display;
		
		LeftState("none");
		
		if (!document.all['IDRight'] == false)
			document.all['IDRight'].style.display = 'none';
		
		
		//Print the document
		window.print();
		
		//Set current leftstate
		LeftState(current_leftstate);
		
		if (!document.all['IDRight'] == false)
			document.all['IDRight'].style.display = 'inline';
	}

//Sets the posision on the mouse, and the button state
var MouseY;
var MouseX;
var MouseIsDown;

function SetMouseUp()
{
	MouseIsDown = false;
}

function SetMouseDown()
{
	MouseIsDown = true;
}

function SetMouse()
{
	MouseY = window.event.y;
	MouseX = window.event.x;
}

document.onmousemove = SetMouse;
document.onmouseup = SetMouseUp;
document.onmousedown = SetMouseDown;

//Open reports
function go_reports()
{
	if (document.frmselecter_reports.select_reports.options[document.frmselecter_reports.select_reports.selectedIndex].value != "none")
		location = document.frmselecter_reports.select_reports.options[document.frmselecter_reports.select_reports.selectedIndex].value
}

//Used for showing boxes with info as a layer
function info(layer)
{
	if ((document.all[layer].style.visibility=='hidden' || document.all[layer].style.visibility==''))
	{
		state = 'visible';
		ShowHideComboes('hidden');
	}
	else
	{
		state = 'hidden';
		ShowHideComboes('visible');
	}
	

	document.all[layer].style.visibility=state;
	document.all[layer].style.left=MouseX+document.body.scrollLeft-100;
	document.all[layer].style.top=MouseY+document.body.scrollTop+10;
}

//Moves a layer
function MoveInfoLayer(layer)
{
	if (MouseIsDown)
	{
		document.all[layer].style.left=MouseX+document.body.scrollLeft+-60;
		document.all[layer].style.top=MouseY+document.body.scrollTop-20;
	}
}

//Globals for use with menues
var LastMenu = "none";
var LastChildMenu = "none";
var SelectedElement = "none";

/*
Function for showing or hiding comboes, as they seem to allways be on top (over layers, ex. menues)
This problem is the same with the report activex object, so we do the same with that.
*/	
function ShowHideComboes(state)
{
	for (var u=0;u<document.forms.length;u++)
	{
		var frm = document.forms[u];

		for (var i=0;i<frm.elements.length;i++)
		{
			var e = frm.elements[i];
			var name = new String(e.name);
			if ((e.type == 'select-one' || e.type == 'select-multiple') && e.style.tag!=='hiddenallways')
			{
				e.style.visibility = state;
			}
		}
	}
	
	//Hide and show the report object if present
	if (!document.all['IDReportObject'] == false)
		document.all['IDReportObject'].style.visibility=state;
}

//Drop down menu
function menu(layer,left,top)
{
	if (!document.all[layer] == false)
	{
		LastMenu = layer;
	
		if ((document.all[layer].style.visibility=='hidden' || document.all[layer].style.visibility==''))
		{
			state = 'visible';
			ShowHideComboes('hidden');
		}
		else
		{
			state = 'hidden';
			LastMenu = 'none';
			ShowHideComboes('visible');			
		}
		
		document.all[layer].style.visibility=state;
		//document.all[layer].style.left=left;
		//document.all[layer].style.top=top;
	}
}

//Close dropdown menu
function CloseLastMenu(CurrentMenu,left,top)
{
	if (!CurrentMenu)
		var CurrentMenu = "";

	if (LastMenu !== "none")
	{

		if((CurrentMenu !== LastMenu) && (CurrentMenu !== ""))
		{
			//Close the last menu
			document.all[LastMenu].style.visibility="hidden";
			//opens the current menu
			document.all[CurrentMenu].style.visibility="visible";
			//document.all[CurrentMenu].style.left=left;
			//document.all[CurrentMenu].style.top=top;
			LastMenu = CurrentMenu;
		}

		if (CurrentMenu == "")
		{
			document.all[LastMenu].style.visibility="hidden";
			LastMenu = 'none';
			ShowHideComboes('visible');						
		}
	}
}

//Child drop down menu
function childmenu(layer,left,top)
{
	var layer = new String(layer);
	
	if (!document.all[layer] == false)
	{
		if (layer.search(SelectedElement) !== -1)
		{
			LastChildMenu = layer;
			document.all[layer].style.visibility="visible";
			//document.all[layer].style.left=left;
			//document.all[layer].style.top=top
		}
	}
	else
		LastChildMenu = 'none';
}

//Close child dropdown menu
function CloseLastChildMenu(CurrentMenu,left,top)
{
	if (!CurrentMenu)
		var CurrentMenu = "";

	if (LastChildMenu !== "none")
	{
		if((CurrentMenu !== LastChildMenu) && (CurrentMenu !== ""))
		{
			//Close the last menu
			document.all[LastChildMenu].style.visibility="hidden";
			//opens the current menu
			if (!document.all[CurrentMenu] == false)
			{
				document.all[CurrentMenu].style.visibility="visible";
				//document.all[CurrentMenu].style.left=left;
				//document.all[CurrentMenu].style.top=top;
				LastChildMenu = CurrentMenu;
			}

		}

		if (CurrentMenu == "")
		{
			document.all[LastChildMenu].style.visibility="hidden";
			LastChildMenu = 'none';
		}
	}
}

function onlyNumbers(obj,value,set0)
{
	var nr = new Number(value)
	var str = new String(value)
	var counter = 0
	var modified = 0

	//Removes white spaces
	if (str.indexOf(" ") != -1)
	{
		modified = 1;
		do
			str = str.replace(" ",'')
		while (str.indexOf(" ") != -1)
	}	

	//Remove characters that are not numbers
	if (isNaN(nr))
	{
		modified = 1;
		do
		{
			if (isNaN(str.charAt(counter)))
			{
				//Replace comma with periode
				if (str.charAt(counter)==',')
				{
					str = str.replace(str.charAt(counter),'.');
					counter = counter + 1
				}
				else
					str = str.replace(str.charAt(counter),'');
			}
			else
				counter = counter + 1
		}
		while(counter <= str.length)
	}

	if (modified == 1)
		obj.value = str	
	if ((obj.value == '' && set0 == 1))
	{
		obj.value = 0;
		obj.select();
	}
}
	
// Uppercase
function uppercase(frmObj) 
	{
		frmObj.value = frmObj.value.toUpperCase();
	}

// Make sure the user don't type invalid characters
function validateContainerNumber(obj,value)
	{
		var str = new String(value)
		var msg = "You can't use - ' or space in containernumber"
	
		if ((str.indexOf("-") != -1) || (str.indexOf(" ") != -1) || (str.indexOf("'") != -1))
		{
			alert(msg);
			do
			{
				str = str.replace("-",'');
				str = str.replace(" ",'');
				str = str.replace("'",'');
			}
			while ((str.indexOf("-") != -1) || (str.indexOf(" ") != -1))
	
			obj.value = str
		}
	}

function Summary(ContainerID,ContainerNumber)
{
	var frm = document.forms.frmSummary
	
	frm.ContainerID.value = ContainerID;
	frm.txtContainerNumber.value = ContainerNumber;
	
	frm.submit();
}	

function Openwin(swindowname,oformname,iwidth,iheight,iScroll)
{
	actionwindow=window.open("",swindowname,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + iScroll + ",resizable=0,width=" + iwidth + ",height=" + iheight + "");
	window.actionwindow.focus();
	oformname.submit();
	return window.actionwindow;
}

function ClearListBox(objName)
{
	var obj = document.all[objName]
	
	{
	for (var i = 0; i < obj.length; i++) 
		obj[i].selected = false;
	}
} 

function Logout()
{
	if (confirm('LOG OUT WARNING\n\n- You do NOT have to click this link if you intend to use this computer again. We will check your login information automatically the next time you visit us.\n\n- We will now delete all stored information about your login information.\n\nDo you want to continue logout?')) 
		window.top.location=wwwroot+'/commonfiles/login/default.cfm?page=3';
}

function execute(name, value, path, domain, secure)
{
	var expires = new Date(3000,01,01);
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "; path=\/") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
	
	if (value=="inline")
	{
		document.all['IDInOut'].src=wwwroot+'/images/web/in.gif';
		document.all['IDInOut'].alt='Hide the left menu';
	}
	else
	{
		document.all['IDInOut'].src=wwwroot+'/images/web/out.gif';
		document.all['IDInOut'].alt='Display the left menu';
	}
}					

function LeftState(state)
{
	//Set the state
	document.all['Left'].style.display=state;

	//Sets a cookie with the state of left
	execute('LeftState',document.all['Left'].style.display);
	
	//See ref: leftstate1
	if (!document.all['Buttons']==false)
		if (document.all['Buttons'].style.visibility="visible")
			document.all['Buttons'].focus();
}

function setLeftState (obj)
{
	var src = new String(obj.src)
	if (src.indexOf("in.gif")!==-1) 
		LeftState("none");
	else
		LeftState("inline");
}

//Called in presentation.cfm
function submit_timer()
{
	actionwindow=window.open("","timer_window","width=640,height=320,resizable=1,status=1,menubar=0,toolbar=0");
	actionwindow.blur();
	if (!document.forms.frmTimer==false)
		document.forms.frmTimer.submit();
} 

//New PAGE from reg forms
function NewActivityInRegistrationForm(lPage,lwwwroot) {

	switch (lPage) 
	{ 
		//Discharge
		case 100 :
			window.top.location = lwwwroot + "/product/terminal/registration/discharge/default.cfm?page=1";
			break; 
	
		//Strip
		case 110 :
			window.top.location = lwwwroot + "/product/terminal/registration/strip/default.cfm?page=1";
			break; 
	
		//Gate Out
		case 120 :
			window.top.location = lwwwroot + "/product/terminal/registration/gate/default.cfm?page=1&activitytypeid=2";
			break; 
		
		//Gate In	  
		case 130 :
			window.top.location = lwwwroot + "/product/terminal/registration/gate/default.cfm?page=1&activitytypeid=3";
			break; 
	
		//Depot In  
		case 140 :
			window.top.location = lwwwroot + "/product/terminal/registration/depot/default.cfm?page=1&activitytypeid=6";
			break; 
	
		//Depot Out  
		case 150 :
			window.top.location = lwwwroot + "/product/terminal/registration/depot/default.cfm?page=1&activitytypeid=7";
			break; 
	
		//Load
		case 160 :
			window.top.location = lwwwroot + "/product/terminal/registration/load/default.cfm?page=1";
			break; 
				
		default : 
			alert('Not implemented');
	} 			
}

function validate_email(email) 
{
	var e = new String(email);
	var message = '';
	
	if(-1 == e.indexOf("@"))
		message = "Your email must have a '@'.\n"; 
	if(-1 != e.indexOf(","))
		message = message + "Your email must not have a ',' in it\n"; 
	if(-1 != e.indexOf("#"))
		message = message + "Your email must not have an '#' in it.\n"; 
	if(-1 != e.indexOf("!"))
		message = message + "Your email must not have a '!' in it.\n"; 
	if(-1 != e.indexOf(" "))
		message = message + "Your email must not have a space in it.\n";
	if(e.length == (e.indexOf("@")+1))
		message = message + "Your email must have a domain name after the '@'.\n";       

	return message;
}

//Function to display / hide the input form descriptions
function formheaderinfo ()
{
	if (document.all['IDdescription'].style.display == 'inline')
		{
		document.all['IDdescription'].style.display = "none";
		//document.all['IDtdImage'].style.display = "none";
		}
	else
		{
		document.all['IDdescription'].style.display = "inline";
 		//document.all['IDtdImage'].style.display = "none";
		}
}

//Display Optional Info
function OptionalInfo(IDName)
{
	if (document.all[IDName].style.display == 'inline')
		document.all[IDName].style.display = "none";
	else
		document.all[IDName].style.display = "inline";
}


function ShowHide(itm, imgTag, iconFileExpand,iconFileCollapse)
{
	var retval;
	
	try
	{
		if (itm.style.display == 'none')
		{
			itm.style.display = 'inline';
			imgTag.src = wwwroot+'/images/web/'+iconFileCollapse;
			retval = true;
		}
		else
		{
			itm.style.display = 'none';
			imgTag.src = wwwroot+'/images/web/'+iconFileExpand;
			retval = false;
		}
	}
	catch(err)
	{
		alert(err.message);
	}
	
	return retval;
	
}

function IsNumeric(strString)
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
   return blnResult;
}

