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< 9; i++) document.getElementById("tx"+i).color = "#2c4e71";
//CHECK NAMES
checktext(theForm.first_name,"tx1",3,20,"Please correct the name field.","HINT: enter only non-numeric characters.");
if (!Valid)return (false);
checktext(theForm.last_name,"tx2",3,20,"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);
    }
}
//Validate the phone fields.
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);
//Validate the Greece destination fields.
var filter=/^[0-9]*$/
Valid = true;
if (theForm.country.value == "None Selected") {
    document.getElementById("tx9").color = "#E93232";
    alert("Please select a destination location in Greece.");
    theForm.country.focus();
	Valid = false;
    return (false);
}

var str= Number(theForm.hvol.value);
if (str < 1) {
alert('You have not entred any boxes or your boxes are too small.' + '\n____________________________\n\n' + 'HINT: Make sure the box units that you have entered are in inches.');
theForm.dim1.focus();
Valid = false;
return (false);
}

if (!theForm.agree.checked) {
alert('You have not agreed to our terms and conditions.');
theForm.agree.focus();
Valid = false;
return (false);
}
theForm.Submit.disabled=true;
return (true);
}

function Validate(theForm) {
Valid = true;
//Validate the calculator fields
for (var i= 1; i< 5; i++) {
var filter=/^[\.0-9]*$/
var filterb=/\.{2}/
var str=theForm['dim'+i];
if (!filter.test(str.value) || filterb.test(str.value) || str.value == "" || str.value == "0") {
alert("Please enter a valid value");
theForm('dim'+i).focus();
Valid = false;
return (false);
}
}
var str= theForm.dim1.value*theForm.dim2.value*theForm.dim3.value*theForm.dim4.value;
if (str < 0.001) {
alert("The values you have entered are too small");
theForm.dim1.focus();
Valid = false;
return (false);
}
var str= (theForm.dim1.value*theForm.dim2.value*theForm.dim3.value*theForm.dim4.value/1728)+ Number(theForm.hvol.value);
if (str > 9.3) {
alert('Your boxes have exceeted the maximum volume of 9.3 CF.' + '\n____________________________\n\n' + 'HINT: Select a different combination of boxes or' + '\n' + 'use our Greece Form instead.');
theForm.dim1.focus();
Valid = false;
return (false);
}
}

function clearlist(tbox,fbox) {
for(var i=0; i<tbox.options.length; i++) {
tbox.options[i].value = "";
tbox.options[i].text = "";
}
tbox.options.length = 0;
fbox.Remove1.disabled = true;
fbox.Clear1.disabled = true;
fbox.hlist.value= "";
fbox.hvol.value= 0;
window.document.getElementById('tvol').innerHTML = fbox.hvol.value;
}

function format(theForm){
var filter=/^[\.1-9][\.0-9]*$/
formated = true;
for (var i= 1; i< 5; i++) {
var str=theForm['dim'+i];
if (!filter.test(str.value)) {
str.value = Number(str.value.substring(1, str.value.length));
formated = false;
}
}
}

function move(tbox,fbox) {
Validate(fbox);
if(Valid){
format(fbox);
var no = new Option();
var volume = new Option();
volume.value = Number(fbox.dim1.value*fbox.dim2.value*fbox.dim3.value*fbox.dim4.value/1728);
if (volume.value.length >5) volume.value = volume.value.substring(0,5);
no.value = '( ' + fbox.dim4.value + ' )' + '-( ' + fbox.dim1.value + '" x ' + fbox.dim2.value + '" x ' + fbox.dim3.value + '" )' + '-( ' + volume.value + ' CF )';
no.text = no.value;
tbox.options[tbox.options.length] = no;
fbox.dim1.value = 0;
fbox.dim2.value = 0;
fbox.dim3.value = 0;
fbox.dim4.value = 1;
//Update Hidden Fields
fbox.hlist.value= "";
for(var i=0; i<tbox.options.length; i++) {
fbox.hlist.value = fbox.hlist.value + "<br>" + tbox.options[i].text;
}
fbox.hvol.value= 0;
for(var i=0; i<tbox.options.length; i++) {
fbox.hvol.value = Number(fbox.hvol.value) + Number(tbox.options[i].text.substring(tbox.options[i].text.lastIndexOf('(')+1, tbox.options[i].text.lastIndexOf(')')-4));
}
if (fbox.hvol.value.length >5) fbox.hvol.value = fbox.hvol.value.substring(0,5);
window.document.getElementById('tvol').innerHTML = fbox.hvol.value;
fbox.Remove1.disabled = false;
fbox.Clear1.disabled = false;
}
}

function remove(tbox,fbox) {
for(var i=0; i<tbox.options.length; i++) {
if(tbox.options[i].selected && tbox.options[i].value != "") {
tbox.options[i].value = "";
tbox.options[i].text = "";
   }
}
BumpUp(tbox,fbox);
if (tbox.options.length == 0) {
fbox.Remove1.disabled = true;
fbox.Clear1.disabled = true;
fbox.hlist.value= "";
fbox.hvol.value= 0;
window.document.getElementById('tvol').innerHTML = fbox.hvol.value;
	}
}

function BumpUp(tbox,fbox)  {
for(var i=0; i<tbox.options.length; i++) {
if(tbox.options[i].value == "")  {
for(var j=i; j<tbox.options.length-1; j++)  {
tbox.options[j].value = tbox.options[j+1].value;
tbox.options[j].text = tbox.options[j+1].text;
}
var ln = i;
fbox.hlist.value= "";
for(var i=0; i<tbox.options.length; i++) {
fbox.hlist.value = fbox.hlist.value + "<br>" + tbox.options[i].text;
}
fbox.hvol.value= "0";
for(var i=0; i<tbox.options.length-1; i++) {
fbox.hvol.value = Number(fbox.hvol.value) + Number(tbox.options[i].text.substring(tbox.options[i].text.lastIndexOf('(')+1, tbox.options[i].text.lastIndexOf(')')-4));
}
window.document.getElementById('tvol').innerHTML = fbox.hvol.value;
break;
   }
}
if(ln < tbox.options.length)  {
tbox.options.length -= 1;
BumpUp(tbox);
   }
}
