/*
Copyright(c) 2006 AD, Daniel Lear all Rights Reserved
lear@ephinity.com.au
*/

if(RUNSCRIPT)
{
	function FORM_BUTTONS(id_total,str_quant,str_add,str_sub,str_price,str_subtotal,bulk_point,bulk_price,max_order)
	{
		this.total_node = document.getElementById(id_total)
		this.str_quant = str_quant
		this.str_add = str_add
		this.str_sub = str_sub
		this.str_price = str_price
		this.str_subtotal = str_subtotal
		this.bulk_point = bulk_point
		this.bulk_price = bulk_price
		this.max_order = max_order

		this.timer = null
		this.old_input_value = ''
		this.active_input = 0

		this.addInstance()
		this.set_buttons()
		this.display(id_total+'_table')
		this.update_all_totals()
	}
	FORM_BUTTONS.prototype.set_buttons = function()
	{
		var i = 0,node_quant,node_add,node_sub,subtotal
		do{
			i++
			try
			{
				node_quant = document.getElementById(this.str_quant + i)
				if( typeof node_quant.value != 'string') break
				this.update_subtotal(i)
				node_add = document.getElementById(this.str_add + i)
				node_sub = document.getElementById(this.str_sub + i)
				node_add.style.display = 'block'
				node_sub.style.display = 'block'
				eval('node_add.onmousedown = function(){'+this.getInstanceName()+'.add('+i+')}')
				eval('node_sub.onmousedown = function(){'+this.getInstanceName()+'.sub('+i+')}')
				eval('node_quant.onfocus = function(){'+this.getInstanceName()+'.update_input('+i+')}')
				eval('node_quant.onblur = function(){'+this.getInstanceName()+'.deactivate_input('+i+')}')
			}
			catch(err)
			{
				//alert(err)
				break
			}
		}while(true)
	}
	FORM_BUTTONS.prototype.display = function(str_id)
	{
		var node = document.getElementById(str_id)
		node.style.display = 'block'
	}
	FORM_BUTTONS.prototype.update_all_totals = function()
	{
		var i = 0,price_item,total = 0
		do{
			i++
			try
			{
				price_item = document.getElementById(this.str_subtotal + i)
				if( typeof price_item.innerHTML != 'string') break
				total += this.dollars_to_float(price_item.innerHTML)
			}
			catch(err)
			{
				//alert(err)
				break
			}
		}while(true)
		this.total_node.innerHTML = this.format_dollars(total)
	}
	FORM_BUTTONS.prototype.update_subtotal = function(num)
	{
		var node_quant,quant,price,subtotal
	
		node_quant = document.getElementById(this.str_quant + num)
		if(node_quant.value == false) return false;
		quant = parseInt(node_quant.value)
		price = this.get_price(num,quant)

		subtotal = document.getElementById(this.str_subtotal + num)
		subtotal.innerHTML = this.format_dollars( this.round_cents(price*quant) )
	}
	FORM_BUTTONS.prototype.format_dollars = function(num)
	{
		num += ''
		if(num == 'null') return '$0.00'
		var r, dollar, cent
		r = /\d+/
		dollar = num.match(r)
		r = /\.+\d+/
		cent = num.match(r) + ''
		if(cent == 'null') cent = '.00'
		else if(cent.length == 2) cent += '0'
		return '$'+dollar+cent
	}
	FORM_BUTTONS.prototype.check_int = function(quant)
	{
		var r, number
		r = /\d+/
		number = quant.match(r)
		if(!number) number = 0
		return parseInt(number)
	}
	FORM_BUTTONS.prototype.round_cents = function(dollar)
	{
		return Math.floor(dollar*100 + 0.5)/100
	}
	FORM_BUTTONS.prototype.dollars_to_float = function(dollar)
	{
		var r = /\d+\.?\d?/
		return parseFloat( dollar.match(r) )
	}
	FORM_BUTTONS.prototype.get_price = function(num,quant)
	{
		var price,str_price,bulk_point,bulk_price
		str_price = document.getElementById(this.str_price + num).innerHTML
		r = /\d+\.?\d?/
		price = parseFloat( str_price.match(r) )

		bulk_price = document.getElementById(this.bulk_price + num).value
		if(bulk_price == '') return price

		bulk_point = document.getElementById(this.bulk_point + num).value
		if(quant>bulk_point)
		{
			price = bulk_price
			document.getElementById(this.str_price + num).innerHTML = '$'+price
		}
		else
		{
			price = document.getElementById('norm_price_' + num).value
			document.getElementById(this.str_price + num).innerHTML = '$'+price
		}
		return price
	}
	FORM_BUTTONS.prototype.add = function(num)
	{
		var node_quant,quant,price,subtotal,max_order

		node_quant = document.getElementById(this.str_quant + num)
		max_order =  document.getElementById(this.max_order + num).value
		if(max_order == node_quant.value) return this.message()
		if(node_quant.value == false) node_quant.value = 0
		quant = parseInt(node_quant.value) + 1
		node_quant.value = quant

		price = this.get_price(num,quant)

		subtotal = document.getElementById(this.str_subtotal + num)
		subtotal.innerHTML = this.format_dollars( this.round_cents(price*quant) )

		if(quant==0) node_quant.value = ''
		this.update_all_totals()
	}
	FORM_BUTTONS.prototype.sub = function(num)
	{
		var node_quant,quant,price,subtotal

		node_quant = document.getElementById(this.str_quant + num)
		if(node_quant.value == false) node_quant.value = 0
		quant = parseInt(node_quant.value) - 1
		if( quant < 0 ) return false;

		node_quant.value = quant			

		price = this.get_price(num,quant)

		subtotal = document.getElementById(this.str_subtotal + num)
		subtotal.innerHTML = this.format_dollars( this.round_cents(price*quant) )

		if(quant==0) node_quant.value = ''
		this.update_all_totals()
	}
	FORM_BUTTONS.prototype.value_onchange = function(num)
	{
		var node_quant,quant,price,subtotal,max_order

		node_quant = document.getElementById(this.str_quant + num)
		quant = this.check_int(node_quant.value)
		node_quant.value = quant

		max_order = parseInt( document.getElementById(this.max_order + num).value )
		if(quant > max_order)
		{
			node_quant.value = quant = parseInt(max_order)
			this.message()
		}

		price = this.get_price(num,quant)

		subtotal = document.getElementById(this.str_subtotal + num)
		subtotal.innerHTML = this.format_dollars( this.round_cents(price*quant) )

		if(quant==0) node_quant.value = ''
		this.update_all_totals()
	}
	FORM_BUTTONS.prototype.update_input = function(num)
	{
		clearTimeout(this.timer)

		var node_quant
		this.active_input = num
		node_quant = document.getElementById(this.str_quant + num)
		if(node_quant.value != this.old_input_value)
		{
			this.old_input_value = node_quant.value
			this.value_onchange(num)
		}
		this.timer = setTimeout(this.getInstanceName()+'.update_input('+num+')',300)
	}
	FORM_BUTTONS.prototype.deactivate_input = function(num)
	{
		if(this.active_input == num)
		{
			this.active_input = 0
			clearTimeout(this.timer)
		}
		this.value_onchange(num)
	}
	FORM_BUTTONS.prototype.message = function()
	{
		try
		{
			alert('You have reached the maximum quantity you can order on this shopping cart. Please contact Fullmoon Publications to order a larger amount, thank you.')
		}
		catch(err)
		{
			//alert(err)
		}
	}
}