// NCI JavaScript Functions

function check_tax()
{
	alert ("checking...");
	show_tax(document.main.tax_status.value);
}

function hide_tax()
{
	document.getElementById('tax_status_0').style.display = "none";	
	document.getElementById('tax_status_1').style.display = "none";	
	document.getElementById('tax_status_2').style.display = "none";	
}
function show_tax(wdiv)
{
	hide_tax();
	document.getElementById(wdiv).style.display = "block";
	
	if (wdiv != "tax_status_0") 
	{ 
		document.main.tax_perc.value = 0;
		changetax();
	}
	else
	{
		document.main.tax_perc.value = 7;
		changetax();
	}
}
function confirmReset()
{
	var agree = confirm("Are you sure you wish to reset the form completely?");
	if (agree == true) { return true; } else { return false; };
}
function confirmSubmit()
{
	var agree = confirm("Are you sure that all the information is correct?");
	if (agree == true) { return true; } else { return false; };
}

function checkcoupon() 
{
	var code = document.main.coupon.value;
	
    if (document.main.coupon.value=='WW326'||document.main.coupon.value=='ww326'||document.main.coupon.value=='NC442'||document.main.coupon.value=='nc442'||document.main.coupon.value=='fr525'||document.main.coupon.value=='FR525'||document.main.coupon.value=='AE425'||document.main.coupon.value=='ae425'||document.main.coupon.value=='AT475'||document.main.coupon.value=='at475'||document.main.coupon.value=='ak495'||document.main.coupon.value=='AK495'||document.main.coupon.value=='SW354'||document.main.coupon.value=='sw354'||document.main.coupon.value=='NP324'||document.main.coupon.value=='np324'||document.main.coupon.value=='PC334'||document.main.coupon.value=='pc334'||document.main.coupon.value=='NW344'||document.main.coupon.value=='nw344'||document.main.coupon.value=='DA364'||document.main.coupon.value=='da364'||document.main.coupon.value=='UA374'||document.main.coupon.value=='ua374'||document.main.coupon.value=='US384'||document.main.coupon.value=='us384'||document.main.coupon.value=='CA394'||document.main.coupon.value=='ca394'||document.main.coupon.value=='AM395'||document.main.coupon.value=='am395') 
    {
    	changetotal('1');
    	document.getElementById('coup').innerHTML = "<span class='red'>10% Off</span><input type='hidden' name='coupon_code' value='" + code + "' />";
    	disablefield('discount');
    	changetotal('1');
    }
    else
    {
    	alert("Coupon code is not valid!");
    	return false;
    }
}

function changetotal(field)
{
	var curr_total = document.main.total.value;
	var new_total = 0;
	
	
	totalfield = document.getElementById(field + '_total');
	quantfield = document.getElementById(field + '_quant');
	unitfield = document.getElementById(field + '_unit');
	
	if (document.getElementById(field + '_size').selectedIndex == "6") { sizeValue = 0.15; }
	else if (document.getElementById(field + '_size').selectedIndex == "7") { sizeValue = 0.30; }
	else if (document.getElementById(field + '_size').selectedIndex == "8") { sizeValue = 0.45; }
	else if (document.getElementById(field + '_size').selectedIndex == "9") { sizeValue = 0.60; }
	else { sizeValue = 0; };
	
	sizeValue = parseFloat(parseFloat(sizeValue) * parseFloat(quantfield.value)).toFixed(2);
	
	if (document.getElementById(field + '_back_att').selectedIndex == "2") { backAttValue = 0.10; }
	else if (document.getElementById(field + '_back_att').selectedIndex == "3") { backAttValue = 0.20; }
	else if (document.getElementById(field + '_back_att').selectedIndex == "4") { backAttValue = 0.60; }
	else { backAttValue = 0; };
	
	backAttValue = parseFloat(parseFloat(backAttValue) * parseFloat(quantfield.value)).toFixed(2);
	
	totalfield.value = parseFloat(parseFloat(quantfield.value) * parseFloat(unitfield.value)).toFixed(2);
	if (totalfield.value != "" || totalfield.value != "NaN") { newTotalField = parseFloat(parseFloat(totalfield.value) + parseFloat(sizeValue) + parseFloat(backAttValue)).toFixed(2); totalfield.value = newTotalField; };
	
	for (i=1; i<6; i++)
	{
		if (document.getElementById(i + '_total').value == "" || document.getElementById(i + '_total').value == "NaN") { document.getElementById(i + '_total').value = 0; };
		new_total += parseFloat(document.getElementById(i + '_total').value);
	};
	
	document.main.subtotal.value = new_total.toFixed(2);
	changetax();
	if (document.main.discount.disabled == true) { new_total -= new_total * (10/100); };
	if (document.main.air_shipping.value == "") { document.main.air_shipping.value = 0; } else { new_total = new_total + parseFloat(document.main.air_shipping.value); };
	if (document.main.rush_charge.value == "") { document.main.rush_charge.value = 0; } else { new_total = new_total + parseFloat(document.main.rush_charge.value); };
	if (document.main.tax_perc.value != "0" || document.main.tax_perc.value != "") { document.main.tax_amount.value = (new_total * (document.main.tax_perc.value/100)).toFixed(2); new_total += new_total * (document.main.tax_perc.value/100); };
	
	var rtotal = Math.round(new_total*100)/100;
	document.main.total.value = rtotal.toFixed(2);
} 

function changetax()
{
	tax_perc = document.main.tax_perc.value / 100;
	new_tax = tax_perc * document.main.subtotal.value;
	
	document.main.tax_amount.value = Math.round(new_tax*100)/100;
}


function disablefield(field)
{
	document.getElementById(field).disabled = true;
}

function enablefield(field)
{
	document.getElementById(field).disabled = false;
}