function alter_form(name) {
	var cntry = document.getElementById(name).value;
	if(cntry!='United States') {
		var location = "https://www.compushade.com/order_intl.php?country="+cntry;
		//alert(location);
		window.location = location;
	}
}
function alter_form_cs(name) {
	var cntry = document.getElementById(name).value;
	if(cntry!='United States') {
		var location = "https://www.compushade.com/order_compushade_intl_cs.php?country="+cntry;
		//alert(location);
		window.location = location;
	}
}
function alter_form_cs_domestic(name) {
	var cntry = document.getElementById(name).value;
	if(cntry!='United States') {
		var location = "https://www.compushade.com/order_compushade.php";
		//alert(location);
		window.location = location;
	}
}

function alter_form_sh(name) {
	var cntry = document.getElementById(name).value;
	if(cntry!='United States') {
		var location = "https://www.compushade.com/order_sunhoods_intl_cs.php?country="+cntry;
		//alert(location);
		window.location = location;
	}
}
function alter_form_sh_domestic(name) {
	var cntry = document.getElementById(name).value;
	if(cntry!='United States') {
		var location = "https://www.compushade.com/order_sunhoods.php";
		//alert(location);
		window.location = location;
	}
}
function addShipping() {
	// check for non ups items
	// if only one product is displayed
	f = document.order1;
	if (typeof document.order1.line.length =="undefined") {
		// if single item
		if ((f.ship_ups.value == "N")&&(f.ship_choice[1].checked)) {
		  alert('Overnight shipping not available for desktop models due to size limits.\nYour order will be shipped via USPS first class mail.');
		  f.ship_choice[0].checked = true;
		}			  
	} // end if
	
	// if mult' products are displayed
	else {
		for (n=0; n<f.ship_ups.length; n++) {
			if ((f.ship_ups[n].value == "N")&&(f.line[n].value>0)&&(f.ship_choice[1].checked)) {
			  alert('Overnight shipping not available for desktop models due to size limits.\nYour order will be shipped via USPS first class mail.');
			  f.ship_choice[0].checked = true;
			}
		}
	} // end else  
	if (document.order1.ship_choice[0].checked) {
		usps_cost = parseFloat(f.usps_cost.value);
		break_point = parseFloat(f.break_point.value);
		subtotal_string = f.grand_total_tax.value.substring(2,f.grand_total_tax.value.length);
		//alert("subtotal: "+subtotal_string);
		subtotal = parseFloat(subtotal_string) - usps_cost;
		if((break_point>subtotal) && (subtotal>0)) {
			document.order1.ship_cost.value = "$ "+usps_cost;
		}
		else {
			document.order1.ship_cost.value = "$ 0.00";
		}
	}
	else if(document.order1.ship_choice[1].checked) {
		on_cost = parseFloat(f.overnight_cost.value);
		document.order1.ship_cost.value = "$ "+ round_decimals(on_cost,2);
	}
	addTax();
}
function addTax() {
	var gt = 0.00;
	var el = "";
	var num = "";
	var x = 0.00;
	var tax_disp = 0.00;
	var final_total = 0.00;
	var ship_t = "";
	
	// if only one product is displayed
	if (typeof document.order1.line.length =="undefined") {
		//alert("number of fields: undefined");
		if (document.order1.line_sum.value != "") {
			strng = document.order1.line_sum.value;
			el = strng.substring(2,strng.length);
			gt = gt + parseFloat(el);
		}
	}
	else {
		//alert("something");
		for(i=0; i < document.order1.line_sum.length; i++) {
			if (document.order1.line_sum[i].value != "") {
				strng = document.order1.line_sum[i].value;
				el = strng.substring(2,strng.length);
				gt = gt + parseFloat(el);
			}
		}
	}
	// calc subtotal
	x = round_decimals(gt,2);
	document.order1.grand_total.value = "$ "+x;
	ship = parseFloat(document.order1.ship_cost.value.substring(2,document.order1.ship_cost.value.length));	
	var tax = 0.00;
	if (document.order1.x_state.value == "FL") {
		tax = 0.075 * (gt+ship);
		//tax_disp = dollar(tax);
		tax_disp = round_decimals(tax,2);
		document.order1.tax.value = "$ "+tax_disp;
	}
	else {
		document.order1.tax.value = "$ 0.00";
	}
	total_tax_ship = tax+gt+ship;
	//final_total = dollar(total_tax);
	final_total = round_decimals(total_tax_ship, 2);
	document.order1.grand_total_tax.value = "$ "+final_total;	
}
function count(f,n,u)
{
	// if only one product is displayed
	if (typeof document.order1.line.length =="undefined") {
		f.line_sum.value = f.line.value * u;
		/*f.line_sum.value = Math.ceil(f.line_sum.value * 1000) /1000;
		f.line_sum.value = Math.floor(f.line_sum.value * 1000) /1000;
		f.line_sum.value = Math.round(f.line_sum.value * 100)/100;*/
		f.line_sum.value = round_decimals(f.line_sum.value,2);
		if(f.line_sum.value == "NaN") {
			alert("Error:\nYou may only enter numbers...\nPlease retry");
			f.line.value = f.line.value.substring(0, f.line.value.length-1);
			f.line_sum.value = f.line.value * u;
			if(f.line_sum.value == "0") {
				f.line_sum.value = "";
			}
		}
	  
		 // check to see whether can ship ups
		 //alert('ship value: '+f.ship_ups.value);
		if ((f.ship_ups.value == "N")&&(f.ship_choice[1].checked)) {
		  alert('Overnight shipping not available for desktop models due to size limits.\nYour order will be shipped via USPS first class mail.');
		  f.ship_choice[0].checked = true;
		  addShipping();
		}		
		t = round_decimals(f.line_sum.value,2);
		//t = dollar(f.line_sum.value);
		f.line_sum.value = "$ "+ t;
	  
	} // end if
	
	// if mult' products are displayed
	else {
		v = f.line[n].value * u;
		f.line_sum[n].value = round_decimals(v,2);
	  /*f.line_sum[n].value = Math.ceil(f.line_sum[n].value * 1000) /1000;
	  f.line_sum[n].value = Math.floor(f.line_sum[n].value * 1000) /1000;
	  f.line_sum[n].value = Math.round(f.line_sum[n].value * 100)/100;*/
	  if(f.line_sum[n].value == "NaN")
	  {
		alert("Error:\nYou may only enter numbers...\nPlease retry");
		f.line[n].value = f.line[n].value.substring(0, f.line[n].value.length-1);
		f.line_sum[n].value = f.line[n].value * u;
		if(f.line_sum[n].value == "0") {
			f.line_sum[n].value = "";
		}
	  }
	  else 
	  {
		if ((f.ship_ups[n].value == "N")&&(f.ship_choice[1].checked)) {
		  alert('Overnight shipping not available for desktop models due to size limits.\nYour order will be shipped via USPS first class mail.');
		  
		  f.ship_choice[0].checked = true;
		  addShipping();
		}
		//t = dollar(f.line_sum[n].value);
		t = round_decimals(f.line_sum[n].value,2);
		f.line_sum[n].value = "$ "+ t;
	   }
	} // end else  
   addTax();
   addShipping();
}
function count1(f,n,u)
{
	// if only one product is displayed
	if (typeof document.order1.line.length =="undefined") {
		f.line_sum.value = f.line.value * u;
		/*f.line_sum.value = Math.ceil(f.line_sum.value * 1000) /1000;
		f.line_sum.value = Math.floor(f.line_sum.value * 1000) /1000;
		f.line_sum.value = Math.round(f.line_sum.value * 100)/100;*/
		f.line_sum.value = round_decimals(f.line_sum.value,2);
		if(f.line_sum.value == "NaN") {
			alert("Error:\nYou may only enter numbers...\nPlease retry");
			f.line.value = f.line.value.substring(0, f.line.value.length-1);
			f.line_sum.value = f.line.value * u;
			if(f.line_sum.value == "0") {
				f.line_sum.value = "";
			}
		}
	  
		 // check to see whether can ship ups
		 //alert('ship value: '+f.ship_ups.value);
		if ((f.ship_ups.value == "N")&&(f.ship_choice[1].checked)) {
		  alert('Overnight shipping not available for desktop models due to size limits.\nYour order will be shipped via USPS first class mail.');
		  f.ship_choice[0].checked = true;
		  addShipping();
		}		
		t = round_decimals(f.line_sum.value,2);
		//t = dollar(f.line_sum.value);
		f.line_sum.value = "$ "+ t;
	  
	} // end if
	
	// if mult' products are displayed
	else {
		v = f.line[n].value * u;
		f.line_sum[n].value = round_decimals(v,2);
	  if(f.line_sum[n].value == "NaN")
	  {
		alert("Error:\nYou may only enter numbers...\nPlease retry");
		f.line[n].value = f.line[n].value.substring(0, f.line[n].value.length-1);
		f.line_sum[n].value = f.line[n].value * u;
		if(f.line_sum[n].value == "0") {
			f.line_sum[n].value = "";
		}
	  }
	  else 
	  {
		
		//t = dollar(f.line_sum[n].value);
		t = round_decimals(f.line_sum[n].value,2);
		//alert('overnight item total: '+t);
		f.line_sum[n].value = "$ "+ t;
		addShipping();
	   }
	} // end else  
   addTax();
   addShipping();
}
function dollar (amount)
{
        amount = parseInt(amount * 100);
        amount = parseFloat(amount/100);
        if (((amount) == Math.floor(amount)) && ((amount - Math.floor (amount)) == 0))
        {
                amount = amount + ".00"
                return amount;
        }
        if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
        {
                amount = amount + "0";
                return amount;
        }
        if ( ((amount * 100) - Math.floor(amount * 100)) == 0)
        {
                amount = amount;
                return amount;
        }
        return amount;
}

function get_data() {
	// if only one product is displayed
	if (typeof document.order1.line.length =="undefined") {
		//var order_data = "This Order is ...<br/>";
		var order_data = "";
  
		if(document.order1.line.value != "") {  
			var item_name = document.order1.modelnumber.value;
			//order_data += item_name+ " = "+document.order1.line.value +" Unit(s)\<br/>";
			//order_data += item_name+ "<>"+document.order1.line.value +"<>";
			order_data += document.order1.modeli.value+ "<>"+item_name+ "<>"+document.order1.line.value +"<>";
		}
		else {
			document.order1.line.value = "0";
		}
		
	  
	  if(document.order1.grand_total_tax.value == "") document.order1.grand_total_tax.value = "Nil";
	 
	  /*if (document.order1.ship_cost.value == "$ 18.00") {
		   order_data += "Shipping method: UPS Overnight Service.<br/>";
	  }
	  else {
		  order_data += "Shipping method: USPS first class mail.<br/>";
	  }*/
	  //order_data += "Shipping Charges: " + document.order1.ship_cost.value +"<br/>";
	  //order_data += "Total order value with tax = " + document.order1.grand_total_tax.value;
	  document.order1.x_description.value = order_data;
	}
	// if mult' products displayed
	else {
	  //var order_data = "This Order is ...<br/>";
	  var order_data = "";
  
	  for(i=0; i < document.order1.line.length; i++)
	  {
		if(document.order1.line[i].value != "") {  
			var item_name = document.order1.modelnumber[i].value;
			//order_data += item_name+ " = "+document.order1.line[i].value +" Unit(s)\<br/>";
			order_data += document.order1.modeli[i].value+ "<>"+item_name+ "<>"+document.order1.line[i].value +"<>";
		}
		else {
			document.order1.line[i].value = "0";
		}
		
	  }
	  if(document.order1.grand_total_tax.value == "") document.order1.grand_total_tax.value = "Nil";
	  //order_data += "Shipping Charges: " + document.order1.ship_cost.value +"<br/>";
	  document.order1.x_description.value = order_data;
	}
	/*if (document.order1.tax.value!="$ 0.00") {
		order_data = "CT Tax: "+document.order1.tax.value+"<br/>";
		document.order1.x_description.value += order_data;
	}*/
}

function send_data(g)
{
  get_data();
  if(Form_Validator(g)) {
	  g.submit();
  }  
}

function init()
{
  document.order1.reset();
  document.order1.x_first_name.select();
  document.order1.x_first_name.focus();
  addShipping();  
}

function Form_Validator(theForm){

  if (theForm.x_first_name.value.length < 1)  {
    alert("Please enter your first name in the \"First Name\" field.");
    theForm.x_first_name.focus();
	//theForm.x_first_name.style.backgroundColor='#63604d';
    return (false);
  }
  
  if (theForm.x_last_name.value.length < 1)  {
    alert("Please enter your last name in the \"Last Name\" field.");
    theForm.x_last_name.focus();
    return (false);
  }

  if (theForm.x_address.value.length < 5)  {
    alert("Please enter your shipping street in the \"Address\" field.");
    theForm.x_address.focus();
    return (false);
  }

  if (theForm.x_city.value.length < 3)  {
    alert("Please enter your shipping city in the \"City\" field.");
    theForm.x_city.focus();
    return (false);
  }

  if (theForm.x_state.selectedIndex < 1)  {
    alert("Please select one of the \"State\" options.");
    theForm.x_state.focus();
    return (false);
  } 
  if ((document.order1.x_state.value == "PR")&&(document.order1.ship_choice[1].checked)) {
	  alert("UPS overnight service is not available.\n"+
			"Please select the USPS first class mail option\n" +
			"or contact us for different arrangements.");
	  theForm.x_state.focus();
	  return (false);
  }
  if ((document.order1.x_state.value == "VI")&&(document.order1.ship_choice[1].checked)) {
	  alert("UPS overnight service is not available.\n"+
			"Please select the USPS first class mail option\n" +
			"or contact us for different arrangements.");
	  theForm.x_state.focus();
	  return (false);
  }
  if ((document.order1.x_state.value == "AK")&&(document.order1.ship_choice[1].checked)) {
	  alert("UPS overnight service is not available.\n"+
			"Please select the USPS first class mail option\n" +
			"or contact us for different arrangements.");
	  theForm.x_state.focus();
	  return (false);
  }
  if ((document.order1.x_state.value == "HI")&&(document.order1.ship_choice[1].checked)) {
	  alert("UPS overnight service is not available.\n"+
			"Please select the USPS first class mail option\n" +
			"or contact us for different arrangements.");
	  theForm.x_state.focus();
	  return (false);
  }

  if (theForm.x_zip.value.length < 5)  {
    alert("Please enter at least 5 characters in the \"Zip\" field.");
    theForm.x_zip.focus();
    return (false);
  }
  var shlomo = document.order1.grand_total_tax.value;
  if((shlomo == "$ 0.00")||(shlomo=="$ 18.00")||(shlomo=="$ 19.07"))
  {
    alert("No items are entered - \nPlease select at least one item.");
	// if only one product is displayed
	if (typeof document.order1.line.length =="undefined") {
		theForm.line.select();
		theForm.line.focus();
		return (false);
	}
	else {
		theForm.line[0].select();
		theForm.line[0].focus();
		return (false);
	}
  }

  var checkOK = "0123456789--";
  var checkStr = theForm.x_zip.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)    {
      allValid = false;
      break;
    }
  }
  if (!allValid)  {
    alert("Please enter only digit and \"-\" characters in the \"Zip\" field.");
    theForm.x_zip.focus();
    return (false);
  }

  var checkOK = "0123456789--";
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)    {
      allValid = false;
      break;
    }
  }
  if (!allValid)  {
    alert("Please enter only digit and \"-\" characters in the \"Quantity\" field.");
    theForm.Quantity_laptop.focus();
    return (false);
  }

  return (true);
} // end validator func
window.onload=init;
