var adr_fields = new Array('name', 'address', 'address_2', 'city', 'state', 'zip', 'phone_1', 'phone_2', 'phone_3');

var ship_code = 'NONE';
var state = 'ANY';
var country = 'US';
var bcountry = 'US';
var coupon = 'NONE';
var qtd = -1;

var tcolor = 0;

var coupon_field;
var cto = 0;

function setShipInit(qfield, sfield)
{
	qtd = qfield.value;
	ship_code = sfield.value;
	calcTotal();
}//setShipInit

function setQtd(field)
{
	qtd = field.value;
	calcTotal();
}//setQtd

function setShipCode(field)
{
	ship_code = field.value;
	calcTotal();
}//setShipCode

function setState(field)
{
	state = field.value;
	calcTotal();
}//setState

function setCountry(field)
{
	if(field.value == country)return;

	var new_sel = document.getElementById('_state');
	var old_sel = document.getElementById('state');

	var new_sel_ship = document.getElementById('_ship_code');
	var old_sel_ship = document.getElementById('ship_code');

	new_sel.name = 'state';
	new_sel.id = 'state';
	new_sel_ship.name = 'ship_code';
	new_sel_ship.id = 'ship_code';

	old_sel.name = '_state';
	old_sel.id = '_state';
	old_sel_ship.name = '_ship_code';
	old_sel_ship.id = '_ship_code';

	old_sel.style['display'] = 'none';
	new_sel.style['display'] = 'block';
	new_sel_ship.style['display'] = 'block';
	old_sel_ship.style['display'] = 'none';

	country = field.value;

	setShipCode(new_sel_ship)

	$('bill_country').val($('#country').val());
}//setState

function setBillCountry(field)
{
	if(field.value == bcountry)return;

	var new_sel = document.getElementById('_bill_state');
	var old_sel = document.getElementById('bill_state');

	new_sel.name = 'bill_state';
	new_sel.id = 'bill_state';

	old_sel.name = '_bill_state';
	old_sel.id = '_bill_state';

	old_sel.style['display'] = 'none';
	new_sel.style['display'] = 'block';

	bcountry = field.value;
}//setState

function setCoupon(field, silent)
{
	clearTimeout(cto);
	coupon_field = field;

	if(silent != true)cto = setTimeout(submitCoupon, 500);
}//setCoupon

function submitCoupon()
{
	coupon = coupon_field.value;
	calcTotal();
}//submitCoupon

function calcTotal()
{
	if(document.getElementById('discount') != undefined)document.getElementById('discount').innerHTML = '...';
	if(document.getElementById('shipping') != undefined)document.getElementById('shipping').innerHTML = '...';
	if(document.getElementById('taxes') != undefined)document.getElementById('taxes').innerHTML = '...';

	if(document.getElementById('price_total') != null)document.getElementById('price_total').innerHTML = '...';
	if(document.getElementById('go') != null)document.getElementById('go').disabled = true;
	if(document.getElementById('gob') != null)document.getElementById('gob').disabled = true;

	callRPC('calcTotal', 'qtd=' + qtd + '|ship_code=' + ship_code + '|state=' + state + '|coupon=' + coupon, totalReceived);
}//calcTotal

function totalReceived(res)
{
	if(document.getElementById('shipping') != undefined)
	{
		if(res.shipping > 0)
		{
			document.getElementById('shipping').innerHTML = '$' + res.shipping;
		}//if shipping
		else document.getElementById('shipping').innerHTML = '<span style="color:red;margin:0;padding:0">FREE</span>';
	}//if shipping
	if(document.getElementById('taxes') != undefined)document.getElementById('taxes').innerHTML = '$' + res.taxes;
	if(document.getElementById('qtd') != undefined)
	{
		if(res.qtd != undefined)
		{
			if(res.extraQtd > 0)
			{
				document.getElementById('qtd').value = res.qtd - res.extraQtd;
				if(document.getElementById('discount') != null)
				{
					document.getElementById('discount').innerHTML = '+ ' + res.extraQtd + ' FREE';
					document.getElementById('discount_wraper').style['display'] = '';
					document.getElementById('discount').style['display'] = '';
					document.getElementById('discount_h').style['display'] = '';
				}
			}//if extraQtd
			else
			{
				document.getElementById('qtd').value = res.qtd;
				//document.getElementById('extraQtd').style.display = 'none';
			}//else normal
		}//if res
	}//if qtd

	if(document.getElementById('price_total') != null)document.getElementById('price_total').innerHTML = '$' + res.total;
	if(res.discount > 0 && !(res.extraQtd > 0))
	{
		if(document.getElementById('discount') != undefined)
		{
			document.getElementById('discount').innerHTML = '$' + res.discount;
			document.getElementById('discount').style['display'] = '';
                        document.getElementById('discount_wraper').style['display'] = '';
			document.getElementById('discount_h').style['display'] = '';
		}//if discount col
	}//if discount
	else if(!(res.extraQtd > 0))
	{
		if(document.getElementById('discount') != undefined)
		{
			document.getElementById('discount').style['display'] = 'none';
			document.getElementById('discount_h').style['display'] = 'none';
                        document.getElementById('discount_wraper').style['display'] = 'none';
		}//if discount col
	}//else ok

	document.getElementById('go').disabled = false;
	if(document.getElementById('gob') != null)document.getElementById('gob').disabled = false;
}//totalReceived

function copyAddress(chk)
{
	if(chk == null)chk = document.getElementById('bill_ad_same');

	if(country != bcountry)
	{
		document.getElementById('bill_country').value = document.getElementById('country').value;
		setBillCountry(document.getElementById('bill_country'));
	}//if !country

	if(chk.checked == true)
	{
		$('.ship').each(function(){
			if($('#bill_' + $(this).attr('name')).length > 0)
			{
				$('#bill_' + $(this).attr('name')).val($(this).val());
			}//if field
		});

		applyBillName();
	}//if checked
	else
	{
		$('#billAddress').slideDown();

		for(i in adr_fields)
		{
			document.getElementById('bill_' + adr_fields[i]).value = '';
		}//for i in adr_fields

		applyBillName();
	}//else clean up
}//copyAddress

var applyName = function()
{
	$('#name').val($('#fname').val() + ' ' + $('#lname').val());
	copyAddress(null);
};//applyName

var applyBillName = function()
{
	$('#bill_name').val($('#bill_fname').val() + ' ' + $('#bill_lname').val());
};//applyBillName

$(document).ready(function(){
	//$('.nameField').keydown(applyName);
	$('.bnameField').keydown(applyBillName);

	//$('.ship').keydown(function(){copyAddress(null);});
	//$('.ship').change(function(){copyAddress(null);});
});

