/*
 * E-Commerce - Cart Functions
 * Revision: 1.0.0
 */

function addCart(id) {
	var el = document.getElementById('qty-'+id);
	var qty = el ? el.value : 1;
	var sel = document.getElementsByTagName('SELECT');
	var prefix = 'attrib-'+id+'-';
	var att = new Array();
	for (var i = 0; i < sel.length; i++) {
		el = sel[i];
		if (el.id.substring(0, prefix.length) == prefix) {
			att[el.id.substring(prefix.length)] = el.value;
		}
	}
	setCart(id, qty, att);
}

function setCart(inv, quant, att) {
	if (typeof(att) == "undefined") att = new Array();
	ajaxCall('/_std.ajax?action=14&inv='+inv+'&quant='+quant+'&att='+json_encode(att), 'update_cart');
}

function deleteItem(item) {
	ajaxCall('/_std.ajax?action=18&key='+item, 'cart_refresh');
}

function update_cart(txt) {
	var response;
	try {
		response = eval("("+txt+")");
	} catch (e) {
		setStatus('Error - '+txt);
		return;
	}
	
	setStatus('Cart updated', true);
	if (response['cart']) window.location = response['cart'];
}


function loadCart() {
	openAthenaWindow('cartWindow');
	centerWindow('cartWindow');
}

function cartLoadSubmit() {
	if (document.getElementById('uploadFN').value) {
		return true;
	} else {
		alert('Please select a cart file');
		return false;
	}
}

function postOptChange() {
	var postOpt = document.getElementById('postage').value;
	ajaxCall(uri+'?action=postage&postopt='+postOpt, 'cart_refresh');
}

function cart_refresh(txt) {
	var el = document.getElementById('cartBox');
	try {
		el.innerHTML = txt;
		closeAthenaWindow('couponWindow');
	} catch (e) {
		window.location.refresh();
	}
}

function confirmPay() {
	var paymethod = document.getElementById('paymentMethod').value;
	window.location = uri+'?action=2&paymethod='+paymethod;
}

function skipPay() {
	var paymethod = document.getElementById('paymentMethod').value;
	window.location = uri+'?action=2&paymethod=-1';
}

function payMethodChange() {
	if (document.getElementById('paymentMethod').value == 0) {
		document.getElementById('ccDetails').className = 'pSubSect';
	} else {
		document.getElementById('ccDetails').className = 'pSubSect semiHidden';
	}
}

function addCoupon() {
	document.getElementById('couponMain').style.display = 'block';
	document.getElementById('couponLoading').style.display = 'none';
	document.getElementById('couponCode').value = '';
	openAthenaWindow('couponWindow');
	centerWindow('couponWindow');
}

function submitCoupon() {
	document.getElementById('couponMain').style.display = 'none';
	document.getElementById('couponLoading').style.display = 'block';
	var code = document.getElementById('couponCode').value;
	ajaxCall('/_std.ajax?action=19&code='+code.urlSafe(), 'cart_refresh');
	return false;
}

function deleteCoupon(code) {
	ajaxCall('/_std.ajax?action=20&code='+code.urlSafe(), 'cart_refresh');	
}

function fixCoupon() {
	var el = document.getElementById('couponCode')
	var val = el.value.toUpperCase().replace(/ /g, '');
	if (val != el.value) el.value = val;
}






