﻿
function printCart()
{
	modCart("nothing", 0, 0, 0, null);
}

function clearCart()
{
	modCart("clearall", 0, 0, 0, null);
}

function modCart(todo, id, cnt, dsp, element)
{
	element = null;
	//checkUnpaied();
	toTimer = setTimeout("timeouted();", 6000);
	
	maskElement = element;
	if (maskElement != null)
	{
		maskElement.style.visibility = "visible";
	}
	
	if (todo == "add")
	{
		updatingCart = true;
	}
	else
		updatingCart = false;
		
    var url = "/sars.asp?do=" + todo + "&id=" + id + "&cnt=" + cnt + "&dsp=" + escape(dsp) + "&print=yes&r=" + Math.random();
    //document.getElementById("loader").style.visibility = "visible";
    
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processCartRequest;
    req.open("GET", url, true);
    req.send(null);
}

function processCartRequest()
{
    if (req.readyState == 4)
    {
        if (req.status == 200)
        {    	
        	XMLText = req.responseText;
        	
    		var xmlobject = (new DOMParser()).parseFromString(XMLText, "text/xml");
			
			var root = xmlobject.getElementsByTagName('cart')[0];
			var productsText = root.getElementsByTagName('products')[0].firstChild.nodeValue;
			//document.getElementById("cartProducts").innerHTML = unescape(productsText);
			prodCount = root.getElementsByTagName('totalCount')[0].firstChild.nodeValue;
			allowOrder = root.getElementsByTagName('allowOrder')[0].firstChild.nodeValue;
			
			if (prodCount == "0" || prodCount == "")
			{
				document.getElementById("cartAmount").innerHTML = '';
				document.getElementById("cartAmount2").innerHTML = '';
			}
			else
			{
				document.getElementById("cartAmount").innerHTML = root.getElementsByTagName('totalPrice')[0].firstChild.nodeValue;
				document.getElementById("cartAmount2").innerHTML = root.getElementsByTagName('totalPrice')[0].firstChild.nodeValue;
			}
			document.getElementById("cartCount").innerHTML = prodCount;
			document.getElementById("cartCount2").innerHTML = prodCount;
			
			
			if (productsText.indexOf("Your cart is empty") >= 1 || prodCount == 0)
			{
				cartEmpty = true;
			}
			else
			{
				cartEmpty = false;
			}
        	
        	if (root.getElementsByTagName('message') != null && root.getElementsByTagName('message')[0].firstChild != null)
        	{
				msg = trim(root.getElementsByTagName('message')[0].firstChild.nodeValue);
				if (msg != null && msg != "")
				{
					alert(msg);
				}
			}
        }
        
        if (isProductPage && !hppp)
		{
			printVari(); hppp = true;
		}
    }
}

var hppp = false;


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

if (typeof DOMParser == "undefined") {
   DOMParser = function () {}

   DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
      }
   }
}

function timeouted()
{
	if (maskElement != null)
	{
		maskElement.style.visibility = "hidden";
		//Cp kommer ju upp hela tiden
		//alert("Timed out =/\nTry again.");
	}
	timer = null;
}



var keyarr = new Array(5);
var isProductPage = false;

function handleKeyDown( e )
{

	if( e == null && event != null ) {
		e = event;
	}
	
	if( e == null ) {
		return true;
	}
	
	var keycode = (e.which == null) ? e.keyCode : e.which;
	
	
	//alert(keycode)
	switch( keycode )
	{
		case 13:
			keystr = "";
			for (i = 0; i < 6; i++)
			{
				keystr += keyarr[i] + "-";
			}
			//alert(keystr);
			if (keystr == "82-69-68-76-73-66-")
			{
				for (i = 0; i < 6; i++)
				{
					keyarr[i] = "";
				}
				enableImages();
				alert("Produktbilder aktiverade");
				if (isProductPage != null && isProductPage == true)
				{
					location.reload();
				}
			}
		break;
		case 27:
			//esc
		break;
		default:
			for (i = 4; i >= 0; i--)
			{
				keyarr[i+1] = keyarr[i];
			}
			keyarr[0] = keycode;
		break;
	
	}
	
	return true;
}


function enableImages()
{	
    var url = "ajx_enable_img.asp?" + Math.random();
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    req.onreadystatechange = function ()
	{
	    if (req.readyState == 4)
	    {
	        if (req.status == 200)
	        {
	        }
	    }
	};
    req.open("GET", url, true);
	req.send(null);
}


var orderType = 'standard';
function swOrderType()
{	
	if (orderType != 'mango')
	{
		orderType = 'mango';
		document.getElementById('imgOrderType').src = '/img/ot_wish.png';
	}
	else
	{
		orderType = 'standard';
		document.getElementById('imgOrderType').src = '/img/ot_standard.png';
	}
	
    var url = "/sars.asp?do=setOrderType&type=" + orderType + "&r=" + Math.random();
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    req.onreadystatechange = function ()
	{
	    if (req.readyState == 4)
	    {
	        if (req.status == 200)
	        {
				if (isProductPage)
				{
					document.getElementById("variContainer").innerHTML = '<div style="text-align:center;font-size:10px;padding:6px;color:#222;"><img src="/img/ajx_fff_000.gif"/></div>';
					printVari();
				}
	        }
	    }
	};
    req.open("GET", url, true);
	req.send(null);
}

function _ginit()
{
	printCart();
}


function blogPost(blogger, _view)
{
	var _req;
    var url = "/blogpost.php?b=" + blogger + "&v=" + _view;
    if (window.XMLHttpRequest)
    {
        _req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        _req = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    _req.onreadystatechange = function ()
	{
	    if (_req.readyState == 4)
	    {
	        if (_req.status == 200)
	        {
	        	if (_view == 1)
	        	{
	        		document.getElementById('blogpost'+blogger).innerHTML = '<div style="width:285px;height:148px;overflow:hidden;">' + _req.responseText + '</div>';
	        	}
	        	else
	        	{
					document.getElementById('blogpost'+blogger).innerHTML = _req.responseText;
				}
	        }
	    }
	};
    _req.open("GET", url, true);
    document.getElementById('blogpost'+blogger).innerHTML = '<br/><img src="/img/w_b_loader_1.gif" style="vertical-align:middle;"/>&nbsp;Laddar...';
	_req.send(null);
}


var oc = {};

function Tip(z)
{

}

function UnTip()
{

}

function menuAct(mid, act)
{
	if (oc[mid] == null)
		oc[mid] = 0;
	switch(act)
	{
		case 'out':
			oc[mid]++;
			setTimeout("menuHide('"+mid+"',"+oc[mid]+");", 100);
			break;
		case 'over':
			document.getElementById("hmenu"+mid).style.opacity = 1;
		default:
			oc[mid]++;
	}
}

function menuHide(mid, _oc)
{
	var o = document.getElementById("hmenu"+mid);
	var ot = document.getElementById("htitle"+mid);
	if (_oc == oc[mid])
	{
		//fom(mid);
		o.style.visibility = 'hidden';
		ot.style.background = 'transparent';
		ot.style.color = '#444';
	}
}

function fom(mid, _oc)
{
	var o = document.getElementById("hmenu"+mid);
	
	op = o.style.opacity * 100;
	op = parseInt(op)-25;
	o.style.opacity = (op / 100);
	o.setAttribute("myOp", op);
	if (op > 0)
		setTimeout("fom('" + mid + "');", 40);
	else
		o.style.visibility = 'hidden';
}

function tmtda(_o, _id, _act)
{
	if (_act == 'over')
	{
		_o.className = 'tmtdHover';
		_o.style.color='#fff';
		if (_id != null)
			document.getElementById('hmenu-x' + _id).style.visibility='visible';
	}
	else
	{
		_o.className = 'tmtd';
		_o.style.color='#fff';
		if (_id != null)
			document.getElementById('hmenu-x' + _id).style.visibility='hidden';
	}
}

function signupNewsletter()
{
	var _name = document.getElementById('nl_name').value;
	var _mail = document.getElementById('nl_mail').value;
	
	if (_name.length <= 2 || _mail.length <= 2 || _mail.indexOf("@") <= 0)
		return;
	
    var url = "/sys/newsletter_signup.adi?name=" + _name + "&mail=" + _mail;
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    req.onreadystatechange = function()
	{
	    if (req.readyState == 4)
	    {
	        if (req.status == 200)
	        {
	  			alert(req.responseText);
	  			document.getElementById('nl_container').style.display = 'none';
	        }
	        else
	        {
	  			alert(req.responseText);
	        }
	    }
	};	
	
    
	req.open('GET', url, true);
	req.send(null);
}

function changeCountry(_c)
{
	
}
