var loc, currenturl, first

if (window.parent.frames.length<3)
{
	currenturl = document.location;
	//	location.replace("/index.php?" + currenturl);   
	parent.location.replace("http://www.cyberphoto.se/?" + currenturl);

}

function Cookie(document, name, hours, path, domain, secure)
{
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

function _Cookie_store()
{
    var cookieval = "";
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }

    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}

function _Cookie_load()
{
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;
    
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    cookieval = unescape(cookieval);
    //alert (cookieval);
    var a = cookieval.split('&');
    for(var i=0; i < a.length; i++)
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    return true;
}

function _Cookie_remove()
{
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

// Create a dummy Cookie object, so we can use the prototype object to make
// the functions above into methods.
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

// Create a cookie object
var kundcookie = new Cookie(document, "kundvagn", 2, "/", ".cyberphoto.se");
// Load the values of the cookie
kundcookie.load();

// changeItem: Adds, changes, removes an article.
// Arguments: prod_id; the article id
//            count:   the number of articles. If this is set to 0 the article is removed.
//            add:     if this value is >0 the number in count is added to
//                     the already existing count of the same article (if articles
//                     of the same kind already exist in the shopping kind)
function changeItem(prod_id, count, add, freight)
{
    var kundvagn;
    kundvagn = kundcookie.grejor;

    var found = 0;
    var checkfreight = 0;
    var pattern = /^frakt/;
    if (pattern.test(prod_id))
    {
       checkfreight = 1;
    }


    if (kundvagn)
    {
      	// Create an array of the cookie
		var items = kundvagn.split("|");
      	kundvagn = "";

      	// Go through the list
		for (var i=0; i<items.length && !found; i+=2)
		{
			if (checkfreight && pattern.test(items[i])) {
				// Remove freight
				items[i+1] = 0;
				continue;
			}

			// The item that's changed is already in the list
			if (items[i]==prod_id)
			{
				// Are going to add the number in count?
				if (add>0)
				{
					var tmp = parseInt(items[i+1]);
					items[i+1]=tmp + parseInt(count);
				}
				else
					items[i+1] = count;

				found = 1;
			}
	  	}

      	// Put together the list again
      	for (i=0; i<items.length; i+=2)
     	{
     	   if (items[i+1]>0)
     	   				 // Art id    Count
     	     kundvagn += items[i]+"|"+items[i+1]+"|";
      	}
    }
    else
      kundvagn="";

    // The article wasn't already in the list so add it now.
    if (!found)
    {
       kundvagn += prod_id + "|" + count + "|";
    }

    // Remove the last separator at the end of the cookie string
    kundvagn = kundvagn.substring(0,kundvagn.length-1);

    // Set the cookie to the string we've created
    kundcookie.grejor = kundvagn;

    // Store the cookie in the client
    kundcookie.store();
}

// This is the function called by when the user click on the purchase button
function modifyItems(prod_id)
{
    var kundvagn;
    var first;
    kundvagn = kundcookie.grejor;

    var pattern = /\|/;
    
    if (!(pattern.test(kundvagn)))
    {
    first = 'yes';
    }

    // Add one to the shopping basket.
    changeItem(prod_id, 1, 1);
  
    if (first == 'yes')
    {
    parent.frames[0].location.reload();

    // Go to
    var hojd = 160;
    var bredd = 215;
    
    var loc = "/kundvagn/ruta2.htm";
    var y = (screen.height / 2) - (hojd * 2);
    var x = (screen.width / 2) - (bredd / 2);
        
        
    var features = "width=" + bredd + ",height=" + hojd + ",top=" + y + ",left=" + x;
    
    var w = window.open(loc, "smallshopwin", features);
    
    w.focus();
    }
    else
    {
    parent.frames[0].location.reload();
    }
}

function addItemsNoPop(prod_id)
{
    changeItem(prod_id, 1, 1, 1);  
    parent.frames[0].location.reload();    
}

function addFreight(prod_id, pay)
{

          changeItem(prod_id, 1, 1, 1);

    // Move on to add shipping info
   
    if (pay == 'kort')
    { location="kort_new.shtml"; }
    if (pay == 'faktura')
    { location="faktura_new.shtml"; }
    if (pay == 'pf')
    { location="pf_new.shtml"; }
    if (pay == 'np')
    { location="netpay.shtml"; }

}

function adjustItem(prod_id, n)
{
   count = prompt("Ange antal. Noll (0) tar bort varan ur kundvagnen:", n);
   if (count)
   {
     changeItem(prod_id, count, 0);
     document.location.reload();
   }
}
function levStatusPaket(prod_id, antal, fi, sv)
{
    var loc = "/kundvagn/levstatus.php?artnr=" + prod_id + "&antal=" + antal;
    if (fi == 1)
	loc += "&fi=true";
    if (sv == 1)
	loc += "&sv=true";


    var hojd, bredd, x, y;		   
    hojd = 300; bredd = 500;

    y = (screen.height / 2) - (hojd / 2);
    x = (screen.width / 2) - (bredd / 2);


    var features = "width=" + bredd + ",height=" + hojd + ",top=" + y + ",left=" + x;
    var w2 = window.open(loc, "smallshopwin", features);
    w2.focus();
}
function PaketDetaljer(prod_id, antal, fi, sv)
{
    var loc = "/kundvagn/paketdetaljer.php?artnr=" + prod_id + "&antal=" + antal;
    if (fi == 1)
	loc += "&fi=true";
    if (sv == 1)
	loc += "&sv=true";


    var hojd, bredd, x, y;		   
    hojd = 380; bredd = 500;

    y = (screen.height / 2) - (hojd / 2);
    x = (screen.width / 2) - (bredd / 2);


    var features = "width=" + bredd + ",height=" + hojd + ",top=" + y + ",left=" + x;
    var w2 = window.open(loc, "smallshopwin", features);
    w2.focus();
}
function modifyItemsInBasket(prod_id)
{

    // Go to
    var hojd = 60;
    var bredd = 155;
   
    var arg = escape(prod_id);

    var loc = "ruta_b.php?" + arg;
    var y = (screen.height / 2) - (hojd * 2);
    var x = (screen.width / 2) - (bredd / 2);
        
        
    var features = "width=" + bredd + ",height=" + hojd + ",top=" + y + ",left=" + x;
    
    var w = window.open(loc, "smallshopwin", features);
    
    w.focus();
  

}

var oTRSel;
function HiLite(oTR) {
if (oTRSel) {
oTRSel.style.backgroundColor='#F7F7F4';
}
oTR.style.backgroundColor='#E1E1E1';
oTRSel=oTR;
}
function ClearHiLite() {
if (oTRSel) {
oTRSel.style.backgroundColor='#F7F7F4';
}
}

function ShowPackageDetail(strInput) {

     if (strInput != 0) {
          document.getElementById('rules').style.visibility = 'visible'; // visar
     } else {
          document.getElementById('rules').style.visibility = 'hidden'; // visar inte
     }
}

