//Copyright 2002-2004 Sandy Point Soft Furnishings.  All Rights Reserved.
//Curtain Shack
function SLCookie(document){
    this.$document = document;
    this.$name = "shackshortlist";
    this.$expiration = new Date((new Date()).getTime() + 24 * 60 * 3600000);
    this.$path = null;
    this.$domain = null;
    this.$secure = false;
    this.numitems = 0;
    this.fabs = "";
}

SLCookie.prototype.store = function(){
    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;
};
SLCookie.prototype.load = function(){
    var i;
    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);
    var a = cookieval.split('&');
    for (i = 0; i < a.length; i++) {
        a[i] = a[i].split(':');
    }
    for (i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }
    return true;
};
SLCookie.prototype.remove = function(){
    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.numitems = 0;
    this.$document.cookie = cookie;
    window.open(this.$document.URL, "shackshortlist");
};
SLCookie.prototype.addfab = function(fabno){
    var fabrics, fn, s, alreadyadded;
    fabrics = this.fabs.split("_");
    alreadyadded = false;
    for (i = this.numitems; i >= 1; i--) {
        if (parseInt(fabrics[i], 10) == parseInt(fabno, 10)) {
            alreadyadded = true;
        }
    }
    if (alreadyadded === false) {
        this.numitems++;
        this.fabs = this.fabs + '_' + fabno.toString();
        this.store();
        this.poplist(fabno);
    }
    else {
        this.poplist(-fabno);
    }
};
SLCookie.prototype.transferfab = function(destboxname, sourceboxname){
    destboxname.value = sourceboxname.value;
};
SLCookie.prototype.writeselectbox = function(swatchno){
    var snum;
    var s = '';
    snum = parseInt(swatchno, 10);
    switch (snum) {
        //special cases - fabrics
        case -2:
            s = '<SELECT VALUE="0" class=discrete onchange="M_fabric.value=this.value;qt.setcushfabric(this.value,itemdata);" name=shortbox>';
            break;
        case -4:
            s = '<SELECT VALUE="0" class=discrete onchange="M_fabric.value=this.value;qt.setpelmfabric(this.value,itemdata);" name=shortbox>';
            break;
        case -5:
            s = '<SELECT VALUE="0" class=discrete onchange="M_fabric.value=this.value;qt.settiefabric(this.value,itemdata);" name=shortbox>';
            break;
        case -6:
            s = '<SELECT VALUE="0" class=discrete onchange="M_fabric.value=this.value;qt.setromfabric(this.value,itemdata);" name=shortbox>';
            break;
        //special cases - fabric ranges that require populating fabrics
        case -1:
        case 0:
            s = '<SELECT VALUE="0" class=discrete onchange="fabricrange.value=fabricrangelookup(this.value);fabricpopulate(fabricrange.value,M_fabric);M_fabric.value=this.value;qt.setfabric(this.value,itemdata);" name=shortbox>';
            break;
        case -60:
            s = '<SELECT VALUE="0" class=discrete onchange="fabricrange.value=fabricrangelookup(this.value);fabricpopulate(fabricrange.value,M_fabric);M_fabric.value=this.value;qt.setromfabric(this.value,itemdata);" name=shortbox>';
            break;
        case -61:
            s = '<SELECT VALUE="0" class=discrete onchange="featurefabricrange.value=fabricrangelookup(this.value);fabricpopulate(featurefabricrange.value,M_featurefabric);M_featurefabric.value=this.value;qt.setromfeaturefabric(this.value,itemdata);" name=featureshortbox>';
            break;
        //usual swatch page
        default:
            s = '<SELECT VALUE="0" class=discrete onchange="swatchrange' + swatchno + '.value=fabricrangelookup(this.value);fabricpopulate(swatchrange' + swatchno + '.value,swatch' + swatchno + ');swatch' + swatchno + '.value=this.value;shackswatch.updatecookieform(swatchform)"  name=shortbox>';
            break;
    }
    s = s + '<OPTION VALUE="0">Your Short List</OPTION>';
    fabrics = this.fabs.split("_");
    for (i = 1; i <= this.numitems; i++) {
        s = s + '<OPTION VALUE=' + fabrics[i] + '>' + fabname(parseInt(fabrics[i], 10)) + '</OPTION>';
    }
    s = s + '</SELECT>';
    document.write(s);
};

SLCookie.prototype.deletefab = function(fabno){
    var i;
    fabrics = this.fabs.split("_");
    j = 0;
    for (i = 1; i <= this.numitems; i++) {
        if (fabrics[i] == fabno) {
            break;
        }
    }
    fabrics.splice(i, 1);
    this.fabs = fabrics.join('_');
    this.numitems--;
    this.store();
    window.open(this.$document.URL, "shackshortlist");
};
SLCookie.prototype.write = function(){
    var fabrics, fn, s;
    fabrics = this.fabs.split("_");
    document.write('<FORM><TABLE>');
    s = '';
    for (i = this.numitems; i >= 1; i--) {
        if ((this.numitems - i) % 5 === 0) {
            s = s + '<TR>';
        }
        fn = fabrics[i].toString();
        s = s + '<TD><A HREF="Javascript:popfab(' + fn + ');"><IMG width=100px Name=S' + fn + '  src=http://www.romansdirect.co.uk/fabrics/fab' + fn + 's.jpg><BR>' + fabname(parseInt(fabrics[i], 10)) + '</A>';
        s = s + '<BR><A HREF="Javascript:SL.deletefab(' + fn + ')"><span class=smallprint>Delete</span></A>';
    }
    document.write(s);
    document.write('</TABLE></FORM');
    document.write('</BODY></HTML>');
    document.close();
};
SLCookie.prototype.poplist = function(fabadded){
    var fabrics, fn, fan;
    var feat = 'toolbar=no,resizable=yes,menubar=no,width=300,height=500,status=no,left=0,screenX=0,top=0,screenY=0,location=no,scrollbars=yes,directories=no';
    var poplist = null;
    poplist = window.open('', 'poplist', feat);
    poplist.document.write('<HTML>');
    poplist.document.write('<HEAD>');
    poplist.document.write('<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
    poplist.document.write('<LINK REL="STYLESHEET" TYPE="text/css" HREF="softfurn.css">');
    poplist.document.write('<TITLE>Fabric short list</TITLE>');
    poplist.document.write('<BODY style="margin: 0em;padding: 2em;border: none;" onload=window.focus()>');
    fabrics = this.fabs.split("_");
    poplist.document.write('<P align=center><BUTTON  class=mini OnClick="javascript:window.close()";>Close</BUTTON></P>');
    fan = parseInt(fabadded, 10);
    if (isNaN(fan) === false) {
        if (fan < 0) {
            poplist.document.write('<DIV ALIGN=CENTER><SPAN class=smallprint>' + fabname(-fan) + ' was already in your shortlist</SPAN></DIV>');
        }
        else {
            poplist.document.write('<DIV ALIGN=CENTER><SPAN class=smallprint>' + fabname(fan) + ' has been added to your shortlist</SPAN></DIV>');
        }
    }
    poplist.document.write('<FORM><TABLE>');
    for (i = 1; i <= this.numitems; i++) {
        if ((i - 1) % 3 === 0) {
            poplist.document.write('<TR>');
        }
        fn = fabrics[i].toString();
        poplist.document.write('<TD align=center><IMG height=50 px Name=S' + fn + ' style="border:none;" src=http://www.romansdirect.co.uk/fabrics/fab' + fn + 's.jpg><BR><SPAN class=smallprint>' + fabname(parseInt(fabrics[i], 10)) + '</SPAN>');
        //poplist.document.write('<TD><IMG height=50 px Name=S'+fn+'  src=http://www.romansdirect.co.uk/fabrics/fab'+fn+'s.jpg><TD>'+fabname(parseInt(fabrics[i])));
    }
    poplist.document.write('</TABLE></FORM>');
    poplist.document.write('<DIV style="align:center;" align=center><BUTTON  class=mini OnClick="javascript:window.close()";>Close</BUTTON></DIV>');
    poplist.document.write('</BODY></HTML>');
    poplist.document.close();
};

SLCookie.prototype.diagnostic = function(form){
    //if (parseInt(this.numitems)>4)
    //	{
    //		form.submit();
    //	}
};
