var CONNECTION_TYPE = (location.href.substring(0,5).toLowerCase() == "https") ? "secure" : "nonsecure";
if (CONNECTION_TYPE == "secure"){
	try {
		SERVER_PORT = SERVER_PORT_SECURE;
	} catch (e){}
}

var ua = window.navigator.userAgent.toLowerCase();
var _ie = false;
var _gecko = false;
if (ua.indexOf('msie') != -1) _ie = true;
else if (ua.indexOf('gecko') != -1) _gecko = true;
//alert((navigator.appVersion));
var _browserOK = true;

var is_major = parseInt(navigator.appVersion);
var is_ie3    = (_ie && (is_major < 4));
var is_ie4    = (_ie && (is_major == 4) && (ua.indexOf("msie 4")!=-1) );
var is_ie5    = (_ie && (is_major == 4) && (ua.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (_ie && (is_major == 4) && (ua.indexOf("msie 5.5") !=-1));
var is_ie6    = (_ie && (is_major == 4) && (ua.indexOf("msie 6.")!=-1) );
var is_ie6up  = (_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
var _interval

if (!is_ie6up) {
	document.write("<div id='divWarning' class='txtGeneral' align='center'>WARNING: You must install Internet Explorer 6.0 to run MV</div>");
	_interval = setInterval("warningBlink()", 3000);
}

var is_dome = (screen.height > screen.width && screen.colorDepth == 8);

// Checking if connection is Intranet or Internet
function isConnectionLocal(CLIENT_IP, SERVER_IP){
	return true;
	// testing if href is via IP address or domain name
	try{
		// PATCH: testing if IP address is an Algotec internal one (aka "10.2.xx.xx")
		var clientIP = CLIENT_IP.split(".");
		if (clientIP[0] == "10" && clientIP[1] == "2"){
			return true;
		}

		// PATCH: testing if IP address is an Algotec internal one (aka "199.203.80.xx")
		var clientIP = CLIENT_IP.split(".");
		if (clientIP[0] == "199" && clientIP[1] == "203" && clientIP[2] == "80"){
			return true;
		}

		// testing if server and client in the same subnet
		var maskIP = SERVER_SUBNET_MASK.split(".");
		var serverIP = SERVER_IP.split(".");
		var tmpA, tmpB;

		for (var i=0 ; i < maskIP.length ; i++){
			tmpA = parseInt(clientIP[i]) & parseInt(maskIP[i]);
			tmpB = parseInt(serverIP[i]) & parseInt(maskIP[i]);
			if (tmpA != tmpB)
				return false;
		}
		return true;
	}
	catch (e){}
	return true;
}

function getServerName(){
	var tmpUrl = location.href;
	tmpUrl = tmpUrl.substring(tmpUrl.indexOf("/")+2);
	tmpUrl = tmpUrl.substring(0,tmpUrl.indexOf("/"));
	return tmpUrl;
}

SERVER_NAME = getServerName();

function warningBlink(){
	clearInterval(_interval);
	if (divWarning.style.visibility == "hidden") {
		divWarning.style.visibility = "visible";
		_interval = setInterval("warningBlink()", 3000);
	} else {
		divWarning.style.visibility = "hidden";
		_interval = setInterval("warningBlink()", 500);
	}
}

// the following line will make sure the the absolute will stay in place after resizing
window.onresize = function () {
	//location.href = location.href
	refreshAbsolutesLinks()
}
function refreshAbsolutesLinks(){
	for (var i = 0 ; i < divArr.length ; i++ )
	{
		divLoc.removeChild(divArr[i]);
		divLoc.appendChild(divArr[i]);
	}
}

// Checks for a custom locale

	if (location.search.length > 1){
		var arr = location.search.substring(1,location.search.length).split("&");
		for (var i = 0 ; i < arr.length ; i++){
			var tmp = arr[0].split("=");
			switch (tmp[0]){
				case "locale":
					if (tmp.length > 1){
						SERVER_LOCALE = tmp[1];
					}
					break;
			}
		}
	}
	
// read languages by locale:
	if (_ie == true) {
		var xmlDoc = getXMLByLocale(SERVER_LOCALE);
		var xmlENG = getXMLByLocale("en");
	}

function getXMLByLocale(sLocale){
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = "false";
	xmlDoc.load("/i18n.xml");
	xmlDoc = xmlDoc.documentElement.firstChild;
	while (xmlDoc != null){
		if (xmlDoc.attributes[0].name == "lang" && xmlDoc.attributes[0].value == sLocale)
			break;
		xmlDoc = xmlDoc.nextSibling;
	}
	return xmlDoc;
}

// get localized string
function getLocaleString(strName){
	if (_ie == true){
		var tmp = xmlDoc.firstChild;
		while (tmp != null){
			if (tmp.attributes[0].name == "name"	&& tmp.attributes[0].value == strName){
				return unescape(tmp.firstChild.xml);
			}
			tmp = tmp.nextSibling;
		}
		tmp = xmlENG.firstChild;
		while (tmp != null){
			if (tmp.attributes[0].name == "name"	&& tmp.attributes[0].value == strName){
				return unescape(tmp.firstChild.xml);
			}
			tmp = tmp.nextSibling;
		}
	} else {
		// Till we have an XML implementation for Netscape/Mozilla
		if (strName == "TXT_RECEIVE"){
			return "Receive";
		}
		if (strName == "TXT_HOME"){
			return "Home";
		}
	}

	return "";
}


// set absolut link (the link url, xPos, yPos, width, height, show (for testing - true/false))
var divCounter = 0;
var divArr = new Array();
function setAbsoluteLink(urlID, xPos, yPos, w, h, show){
	var obj = document.createElement("div");
	divArr[divCounter] = obj;
	obj.id = "aDiv" + divCounter++;
	obj.style.position = "relative";
	
	obj.style.left = xPos;
	obj.style.top = yPos;
	obj.style.width = w;
	obj.style.height = h;
	obj.onclick = function () { getLink(urlID) }
	if (_ie != true) divLoc = document.getElementById("divLoc");
	divLoc.onmouseover = function () { this.style.cursor = 'hand' }
	if (show) {
		obj.style.backgroundColor = "#FF0000";
		obj.innerHTML = "<font size=\"-1\">lnk</font>";
	}
	//document.body.appendChild(obj);
	divLoc.appendChild(obj)
}

function loadFirstImage(obj, type, isOnLoad) {
	var name = eval(obj.id);
	var ext = eval(obj.id + "_EXT");
	if (_ie == true && is_dome == false){
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + IMAGES_DIR + name + type + ext + "', sizingMethod='scale')"
	} else {
		// Handling Netsacpe/Mozilla
		var newSrc = IMAGES_DIR + name + type + ext;
		if (isOnLoad == true){
			if (obj.src.indexOf("common_images/x.gif") != -1){
				obj.src = newSrc;
			}
		} else {
			if (obj.src.indexOf(newSrc) == -1){
				obj.src = newSrc;
			}
		}
	}
}

// load/change images
function loadImage(obj, type){
	loadFirstImage(obj, type, false) 
}


function getLink(obj){
	getLinkParams(obj,true)
}


function getLinkParams(obj, add_params, new_window){
	//alert(obj)
	var tmp = eval(obj + "_LNK").split("|");
	switch (tmp[0]){
		case "lnk":
			var new_location =  tmp[1];
			if (add_params){
				new_location += location.search;
			}
			if (new_window){
				window.open(new_location,'','location=yes, top=0, left=0, height=' + screen.availHeight + ', width=' + screen.availWidth);
			}
			else {
				location.href = new_location;
			}
			break;
		case "exe":
			runApp(tmp[1]);
			break;
	}
}

function setLink(obj){
	setLinkParams(obj, true);
}

function setLinkParams(obj, add_params, new_window){
	obj.href = "javascript: getLinkParams('"+obj.id+"',"+add_params+","+new_window+")";
}


var Application = null;
function isInstalled(){
	var ret_val = true;
	try{
		var sMpiVersion = (typeof(MPI_VERSION) != "undefined") ? ("." + MPI_VERSION) : ".1";
		Application = new ActiveXObject("MPI.MPI_Runner" + sMpiVersion);
		if (!Application.Initialize(SERVER_NAME, SERVER_PORT, CONNECTION_TYPE, "80", SERVER_INSTALLED_NAME, MV_VERSION, SERVER_HTTPS_PORT, 0))
			ret_val = false;
	} catch(e) {
		//alert("catched error:" + e.description)
		Application = null;
		ret_val = false;
	}
	return ret_val;
}

function runApp(app){
	if (app != "mp" && isInstalled()){
		Application.RunExe(app);
	} else {
		location.href = "masterview/mv.jsp";
	}
}

// as sound :)
function doNothing(){}

function setDiclaimer(){
	div_Disclaimer = document.getElementById("divDisclaimer");
	div_Disclaimer.style.width = "480";
	div_Disclaimer.style.position = "static";
	div_Disclaimer.style.overflow = "visible";
	div_Disclaimer.style.display = "inline";
	//divDisclaimer.style.marginLeft = "10px";
	div_Disclaimer.innerHTML = getLocaleString("HTML_DISCLAIMER");

	setLinkParams(LNK_DISCLAIMER, true, true);
	setLinkParams(LNK_PRIVACY_NOTICE, true, true);
}
