<!-- Validate data -->
function DoPrices() {
  var TaxState=document.OrderForm.PState;
  
  if (TaxState.value=="Not") {
    alert("Please answer whether your business has a location in Vermont.");
    return false;
    }
  if (document.OrderForm.CPS_Order.checked || document.OrderForm.OES_Order.checked || document.OrderForm.CAP_Order.checked || document.OrderForm.WMS_Order.checked) 
    if (!document.OrderForm.CD_Order.checked && !document.OrderForm.Express_Order.checked && !document.OrderForm.Electronic_Order.checked) {
      alert("Please choose a Delivery Method, Electronic Download, Prioirty Mail, or Express Mail.");
      return false;
      }
  if (document.OrderForm.CPS_CD.checked || document.OrderForm.OES_CD.checked || document.OrderForm.CAP_CD.checked) {
  	if (!document.OrderForm.Express_Order.checked && !document.OrderForm.CD_Order.checked) {
  		alert("If you desire a printed manual and CD you need to select Priority Mail or Express Mail delivery.");
  		document.OrderForm.CD_Order.focus();
  		document.refresh;
  		return false;
  		}
  	}
  if (document.OrderForm.Express_Order.checked || document.OrderForm.CD_Order.checked) {
	  if (!document.OrderForm.CPS_CD.checked && !document.OrderForm.OES_CD.checked && !document.OrderForm.CAP_CD.checked) {
  		alert("Do not select Priority Mail or Express Mail delivery, if you do not desire a CD and manual.");
  		document.OrderForm.CD_Order.focus();
  		document.refresh;
  		return false;
  		}
  	}
    
  return CalcPrices();
}

function CanICalculate() {
  var TaxState=document.OrderForm.PState;
  if (TaxState.value=="Not") {
//	  alert("Test 0");
    return false;
    }
  if (document.OrderForm.CPS_Order.checked || document.OrderForm.OES_Order.checked || document.OrderForm.CAP_Order.checked || document.OrderForm.WMS_Order.checked)  {
    if (!document.OrderForm.CD_Order.checked && !document.OrderForm.Express_Order.checked && !document.OrderForm.Electronic_Order.checked) {
//		  alert("Test 1");
      return false;
      }
    }
  if (document.OrderForm.CPS_CD.checked || document.OrderForm.OES_CD.checked || document.OrderForm.CAP_CD.checked) {
  	if (!document.OrderForm.Express_Order.checked && !document.OrderForm.CD_Order.checked) {
//		  alert("Test 2");
  		return false;
  		}
  	}
  if (document.OrderForm.Express_Order.checked || document.OrderForm.CD_Order.checked) {
	  if (!document.OrderForm.CPS_CD.checked && !document.OrderForm.OES_CD.checked && !document.OrderForm.CAP_CD.checked) {
//		  alert("Test 3");
  		return false;
  		}
  	}
	return true;	
	}

function checkrequired(which) {
  var pass=true;
  
  if (document.OrderForm.CPS_Order.checked || document.OrderForm.OES_Order.checked || document.OrderForm.CAP_Order.checked || document.OrderForm.WMS_Order.checked) 
    if (!document.OrderForm.CD_Order.checked && !document.OrderForm.Express_Order.checked && !document.OrderForm.Electronic_Order.checked) {
      alert("Please choose a Delivery Method, Electronic Download, Prioirty Mail, or Express Mail.");
      return false;
      }
  if (document.OrderForm.CPS_CD.checked || document.OrderForm.OES_CD.checked || document.OrderForm.CAP_CD.checked) {
  	if (!document.OrderForm.Express_Order.checked && !document.OrderForm.CD_Order.checked) {
  		alert("If you desire a printed manual and CD you need to select Priority Mail or Express Mail delivery.");
  		document.OrderForm.CD_Order.focus();
  		document.refresh;
  		return false;
  		}
  	}
  if (document.OrderForm.Express_Order.checked || document.OrderForm.CD_Order.checked) {
	  if (!document.OrderForm.CPS_CD.checked && !document.OrderForm.OES_CD && !document.OrderForm.CAP_CD) {
  		alert("Do not select Priority Mail or Express Mail delivery, if you do not desire a CD and manual.");
  		document.OrderForm.CD_Order.focus();
  		document.refresh;
  		return false;
  		}
  	}
  if (!CalcPrices()) {
    alert("Please check products to order, delivery state.");
    return false;
    }


  if (!document.OrderForm.LegalTerms.checked) {
    alert("Orders may not be processed until the End User License Agreement is accepted.");
    document.OrderForm.LegalTerms.focus();
    document.refresh;
    return false;
    }

  var tmpName=document.OrderForm.SSS_CUSTOMER_LICENSE;
  if (trim(tmpName.value)=='') {
    alert("Please fill out the Company Name.");
    document.OrderForm.SSS_CUSTOMER_LICENSE.focus();
    document.refresh;
    return false;
  }

  var CpsTmp=document.OrderForm.CPS_Order;
  var OesTmp=document.OrderForm.OES_Order;
  var CapTmp=document.OrderForm.CAP_Order;
  var WmsTmp=document.OrderForm.WMS_Order;
  if (!CpsTmp.checked && !OesTmp.checked && !CapTmp.checked && !WmsTmp.checked) {
  	// Don't need remaining info for trianing only.
  	return true;
    }
  tmpContact=document.OrderForm.SSS_CONTACT;
  if (trim(tmpContact.value)=='') {
    alert("Please fill out the Contact Person.");
    document.OrderForm.SSS_CONTACT.focus();
    document.refresh;
    return false;
    }
  if (!emailCheck(document.OrderForm.SSS_CUSTOMER_EMAIL.value)) {
    document.OrderForm.SSS_CUSTOMER_EMAIL.focus();
    document.refresh;
    return false;
    }
  

  if (CpsTmp.checked) {
    var StateBox=document.OrderForm.SSS_CUSTOMER_STATE;
    if (StateBox.selectedIndex==0 || StateBox.selectedIndex==1) {
      alert("You must select the state for your Certified Payroll Solution forms.");
      document.OrderForm.SSS_CUSTOMER_STATE.focus();
      document.refresh;
      return false;
      }
    }

  var qbVer=document.OrderForm.SSS_CUSTOMER_QBVER;
  var qbVerSlctIdx=qbVer.selectedIndex

  if (qbVerSlctIdx==0) {
    alert('Please select which QuickBooks Version you are using.');
    document.OrderForm.SSS_CUSTOMER_QBVER.focus();
    document.refresh;
    return false;
    }
  if (qbVerSlctIdx==1) {
    if (CapTmp.checked) {
      alert("Construction Application for Payment Solution no longer supports this version of QuickBooks, your order may not be processed.");
      document.OrderForm.SSS_CUSTOMER_QBVER.focus();
      document.refresh;
      return false;
      }
    if (OesTmp.checked) {
      alert("Overtime Entry Solution does not support this version of QuickBooks, your order may not be processed.");
      document.OrderForm.SSS_CUSTOMER_QBVER.focus();
      document.refresh;
      return false;
      }
    if (!CpsTmp.checked) {
      alert("Wage Manager Solution does not support this version of QuickBooks, your order may not be processed.");
      document.OrderForm.SSS_CUSTOMER_QBVER.focus();
      document.refresh;
      return false;
      }
    alert("Certified Payroll Solution will function properly, but Wage Manager Solution will not be operative until you update to a newer version of QuickBooks.");
    }
  if (qbVerSlctIdx==1 || qbVerSlctIdx==2) {
    if (WmsTmp.checked>0) {
      if (CpsTmp.checked) {
        alert("Certified Payroll Solution will function properly, but Wage Manager Solution will not be operative until you update to a newer version of QuickBooks.");
        }
      else {
        alert("Wage Manager Solution does not support this version of QuickBooks, your order may not be processed.");
        document.OrderForm.SSS_CUSTOMER_QBVER.focus();
        document.refresh;
        return false;
        }
     }
    }

  if (CpsTmp.checked || CapTmp.checked) {
    var OfficeBox=document.OrderForm.SSS_CUSTOMER_OFFICEVER;
    if (OfficeBox.selectedIndex==0) {
      alert("Word and Excel are required for the software you are purchasing.  Please verify the version of Word and Excel that you own.");
      document.OrderForm.SSS_CUSTOMER_OFFICEVER.focus();
      document.refresh;
      return false;
      }
    }

  var tmpFound=document.OrderForm.SSS_FOUND;
  if (trim(tmpFound.value)=='') {
    alert("Please fill out how you Found Us. (Internet, Google, Intuit, Friend...)");
    document.OrderForm.SSS_FOUND.focus();
    document.refresh;
    return false;
  }

return true;
}

//function WmsChange() {
//  var WmsTmp=document.OrderForm.WMS_Order;
//  var WmseTmp=document.OrderForm.WMSE_Order;
//  if (WmsTmp.checked && WmseTmp.checked) {
//    WmseTmp.checked = false;
//    document.refresh;
//    }
//  CalcPrices();
//  return true;
//}
//
function PriorityChange() {
  var PriorityTmp=document.OrderForm.CD_Order;
  var ExpressTmp=document.OrderForm.Express_Order;
  if (PriorityTmp.checked && ExpressTmp.checked) {
    ExpressTmp.checked = false;
    document.refresh;
    }
  CalcPrices();
  return true;
}

function ExpressChange() {
  var PriorityTmp=document.OrderForm.CD_Order;
  var ExpressTmp=document.OrderForm.Express_Order;
  if (PriorityTmp.checked && ExpressTmp.checked) {
    PriorityTmp.checked = false;
    document.refresh;
    }
  CalcPrices();
  return true;
}
function CalcPrices() {
  var SubTotAmt = 0.0;
  var TotAmt = 0.0;
  var DisAmt = 0.0;
  var TaxAmt = 0.0;
  var ShipAmt = 0.0;
  var OrderCt = 0;
  var TrainAmt = 0.0;
  var CpsTmp=document.OrderForm.CPS_Order;
  var OesTmp=document.OrderForm.OES_Order;
  var CapTmp=document.OrderForm.CAP_Order;
  var WmsTmp=document.OrderForm.WMS_Order;
  var TaxState=document.OrderForm.PState;
  var OrderAz='';
  var Train1=document.OrderForm.Train_1;
  var Train2=document.OrderForm.Train_2;
  //var Train3=document.OrderForm.Train_3;
  var Train4=document.OrderForm.Train_4;
  var CpsCD=document.OrderForm.CPS_CD;
  var OesCD=document.OrderForm.OES_CD;
  var CapCD=document.OrderForm.CAP_CD;


	if (!CanICalculate()) {
	  document.OrderForm.ShowTotal.value = "Incomplete";
  	document.OrderForm.ShowDisc.value = "Incomplete";
	  document.OrderForm.x_Tax.value = "Incomplete";
  	document.OrderForm.x_Amount.value = "Incomplete";
	  document.refresh;
  	return true;
		}

  if (CpsTmp.checked) {
    OrderAz= 'Cps,'
    OrderCt = OrderCt + 1;
    TotAmt = TotAmt + 375;
    }
  if (CapTmp.checked) {
    OrderAz = OrderAz + 'Cap,'
    OrderCt = OrderCt + 1;
    TotAmt = TotAmt + 250;
    }
  if (OesTmp.checked) {
    OrderAz = OrderAz + 'Oes,'
    OrderCt = OrderCt + 1;
    TotAmt = TotAmt + 250;
    }
  
  if (WmsTmp.checked && !CpsTmp.checked) {
    OrderAz = OrderAz + 'Wms,';
    OrderCt = OrderCt + 1;
    TotAmt = TotAmt + 100;
    }

  if (OrderCt > 1) {
    DisAmt = TotAmt * .1;
    TotAmt = TotAmt - DisAmt;
    }
  var CdObj=document.OrderForm.CD_Order;
  var ExpressObj=document.OrderForm.Express_Order;
  if (CdObj.checked) {
    OrderAz = OrderAz + 'Priority,'
    ShipAmt = ShipAmt + 15.00;
    }
  if (ExpressObj.checked) {
    ShipAmt = ShipAmt + 35.0;
    OrderAz = OrderAz + 'Express,'
    } 
  if (CpsCD.checked) {
    ShipAmt = ShipAmt + 15.00;
    OrderAz = OrderAz + 'CpsCD,'
   }
  if (OesCD.checked) {
    ShipAmt = ShipAmt + 15.00;
    OrderAz = OrderAz + 'OesCD,'
    }
  if (CapCD.checked) {
    ShipAmt = ShipAmt + 15.00;
    OrderAz = OrderAz + 'CapCD,'
    }
  TrainAmt = 0.00;
  if (Train1.checked)
    TrainAmt = 80.00;
  if (Train2.checked) 
  	TrainAmt = TrainAmt + 155.00;
  //if (Train3.checked)
  //	TrainAmt = TrainAmt + 225.00;
  if (Train4.checked)
  	TrainAmt = TrainAmt + 280.00;
//
// Tax Calc
//
  if (TaxState.value=="VT") {
    OrderAz = OrderAz + 'Taxable,'
    TaxAmt = Math.round((TotAmt + ShipAmt) * 6) / 100;
    }
//  else if (TaxState.value=="ME") {
//    OrderAz = OrderAz + 'Taxable,'
//    TaxAmt = Math.round((TotAmt + ShipAmt) * 5) / 100;
//    }
  else {
    TaxAmt = 0;
    }
  SubTotAmt = TotAmt + DisAmt  + ShipAmt + TrainAmt;  
  document.OrderForm.ShowTotal.value = formatCurrencyNum(SubTotAmt);
  document.OrderForm.ShowDisc.value = formatCurrencyNum(DisAmt);
  document.OrderForm.x_Tax.value = formatCurrencyNum(TaxAmt);
  TotAmt = TotAmt + ShipAmt + TaxAmt + TrainAmt;
  document.OrderForm.x_Amount.value = formatCurrencyNum(TotAmt);
  document.OrderForm.SSS_ORDERING.value = OrderAz;
  document.refresh;
  return true;
  }


// ---------------------------------------------
// Format Number like Currency
// ---------------------------------------------
function formatCurrencyNum(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
          num.substring(num.length-(4*i+3));
  return ('$' + num + '.' + cents);
}

// ---------------------------------------------
// Validate Email
// ---------------------------------------------
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|COM|NET|Com|Net|ORG|EDU|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("The email username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("The email domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {
  // user is not valid
  alert("The email username doesn't seem to be valid.");
  return false;
  }

// if the e-mail address is at an IP address (as opposed to 
// a symbolic host name) make sure the IP address is valid. 
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
  // this is an IP address
  for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
      alert("Email destination IP address is invalid!");
      return false;
      }
    }
  return true;
  }

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
  if (domArr[i].search(atomPat)==-1) {
    alert("The email domain name does not seem to be valid.");
    return false;
    }
  }

// domain name seems valid, but now make sure that it ends in a
// known top-level domain (like com, edu, gov) or a two-letter word,
// representing country (uk, nl), and that there's a hostname preceding 
// the domain or country. 
if (checkTLD && domArr[domArr.length-1].length!=2 
&&  domArr[domArr.length-1].search(knownDomsPat)==-1) {
  alert("The email address must end in a well-known domain or two letter " + "country.");
  return false;
  }

// Make sure there's a host name preceding the domain.
if (len<2) {
  alert("This email address is missing a hostname!");
  return false;
  }

// If we've gotten this far, everything's valid!
return true;
}

function putFocus(formInst, elementInst) {
  if (document.forms.length > 0) {
   document.forms[formInst].elements[elementInst].focus();
  }
 }


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


