function $(id) {
	return document.getElementById(id);
}

var popupWindow;
var UserAgent = navigator.userAgent.toLowerCase();

function moveIFrame(x,y,w,h) 
{
	var frame = $("myFrame");
	frame.style.left = x+'px';
	frame.style.top = y+'px';
	frame = $("myIFrame");
	frame.width = w+'px';
	frame.height = h+'px';
}

function hideIFrame()
{
	$("myFrame").style.visibility="hidden";
}
	
function showIFrame()
{
	$("myFrame").style.visibility="visible";
}

function loadIFrame(url)
{
	$("myFrame").innerHTML = "<iframe id='myIFrame' src='" + url + "'frameborder='0'></iframe>";
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function sendToActionScript(value, id) {
	thisMovie("main").sendMsg(value, id);
}

function printLabel(labelDef, printerType, id) {
	document.printerApplet.print(labelDef, printerType);
}

function openReportWindow(url) {
	if (popupWindow != null)
	{
		popupWindow.location.replace(url);
		popupWindow.focus();
	}
}

function openPopup()
{
	try
	{
		popupWindow = window.open('');

		if (popupWindow == null)
			return false;

		window.focus();
	}
	catch (err)
	{
		return false;
	}

	return true;
}

function testPopup()
{
	try
	{
		var popup = window.open('');
		if ( popup == null )
			return false;

		popup.close();
	}
	catch(err)
	{
		return false;
	}
	return true;
}

var Cookie = {
	erase: function(name) {
		Cookie.create(name, "", -1);
	},
	create: function(name, value, days) {
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},
	read: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(";");
		for (var i=0; i<ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	}
};

var FreightMasterUserID = "";
var FreightMaster = {
	userID: '',
	flashVersionString: "9.0.0",
	flashVersion: 9,
	showVideo: function(vid) {
		vid = vid + ".flv";
		var lb = document.createElement("div");
		lb.id = "videoLightbox";
		document.body.appendChild(lb);
		var container = document.createElement("div");
		container.id = "videoHolder";
		document.body.appendChild(container);
		container.style.display = 'block';
		var div = document.createElement("div");
		div.id = "videoContainer";
		container.appendChild(div);
		swfobject.embedSWF("VideoHolder.swf", "videoContainer", "1014", "602", FreightMaster.flashVersionString, null, {movie: vid});
	},
	hideVideo: function() {
		setTimeout(function() { $("videoHolder").parentNode.removeChild($("videoHolder")); }, 50);
		setTimeout(function() { $("videoLightbox").parentNode.removeChild($("videoLightbox")); }, 80);
	},
	checkRequirements: function() {
		var loadFlash = true;
		if (!navigator.javaEnabled()) {
			$("Requirement_Java").style.display = "block";
			loadFlash = false;
		}
		
		var flashver = flashVersion();
		if ((flashver.major < FreightMaster.flashVersion) || (flashver == -1)) {
			$("Requirement_Flash").style.display = "block";
			if (flashver.major) $("Requirement_Flash").getElementsByTagName("span")[0].innerHTML = "<b>Adobe Flash Player "+FreightMaster.flashVersionString+"</b> (<i>Upgrade required, detected "+flashver.major+".0</i>)<br />Download the latest version here";
			loadFlash = false;
		}

		/*
		 * var acrobatver = acrobatVersion(); if (acrobatver == -1) { $("Requirement_Acrobat").style.display = "block";
		 * loadFlash = false; }
		 */

		if (loadFlash) {
			var flashvars = {};
			var params = {
				menu: "false",
				allowScriptAccess: "always",
				wmode: "opaque",
				scale: "noscale",
				bgcolor: "#869ca7"
			};
			var attributes = {
				id: "main",
				name: "main"
			};

			swfobject.embedSWF("main.swf", "main_div", "100%", "100%", FreightMaster.flashVersionString, null, flashvars, params, attributes);
			
			setTimeout(function() {
				$("main").focus();
			}, 1000);
			swfobject.addLoadEvent(loadEventHandler);
		}
	},
	onbeforeunload: 'You will be logged out and lose any unsaved information.'
};

function setManifestPopupID(user) {
	FreightMaster.userID = user;
}

function showManifestPopup() {
	var c = 'au.com.aae.fm2.showManifestAlert.'+FreightMaster.userID;
	var cook = Cookie.read(c);
	return cook==null?true:false;
}

function loadEventHandler() {
	BrowserHistory.flexApplication = swfobject.getObjectById("main");
	
}

function enableStatus() {
	var status = document.getElementById("printerStatus");
	status.value = "Active";
}

function resetStatus() {
	var status = document.getElementById("printerStatus");
	status.value = "Inactive";
}

function checkPrinter(){
	// var status = document.printerApplet.checkPrinter();
	var status = document.getElementById("printerStatus");
	thisMovie("main").printerCheckResult(status.value);	
}

/*
 * function acrobatVersion() { var version = -1; if (/msie/.test( UserAgent ) && !/opera/.test( UserAgent )) { var axo =
 * new ActiveXObject("AcroPDF.PDF"); if (axo) { version = 1; } } else { var np = navigator.plugins; for (var i=0; i<np.length;
 * i++) { if (np[i].name == "Adobe Acrobat") { version = 1; } } } return version; }
 */
function flashVersion() {
	var version = -1;
	if (/msie/.test( UserAgent ) && !/opera/.test( UserAgent )) {
		var maxver = 11;
		var axo;
		var found = false;
		var e;
		while (maxver > 0) {
			if (!found) {
				try {
					var sw = "ShockwaveFlash.ShockwaveFlash"+(maxver > 0?"."+maxver:"");
					axo = new ActiveXObject(sw);
					if (axo) {
						var ver = "WIN "+maxver+",0,0,0";
						try {
								axo.AllowScriptAccess = "always";
								ver = axo.GetVariable("$version");
						} catch (e) {}
						ver = ver.split(" ")[1].split(",");
						version = {
								major: ver[0],
								minor: ver[1],
								revision: ver[2]
						};
						found = true;
					}
				} catch (e) {}
			}

			maxver--;
		}
	} else {
		var pi = navigator.plugins;
		if (pi != null && pi.length > 0) {
			if (pi["Shockwave Flash 2.0"] || pi["Shockwave Flash"]) {
				var sw = "Shockwave Flash" + (pi["Shockwave Flash 2.0"] ? " 2.0" : "");
				var desc = pi[sw].description.split(" ");
				var tmp = desc[2].split(".");
				version = {
						major: tmp[0],
						minor: tmp[1],
						revision: desc[3]||desc[4]
				};
			}
		}
	}
	return version;
}

// Runtime
window.onload = function() {FreightMaster.checkRequirements();};

