

// BASKET STUFF

var last_action = '';
var request_in_progress = false;
var xmlhttp=false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}



var update_slots = new Array();
var update_timer = null;
function set_item_data(item_id, qty, option_id){
//	if (request_in_progress){return false;}
//	if (document.location.pathname.indexOf('view_basket')>-1){return true;}
	request_in_progress = true;
    url = '/update_basket.php';
    data_a = new Array();
    if (qty){
    	data_a.push('items['+item_id+'][qty]=' + qty);
    }
    if (option_id){
    	data_a.push('items['+item_id+'][option_id]=' + option_id);
    }

    if (!xmlhttp){
        return true;
    }
    else {
//        show_loading();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=receive_basket_item;
    }
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(data_a.join('&') + '&output_format=json');
//    last_action = 'removed';
    return false;
}
function start_que(){
	if (update_timer){
		window.clearTimeout(update_timer);
	}
	update_timer = window.setTimeout('process_que()', 700);
}
function process_que(){
	for (var item_id in update_slots){
		slot = update_slots[item_id];
		set_item_data(slot.item_id, slot.qty, slot.option_id);
	}
	update_slots = new Array();
}
function set_item_option(item_id, option_id){

}
function add_to_que(item_id, qty, option_id){
	tpele = document.getElementById('view_basket_totalprice_' + item_id);
	if (tpele)
		tpele.innerHTML = '<img src="/images/loading.gif" height="20" alt="Loading...">';
	upele = document.getElementById('view_basket_unitprice_' + item_id);
	if (upele)
		upele.innerHTML = '<img src="/images/loading.gif" height="20" alt="Loading...">';
	document.getElementById('view_basket_totalprice').innerHTML = '<img src="/images/loading.gif" height="20" alt="Loading...">';

	data = {'item_id':item_id};
	if (qty){
		data.qty = qty;
	}
	if (option_id){
		data.option_id = option_id;
	}
	update_slots[item_id] = data;
	start_que();
}
function add_single_item(item_id){
	qty_ele = document.getElementById('view_basket_qty_'+item_id);
	if (!qty_ele){
		return true;
	}
	qty = parseInt(qty_ele.value) + 1;
	// set so we can do repeated events better
	qty_ele.value = qty;
	//set_item_qty(item_id, qty);
	add_to_que(item_id, qty);

	return false;
}
function remove_single_item(item_id){
	qty_ele = document.getElementById('view_basket_qty_'+item_id);
	if (!qty_ele){
		return true;
	}
	qty = parseInt(qty_ele.value) - 1;
	//set_item_qty(item_id, qty);
	add_to_que(item_id, qty);
	qty_ele.value = qty;

	return false;
}
function receive_basket_item(){
	if (update_slots.length){
		return false;
	}
	if (xmlhttp.readyState==4) {
		dcstat = document.getElementById('view_basket_discount_code_status');
		if (dcstat){
			dcstat.innerHTML = '';
		}

		resp = xmlhttp.responseText;
		try {
			eval(resp);
		    for (i=0;i<mybasket.items.length;i++){
		    	item = mybasket.items[i];
				qtyele = document.getElementById('view_basket_qty_' + item.basket_item_id);
				if (qtyele)
					qtyele.value = item.quantity;
				upele = document.getElementById('view_basket_unitprice_' + item.basket_item_id);
				if (upele)
					upele.innerHTML = item.unit_price;
				tpele = document.getElementById('view_basket_totalprice_' + item.basket_item_id);
				if (tpele)
					tpele.innerHTML = item.total_price;
		    }
			document.getElementById('view_basket_totalprice').innerHTML = mybasket.total_price;
			dcstat.innerHTML = '<b>DISCOUNT CODE NOT FOUND</b>';
			hide_loading();
		}
		catch (e){
//			alert(e);
			hide_loading();
			if (blele = document.getElementById('basket_loading')){
				blele.innerHTML = 'ERROR LOADING BASKET';
			}
		}
		request_in_progress = false;
	}
}

function update_discount_code(){
	dcele = document.getElementById('view_basket_discount_code');
	if (dcele){
		discount_code = dcele.value;
		if (discount_code){
			dcstat = document.getElementById('view_basket_discount_code_status');
			dcstat.innerHTML = '<img src="/images/loading.gif" height="20" alt="Loading...">';
		    if (!xmlhttp){
		        return true;
		    }
		    else {
			    url = '/update_basket.php';
		//        show_loading();
		        xmlhttp.open('POST', url, true);
		        xmlhttp.onreadystatechange = receive_basket_item;
		    }
		    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    xmlhttp.send('discount_code='+discount_code+'&output_format=json');
		}
	}
}







function add_to_basket(id, option_id, qty){
//return true;
	if (request_in_progress){return false;}
	if (document.location.pathname.indexOf('view_basket')>-1){return true;}
	request_in_progress = true;
    url = '/add_to_basket.php';
    data = 'product_id='+id+'&productoption_id='+ option_id + '&quantity='+qty;
    if (!xmlhttp){
        return true;
    }
    else {
        show_loading();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=receive_basket;
    }
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(data + '&output_format=json');
    last_action = 'added';
    return false;
}

function remove_from_basket(item_id){
	if (request_in_progress){return false;}
	if (document.location.pathname.indexOf('view_basket')>-1){return true;}
	request_in_progress = true;
    url = '/update_basket.php';
    data = 'items['+item_id+'][qty]=0';
    if (!xmlhttp){
        return true;
    }
    else {
        show_loading();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=receive_basket;
    }
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(data + '&output_format=json');
    last_action = 'removed';
    return false;
}
function load_basket(){
	if (document.cookie.indexOf('basket_id=')==-1){
    bp = document.getElementById('basket_products');
    if (bp){
    	bp.innerHTML = 'BASKET EMPTY';
    }
		return false;
	}

    url = '/view_basket.php';
    if (!xmlhttp){
        return true;
    }
    else {
        show_loading();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=receive_basket;
    }
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('output_format=json');

}

function receive_basket(){
	if (xmlhttp.readyState==4) {
		resp = xmlhttp.responseText;
		try {
			eval(resp);
			populate_basket(mybasket);
			hide_loading();
		}
		catch (e){
			hide_loading();
			document.getElementById('basket_loading').innerHTML = 'ERROR LOADING BASKET';
		}
		request_in_progress = false;
	}
}

function populate_basket(basket_data){
    if (typeof basket_data != 'object'){return false;}
    items = basket_data.items;
	total_items = 0;

    product_html = '<table cellpadding="2" cellspacing="0" class="basket_items">';
    if (items.length==0){
        product_html += '<tr><td colspan="6">[ BASKET EMPTY ]</td></tr>';
    }
	total_qty = new Array();
    for (i=0;i<items.length;i++){
		if (!items[i] || !items[i]['product_id']){
			continue;
		}
		if (typeof total_qty[items[i]['product_id']] == 'undefined'){
			total_qty[items[i]['product_id']] = 0;
		}
    	if (typeof items[i] == 'undefined'){continue;}
        product_html += '<tr>';
        product_html += '<td valign="top"><a href="/remove_from_basket.php?product_id='+items[i]['product_id']+'&productoption_id='+items[i]['option_id']+'&quantity=1" onclick="return true;if (document.getElementById(\'in_basket_'+items[i]['product_id']+'\')){document.getElementById(\'in_basket_'+items[i]['product_id']+'\').innerHTML=\'\';}; return remove_from_basket('+items[i]['basket_item_id']+', '+(items[i]['quantity']-1)+');"><img src="/images/minus.gif" border="0" height="12"></a></td>';
        product_html += '<td valign="top">' + items[i]['quantity'] + '</td>';
        product_html += '<td valign="top"><a href="/add_to_basket.php?product_id='+items[i]['product_id']+'&productoption_id='+items[i]['option_id']+'&quantity=1" onclick="return add_to_basket('+items[i]['product_id']+', '+items[i]['option_id']+', 1);"><img src="/images/plus.gif" border="0" height="12"></a></td>';
        product_html += '<td valign="top"> &nbsp; </td><td class="basket_item">'+items[i]['product_name'];
		if (items[i]['option_name'] != ''){
			product_html += ' - ' + items[i]['option_name'];
		}
		product_html += '</td><td class="basket_item_price">' + items[i]['total_price'] + '</tr>';

		total_qty[items[i]['product_id']] += parseInt(items[i]['quantity']);
        // when on product listing page, adds number above buy now
        if (document.getElementById('in_basket_'+items[i]['product_id'])){
        	document.getElementById('in_basket_'+items[i]['product_id']).innerHTML = total_qty[items[i]['product_id']] + ' in your basket';
        }

		total_items += parseInt(items[i]['quantity']);
    }
    product_html += '<tr><td colspan="6"><hr class="basket_line"></td></tr>';
    product_html += '<tr><td colspan="6" align="right"><b>TOTAL : ' + basket_data.total_price + '</b></td></tr>';
    product_html += '</table>';

	if (document.getElementById('basket_count')){
		document.getElementById('basket_count').innerHTML = total_items;
	}

	if (document.getElementById('basket_total')){
		document.getElementById('basket_total').innerHTML = basket_data.total_price;
	}

    if (basket_data.last_order_id){
        product_html += '<table cellpadding="1" cellspacing="0" width="100%">';
        product_html += '<tr>';
        product_html += '<td colspan="2"><h2>LAST ORDER</h2></td>';
        product_html += '</tr>';
        product_html += '<tr>';
        product_html += '<td>Order Number : </td><td><a href="/order_status.php">'+basket_data.last_order_id+'</a></td>';
        product_html += '</tr>';
        product_html += '<tr>';
        product_html += '<td>Total : </td><td>'+basket_data.last_order_value+'</td>';
        product_html += '</tr>';
        product_html += '</table>';
    }

    bp = document.getElementById('basket_products');
    if (bp){
    	bp.innerHTML = product_html;
    }
    if (last_action=='added'){
    	show_just_added();
    }
    if (last_action=='removed'){
    	show_just_removed();
    }
    last_action = '';
}

function show_loading(){
    html = '<div style="width:150px;text-align:center;background-color:#fff;border-radius:5px;padding:10px;margin-bottom:5px;"><img src="/images/loading.gif" width="20" height="20"><br><h3>LOADING</h3></div>';
    bp = document.getElementById('basket_products');
    if (bp){
    	bp.innerHTML = html;
    }

}
function hide_loading(){
    //document.getElementById('basket_products').innerHTML = '';
}

function get_window_width(){
    if (document.body.offsetHeight){
        return document.body.offsetWidth;
    }
    else {
        return window.innerWidth;
    }
}

function get_window_height(){
    if (document.body.offsetHeight){
        return document.body.offsetHeight;
    }
    else {
        return window.innerHeight;
    }
}

function centre_element(ele){
	if (ele){
		ele.style.top = parseInt(document.body.scrollTop) + (get_window_height()/2) - 50;
		ele.style.left = (get_window_width()-parseInt(document.getElementById('just_added').style.width))/2;
	}
}

function show_just_added(){
	ele = document.getElementById('just_added');
	if (ele){
		centre_element(ele);
		ele.style.display='block';
		setTimeout('hide_just_added()', 2400);
	}
}

function hide_just_added(){
	ele = document.getElementById('just_added');
	if (ele){
		ele.style.display='none';
	}
}

function show_just_removed(){
	ele = document.getElementById('just_removed');
	if (ele){
		centre_element(ele);
		ele.style.display='block';
		setTimeout('hide_just_removed()', 2400);
	}
}

function hide_just_removed(){
	document.getElementById('just_removed').style.display='none';
}
