/*
new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'username') { this.username(o) };
          if(o.name == 'password') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };
        },
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>\@)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doValidate(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
              doValidate(o);
            } else {
              doError(o,'not a valid email');
            };
        },
        emailrecovery: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(emailrecovery)) {
              doValidate(o);
            } else {
              doError(o,'not a valid email');
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'not a valid date');
            };
        }
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="images/accept.png" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="images/delete.png" border="0" style="float:left;" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     //private helper, validates each type after check
     function doValidate(o) {
        	$('#' + o.id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
        	$.post('ajax.php', { id: o.id, value: o.value }, function(json) {
                  	eval("var args = " + json);
                        if (args.success == true)
                  	{
                  	  doSuccess(args);
                  	}
                  	else
                  	{
                     doError(args,args.msg);
                  	}
                  });
    };

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="images/accept.png" border="0" style="float:left;" />');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="images/delete.png" border="0" style="float:left;" />');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("The passwords don't match, please try again");
        };
};
$(document).ready(function()
{

  $("//[@class=validated]/input").blur(function() {
          $(this).validate.init(this);
  });
  

  $("#confirmpass").blur(function() {
          $(this).match({match: '#password', error: '#confirmpass_li'});
  });

  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });
});
*/
/* Login */
function process_login() {
		EmptyDiv('loginhelpdiv');
		EmptyDiv('usermenu');
		theForm = document.getElementById('LoginForm');
		theStr = '';
		for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		
		theStr += ele.name + ' = ' + ele.value + "&";
	 	}
		
		$.post('login.php',  theStr , function(json) {
				eval("var args = " + json);
					if (args.success == true){
						//HideDiv('registrationdiv');
						HideDiv('logindiv');
						//HideDiv('submenu');
						
						GetDiv('usermenu','login.php?act=logged');	
						
							$(function() {
								$( function() {
                    				$('#container ul').tabsEnable(2);
                    				return true;
                					});
								
							});
							$(function() {
								$( function() {
                    				$('#container ul').tabsEnable(3);
                    				return true;
                					});
								
							});
							$(function() {
								$( function() {
                    				$('#container ul').tabsEnable(4);
                    				return true;
                					});
								
							});
							$(function() {
								$( function() {
                    				$('#container ul').tabsEnable(5);
                    				return true;
                					});
								
							});
					
					} else {
						WriteDiv('loginhelpdiv',"Please compile all the fields!");
					}
			  });
}
/* Logout */
function process_logout() {

		GetDiv('random','login.php?act=logout');
		
		ShowDiv('logindiv');
		//ShowDiv('registrationdiv');
		//ShowDiv('submenu');

			$(function() {
				$( function() {
					$('#container ul').tabsDisable(2);
					return true;
					});
			});
			$(function() {
				$( function() {
					$('#container ul').tabsDisable(3);
					return true;
					});
			});
			$(function() {
				$( function() {
					$('#container ul').tabsDisable(4);
					return true;
					});
			});
			$(function() {
				$( function() {
					$('#container ul').tabsDisable(5);
					return true;
					});
				
			});
		EmptyDiv('usermenu');
}

/* Registration Stuff */
function process_registration() {
		theForm = document.getElementById('RegistrationForm');
		theStr = '';
		for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		
		theStr += ele.name + ' = ' + ele.value + "&";
	 	}
		
		WriteDiv('activationdiv',"loading");
		
		$.post('register.php',  theStr , function(json) {
				eval("var args = " + json);
					if (args.success == true){

						do_activate();
					} else {
						WriteDiv('activationdiv',"Please compile all the fields!");
					}
			  });
}

/* Activation page */
function jumpto_activate() {
	$.get("activate.php", function(data){
	WriteDiv('registrationdiv',data);
	HideDiv('logindiv');
	EmptyDiv('activationdiv');
	});
	show_help("Enter the username and the activation code that you've received in your email");
}

function do_activate() {
	$.get("activate.php", function(data){
	WriteDiv('registrationdiv',data);
	//$("#logindiv").hide("fast");
	HideDiv('logindiv');
	EmptyDiv('activationdiv');
	});
	$text = "Within a few moment you will receive an email with the activation code<br>Enter this code in the form.";
	show_help($text);
}


function process_activation() {
		theForm = document.getElementById('RegistrationForm');
		theStr = '';
		for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		theStr += ele.name + ' = ' + ele.value + "&";
	 	}
		WriteDiv('activationdiv',"loading");
		
		$.post('activate.php',  theStr , function(json) {
				eval("var args = " + json);
					if (args.success == true){
						RegistrationFinished();
					} else {
						WriteDiv('activationdiv',"User/session invalid or account already registred.");
					}
		});
}


function RegistrationFinished() {
	ShowDiv('logindiv');
	show_help("Your account has been activated!<br><br>Thanks for registering!");
	EmptyDiv('activationdiv');
	EmptyDiv('registrationdiv');
}



/* Some functions to keep the main code clean */

function show_help(text) {
document.getElementById('helpdiv').style.display = "block";
document.getElementById('helpdiv').innerHTML = text;
}
function hide_help() {
document.getElementById('helpdiv').innerHTML = "";
document.getElementById('helpdiv').style.display = "none";
}
function EmptyDiv(div) {
document.getElementById(div).innerHTML = "";		
}
function WriteDiv(div,text) {
document.getElementById(div).innerHTML = text;		
}
function HideDiv(div) {
document.getElementById(div).style.display = "none";		
}
function ShowDiv(div) {
document.getElementById(div).style.display = "block";		
}
function GetDiv(div,url){
		$.get(url, function(data) {
			document.getElementById(div).innerHTML = data;
		});	
}

