var af = "";
var longtime = 10*365*24*60*60*1000;
var laddtid = "";
var kakan = "";
var cokon = true;
var online = true;
var buffer = "";
var lnr = 0;
var isappl = 0;
var timeout = 0;
var mcordX = 500;
var mcordY = 500;
var mtid = 0;

function initbase() {
	if (xv0q()) {
		loadapp();
		if (! readkaka()) {setkaka();}
		checkqs();
	}
}

function loadapp() {
setappl();
/*	if (online) {
		laddtid = (new Date()).getTime();
		if (timeout > 2) {
			af = window.open("www.panograf.com/base/appl.htm", "af", "width=0 height=0");
		}
		else {
			af = window.open("../../base/appl.htm", "af", "width=0 height=0");		
		}
		setTimeout("checkloadappl()", 1000);
	}   */
}

function checkloadappl() {
	if (af.location.href.indexOf("rror") != -1) {
		if (timeout < 6) {
			loadapp();
			timeout ++;
		}
		else {timeout = 0;}
	}
}

function config() {
	focus();
	checkappl();
}

function checkappl() {
	timeout ++;
	if (af.document.applets.length == 0) {setTimeout("checkappl()",500);}
	else {
		var prova = af.document.ClientApplet.getActive();
		if (prova == 1) {
			var ttmp = (new Date()).getTime();
			laddtid = (parseInt(ttmp) - parseInt(laddtid));
			//speedqual();
			xvd("12:" + laddtid);
			setTimeout("setappl()", 1000);
		}
		else {
			setTimeout("checkappl()", 1000);
		}
	}
}

function setappl() {isappl = 1;xvd("");} // alert(kakan + " Hit " + af.document.ClientApplet.getActive());}

function xvd(logs) {
/*	if ((isappl == 1) && (kakan != "")) { 	
		if (supfunc.indexOf("cobrowse") != -1) {writeMyC(logs);}
		logs = "1:1£9:" + kakan + "£26:" + projid + "£4:" + sourceid + "£" +  logs + "£";
		af.document.ClientApplet.SetLogString(logs);
//		opener.document.logf.log.value += logs;
		if (buffer != "") {
			var pos = buffer.indexOf("€");
			var logd = buffer.slice(0,pos);
			buffer = buffer.slice(pos+1, buffer.length);
			if (logd != "") {
				xvd(logd);
			}
		}
	}
	else {
//		opener.document.logf.log.value += " Buffer : " + logs;
		buffer += logs + "€";
	}   */
}

function setLang (Lang) {
	if (suplang.indexOf(Lang) == -1) {
		var langtmp = suplang.indexOf(",");
		if (langtmp != -1) {
			setLang(suplang.slice(0, langtmp));
		}
	}
	else {
		if ((Lang == "uk") || (Lang == "us")) {language = "english"; xvd("2:12£6:" + active_scene);} 
		if (Lang == "sv") {language = "swedish"; xvd("2:13£6:" + active_scene);} 
		if (Lang == "de") {language = "german"; xvd("2:14£6:" + active_scene);} 
		if (Lang == "no") {language = "norwegian"; xvd("2:15£6:" + active_scene);} 
		if (Lang == "dk") {language = "danish"; xvd("2:16£6:" + active_scene);} 
	}
}

function speedqual() {
	if (laddtid >= 15000) {setqual("modem");}
	if (laddtid < 15000) {setqual("ISDN");}
	if (laddtid < 4000) {setqual("ADSL");}
	if (laddtid <= 1500) {setqual("ISDN");}
}

function setqual(qset) {
	if (! isNaN(parseInt(qset))) {
		if (qset == 0) {qual = "modem";}
		if (qset == 1) {qual = "ISDN";}
		if (qset == 2) {qual = "ADSL";}
		if (qset == 3) {qual = "bredband";}
		if (qset == 4) {qual = "qtvr";}
		if (qset == 5) {qual = "bigqtvr";}
		qset = qual;
	}
	if (supqual.indexOf(qset) == -1) {
		var qtmp = supqual.indexOf(",");
		if (qtmp != -1) {
			setqual(supqual.slice(0, qtmp));
		}
	}
	else {
		if (qual != qset) {
			qual = qset;
			quallog();
		}
	}
}

function quallog() {
	if (qual == "modem") {xvd("2:5£6:" + active_scene)}
	if (qual == "ISDN") {xvd("2:6£6:" + active_scene)}
	if (qual == "ADSL") {xvd("2:7£6:" + active_scene)}
	if (qual == "bredband") {xvd("2:8£6:" + active_scene)}
	if (qual == "qtvr") {xvd("2:9£6:" + active_scene)}
	if (qual == "bigqtvr") {xvd("2:10£6:" + active_scene)}	
}

function chqual(qer) {
	if ((qual == "modem") && (qer == 0)) {return "checked";}
	if ((qual == "ISDN") && (qer == 1)) {return "checked";}
	if ((qual == "ADSL") && (qer == 2)) {return "checked";}
	if ((qual == "bredband") && (qer == 3)) {return "checked";}	
	if ((qual == "qtvr") && (qer == 4)) {return "checked";}
	if ((qual == "bigqtvr") && (qer == 5)) {return "checked";}	
	return "";
}

// Kak kontroll
function readkaka() {
	var kaks = unescape(document.cookie);
	var pos = kaks.indexOf("panograf=");
	if (pos == -1) {
		return false;
	}
	else {
		pos = pos + 9;
		var end = 1 + kaks.indexOf(";", pos);
		if (end == 0) {
			kakan = kaks.slice(pos);
		}
		else {
			kakan = kaks.slice(pos,end);
		}
		return true;
	}
}

// Kak sätting
function setkaka() {
	var expire = (new Date()).getTime();

	if (kakan == "") {
		kakan = projid + "¤" + expire + "¤" + Math.round(Math.random() * 1000);
	}
	expire = new Date(expire + longtime);
	expire = "; expires=" + expire.toGMTString();
	document.cookie = "panograf=" + escape(kakan) + expire;
}

function checkqs() {

	var qs = location.search;
			
	if (qs != "") {
		qs = qs.slice(1);
		var pos = 0;
		for (var i = 0; i < qs.length; i++) {
			pos = qs.indexOf("&", i);
			if (pos == -1) {
				exeqs(qs.slice(i, qs.length));
				break;
				//i = qs.length;
			}
			else {
				exeqs(qs.slice(i, pos));
				i = pos;
			}
		}
	}
	else eval("loadscene(0);");
}

function exeqs(doqs) {
	var ex = doqs.split("=");
	if(ex[0] == "cu") {		
		if (isNaN(parseInt(ex[1]))) {
			for (var i=0; i < customer_array.length; i++) {
				if (customer_array[i].indexOf(ex[1]) != -1) {
					var custtmp = customer_array[i].split("¤");
					customerid = custtmp[0];
					i = customer_array.length;
				}
			} 
		}
		else {
			customerid = ex[1];					
		}
		loadcust(customerid);
	}
	if ((ex[0] == "co") && cokon) {
		if (readkaka()) {
			xvd("2:3");
		}
		kakan = ex[1];
		setkaka();
	}
	if(ex[0] == "la") {
		setlang(ex[1]);
	}
	if(ex[0] == "qu") {
		setqual(parseInt(ex[1]));
	}
	if (ex[0] == "ls") {
		var scen = parseInt(ex[1]);
		if (isNaN(scen)) {
			var koll = 0;
			for (var i = 0;i < pan_name.length;i ++) {
				koll = pan_name[i].indexOf(ex[1]);
				if (koll != -1) {
					scen = i;
					i = pan_name.length;
				}
				else {
					scen = 0;
				}			
			}
			loadscene(scen);
		}
		else {
			loadscene(scen);
		}
		xvd("2:4£6:" + active_scene);
	}
	if (ex[0] == "mt") {
		if (isNaN(parseInt(ex[1]))) {
			xvd("2:105£6:");
		}
		else {
			mtid = parseInt(ex[1]);
			xvd("2:105£6:" + mtid);
		}
	}
	if (ex[0] == "ui") {
		if (isNaN(parseInt(ex[1]))) {
			showblank();
		}
		else {
			showui(parseInt(ex[1]));
		}
	}
}

function checkgeo(nat) {
	if (supcontry == "all") {return true;}
	else {
		if ((supcontry.indexOf(nat) != -1) || (nat == "N")) {return true;}
		else {return false;}
	}
}

// Kommer
function xv0q() {
	xvd("2:1");
	var tid = (new Date()).getTime();
	var mesage = "Best User! ";
	var opfrom = ""; //opener.window.document.URL;
	var brower = navigator.appVersion;
	var browernr = parseInt(brower);
	var os = navigator.platform;
	var javaon = navigator.javaEnabled();
	var userlang;

	if ((brower.indexOf("MSIE")) == -1) {
		if (browernr < 4) {
			mesage += "To run this site you must have a browser version that supports iFrame. Wellcome back!";
			xvd("2:34");
		}
		else {
//			mesage += "This site is not fully Netscape comptible. \n But you are wellcome to try!";
			userlang = "N";			
		} 
	}
	else {
		if (browernr < 4) {
			mesage += "This site is not fully comptible with your browser version " + browernr + ". Please uppgrade.";
		}
		if (! javaon) {
			mesage += "You must activate java in your browser."
		}
		var hs = window.screen.height;
		var ws = window.screen.width;
		mcordX = Math.round(ws/2);
		mcordY = Math.round(hs/2);
		var cpu = navigator.cpuClass;
		cokon = navigator.cookieEnabled;
		online = navigator.onLine;
		userlang = navigator.userLanguage;
		setLang(userlang);
		var userpro = ""; //navigator.userProfile.getAttribute(vCard.Email);
		xvd("10:" + cpu + "¤" + userlang + "¤" + ws + "¤" + hs + "¤" + tid + "£11:" + brower + "¤" + cokon);
//		xvd("11=Lokal Tid:" + tid + " Hs:" + hs + " Ws:" + ws + " Open from:" + opfrom  + " brower:" + brower + " cpu:" + cpu + " os:" + os + " javaon:" + javaon + " cokon:" + cokon + " online:" + online + " userlang:" + userlang + " userpro:" + userpro);
	}
	if (mesage != "Best User! ") {
		alert(mesage);
	}
	if (checkgeo(userlang)){return true;}
	else {alert("Best visitor! \n This function is not suported for your country. \n Pleas contact your local reseller!");return false;}
}

// Lämnar
function xv0r() {
	xvd("2:2");
}

// Ny scen
function xv0l() {
	xvd("2:110£6:" + active_scene);
}

// Lämna scen
function xv0p(panid) {
	xvd("2:61£6:" + panid);
}

function shut(menuname) {
	if (document.all) {
	document.all[menuname].style.visibility = "hidden";}
	else {document[menuname].visibility = "hide";}
}

function popm(menu) {
	if (document.all) {document.all[menu].style.visibility= "visible";}
	else {document[menu].visibility = "show";}
}

function firstup(stri) {
	var tmp1 = stri.charAt(0);
	tmp1 = tmp1.toUpperCase();
	return tmp1 + stri.slice(1);
}