//Copyright 2002-2008 CurtainShack and Sandy Point Soft Furnishings.  All Rights Reserved.
//Prod ID Structure
//Changed Mar06
//p wwwww ddddd l i cc ffff qqqqqq f ss ff rrrrrr ff ffff ssss
//1 23456 78901 2 3 45 6789 012345 6 78 90 123456 78 9012 3456
//           10             20           30           40
//p wwwww ddddd l i cc fff qqqqqq f ss ff rrrrrr ff fff ssss
//1 23456 78901 2 3 45 678 901234 5 67 89 012345 67 890 1234
//1 10000 10000 1 0 0 131 034600 0 1
function descprodtype(curtain){
    switch (parseFloat(curtain.slice(0, 1))) {
        case 1:
            return "Curtain";
        case 2:
            return "Cushion";
        case 3:
            return "Curtain Pole";
        case 4:
            return "Pelmet";
        case 5:
            return "Tieback";
        case 6:
            return "Roman Blind";
        case 7:
            return "Accessory";
        case 8:
            return "Fabric by the Metre";
        case 9:
            return "Wallpaper";
    }
}

function descwidth(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 3:
        case 4:
        case 6:
        case 1:
            return numstr(parseFloat(curtain.slice(1, 6)) / 100, false);
        default:
            return '';
    }
}

function descdrop(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 8:
            var fnum = parseFloat(curtain.slice(6, 11)) / 100;
            return numstr(fnum * 100, false) + ' (' + numstr(fnum, false) + 'm)';
            
        default:
            return '';
        case 6:
        case 4:
            
        case 1:
            return numstr(parseFloat(curtain.slice(6, 11)) / 100, false);
    }
}

function descfeature(curtain){
    var myfeature = parseFloat(curtain.slice(36, 38));
    var myfeaturesize = parseFloat(curtain.slice(42, 46)) / 100;
    var prodtype = parseFloat(curtain.slice(0, 1));
    if (prodtype == 6) {
        switch (myfeature) {
            case 1:
                s = "Shaped Bottom ";
                break;
            case 2:
                s = "Bottom Eyelet Punched Bottom ";
                break;
            case 3:
                s = "Tab Top ";
                break;
            case 11:
                s = "Zig-Zag Bottom ";
                break;
            case 21:
                s = "Border (bottom only) ";
                break;
            case 22:
                s = "Border (sides and bottom) ";
                break;
            case 23:
                s = "Border (Sides Only) ";
                break;
            case 24:
                s = "Horizontal Band ";
                break;
            default:
                s = '';
                break;
        }
    }
    else {
        return "";
    }
    if (myfeaturesize > 0 && myfeature > 20) {
        s = s + numstr(myfeaturesize, false) + " cm ";
    }
    return s;
}

function descfeaturefabric(curtain){
    var myfeaturefabric = parseFloat(curtain.slice(38, 42));
    var myfeature = parseFloat(curtain.slice(36, 38));
    var prodtype = parseFloat(curtain.slice(0, 1));
    if (prodtype == 6 && myfeaturefabric >= 0 && myfeature > 0) {
        if (myfeaturefabric === 0) {
            return "same fabric as blind";
        }
        else {
            return fabname(myfeaturefabric);
        }
    }
    else {
        return "";
    }
}

function descfeaturesize(curtain){
    var myfeaturesize = parseFloat(curtain.slice(42, 46)) / 100;
    var prodtype = parseFloat(curtain.slice(0, 1));
    if (prodtype == 6) {
        return numstr(myfeaturesize, false);
    }
    else {
        return "";
    }
}

function desclining(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 6:
        case 1:
            switch (parseFloat(curtain.slice(11, 12))) {
                case 1:
                    return "Standard";
                case 2:
                    return "Blackout";
                case 3:
                    return "No Lining";
            }
            break;
        default:
            return '';
    }
}

function descfitting(curtain){
    var myfit = parseFloat(curtain.slice(28, 30));
    switch (myfit) {
        case 11:
            return "Face-fitting,Non-Recessed";
        case 12:
            return "Face-fitting, In Recess";
        case 13:
            return "Top-fitting, Non-Recessed";
        case 14:
            return "Top-fitting, In Recess";
        case 1:
            return "Pole";
        case 2:
            return "Curtain Shack Metal Curtain Pole";
        case 3:
            return "Curtain Shack Wooden Curtain Pole";
        case 4:
            return "Track";
        default:
            return '';
    }
}

function descreturn(curtain){
    var myret = parseFloat(curtain.slice(30, 36));
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 1:
            return 'Std Allowance';
        case 4:
            return numstr(parseFloat(myret) / 100, false);
        default:
            return '';
    }
}

function descinterlining(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 6:
        case 1:
            switch (parseFloat(curtain.slice(12, 13))) {
                case 0:
                    return "None";
                case 1:
                    return "Interlined";
            }
            break;
        default:
            return '';
    }
}

function descconfig(curtain){
    var config = parseFloat(curtain.slice(13, 15));
    switch (config) {
        case 1:
            return "Pair of curtains";
        case 2:
            return "Single Curtain";
        case 21:
            return "45cm square";
        case 22:
            return "50cm square";
        case 23:
            return "55cm square";
        case 24:
            return "60cm square";
        case 31:
            return "100cm";
        case 32:
            return "150cm";
        case 33:
            return "180cm";
        case 34:
            return "200cm";
        case 35:
            return "240cm";
        case 36:
            return "300cm";
        case 37:
            return "360cm";
        case 41:
            return "91cm";
        case 42:
            return "122cm";
        case 43:
            return "183cm";
        case 44:
            return "244cm";
        case 51:
            return "Left Drawpull";
        case 52:
            return "Right Drawpull";
        default:
            return '';
    }
}

function descfabric(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 1:
        case 4:
        case 5:
        case 6:
        case 8:
        case 2:
            return fabname(parseFloat(curtain.slice(15, 19)));
        default:
            return '';
    }
}

function descquote(curtain){
    return numstr(parseFloat(curtain.slice(19, 25)) / 100, true);
}

function descfamily(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    switch (prodtype) {
        case 6:
        case 1:
            if (vertrep(parseFloat(curtain.slice(15, 19))) > 0) {
                if (parseFloat(curtain.slice(25, 26)) == 1) {
                    return "(no other items in this fabric in same room)";
                }
                else {
                    return "(other curtains in this fabric in same room)";
                }
            }
            else {
                return "";
            }
            break;
        default:
            return '';
    }
}

function descstyle(curtain){
    var mystyle = parseFloat(curtain.slice(26, 28));
    //alert(curtain);
    switch (mystyle) {
        case 1:
            return "Pencil Pleat";
        case 2:
            return "Pinch Pleat";
        case 3:
            return "Tab Top";
        case 4:
            return "Eyelet 2cm";
        case 5:
            return "Eyelet 4cm";
        case 21:
            return "Oxford";
        case 22:
            return "Piped";
        case 31:
            return "Crescent";
        case 41:
            return "Black Metal (Basket Finials)";
        case 42:
            return "Black Metal (Scroll Finials)";
        case 43:
            return "Black Metal (Fleur de Lys Finials)";
        case 44:
            return "Black Metal (Cannonball Finials)";
        case 51:
            return "Wooden Natural Finish";
        case 52:
            return "Wooden Rosewood Finish";
        case 53:
            return "Wooden Teak Finish";
        case 54:
            return "Wooden Walnut Finish";
        case 55:
            return "Wooden White Finish";
        case 61:
            return "Regular";
        case 62:
            return "Cascade";
        case 71:
            return "Hard";
        case 72:
            return "Soft (Vallance)";
        case 81:
            return "Black Metal Curved Holdback style H1";
        case 82:
            return "Black Metal Curved Holdback style H3";
        case 83:
            return "Black Metal Curved Holdback style H5";
        case 84:
            return "Black Metal Curved Holdback style H6";
        case 85:
            return "Black Metal Straight Holdback style SH5";
        case 86:
            return "Black Metal Tieback Hook";
        case 0:
            return '';
        default:
            return '';
    }
}

function describe(curtain){
    var prodtype = parseFloat(curtain.slice(0, 1));
    var fabric = descfabric(curtain);
    var width = descwidth(curtain);
    var drop = descdrop(curtain);
    var quote = descquote(curtain);
    var family = descfamily(curtain);
    var lining = desclining(curtain);
    var interlining = descinterlining(curtain);
    var config = descconfig(curtain);
    var style = descstyle(curtain);
    var ret = descreturn(curtain);
    var fit = descfitting(curtain);
    var feat = descfeature(curtain);
    if (feat !== "") {
        feat = feat + ' in ' + descfeaturefabric(curtain);
    }
    s = '';
    pt = descprodtype(curtain);
    switch (prodtype) {
        case 8:
            s = pt + ' , ' + fabric + ' length ' + drop + ' cm. ';
            break;
        case 6:
            s = pt + ' in ' + fabric + ', width ' + width + ' cm by drop ' + drop + ' cm. ' + feat;
            break;
        case 1:
            s = pt + ' in ' + fabric + ', width ' + width + ' cm by drop ' + drop + ' cm.';
            break;
        case 2:
            s = pt + ' in ' + fabric + ', style ' + style + ', size ' + config + '.';
            break;
        case 3:
            s = pt + ' ' + style + ' size ' + config + '.';
            break;
        case 4:
            s = pt + ' ' + style + ' in ' + fabric + '.';
            break;
        case 5:
            s = pt + ' ' + style + ' in ' + fabric + '.';
            break;
        case 7:
            s = pt + ' ' + style + '.';
            break;
    }
    return s;
}

function Cookie(document){
    this.$document = document;
    this.$name = "curtainshack";
    var hours = 240;
    this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
    this.$path = null;
    this.$domain = null;
    this.$secure = false;
    this.numitems = 0;
    this.prods = "";
    this.M_subtotal = 0;
    this.M_total = 0;
    this.M_carriage = 0;
    this.M_specs = '';
    this.M_calcs = '';
    this.M_deliverycountry = 'GB';
    this.email = '';
    this.M_deliverypostcode = '';
    this.M_address1 = '';
    this.M_address2 = '';
    this.M_address3 = '';
    this.M_state = '';
    this.tel = '';
    this.M_deliveryname = '';
    this.M_message = '';
    this.M_town = '';
}

Cookie.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;
};
Cookie.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;
};
Cookie.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.$document.cookie = cookie;
};
Cookie.prototype.additem = function(q, prodtype){
    //alert('dbg:additem');
    if (q.M_quote <= 0) {
        alert('No total amount, item not added');
        return;
    }
    if (q.M_prodtype === 0) {
        if (q.checkinputs() === false) {
            return;
        }
        if (q.M_drop <= 0) {
            alert('Drop must be greater than zero, so item was not added');
            return;
        }
        if (q.M_width <= 0) {
            alert('Width must be greater than zero, so the item was not added');
            return;
        }
    }
    //Multiply widths by 100 and save as integers
    var quotepence = Math.round(q.M_quote * 100);
    var drop100 = Math.round(q.M_drop * 100);
    var width100 = Math.round(q.M_width * 100);
    var ret100 = Math.round(q.M_return * 100);
    var fs100 = Math.round(q.M_featuresize * 100);
    //
    //if (q.M_prodtype==8)
    //                alert('AddItems(a):['+padnum(quotepence,6)+']');
    //This is where the prodid token gets made up
    //Mar06, extended for more than 999 fabrics
    var prodid = q.M_prodtype.toString() + padnum(width100, 5) + padnum(drop100, 5) + q.M_lining.toString() + q.M_interlining.toString() + padnum(q.M_config, 2) + padnum(q.M_fabric, 4) + padnum(quotepence, 6) + q.M_family.toString() + padnum(q.M_style, 2) + padnum(q.M_fitting, 2) + padnum(ret100, 6) + padnum(q.M_feature, 2) + padnum(q.M_featurefabric, 4) + padnum(fs100, 4);
    //var prodid=q.M_prodtype.toString()+padnum(width100,5)+padnum(drop100,5)+q.M_lining.toString()+q.M_interlining.toString()+padnum(q.M_config,2)+padnum(q.M_fabric,3)+padnum(quotepence,6)+q.M_family.toString()+padnum(q.M_style,2)+padnum(q.M_fitting,2)+padnum(ret100,6)+padnum(q.M_feature,2)+padnum(q.M_featurefabric,3)+padnum(fs100,4);
    //if (q.M_prodtype==8)
    //{
    //        alert('AddItems(b):'+descquote(prodid));
    //        alert(prodid);
    //}
    this.numitems++;
    this.prods = this.prods + '_' + prodid;
    this.M_calcs = this.M_calcs + '_\n' + q.M_calc;
    //alert('dbg:additem storing ...');
    this.store();
    //alert('dbg:additem calcing ...');
    this.calccart();
    //alert('dbg:additem complete');
    return;
};
Quote.prototype.load = function(curtainno){
};
Quote.prototype.update = function(curtainno){
};
function getquote(curtain){
    return parseFloat(curtain.slice(19, 25)) / 100;
}

Cookie.prototype.numprods = function(prodtype){
    var num = 0;
    curtains = this.prods.split("_");
    for (i = 1; i <= this.numitems; i++) {
        ptype = parseFloat(curtains[i].slice(0, 1));
        if (ptype == prodtype) {
            num++;
        }
    }
    return num;
};
Cookie.prototype.edititem = function(itemno){
    this.store();
};
Cookie.prototype.pushdescart = function(form){
    //alert('dbg:pushdescart');
    if (this.numitems === 0) {
        form.descart.value = '';
    }
    else 
        if (this.numitems == 1) {
            form.descart.value = 'You have ' + this.numitems + ' item totalling \u00A3' + numstr(this.M_subtotal) + ' in your order';
        }
        else {
            form.descart.value = 'You have ' + this.numitems + ' items totalling \u00A3' + numstr(this.M_subtotal) + ' in your order';
        }
};
function Quote(prodtype){
    this.M_prodtype = parseFloat(prodtype);
    //alert('dbg: prodtype='+this.M_prodtype);
    switch (prodtype) {
        case 1:
            this.M_lining = 1;
            this.M_style = 1;
            this.M_config = 1;
            this.M_fitting = 1;
            break;
        case 2:
            this.M_lining = 0;
            this.M_style = 21;
            this.M_config = 21;
            this.M_fitting = 0;
            break;
        case 3:
            this.M_lining = 0;
            this.M_style = 41;
            this.M_config = 31;
            this.M_fitting = 0;
            break;
        case 4:
            this.M_lining = 0;
            this.M_style = 71;
            this.M_config = 0;
            this.M_fitting = 0;
            break;
        case 5:
            this.M_lining = 0;
            this.M_style = 31;
            this.M_config = 0;
            this.M_fitting = 0;
            break;
        case 6:
            this.M_lining = 1;
            this.M_style = 61;
            this.M_config = 51;
            this.M_fitting = 11;
            break;
        case 7:
            this.M_lining = 0;
            this.M_style = 81;
            this.M_config = 0;
            this.M_fitting = 0;
            break;
        case 8:
            this.M_lining = 0;
            this.M_style = 0;
            this.M_config = 0;
            this.M_fitting = 0;
            break;
        default:
            alert('illegal product type');
    }
    this.M_calc = '';
    //this.M_fabric=defaultfabric();                //could be set to -1 for 'select'
    this.M_fabric = 0;
    this.M_width = 0;
    this.M_drop = 0;
    this.M_interlining = 0;
    this.M_quote = 0;
    this.M_family = 0;
    this.M_patterndrop = 0;
    this.M_return = 0;
    this.M_vat = 0.0;
    this.M_fabricprice = 0;
    //this.M_featureprice=0;
    //this.M_featurefabricprice=0;
    this.M_featuresize = 5;
    this.M_labourprice = 0;
    this.M_liningprice = 0;
    this.M_interliningprice = 0;
    this.M_lininglength = 0;
    this.M_interlininglength = 0;
    this.M_fabriclength = 0;
    this.d = 0;
    this.M_labourwidths = 0;
    this.M_fabricwidths = 0;
    this.setupcharge = 15;
    this.vatrate = 0;
    this.M_feature = 0;
    this.M_featurefabric = 0;
}

Quote.prototype.checkdrop = function(drop){
    //alert(this.M_prodtype);
    var p = parseFloat(this.M_prodtype);
    if (p == 8) {
		if (drop > 60) {
			alert('Error: your length (' + numstr(drop, true) + ') is greater than the maximum of 60 metres (6,000 centimetres)');
			return false;
		}
		else 
			if (drop < 1 && drop !== 0) {
				alert('Error: your length (' + numstr(drop, true) + ') is less than the minimum of 1 metre (100cm)');
				return false;
			}
		return true;
	}
	else 
		if (drop > 450) {
			alert('Error: your drop (' + numstr(drop, true) + ') is greater than the maximum of 450 cm');
			return false;
		}
		else 
			//todo: a diagnostics, comments box at the bottom of the get a price page
            if (drop < 25 && drop !== 0) { //was 40 prior to nov08
                alert('Error: your drop (' + numstr(drop, true) + ') is less than the minimum blind drop of 25 cm');
                return false;
            }
            else {
                return true;
            }
};
Quote.prototype.checkwidth = function(width){
    if (this.M_prodtype == 6 && width > 280) {
        alert("Can't have a blind wider than 260cm");
        return false;
    }
    else 
        if (width > 500) {
            alert('Error: your width (' + numstr(width, true) + ') is greater than the maximum of 500 cm');
            return false;
        }
        else 
            if (width < 27 && width !== 0) { //was 34 prior to nov08
                alert('Error: your width (' + numstr(width, true) + ') is less than the minimum width of 27 cm');
                return false;
            }
            else {
                return true;
            }
};
Quote.prototype.checkstylefeature = function(style, feature){
    if (this.M_prodtype == 6) {
        if (style == 62 && (feature == 1 || feature == 2 || feature == 11)) {
            alert('You cannot have a cascade blind with a shaped, eyelet or zigzag bottom, check your inputs');
            return false;
        }
    }
    return true;
};
Quote.prototype.checkinputs = function(){
    if (this.checkdrop(this.M_drop) === false || this.checkwidth(this.M_width) === false) {
        return false;
    }
    return true;
};
Quote.prototype.popfab = function(fab){
    //popfab(this.M_fabric);
    popfab(parseInt(fab, 10));
};
function checkhighlight(swatchrangeobj, swatchobj){
    if (swatchobj.value <= 0) {
        swatchobj.style.color = '#0000FF';
    }
    else {
        swatchobj.style.color = 'black';
    }
    if (swatchrangeobj.value <= 0) {
        swatchrangeobj.style.color = '#0000FF';
    }
    else {
        swatchrangeobj.style.color = 'black';
    }
}

Quote.prototype.changeconfig = function(form){
    switch (this.M_style) {
        case 41:
        case 42:
        case 43:
        case 44:
            form.M_config.options.length = 0;
            form.M_config.options.length = 4;
            form.M_config.options[0] = new Option("100cm", "31");
            form.M_config.options[1] = new Option("150cm", "32");
            form.M_config.options[2] = new Option("200cm", "34");
            form.M_config.options[3] = new Option("300cm", "36");
            switch (this.M_config) {
                case 33:
                    this.M_config = 34;
                    break;
                case 35:
                    this.M_config = 34;
                    break;
                case 37:
                    this.M_config = 36;
                    break;
            }
            break;
        case 51:
        case 52:
        case 53:
        case 54:
        case 55:
            form.M_config.options.length = 0;
            form.M_config.options.length = 4;
            form.M_config.options[0] = new Option("180cm", "33");
            form.M_config.options[1] = new Option("240cm", "35");
            form.M_config.options[2] = new Option("300cm", "36");
            form.M_config.options[3] = new Option("360cm", "37");
            switch (this.M_config) {
                case 31:
                    this.M_config = 33;
                    break;
                case 32:
                    this.M_config = 33;
                    break;
                case 34:
                    this.M_config = 35;
                    break;
            }
            break;
    }
    form.M_config.value = this.M_config;
};
function updatefabricthumb(form, fabno){
    //alert('updatefabricthumb');
    if (fabno > 0) {
        form.fabricthumb.src = 'http://www.romansdirect.co.uk/fabrics/fab' + fabno.toString() + 't.jpg';
        form.fabricthumb.title = 'click for a larger picture of ' + fabname(fabno);
        reveal(form.fabricthumb);
    }
    else {
        hide(form.fabricthumb);
    }
}

function updatefeaturefabricthumb(form, fabno){
    //alert('updatefeaturefabricthumb');
    if (fabno > 0) {
        form.featurefabricthumb.src = 'http://www.romansdirect.co.uk/fabrics/fab' + fabno.toString() + 't.jpg';
        form.featurefabricthumb.title = 'click for a larger picture of ' + fabname(fabno);
        reveal(form.featurefabricthumb, 1);
    }
    else {
        hide(form.featurefabricthumb, 1);
    }
}

function updatefabricfields(form, fabno){
    //alert('updatefabricfields');
    checkhighlight(form.fabricrange, form.M_fabric);
    if (fabno >= 0) {
        form.fabricrange.value = fabricrangelookup(fabno);
    }
    fabricpopulate(parseInt(form.fabricrange.value, 10), form.M_fabric, 1);
}

function updatefeaturefabricfields(form, fabno){
    //alert('dbg:beg updatefeaturefabricfields feature fabric='+form.M_featurefabric.value);
    checkhighlight(form.featurefabricrange, form.M_featurefabric);
    if (fabno >= 0) {
        form.featurefabricrange.value = fabricrangelookup(fabno);
    }
    //alert('dbg:just before updatefeaturefabricfields feature fabric='+form.M_featurefabric.value);
    //NEED TO KNOW WHEN RANGE HAS CHANGED AND WE NEED TO REST FAB BACK TO -1.
    //WE WILL NEED A COOKIE VAR TO HOLD THE CURRENT VALUE OF THE RANGE, ONE FOR FAB AND ONE FOR FEATURE FAB.
    featurefabricpopulate(parseInt(form.featurefabricrange.value, 10), form.M_featurefabric, 1);
    //alert('dbg:end updatefeaturefabricfields feature fabric='+form.M_featurefabric.value);
}

Cookie.prototype.updateorder = function(form, mode){
    if (mode == 1) {
        form.M_orderid.value = this.prods;
        form.M_numitems.value = this.numitems;
        form.M_subtotal.value = this.M_subtotal;
        form.M_carriage.value = this.M_carriage;
        form.amount.value = this.M_total;
        //alert("updateorder: setting specs on form to "+this.M_specs+" and message to "+this.M_message);
        form.M_specs.value = this.M_specs;
        form.M_deliverycountry.value = this.M_deliverycountry;
        form.M_address1.value = this.M_address1;
        form.M_address2.value = this.M_address2;
        form.M_address3.value = this.M_address3;
        form.M_state.value = this.M_state;
        form.tel.value = this.tel;
        form.M_deliveryname.value = this.M_deliveryname;
        form.M_deliverypostcode.value = this.M_deliverypostcode;
        form.email.value = this.email;
        form.M_message.value = this.M_message;
        form.M_town.value = this.M_town;
    }
};

//===========================
Cookie.prototype.calccart = function(){
    //this has to be in cart common
    //alert('dbg:additem calccart');
    this.M_subtotal = 0.0;
    curtains = this.prods.split("_");
    for (i = 1; i <= this.numitems; i++) {
        this.M_subtotal = this.M_subtotal + getquote(curtains[i]);
    }
    this.M_subtotal = sround(this.M_subtotal, 0.01);
    if (this.numitems > 0) {
        this.M_carriage = this.carriage(this.M_deliverycountry, this.numitems);
    }
    else {
        this.M_carriage = 0;
    }
    this.M_total = this.M_subtotal + this.M_carriage;
    this.M_total = sround(this.M_total, 0.01);
    this.store();
    //alert('dbg:additem calccart complete');
};
Cookie.prototype.setspecs = function(specs){
    this.M_specs = specs;
    this.store();
};
Cookie.prototype.updatecookie = function(form){
    this.M_deliverycountry = form.M_deliverycountry.value;
    this.M_address1 = form.M_address1.value;
    this.M_address2 = form.M_address2.value;
    this.M_address3 = form.M_address3.value;
    this.M_state = form.M_state.value;
    this.tel = form.tel.value;
    this.M_deliveryname = form.M_deliveryname.value;
    this.M_deliverypostcode = form.M_deliverypostcode.value;
    this.email = form.email.value;
   // alert("updatecookie: setting specs to:"+form.M_specs.value+" and message to "+form.M_message.value);
    this.M_specs=form.M_specs.value;
    this.M_message = form.M_message.value;
    this.M_town = form.M_town.value;
    this.store();
};
Cookie.prototype.carriage = function(deliverycountry, numitems){
    heavies = this.numprods(1) + this.numprods(6) + this.numprods(4);
    separates = this.numprods(7) + this.numprods(3);
    lights = this.numprods(2) + this.numprods(5) / 2;
    frees = this.numprods(8);
    //basic rate increased 8->9 feb08
    c = 0;
    switch (deliverycountry) {
        case 'GB':
            factor = 1;
            break;
        case 'CA':
        case 'US':
            heavies = heavies + frees / 2;
            frees = 0;
            factor = 2.5;
            break;
        case 'BE':
        case 'FR':
        case 'LU':
        case 'NL':
        case 'DE':
            factor = 1.55;
            break;
        case 'AT':
        case 'DK':
        case 'CH':
            factor = 1.75;
            break;
        default:
            factor = 2;
    }
    if (heavies > 0) {
        if (heavies == 1) {
            c = 9;
        }
        else 
            if (heavies == 2) {
                c = 12;
            }
            else 
                if (heavies == 3) {
                    c = 15;
                }
                else 
                    if (heavies == 4) {
                        c = 18;
                    }
                    else 
                        if (heavies <= 5) {
                            c = 20;
                        }
                        else {
                            c = 20 + (heavies - 5) * 2;
                        }
    }
    
    if (separates > 0) {
        c = c + 7 + (separates - 1) * 1.00;
    }
    if (lights > 0) {
        if (heavies > 0) {
            c = c + lights * 1;
        }
        else {
            c = c + 5 + (lights - 1) * 1;
        }
    }
    c = c * factor;
    return c;
};

