// JavaScript Document

function set_var(sail) {

	shade_type = document.shadesailquoteForm.shade_type.value;
	subtotal = document.getElementById("subtotal");
	stateVar = document.getElementById("state");
	shipping_price = 31.95;
	grand_total = document.getElementById("grand_total");
	shipping = document.getElementById("shipping");
	zipVar = document.getElementById("zip"); 

	if (shade_type == "commercial") {
		foot_price = 25;
		corner_price = 45;
	} else {
		foot_price = 18;
		corner_price = 40;
	}
	
	if (sail==1) {
		shade_measurement = document.getElementById("shade1_measurement");
		measurement_unit = document.getElementById("measurement_unit1");
		number_corners = document.getElementById("number_corners1");
		shade_color = document.getElementById("shade_color1");	
		price_each_field = document.getElementById("shade1_subtotal");
		shippingVar = shipping_price;
	}
	if (sail==2) {
		shade_measurement = document.getElementById("shade2_measurement");
		measurement_unit = document.getElementById("measurement_unit2");
		number_corners = document.getElementById("number_corners2");
		shade_color = document.getElementById("shade_color2");	
		price_each_field = document.getElementById("shade2_subtotal");
		shippingVar = shipping_price * 2;
	}
	if (sail==3) {
		shade_measurement = document.getElementById("shade3_measurement");
		measurement_unit = document.getElementById("measurement_unit3");
		number_corners = document.getElementById("number_corners3");
		shade_color = document.getElementById("shade_color3");	
		price_each_field = document.getElementById("shade3_subtotal");
		shippingVar = shipping_price * 3;
	}
	if (sail==4) {
		shade_measurement = document.getElementById("shade4_measurement");
		measurement_unit = document.getElementById("measurement_unit4");
		number_corners = document.getElementById("number_corners4");
		shade_color = document.getElementById("shade_color4");	
		price_each_field = document.getElementById("shade4_subtotal");
		shippingVar = shipping_price * 4;
	}
	if (sail==5) {
		shade_measurement = document.getElementById("shade5_measurement");
		measurement_unit = document.getElementById("measurement_unit5");
		number_corners = document.getElementById("number_corners5");
		shade_color = document.getElementById("shade_color5");	
		price_each_field = document.getElementById("shade5_subtotal");
		shippingVar = shipping_price * 5;
	}
}

function check(sail) {

	set_var(sail);

	if (shade_type == "select") {
		alert ("Please select the sail type.\n\n - Commercial Quality: \nMade with Stainless steel cabled perimeter and extra reinforcement. Made to fit the space with no adjustment needed. Most of the sails seen on this site were made with are these. \n\n - Ready-Made Quality: \nLimited to a maximum of 325 square feet and are not as strong as Commercial Quality. Usually made a bit smaller with chain added to the corners to allow for stretch.");
		document.shadesailquoteForm.shade_type.focus();
		return false;

	} else {

		if ((shade_measurement.value == "") || !(Number(shade_measurement.value))) {
			alert ("Please enter the total measurement of the sail. \nOnly numbers, no letters or symbols, ie. 300");
			shade_measurement.focus();
			return false;
		} else {
			if (measurement_unit.value == "select") {
				alert ("Please select the measurement unit you used to measure the total size of the sail. (inches or feet)");
				measurement_unit.focus();
				return false;
			}  else {
				if ((number_corners.value == "") || (number_corners.value < 3) || !(Number(number_corners.value))) {
					alert ("Please enter the numbers of corners this sail will have. For example, if triangular shaped, the total number of corners is 3, if square, it is 4. For custom shapes simply enter the desired number of corners. (the number must be higher than 3)");
					number_corners.focus();
					return false;
				} else {
					updateAll();
				}	
			}
		}
	}
	if (shade_color.value == "select" || shade_color.value == "") {
	shade_color.focus();







	alert("Please select the sail color.");
	return false;
	} 

}

function printValues(sail) {




	if (shade_measurement.value > 85 && measurement_unit.value == "feet" || shade_measurement.value > 1000 && measurement_unit.value == "inches" ) {
		shade_subtotal = eval(shade_subtotal) + eval(shade_subtotal * 0.1);
	}
	price_each_field.value = shade_subtotal.toFixed(2);
	if (sail==1) {
		subtotal.value = eval(price_each_field.value);
	}
	if (sail==2) {
		subtotal.value = eval(document.getElementById("shade1_subtotal").value) + eval(price_each_field.value);
	}
	if (sail==3) {
		subtotal.value = eval(document.getElementById("shade1_subtotal").value) + eval(document.getElementById("shade2_subtotal").value) + eval(price_each_field.value);
	}
	if (sail==4) {
		subtotal.value = eval(document.getElementById("shade1_subtotal").value) + eval(document.getElementById("shade2_subtotal").value) + eval(document.getElementById("shade3_subtotal").value) + eval(price_each_field.value);
	}
	if (sail==5) {
		subtotal.value = eval(document.getElementById("shade1_subtotal").value) + eval(document.getElementById("shade2_subtotal").value) + eval(document.getElementById("shade3_subtotal").value) + eval(document.getElementById("shade4_subtotal").value) + eval(price_each_field.value);
	}
	subtotalVar = eval(subtotal.value);
	subtotal.value = subtotalVar.toFixed(2);
	shipping.value = shippingVar.toFixed(2);
	if ( stateVar.value == "CA" ) {
		document.getElementById("sales_Tax").style.display =  "block";
		taxVar = subtotalVar * 0.0825;
		document.getElementById("tax").value =  taxVar.toFixed(2);	
		finalPrice = eval(shippingVar) + eval(subtotalVar) + eval(document.getElementById("tax").value);
	} else {
		finalPrice = eval(shippingVar) + eval(subtotalVar);
		document.getElementById("sales_Tax").style.display =  "none";
		document.getElementById("tax").value =  0;
	}
	grand_total.value =  finalPrice.toFixed(2);
}

function calculate(sail) {
		
	if (shade_measurement.value > 0 && shade_measurement.value != "" && number_corners.value > 2 && number_corners.value != 0 && shade_type != "select" && measurement_unit.value != "select" ) {
		if (measurement_unit.value == "inches") {
			shade_subtotal = ((shade_measurement.value/12) * foot_price) + (number_corners.value * corner_price);
			printValues(sail);
		} else {
			shade_subtotal = (shade_measurement.value * foot_price) + (number_corners.value * corner_price);
			printValues(sail);
		}
		
	} else {
		price_each_field.value = "";
		subtotal.value = "0.00";
		grand_total.value = "0.00";
		shipping.value = "0.00";
	}
}

function updateAll() {
	set_var(1); 
	calculate(1);
	if (document.getElementById("shade2").style.display == "block") {
		set_var(2); 
		calculate(2); 
	}
	if (document.getElementById("shade3").style.display == "block") {
		set_var(3); 
		calculate(3); 
	}
	if (document.getElementById("shade4").style.display == "block") {
		set_var(4); 
		calculate(4); 
	}
	if (document.getElementById("shade5").style.display == "block") {
		set_var(5); 
		calculate(5);
	}
}

function addsail(sail) {
	if (sail == 2) {
		document.getElementById("shade2").style.display = "block";
		document.getElementById("calculate-shade1").style.display = "none";
	}
	if (sail == 3) {
		document.getElementById("shade3").style.display = "block";
		document.getElementById("calculate-shade2").style.display = "none";
	}
	if (sail == 4) {
		document.getElementById("shade4").style.display = "block";
		document.getElementById("calculate-shade3").style.display = "none";
	}
	if (sail == 5) {
		document.getElementById("shade5").style.display = "block";
		document.getElementById("calculate-shade4").style.display = "none";
	}
	if (sail == 6) {
		document.getElementById("tips_body").style.height = "auto";
		document.getElementById("control-1").style.display = "none";
		document.getElementById("control-2").style.display = "block";
	}
	if (sail == 7) {
		document.getElementById("tips_body").style.height = "80px";
		document.getElementById("control-1").style.display = "block";
		document.getElementById("control-2").style.display = "none";
	}
}

function removesail(sail) {
	if (sail == 2) {
		document.getElementById("shade2").style.display = "none";
		document.getElementById("calculate-shade1").style.display = "block";
		document.getElementById("shade2_measurement").value = "";
		document.getElementById("number_corners2").value = "";		
		document.getElementById("shade2_subtotal").value = "";		
		updateAll();
	}
	if (sail == 3) {
		document.getElementById("shade3").style.display = "none";
		document.getElementById("calculate-shade2").style.display = "block";
		document.getElementById("shade3_measurement").value = "";
		document.getElementById("number_corners3").value = "";		
		document.getElementById("shade3_subtotal").value = "";		
		updateAll();
	}
	if (sail == 4) {
		document.getElementById("shade4").style.display = "none";
		document.getElementById("calculate-shade3").style.display = "block";
		document.getElementById("shade4_measurement").value = "";
		document.getElementById("number_corners4").value = "";		
		document.getElementById("shade4_subtotal").value = "";		
		updateAll();
	}
	if (sail == 5) {
		document.getElementById("shade5").style.display = "none";
		document.getElementById("calculate-shade4").style.display = "block";
		document.getElementById("shade5_measurement").value = "";
		document.getElementById("number_corners5").value = "";		
		document.getElementById("shade5_subtotal").value = "";		
		updateAll();
	}
}

function checkForm(thisform) {
	with (thisform)
	{
		updateAll();
		if (check(1) == false ) {
				return false;
		}
		if (document.getElementById("shade2").style.display == "block") {
			if (check(2) == false ) {
				return false;
			} else if (document.getElementById("shade3").style.display == "block") {
				if (check(3) == false ) {
					return false;
				} else if (document.getElementById("shade4").style.display == "block") {
					if (check(4) == false ) {
						return false;
					} else if (document.getElementById("shade5").style.display == "block") {
						if (check(5) == false ) {
							return false;
						} else {
							return true;
						}							
					}
				}
			}
		}
		if (zipVar.value =="zip code" || zipVar.value == "") {
			zipVar.focus();
			alert("Please enter your zip code.");
			return false;
		} 
		if (stateVar.value == "select") {
			stateVar.focus();
			alert("Please select your state.");
			return false;
		} else {
			return true;
		}		
	}
}

function copyValues() {
	
	sameVar = document.getElementById("same");
	company_billingVar = document.getElementById("company_billing");
	firstname_billingVar = document.getElementById("firstname_billing");
	lastname_billingVar = document.getElementById("lastname_billing");
	address_billingVar = document.getElementById("address_billing");
	country_billingVar = document.getElementById("country_billing");
	city_billingVar = document.getElementById("city_billing");
	state_billingVar = document.getElementById("state_billing");
	zip_billingVar = document.getElementById("zip_billing");
	email_billingVar = document.getElementById("email_billing");
	phone_billingVar = document.getElementById("phone_billing");
	
	if (sameVar.checked) {
		company_billingVar.value = document.getElementById("company").value;
		firstname_billingVar.value = document.getElementById("firstname").value;
		lastname_billingVar.value = document.getElementById("lastname").value;
		address_billingVar.value = document.getElementById("address").value;
		country_billingVar.value = document.getElementById("country").value;
		city_billingVar.value = document.getElementById("city").value;
		state_billingVar.value = document.getElementById("state").value;
		zip_billingVar.value = document.getElementById("zip").value;
		email_billingVar.value = document.getElementById("email").value;
		phone_billingVar.value = document.getElementById("phone").value;
/*		company_billingVar.disabled= true;
		firstname_billingVar.disabled= true;
		lastname_billingVar.disabled= true;
		address_billingVar.disabled= true;
		country_billingVar.disabled= true;
		city_billingVar.disabled= true;
		state_billingVar.disabled= true;
		zip_billingVar.disabled= true;
		email_billingVar.disabled= true;
		phone_billingVar.disabled= true; */
	} else {
/*		company_billingVar.disabled= false;
		firstname_billingVar.disabled= false;
		lastname_billingVar.disabled= false;
		address_billingVar.disabled= false;
		country_billingVar.disabled= false;
		city_billingVar.disabled= false;
		state_billingVar.disabled= false;
		zip_billingVar.disabled= false;
		email_billingVar.disabled= false;
		phone_billingVar.disabled= false; */
	}
}
function validate_required(field,alerttxt)
{
with (field)
{
  if (value==null||value==""||value=="select")
  {
  alert(alerttxt);return false;
  }
  else
  {
  return true;
  }
}
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}

function checkBilling(thisform)
{
with (thisform)
{
copyValues();
if (validate_required(firstname,"Please enter your First Name.")==false)
  {firstname.focus();return false;}
if (validate_required(lastname,"Please enter your Last Name.")==false)
  {lastname.focus();return false;}
if (validate_required(address,"Please enter your Address.")==false)
  {address.focus();return false;}
if (validate_required(country,"Please enter your Country.")==false)
  {country.focus();return false;}
if (validate_required(city,"Please enter your City.")==false)
  {city.focus();return false;}
if (validate_required(state,"Please enter your State.")==false)
  {state.focus();return false;}
if (validate_required(zip,"Please enter your Zip Code.")==false)
  {zip.focus();return false;} 
if (validate_email(email,"Please enter a valid e-mail.")==false)
  {email.focus();return false;}
if (validate_required(phone,"Please enter your Phone.")==false)
  {phone.focus();return false;} 
if (validate_required(firstname_billing,"Please enter your First Name (billing).")==false)
  {firstname_billing.focus();return false;}
if (validate_required(lastname_billing,"Please enter your Last Name (billing).")==false)
  {lastname_billing.focus();return false;}
if (validate_required(address_billing,"Please enter your Address (billing).")==false)
  {address_billing.focus();return false;}
if (validate_required(country_billing,"Please enter your Country (billing).")==false)
  {country_billing.focus();return false;}
if (validate_required(city_billing,"Please enter your City (billing).")==false)
  {city_billing.focus();return false;}
if (validate_required(state_billing,"Please enter your State (billing).")==false)
  {state_billing.focus();return false;}
if (validate_required(zip_billing,"Please enter your Zip (billing).")==false)
  {zip_billing.focus();return false;}
if (validate_email(email_billing,"Please enter a valid Email (billing).")==false)
  {email_billing.focus();return false;}
if (validate_required(phone_billing,"Please enter your Phone (billing).")==false)
  {phone_billing.focus();return false;}
if (validate_required(cc_type,"Please select the Credit Card Type.")==false)
  {cc_type.focus();return false;}
if (validate_required(cc_number,"Please enter your Credit Card Number.")==false)
  {cc_number.focus();return false;}
if (validate_required(cc_expiration_month,"Please enter your Credit Card expiration month.")==false)
  {cc_expiration_month.focus();return false;}
if (validate_required(cc_expiration_year,"Please enter your Credit Card expiration_year.")==false)
  {cc_expiration_year.focus();return false;}
}
}