function hide_shoppingcart(){
	var cartlayer = jslib.$('cart_layer');
	if(cartlayer){
		cartlayer.style.display = 'none';
	}
}
function show_shoppingcart(force){
	var cartlayer = jslib.$('cart_layer');
	if(cartlayer){
		if(force || cartlayer.style.display == 'none'){
			cartlayer.style.display = '';
		}else{
			hide_shoppingcart();
		}
	}
}
function load_shoppingcart(isshow){
	var cartlayer = jslib.$('cart_layer');
	if(cartlayer){
		jslib.ajax.load(site_root_path + 'service/shopping.ashx?do=getcart', function(request){
			eval(request.responseText);
			if(typeof(cart) != 'undefined' && cart != null){
				var carttable = jslib.$('carttable');
				for(var i=carttable.rows.length-1; i>0; i--){
					carttable.deleteRow(i);
				}
				var spcertnames = jslib.$$('spcertname');
				if(cart.certification){
					var h;
					h = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
					h += '<tr class="cart_title"><th style="height:22px;"></th><th width="16%" align="center">Remove</th></tr>';
					h += '<tr>';
					h += '<td style="text-align:left;">Certification Program:&nbsp;&nbsp;&nbsp;&nbsp;{0}</td>'.format(cart.certification.shortName);
					h += '<td align="center" style="text-align:center;"><img style="cursor:pointer" src="{0}skin/system/images/ico_del.gif" onclick="remove_items(\'removecert\',{1});" align="absmiddle" /></td>'.format(site_root_path, cart.certification.id);
					h += '</tr>';
					h += '</table>';
					//h = 'Certification Program:&nbsp;&nbsp;&nbsp;&nbsp;' + cart.certification.shortName + '&nbsp;&nbsp;&nbsp;&nbsp;' + '<div style="float:right;padding-right:20px;"><img style="cursor:pointer" src="{0}images/ico_del.gif" onclick="remove_items(\'removecert\',{1});" align="absmiddle" /></div>'.format(site_root_skin_path, cart.certification.id)
					spcertnames.html(h);
					spcertnames.show();
				}else{
					spcertnames.hide();
				}
				if(cart.exams && cart.exams.length > 0){
					for(var i=0; i<cart.exams.length; i++){
						addexam_tocarttable(i+1,carttable, cart.exams[i], cart.certification != null);
					}
				}
				if(cart.totalAmount)jslib.$$('spAmount').html(cart.totalAmount);
				if(cart.payAmount)jslib.$$('spPayAmount').html(cart.payAmount);
				if(cart.payDeposit)jslib.$$('spPayDeposit').html(cart.payDeposit);
				if(cart.certification != null){
					if(cart.discount != null){
						var h = '';
						if(!cart.discount.limiting && cart.certification.priceDiscount > 0){
							h += cart.certification.priceDiscount + '%';
						}
						if(cart.discount.rate > 0){
							if(h != '')h += ' + ';
							h += cart.discount.rate + '%';
						}
						if(cart.discount.parValue != '0.00'){
							if(h != '')h += ' - ';
							h += '$' + cart.discount.parValue;
						}
						jslib.$$('spCertDiscount').html(h);
					}else{
						jslib.$$('spCertDiscount').html(cart.certification.priceDiscount + '%');
					}
					jslib.$$('certspan').show();
					jslib.$$('examspan').hide();
				}else{
					jslib.$$('certspan').hide();
					jslib.$$('examspan').show();
				}
				cart = null;
			}
			if(isshow){
				show_shoppingcart(true);
				cartlayer.focus();
			}
		});
	}
}
function addexam_tocarttable(index, table, exam, iscert){
	var tr = table.insertRow(-1);
	tr.align = 'center';

	var td = document.createElement('td');
	td.align = 'center';
	td.innerHTML = exam.code;
	tr.appendChild(td);

	td = document.createElement('td');
	td.align = 'center';
	td.innerHTML = exam.durationDay + (exam.durationDay > 1 ? ' days' : ' day');
	tr.appendChild(td);

	td = document.createElement('td');
	td.align = 'center';
	td.innerHTML = '$' + exam.price;
	tr.appendChild(td);

	td = document.createElement('td');
	td.align = 'middle';
	td.innerHTML = '<img style="cursor:pointer" src="{0}skin/system/images/ico_del.gif" onclick="remove_items(\'{1}\',{2});" />'.format(site_root_path, (iscert ? 'removecertexam' : 'removeexam'), exam.id);
	tr.appendChild(td);
}
function remove_items(todo,id){
	jslib.ajax.load(site_root_path + 'service/shopping.ashx?do={0}&id={1}'.format(todo, id), function(request){
		load_shoppingcart();
	});
}
load_shoppingcart();