/*****trim*****
this function trims
excess white space from
the front of the string,
and the back of a string.
This is a destructive function
s->s*
*/
function trim(s) {
   //trim the front
   for(var i=0;i<s.length;) {
      if(s.charAt(i)==" ") s=s.substring(i+1,s.length);
      else break;
	 }
	 //trim the back
	 for(var i=s.length-1;i>=0;i=s.length-1) {
	    if(s.charAt(i)==" ") s=s.substring(0,i);
		     else break;
	  }
    return s;
}

/*****trimElements*****
trimeElements loops through the 
form elements trimming each one.
This is a destructive function
(ie the form elements get changed)
*/
function trimElements(){
   for(x=0;x<document.newuser.length;x++){
	    document.newuser.elements[x].value=trim(document.newuser.elements[x].value);
	 }
}


//global variable 
//so that the error messages
//can get appended to it
var errorsmsg="";

/*****showerror*****
appends "field" to the current
list of error messages.  This
is a bulleted list
*/
//Pops up an alert message and sets
function showerror(field){
		errorsmsg+="<li>Please enter "+field+".</li>"
}			

/*****displayerrors*****												 
opens a new pop up window
displaying the list of errors
that need to be fixed in order for
the form to be submitted correctly
*/
function displayerrors(){
   if(errorsmsg!=""){
	   var header="Please correct the following mistake(s):<ul>"; 
     var footer="</ul><br><center>"+
		 "<a href=\"\" onClick=\"javascript:self.close(); \">Revise Form</a></center><br>";
	   DispWin=window.open('','NewWin','toolbar=no, status=no,width=300,height=300');
		 DispWin.document.open();
 		 DispWin.document.write(header+errorsmsg+footer);
 		 DispWin.document.close();
		 errorsmsg="";
	}
}


/*****isFieldsFull*****
checks to make sure that there is atleast
one character in the *required* fields
*/
function isFieldsFull(){
   var reqfield;
	 var truthval=true;
   if(document.newuser.last_name.value.length < 1){
	    reqfield="a last name";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.first_name.value.length < 1){
	    reqfield="a first name";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.city.value.length < 1){
	    reqfield="a city";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.state_province.value.length < 1){
	    reqfield="a state";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.postal_code.value.length < 1){
	    reqfield="a postal code";
			showerror(reqfield);
			truthval=false;
	 }
 	 if(document.newuser.country.value.length < 1){
	    reqfield="a country";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.email_address.value.length < 1){
	    reqfield="an email address";
			showerror(reqfield);
			truthval=false;
	 }
   if(document.newuser.userid.value.length < 1){
	    reqfield="a UserID";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.userpass.value.length < 1){
	    reqfield="a password";
			showerror(reqfield);
			truthval=false;
	 }
	 if(document.newuser.verify_userpass.value.length < 1){
	    reqfield="a confirmation password";
			showerror(reqfield);
			truthval=false;
	 }
   return truthval;
}

/*****checkRequired*****
checks to make sure that there is something
present in the required fields.  If there
is not, a window pops up with the corresponding
errors.  Returns true if all is OK, false otherwise.
*/
function checkRequired(){
   if(isFieldsFull()) return true;
	 else {
	    displayerrors();
			return false;
	} 
}

function checkPasswords(){
   var value=(document.newuser.userpass.value==document.newuser.verify_userpass.value);
	 if(value==true) return true;
	 else{
	    alert("Your Passwords do not match.");
      return false;
   }
}

function checkNewAccount(){
   return (checkRequired()&&checkPasswords());
}

/*****AllUpper*****
changes all of the form elements to upper
case except the userid and both passwords.
*/
function AllUpper(){
   //temporarily copy form elements that
   //we want to keep in the proper case
   //this will allow us to do a simple 
   //loop with ToUpperCase()
   var tempuserid, temppassword,tempvpassword;
   tempuserid=document.newuser.userid.value;
   temppassword=document.newuser.userpass.value;
   tempvpassword=document.newuser.verify_userpass.value;
   for(x=0;x<document.newuser.length;x++){
      document.newuser.elements[x].value=document.newuser.elements[x].value.toUpperCase();
   }
   //copy form elements back to original place
   document.newuser.userid.value=tempuserid;
   document.newuser.userpass.value=temppassword;
   document.newuser.verify_userpass.value=tempvpassword;
}

function recover(x){
 trim(x.value);
}

