﻿var products;
var locations;
var basket=[];
var basketindex=0;
var deliprice=0;

function sendRequest(a,args,cb) {
	new Ajax.Request('onlinepurchaseaction.asp', { method: 'post', parameters: 'a='+a+(args==''?'':('&'+args)), onComplete: cb });
}

function locationChange() {
	if ($('selLocations').value=='1')
		deliprice=deli_us1;
	else
		deliprice=deli_us2;
	updateBasket();
}

function setLocations() {
	t='<select id="selLocations" onchange="locationChange()">\n';
	
	locations.locations.each(function(item) {
		t+='	<option value="'+item.v+'">'+item.n+'</option>\n';
	});
	
	t+='</select>\n';
	
	$('divLocations').innerHTML=t;
	locationChange();
}

function deliverCheck() {
	if ($('rdoDeliver1').checked) {
		$('divLocations').innerHTML='N/A';
		deliprice=deli_hk;
	} else {
		$('divLocations').innerHTML='<img src="selfhelp/loading.gif" />';
		sendRequest('l','',setLocations);
	}
	updateBasket();
}

function searchProduct(pid) {
	ret=null;
	products.products.each(function(item) {
		if (ret==null && item.pid==pid) {
			ret=item;
		}
	});
	return ret;
}

function updateBasket() {
	price=0;
	boxes=0;
	index=0;
	hk=$('rdoDeliver1').checked;
	t='<table border="1" style="width: 100%">\n\
		<tr>\n\
			<th>#</th>\n\
			<th>'+rc_item+'</th>\n\
			<th style="text-align: right"><span id="spnPrice">'+rc_price+' ('+(hk?'HKD':'USD')+')</span></th>\n\
			<th></th>\n\
		</tr>\n\
';
	
	deliprice=0;
	var area=hk?0:($('selLocations').value=='1'?1:2);
	
	basket.each(function(item) {
		parts=item.split('|');
		prod=searchProduct(parseInt(parts[1]));
		oneprice=parseFloat(parts[2]=='0'?(hk?prod.box_hkd:prod.box_usd):(hk?prod.esd_hkd:prod.esd_usd));
		price+=oneprice;
		t+='	<tr><td>'+(++index)+'</td><td>'+prod.name+(parts[2]=='0'?rc_boxed:rc_down)+'</td><td style="text-align: right">'+oneprice.toFixed(2)+'</td><td><a href="javascript:void(0)" onclick="removeBasket(\''+item+'\')">'+rc_remove+'</a></td></tr>\n';
		if (parts[2]=='0') {
			boxes++;
			oneprice=parseFloat(area==0?prod.postage_hkd:area==1?prod.postage_usd1:prod.postage_usd2);
			price+=oneprice;
			deliprice+=oneprice;
		}
	});
	
	if (boxes>0) {
		// oneprice=deliprice*boxes;
		// price+=oneprice;
		
		t+='	<tr><td>'+(++index)+'</td><td>'+rc_deliver1+boxes+rc_deliver2+'</td><td style="text-align: right">'+deliprice.toFixed(2)+'</td><td></td></tr>\n';
	}
	
	if (price>0) {
		t+='	<tr><td></td><td>'+rc_total+'</td><td style="text-align: right">'+price.toFixed(2)+'</td><td><span id="spnCheckout"><a href="javascript:void(0)" onclick="checkout()">'+rc_checkout+'</a></span></td></tr>\n';
	}
	t+='</table>\n';
	
	$('divCheckout').innerHTML=t;
}

function removeBasket(s) {
	basket=basket.without(s);
	updateBasket();
}

function addBasket(pid,esd) {
	basket[basket.length]=(basketindex++)+'|'+pid+'|'+esd;
	updateBasket();
}

function setProducts() {
	t='<table border="1">\n';
	t+='	<tr style="text-align: center"><th>'+rc_product+'</th><th>'+rc_usd+'</th><th>'+rc_hkd+'</th><th>&nbsp;</th></tr>\n';
	
	products.products.each(function(item) {
		if (item.pid!=0) {
			if (item.box_usd!='0.00')
				t+='	<tr><td style="width: 100%"><a href="products.asp?pf=w&id='+item.pid+'" target="_blank">'+item.name+(parseInt(item.pid)<100?rc_boxed:'')+'</a></td><td nowrap="nowrap" style="padding: 0px 5px 0px 5px; text-align: right">$'+item.box_usd+'</td><td nowrap="nowrap" style="padding: 0px 5px 0px 5px; text-align: right">$'+item.box_hkd+'</td><td nowrap="nowrap"><a href="javascript:void(0)" onclick="addBasket('+item.pid+',0)">'+rc_buy+'</a></td></tr>\n';
			if (item.esd_usd!='0.00')
				t+='	<tr><td style="width: 100%"><a href="products.asp?pf=w&id='+item.pid+'" target="_blank">'+item.name+''+rc_down+'</a></td><td nowrap="nowrap" style="padding: 0px 5px 0px 5px; text-align: right">$'+item.esd_usd+'</td><td nowrap="nowrap" style="padding: 0px 5px 0px 5px; text-align: right">$'+item.esd_hkd+'</td><td nowrap="nowrap"><a href="javascript:void(0)" onclick="addBasket('+item.pid+',1)">'+rc_buy+'</a></td></tr>\n';
		}
	});
	
	t+='</table>\n';
	
	$('divProductList').innerHTML=t;
}

function doCheckout() {
	document.forms['frmPaypal'].submit();
}

function checkout() {
	price=0;
	boxes=0;
	index=0;
	hk=$('rdoDeliver1').checked;
	q='';
	
	if (hk)
		q='t=0';
	else {
		q='t='+$('selLocations').value;
	}
	
	basket.each(function(item) {
		q+='&p'+(++index)+'='+item;
	});
	
	$('spnCheckout').innerHTML='<img src="selfhelp/loading.gif" />';
	sendRequest('c',q,doCheckout);
}

sendRequest('p','t='+$('rdoDeliver1').checked?'hk':'us',setProducts);
deliverCheck();
updateBasket();

