
function num_format(expr, decplaces) {
	var str = ' ' + Math.round(eval(expr) * Math.pow(10, decplaces));
	while (str.length <= decplaces) {
		str = '0' + str;
	}
	var decpoint = str.length - decplaces;
	return '$' + str.substring(1, decpoint) + '.' + str.substring(decpoint,str.length);
}

function calc(object, row) {
	var total = 0.0;
	var line_total = 0.0;
	var stringValue = '';
	var form = window.document.purchaseForm;
        
	start = 0;
	end = parseInt(form.elements['sys_product_row_count'].value);
	
        for(i=start; i <= end; i++) {
                        
                quote_quantity = 'quantity_'  + String(i);
                quote_limit = 'limit_' +  String(i);
                soldout = 'soldout_' +  String(i);
                quote_price = 'price_' +  String(i);
                        
                quote_value = 'value_' + String(i);
                quote_discount = 'discount_' +  String(i);
                var value = 'frm_value_' + String(i);
                var actual_value = 'frm_actual_value_' + String(i);
				
               if (form.elements[quote_quantity].value != '') {
                    if (parseInt(form.elements[quote_quantity].value) >= 0) {
                    
                        var limit = parseInt(form.elements[quote_limit].value);
                        var quantity = parseInt(form.elements[quote_quantity].value);
                        if ((form.elements[quote_limit].value == '' || 
                            limit >= quantity || limit == 0 ) &&
                            form.elements[soldout].value != 'Y') {
                            
                            var price = parseFloat(form.elements[quote_price].value);
                            var discount = parseFloat(form.elements[quote_discount].value);
                                        
                            line_total = (price - discount) * quantity;
                            form.elements[quote_value].value = line_total;	
                            fmt = '$' + line_total;
                            
                            form.elements[value].value = num_format(line_total, 2);
                            form.elements[actual_value].value = line_total;
                            total = total + line_total;
                        }
                    }
                }
                else {
                    form.elements[quote_value].value = 0.0;	
                    fmt = '$' + 0;
                    form.elements[value].value = fmt;
                }
        }

//	if (total > 0) 
//            form.elements['total'].value = num_format(total, 2);
//	else
//            form.elements['total'].value = '$0.0';
}

function trim(object) {
	value = object.value;
	object.value=value;
}

function paymentMethod(type) {
	var form = window.document.displayProduct;
	form.action.value= type;
	form.submit();
}


function saveCustomer() {       
	var form = window.document.manage;
	form.action.value= 'saveCustomer';
	if (validate('shop')) form.submit();
}

function savePayment() {       
	var form = window.document.payments;
	form.action.value= 'savePayment';
	if (validatePayment()) form.submit();
}

function validatePayment() {

	var form = window.document.payments;
	if (!validateObject(form.frm_card_holder)) return false;

	if (form.sys_expiry_month_list.options[form.sys_expiry_month_list.selectedIndex].value == "Select") {
		form.sys_expiry_month_list.style.backgroundColor = '#e0d4bc';
		form.sys_expiry_month_list.focus();
		confirm("Please an expiry month from the list.");
		return false;
	}

	if (form.sys_expiry_year_list.options[form.sys_expiry_year_list.selectedIndex].value == "Select") {
		form.sys_expiry_year_list.style.backgroundColor = '#e0d4bc';
		form.sys_expiry_year_list.focus();
		confirm("Please an expiry year from the list.");
		return false;
	}

	if (!validateObject(form.frm_card_number_1)) return false;
	if (!validateObject(form.frm_card_number_2)) return false;
	if (!validateObject(form.frm_card_number_3)) return false;
	if (!validateObject(form.frm_card_number_4)) return false;

	return true;
}


function validate(action) {
	var form = window.document.manage;
	
	if (!validateObject(form.frm_firstname)) return false;
	if (!validateObject(form.frm_surname)) return false;	

	if (!validateObject(form.frm_delivery_address_1)) return false;	
	if (!validateObject(form.frm_delivery_address_2)) return false;	
	
	if (!validateObject(form.frm_home_phone)) return false;	

	form.frm_email.style.backgroundColor = '#ffffff';
	if (!validateObject(form.frm_email)) return false;
	if (!isEmail(form.frm_email.value)) {
		form.frm_email.style.backgroundColor = '#e0d4bc';
		form.frm_email.focus();
		confirm("Please enter a valid email address.");
		return false;
	}

	if (!form.frm_courier_consent.checked) {
		form.frm_courier_consent.style.backgroundColor = '#e0d4bc';
		form.frm_courier_consent.focus();
		confirm("Please tick the indemity Emporios and their courier company.");
		return false;
	}

	
	return true;
}


function listChanged(object) {
    var form = window.document.manage;
	if (object.name == 'month_list' || object.name == 'year_list') {
		form.expiry_date.value = form.month_list.options[form.month_list.selectedIndex].text + '/' +  form.year_list.options[form.year_list.selectedIndex].text ;
	}
	else if (object.name == 'frm_delivery_country' || object.name == 'frm_postal_country') {
		form.action.value= 'checkout';
		form.submit();
	}
	else  if (object.name != "frm_account_type") {
		form.action.value= 'changed';
		form.submit();
	}
}

function buyProduct(item_id) {       
    var form = window.document.shopForm;
    
    form.sys_item_id.value = item_id;
	form.action.value= 'selectQty';
	form.submit();
}


function addSample(item_id, thig) {       
    var form = window.document.shopForm;
    form.sys_item_id.value = item_id;
	form.action.value= 'quickBuy';
	form.submit();
}


function frmDel(product_id, row, category, sub_category, style, colour) {
    var form = window.document.checkoutForm;
    form.sys_item_id.value  = product_id;
    form.style.value = style;
    form.colour.value = colour;
    form.category.value = category;
    form.sub_category.value = sub_category;
	form.action.value = "deleteItem";
    if (confirm("Are you sure you want to delete this product from your order.")) form.submit();
}

function frmAdd(id, row, category, sub_category) {       
    var totalQty = 0;
    var form = window.document.purchaseForm;
    
    
	quote_quantity = 'quantity_'  + String(row);
	var quantity = parseInt(form.elements[quote_quantity].value);
	if (!isNaN(quantity)) totalQty = totalQty + quantity;

    if (totalQty <= 0)
		alert("You have not selected any products.");
    else {
		form.action.value= 'buy';
		
		form.value_0.value = form.frm_actual_value_0.value; 
		form.submit();
    }
}

function checkout() {     
	var form = window.document.checkoutForm;
	form.action.value= 'checkout';
	form.submit();
}

function cancelDel() {       
	var form = window.document.checkout;
	form.action.value= 'cancel';
	form.submit();
}

function cancel() {       
	var form = window.document.checkoutForm;
	form.action.value= 'cancelOrder';
	form.submit();
}
function cancelOrder() {       
	var form = window.document.manage;
	form.action.value= 'cancelOrder';
	form.submit();
}
function cancelPayment() {       
	var form = window.document.displayProduct;
	form.action.value= 'cancelOrder';
	form.submit();
}

