var first_run=0;

function update_offer_message(message)
{
$('offer_message').innerHTML=''+message+'';

}

function update_offer(item)
{
offer_value=($('offer_code').value);
//if (offer_value.length==0) return;
url='/Ajax/get_offer_code.php?id='+offer_value;
new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
	var json = transport.responseText.evalJSON();
	if (json[0]=='nodata') {
		window.location.href=window.location.href
		update_offer_message("Invalid Offer Code");
		return(false);
		}
	window.location.href=window.location.href
  },
  onFailure: function(transport) {
	update_offer_message("Invalid Offer Code");
  }
});


}

function limit_chest()
{
if ($('QTY_CHEST')) {
c_value=$('QTY_CHEST').value;
if (c_value>3) $('QTY_CHEST').value=3;
}
}

function check_form(id)
{
limit_chest();
if (first_run==0) {
	alert("Please Select a Shipping State");
	return(false);
	}
select_state($('state'));

if (first_run==2) {
	alert("Please Call for a Shipping Quote.  (800) 891-1674");
	return(false);
	}
return(true);
}


function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function s_chest(chest_name,cname)
{
	$('chest_image').src='/images/cartimages/'+chest_name+'.jpg';
	$('chest_name').innerHTML=cname;
	$('CHEST_COLOR_SEL').value=cname;
	if (cname=="Custom Color") {
		$('QTY_CUSTCOLOR').checked=true;
		$('QTY_CUSTCOLOR').enable();
	}
	else {
		$('QTY_CUSTCOLOR').checked=false;
		$('QTY_CUSTCOLOR').disable();
	}
	return(false);
}

function select_state(id)
{
if (first_run==0) {
	id.remove(0);
	first_run=1;
	}
// TAX GEORGIA 8%
state=(($(id).value));

// GET SHIPPING VALUE HERE

load_shipping(state)
return;


}

function add_cart(id)
{
var cval;
cval=parseInt(($(id).value));
if (isNaN(cval)) cval=0;
cval=cval+1;
$(id).value=cval;
limit_chest();
}

function del_cart(id)
{
var cval;
cval=parseInt(($(id).value));
if (isNaN(cval)) cval=1;
if (cval>0) cval=cval-1;
$(id).value=cval;
}

function checkvalue(id)
{
id=id.name;
var cval;
cval=parseInt(($(id).value));
if (isNaN(cval)) cval=0;
$(id).value=cval;
limit_chest();
}


function load_shipping(load_state)
{
var url

count=0;
if ($('QTY_CHEST')) {
	count=parseInt($('QTY_CHEST').value);
	}
if (isNaN(count)) count=0;

url="/Ajax/shipping.php"
new Ajax.Request(url, {
  method: 'get',
  parameters: {state: load_state},
  onSuccess: function(transport) {
		var json = transport.responseText.evalJSON();
		if (json[count+2]) {
			price=parseFloat((json[count+2]));
			if (price>0){
			price=price-json[0];
			if (price<0) price=0;
				}
			}
		
		if (price==-1) {
			first_run=2;
			$('ship_value').innerHTML='Call for Quote';
			//$('tt_total').innerHTML='Call for Quote';
			price=0;
			}
			else
			{
			first_run=1;
			$('ship_value').innerHTML='$'+CurrencyFormatted(price);
			//$('final_value').innerHTML=$('store_value').value;
			}

// SHIP VALUE
var ship_value;
ship_value=price;
sales_tax=0;

current_value=parseFloat(($('cart_total').innerHTML).substring(1));

if (load_state=='GA') {
        sales_tax=current_value*.08;
        }
if (load_state=='AT') {
        sales_tax=current_value*.08;
        }

new_value=parseFloat(ship_value)+parseFloat(current_value)+parseFloat(sales_tax);
// ship_value total_value
$('final_value').innerHTML='$'+CurrencyFormatted(new_value);
$('tax_value').innerHTML='$'+CurrencyFormatted(sales_tax);


$('cc_tax').value=CurrencyFormatted(sales_tax);
$('cc_ship').value=CurrencyFormatted(ship_value);
$('cc_total').value=CurrencyFormatted(new_value);



        }


});



}
