/**
 * @author nfletcher
 */
/* ---------------------------- */
/* XMLHTTPRequest Enable 		*/
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
		return request_type;
}

var http = createObject();

/* -------------------------- */
/* SEARCH					 */
/* -------------------------- */
function autosuggest() {
q = document.getElementById('search-q').value;
smo = document.getElementById('session_mem_org').value;
// Set te random number to add to URL request
nocache = Math.random();
http.open('get', 'include/search.php?q='+q+'&nocache = '+nocache+'&smo='+smo);
http.onreadystatechange = autosuggestReply;
http.send(null);
}
function autosuggestReply() {
if(http.readyState == 4){
	var response = http.responseText;
	e = document.getElementById('search_results');
	if(response!=""){
		e.innerHTML=response;
		e.style.display="block";
		e.style.zIndex="1000000";
	} else {
		e.style.display="none";
	}
}
}

function hide() {
	document.getElementById('fb-modal').style.display="block";
}
function show(src) {
	//document.getElementById('fb-modal').style.display="none";
	window.open(src,"popup","menubar=no,width=300,height=auto,toolbar=no");
}
function togglemodal(src) {
	alert(document.getElementById('fb-modal').style.display);
	if (document.getElementById('fb-modal').style.display=="block"){
		hide();
	} else {
		show(src);
	}
}

/* Change price in user stock order */
var priceid,priceqty,selIndex;

function show_price(id,val,sel) {
	priceid = id;
	priceqty = val;
	selIndex = sel;
// Set the random number to add to URL request
nocache = Math.random();
http.open('get', 'print_order.php?action=get_price&id='+id+'&qty='+val+'&index='+selIndex+'&nocache='+nocache);
http.onreadystatechange = show_price_reply;
http.send(null);
}
function show_price_reply() {
	if(http.readyState == 4){
		var response = http.responseText;
		e = document.getElementById('res_'+priceid);
		if(response!=""){
			e.innerHTML=response;
		} else {
			e.innerHTML='error';
		}
		show_total()
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function show_total() {
	j = 0;
	var totalprice;
	totalprice = 0;
	input = document.getElementsByTagName("input");	
	while (element = input[j++]) {
		if (element.className == "formprice") {
			totalprice = parseInt(element.value)+parseInt(totalprice);
		}
	}
	totaltag = document.getElementById('total');
	totalprice = parseInt(totalprice).toFixed(2);
	totaltag.innerHTML='<span class="good_mini">$'+addCommas(totalprice)+'</span>';
}
/*
function update_qty_and_shipping(qty,id) {
	var updateqty,updateid,fullqty,remainder,shipping;
	updateqty=qty;
	updateid=id;
	fullqty=document.getElementById(updateid+'_qty').value;
	remainder=fullqty-updateqty;
	shipping=(updateqty/10).toFixed(2);
	document.getElementById('display_'+updateid).value=remainder;
	document.getElementById('target_'+updateid).value=remainder;
	document.getElementById('shipping_'+updateid).innerHTML='<span class="good_mini">$'+addCommas(shipping)+'</span><input type="hidden" name="shipping[]" value="'+shipping+'" class="formshipping" />';
	show_total_shipping();
	show_grand_total();
	//alert('qty='+updateqty+' id='+id+' rem='+remainder);
}
*/
/************************************/
/* Change price in user stock order */
var shipid,shipqty,shipIndex;

function update_qty_and_shipping(val,id,sel) {
	shipid = id;
	shipqty = val;
	shipIndex = sel;
// Set the random number to add to URL request
nocache = Math.random();
http.open('get', 'print_order.php?action=get_shipping&id='+shipid+'&qty='+shipqty+'&index='+shipIndex+'&nocache='+nocache);
http.onreadystatechange = show_ship_reply;
http.send(null);
}
function show_ship_reply() {
	var updateqty,updateid,fullqty,remainder,shipping;
	updateqty=shipqty;
	updateid=shipid;
	fullqty=document.getElementById(updateid+'_qty').value;
	remainder=fullqty-updateqty;
	document.getElementById('display_'+updateid).value=remainder;
	document.getElementById('target_'+updateid).value=remainder;
	if(http.readyState == 4){
		var response = http.responseText;
		e = document.getElementById('shipping_'+shipid);
		if(response!=""){
			e.innerHTML=response;
		} else {
			e.innerHTML='error';
		}
		show_total_shipping()
		show_grand_total();
	}
}

function show_total_shipping() {
	j = 0;
	var subtotalshipping;
	subtotalshipping = 0;
	input = document.getElementsByTagName("input");	
	while (element = input[j++]) {
		if (element.className == "formshipping") {
			subtotalshipping = parseInt(element.value)+parseInt(subtotalshipping);
		}
	}
	totalshippingtag = document.getElementById('totalshipping');
	subtotalshipping = parseInt(subtotalshipping).toFixed(2);
	totalshippingtag.innerHTML='<span class="good_mini">$'+addCommas(subtotalshipping)+'</span><input type="hidden" name="shippingtotal" id="shipping_input" value="'+subtotalshipping+'" />';
}
function show_grand_total() {
	var subtotal,shipping,grand;
	subtotal=document.getElementById('subtotal_input').value;
	shipping=document.getElementById('shipping_input').value;
	//alert('sub:'+subtotal+', ship:'+shipping);
	grand=parseInt(subtotal)+parseInt(shipping);
	grand=grand.toFixed(2);
	document.getElementById('total').innerHTML='<span class="good_mini">$'+addCommas(grand)+'</span><input type="hidden" name="grandtotal" value="'+grand+'" />';;
}

