var bcost=0; mcost=0; wcost=0; xcost=0; tcost=0; size=0; tinyDisplay=0;
var color="Red"; wflag=0; wire=0; address=""; specialPay=0;
var pvcColors = new Array("#ffffff","#989090","#000000");
var ledColors = new Array("#ff2020","#ffff00","#80d000","#ffc000","#33c1ff","ffffff","00c050","ffff80");
var pattern = / /
var canPattern = /[a-zA-Z][0-9][a-zA-Z][ |-]*[0-9][a-zA-Z][0-9]/
// pattern above allows hyphens or spaces between the two 3-character groups
var pcode=""; discount=0; alignment="Horizontal";
var v_index=0; c_index=0;
// update prices here--first 7 elements are horizontal prices, last 7 elements are vertical prices
var price1 = new Array(69,69,69,69,69,70,71,"-x-","-x-","-x-","-x-","-x-","-x-","-x-")
var price2 = new Array(74,74,73,75,77,78,79,84,84,83,85,87,88,89)
var price3 = new Array(79,79,78,80,84,85,87,91,91,90,92,96,97,99)
var price4 = new Array(85,85,83,86,89,91,94,99,99,97,99,105,107,111)
var price5 = new Array(89,89,88,91,93,96,99,109,109,108,111,113,117,121)
//set express shipping charge in shipping_cost function
//set Canadian discount value in country function

function populate()
// fills pricing tables with price array values
{
 for (i=0; i < 14; i++)
  { 
   priceTable.rows[2].cells[i+1].innerHTML=price1[i]
   priceTable.rows[3].cells[i+1].innerHTML=price2[i]
   priceTable.rows[4].cells[i+1].innerHTML=price3[i]
   priceTable.rows[5].cells[i+1].innerHTML=price4[i]
   priceTable.rows[6].cells[i+1].innerHTML=price5[i]
  }
}

function display(arg)
{
 address=arg.digits.value;
 size=address.length;
 tinyDisplay=0; // resets this flag for any display alteration
 if ((isNaN(address)) || (pattern.test(address)))
  if (address=="$")
   specialPay=1
 else
 {
  alert("Please use only numbers!")
  arg.digits.value=""
  address=""
  size=0
  bcost=0
  document.ordering.digits.focus()
 }
 displayPrice()
}

function displayPrice()
{
 {
 if ((alignment=="Vertical") && (size<2))
  {
   alert("There must be at least 2 display digits to order the vertical style. Enter the digits above and reselect the vertical style.")
   document.ordering.axis.selectedIndex=0
   v_index=0
   alignment="Horizontal"
   document.ordering.digits.focus() 
  }  
 if ((size==0) || (address=="$"))
  bcost=0
 else if (size==1)
  bcost=price1[v_index+c_index]
 else if (size==2)
  bcost=price2[v_index+c_index]
 else if (size==3)
  bcost=price3[v_index+c_index]
 else if (size==4)
  bcost=price4[v_index+c_index]
 else if (size==5)
  bcost=price5[v_index+c_index]
 }
 theTable.rows[1].cells[2].innerHTML=bcost
 total_cost()
}
 
function vertical_cost(arg)
{
 alignment=arg.axis.value
 {
  if ((alignment=="Vertical") && (size>=2))
   v_index=7
  else
   v_index=0
 }
 displayPrice()
}

function PVCfoam(index)
{
theLEDress.style.borderColor=pvcColors[index]
}

function led_color(selColor,index)
{
 theLEDress.style.color=ledColors[index]
 color=selColor.leds.value
 c_index=0
 {
  if (color=="Red")
   c_index=0
  else if (color=="Yellow")
   c_index=1
  else if (color=="Green")
   c_index=2
  else if (color=="Orange")
   c_index=3
  else if (color=="Blue")
   c_index=4
  else if (color=="White")
   c_index=5
  else if (color=="coolGreen")
   c_index=6
 }
 displayPrice()
}

function mounting_cost(arg)
{
if (arg.mounting.value=="yes")
 {
   mcost=4
 }
else
 {
   mcost=0
 }
theTable.rows[5].cells[2].innerHTML=mcost
total_cost()
}

function wiring_option(arg)
{
if (arg.wiring.value=="no")
 {
 theTable.rows[7].cells[2].innerHTML=0
 arg.wire_length.value=""
 wflag=0
 wcost=0
 }
else
 {
 wflag=1
 }
total_cost()
}

function wiring_cost(arg)
{
if ((arg.wiring.value=="no") && (specialPay==0)) 
 {
 alert("No wiring kit option selected yet!")
 wcost=0
 arg.wire_length.value=""
 document.ordering.wiring.focus()
 return false
 }
if (isNaN(arg.wire_length.value))
 {
 alert("Please use only numbers!")
 wcost=0
 theTable.rows[7].cells[2].innerHTML=wcost
 total_cost()
 arg.wire_length.value=""
 document.ordering.wire_length.focus()
 return false
 }
if (specialPay==1)
 wcost=arg.wire_length.value // using wire length for special situation payment amount
else
 {
 wcost=Math.floor(((arg.wire_length.value*1)+4)/5) // add 4 and round down 5' integrals for exact $ >0 for lengths >0
 }
theTable.rows[7].cells[2].innerHTML=wcost
total_cost() 
}

function shipping_cost(arg)
{
if (arg.express.value=="yes")
 {
 xcost=29
 alert("Friday, Saturday, or Sunday orders ship on Monday, arrive on Tuesday. Monday-Thursday orders ship next day, arrive the day after that.")
 }
else
 {
 xcost=0
 }
theTable.rows[8].cells[2].innerHTML=xcost
total_cost()
}

function country(arg)
{
// if customer is Canadian then discount total$ 
pcode=arg.zip.value
if (canPattern.test(pcode))  
 {
 discount=3
 alert("Hello Canadian customer! After you click \"OK\", notice that your Total$ is reduced.")
 total_cost()
 }
else
 {
 discount=0
 total_cost()
 }
}

function total_cost()
{
tcost=((bcost*1)+(mcost*1)+(wcost*1))
theTable.rows[9].cells[1].innerHTML="Total $"
theTable.rows[9].cells[1].style.color="#000000"
if ((tcost>99) && (alignment=="Horizontal") && (specialPay==0))
 {
 if (wcost<=10) // 10 being the cost of 50 wire feet
  tcost=99
 else 
  tcost=99+(wcost-10) // first 50 wire feet remains free
 theTable.rows[9].cells[1].innerHTML="Special Total $" // instead of alert box
 theTable.rows[9].cells[1].style.color="#ff0000"
 }
tcost=((tcost*1)+(xcost*1)-(discount*1)) // express shipping cost is not included in $99 special
theTable.rows[9].cells[2].innerHTML=tcost
}

function pay_method(arg)
{
if (arg.payMethod.value=="none")
 thePayment.rows[0].cells[1].innerHTML="No pay method is selected"
else if (arg.payMethod.value=="pal")
 thePayment.rows[0].cells[1].innerHTML="BUY NOW button will appear<br>after order is submitted"
else
 thePayment.rows[0].cells[1].innerHTML="Mailing instructions will appear<br>after order is submitted"
}

function zero()
{
color="Red"; address=""; bcost=0; mcost=0; wcost=0; size=0; xcost=0; tcost=0; wflag=0; wire=0; tinyDisplay=0; specialPay=0;
pcode=""; discount=0; v_index=0; c_index=0; alignment="Horizontal"
theLEDress.style.borderColor="#ffffff"
theLEDress.style.color="#ff2020"
theLEDress.value=""
document.ordering.axis.selectedIndex=0
document.ordering.enclosure.selectedIndex=0
document.ordering.leds.selectedIndex=0
document.ordering.mounting.selectedIndex=0
document.ordering.wiring.selectedIndex=0
document.ordering.wire_length.value=""
document.ordering.express.selectedIndex=0 // ? express selection was working intermittantly
document.ordering.zip.value="" // so that an IE browser refresh erases this value, forces re-entry for Canadian discount
populate()
theTable.rows[1].cells[2].innerHTML=0
theTable.rows[5].cells[2].innerHTML=0
theTable.rows[7].cells[2].innerHTML=0
theTable.rows[8].cells[2].innerHTML=0
theTable.rows[9].cells[2].innerHTML=0
theTable.rows[9].cells[1].innerHTML="Total $" // default starting text
theTable.rows[9].cells[1].style.color="#000000" // initialize or reset color to black
theTable.rows[18].cells[1].innerHTML="<input id=completed type=button value='SUBMIT ORDER' onclick='return toLogician()'>"
thePayment.rows[0].cells[1].innerHTML="Payment method not selected"
theInstruct.style.backgroundColor="#ffffa0"
theInstruct.innerHTML="<p><b>Please do not use browser back/forward or refresh buttons,</b> the form entries may be erased.</p><p><b><span>The LEDress ships in the U.S. Mail.</span></b> Use a PO Box or alternate address if the postal carrier cannot leave the parcel at your home or business.</p><p><b>The * fields</b> are required to process the order.</p><p><b>The ship-to address</b> does not have to match the display numbers, like when you are buying a LEDress for someone else.</p><p><b>Customers giving an e-mail address</b> will receive order confirmation and shipping info.</p><p><b>Paying with PayPal:</b> This is a separate step from \"SUBMIT ORDER\".  PayPal is used here to make payment securely and electronically using a credit card or PayPal account.</p><p><b>Paying by Mail: Write down the Total $ or print this webpage before leaving!</b><br>You can pay by check, money order, bank draft, etc.</p><p><b>Ordering multiple units:</b> Repeat the ordering process for each unit.</p><p><b>More payment instructions</b> will appear here after the order form is submitted.</p>";
document.ordering.digits.focus()
}

function toLogician()
{
 with (document.ordering)
 if (tcost<=0)
  {
  alert("Nothing was ordered!")
  digits.focus()
  return false
  }
 else if (address == "")
  {
  alert("Please enter your address display numbers in the box next to the little bear!")
  digits.focus()
  return false
  }
 else if ((tinyDisplay==0) && (address<6))
  {
  if (confirm("Display numbers: If you are ordering a single-digit display, click OK, but if you have entered the number of digits in your address, click Cancel and enter your actual address numbers"))
   tinyDisplay=1 // intentional single-digit display flag
  else
   {
   tinyDisplay=0
   digits.focus()
   return false
   }
  }
 else if ((wflag==1) && (wcost==0))
  {
  alert("Wiring kit has no wire length!")
  wire_length.focus()
  return false
  }
 else if (((street.value == "") || (city.value == "")) || ((state.value == "") || (zip.value == "")))
  {
  alert("Shipping information is incomplete!")
  street.focus()
  return false
  }
 else if (payMethod.value == "none")
  {
  alert("Please select a payment method!")
  payMethod.focus()
  return false
  }
 else
  {
  var with_cents = tcost + ".00"
  document.ordering.payment_due.value=with_cents
  document.ordering.screen_width.value=window.screen.width + "px"
  document.paying.amount.value=with_cents
  submit()
  if (payMethod.value == "pal")
   {
   thePayment.rows[0].cells[1].innerHTML="<b>&nbsp;Buy Now button will appear shortly.<br><font color=red>Read new instructions at right.</font></b>"
   theInstruct.style.backgroundColor="#c0ffc0"
   theInstruct.innerHTML="<p><font color=red><b>New payment instructions.</b></font></p><p><b>Thanks!</b> The order form information has been sent to the Logician. If you want to change anything, make the changes and re-submit. Otherwise, this order comes to $" +tcost+".</p><p><b>Please do Step 4, Payment.</b> The PayPal website will accept many credit cards, but it\'s a busy place and it may take a moment for the checkout page to appear. The PayPal website will open in a new window or a new browser tab.  At PayPal, you have the option of paying without becoming a PayPal member.</p><p><b>Cookies must be enabled for PayPal to work,</b> and certain browser security settings or firewalls may interfere. If you have problems getting to the PayPal site, consider using a different computer unless you have had success with PayPal on this one.</p><p><b>If PayPal problems persist,</b> consider selecting the Send Payment in the Mail method.</p>"
   setTimeout("thePayment.rows[0].cells[1].innerHTML='<input id=paybutton type=image src=./thumbs/buy-now.gif border=0 name=submit>'", 5000)
   }
  else
   {
   thePayment.rows[0].cells[1].innerHTML="<b>&nbsp;Pausing while form submits.<br><font color=red>Read new instructions at right.</font></b>"
   theInstruct.style.backgroundColor="#c0ffc0"
   theInstruct.innerHTML="<p><font color=red><b>New payment instructions.</b></font></p><p><b>Thanks!</b> The order form information has been sent to the Logician. If you want to change anything, make the changes and re-submit. Otherwise, this order comes to $" +tcost+".</p><p><b>Please do Step 4, Payment.</b> Write down the $ Total or print this webpage before leaving!<br>The LEDress will ship the next weekday after your payment arrives.<br>This webpage prints best using landscape mode.</p><p>Make check or other document payable to<br><b>\"The Logician LLC\"</b><br>and mail to:<br><b>The Logician LLC, 4207 SE Woodstock Blvd, Department 123, Portland OR 97206</b></p><p><b>Your payment will go to a locked and secure mailbox.</b><br>A receipt will be sent with the LEDress for your records."
   setTimeout("thePayment.rows[0].cells[1].innerHTML='Order form submission complete.'", 5000)
   }
  }
}
