// JavaScript, form, and PHP coded by Nikola Vrtis and Nicholas Vrtis, vrtisjobs@vrtisworks.com, www.vrtisworks.com
// includes an email check script from Sandeep V. Tamhankar (stamhankar@hotmail.com) on javascript.internet.com

function Validate(njv) {

//Everybody needs to have both first & last names
if (document.form1.first_name.value == "" || document.form1.last_name.value == "") {
    alert("Please provide both your first and last name.");
    return false;
}
//email address is required.
var email = document.form1.email.value;
if (email != "") {
    if (!emailCheck (email)) {
        return false;   //Poorly formed email (alert already given)
    }
} else {
    alert ("Please enter an email address.");
    return false;
}
var phone = document.form1.phone.value;
if (phone != "") {
    //Check the phone number - if they submitted one...
    if (!phoneCheck (phone)) {
        return false;       //Poorly formed phone number (alert already given)
    }
}
if (document.form1.address1.value == "") {
	alert("Please type in your street address.");
	return false;
}
//address2 is optional...
if (document.form1.city.value == "") {
	alert("Please type in your city.");
	return false;
}
//Need to see which (if any) shipping type is checked...
var stradios=document.form1.shipping_type1;
var idx=-1;
for (i=0; i<stradios.length; i++) {
    if (stradios[i].checked) {
        idx=i;
        break;
    }
}
if (idx<0) {
    alert("Please select a shipping type.");
    return false;
}
if (stradios[idx].value=="dom") {
    //US needs state and zip selected....
    if (document.form1.state.options[document.form1.state.selectedIndex].value == "") {
	   alert("Please select your state.");
	   return false;
    }
    var z=document.form1.zip.value;
    if (z == "") {
        alert("Please type in your Zip code.");
        return false;
    }
    if (z.length != 5) {
        alert("Zip code must be 5 digits.");
        return false;
    }
    var valid = "0123456789";
    for (var i=0; i < z.length; i++) {
        temp = "" + z.substring(i, i+1);
        if (valid.indexOf(temp) == "-1") {
            alert("Zip code should only have digits.");
            return false;
        }
    }
}
if (stradios[idx].value=="int") {
    //International needs country selected
    if (document.form1.country.options[document.form1.country.selectedIndex].value == "") {
       alert("Please select your country.");
       return false;
    }
    if (document.form1.state.value=="") {
        alert("Please enter your Province/State.");
        return false;
    }
}
//Passed all edits.. let the submit go through
return true;
}
//These 'limit' functions are just to keep catalog.js happy....
function limitDomShipMeths(a,b,c) {
    return false;
}
function limitIntShipMeths(a,b,c) {
    return false;
}
function phoneCheck (phoneStr) {
   var valid = "0123456789";
   var skips = "()-. ";
   var testPhone = "";
   var temp = "";
   var chrsOK = 0;
   for (var i=0; i<phoneStr.length; i++) {	//Strip out all non-numerics
     temp = phoneStr.substring(i, i+1);
     if (skips.indexOf(temp) != -1) {
     } else {
        if (valid.indexOf(temp) != -1) {
        testPhone = testPhone + temp;
        } else {
            chrsOK=1;   //wasn't one of the allowable "junk" and wasn't numeric.
        }
     }
   }
   if (chrsOK == 1) {
       alert("Your phone number has invalid characters, please correct");
       return false;
   }
   if (phoneStr.substring(0,1) == "0") {
       return true;     //0 at start means international.. so any length is OK.
   }
   if (testPhone.substring(0,1) == "1") {   //See if it starts with a '1'
       if (testPhone.length != 11) {        //YES-then it needs to be 1aaaxxxnnnn
           alert("Please correct your phone number");
           return false;}
   } else {
       if (testPhone.length != 10) {        //NO-then it needs to be aaaxxxnnnn
           alert ("Please correct your phone number");
           return false;}
   }
   return true;
}

function emailCheck (emailStr) {
// This script from JavaScript Source!! http://javascript.internet.com
// V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com)
// The following is the list of known TLDs that an e-mail address must end with.
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var checkTLD=1;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
    alert("Please correct your email address.");
    return false;}

var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
  if (user.charCodeAt(i)>127) {
      alert("Please correct your email address.");
      return false; }}
for (i=0; i<domain.length; i++) {
  if (domain.charCodeAt(i)>127) {
    alert("Please correct your email address.");
    return false; }}
if (user.match(userPat)==null) {
  alert("Please correct your email address.");
  return false; }

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
  for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
      alert("Please correct your email address.");
      return false; }} }

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("Please correct your email address.");
  return false; }}
if (checkTLD && domArr[domArr.length-1].length!=2 && 
  domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("Please correct your email address.");
    return false; }
if (len<2) {
  alert("Please correct your email address.");
  return false; }

return true;
}

