﻿// JavaScript Document
<!--
var boxCount=0;
var serviceLevelChecked = -1;
var serviceLevel = "";
var selState = "";
var annual = Array("Imperial", "San Bernardino", "Santa Clara", "Solano");
var annualIndex = Array("12", "37", "44", "49");
var quarterly = Array();
var quarterlyIndex = Array();

// Changes the count of the number of regions selected.
function chkCount() {
    var f = document.forms["form2"];
    boxCount = 0;
    for (var i=0;i<60;i++) {
        if (f["CheckBoxList1$" + i].checked) {
            boxCount++;
        }
    }
    return discountPricing();
}

function discountPricing(){
    if(boxCountAlert(boxCount) == false) { // If there is no box count alert, check service levels and price
        if(serviceLevelCheck() == true) {  // If they have selected a service level, calculate the price
            calculateTotals();
            return true;
        }
    }
    return false;
}

function boxCountAlert(myBoxCount) {
    if (myBoxCount == 0) {
        alert('Please select at least one county.');
        return true;
    }else if (myBoxCount > 3) {
        alert('For orders of 4 or more counties, please call us at: (888) 217-8999 for volume discount pricing!');
        return true;
    } else {
        return false;
    }
}

// Function to run after the user clicks on a service level
function checkServiceLevel(){
   var form = window.document.forms[1];
   if(boxCount==0){
      alert('Please select counties above before choosing a Service Level.');
      window.location.href='#top';
      // unselect service level
      for(var i=0; i<form.ServiceLevel.length; i++){
         if(form.ServiceLevel[i].checked){
            form.ServiceLevel[i].checked=false;
         }
      }
      return false;
   }
   discountPricing();
}

// Ensure that the user has selected a service level and sets serviceLevelChecked and serviceLevel variables
function serviceLevelCheck(){
   var form = window.document.forms[1];
   var myReturn = false;
   for(var i=0; i<4; i++){
      if(form.ServiceLevel[i].checked){
        serviceLevelChecked=i;
        serviceLevel = form.ServiceLevel[i].value;
        myReturn = true;
      }
   }
   return myReturn;
}

function calculateTotals(){
   verifyServiceLevel();
   var TaxNoSavings=0;
   var CostNoSavings=0;
   var TaxTotal=0;
   var CostTotal=0;
   var pricePerUnit=new Array(0,0);
   var form = window.document.forms[1];
   selState = form.State.value;

   var tyd = document.forms["form2"].ThreeYearDeal;
   var Discount = 0;
   var Years = 1;
   if (tyd.checked) {
     Years = 3;
     Discount = 0.10;
   }

      // Monthly pricing
      if(serviceLevelChecked==0){
         pricePerUnit[0]=745.00;
         pricePerUnit[1]=120.00;
      }
      // Quarterly pricing
      if(serviceLevelChecked==1){
         pricePerUnit[0]=645.00;
         pricePerUnit[1]=100.00;
      }
      // Semiannual pricing
      if(serviceLevelChecked==2){
         pricePerUnit[0]=525.00;
         pricePerUnit[1]=75.00;
      }
      // Annual pricing
      if(serviceLevelChecked==3){
         pricePerUnit[0]=495.00;
         pricePerUnit[1]=50.00;
      }
      //alert(pricePerUnit[0]);
      //alert(pricePerUnit[1]);
      //alert(boxCount-1);
      CostTotal=eval((pricePerUnit[0]) + ((boxCount-1) * pricePerUnit[1]));
      CostNoSavings=Years * CostTotal;
      if (selState == "ca") {
        TaxNoSavings=eval((CostNoSavings * TaxRate) *100); //multiply by 100 to get rid of decimal
      } else {
        TaxNoSavings=0; //multiply by 100 to get rid of decimal
      }
      CostTotal = Years * CostTotal * (1.00 - Discount);

      //alert(CostTotal);
      if (selState == "ca") {
        TaxTotal=eval((CostTotal * TaxRate) *100); //multiply by 100 to get rid of decimal
      } else {
        TaxTotal=0;
      }
      TaxTotal=Math.round(TaxTotal); //round it to the nearest cent
      TaxTotal=parseInt(TaxTotal); //chop off remaining decimal
      TaxTotal=parseFloat(TaxTotal/100); //replace decimal in the 100ths place


      TaxNoSavings=Math.round(TaxNoSavings); //round it to the nearest cent
      TaxNoSavings=parseInt(TaxNoSavings); //chop off remaining decimal
      TaxNoSavings=parseFloat(TaxNoSavings/100); //replace decimal in the 100ths place

      form.SubTotal.value=fmtPrice(CostTotal);
      form.Tax.value=fmtPrice(parseFloat(TaxTotal));
      form.Total.value=fmtPrice(parseFloat(CostTotal + TaxTotal))
      form.TotalFinal.value=fmtPrice(parseFloat(CostTotal + TaxTotal))

      var SV = fmtPrice(0 + CostNoSavings + TaxNoSavings - CostTotal - TaxTotal);
      //alert("CNS: "+CostNoSavings+" TNS: "+TaxNoSavings+" CT: "+CostTotal+" TT:"+TaxTotal + "SV: " +SV);
      form.savings.value=SV;
      //alert(TaxTotal.indexOf('.'));
}

function verifyServiceLevel() {
  var f = document.forms["form2"];
  var x;
  // 1: Annual Counties:  Santa Clara, San Bernardino, Solano, Imperial

  if (serviceLevel == "") return true;

  // Ensure annual counties are not selected more frequently than annually
  if (serviceLevel != "Annual") {
    for (x = 0; x < annualIndex.length; x++) {
      if (f["CheckBoxList1$" + annualIndex[x]].checked) {
        displayError(annual[x], serviceLevel);
        break;
      }
    }
    // If frequency problem: uncheck selected service level and check Annual
    if (x != annual.length && serviceLevelChecked >= 0 && serviceLevelChecked < f.ServiceLevel.length) {
      f.ServiceLevel[serviceLevelChecked].checked = false;
      f.ServiceLevel[f.ServiceLevel.length-1].checked = true;
      serviceLevelCheck();
      return true;
    }
  }

  if (serviceLevel == "Monthly") {
    for (x = 0; x < quarterlyIndex.length; x++) {
      if (f["CheckBoxList1$" + quarterlyIndex[x]].checked) {
        displayError(quarterly[x], serviceLevel);
        break;
      }
    }
    // If frequency problem: uncheck selected service level and select Quarterly
    if (x != quarterly.length && serviceLevelChecked >= 0 && serviceLevelChecked < f.ServiceLevel.length) {
      f.ServiceLevel[serviceLevelChecked].checked = false;
      f.ServiceLevel[1].checked = true;
      serviceLevelCheck()
      return true;
    }
  }
  return true;
}

function fmtPrice(amount) {
   var i = parseFloat(amount);
   if(isNaN(i)) { i = 0.00; }
   var minus = '';
   i = Math.abs(i);
   i = parseInt((i + .005) * 100);
   i = i / 100;
   s = new String(i);
   if(s.indexOf('.') < 0) { s += '.00'; }
   if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
   s = minus + s;
   return s;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0

  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function finalCheck(){
   if(chkCount() == false) {
       if(serviceLevelCheck() == false) {
           alert('Please select a service level.');
       }
       return false;
   }
   var form = window.document.forms[1];
   if(boxCount==0){
      alert('You have not chosen any counties.');
      window.location.href='#top';
      return false;
   }
   if(!serviceLevelCheck()){
      alert('Please select a service level.');
      window.location.href='#serviceLvl';
      return false;
   }
   calculateTotals();

    MM_validateForm('Name','','R','Address','','R','City','','R','State','','R','County','','R','Zip','','R','Phone','','R','Email','','R','Industry','','R','HowDidYouHearOfUs','','R','SourcesUsed','','R','RisEmail');

   //alert(document.MM_returnValue);
   if(document.MM_returnValue==false){
      return document.MM_returnValue;
   }

   var f = document.forms["form2"];
   if (f.Email.value != f.Email2.value) {
     alert("Your E-mail addresses don't match.");
     return false;
   }

   var paymentChoice=form.PaymentMethod.selectedIndex;
   paymentMethod=form.PaymentMethod.options[paymentChoice].value;
   //alert(paymentMethod);

   // If check payment selected, do redirect
   //if(paymentMethod=='Check')
   //{
      //alert('Thank you for your order. \n A CD Data customer service representative will contact you shortly regarding your order.');
   //   form.submit();
      //window.location="thankyou.asp";
   //}
   //if(paymentMethod=='Credit')
   //{
   //   form.submit();
   //}
}

function checkPaymentMethod(){
   var form = window.document.forms[1];
   var paymentChoice=form.PaymentMethod.selectedIndex;
   paymentMethod=form.PaymentMethod.options[paymentChoice].value;
   //check payment method...if check goto printout page.
   //alert(paymentMethod);
}

function displayError(county, freq) {
  alert("The CD you are ordering for " + county + ", is not available for "+
        freq + " delivery.  Please refer to our Coverage & Features "+
        "information or call us at (888) 217-8999 for more information.");
}


function openEmail() {
  var f = document.forms["form2"];
  if (f.ContactMe.checked) {
    var body = "Please call me regarding additional ParcelQuest Licenses";
    if (f.Phone.value != "") {
      body += " (" + f.Phone.value + ").";
    } else {
      body += ".";
    }
    window.location.href = "mailto:sales@cd-data.com?subject=" +
                           escape("Additional Licenses") + "&body=" +
                           escape(body);
  }
}
// -->