function isnumber(num) {
	for (i=0; i<parseInt(num.length); i++)
	if (num.charAt(i)>"9" || num.charAt(i)<"0") {
		return false; 
	}
	return true;
}

function onlyspaces(inp) {
	for (i=0; i<inp.length; i++)
	if (inp.charAt(i) != " ") {
		return false; 
	}
	return true;
}

//function al(place) {
//	alert("Please use a valid number.")
//	place.focus();
//	place.select();
//}

function al2(place) {
	place.focus();
	place.select();
}
function al3(place) {
	place.focus();
}

function chkquoteid(a) {
   if (a.id.value == "") {
      alert("The 'Retrieve Quote from this ID#' button was pressed, but no quote ID was entered into the form field next to it. Please enter the quote ID you want to retrieve and try again. NOTE: If you are trying to get a new quote or place an order for quantities you entered on this form, press the 'Buy Now' or 'Get a quote' button at the bottom of this page instead of the 'Retrieve Quote from this ID#' button.");
   }
   else {
      a.submit();
   }
}

function chkinput(a,x) {
   var totqty = 0;
   for (xid in document.orderform) {
      if (xid.substring(0,5) == "item_") {
         var fld = document.getElementById(xid);
         if ( (!isnumber(fld.value)) && (!onlyspaces(fld.value))) {
            alert("The quantity fields may contain only numbers or be left blank.  You will now be taken back to the field with invalid input.  Please set it to a numeric value or delete its contents.");
            fld.focus();
            fld.select();
            return false;
         }
         else {
            totqty = totqty + fld.value;
         }
      } 
   }
   if ((totqty == 0) && (x == 0)) {
      alert('No quantities have been entered. To order a product, please enter the quantity you wish to order in the form fields labeled "Qty" next to the items you are interested in.');
      return false;
   }
   a.submit()
}

function verifyContactAndBilling(a) {
	if (a.ContactName.value=="") {
		alert("Please use a valid name.")
		al2(a.ContactName);
		return;
	}
	if (a.address1.value=="") {
		alert("Please use a valid address.")
		al2(a.address1);
		return;
	}
	if (a.city.value=="") {
		alert("Please indicate a valid city.")
		al2(a.city);
		return;
	}

	var ct1=a.nation;
	if (ct1.options[ct1.selectedIndex].value=="") {
		alert("Please indicate a valid country.")
		al3(ct1);
		return;
	}

	var st1=a.state;
	if (st1.options[st1.selectedIndex].value=="") {
		if (a.statealt.value=="") {
			if (a.nation.selectedIndex < 4) {
				alert("For U.S. and Canada you must select either a State/Prov or make an entry in the field labeled Other.");
				al3(st1);
				return;
			}
		}
	}
	if (a.zip.value=="") {
		alert("Please indicate a valid zip or postal code.")
		al2(a.zip);
		return;
	}

	if (a.phone.value=="") {
		alert("Please indicate a valid phone number.")
		al2(a.phone);
		return;
	}
	if ((a.email.value=="") || (a.email.value.indexOf("@")==-1) || (a.email.value.length<=6)) {
		alert("Please use a proper e-mail address.")
		al2(a.email);
		return;
	}

	if (a.billEmail.value !="same as above") {
		if ((a.billEmail.value.indexOf("@")==-1) || (a.billEmail.value.length<=6)) {
			alert("Please use a proper e-mail address for the billing contact. Or use 'same as above' (without the quotes) if you do not want to specify a billing contact.")
			al2(a.billEmail);
			return;
		}
	}

	var ct2=a.CCtype;
	if (ct2.options[ct2.selectedIndex].value=="") {
		alert("Please choose a payment method.")
		al3(ct2);
		return;
	}

        if (a.CCtype.options[ct2.selectedIndex].value != "Purchase Order") {
   	   if ((a.CCnumber.value=="") || (a.CCnumber.value.length<=7) || (!isnumber(a.CCnumber.value))) {
		   alert("Please indicate a valid credit card number.")
		   al2(a.CCnumber);
		   return;
           }
	}

	if ((ct2.options[ct2.selectedIndex].value=="Visa") && (a.CCnumber.value.charAt(0) != '4')) {
		alert("You have selected a credit card type of Visa. However the card number entered is not a valid Visa number since it does not begin with a 4. Please change the credit card type or number as needed to correct the issue.");
		al3(ct2);
		return;
	}

	if ((ct2.options[ct2.selectedIndex].value=="MasterCard") && (a.CCnumber.value.charAt(0) != '5')) {
		alert("You have selected a credit card type of MasterCard. However the card number entered is not a valid MasterCard number since it does not begin with a 5. Please change the credit card type or number as needed to correct the issue.");
		al3(ct2);
		return;
	}

	if ((ct2.options[ct2.selectedIndex].value=="American Express") && (a.CCnumber.value.charAt(0) != '3')) {
		alert("You have selected a credit card type of American Express. However the card number entered is not a valid American Express number since it does not begin with a 3. Please change the credit card type or number as needed to correct the issue.");
		al3(ct2);
		return;
	}

        if (a.CCtype.options[ct2.selectedIndex].value != "Purchase Order") {
	   var ct3=a.CCmonth;
	   if (ct3.options[ct3.selectedIndex].value=="") {
		   alert("Please choose a valid expiry month.")
		   al3(ct3);
		   return;
           }
	}

        if (a.CCtype.options[ct2.selectedIndex].value != "Purchase Order") {
	   var ct4=a.CCyear;
	   if (ct4.options[ct4.selectedIndex].value=="") {
		   alert("Please choose a valid expiry year.")
		   al3(ct4);
		   return;
           }
	}

     if (!a.CCadd.checked) {
	if (a.CCadd1.value=="") {
		alert("Please indicate a valid address.")
		al3(a.CCadd1);
		return;
	}			
	if (a.CCcity.value=="") {
		alert("Please indicate a valid city.")
		al2(a.CCcity);
		return;
	}	

	var ct6=a.CCnation;			
	if (ct6.options[ct6.selectedIndex].value=="") {
		alert("Please indicate a valid country.")
		al3(ct6);
		return;
	}
		
	var ct5=a.CCstate;
	if (ct5.options[ct5.selectedIndex].value=="") {
		if (a.CCstateAlt.value=="") {
			if (a.CCnation.selectedIndex < 4) {
				alert("For U.S. and Canada you must select either a State/Prov or make an entry in the field labeled Other.");
				al3(ct5);
				return;
			}
		}
	}
	if (a.CCzip.value=="") {
	alert("Please indicate a valid zip or postal code.")
		al2(a.CCzip);
		return;
	}
  }

  a.submit();
}

function verifyPO(a) {
   var ct2=a.CCtype; 
   if (ct2.options[ct2.selectedIndex].value=="Purchase Order") {
         alert("Note for PO orders: After submitting this form you must fax the approved PO for this order to 301-424-8201.  On the PO please include the email address where you want us to deliver the software electronically.")
   }
}

function autoSetCountry(a) {
   if (a.state.selectedIndex < 54)
      a.nation.selectedIndex = 0
   else if (a.state.selectedIndex > 55)
      a.nation.selectedIndex = 1
}

function autoSetCountryCC(a) {
   if (a.CCstate.selectedIndex < 54)
      a.CCnation.selectedIndex = 0
   else if (a.CCstate.selectedIndex > 55)
      a.CCnation.selectedIndex = 1
}

function findSel(f, st, ct, bst, bct) {
   for (i=0; i< f.length; i++) {
      if (f.options[i].value == st) {
         f.selectedIndex = i
         return;
      }
  }
}

function processSel(f, st, ct, bst, bct, bm, cm, cy) {
   findSel(f.state, st);
   findSel(f.nation, ct);
   findSel(f.CCstate, bst);
   findSel(f.CCnation, bct);
   findSel(f.CCtype, bm);
   findSel(f.CCmonth, cm);
   findSel(f.CCyear, cy);

  if ( (f.CCadd1.value != "") && (f.CCadd1.value != f.address1.value)) {
      document.getElementById("billxtra").style.display="block";
      f.CCadd.checked = false;
   }
}

function clearquantities() {
   for (xid in document.orderform) {
      if (xid.substring(0,5) == "item_")
         document.getElementById(xid).value = "";
   }
}
