/*
	Functions to support the operation of the photosales cart.
*/

// expands the delivery country list
function expand() {
	document.checkout.delivery_location.style.pixelWidth = 250;
}

// contracts the delivery country list
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 }
    if ((($("frames_" + rowindex + "_id").value == 'Y') && (item[2] == 1)) ||  
    	(($("frames_" + rowindex + "_id").value == 'N') && (item[2] == 0))) {
	    var opt = document.createElement("OPTION");
	    opt.text = item[1];
	    opt.value = item[0];
	    opt.fcode = item[4];
	    $("image_size_" + rowindex + "_id").options.add(opt);
    }
}

// Update size based on frame selection
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);
    } else {
    	framePreSelected(event, rowindex);
    }
    
}

function framePreSelected(event, rowindex) {
	//default to unframed
	$("frames_" + rowindex + "_id").selectedIndex = 1;
	
	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 sizeSelected(event, rowindex) {
    displayPrice(event, rowindex);
    updateDeliveryCosts(event);
    updateTotals();
}

