/*****************************************************************************************
	Author: Anil Naicker
	contact: anil@netmastery.com.au
	
	Script Purpose: Javascript base function include file.
	
	
	Last Modified: 11/05/2002 (dd/mm/yyyy)
	Modified By: Anil Naicker
	
*****************************************************************************************/

function openWindow( code )
{
	window.open("./bin/popup.php?code=" + code,"Product_popup","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=400,height=500");
}


function confirm_buy( form_obj )
{
	var item_name = form_obj['name[0]'].value;
	var qty = validate_qty(form_obj['qty[0]'].value);
	var price = form_obj['price[0]'].value;
	var units = form_obj['weight[0]'].value;
	var prod_code = form_obj['serial_number[0]'].value;
	
	if(units == 'l/m')
		qty_units = 'metres'
	else
		qty_units = '';
	
	if(qty <= 0){
		alert("The quantity for the item you are trying to order needs greater than zero.\nPlease check that you have entered the quantity correctly.");
		return false;
	}
	
	total = price * qty;
	
	return confirm("Do you wish to order: \n\n\t" + qty + " " + qty_units + " x (" + prod_code + ") " + item_name + "\n\tAt $" + currency_string(price) + " " + units + "\n\n\tTotal price of $" + currency_string(total));
}

function validate_qty(value){
	return (Math.round(value*100)/100);
}

function currency_format(num){
	return (Math.round(num*100)/100);
}

function currency_string(num){
	var neg_value = false;
	
	if(num < 0){
		num = -1 * num;
		neg_value = true;
	}
	
	if(num > 0){
		num = currency_format(num * 100);
		var snum = num.toString();
		var len = snum.length;
		var head = snum.substring(0, (len - 2))				
		if(head < 1)
			head = '0';
					
		if(neg_value)
			return '-' + head + "." + snum.substring((len-2), len);
		else
			return head + "." + snum.substring((len-2), len);
	}
		
	return '0.00';
}

