/*This file validates the form entries and transfers 
the variables to the php file, where they are entered sql
file. Control then returns to the form page - donateform.php
which sends them to the paypal checkout. This file has a lot of 
repeated code. Could it be shortened?*/

//When the form's record button is clicked

function check_it() {
var fget=document.getElementById("btn1");
fget.onclick=function() {
var complete=true;

//validate first name
var thename=document.form1.first_name.name;
var thenamef=document.form1.first_name.value;
if((thenamef == "") || (thenamef == null)){
alert(thename+" is blank, please enter!");
complete=false;return false;return false;
}
//validate initial
var thename=document.getElementById("init");
var thenamein=thename.value;
var chcode=thenamein.charCodeAt(0);
if((chcode<65)||(chcode>122)||(thenamein.length>1)){
alert("Initial must be one letter of blank. Please reenter!");
complete=false;return false;return false;
}
//validate last name
var thename=document.form1.last_name.name;
var thenamel=document.form1.last_name.value;
if((thenamel == "") || (thenamel == null)){
alert(thename+" is blank, fplease enter!");
complete=false;return false;
}
//validate address1
var thename=document.form1.address1.name;
var thenamead1=document.form1.address1.value;
if((thenamead1 == "") || (thenamead1 == null)){
alert(thename+" is blank, fill it in please!");
complete=false;return false;
}

//validate city
var thename=document.form1.city.name;
var thenamecty=document.form1.city.value;

if((thenamecty == "") || (thenamecty == null)){
alert(thename+" is blank, fill it in please!");
complete=false;return false;
}
for (var i=0;i<thenamecty.length;i++){
var chcode=thenamecty.charCodeAt(i);
if((chcode<65)||(chcode>122)&&(chcode!="-")){
alert("Invalid city name, please re-enter!");
complete=false;return false;
 }
}

//validate state
var thename=document.form1.state.name;
var thenamest=document.form1.state.value;
if((thenamest == "") || (thenamest == null)){
alert("State is blank, please select with arrow!");
complete=false;return false;
}


//validate zip
var thename=document.form1.zip.name;
var thenamezp=document.form1.zip.value;
if((thenamezp == "") || (thenamezp == null)){
alert("Zip is blank, please enter");
complete=false;return false;
}
for (var i=0;i<thenamezp.length;i++){
var chcode=thenamezp.charCodeAt(i);
if((chcode<48)||(chcode>57)||(thenamezp.length!=5)){
alert("Invalid zip, please re-enter!");
complete=false;return false;break;
  }
 }

//validate phone no
var thename=document.form1.phone.name;
var thenameph=document.form1.phone.value;

var cod3=thenameph.charCodeAt(3);
var cod7=thenameph.charCodeAt(7);
if(thenameph.length!=12||cod3!="45"||cod7!="45"){
alert("Invalid phone ,Please use correct format");
complete=false;return false;
}
for (var i=0;i<thenameph.length;i++)
var chcode=thenameph.charCodeAt(i);
if((chcode<48)||(chcode>57)){
if(chcode!=45){
alert("Invalid phone,Please use correct format");
complete=false;return false;
   }
 }


//validate email address
var thename=document.form1.email.name;
var thenameem=document.form1.email.value;
if((thenameem == "") || (thenameem == null)){
alert("Email is blank, please enter!");
complete=false;return false;
}
if((thenameem.indexOf("@")==-1)||(thenameem.indexOf(".")==-1)||(thenameem.length<8)){
alert("Invalid Email, please re-enter!");
complete=false;return false;
}


if(complete==true){
document.form1.action="http://www.patswebpage.com/diocaccess/diocadmin/cflmems.php";
document.form1.method="POST";
}
return complete;
}
}