// JavaScript Document

function send_wrap(ajax_object,cart_func,load_id,status_id,remove_id,form_id,id,qty,id_real,price){
	ajax_object.sendData("cart_func="+cart_func+"&id="+id+"&qty="+qty+"&id_real="+id_real+"&price="+price,null,null,null,null,null,null,{"cart_func":cart_func,"item_id":id,"status_id":status_id,"remove_id":remove_id,"form_id":form_id},{"id":load_id},null);
	return false;
}

//Response Function
function ajax_response(r_type,r_data,args){
	switch(r_type){
		case "xml":
			var r_xml = r_data;
			var status = r_xml.getElementsByTagName("status");
			var message = r_xml.getElementsByTagName("message");
			var total_items = r_xml.getElementsByTagName("total_items");
			var item_qty = r_xml.getElementsByTagName("item_qty");
			var total = r_xml.getElementsByTagName("total");
					
			//alert(message[0].childNodes[0].nodeValue);
			
			// Update Shopping Basket
			document.getElementById("cart_items").innerHTML = total_items[0].childNodes[0].nodeValue;
			
			if(item_qty[0].childNodes[0].nodeValue==0){
				if(document.getElementById(args.remove_id)){
					document.getElementById(args.remove_id).innerHTML = "";
				}
				// Update item status
				if(document.getElementById(args.status_id)){
					document.getElementById(args.status_id).innerHTML = "";
				}
				if(args.cart_func == "edit" || args.cart_func == "remove"){
					// Update item status
					if(document.getElementById(args.form_id)){
						document.getElementById(args.form_id).elements['qty'].value = item_qty[0].childNodes[0].nodeValue;
					}
				}						
			}else{
				if(args.cart_func == "add"){
					// remove item button
					if(document.getElementById(args.remove_id)){
						document.getElementById(args.remove_id).innerHTML = window.cart_templates.remove_button;
					}
					// Update item status
					if(document.getElementById(args.status_id)){
						document.getElementById(args.status_id).innerHTML = "("+item_qty[0].childNodes[0].nodeValue+" in cart)";
					}					
				}else if(args.cart_func == "edit"){
					// remove item button
					if(document.getElementById(args.remove_id)){
						document.getElementById(args.remove_id).innerHTML = window.cart_templates.remove_button;
					}					
					// Update item status
					if(document.getElementById(args.status_id)){
						document.getElementById(args.status_id).innerHTML = "("+item_qty[0].childNodes[0].nodeValue+" in cart)";
					}
					if(document.getElementById(args.form_id)){
						document.getElementById(args.form_id).elements['qty'].value = item_qty[0].childNodes[0].nodeValue;
					}					
				}
			}
			// Update Total (check out)
			if(document.getElementById("item_total")){
				document.getElementById("item_total").innerHTML = total[0].childNodes[0].nodeValue;
			}
		break;
		case "txt":
			var r_txt = r_data;
			alert('r_txt')
		break;
	}
}
//Load Status function
function ajax_load(loading,args){
	if(loading){
		document.getElementById(args.id).innerHTML = "Loading...";
	}else{
		document.getElementById(args.id).innerHTML = "";	
	}	
}
//No Data function
function ajax_no_data(args){
	alert('No Data');
}

//constructor: var a = new ajax(params, script_name, s_method, output_format, response_func, load_func, no_data_func);
//sendrequest: ajax.sendData(params, script_name, s_method, output_format, response_func, load_func, no_data_func, response_func_arg_array, load_func_arg_array, no_data_func_arg_array);

//define new object
var ca = new ajax('','/modules/cart/cart_func_ajax.xml.php','post','xml',ajax_response,ajax_load,ajax_no_data);


