function expand() {
	document.checkout.delivery_location.style.pixelWidth = 250;
}

function collapse() {
	document.checkout.delivery_location.style.pixelWidth = 150;
}


function isInteger(s) {
return (s.toString().search(/^-?[0-9]+$/) == 0);
}
function loadProducts(item, index , rowindex) {
    if (item == null) { return }
    var year =  $("year_" + rowindex + "_id").value;
    if ((($("frames_" + rowindex + "_id").value == 'Y') && (item[2] == 1)) ||  (($("frames_" + rowindex + "_id").value == 'N') && (item[2] == 0))) {
        if (year < 1980) {
            if (item[0] == "3" || item[0] == "1") {
                var opt = document.createElement("OPTION");
                opt.text = item[1];
                opt.value = item[0];
                opt.fcode = item[4];
                $("image_size_" + rowindex + "_id").options.add(opt);
            }
        } else {
            var opt = document.createElement("OPTION");
            opt.text = item[1];
            opt.value = item[0];
            opt.fcode = item[4];
            $("image_size_" + rowindex + "_id").options.add(opt);
        }
    }
}
function frameSelected(event, rowindex) {
    place = $("image_size_" + rowindex + "_id").selectedIndex;
    if ($("frames_" + rowindex + "_id").value == '') {
        $("image_size_" + rowindex + "_id").value = '';
    } else {
        $("image_size_" + rowindex + "_id").options.length = 0;
        var opt = document.createElement("OPTION");
        opt.text = '';
        opt.value = '';
        $("image_size_" + rowindex + "_id").options.add(opt);
    }
    aoptions.each(function (item, index) {loadProducts(item, index, rowindex )});
    $("price_" + rowindex + "_id").innerHTML = '';
    
    //attempt to select the same size as was selected before
    $("image_size_" + rowindex + "_id").selectedIndex = place;
    if ($("image_size_" + rowindex + "_id").value != '') {
        sizeSelected(null, rowindex);
    }
}
function framePreSelected(event, rowindex) {
    var obj = $("image_size_" + rowindex + "_id");
    var p = obj.selectedIndex;
       
    $("image_size_" + rowindex + "_id").options.length = 0;
    var opt = document.createElement("OPTION");
    opt.text = '';
    opt.value = '';
    $("image_size_" + rowindex + "_id").options.add(opt);
    aoptions.each(function (item, index) {loadProducts(item, index, rowindex )});
     $("price_" + rowindex + "_id").innerHTML = '';
    $("image_size_" + rowindex + "_id").selectedIndex = p;
}
function displayPrice(event, rowindex) {
    var val = $("image_size_" + rowindex + "_id").value;
    var framed = $("frames_" + rowindex + "_id").value;
    var qty = $("quantity_" + rowindex + "_id").value;
    if (!isInteger(qty)) {
        qty = 0;
    }
     $("price_" + rowindex + "_id").innerHTML = '';

    aoptions.each(function(item, index) {
        if ( item != null ) {
            if ( item[0] == val) {
                if (((framed == 'Y') && (item[2] == 1)) 
                    || ((framed == 'N') && (item[2] == 0))) {
                    full = qty * item[3];
                    if (full != 0 ) {
                        $("price_" + rowindex + "_id").innerHTML = '&euro;' + full.toFixed(2);
                    }
                }
            }
        }
    });
}
function getPrice(id) {
    var val = '';
     val = $(id).innerHTML;
    
    if (val == '') {
        return 0;
    }
    return (parseFloat(val.replace('€', '')));
}
function updateTotals() {
    // NB: VAT is not applicable to delivery charges
    // and discount only applies to product
    var total = 0;
   
    for (i = 1; i <= numrows; i++) {
        total = total + getPrice('price_' + i + '_id');
    }
    if ($("delivery_costs").checked) {
        total = total + getPrice('deliveryprice');      
    }
    $('totalprice').value = total.toFixed(2);
    applyDiscount();
    total = $('totalprice').value * 1;
     $('totalprice').innerHTML = '&euro;' + total.toFixed(2);

}

function updateDeliveryCosts(event) {
    //event.stop;
        
    var country = $("delivery_location_id").value;
    var url = "http://www.irishtimes.com/pagesales/dp.cfm?"
    var rowcount = numrows;
    var tq = 0;

    var totalDelivery = 0;
            
    for (i = 1; i <= numrows; i++) {
        var imgobj = $("image_size_" + i + "_id");
        var framed = $("frames_" + i + "_id").value == 'Y';
        var p = imgobj.selectedIndex;
        if (p == 0) {
            rowcount = rowcount - 1;
            continue;
        }
        
        <!--- get quantity --->
        var qty = $("quantity_" + i + "_id").value;
        if (!isInteger(qty)) {
            qty = 0;
        }
        if (qty == 0) {
            rowcount = rowcount - 1;
            continue;
        }             

        var fcode = null;
        fcode = imgobj.options[p].fcode;
        if (fcode == null) {
            //determine fcode by lookup table.
            aoptions.each(function(item, index) { 
                if (item != null) {
                    if ((imgobj.value == item[0]) &&  (framed == item[2])) {
                        fcode = item[4];
                    }
                }
            });
        }
        
        var ref = $("ref_" + i + "_id").value;
        var chunk = "c" + i + "=" +  fcode + "x" + qty + "x" + ref + "&";
        url = url + chunk;
        tq = tq + qty;                        
               
      
    } <!--- end of loop --->

    url = url + "c=" + country + "&n=" + rowcount;  
       
    new Ajax(url, {
        method: 'get',
        onComplete: function(request) {
            var r = parseFloat(request.trim());            
            
            if ($("delivery_costs").checked) {           	 
           	<!--- display delivery --->
           	$('deliveryprice').innerHTML = '&euro;' + r.toFixed(2) ;
                updateTotals();
            } else {
                $('deliveryprice').innerHTML = '&euro; 0.00';
            }
	}
     }).request(); 
    

} <!--- end of updateDeliveryCosts --->


function applyDiscount(togg) {
    var voucher = $("discount_enter").value;
    if (voucher.trim() == '') {
        total = $('totalprice').value;
        if (togg) {
            updateTotals();
            total = $('totalprice').value * 1;
             $('totalprice').innerHTML = '&euro;' + total.toFixed(2);
        }
        return;
    }
    var url = "http://www.irishtimes.com/pagesales/cv.cfm?v=" + voucher;
    new Ajax(url, {
        method: 'get',
        onSuccess: function(responseText, responseXML, togg) {
            if (responseText.trim() == '') {
                if (togg) {
                    updateTotals();
                    total = $('totalprice').value * 1;
                    $('totalprice').innerHTML = '&euro;' + total.toFixed(2);
                }
            } else {
                var amount = responseXML.getElementsByTagName("am")[0].firstChild.nodeValue;
                var amountpercentage = responseXML.getElementsByTagName("amp")[0].firstChild.nodeValue;
                var minorder = responseXML.getElementsByTagName("mo")[0].firstChild.nodeValue;
                var total = $('totalprice').value;
                if (total > minorder) {
                    // discount shouldn't apply to delivery cost
                    // subtract delivery from total and add it back 
                    // after adjusting 'total' with discount
                    delivery = getPrice('deliveryprice');
                      
                 
		    if ($("delivery_costs").checked) {
			total = total - delivery;
		    }
                 
                    if (amount > 0) {
                        total = total - amount;
                    } else {
                        m = ( 100 - amountpercentage) / 100;
                        total = total * m;                 
                    }
                    
                    if ($("delivery_costs").checked) {
                    	total = total + delivery;
                    }                    
                    
                    $('totalprice').value = total;
                    $('totalprice').innerHTML = '&euro;' + total.toFixed(2);
                }
            }
        }
        }).request();
}
function sizeSelected(event, rowindex) {
    displayPrice(event, rowindex);
    updateDeliveryCosts(event);
    updateTotals();
}

