var shoppingCart = new Array();

shoppingCart_init();

function shoppingCart_init()
{
    var cookieXml = getCookie("shoppingcart");
    if (cookieXml != null)
    {
        var xmlDoc;
        if (window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Msxml.DOMDocument");
            xmlDoc.loadXML(cookieXml);
        }
        else
        {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(cookieXml,"text/xml");
        }
        
        var doc = xmlDoc.documentElement;
       
        for (var i = 0; i < doc.childNodes.length; i++)
        {
            var child = doc.childNodes[i].childNodes;
            
            var cartItem = new Object();
            cartItem.ID = child[0].firstChild.nodeValue;
            cartItem.Name = child[1].firstChild.nodeValue;
            cartItem.Price = child[2].firstChild.nodeValue;
            cartItem.Count = child[3].firstChild.nodeValue;
            cartItem.VAT = child[4].firstChild.nodeValue;
            cartItem.AdditionalInfo = child[5].firstChild.nodeValue;
            
            shoppingCart[shoppingCart.length] = cartItem;
        }
    }
}

function shoppingCart_renderDropDown()
{       
    var element = getDropDown();
    
    element.options.length = 0;

    if (shoppingCart.length > 0)
    {
        for (var i = 0; i < shoppingCart.length; i++)
        {
            var cartItem = shoppingCart[i];
          
            element.options[i] = new Option(cartItem.Count + " st. " + cartItem.Name,cartItem.ID);
        }    
    }
    else
    {
        element.options[0] = new Option("Din varukorg \u00E4r tom","");
    }
}

function shoppingCart_addItem(id, name, price, count, vat, additionalInfo)
{
    var exists = false;
    var newCartItem = new Object();
    newCartItem.ID = id;
    newCartItem.Name = name;
    newCartItem.Price = price;
    newCartItem.Count = count;
    newCartItem.VAT = vat;
    newCartItem.AdditionalInfo = additionalInfo;
    
    for (var i = 0; i < shoppingCart.length; i++)
    {
        var cartItem = shoppingCart[i];
        if (cartItem.ID == newCartItem.ID)
        {
            exists = true;
            cartItem.Count = parseInt(cartItem.Count) + parseInt(newCartItem.Count);
        }
    }
    if (!exists)
        shoppingCart.unshift(newCartItem)
    //shoppingCart[shoppingCart.length] = cartItem;
    
    shoppingCart_save();
    
    shoppingCart_renderDropDown();
    window.scrollTo(0, 0);
    blinkCounter = 6;
    window.setTimeout("blink(true)", 200);
}

var blinkCounter;

function blink(bln)
{
   var element = getDropDown();
   if (bln)
      element.style.backgroundColor = "yellow";
   else
      element.style.backgroundColor = "white";
   if (--blinkCounter > 0)
      window.setTimeout("blink(" + !bln + ")", 500);
   else
      element.style.backgroundColor = "white";
}

function shoppingCart_updateItemById(id,count)
{
    for (var i = 0; i < shoppingCart.length; i++)
    {
        var cartItem = shoppingCart[i];
        if (cartItem.ID == id)
        {
            if (count > 0)
                cartItem.Count = count;
            else
                shoppingCart.splice(i,1);
        }
    }
    
    shoppingCart_save();
    
    shoppingCart_renderDropDown();
}

function shoppingCart_removeItem()
{
    var element = getDropDown();
    
    var index = element.options.selectedIndex;

    shoppingCart.splice(index,1);
    
    shoppingCart_save();
    
    shoppingCart_renderDropDown();
}

function shoppingCart_removeItemById(id)
{
    for (var i = 0; i < shoppingCart.length; i++)
    {
        var cartItem = shoppingCart[i];
        if (cartItem.ID == id)
        {
            shoppingCart.splice(i,1);
        }
    }
    
    shoppingCart_save();
    
    shoppingCart_renderDropDown();
}

function shoppingCart_clear()
{
    deleteCookie("shoppingcart","","");
    
    shoppingCart = new Array();
    
    shoppingCart_renderDropDown();
}

function shoppingCart_save()
{
    if (shoppingCart.length == 0)
    {
        deleteCookie("shoppingcart","","");
        return;
    }
        
    var xmlDoc;
    var isIE;
    
    if (window.ActiveXObject)
    {
        xmlDoc = new ActiveXObject("Msxml.DOMDocument");
        xmlDoc.loadXML("<shoppingCart/>");
        isIE = true;
    }
    else
    {
        xmlDoc = document.implementation.createDocument("", "shoppingCart", null);
        isIE = false;
    }
   
    var root = xmlDoc.documentElement;
    var item;
    var child;
    var text;
     
    for (var i = 0; i < shoppingCart.length; i++)
    {
        var cartItem = shoppingCart[i];
        item = xmlDoc.createElement("cartItem");
        
        child = xmlDoc.createElement("id");
        text = xmlDoc.createTextNode(cartItem.ID);
        child.appendChild(text);
        
        item.appendChild(child);
        
        child = xmlDoc.createElement("name");
        text = xmlDoc.createCDATASection(cartItem.Name);
        child.appendChild(text);
        
        item.appendChild(child);
        
        child = xmlDoc.createElement("price");
        text = xmlDoc.createTextNode(cartItem.Price);
        child.appendChild(text);
        
        item.appendChild(child);        
        
        child = xmlDoc.createElement("count");
        text = xmlDoc.createTextNode(cartItem.Count);
        child.appendChild(text);
        
        item.appendChild(child);
        
        child = xmlDoc.createElement("vat");
        text = xmlDoc.createTextNode(cartItem.VAT);
        child.appendChild(text);
        
        item.appendChild(child);
        
        
        child = xmlDoc.createElement("additionalinfo");
        text = xmlDoc.createCDATASection(cartItem.AdditionalInfo);
        child.appendChild(text);
                
        //child = xmlDoc.createElement("additionalinfo");
        //text = xmlDoc.createTextNode(cartItem.AdditionalInfo);
        //child.appendChild(text);
        
        item.appendChild(child);            
        
        root.appendChild(item);
    }

    if (isIE)
    {
        setCookie("shoppingcart", xmlDoc.xml, null);
    }
    else
    {
        var serializer = new XMLSerializer();
        var xml = serializer.serializeToString(xmlDoc);

        setCookie("shoppingcart", xml, null);
    }
}

function viewcookie()
{
    var value = getCookie("shoppingcart");
    
    alert(value);
}

function setCookie(cookieName,value,expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = cookieName + "=" + encodeURI(value) + ";path=/" + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(cookieName)
{
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(cookieName + "=");
        if (c_start != -1)
        {
            c_start = c_start + cookieName.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
                
            return decodeURI(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

function deleteCookie(cookieName,path,domain)
{
    if (getCookie(cookieName) != "")
    {
        document.cookie = cookieName + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

var httpOb;

function sendM(strEmail)
{
    if (CorrectEmailaddress(strEmail) == false) 
       return;

    if (window.XMLHttpRequest)
       httpOb = new XMLHttpRequest();
    else
       httpOb = new ActiveXObject("Microsoft.XMLHTTP");

    httpOb.open("GET", "/Util/sendM.aspx?email=" + strEmail + "&__=" + encodeURIComponent((new Date()).getTime()), true);
    //httpOb.open("POST", "http://www.anp.se/processSubscriptionForm.asp?__=" + encodeURIComponent((new Date()).getTime()), true);
    //Send the proper header information along with the request
    //httpOb.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //httpOb.setRequestHeader("Content-length", params.length);
    //httpOb.setRequestHeader("Connection", "close");
    httpOb.onreadystatechange = HandleStateChange;
    httpOb.send(null);
}


function HandleStateChange()
{
  if (httpOb.readyState == 4)
  {
    var Ans = httpOb.responseText;
    if (Ans.indexOf("att du uppdaterat din prenumeration. Du prenumererar just nu p") > 0)
        alert(alreadyReg);
    else
    {
        if (Ans.indexOf("har lagts till som prenumerant p") < 0)
           alert(unsucc);
        else
        {
           document.forms[0].Text2.value = "";
           alert(succ);
        }
    }
  }
}



function CorrectEmailaddress(emailAddress)
{ 
    if (emailAddress.length > 255) 
    { 
        alert(err1); 
        document.forms[0].Text2.focus();
        return false; 
    }
    if (emailAddress == "") 
    { 
        alert(err2);
        document.forms[0].Text2.focus();
        return false; 
    } 
    if (emailAddress.length < 7) 
    { 
        alert(err3); 
        document.forms[0].Text2.focus();
        return false;
    } 
    at=emailAddress.indexOf("@"); 
    lastat=emailAddress.lastIndexOf("@"); 
    dot=emailAddress.lastIndexOf("."); 
    if (at<1||at!=lastat||dot<at)
    { 
        alert(err4);
        return false;
    }
} 
