/*
 * E-Commerce - Cart Functions
 * Revision: 1.1.2
 */

function addCart(id, sample) {
	if (typeof(sample) == "undefined") sample = false;
	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;
		}
	}
	if (sample) att[0] = 1;
	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;
	}
	
	if (response['err']) {
		alert(response['err']);
		return;
	}
	
	setStatus('Cart updated', true);
	
	// Sample offering
	if (response['offer_url']) {
		if (confirm("You haven't ordered a sample of this product before. Would you like a sample of this item?")) {
			window.location = response['offer_url'];
			return;
		}
	}
	
	// Redirect to cart
	if (response['redir']) {
		window.location = response['redir'];
	}
}


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() {
	document.checkout.submit();
}

function skipPay() {
	document.getElementById('payAlt').value = -1;
	document.checkout.submit();
}

function payMethodChange() {
	if (document.getElementById('paymentMethod').value == 0) {
		document.getElementById('ccTable').className = 'ccShow';
	} else {
		document.getElementById('ccTable').className = 'ccHide';
	}
}

function addCoupon() {
	document.getElementById('couponMain').style.display = 'block';
	document.getElementById('couponLoading').style.display = 'none';
	document.getElementById('couponCode').value = '';
	openAthenaWindow('couponWindow');
	centerWindow('couponWindow');
	document.getElementById('couponCode').focus();
}

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(), 'coupon_callback');
	return false;
}

function deleteCoupon(code) {
	ajaxCall('/_std.ajax?action=20&code='+code.urlSafe(), 'coupon_callback');	
}

function coupon_callback(txt) {
	if (txt.substr(0,3) == 'ERR') {
		alert(txt.substr(4));
	} else {
		var el = document.getElementById('cartBox');
		try {
			el.innerHTML = txt;
		} catch (e) {
			window.location.refresh();
		}
	}
	closeAthenaWindow('couponWindow');
}

function fixCoupon() {
	var el = document.getElementById('couponCode')
	var val = el.value.toUpperCase().replace(/ /g, '');
	if (val != el.value) el.value = val;
}

function giftUp(maxLen) {
	maxLen = setDefault(maxLen, 110);
	var el = document.getElementById('giftMsg');
	if (el.value.length > maxLen) {
		var pos = getCaretPosition(el);
		el.value = el.value.substr(0, maxLen);
		setCaretPosition(el, pos);
	}
}

function sendFriend() {
	var email = document.getElementById('friendEmail');
	var name = document.getElementById('friendName');
	ajaxCall(uri+'?action=sendToFriend&email='+email.value.urlSafe()+'&name='+name.value.urlSafe(), 'friend_callback');
	
	name.value = '';
	email.value = '';
	
	closeAthenaWindow('friendWindow');
	
	openAthenaWindow('friendNote');
	centerWindow('friendNote');
	changeOpac('friendNote', 0.9);
	document.getElementById('friendNote-body').innerHTML = 'Sending e-mail..';
	
}

function friend_callback(txt) {
	document.getElementById('friendNote-body').innerHTML = 'Message sent!';
	setTimeout('closeAthenaWindow("friendNote")', 2000);
}



