//--------------------------------------------------------//
// Multiple Functions to handle element OnFocus event
function UnameFocus() 
 {
   document.getElementById('username').style.backgroundColor = '#FFF9EC'; //Make the textbox color lightyellow
   document.getElementById('lbluname').style.color = '#555555'; //Make the label text color black
   document.getElementById('lbluname').innerHTML = 'Username:'; //On error raised textbox focus, assign label default text 
   document.getElementById('check').style.display='block';
}
function UnameFocus() 
 {
   document.getElementById('username').style.backgroundColor = '#FFF9EC'; //Make the textbox color lightyellow
   document.getElementById('lbluname').style.color = '#555555'; //Make the label text color black
   document.getElementById('lbluname').innerHTML = 'Username:'; //On error raised textbox focus, assign label default text 
   document.getElementById('check').style.display='block';
}
function PassWordFocus1() 
 {
   document.getElementById('password').style.backgroundColor = '#FFF9EC'; 
   document.getElementById('lblpass').style.color = '#555555';
   document.getElementById('lblpass').innerHTML = 'Password:';
}
function RetypePassFocus() 
 {
   document.getElementById('repass').style.backgroundColor = '#FFF9EC'; 
   document.getElementById('lblretype').style.color = '#555555';
   document.getElementById('lblretype').innerHTML = 'Re-Type Pass:';
   document.getElementById('lblpass').style.color = '#555555';
   document.getElementById('lblpass').innerHTML = 'Password:';
}
function UsernameFocus() 
 {
   document.getElementById('username').style.backgroundColor='#FFF9EC';
   document.getElementById('ulusername').style.color = '#555555';
   document.getElementById('ulusername').innerHTML = 'Username:';
}

function FUnameFocus() 
 {
   document.getElementById('fname').style.backgroundColor='#FFF9EC';
   document.getElementById('ufname').style.color = '#555555';
   document.getElementById('ufname').innerHTML = 'First name:';
}
function LUnameFocus() 
 {
   document.getElementById('lname').style.backgroundColor='#FFF9EC';
   document.getElementById('ulname').style.color = '#555555';
   document.getElementById('ulname').innerHTML = 'Last name:';
}
function FUageFocus() 
 {
   document.getElementById('age').style.backgroundColor='#FFF9EC';
   document.getElementById('ulage').style.color = '#555555';
   document.getElementById('ulage').innerHTML = 'Age:';
}
function UaddressFocus() 
 {
   document.getElementById('address').style.backgroundColor='#FFF9EC';
   document.getElementById('staddress').style.color = '#555555';
   document.getElementById('staddress').innerHTML = 'Address:';
}
function UCityFocus() 
 {
   document.getElementById('city').style.backgroundColor='#FFF9EC';
   document.getElementById('usercity').style.color = '#555555';
   document.getElementById('usercity').innerHTML = 'City:';
}
function UzipFocus() 
 {
   document.getElementById('zipcode').style.backgroundColor='#FFF9EC';
   document.getElementById('userzip').style.color = '#555555';
   document.getElementById('userzip').innerHTML = 'Zip/Postal code:';
}
function UemailFocus() 
 {
   document.getElementById('email').style.backgroundColor='#FFF9EC';
   document.getElementById('ulemail').style.color = '#555555';
   document.getElementById('ulemail').innerHTML = 'Email Address:';
   //document.getElementById('Echeck').style.display='block';
}
function UTelPhoneFocus() 
 {
   document.getElementById('telephone').style.backgroundColor='#FFF9EC';
   document.getElementById('ulphone').style.color = '#555555';
   document.getElementById('ulphone').innerHTML = 'Telephone:';
}
//--------------------------------------------------------//

//--------------------------------------------------------//
// Handle User Registration Form Validation
function RegValidate(RegForm) 
 {
// Declare Variables - get the textbox value and assign it into the variable
 var username = RegForm.elements['username'].value; 
 var Email = RegForm.elements['email'].value; 
 var Pass = RegForm.elements['password'].value;
 var RepeatPass = RegForm.elements['repass'].value;
 var FirstName = RegForm.elements['fname'].value;
 var LastName = RegForm.elements['lname'].value;
 //var CheckAge = RegForm.elements['age'].value;
 var StreeAddress = RegForm.elements['address'].value;
 //var CityName = RegForm.elements['city'].value;
 var ZipCode = RegForm.elements['zipcode'].value;
// If the first five required fields are empty, then do the textbox color magic
if (username=="" && Email=="" &&Pass == "" && RepeatPass =="" && FirstName =="" && LastName =="") 
  {
      // Change the background color of the textbox,border as well as label text
     document.getElementById('username').style.backgroundColor='#FFF4F4'; 
     document.getElementById('username').style.border = '1px solid #CC0000';
     document.getElementById('ulusername').style.color = '#CC0000';
     document.getElementById('ulusername').innerHTML = 'Empty Field:';

	document.getElementById('email').style.backgroundColor='#FFF4F4'; 
     document.getElementById('email').style.border = '1px solid #CC0000';
     document.getElementById('ulemail').style.color = '#CC0000';
     document.getElementById('ulemail').innerHTML = 'Empty Field:';
     document.getElementById('password').style.backgroundColor='#FFF4F4'; 
     document.getElementById('password').style.border = '1px solid #CC0000';
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Empty Field:';
     document.getElementById('repass').style.backgroundColor='#FFF4F4';
     document.getElementById('repass').style.border = '1px solid #CC0000'; 
     document.getElementById('lblretype').style.color = '#CC0000';
     document.getElementById('lblretype').innerHTML = 'Empty Field:';
     document.getElementById('fname').style.backgroundColor='#FFF4F4';
     document.getElementById('fname').style.border = '1px solid #CC0000'; 
     document.getElementById('ufname').style.color = '#CC0000';
     document.getElementById('ufname').innerHTML = 'Empty Field:';
     document.getElementById('lname').style.backgroundColor='#FFF4F4';
     document.getElementById('lname').style.border = '1px solid #CC0000'; 
     document.getElementById('ulname').style.color = '#CC0000';
     document.getElementById('ulname').innerHTML = 'Empty Field:';

     alert("Please fill in the empty fields.\n-Username\n- Email\n- Password\n- Password 2\n- Firstname\n- Lastname");
     document.getElementById('username').focus();
	 return false;
}
// Username
 if (username == "")
  {
     alert("You must enter a Username");   
     document.getElementById('username').style.backgroundColor='#FFF4F4'; 
     document.getElementById('username').style.border = '1px solid #CC0000';
	 document.getElementById('username').focus();
     document.getElementById('ulusername').style.color = '#CC0000';
     document.getElementById('ulusername').innerHTML = 'Empty Field:';
     return false;
  }
else if (username.length < 6)
 {
     alert ("The password is too short.\n Please enter a username from 6 to 10 characters.");
     document.getElementById('username').style.backgroundColor='#FFF4F4';
     document.getElementById('username').style.border = '1px solid #CC0000';
	 document.getElementById('username').focus();
     document.getElementById('ulusername').style.color = '#CC0000';
     document.getElementById('ulusername').innerHTML = 'Too Short:';
     return false;
}

// Password
 if (Pass == "")
  {
     alert("You must enter a password");   
     document.getElementById('password').style.backgroundColor='#FFF4F4'; 
     document.getElementById('password').style.border = '1px solid #CC0000';
	 document.getElementById('password').focus();
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Empty Field:';
     return false;
  }
else if (Pass.length < 6)
 {
     alert ("The password is too short.\n Please enter a password from 6 to 10 characters.");
     document.getElementById('password').style.backgroundColor='#FFF4F4';
     document.getElementById('password').style.border = '1px solid #CC0000';
	 document.getElementById('password').focus();
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Too Short:';
     return false;
}
else if (Pass.length > 10)
 {
     alert ("The password is too long.\n Please enter a password from 6 to 10 characters.");
     document.getElementById('password').style.backgroundColor='#FFF4F4';
     RegForm.elements['password'].style.border = '1px solid #CC0000';
	 document.getElementById('password').focus();
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Too Long:';
     return false;
}
//Check for illegal characters in password - allow only letters and numbers
var illegalCharsPass =/[\W_]/; 
if (illegalCharsPass.test(Pass)) 
  {
     alert ("The password contains illegal characters.\n Please try again.");
     document.getElementById('password').style.backgroundColor='#FFF4F4';
     document.getElementById('password').style.border = '1px solid #CC0000';
	 document.getElementById('password').focus();
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Illegal Char:';
     return false;
} 
// Repeat Password
 if (RepeatPass == "")
  {
     alert("Please repeat the password");   
     document.getElementById('repass').style.backgroundColor='#FFF4F4';
     document.getElementById('repass').style.border = '1px solid #CC0000'; 
	 document.getElementById('repass').focus();
     document.getElementById('lblretype').style.color = '#CC0000';
     document.getElementById('lblretype').innerHTML = 'Empty:';
     return false;
 }
 if (RepeatPass != Pass)
  {
     alert("Password did not match.\n Please try again");   
     document.getElementById('repass').style.backgroundColor='#FFF4F4';
     document.getElementById('repass').style.border = '1px solid #CC0000'; 
	 document.getElementById('repass').focus();
     document.getElementById('password').style.border = '1px solid #CC0000';
     document.getElementById('password').style.backgroundColor='#FFF4F4';
     document.getElementById('lblpass').style.color = '#CC0000';
     document.getElementById('lblpass').innerHTML = 'Did not Match:';
     document.getElementById('lblretype').style.color = '#CC0000';
     document.getElementById('lblretype').innerHTML = 'Did not Match:';
     return false;
 }
// Email
var UserEMail = RegForm.elements['email'].value;
 if (UserEMail == "")
  {
     alert("You must enter an email address");
     document.getElementById('email').style.backgroundColor='#FFF4F4'; 
     document.getElementById('email').style.border = '1px solid #CC0000';
     document.getElementById('ulemail').style.color = '#CC0000';
     document.getElementById('ulemail').innerHTML = 'Empty Field:';
     return false;
  }
else if (!(UserEMail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1))
 {
     alert("E-mail address is not valid.\n Please enter a valid email address.");
     document.getElementById('email').style.backgroundColor='#FFF4F4'; 
     document.getElementById('email').style.border = '1px solid #CC0000';
	 document.getElementById('email').focus();
     document.getElementById('ulemail').style.color = '#CC0000';
     document.getElementById('ulemail').innerHTML = 'Invalid Email:';
    return false ;
}

// First name
 if (FirstName == "")
  {
     alert("You must enter your name");
     document.getElementById('fname').style.backgroundColor='#FFF4F4';
     document.getElementById('fname').style.border = '1px solid #CC0000'; 
	 document.getElementById('fname').focus();
     document.getElementById('ufname').style.color = '#CC0000';
     document.getElementById('ufname').innerHTML = 'Empty Field:';
     return false;
  }
// Allow only letters 
else if (FirstName.search(/^[A-Z ]+$/i) == -1) 
  {
     alert ("Your name contained numbers. Only alphabetic characters are allowed..\n Please try again.");
     document.getElementById('fname').style.backgroundColor='#FFF4F4';
     document.getElementById('fname').style.border = '1px solid #CC0000'; 
	 document.getElementById('fname').focus();
     document.getElementById('ufname').style.color = '#CC0000';
     document.getElementById('ufname').innerHTML = 'Illegal Char:';
     return false;
} 
else if (FirstName.length < 3)
{
     alert ("The first name is too short.\n Please enter a first name from 3 characters.");
     document.getElementById('fname').style.backgroundColor='#FFF4F4';
     document.getElementById('fname').style.border = '1px solid #CC0000'; 
	 document.getElementById('fname').focus();
     document.getElementById('ufname').style.color = '#CC0000';
     document.getElementById('ufname').innerHTML = 'Illegal Char:';
     return false;
}
// Last name
 if (LastName == "")
  {
     alert("You must enter your last name");
     document.getElementById('lname').style.backgroundColor='#FFF4F4';
     document.getElementById('lname').style.border = '1px solid #CC0000'; 
	 document.getElementById('lname').focus();
     document.getElementById('ulname').style.color = '#CC0000';
     document.getElementById('ulname').innerHTML = 'Empty Field:';
     return false;
  }
// Allow only letters 
else if (LastName.search(/^[A-Z ]+$/i) == -1) 
  {
     alert ("Your last name contained numbers. Only alphabetic characters are allowed..\n Please try again.");
     document.getElementById('lname').style.backgroundColor='#FFF4F4';
     document.getElementById('lname').style.border = '1px solid #CC0000'; 
	 document.getElementById('lname').focus();
     document.getElementById('ulname').style.color = '#CC0000';
     document.getElementById('ulname').innerHTML = 'Illegal Char:';
     return false;
} 
else if (LastName.length < 3)
{
     alert ("The last name is too short.\n Please enter a last name from 3 characters.");
     document.getElementById('lname').style.backgroundColor='#FFF4F4';
     document.getElementById('lname').style.border = '1px solid #CC0000'; 
	 document.getElementById('lname').focus();
     document.getElementById('ulname').style.color = '#CC0000';
     document.getElementById('ulname').innerHTML = 'Illegal Char:';
     return false;
}
// Gender
 var SexGender = RegForm.elements['gender'].value;
 if (SexGender == "")
  {
     alert("You must select a gender/sex");
     return false;
 }
// Street Address
 if (StreeAddress == "")
  {
     alert("You must enter your address");
     document.getElementById('address').style.backgroundColor='#FFF4F4'; 
     document.getElementById('address').style.border = '1px solid #CC0000';
	 document.getElementById('address').focus();
     document.getElementById('staddress').style.color = '#CC0000';
     document.getElementById('staddress').innerHTML = 'Empty Field:';
     return false;
  }
// State
/*
var CheckState = RegForm.elements['state'].value;
 if (CheckState == "")
  {
     alert("You must enter a state");
     return false;
  }
// Zipcode

 if (ZipCode == "")
  {
     alert("You must enter a zipcode");
     document.getElementById('zipcode').style.backgroundColor='#FFF4F4'; 
     document.getElementById('zipcode').style.border = '1px solid #CC0000';
     document.getElementById('userzip').style.color = '#CC0000';
     document.getElementById('userzip').innerHTML = 'Empty Field:';
     return false;
  }
*/if (ZipCode.length > 11)
 {
     alert ("The zipcode is too long.\n Please enter zipcode i.e. 14156 or 90650-13134");
     document.getElementById('zipcode').style.backgroundColor='#FFF4F4'; 
     document.getElementById('zipcode').style.border = '1px solid #CC0000';
	 document.getElementById('zipcode').focus();
     document.getElementById('userzip').style.color = '#CC0000';
     document.getElementById('userzip').innerHTML = 'Too Long:';
     return false;
}
//Check zipcode format - 5 digits or  nine digits with hyphen i.e. 90650-7221
if(ZipCode!="")
{
	var ZipFormat = /\d{5}(-\d{5})?/;
	if (!ZipFormat.test(ZipCode)) 
	  {
		 alert ("The postal code contained illegal characters.\n Please enter zipcode i.e. 14156 or 14156-13134");
		 document.getElementById('zipcode').style.backgroundColor='#FFF4F4'; 
		 document.getElementById('zipcode').style.border = '1px solid #CC0000';
		 document.getElementById('zipcode').focus();
		 document.getElementById('userzip').style.color = '#CC0000';
		 document.getElementById('userzip').innerHTML = 'Illegal Char:';
		 return false;
	} 
}
// Telephone Number
 var PhoneNumber = RegForm.elements['telephone'].value;
 if (PhoneNumber == "")
  {
     alert("You must enter a telephone number");
     document.getElementById('telephone').style.backgroundColor='#FFF4F4'; 
     document.getElementById('telephone').style.border = '1px solid #CC0000';
	 document.getElementById('telephone').focus();
     document.getElementById('ulphone').style.color = '#CC0000';
     document.getElementById('ulphone').innerHTML = 'Empty Field:';
     return false;
  }
else if (PhoneNumber.length > 14)
 {
     alert ("The telephone number is too long.\n Please enter a telephone number format 98-21-8895100/98218895100 including area code");
     document.getElementById('telephone').style.backgroundColor='#FFF4F4'; 
     document.getElementById('telephone').style.border = '1px solid #CC0000';
     document.getElementById('ulphone').style.color = '#CC0000';
     document.getElementById('ulphone').innerHTML = 'Too Long:';
     return false;
}
/*Check Telephone Number format - format 562-805-9656 including area code
var PhoneNumFormat = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
if (!PhoneNumFormat.test(PhoneNumber)) 
  {
     alert ("The telephone number contained illegal characters.\n Please enter a telephone number format 562-805-9656/5628050917 including area code");
     document.getElementById('telephone').style.backgroundColor='#FFF4F4'; 
     document.getElementById('telephone').style.border = '1px solid #CC0000';
     document.getElementById('ulphone').style.color = '#CC0000';
     document.getElementById('ulphone').innerHTML = 'Illegal Char:';
     return false;
}*/ 
	//Subjects
	ST=0;
	for(i=0;i<RegForm.Subject_Code.length;i++)
		if(RegForm.Subject_Code[i].checked)	ST=1;
	if(ST==0)
	{
		alert("Please select interest subject(s)");
		return false;
	}
return true;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
// Handle the registration form hide and unhide optional fields
window.onload = function()
 {
  if(document.getElementById('optreqlink')!=null)
   {
     var linkContainer = document.getElementById('optreqlink');
     var linebreak = linkContainer.appendChild(document.createElement('br'));
     var toggle = linkContainer.appendChild(document.createElement('a'));
     toggle.href = '#';
     toggle.appendChild(document.createTextNode(' :: Hide optional fields '));

   toggle.onclick = function()
    {
      var linkText = this.firstChild.nodeValue;
      this.firstChild.nodeValue = (linkText == ' :: Hide optional fields ') ? ' :: Display optional fields ' : ' :: Hide optional fields ';

      var tmp = document.getElementsByTagName('div');
      for (var i=0;i<tmp.length;i++)
      {
      if(tmp[i].className == 'optfield')
       {
         tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
       }
     }
     return false;
    }
  }
}
//--------------------------------------------------------//
function Set_Request(){
	if(window.XMLHttpRequest)
		Req=new XMLHttpRequest();
	else if(window.ActiveXObject)
		Req = new ActiveXObject("Microsoft.XMLHTTP");
	return Req;
}

function ch_user(){
	Request=Set_Request();
	Request.onreadystatechange = function(){
		if (Request.readyState == 4)
			if (Request.status == 200) {
				if(Request.responseText!=0){
					alert("Entered Username already exists.\n Please Enter another Username.");
					document.RegForm.username.select();
					document.RegForm.username.focus();
					document.getElementById('username').style.border = '1px solid #CC0000';
				}
			} else
				alert("Problem: " + Request.statusText);
		
	};
	Request.open("GET","Frame.cfm?check_user=1&User_Code="+document.RegForm.Contact_Code.value+"&UN="+document.RegForm.username.value, true);
	Request.send(null);
}
function ch_email(){
	Request=Set_Request();
	Request.onreadystatechange = function(){
		if (Request.readyState == 4)
			if (Request.status == 200) {
				if(Request.responseText!=0){
					alert("Entered Email Address already exists.\n Please Enter another Email Address.");
					document.RegForm.email.select();
					document.RegForm.email.focus();
					document.getElementById('email').style.border = '1px solid #CC0000';
				}
			} else
				alert("Problem: " + Request.statusText);
		
	};
	Request.open("GET","Frame.cfm?check_email=1&User_Code="+document.RegForm.Contact_Code.value+"&Email="+document.RegForm.email.value, true);
	Request.send(null);
	
}

//--------------------------------------------------------//

