// Version 0.1.0 IN PROGRESS DEVELOPMENT
// Date: 21 Feb 2005
// Author: Yanni Wang
// Company: Brain Juice

//========================================================

// centralise URL
function GetKMAXHost()
{
	//return "http://www.kawasaki.net.au";
	return "http://localhost/sp/asp/";
}

//add an order to cookie

function AddToOrder(sOrder,partNo,Desc,backOrder,quantity,price,discPrice,prodClass,packageQty,stock)
{
  orderString = partNo + "|" + Desc + "|" + backOrder + "|" + quantity + "|" + price + "|" + discPrice + "|" + prodClass + "|" + packageQty + "|" + stock ;
  if(CheckOrder(sOrder,partNo)){
  	if(sOrder != ""){ 
  		sOrder = sOrder + ";" + orderString ;
  	}
	else
	{
		sOrder = orderString;
	} 
  	return sOrder; 
  } 
  return null;
	
}

//check if the part has been ordered already
function CheckOrder(sOrder,partNo){
	if(sOrder != ""){
		orders = sOrder.split(";");
		for(i=0;i<orders.length;i++){
			orderItem = orders[i].split("|");
			if(orderItem[0] == partNo){
				alert("This part is already in the order");
				return false;
			}
		}
	}
	return true;
}

//delete an order from cookie
function DeleteOrder(sOrder,partNo){
	sTemp ="";
	if(sOrder != null){
		orders = sOrder.split(";");
		for(i=0;i<orders.length;i++){
			orderItem = orders[i].split("|");
			if(orderItem[0] != partNo){
				if(sTemp !="")
					sTemp = sTemp +";" + orders[i];
				else
					sTemp = orders[i];
			}
		}
		return sTemp;
	}
	return null;
}

//modify orders
function ModifyOrder(sOrder, partNo, backorder, qty)
{
	sTemp ="";
	if(sOrder != null)
	{
		orders = sOrder.split(";");
		for(i=0;i<orders.length;i++)
		{
			if(sTemp != "")
				sTemp =  sTemp + ";";
			orderItem = orders[i].split("|");
			if(orderItem[0] == partNo)
			{
				newQty = checkPartStock(partNo,qty,backorder,orderItem[7],orderItem[8]);
				if(newQty > -1)
				{
					orderItem[2] = backorder;
					orderItem[3] = parseInt(newQty);
				}
				sTemp = sTemp + orderItem.join('|');
			}	
			else
				sTemp = sTemp + orders[i];
		}
		return sTemp;
	}	
	return null;
}

function checkPartStock(partNo,partQuantity,backorderOpt,packageQuantity,partStock)
{	
		partStock = parseInt(partStock);
		packageQuantity = parseInt(packageQuantity);
		partQuantity = parseInt(partQuantity);
		if(partQuantity < packageQuantity){
			partQuantity = packageQuantity;
		}
		else if (mod(partQuantity,packageQuantity) != 0)
		{
			partQuantity = partQuantity - mod(partQuantity,packageQuantity) + packageQuantity;
		}
		if(backorderOpt == "Yes"){
			return partQuantity;
		}
		else
		{
			if(partStock == 0){
				alert("there's not enough stock to order");
				return -1;
			}
			if(partStock < packageQuantity){
				alert("there's not enough stock to order");
				return -1;
			}
			if(partQuantity <= partStock){					
				return partQuantity;
			}
			else
			{
				partQuantity = partStock;
				
				if (mod(partQuantity,packageQuantity) > 0){
					partQuantity = parseInt(partQuantity) - mod(partQuantity,packageQuantity); 
				}
				alert("The available stock of part " + partNo+" is " + partQuantity +".\n" + partQuantity + " items were added to the order.")
				return partQuantity;
			}
		
		}
	return -1;
}

//return the number of orders
function getItemCount(sOrder){
	sOrder = sOrder.replace("&apos;","'");
	if(sOrder.length > 0){
		orders = sOrder.split(";");
		return orders.length;
	}
	return 0;
}

//========================================================

function calcDiscountPrice(sPrice, sProdClass, sOrderType)
{
	// calculate the discount price based on the passed list price, product class and order type
	var fDisc = 0, fPartPrice, sDiscountPrice;

	// get rid of commas in string price
	sPrice = replaceAll(sPrice,",","");

	if (sProdClass == "R" || sProdClass == "P" || sProdClass == "T")
	{
		fDisc = 20.00;
	}
	else if(sProdClass == "A")
	{
		fDisc = 30.00;
	}
	else
	{
		if(sOrderType == "D")
		{
			// daily order
			fDisc = 33.33;
		}
		else
		{
			// must be a stock order
			fDisc = 40.00;
		}
	}

	// calc price, then turn it back into a string
	fPartPrice = sPrice - (sPrice * fDisc / 100);
	sDiscountPrice = fmtF(fPartPrice, 13, 2);

	return sDiscountPrice;
}

//format a number, w is number of digits, d is number of decimals
function fmtF(number,w,d)
{
	var width=w;
	var dpls=d;
	var lt1=false;
	var len=number.toString().length;
	var junk;
	var res="";

	// First check for valid format request
	if ( width < (dpls+2))
	{
		window.alert(	"Illegal format specified : w = " + d +
						" w = " + d +
						"\nUsage: [ToFmt].fmtF(w,d)" +
						"\nWidth (w) of field must be greater or equal to the number " +
						"\nof digits to the right of the decimal point (d) + 2");
		junk = filljunk(width);
		return junk;
	}

	// Work with absolute value
	var absx=Math.abs(number);
	// Nasty fix to deal with numbers < 1 and problems with leading zeros!
	if ((absx < 1) && (absx > 0))
	{
  		lt1 = true;
  		absx+=10;
 	}

	// Get postion of decimal point
	var pt_pos = absx.toString().indexOf(".");
	if ( pt_pos == -1)
	{
		res+= absx;
		res+= ".";
		for (var i = 0; i < dpls; i++)
		{
			res += 0;
		}  
	}
	else
	{
		res = Math.round(absx * Math.pow(10,dpls));
		res=res.toString();
		if (res.length == Math.round(Math.floor(absx * Math.pow(10,dpls))).toString().length)
		{ 
			res = res.substring(0,pt_pos) + "." + res.substring(pt_pos,res.length);
  		}
  		else
		{
   			pt_pos++;
			res = res.substring(0,pt_pos) + "." + res.substring(pt_pos,res.length);
		} 

		// Remove leading 1 from  numbers < 1 (Nasty fix!)
		if (lt1) 
		{
			res=res.substring(1,res.length);
		}
	}

	// Final formatting statements
	// Reinsert - sign for negative numbers
	if (number < 0)
		res = "-"+res;
	// Check whether the result fits in the width of the field specified
 	if (res.length > width)
 	{
		res=filljunk(width);
 	}
	// If necessary, pad from the left with the spacer string
	else if (res.length < width)
	{
		var res_bl="";
		for (var i = 0; i < (width - res.length); i++)
		{
			res_bl += " " ;
		} 
		res = res_bl + res;
	}
	return res;
}

function filljunk(lenf)
{
	// Fills field of length lenf with asterisks
	var str="";
	for (var i=0; i < lenf; i++)
	{
		str +="*";
 	}
	return str;
}

function setSpacer(spc)
{
	var spc;
	this.spacer=spc;
	return this.spacer;
}

function cancelLink ()
{
	return false;
}

function disableLink (link)
{
	if (link.onclick)
		link.oldOnClick = link.onclick;
	link.onclick = cancelLink;
	if (link.style)
		link.style.cursor = 'default';
}

function enableLink (link) 
{
	link.onclick = link.oldOnClick ? link.oldOnClick : null;
	if (link.style)
		link.style.cursor = document.all ? 'hand' : 'pointer';
}

function toggleLink (link)
{
	if (link.disabled) 
		enableLink (link);
	else 
		disableLink (link);
	link.disabled = !link.disabled;
}

function mod(a,b)
{
	return a-Math.floor(a/b)*b ;
}

function replaceAll(str,from,to)
{
	var idx = str.indexOf( from );
	
	while ( idx > -1 )
	{
		str = str.replace( from, to ); 
		idx = str.indexOf( from );
    }

    return str;
}

//========================================================

function IsObject( obj )
{
	/*
	    Tests if argument is an object or not
	*/
	return (typeof( obj ) == 'object' && obj != null);
}

//========================================================

function IsFunction( obj )
{
	/*
	    Tests if argument is a function or not
	*/
	return (typeof( obj ) == 'function');
}

//========================================================
