function checktext(str,tx,mi,ma,al,hint) {
Valid = true;
var filter=/^[A-Za-z]*$/
if (!filter.test(str.value) || str.value == "" || str.value.length < mi || str.value.length > ma) {
    for (var i= 1; i< 8; i++) document.getElementById("tx"+i).color = "#2c4e71";
    Valid = false;
	alert(al + '\n____________________________\n\n' + hint);
	str.focus();
    document.getElementById(tx).color = "#E93232";
	}
}

function checknum(str,tx,mi,ma,al,hint) {
Valid = true;
var filter=/^[0-9]*$/
if (!filter.test(str.value) || str.value == "" || str.value.length < mi || str.value.length > ma) {
    for (var i= 1; i< 8; i++) document.getElementById("tx"+i).color = "#2c4e71";
    Valid = false;
	alert(al + '\n____________________________\n\n' + hint);
	str.focus();
    document.getElementById(tx).color = "#E93232";
	}
}

function Validator(theForm) {
//RESET HIGHTLIGHTS
for (var i= 1; i< 8; i++) document.getElementById("tx"+i).color = "#2c4e71";
//CHECK NAMES
checktext(theForm.first_name,"tx1",2,25,"Please correct the name field.","HINT: enter only non-numeric characters.");
if (!Valid)return (false);
checktext(theForm.last_name,"tx2",2,25,"Please correct your last name field.","HINT: enter only non-numeric characters.");
if (!Valid)return (false);

// CHECK EMAIL
Valid = true;
var str = theForm.email.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (!filter.test(str)) {
    Valid = false;
    document.getElementById("tx3").color = "#E93232";
    alert("Please enter a valid email address.");
    theForm.email.focus();
    return (false);
  }

CurrentDate= new Date();
var cYear=CurrentDate.getYear();
var cMonth=CurrentDate.getMonth()+1;
var cDay=CurrentDate.getDate();
Valid = true;
var str = theForm.date.value
var filter=/^\d{2}[\/\s]?\d{2}[\/\s]?\d{4}$/
//Split the date imput into day, month and year.
var a = Number(str.substring(0, str.indexOf('/')));
var b = Number(str.substring(str.indexOf('/') + 1, str.indexOf('/',3)));
var c = Number(str.substring(str.indexOf('/',3) + 1, str.length));

if ((a > 12) || (b > 31) || (!filter.test(str))) {
    Valid = false;
    document.getElementById("tx4").color = "#E93232";
    alert('Please enter a valid date.' + '\n____________________________\n\n' + 'HINT: use the format: mm/dd/yyyy.');
    theForm.date.focus();
    return (false);
} else if ( c < cYear ) {
    Valid = false;
    document.getElementById("tx4").color = "#E93232";
    alert('Please enter a valid year.' + '\n____________________________\n\n' + 'HINT: enter a future date.');
    theForm.date.focus();
    return (false);
} else if ( c == cYear ) {
if ( a < cMonth ) {
    Valid = false;
    document.getElementById("tx4").color = "#E93232";
    alert('Please enter a valid month.' + '\n____________________________\n\n' + 'HINT: enter a future date.');
    theForm.date.focus();
    return (false);
} else if ( a == cMonth ) {
if ( b < cDay ) {
    Valid = false;
    document.getElementById("tx4").color = "#E93232";
    alert('Please enter a valid day.' + '\n____________________________\n\n' + 'HINT: enter a future date.');
    theForm.date.focus();
    return (false);
}
}
}

checktext(theForm.city,"tx5",3,20,"Please correct the city field.","HINT: enter only non-numeric characters.");
if (!Valid)return (false);

  if (theForm.state.value == 0)
  {
    document.getElementById("tx6").color = "#E93232";
    alert("Please select your state.");
    theForm.state.focus();
    return (false);
  }

Valid = true;
var str=theForm.zip
var filter1=/^\d{5}$/
var filter2=/^\d{5}[\-\s]?\d{4}$/
var filter3=/^[A-Z0-9]{3}[\-\s][A-Z0-9]{3}$/
    if (!filter1.test(str.value) && !filter2.test(str.value) && !filter3.test(str.value)) {
    Valid = false;
    document.getElementById("tx7").color = "#E93232";
    alert("Please enter a correct value for the zip field.");
    theForm.zip.focus();
    return (false);
    }else if(filter1.test(str.value) || filter2.test(str.value)) {
    var a = Number(theForm.state.value.substring(0, theForm.state.value.indexOf('_')));
    var b = Number(theForm.state.value.substring(theForm.state.value.indexOf('_') + 1, theForm.state.value.length));
	if ((theForm.zip.value < a) || (theForm.zip.value > b)){
    Valid = false;
    document.getElementById("tx7").color = "#E93232";
    alert("Please enter a correct value for the zip field.");
    theForm.zip.focus();
    return (false);
    }
}

checknum(theForm.home_phone_area,"tx8",3,20,"Please correct the area code field.","HINT: enter only numeric characters.");
if (!Valid)return (false);
checknum(theForm.home_phone,"tx8",7,20,"Please correct the phone number field.","HINT: enter only numeric characters.");
if (!Valid)return (false);
  if (Valid) {
    theForm.Submit.disabled=true;
  }
return (true);
}

function checkdim(theForm,str) {
	var filter=/^[1-9][0-9]*$/
	org1 = window.document.getElementById('dimb1');
	org2 = window.document.getElementById('dimb2');
	org3 = window.document.getElementById('dimb3');
	if ( filter.test(theForm.dima1.value) ) {org1.innerHTML = Number(theForm['dima1'].value)} else { org1.innerHTML = "width";}
	if ( filter.test(theForm.dima2.value) ) {org2.innerHTML = Number(theForm['dima2'].value)} else { org2.innerHTML = "length";}
	if ( filter.test(theForm.dima3.value) ) {org3.innerHTML = Number(theForm['dima3'].value)} else { org3.innerHTML = "height";}
	if ( !filter.test(str.value) ) {
	alert('The value you have entered is invalid' + '\n____________________________\n\n' + 'Hint: Please enter only numeric characters');
	str.value="";
	}
}
