/* checkOrg function used in Premium Support login */
function checkOrg(form1) {
    var numOnly=/\D/;
    if (numOnly.test(form1.org_id.value)) {
     
        var text = "Organization ID must be numbers only."
        alert(text)
        return false
    }
    return true;
}

function checkCN(form1) {
    var numOnly=/\D/;
    if (numOnly.test(form1.cn.value)) {
     
        var text = "Certification Number must be numbers only."
        alert(text)
        return false
    }
    return true;
}

/* checkform function used in Premium Support registration, Getting Started and CrossTrain */

function checkform(form1) {
var text = "";
//if (form1.swtitle && form1.swtitle.selectedIndex == 0) {
   //text += "\nTitle"
//} 
if (form1.firstname) {
if (form1.firstname.value == "") {
text += "\nFirst name"
} else {
   
   var len=form1.firstname.value.length;
   for (var i=1; i <= len; i++) {
      if (form1.firstname.value.charAt(0) == ' ') {
         form1.firstname.value=form1.firstname.value.substring(1,form1.firstname.value.length);    
      } 
        
   }
   if (form1.firstname.value == ""  ) {
      text += "\nFirst Name"
   }    
   
}
}
if (form1.lastname) {
if (form1.lastname.value == "") {
text += "\nLast Name"
} else {
   
   var len=form1.lastname.value.length;
   for (var i=1; i <= len; i++) {
      if (form1.lastname.value.charAt(0) == ' ') {
         form1.lastname.value=form1.lastname.value.substring(1,form1.lastname.value.length);    
      } 
        
   }
   if (form1.lastname.value == ""  ) {
      text += "\nLast name"
   }    
   
}
}
if (form1.email && form1.email.value == "") {
  text += "\nEmail"
}
if (form1.veremail && form1.veremail.value == "") {
  text += "\nRe-type Email"
}
if (form1.email && form1.veremail && form1.email.value !=form1.veremail.value) {
	text += "\nEmail and re-typed Email are not identical"
}
if (form1.job && form1.job.selectedIndex == 0) {
   text += "\nJob Title"
} 
if (form1.org_type && form1.org_type.selectedIndex == 0) {
   text += "\nOrganization Type"
   
}
if (form1.country && form1.country.selectedIndex == 0) {
   text += "\nLocation"
   
} 
var url=location.href;

if (form1.org_type  && form1.org_type.selectedIndex != 0  && url.indexOf('getstart')>0) {
//the validation code now allows for International as an org type; this is the one that is posted to the confirm page if someone selects a location other than US/Canada
   if (form1.org_type.value == 'Public School' || form1.org_type.value == 'School District' || form1.org_type.value == 'Private School' || form1.org_type.value == 'International') {
      form1.action='/getstart/?main=webregver';
   } else {
      if (!confirm('You have selected a private organization.  You will be taken to the Clinical Professionals Registration form for new providers.  If you wish to change your Organization Type, please click the "Cancel" button.')) {
        return false
      } else {  
         form1.action='/prof/main=ffreg';  
         return true;
      }   
   }
   
} 
if (form1.job && form1.job_fillin) {
   var found=0;
   
   for (var i=0; i < form1.job.length; i++) {
         if (form1.job[i].checked) {
            found=1;
         }
   } 
   if (found==0 && form1.job_fillin.value == "") {
      text += "\nJob Title"
   }
} 

if (form1.areacode) {
   if (form1.areacode.value=="") {
	text += "\nArea Code"
   }

}
if (form1.phone) {
   if (form1.phone.value=="") {
	text += "\nPhone"
   }

}

if (form1.org_id ) {
   if (form1.org_id.value=="") {
	text += "\nOrganization ID"
   } else {
    var numOnly=/\D/;
    if (numOnly.test(form1.org_id.value)) {
     
        text += "\nOrganization ID must be numbers only"
    }
   }
}

if (form1.password) {
   if (form1.password.value=="") {
	 text += "\nPassword"
   } else {
     var illegalChars = /\W/;
    
     if (illegalChars.test(form1.password.value)) {
     
        text += "\nPassword contains invalid characters (use letters and numbers only)"
     }
     if (form1.password.value.length< 4 ||form1.password.value.length>8) {
    
		text += "\nPassword must be 4-8 characters"
	}
   }
}

if (form1.verpassword && form1.verpassword.value=="") {
	text += "\nConfirm Password"
}
if (form1.password && form1.verpassword && form1.password.value !=form1.verpassword.value) {
	text += "\nPassword and Confirm Password are not identical"
}
if (form1.contact_method) {
var foundone=false;
for (i=0;i<form1.contact_method.length;i++){
//alert('method '+ form1.contact_method[i].checked);
 if (form1.contact_method[i].checked==true){
   foundone=true;
   break //exist for loop, as target acquired.
 }
} 
if (foundone==false) {
 	text += "\nPreferred method of contact"
}
}


if (form1.accept && !form1.accept.checked) {
	text += "\nAccept Terms of Use"
}


if (!text) {
   if (form1.email && emailCheck(form1.email.value)) {
      // phone check
     if (form1.phone) {
        if (checkInternationalPhone(form1.phone.value)) {
          if (form1.areacode) {
              if (checkInternationalCode(form1.areacode.value)) {
				  	if (form1.phonecntry && form1.phoncntry!="") {
				  	   if (checkInternationalCode(form1.phonecntry.value)) {
				  	      return true;
				  	   } else {
						 alert('Please enter a valid phone country code (all numbers, + and - OK).');
						 return false;	 
					   } 
				  	} else {
				  	   return true;
				  	}   
			   } else {
				 alert('Please enter a valid area code (all numbers).');
				 return false;	 
			   } 
		   }	  
         } else {
           alert('Please enter a valid phone number (7-10 digits, + and - OK).');
           return false;
        }   
     }   
         
   } else {
     return false
   }   
  
} else {
   
   text = "The following fields are required or need attention:" + text   
   alert(text)
   return false
   
}
}

