var ajax_url = "/index.asp";
var big=false;
var callback_code="";
var callback_target="";
var current_menu;
var default_height;
var person_template="<td width='33%' style='padding-top:15px' valign='top'><div><img src='/img/personnel/{PHOTO}'/></div><div><b>{NAME}</b><br/>{TITLE}{OPTIONAL}</div></td>";
var person_email_template = "<br/><a href='mailto:{EMAIL}'>{EMAIL}</a>";
var person_phone_template = "<br/>{PHONE}";
var person_mobile_template = "<br/>{MOBILE}";
var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
function download(file) {
    window.open('/images/doc/' + file, '');
}
function format_value(value) {
	return escape(value).replace(/%/gi,'{*}');
}
function frames() {
	return parseInt(document.getElementById("frames").value);
}
function get_info(target,id,code) {
	callback_code=code;
	callback_target=target;
	Sarissa.updateContentFromURI(ajax_url+"?CMD="+id,document.getElementById(target),null,get_info_callback,true);
}
function get_info_callback(node,target) {
	show_info(callback_target);
	if(callback_code.length) eval(callback_code);
}
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=="CSS1Compat") return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=="CSS1Compat") return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}
function goto_url(url) {
	document.location.href=url;
}
function hide_info() {
	document.getElementById("big_content_info").style.display="none";
	var node=document.getElementById("small_content_info");
	if(node) node.style.display="none";;
	document.getElementById("content_menu").style.display="";
	node=document.getElementById("column_1");
	if(node) node.style.display="";
	node=document.getElementById("column_2");
	if(node) node.style.display="";
	node=document.getElementById("column_3");
	if(node) node.style.display="";
}
function hide_login() {
	document.getElementById("login").style.display="none";
	document.getElementById("login_content").style.display="none";
}
function hl(elem,state) {
    elem.style.color = (state ? '#ffffff' : '#000000');
    elem.style.backgroundColor = (state ? '#acabab' : '#ffffff');
}
function hit_of_week() {
    get_info("small_content_info", "hitofweek", "");
}
function home(ssl) {
    document.location.href = "http" + (ssl ? "s" : "") + "://www.copiax.se" + ajax_url;
}
function init_page() {
	var body=document.body;
	if(default_height==undefined) {
		big=body.scrollHeight>body.offsetHeight;
		default_height=(big?body.scrollHeight-12:body.offsetHeight);
	} else
	    default_height = (body.offsetHeight > default_height || !big ? body.offsetHeight : default_height);
	var link = document.getElementById("my_shop_tv");
	if (link) link.style.display = !has_shop_tv ? "none" : "";
	link = document.getElementById("my_shop");
	if (link) link.style.display = !has_catalog ? "none" : "";
	link = document.getElementById("my_campaigns");
	if (link) link.style.display = !has_agreement ? "none" : "";
	link = document.getElementById("new_pricefile");
	if (link) link.style.display = secure_pricefile && !has_agreement ? "none" : "";
	link = document.getElementById("pricefile");
	if (link) link.style.display = secure_pricefile && !has_agreement ? "none" : "";
	if (current_menu != 4) resize();
    if (logged_on()) update_news();
}
function load_cities(id) {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",ajax_url+"?CMD="+id+"&PART=cities",true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4) {
	        var elem = document.getElementById(id + "_city");
	        while (elem.options.length) elem.remove(0);
	        var o = document.createElement("OPTION");
	        elem.options.add(o);
	        o.innerHTML = "-- Ingen stad vald --";
	        var xmlDoc = Sarissa.getDomDocument();
	        xmlDoc = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
	        xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
	        xmlDoc.setProperty("SelectionLanguage", "XPath");
	        var list = xmlDoc.selectNodes("/cities/city");
	        var last = "";
	        for (var i = 0; i < list.length; i++) {
	            var name = list[i].getAttribute("name");
	            if (name != last) {
	                o = document.createElement("OPTION");
	                elem.options.add(o);
	                o.innerHTML = name
	            }
	            last = name;
	        }
	        update_footer();
	    } 
	};
	xmlhttp.send("");
}
function logged_on() {
	return document.getElementById("logged_on").value=="1";
}
function login_post_action(status) {
    if (status != "ok") {
        var lb = document.getElementById("login_button");
        var le = document.getElementById("login_error");
        var lem = document.getElementById("login_error_msg");
        var lp = document.getElementById("login_progress");
        var p = document.getElementById("password");
        var rm = document.getElementById("remember_me");
        var u = document.getElementById("username");
        lem.innerHTML = "Felaktigt kundnummer eller lösenord!";
        le.style.display = "";
        u.disabled = false;
        p.disabled = false;
        lb.style.display = "";
        lp.style.display = "none";
        rm.disabled = false;
    } else
        home(true);
}
function logout() {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",ajax_url+"?CMD=logout",false);
	xmlhttp.send("");
	home(false);
}
function menu(id) {
    hide_info();
    if (id == 1 && logged_on()) {
        reset_search();
        id = 11;
    }
	document.getElementById("content_left").style.display=(id==11?"none":"");
	document.getElementById("content_right").style.display=(id==11?"none":"");
	current_menu=id;
	for(var i=1;i<=frames();i++)
		document.getElementById("frame"+i).style.display=(i==id?"":"none");
	resize();
	switch(id) {
		case 4: show_personnel(); break;
		case 5: show_suppliers(); break;
		//case 6: load_cities("copiax"); break;
		case 7: load_cities("kidde"); break;
		case 8: load_cities("secureline"); break;
		case 10: search_info("search_text2", "frame10",true); break;
		default: update_footer(); break;
	}
}
function goto_my_shop() {
    goto_url(has_shop ? '/shop/mysettings.asp' : '/shop/mystatistics.asp');
}
function openwin(url) {
	window.open(url);
}
function page(title,url) {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4) {
	        var node = document.getElementById("column_1");
	        if (node) node.style.display = "none";
	        node = document.getElementById("column_2");
	        if (node) node.style.display = "none";
	        node = document.getElementById("column_3");
	        if (node) node.style.display = "none";
	        var html = "<div class='info'>";
	        html += "<div class='info_header'><div class='info_header_content'>" + title + "</div></div>";
	        html += "<div class='info_content'>" + xmlhttp.responseText + "</div>";
	        html += "</div>";
	        var info = document.getElementById("small_content_info");
	        info.innerHTML = html;
	        info.style.width = "638px";
	        info.style.display = "";
	    }
	}
	xmlhttp.send("");
}
function reset_search() {
    document.getElementById("search_data").style.display = "";
    document.getElementById("search_status").style.display = "none";
}
function resize() {
    document.body.height = default_height + "px";
	document.getElementById("content").style.height=default_height+"px";
	var l=document.getElementById("login");
	if(!l.style.display.length) {
		l.style.left=(968-470+(document.body.offsetWidth-968)/2)+"px";
		document.getElementById("login_content").style.left=l.style.left;
	}
}
function search_info(data_elem,target,clearchk) {
    var elem = document.getElementById("search_data");
    if (elem) elem.style.display = "none";
    elem = document.getElementById("search_status");
    if (elem) elem.style.display = "";
    elem = document.getElementById(data_elem);
    var data = (elem ? format_value(elem.value) : "");
    var chk1 = document.getElementById("doctype_1");
    var chk5 = document.getElementById("doctype_5");
    var chk6 = document.getElementById("doctype_6");
    var chk7 = document.getElementById("doctype_7");
    get_info(target, "search:info&DATA=" + data + "&CHK1=" + ((chk1 && chk1.checked) || clearchk ? "1" : "0") + "&CHK5=" + ((chk5 && chk5.checked) || clearchk ? "1" : "0") + "&CHK6=" + (!chk6 || chk6.checked || clearchk ? "1" : "0") + "&CHK7=" + ((chk7 && chk7.checked) || clearchk ? "1" : "0") + "&TARGET=" + target, "");
}
function send_opinion() {
    var info = document.getElementById("opinion");
    if (!info.value.length) {
        alert("Vänligen fyll i Dina synpunkter i textrutan och försök igen.");
        return;
    }
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", ajax_url + "?CMD=opinion", true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("CharSet", "UTF-8");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.responseText == "<ok/>") {
                document.getElementById("opinion_header").style.display = "none";
                document.getElementById("opinion_data").style.display = "none";
                document.getElementById("opinion_status").style.display = "";
            } else
                alert("Tyvärr uppstod ett fel vid skickadet!\r\nVänligen försök igen eller kontakta Copiax.");
        }
    };
    xmlhttp.send("INFO=" + format_value(info.value));
}
function show_info(target) {
	switch(target) {
	    case "big_content_info": document.getElementById("content_menu").style.display = "none"; break;
	    case "small_content_info": 
	        {
			var node=document.getElementById("column_1");
			if(node) node.style.display="none";
			node=document.getElementById("column_2");
			if(node) node.style.display="none";
			node=document.getElementById("column_3");
			if(node) node.style.display="none";
		} break;
	}
	document.getElementById(target).style.display="";
}
function show_less_news() {
    document.getElementById("more_news_link").style.display = "";
    document.getElementById("more_news").style.display = "none";
    document.getElementById("less_news_link").style.display = "none";
}
function show_login() {
	var u=document.getElementById("username");
	u.value="";
	var p=document.getElementById("password");
	p.value="";
	var l=document.getElementById("login");
	l.style.left=(968-470+(document.body.offsetWidth-968)/2)+"px";
	var lc=document.getElementById("login_content");
	lc.style.left=l.style.left;
	l.style.display="";
	lc.style.display="";
	document.getElementById("login_error").style.display="none";
	u.focus();
}
function show_more_news() {
    document.getElementById("more_news_link").style.display = "none";
    document.getElementById("more_news").style.display = "";
    document.getElementById("less_news_link").style.display = "";
}
function show_news(id) {
    get_info("small_content_info", "news&ID=" + id, "");
}
function show_personnel() {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",ajax_url+"?CMD=personnel",true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4) {
	        var html = "";
	        var xmlDoc = Sarissa.getDomDocument();
	        xmlDoc = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
	        xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
	        xmlDoc.setProperty("SelectionLanguage", "XPath");
	        var deps = xmlDoc.selectNodes("/personnel/department");
	        for (var dep = 0; dep < deps.length; dep++) {
	            if (dep) html += "<div style='width:100%;text-align:right'><input style='margin-left:10px;background:url(/img/btn_bg.png) repeat-x;border:1px solid #cccccc;font-family:Verdana;font-size:8pt;color:#000000;font-weight:bold;width:80px;height:21px' type='button' onclick='document.location.href=\"#\"' value='Upp ^'/></div><hr class='info_divider'/>";
	            html += "<div>";
	            html += "<div class='info_content_header'>" + Sarissa.getText(deps[dep].selectSingleNode("name"), false) + "</div>";
	            var info = Sarissa.getText(deps[dep].selectSingleNode("info"), false);
	            html += (info.length ? "<div>" + info + "</div>" : "");
	            html += "<table class='info_content' style='width:600px'><tr>";
	            var pers = deps[dep].selectNodes("person");
	            for (var per = 0; per < pers.length; per++) {
	                var p = person_template;
	                p = p.replace(/{NAME}/g, Sarissa.getText(pers[per].selectSingleNode("name"), false));
	                p = p.replace(/{PHOTO}/g, Sarissa.getText(pers[per].selectSingleNode("photo"), false));
	                p = p.replace(/{TITLE}/g, Sarissa.getText(pers[per].selectSingleNode("title"), false).replace(/\\n/g, "<br/>"));
	                var opt = "";
	                var data = Sarissa.getText(pers[per].selectSingleNode("email"), false);
	                if (data.length) {
	                    opt += person_email_template;
	                    opt = opt.replace(/{EMAIL}/g, data);
	                }
	                data = Sarissa.getText(pers[per].selectSingleNode("phone"), false);
	                if (data.length) {
	                    opt += person_phone_template;
	                    opt = opt.replace(/{PHONE}/g, data);
	                }
	                data = Sarissa.getText(pers[per].selectSingleNode("mobile"), false);
	                if (data.length) {
	                    opt += person_mobile_template;
	                    opt = opt.replace(/{MOBILE}/g, data);
	                }
	                p = p.replace(/{OPTIONAL}/g, opt);
	                html += p;
	                if (per % 3 == 2) html += "</tr><tr>";
	            }
	            if (per % 3) html += "<td colspan='" + (3 - per % 3) + "'></td>";
	            html += "</tr></table>";
	        }
	        html += "<div style='width:100%;text-align:right'><input style='margin-left:10px;background:url(/img/btn_bg.png) repeat-x;border:1px solid #cccccc;font-family:Verdana;font-size:8pt;color:#000000;font-weight:bold;width:80px;height:21px' type='button' onclick='document.location.href=\"#\"' value='Upp ^'/></div>";
	        document.getElementById("personnel").innerHTML = html;
	        update_footer();
	    }
	};
	xmlhttp.send("");
}
function show_suppliers() {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",ajax_url+"?CMD=suppliers",true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4) {
	        var html = "";
	        var xmlDoc = Sarissa.getDomDocument();
	        xmlDoc = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
	        xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
	        xmlDoc.setProperty("SelectionLanguage", "XPath");
	        var sups = xmlDoc.selectNodes("/suppliers/supplier");
	        for (var sup = 0; sup < sups.length; sup++) {
	            html += "<div class='supplier_link'>&gt;&#160;<a href='javascript:openwin(\"" + Sarissa.getText(sups[sup].selectSingleNode("url"), false) + "\");'>" + Sarissa.getText(sups[sup].selectSingleNode("name"), false) + "</a></div>";
	            html += "<br/>";
	        }
	        document.getElementById("suppliers").innerHTML = html;
	        update_footer();
	    } 
	};
	xmlhttp.send("");
}
function update_city(id) {
	var list=document.getElementById(id+"_city");
	var city=escape(list.options[list.selectedIndex].text).replace(/%/gi,"{*}");
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",ajax_url+"?CMD="+id+"&PART=retailers&DATA="+city,true);
	xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4){ 
		var xmlDoc=Sarissa.getDomDocument();
		xmlDoc=(new DOMParser()).parseFromString(xmlhttp.responseText,"text/xml");
		xmlDoc.setProperty("SelectionNamespaces","xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
		xmlDoc.setProperty("SelectionLanguage","XPath");
		var html="<table>";
		var nodes=xmlDoc.selectNodes("//retailer");
		var value;
		for(i=0;i<nodes.length;i++) {
			var node=nodes[i];
			html+="<tr><td><table cellpadding='3' class='retailer'><tr><td>";
			html+=((value=node.getAttribute("box"))!=null)?"<b>"+value+"</b><br/>":"";
			html+="<b>"+node.getAttribute("company")+"</b><br/>";
			html+=((value=node.getAttribute("street"))!=null)?value+"<br/>":"";
			html+=((value=node.getAttribute("zip"))!=null)?value+" ":"";
			html+=((value=node.getAttribute("city"))!=null)?value:"";
			html+="<br/>";
			html+=((value=node.getAttribute("phone"))!=null)?"Tel: "+value+"<br/>":"";
			html+=((value=node.getAttribute("fax"))!=null)?"Fax: "+value+"<br/>":"";
			html+=((value=node.getAttribute("email"))!=null)?"E-mail: <a href='mailto:"+value+"'>"+value+"</a><br/>":"";
			html+=((value=node.getAttribute("url"))!=null)?"Hemsida: <a href='"+value+"' target='_blank'>"+value+"</a>":"";
			html+="</td></tr></table></td></tr>";
		}
		html+="</table>";
		list=document.getElementById(id+"_retailers");
		list.innerHTML=html;
	} };
	xmlhttp.send("");
}
function update_footer() {
    var body = document.body;
    var h = (body.scrollHeight > default_height ? body.scrollHeight : default_height);
    body.height = h + "px";
    document.getElementById("content").style.height = (h + (body.scrollHeight > default_height ? 12 : 0)) + "px";
}
function update_news() {
    Sarissa.updateContentFromURI(ajax_url + "?CMD=news_list", document.getElementById("news"), null, null, false);
    update_footer();
}