
var form, $qo, line = [null];

function init_page(){
	form = document.forms["quick-order"];
	$qo = $("#quick-order-table>tbody");
	add_quickorder_line(); add_quickorder_line(); add_quickorder_line();
}

function sku_entry(e){
	//console.dir(e);
	var charCode = e.charCode? e.charCode: e.keyCode;
	if (charCode==13){
		e.preventDefault();
		return false;
	}
}

function quantity_entry(e){
	var id_ = parseInt(/(\d+)/.exec(e.target.name)[0]);
	if (line[id_].timer){clearTimeout(line[id_].timer); line[id_].timer = 0;}
	
	var do_check_sku = false;
	
	var charCode = e.charCode? e.charCode: e.keyCode;
	//alert(charCode);
	if ((charCode >=48 && charCode <=57) || (charCode >=96 && charCode <=105)){ //allow only digits
		do_check_sku = true;
	}else if (charCode!=8 && charCode!=9){ //prevent anything else except backspace and tab
		e.preventDefault();
		return false;
	}else if (charCode!=9){
		do_check_sku = true;
	}
	
	if (do_check_sku){
		$qo.find("tr>td>img[name='sku_status_"+ id_ +"']").attr("src", "/images/misc/loading.gif");
		line[id_].timer = setTimeout("sku_lookup("+ id_ +")", 2000);
	}
}

function sku_lookup(id_){
	
	line[id_].timer = 0;
	
	var fn_on_ajax_success = function(obj){
		//console.dir(obj);
		id_ = parseInt(obj.echo);
		if (obj.result == "OK"){
			if (obj.item_quantity == 0){
				form.elements["sku_qty_"+ id_].value = '';
				$qo.find("tr>td>img[name='sku_status_"+ id_ +"']").attr("src", "/images/misc/1.gif");
				$qo.find("tr>td>span[name='sku_text_"+ id_ +"']").empty();
			}else{
				form.elements["sku_qty_"+ id_].value = obj.item_quantity;
				html =  "<a href=\""+ obj.item_url +"\">"+ obj.item_description +"</a>" +" - "+ obj.item_price + (!obj.item_onhand? "<br /><span style=\"color:#C72361\">"+ obj.item_shipsin +"</span>": "");
				$qo.find("tr>td>span[name='sku_text_"+ id_ +"']").html(html);
				$qo.find("tr>td>img[name='sku_status_"+ id_ +"']").attr("src", "/images/misc/ok_check.gif");
			}
		}else{
			form.elements["sku_qty_"+ id_].value = '';
			$qo.find("tr>td>img[name='sku_status_"+ id_ +"']").attr("src", "/images/misc/x_failed.png");
			if ("item_url" in obj){
				html =  "<a href=\""+ obj.item_url +"\">"+ obj.item_description +"</a><br />This product has options and cannot be ordered directly. Enter an option SKU or see product page.";
				$qo.find("tr>td>span[name='sku_text_"+ id_ +"']").html(html);
			}else{
				$qo.find("tr>td>span[name='sku_text_"+ id_ +"']").text(obj.message);
			}
			
		}
	};
	
	var sku, qty;
	sku = form.elements["sku_"+ id_].value.trim();
	qty = parseInt(form.elements["sku_qty_"+ id_].value);
	if (isNaN(qty)) qty = 0;
	var re = /"/g;
	
	$.ajax({
		url: "/quick-order?sku_lookup", type: "POST",	//contentType: "application/json",
		data: "JSON="+ escape("{\"sku\":\""+ sku.replace(re,"\\\"") +"\",\"quantity\":"+ qty +",\"echo\":\""+ id_ +"\"}"),
		dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Error!\n"+ textStatus +"\n"+ errorThrown);
			console.dir(XMLHttpRequest);
		},
		success: function(data, textStatus){
			//alert("HTTP "+ this.type +"\n"+ textStatus +"\n"+ typeof(data));
			//console.dir(data);
			if (!("error" in data)) fn_on_ajax_success(data);
			else alert("Error!\n"+data.error);
		},
		complete: function(XMLHttpRequest, textStatus){
			//alert("Complete.\n"+ textStatus);
		}
	});
}


function add_quickorder_line(){
	var id_ = $qo.children().length + 1;	//alert(id_);
	var html = "<tr>\
	<td class=\"c1\"><input type=\"text\" name=\"sku_"+ id_ +"\" value=\"\" maxlength=\"30\" class=\"qo_sku\"></td>\
	<td class=\"c2\"><input type=\"text\" name=\"sku_qty_"+ id_ +"\" value=\"\" size=\"3\" maxlength=\"8\" class=\"qo_sku_qty\"></td>\
	<td class=\"c3\"><img name=\"sku_status_"+ id_ +"\" src=\"/images/misc/1.gif\" width=\"16\" height=\"16\" align=\"absmiddle\">&nbsp; <span name=\"sku_text_"+ id_ +"\"></span></td>\
	</tr>";
	$qo.append(jQuery(html));
	var $n = $qo.find("tr>td>input[name='sku_"+ id_ +"']");
	$n.keydown(sku_entry);
	$n = $("#quick-order-table>tbody>tr>td>input[name='sku_qty_"+ id_ +"']");
	$n.keydown(quantity_entry);
	line[id_] = {"timer":0};
}

