var passStates = ["Weak", "Poor", "Fair", "Good", "Very Good", "Perfect"]; // different strength levels
var fields = ["username","password","fname","lname","email","phone","userCaptcha"];
var bRefresh = (new Boolean(false)); // refresh has not been clicked yet
var valid_email = (new Boolean(false)); // did they provide a valid email?
var is_available = (new Boolean(false)); // check if the user is available

$.ready(function(complete){
 
 $.events("email", "blur", emailBlur);
 $.events("username", "blur", userBlur); 
 $.events("password", "blur", passBlur);
 $.events("email", "click", emailPress);
 $.events("username", "click", userPress);
 $.events("password", "click", passPress);
 $.events("email", "keyup", emailPress);
 $.events("username", "keyup", userPress);
 $.events("password", "keyup", passPress);
 $.events("submitRegister", "click", submitClick);
 $.events("clearRegister", "click", clearClick);
 $.events("refreshCaptcha","click", refreshClick);
 refreshCaptchaNumbers(); // ### we need to refresh the image 
 $.img.preload(rel+"lib/img/clear_down.png"); // preload the clear down state image
 $.img.preload(rel+"lib/img/register_down.png"); // preload the register down state image
 $.obj.fx.hover("submitRegister",(rel+"lib/img/register_up.png"),(rel+"lib/img/register_down.png"));
 $.obj.fx.hover("clearRegister",(rel+"lib/img/clear_up.png"),(rel+"lib/img/clear_down.png"));
 var objects = ($.obj.getElementsByClass("liveData"));
 for(var i=0;(i<(objects.length));i++){
  $.obj.fx.collapse(objects[i], $.gl.FX_DISPLAY());
  $.obj.alpha(objects[i], "90"); // change opacity
  objects[i].style.visibility = "visible";
 }; // end of for-loop
 
}); // end of ready-statement

function submitClick(e){
 var errors = (new Array()); 
 // ## as long as the length of the array stays 0, then no errors have 
 // ## occured; if it is greater than 0 then something happened; and the 
 // ## error message will be equivalent to the errors integer number.
 var errmsgs = ["Username must be at least 5 characters long.", "Password must be at least 5 characters long.",
 "First name is a required field.", "Last name is a required field.", "Please provide a valid email address.",
 "Please check that you've read the terms of service before registering.","The username you have chosen is not available."];
 
 if($.o("username").value.length < 5) errors.push(errmsgs[0]); // ### insufficient characters (+5)
 if($.o("password").value.length < 5) errors.push(errmsgs[1]); // ### insufficient characters (+5)
 if($.o("fname").value.length < 3) errors.push(errmsgs[2]); // ### insufficient characters (+3)
 if($.o("lname").value.length < 3) errors.push(errmsgs[3]); // ### insufficient characters (+3)
 if(valid_email == false) errors.push(errmsgs[4]); // ### invalid email address provided
 if($.o("agree").checked == false) errors.push(errmsgs[5]); // ### didn't agree to terms
 if(is_available == false) errors.push(errmsgs[6]); // ### username is not available
 
 if(errors.length > 0) { 
  // ## this area is only accessed when an error occurs, this 
  // ## will output the errors to the screen for the user to see
  $.o("caused_errors").innerHTML = "<h1>Error(s)</h1>";
  for(var i = 0; i < (errors.length); i++){
   $.o("caused_errors").innerHTML += "<h2>"+errors[i]+"</h2>";
  }; // end of errors for-loop
 }; // ## end of errors-section
 
 if(errors.length == 0) $.o("fRegister").submit(); // submit the form
}; // end of submitClick();

function clearClick(e){
 for(var i=0;i<(fields.length);i++){
  $.o(fields[i]).value = ""; // clear value
 }; // end of for-loop ; clear fields
}; // end of clearClick();

function refreshClick(e){
 $.ajax({type: "GET", url: rel+"lib/includes/ajax.php", data: "?action=newCaptcha"+"&rel="+rel, 
 callback: function(complete){
  refreshCaptchaNumbers(); // ### we need to refresh the image 
  $.o("sysCaptcha").value = (complete); // store the new captcha value.
 }, cont: true});
} // end of refreshClick();

function refreshCaptchaNumbers() {
 $.o("captcha").src = $.o("captcha").src + (bRefresh==false?"?":"&") +
 "id=" + (Math.round(Math.random()*50)); // we must do this to refresh.
 if(bRefresh == false) bRefresh = true; // refresh has now been clicked.
}; // end of refreshCaptchaNumbers();

function doesContainNumerics(data){
 var numbers = "0123456789";
 for (i = 0; i < 10; i++){ 
 var temp = (numbers.split(""));  
 var returnval = data.match(temp[i]);
 if(returnval==temp[i]){return 1;}};
 return 0; // nothing else was returned.
}; // end of doesConatinNumerics();
			  
function doesContainWildKeys(data){ var found;
 var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"; 
 for (var i = 0; i < data.length; i++) {
  if (iChars.indexOf(data.charAt(i)) != -1){ return 1; }
 }; return 0; // nothing was returned; so return 0.
}; // end of doesContainWildKeys();
	
function doesContainLowerCase(data){ // chars: 97-to-122
 for(d = 0;d < data.length; d++){
  for(i = 97; i < 122; i++){
   charToChk = String.fromCharCode(i);
   if(data.charAt(d)==charToChk){ return 1; };
 }; }; // end of both the for-loops
 return 0; // nothing was returned.
}; // end of doesContainLowerCase();
			  
function doesContainUpperCase(data){ // chars: 65-to-90
 for(d = 0;d < data.length; d++){
  for(i = 65;i < 90; i++){
   charToChk = String.fromCharCode(i);
   if(data.charAt(d)==charToChk){ return 1; };
 }; }; // end of both the for-loops
 return 0; // nothing was returned.
} // end of doesContainUpperCase();

function passStrength(value){
 var length = (new String(value).length);
 var totalSteps = (new Number(6));
 var curSteps = (new Number(0));
 var valid = (new Number(0));
 // if we have 5 characters ~ up 1 step
 // if there is at least 1 numerical characters
 // if there is at least 1 string character (anything)
 // if there is at least 1 wild character ~ up 1 step
 var passMeter = (document.createElement("div"));
 while($.o("passData").hasChildNodes()) // remove all children.
 $.o("passData").removeChild($.o("passData").lastChild);
 $.o("passData").appendChild(passMeter); // will contain images
 if(doesContainLowerCase(value) == 1){ curSteps++; };
 if(doesContainUpperCase(value) == 1){ curSteps++; };
 if(doesContainWildKeys(value) == 1){ curSteps++; };
 if(doesContainNumerics(value) == 1){ curSteps++; };
 if(length > 4){ curSteps++; valid = 1;}; 
 if(length != 0){ curSteps++; };
 // now that we have checked for all six steps that
 // we check for throughout the password; we now 
 // setup the password strength meter; which allows
 // them to visually see how strong their password is.
 for(i = 0; i < (curSteps * 4); i++){
  var img = (document.createElement("img")); // image element
  var cdiv = ($.o("passData").getElementsByTagName("div")[0]);
  img.src = (rel+"lib/img/progup.png"); // source path
  cdiv.appendChild(img); // append to pass meter
 }; // end of for-loop

 // we have added all the proggressive images; now
 // we will add all the images that go down.
 for(i = 0; i < ((totalSteps-curSteps) * 4); i++){
  var img = (document.createElement("img")); // image element
  var cdiv = ($.o("passData").getElementsByTagName("div")[0]);
  img.src = (rel+"lib/img/progdown.png"); // source path
  cdiv.appendChild(img); // append to pass meter
 }; // end of for-loop
 
 var clear = (document.createElement("div"));
 var passStrength = (document.createElement("h3"));
 if(curSteps!=0) passStrength.innerHTML = (passStates[curSteps-1]);
 if(curSteps==0) passStrength.innerHTML = "Unknown";
 $.o("passData").appendChild(passStrength);
 $.o("passData").appendChild(clear);
 clear.className = "clearfloat";
 
}; // end of passStrength();

function userBlur(e){
 var currentObject = ($.returnElement(e).sliderObject);
 if(currentObject) { // ## does the object exist?
 if($.returnElement(e).slideDown != false){
 $.obj.fx.fade(currentObject, 90, 0, 1, function(e){
  $.obj.alpha("userData", "90"); // change back
  $.obj.fx.collapse("userData", $.gl.FX_DISPLAY());
  $.o("username").slideDown = false;
 });};}; // only if it's not false.
}; // end of userBlur();

function passBlur(e){
 var currentObject = ($.returnElement(e).sliderObject);
 if(currentObject) { // ## does the object exist?
 if($.returnElement(e).slideDown != false){
 $.obj.fx.fade(currentObject, 90, 0, 1, function(e){
  $.obj.alpha("passData", "90"); // change back
  $.obj.fx.collapse("passData", $.gl.FX_DISPLAY());
  $.o("password").slideDown = false;
 });};}; // only if it's not false.
}; // end of passBlur();

function emailBlur(e){
 var currentObject = ($.returnElement(e).sliderObject);
 if(currentObject) { // ## does the object exist?
 if($.returnElement(e).slideDown != false){
 $.obj.fx.fade(currentObject, 90, 0, 1, function(e){
  $.obj.alpha("emailData", "90"); // change back
  $.obj.fx.collapse("emailData", $.gl.FX_DISPLAY());
  $.o("email").slideDown = false;
 });};}; // only if it's not false.
}; // end of emailBlur();

function emailPress(e){
 var obj = ($.returnElement(e)); // actual textbox
 var val =(obj.value.length); // length of the string
 if((e.type != "click" && obj.slideDown != true) || (e.type == "click" && val > 0 && obj.slideDown != true)) {
  $.obj.fx.slide($.o("emailData"),Tween.strongEaseOut,$.gl.OPEN(), .5, true);
  obj.sliderObject = ($.o("emailData")); // the sliding object
  obj.slideDown = true; // the object container has been revealed.
 } // object slide down the real-time data.
 var is_valid_email = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
 if(is_valid_email.test(obj.value)) {
  $.o("uemail").className = "validData";
  $.o("uemail").innerHTML = "Your email appears to be valid.";
  valid_email = true; // valid email data
 } else {
  $.o("uemail").className = "invalidData";
  $.o("uemail").innerHTML = "Please provide a valid email address.";
  valid_email = false; // invalid email data
 }; // end of checking for a valid email
}; // end of emailPress();

function passPress(e){
 var obj = ($.returnElement(e)); // actual textbox
 var val =(obj.value.length); // length of the string
 if((e.type != "click" && obj.slideDown != true) || (e.type == "click" && val > 0 && obj.slideDown != true)) {
  passStrength($.o("password").value); // commence the password strength test.
  $.obj.fx.slide($.o("passData"),Tween.strongEaseOut,$.gl.OPEN(), .5, true);
  obj.sliderObject = ($.o("passData")); // the sliding object
  obj.slideDown = true; // the object container has been revealed.
 } // object slide down the real-time data.
 passStrength($.o("password").value);
}; // end of passPress();

function userPress(e){
 var obj = ($.returnElement(e)); // actual textbox
 var val =(obj.value.length); // length of the string
 if((e.type != "click" && obj.slideDown != true) || (e.type == "click" && val > 0 && obj.slideDown != true)) {
  $.obj.fx.slide($.o("userData"),Tween.strongEaseOut,$.gl.OPEN(), .5, true);
  obj.sliderObject = ($.o("userData")); // the sliding object
  obj.slideDown = true; // the object container has been revealed.
 } // object slide down the real-time data.
 $.o("ucount").innerHTML = "Current characters: "+(val);
 if(val > 4) { // data is valid; there is more than 5 characters
  $.o("uvalid").className = "validData";
  $.o("uvalid").innerHTML = "Username contains 5 characters.";
  $.o("uavailable").innerHTML = "Checking availability...";
  $.ajax({type: "GET", url: rel+"lib/includes/ajax.php", data: "?action=lookup&user="+obj.value, 
  callback: function(complete){
   if(new String(complete) == "1") {
	$.o("uavailable").className = "invalidData";
	$.o("uavailable").innerHTML = "Username is not available.";
	is_available = false; // username is not available.
   } else if(new String(complete) == "0") {
    $.o("uavailable").className = "validData";
	$.o("uavailable").innerHTML = "Username is available!"; 
	is_available = true; // username is available
  }; }, cont: true});
 } else {
  $.o("uvalid").className = "invalidData";
  $.o("uavailable").className = "invalidData";
  $.o("uvalid").innerHTML = "Username must have 5 characters.";
  $.o("uavailable").innerHTML = "Cannot check for availability.";
 } // if they have more than 4 characters
}; // end of userPress();