// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
    error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 35)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng,field) {
var error = "";
  if (strng.length == 0) {
     error = "The "+field+" has not been filled in.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,field) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the "+field+" drop-down list.\n";
    }    
return error;
}    

function checkWholeForm(theForm,subreg) {
    
    var why = "";
    why += checkUsername(theForm.email.value);
    why += isEmpty(theForm.password.value,'Password');
    why += isEmpty(theForm.password2.value,'Retype Pass');
    why += checkDropdown(theForm.squestion.selectedIndex,'Security question');
    why += isEmpty(theForm.sanswer.value,'Security answer');
    if(subreg==0){
        why += isEmpty(theForm.ssn.value,'SSN');
//	    why += isEmpty(theForm.license.value,'License');
        why += isEmpty(theForm.date_born.value,'Birth date');
    }
    why += checkDropdown(theForm.occupation.selectedIndex,'Occupation');
    why += isEmpty(theForm.fname.value,'First name');
    why += isEmpty(theForm.lname.value,'Last name');
    why += isEmpty(theForm.credentials.value,'Credentials');
    why += isEmpty(theForm.address1.value,'Address 1');
    why += isEmpty(theForm.city.value,'City');
    why += checkDropdown(theForm.state.selectedIndex,'State');
    why += isEmpty(theForm.zip.value,'Zip');
    why += checkDropdown(theForm.country.selectedIndex,'Country');
    why += isEmpty(theForm.phone.value,'Phone');
    why += checkEmail(theForm.email1.value);			
    why += isEmpty(theForm.security.value,'Security code');

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkWholeFormPref(theForm) {
    
    var why = "";
    why += checkEmail(theForm.email1.value);
    why += isEmpty(theForm.fname.value,'First name');
    why += isEmpty(theForm.lname.value,'Last name');
    why += isEmpty(theForm.address1.value,'Address 1');
    why += isEmpty(theForm.credentials.value,'Credentials');
    why += isEmpty(theForm.phone.value,'Phone');
    why += checkDropdown(theForm.squestion.selectedIndex,'Security question');
    why += isEmpty(theForm.sanswer.value,'Security answer');
	why += isEmpty(theForm.city.value,'City');
	why += isEmpty(theForm.zip.value,'Zip');
    why += checkDropdown(theForm.state.selectedIndex,'State');
    

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkSurveyForm(theForm,nrq) {
    var why = "";

  for(i=1;i<=nrq;i++){
      var myQ=eval("document.theForm.a"+i);
    
        var val = 0;

	for( j = 0; j < myQ.length; j++ )
	{
	if( myQ[j].checked == true )
	val = myQ[j].value;
	}
	if(!val)
	why +='No answer for question '+i+'.';
	why += checkRadio(val);			
	}

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkSurveyForm1022(theForm,nrq) {
    var why = "";

  for(i=1;i<=nrq;i++){
      var myQ=eval("document.theForm.a"+i);
    
        var val = 0;

	for( j = 0; j < myQ.length; j++ )
	{
	if( myQ[j].checked == true )
	val = myQ[j].value;
	}
	
	if(!val)
	why +='No answer for question '+i+'.';

	why += checkRadio(val);			
	}

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}


function checkWholeFormDi(theForm,subreg) {
    
   var why = "";
    why += checkUsername(theForm.email.value);
    why += isEmpty(theForm.password.value,'Password');
    why += isEmpty(theForm.password2.value,'Retype Pass');
    why += isEmpty(theForm.father_name.value,'Fathers\'s name');
	why += isEmpty(theForm.license.value,'Registration number');
	
    if(subreg==0){
        why += isEmpty(theForm.date_born.value,'Birth date');
    }
	
    why += checkDropdown(theForm.occupation.selectedIndex,'Occupation');
    why += isEmpty(theForm.fname.value,'First name');
    why += isEmpty(theForm.lname.value,'Last name');
    why += isEmpty(theForm.address1.value,'Address 1');
	why += checkEmail(theForm.email1.value,'Email');
    why += isEmpty(theForm.city.value,'City');
    why += checkDropdown(theForm.state.selectedIndex,'State');
    why += isEmpty(theForm.zip.value,'Pin Code');
    why += checkDropdown(theForm.country.selectedIndex,'Country');

    why += isEmpty(theForm.degree.value,'Degree');
	why += isEmpty(theForm.accrediting_school.value,'Accrediting school');

    why += isEmpty(theForm.security.value,'Security code');

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkWholeFormPrefDi(theForm,subreg) {
    
   var why = "";
    why += checkUsername(theForm.email.value);
    why += isEmpty(theForm.father_name.value,'Fathers\'s name');


    why += isEmpty(theForm.fname.value,'First name');
    why += isEmpty(theForm.lname.value,'Last name');
    why += isEmpty(theForm.address1.value,'Address 1');
	why += checkEmail(theForm.email1.value,'Email');
    why += isEmpty(theForm.city.value,'City');
    why += checkDropdown(theForm.state.selectedIndex,'State');
    why += isEmpty(theForm.zip.value,'Pin Code');


    why += isEmpty(theForm.degree.value,'Degree');
	why += isEmpty(theForm.accrediting_school.value,'Accrediting school');



    if (why != "") {
       alert(why);
       return false;
    }
return true;
}
