// WebWiz Merlin Stock Check - Version 9.9.999

var timer,msg_waiting,msg_instock,msg_outofstock,msg_timeout,checkpartqty,price_timer,config_account,config_stockid,config_company,config_quantity,returned_price,msg_waiting_price,msg_return_price,msg_timeout_price;
var packquantities;
var partcodes;

function responseAJAX(){   
	if(this.readyState==4){
		var strData=this.responseText;
		var arrData=strData.split('\r\n');

		for(var n in arrData){
			if(arrData[n]!=''){
				arrPair=arrData[n].split('\t');
				var fieldid='livestock-'+arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.');
				
				if(arrPair.length==3){
					if(arrPair[1]>0){

						//do we have pack quantities?
						if(checkpartqty!=0&&packquantities.length>0) {
									
							//this is incase we have multiple parts
							//find the relevant one, and match up it's pack qty
							for(var x in packquantities) {
								//if the part code matches, the divide the packqty by the unit qty
								//to get the number of "boxes" available
								
								if(partcodes[x]==arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.')) {
									var actualStock=(parseFloat(arrPair[1])/parseFloat(packquantities[x]));
									actualStock=Math.floor(actualStock);
								}
							}
							
						} else {
							var actualStock=arrPair[1];
						}
					
						if(actualStock>=1) {
						
							$('#'+fieldid).html(msg_instock.replace('<qty>',actualStock));
						} else {
						
							$('#'+fieldid).html(msg_outofstock.replace('<qty>',arrPair[2]))
						}
					}else{
						
						$('#'+fieldid).html(msg_outofstock.replace('<qty>',arrPair[2]));
					}
				}
			}
		}
		
		this.abort();
		window.clearTimeout(timer);
		timer=window.setTimeout('getStock();',60000)
	}
}

function responseSingleStockAJAX(){   
	if(this.readyState==4){
		var strData=this.responseText;
		var arrData=strData.split('\r\n');

		for(var n in arrData){
			if(arrData[n]!=''){
				arrPair=arrData[n].split('\t');
				var fieldid='livestock-'+arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.');
				
				if(arrPair.length==3){
					if(arrPair[1]>0){

						//do we have pack quantities?
						if(checkpartqty!=0&&packquantities.length>0) {
									
							//this is incase we have multiple parts
							//find the relevant one, and match up it's pack qty
							for(var x in packquantities) {
								//if the part code matches, the divide the packqty by the unit qty
								//to get the number of "boxes" available
								
								if(partcodes[x]==arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.')) {
									var actualStock=(parseFloat(arrPair[1])/parseFloat(packquantities[x]));
									actualStock=Math.floor(actualStock);
								}
							}
							
						} else {
							var actualStock=arrPair[1];
						}
					
						if(actualStock>=1) {
						
							$('#'+fieldid).html(msg_instock.replace('<qty>',actualStock));
						} else {
						
							$('#'+fieldid).html(msg_outofstock.replace('<qty>',arrPair[2]))
						}
					}else{
						
						$('#'+fieldid).html(msg_outofstock.replace('<qty>',arrPair[2]));
					}
				}
			}
		}
		
		this.abort();
		window.clearTimeout(timer);
		timer=window.setTimeout('getSingleStock();',60000)
	}
}

function responsePriceAJAX() {
	if(this.readyState==4){
	
		var strData=this.responseText;
		var arrData=strData.split('\r\n');

		for(var n in arrData){
			if(arrData[n]!=''){
				arrPair=arrData[n].split('\t');
				var fieldid='liveprice-'+arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.');
				var fielduoi='uoi-'+arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.');
				var fieldpackqty='packqty-'+arrPair[0].replace(/\s/g,'_').replace(/\./g,'\\.');
				
				var uoi = document.getElementById(fielduoi).value;
				var packqty=document.getElementById(fieldpackqty).value;
				
				var dbluoi = parseFloat(uoi);
				var price=parseFloat(arrPair[1]);
				
				$('#uoiprice').val(price);
				
				price = price/dbluoi;
				var strPrice=price;
				
				price = price*packqty;
				var strPrice=price.toFixed(2);
				
				$('#price').val(strPrice);
				$('#'+fieldid).html(msg_return_price.replace('<price>',strPrice));
				
				
			}
		}
		
		this.abort();
		window.clearTimeout(price_timer);
		price_timer=window.setTimeout('getPrice();',60000)
			
	}   

}

function stockTimeout(){
	$('.livestock').each(function(){
		$(this).html(msg_timeout);
	});
	
	window.clearTimeout(timer);
	timer=window.setTimeout('getStock();',10000)
}

function getStock(waiting,instock,outofstock,timeout,checkpartquantity){
	//'<img> Checking Stock','<qty> in Stock','Out of Stock','Live Stock Unavailable'
	
	if(waiting){
		msg_waiting=waiting;
		msg_instock=instock;
		msg_outofstock=outofstock;
		msg_timeout=timeout;
		if(checkpartquantity=='1') {
			checkpartqty=1;
		} else {
			checkpartqty=0;
		}
	}
	
	packquantities=new Array();
	partcodes=new Array();

	$('.livestock').each(function(){
		partcodes[partcodes.length]=$(this).attr('id').substring(10).replace(/_/g,' ');
		$(this).html(msg_waiting.replace('<img>','<img src="/images/ajax-loader.gif" />'));
	});
	
	$('.packquantity').each(function(){
		packquantities[packquantities.length]=$(this).attr('id').substring(13).replace(/_/g,' ');
	});
	
	if(waiting){
		//window.clearTimeout(timer);
		timer=window.setTimeout('getStock();',2000)
	}else{
		timer=window.setTimeout('stockTimeout();',10000)
		
		var http=createAJAX();

		if(http.readyState>0){http.abort();}
		http.onreadystatechange=responseAJAX;
		http.open('get', 'http://' + window.location.hostname + '/stockchecker.asp?partcodes='+partcodes.join(),true);
		//alert('http://' + window.location.hostname + '/stockchecker.asp?partcodes='+partcodes.join());
		http.send(null);
	}
}

function singleStockTimeout(){
	$('.livestock').each(function(){
		$(this).html(msg_timeout);
	});
	
	window.clearTimeout(timer);
	timer=window.setTimeout('getSingleStock();',10000)
}


function getSingleStock(waiting,instock,outofstock,timeout,checkpartquantity,account,company,quantity){
	//'<img> Checking Stock','<qty> in Stock','Out of Stock','Live Stock Unavailable'
	
	if(waiting){
		msg_waiting=waiting;
		msg_instock=instock;
		msg_outofstock=outofstock;
		msg_timeout=timeout;
		if(checkpartquantity=='1') {
			checkpartqty=1;
		} else {
			checkpartqty=0;
		}
	}
	
	packquantities=new Array();
	partcodes=new Array();

	$('.livestock').each(function(){
		partcodes[partcodes.length]=$(this).attr('id').substring(10).replace(/_/g,' ');
		$(this).html(msg_waiting.replace('<img>','<img src="/images/ajax-loader.gif" />'));
	});
	
	$('.packquantity').each(function(){
		packquantities[packquantities.length]=$(this).attr('id').substring(13).replace(/_/g,' ');
	});
	
	timer=window.setTimeout('singleStockTimeout();',10000)
	
	var http=createAJAX();
	
	if(http.readyState>0){http.abort();}
	http.onreadystatechange=responseSingleStockAJAX;
	http.open('get', 'http://' + window.location.hostname + '/stockchecker.asp?action=stockcheck&account=' + config_account + '&company=' + config_company + '&quantity=' + config_quantity  + '&stockid=' + config_stockid);
	http.send(null);
}


function priceTimeout(){
	$('.liveprice').each(function(){
		$(this).html(msg_timeout_price);
	});
	
	window.clearTimeout(price_timer);
	price_timer=window.setTimeout('getPrice();',12000)
}

function getPrice(waiting,returnprice,timeout,account,company,quantity) {
	
	if(waiting){
		msg_waiting_price=waiting;
		msg_timeout_price=timeout;
		msg_return_price=returnprice;
		config_account=account;
		config_company=company;
		config_quantity=quantity;
	}
	

	var stockcode;
	
	$('.liveprice').each(function(){
		config_stockid=$(this).attr('id').substring(10).replace(/_/g,' ');
		$(this).html(msg_waiting_price.replace('<img>','<img src="/images/ajax-loader.gif" />'));
	});
	
	//alert(stockcode);
	
	price_timer=window.setTimeout('priceTimeout();',12000)
	
	var http=createAJAX();
	
	if(http.readyState>0){http.abort();}
	http.onreadystatechange=responsePriceAJAX;
	http.open('get', 'http://' + window.location.hostname + '/stockchecker.asp?action=pricecheck&account=' + config_account + '&company=' + config_company + '&quantity=' + config_quantity  + '&stockid=' + config_stockid);
	//alert('http://' + window.location.hostname + '/stockchecker.asp?action=pricecheck&account=' + config_account + '&company=' + config_company + '&quantity=' + config_quantity  + '&stockid=' + config_stockid);
	http.send(null);
}

function getUpdatePrice(quantity) {
	
	if(quantity){
		config_quantity=quantity;
	}
	
	var stockcode;
	
	$('.liveprice').each(function(){
		stockcode=$(this).attr('id').substring(10).replace(/_/g,' ');
		$(this).html(msg_waiting.replace('<img>','<img src="/images/ajax-loader.gif" />'));
	});
	
	//alert(stockcode);
	
	price_timer=window.setTimeout('priceTimeout();',10000)
	
	var http=createAJAX();
	
	if(http.readyState>0){http.abort();}
	http.onreadystatechange=responsePriceAJAX;
	http.open('get', 'http://' + window.location.hostname + '/stockchecker.asp?action=pricecheck&account=' + config_account + '&company=' + config_company + '&quantity=' + config_quantity  + '&stockid=' + config_stockid);
	http.send(null);
}
