var blueagentTotal = 0;
var transmitterTotal = 0;
var caseTotal = 0;

var transmitterTimer = null;
var caseTimer = null;



function updateSubtotal()
{
   var subtotal = blueagentTotal + transmitterTotal + caseTotal;
   var elementRef = document.getElementById('subtotal');
   if (elementRef != null) {
      elementRef.innerHTML = '<p class=black>$' + subtotal;
   }

   var elementRef = document.getElementById('subtotalText');
   if (elementRef != null) {
      elementRef.innerHTML = '<p class=white><b>Subtotal</b>';
   }
}

function resetBlueAgent()
{
   if (document.orderform.blueagentCount.value == "") {
     document.orderform.blueagentCount.value = 0;
     checkBlueAgent();
   }
   return true;
}

function checkBlueAgent()
{
   if (document.orderform.blueagentCount.value == "") return;
   
   var count = parseInt(document.orderform.blueagentCount.value);
   if (isNaN(count)) count = 0;
   if (count < 0) count = 0;
   if (count > 100) count = 100;
   
   document.orderform.blueagentCount.value = count;
   var elementRef = document.getElementById('blueagentCostText');
   var total = document.getElementById('blueagentTotalText');

   if (elementRef != null && total != null) {
     if (count >= 30) {
        elementRef.innerHTML = '<p class=black>$329';
        blueagentTotal = 329*count;
     }
     else if (count >= 15) {
        elementRef.innerHTML = '<p class=black>$339';
        blueagentTotal = 339*count;
     }
     else {
        elementRef.innerHTML = '<p class=black>$349';
        blueagentTotal = 349*count;
     }
     total.innerHTML = '<p class=black>$' + blueagentTotal;
   }

   clearTimeout(transmitterTimer);
   clearTimeout(caseTimer);
   transmitterTimer = setTimeout("checkTransmitter()",2000);
   caseTimer = setTimeout("checkCase()",2000);

   updateSubtotal();
}

function resetTransmitter()
{
   if (document.orderform.transmitterCount.value == "") {
     document.orderform.transmitterCount.value = 0;
     checkTransmitter();
   }
   return true;
}

function checkTransmitter()
{
   if (document.orderform.transmitterCount.value == "") return;
   
   var count = parseInt(document.orderform.transmitterCount.value);
   if (isNaN(count)) count = 0;
   if (count < 0) count = 0;
   if (count > 100) count = 100;
   
   document.orderform.transmitterCount.value = count;

   var unitCount = parseInt(document.orderform.blueagentCount.value);
   if (unitCount == "") unitCount = 0;

   if  (count > unitCount) {
      document.orderform.transmitterCount.value = unitCount;
      count = unitCount;
   }

   var total = document.getElementById('transmitterTotalText');

   if (total != null) {
     transmitterTotal = 59*count;
     total.innerHTML = '<p class=black>$' + transmitterTotal;
   }

   updateSubtotal();
}

function resetCase()
{
   if (document.orderform.caseCount.value == "") {
     document.orderform.caseCount.value = 0;
     checkCase();
   }

   return true;
}

function checkCase()
{
   if (document.orderform.caseCount.value == "") return;
   
   var count = parseInt(document.orderform.caseCount.value);
   if (isNaN(count)) count = 0;
   if (count < 0) count = 0;
   if (count > 100) count = 100;
   
   document.orderform.caseCount.value = count;

   var unitCount = parseInt(document.orderform.blueagentCount.value);
   if (unitCount == "") unitCount = 0;

   if  (count > unitCount) {
      document.orderform.caseCount.value = unitCount;
      count = unitCount;
   }

   var total = document.getElementById('caseTotalText');

   if (total != null) {
     caseTotal = 39*count;
     total.innerHTML = '<p class=black>$' + caseTotal;
   }

   updateSubtotal();
}


function clearFirstName()
{
   if (document.orderform.firstname.value == "First Name") {
//     document.orderform.firstname.value = "";
     document.orderform.firstname.select();
   }
   return true;
}

function resetFirstName()
{
   if (document.orderform.firstname.value == "") {
     document.orderform.firstname.value = "First Name";
   }
   return true;
}

function clearLastName()
{
   if (document.orderform.lastname.value == "Last Name") {
      //document.orderform.lastname.value = "";
      document.orderform.lastname.select();
   }
   return true;
}

function resetLastName()
{
   if (document.orderform.lastname.value == "") {
     document.orderform.lastname.value = "Last Name";
   }
   return true;
}

function clearCompany()
{
   if (document.orderform.firstname.value == "Company") {
//     document.orderform.firstname.value = "";
     document.orderform.company.select();
   }
   return true;
}

function resetCompany()
{
   if (document.orderform.company.value == "") {
     document.orderform.company.value = "Company";
   }
   return true;
}

function clearAddress1()
{
   if (document.orderform.address1.value == "Address") {
//     document.orderform.address1.value = "";
     document.orderform.address1.select();
   }
   return true;
}

function resetAddress1()
{
   if (document.orderform.address1.value == "") {
     document.orderform.address1.value = "Address";
   }
   return true;
}

function clearAddress2()
{
   if (document.orderform.address2.value == "Address") {
//     document.orderform.address2.value = "";
     document.orderform.address2.select();
   }
   return true;
}

function resetAddress2()
{
   if (document.orderform.address2.value == "") {
     document.orderform.address2.value = "Address";
   }
   return true;
}

function clearCity()
{
   if (document.orderform.city.value == "City") {
//     document.orderform.city.value = "";
     document.orderform.city.select();
   }
   return true;
}

function resetCity()
{
   if (document.orderform.city.value == "") {
     document.orderform.city.value = "City";
   }
   return true;
}


function selectState(county)
{
   var firstname = document.orderform.firstname.value;                   
   var lastname = document.orderform.lastname.value;                   
   var address1 = document.orderform.address1.value;                   
   var address2 = document.orderform.address2.value;                   
   var city = document.orderform.city.value;                   
   var state = document.orderform.state.value;                   
   var zip = document.orderform.postcode.value;                   
   var phone = document.orderform.phone.value;                   
   var comments = document.orderform.comments.value;                   
         
   if (state == "FL") {
      var elementRef = document.getElementById('Fields');
      if (elementRef != null) {
         if (elementRef.innerHTML.indexOf ("county", 0) != -1) return;

         var countyHTML = '<select name="county"  style="width: 315px">';
         if (county != "" && county != "(Select County)") {
            countyHTML += '<option value="' + county + '" selected>' + county + '</option>';
         }
         
         countyHTML += '<option value="">(Select County)</option>'
                   + '<option value="Alachua">Alachua</option>'
                   + '<option value="Baker">Baker</option>'
                   + '<option value="Bay">Bay</option>'
                   + '<option value="Bradford">Bradford</option>'
                   + '<option value="Brevard">Brevard</option>'
                   + '<option value="Broward">Broward</option>'
                   + '<option value="Calhoun">Calhoun</option>'
                   + '<option value="Charlotte">Charlotte</option>'
                   + '<option value="Citrus">Citrus</option>'
                   + '<option value="Clay">Clay</option>'
                   + '<option value="Collier">Collier</option>'
                   + '<option value="Columbia">Columbia</option>'
                   + '<option value="DeSoto">DeSoto</option>'
                   + '<option value="Dixie">Dixie</option>'
                   + '<option value="Duval">Duval</option>'
                   + '<option value="Escambia">Escambia</option>'
                   + '<option value="Flagler">Flagler</option>'
                   + '<option value="Franklin">Franklin</option>'
                   + '<option value="Gadsden">Gadsden</option>'
                   + '<option value="Gilchrist">Gilchrist</option>'
                   + '<option value="Glades">Glades</option>'
                   + '<option value="Gulf">Gulf</option>'
                   + '<option value="Hamilton">Hamilton</option>'
                   + '<option value="Hardee">Hardee</option>'
                   + '<option value="Hendry">Hendry</option>'
                   + '<option value="Hernando">Hernando</option>'
                   + '<option value="Highlands">Highlands</option>'
                   + '<option value="Hillsborough">Hillsborough</option>'
                   + '<option value="Holmes">Holmes</option>'
                   + '<option value="Indian River">Indian River</option>'
                   + '<option value="Jackson">Jackson</option>'
                   + '<option value="Jefferson">Jefferson</option>'
                   + '<option value="Lafayette">Lafayette</option>'
                   + '<option value="Lake">Lake</option>'
                   + '<option value="Lee">Lee</option>'
                   + '<option value="Leon">Leon</option>'
                   + '<option value="Levy">Levy</option>'
                   + '<option value="Liberty">Liberty</option>'
                   + '<option value="Madison">Madison</option>'
                   + '<option value="Manatee">Manatee</option>'
                   + '<option value="Marion">Marion</option>'
                   + '<option value="Martin">Martin</option>'
                   + '<option value="Miami-Dade">Miami-Dade</option>'
                   + '<option value="Monroe">Monroe</option>'
                   + '<option value="Nassau">Nassau</option>'
                   + '<option value="Okaloosa">Okaloosa</option>'
                   + '<option value="Okeechobee">Okeechobee</option>'
                   + '<option value="Orange">Orange</option>'
                   + '<option value="Osceola">Osceola</option>'
                   + '<option value="Palm Beach">Palm Beach</option>'
                   + '<option value="Pasco">Pasco</option>'
                   + '<option value="Pinellas">Pinellas</option>'
                   + '<option value="Polk">Polk</option>'
                   + '<option value="Putnam">Putnam</option>'
                   + '<option value="Santa Rosa">Santa Rosa</option>'
                   + '<option value="Sarasota">Sarasota</option>'
                   + '<option value="Seminole">Seminole</option>'
                   + '<option value="St. Johns">St. Johns</option>'
                   + '<option value="St. Lucie">St. Lucie</option>'
                   + '<option value="Sumter">Sumter</option>'
                   + '<option value="Suwannee">Suwannee</option>'
                   + '<option value="Taylor">Taylor</option>'
                   + '<option value="Union">Union</option>'
                   + '<option value="Volusia">Volusia</option>'
                   + '<option value="Wakulla">Wakulla</option>'
                   + '<option value="Walton">Walton</option>'
                   + '<option value="Washington">Washington</option>'
                   + '</select> ';
         
         elementRef.innerHTML = fields1 + countyHTML + fields2;
         setTimeout("document.orderform.state.focus();", 100);
      }
   }

   else {
      var elementRef = document.getElementById('Fields');
      if (elementRef != null) {
         elementRef.innerHTML = fields1 + fields2;
         setTimeout("document.orderform.state.focus();", 100);
     }
   }

   document.orderform.firstname.value = firstname;             
   document.orderform.lastname.value = lastname;                   
   document.orderform.address1.value = address1;                   
   document.orderform.address2.value = address2;                   
   document.orderform.city.value = city;                   
   document.orderform.state.value = state;                   
   document.orderform.postcode.value = zip;                   
   document.orderform.phone.value = phone;                   
   document.orderform.comments.value = comments;                   
   return true;
}

function clearZip()
{
   if (document.orderform.postcode.value == "Zip Code") {
//     document.orderform.postcode.value = "";
     document.orderform.postcode.select();
   }
   return true;
}

function resetZip()
{
   if (document.orderform.postcode.value == "") {
     document.orderform.postcode.value = "Zip Code";
   }
   return true;
}
function clearPostcode()
{
   if (document.orderform.postcode.value == "Postal Code") {
//     document.orderform.postcode.value = "";
     document.orderform.postcode.select();
   }
   return true;
}

function resetPostcode()
{
   if (document.orderform.postcode.value == "") {
     document.orderform.postcode.value = "Postal Code";
   }
   return true;
}

function clearEmail()
{
   if (document.orderform.email.value == "E-Mail Address") {
//     document.orderform.email.value = "";
     document.orderform.email.select();
   }
   return true;
}

function resetEmail()
{
   if (document.orderform.email.value == "") {
     document.orderform.email.value = "E-Mail Address";
   }
   return true;
}

function clearPhone()
{
   if (document.orderform.phone.value == "Phone Number (xxx-xxx-xxxx format)") {
//     document.orderform.phone.value = "";
     document.orderform.phone.select();
   }
   return true;
}

function resetPhone()
{
   if (document.orderform.phone.value == "") {
     document.orderform.phone.value = "Phone Number (xxx-xxx-xxxx format)";
   }
   return true;
}

function clearComments()
{
   if (document.orderform.comments.value == "Comments") {
//     document.orderform.comments.value = "";
     document.orderform.comments.select();
   }
   return true;
}

function resetComments()
{
   if (document.orderform.comments.value == "") {
     document.orderform.comments.value = "Comments";
   }
   return true;
}

function selectCountry(address1, address2, city, state, county, postcode, phone)
{
   if (document.orderform.country.value == "United States" ||
       document.orderform.country.value.indexOf("U.S. Domestic") != -1) {
      var elementRef = document.getElementById('Fields');
      if (elementRef != null) {

        var stateHTML = '<select name="state"  style="width: 315px" onChange="selectState(\'' + county + '\')">';
        if (document.orderform.country.value.indexOf("U.S. Domestic") != -1) {
           stateHTML += '<option value="XX" selected>U.S. Territory or Possession</option>';
        }
        else if (state != "" && state != "(Select State)") {
           stateHTML += '<option value="' + state + '" selected>' + state + '</option>';
        }
        
        stateHTML += '<option value="">(Select State)</option>'
                   + '<option value="XX">U.S. Territory or Possession</option>'
                   + '<option value="AL">AL</option>'
                   + '<option value="AK">AK</option>'
                   + '<option value="AZ">AZ</option>'
                   + '<option value="AR">AR</option>'
                   + '<option value="CA">CA</option>'
                   + '<option value="CO">CO</option>'
                   + '<option value="CT">CT</option>'
                   + '<option value="DE">DE</option>'
                   + '<option value="DC">DC</option>'
                   + '<option value="FL">FL</option>'
                   + '<option value="GA">GA</option>'
                   + '<option value="HI">HI</option>'
                   + '<option value="ID">ID</option>'
                   + '<option value="IL">IL</option>'
                   + '<option value="IN">IN</option>'
                   + '<option value="IA">IA</option>'
                   + '<option value="KS">KS</option>'
                   + '<option value="KY">KY</option>'
                   + '<option value="LA">LA</option>'
                   + '<option value="ME">ME</option>'
                   + '<option value="MD">MD</option>'
                   + '<option value="MA">MA</option>'
                   + '<option value="MI">MI</option>'
                   + '<option value="MN">MN</option>'
                   + '<option value="MS">MS</option>'
                   + '<option value="MO">MO</option>'
                   + '<option value="MT">MT</option>'
                   + '<option value="NE">NE</option>'
                   + '<option value="NV">NV</option>'
                   + '<option value="NH">NH</option>'
                   + '<option value="NJ">NJ</option>'
                   + '<option value="NM">NM</option>'
                   + '<option value="NY">NY</option>'
                   + '<option value="NC">NC</option>'
                   + '<option value="ND">ND</option>'
                   + '<option value="OH">OH</option>'
                   + '<option value="OK">OK</option>'
                   + '<option value="OR">OR</option>'
                   + '<option value="PA">PA</option>'
                   + '<option value="RI">RI</option>'
                   + '<option value="SC">SC</option>'
                   + '<option value="SD">SD</option>'
                   + '<option value="TN">TN</option>'
                   + '<option value="TX">TX</option>'
                   + '<option value="UT">UT</option>'
                   + '<option value="VT">VT</option>'
                   + '<option value="VA">VA</option>'
                   + '<option value="WA">WA</option>'
                   + '<option value="WV">WV</option>'
                   + '<option value="WI">WI</option>'
                   + '<option value="WY">WY</option>'
                   + '</select> ';

        fields1 = '<font size=5><input type=text name="address1" onFocus="clearAddress1();" onBlur="resetAddress1();" value="' + address1 + '" style="width: 315px"> '
                + '<input type=text name="address2" onFocus="clearAddress2();" onBlur="resetAddress2();" value="' + address2 + '" style="width: 315px"> '
                + '<input type=text name="city" onFocus="clearCity();" onBlur="resetCity();" value="' + city +'" style="width: 315px"> '
                + stateHTML;

        if (postcode == "Postal Code") postcode = "Zip Code";
        fields2 = '<input type=text name="postcode" onFocus="clearZip();" onBlur="resetZip();" value="' + postcode + '" style="width: 315px"> '
                + '<input type=text name="phone" onFocus="clearPhone();" onBlur="resetPhone();" value="' + phone + '" style="width: 315px"> ';

        elementRef.innerHTML = fields1 + fields2;

        if (state != "" && state != "(Select State)") selectState(county);
      }

   }
   else if (document.orderform.country.value != "") {
      
      var elementRef = document.getElementById('Fields');
      if (elementRef != null) {
        if (postcode == "Zip Code") postcode = "Postal Code";

        elementRef.innerHTML = '<font size=5>'
                             + '<input type=text name="address1" onFocus="clearAddress1();" onBlur="resetAddress1();" value="' + address1 + '" style="width: 315px"> '
                             + '<input type=text name="address2" onFocus="clearAddress2();" onBlur="resetAddress2();" value="' + address2 + '" style="width: 315px"> '
                             + '<input type=text name="city" onFocus="clearCity();" onBlur="resetCity();" value="' + city +'" style="width: 315px"> '
                             + '<input type=text name="postcode" onFocus="clearPostcode();" onBlur="resetPostcode();" value="' + postcode +'" style="width: 315px"> </font>';
      }
   }
}

function checkform()
{
  if (document.orderform.blueagentCount.value <= 0) {
    alert( "No BlueAgent units are in your cart." );
    document.orderform.blueagentCount.select();
    return false;
  }

  document.orderform.firstname.value = trim(document.orderform.firstname.value.replace(/First Name/gi,""));

  if (document.orderform.firstname.value == "") {
    alert( "Please enter your first name." );
    document.orderform.firstname.select();
    return false;
  }

  document.orderform.lastname.value = trim(document.orderform.lastname.value.replace(/Last Name/gi,""));

  if (document.orderform.lastname.value == "") {
    alert( "Please enter your last name." );
    document.orderform.lastname.select();
    return false;
  }

  if (document.orderform.country.value == "") {
    alert( "Please select a country." );
    document.orderform.country.focus();
    return false;
  }

  document.orderform.address1.value = trim(document.orderform.address1.value.replace(/Address/gi,""));
  document.orderform.address2.value = trim(document.orderform.address2.value.replace(/Address/gi,""));

  if (document.orderform.address1.value == "") {
    alert( "Please enter an address." );
    document.orderform.address1.select();
    return false;
  }

  document.orderform.city.value = trim(document.orderform.city.value);

  if (document.orderform.city.value == "City") {
    alert( "Please enter a city." );
    document.orderform.city.select();
    return false;
  }

  if (document.orderform.country.value == "United States" && document.orderform.state.value == "") {
    alert( "Please select a state." );
    document.orderform.state.focus();
    return false;
  }

  if (document.orderform.country.value == "United States" && document.orderform.state.value == "FL" && document.orderform.county.value == "") {
    alert( "Florida residents need to select a county." );
    document.orderform.county.focus();
    return false;
  }

  if ((document.orderform.country.value == "United States" || document.orderform.country.value.indexOf("U.S. Domestic") != -1)) {
    document.orderform.postcode.value = trim(document.orderform.postcode.value.replace(/Zip Code/gi,""));
  
    if (document.orderform.postcode.value == "") {
      alert( "Please enter a zip code." );
      document.orderform.postcode.select();
      return false;
    }

    if (!CheckZip()) {
      document.orderform.postcode.select();
      return false;
    }

    document.orderform.phone.value = trim(document.orderform.phone.value);

    document.orderform.phone.value = trim(document.orderform.phone.value.replace(/Phone Number \(xxx-xxx-xxxx format\)/gi,""));

    if (document.orderform.phone.value == "") {
      alert( "Please enter a phone number." );
      document.orderform.phone.select();
      return false;
    }
  
    // check number if provided
    if (!CheckPhone()) {
       document.orderform.phone.select();
       return false;
    }
  }
  else {
    document.orderform.postcode.value = trim(document.orderform.postcode.value.replace(/Postal Code/gi,""));

    if (document.orderform.postcode.value == "") {
      alert( "Please enter a postal code." );
      document.orderform.postcode.select();
      return false;
    }
  }

  document.orderform.email.value = trim(document.orderform.email.value.replace(/E-Mail Address/gi,""));

  if (document.orderform.email.value == "") {
    alert( "Please enter an e-mail address." );
    document.orderform.email.select();
    return false;
  }
  
  if (!CheckEmail()) {
    document.orderform.email.select();
    return false;
  }
  
  return true ;
}

function CheckZip()
{
   var s_zip = document.orderform.postcode.value;

   var i = s_zip.indexOf(' ',0);
   while(i > -1)
   {
      s_zip = s_zip.substring(0,i) + 
         s_zip.substring((i + 1),s_zip.length);
      i = s_zip.indexOf(' ',0);
   }

   document.orderform.postcode.value = s_zip.substring(0,5);
   if((s_zip.length < 5) ||
      (s_zip.charAt(0) < '0' || s_zip.charAt(0) > '9') ||
      (s_zip.charAt(1) < '0' || s_zip.charAt(1) > '9') ||
      (s_zip.charAt(2) < '0' || s_zip.charAt(2) > '9') ||
      (s_zip.charAt(3) < '0' || s_zip.charAt(3) > '9') ||
      (s_zip.charAt(4) < '0' || s_zip.charAt(4) > '9'))
   {
      alert('The zip code "' + s_zip + '" is not a valid 5 digit zip code.');
      return false;
   }
   return true;
}

function CheckPhone() 
{
   var thisnumber = document.orderform.phone.value;

   if(thisnumber.length == 0)
   {
      return false;
   }

   OK = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/.exec(thisnumber)
   if (!OK)		
   {
      alert('The phone number "' + thisnumber + '" is not a valid number in the format xxx-xxx-xxxx.');
      return false;
   }

   return true;
}

function CheckEmail()
{
   var s_email = document.orderform.email.value;

   // developed by willmaster.com
   var i = s_email.indexOf(' ',0);
   while(i > -1)
   {
      s_email = s_email.substring(0,i) + 
         s_email.substring((i + 1),s_email.length);
      i = s_email.indexOf(' ',0);
   }

   document.orderform.email.value = s_email;
   if((s_email.length < 6) ||
      (s_email.indexOf('@',0) < 1) ||
      (s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||
      (s_email.lastIndexOf('@') > (s_email.length - 5)) ||
      (s_email.lastIndexOf('.') > (s_email.length - 3)) ||
      (s_email.lastIndexOf('.') < (s_email.length - 4)) ||
      (s_email.indexOf('..',0) > -1) ||
      (s_email.indexOf('@.',0) > -1) ||
      (s_email.indexOf('.@',0) > -1) ||
      (s_email.indexOf(',',0) > -1))
   {
      alert('The email address "' + s_email + '" is not valid.');
      return false;
   }
   return true;
}