jQuery(document).ready(function() {
	var valid=new Array('0','0','0','0');
	var checkUser = function (username) {
		//remove all the class add the messagebox classes and start fading
		jQuery("#msgbox").removeClass().addClass('messagebox').text(window.verific).fadeIn("slow");
		valid[0] = 1;
		var username = jQuery('#username').val(); 
        var match = /(?!^[0-9]*$)^([a-zA-Z0-9]*?((-|_|.)[a-zA-Z0-9]+)?((-|_|.)[a-zA-Z0-9]+))$/; 
		if(username.length < 3) { 
		  	jQuery("#msgbox").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[0] = 2;
	  			jQuery(this).html(window.inreg_display_scurt).addClass('messageboxerror').fadeTo(12,1);
			});
  		}else if(username.length > 30) { 
		  	jQuery("#msgbox").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[0] = 2;
	  			jQuery(this).html(window.inreg_display_lung).addClass('messageboxerror').fadeTo(12,1);
			});
  		}else if(!match.test(username)){
            jQuery("#msgbox").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[0] = 2;
	  			jQuery(this).html(window.inreg_display_nomatch).addClass('messageboxerror').fadeTo(12,1);
			});
        }else{
			jQuery.post(
				"/validateForm/username/",
				{user:username},
				function(data) { 
					if(data=='no' ) { //if username not avaiable
					  	jQuery("#msgbox").fadeTo(13,0.1,function() { //start fading the messagebox
							//add message and change the class of the box and start fading
				  			valid[0] = 2;
				  			jQuery(this).html(window.inreg_display_indisponibil).addClass('messageboxerror').fadeTo(12,1);
						});
	          		}
			  		else {
			  			jQuery("#msgbox").fadeTo(13,0.1,function() {  //start fading the messagebox
				  			//add message and change the class of the box and start fading
				  			valid[0] = 3;
				 		 	jQuery(this).html(window.inreg_display_ok).addClass('messageboxok').fadeTo(12,1);
						});
			  		}
				}
			)
		}
		return false;
	}
	var checkPass1 = function (password) {
		jQuery("#pmsgbox1").removeClass().addClass('messagebox').text(window.verific).fadeIn("slow");
		valid[1] = 1;
		var password = jQuery('#password').val();
		if(password.length < 6 || password.length > 30) {
			jQuery("#pmsgbox1").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[1] = 2;
				jQuery(this).html(window.inreg_parola_scurta).addClass('messageboxerror').fadeTo(12,1);
			});
		}
		else {
			jQuery("#pmsgbox1").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[1] = 3;
				hide_div_by_id('pmsgbox1');
				jQuery(this).html(window.inreg_parola_ok).removeClass('').fadeTo(12,1);
			});
		}
		return false;
	}
	var checkPass = function (password) {
		jQuery("#pmsgbox").removeClass().addClass('messagebox').text(window.verific).fadeIn("slow");
		valid[2] = 1;
		var password = jQuery('#password').val();
		var cpassword = jQuery('#cpassword').val();
		if(password.length < 6 || password.length > 30) {
			jQuery("#pmsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[2] = 2;
				jQuery(this).html(window.inreg_parola_scurta).addClass('messageboxerror').fadeTo(12,1);
			});
		}else{
			if(password != cpassword) {
				jQuery("#pmsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
					//add message and change the class of the box and start fading
					jQuery(this).html(window.inreg_parola_nu_coincide).addClass('messageboxerror').fadeTo(12,1);
					valid[2] = 2;
				});
			}
			else {
				jQuery("#pmsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
					//add message and change the class of the box and start fading
					jQuery(this).html(window.inreg_parolele_coincid).addClass('messageboxok').fadeTo(12,1);
					valid[2] = 3;
				});
			}
		}
		return false;
	}
	var checkEmail = function (email) {
		jQuery("#emsgbox").removeClass().addClass('messagebox').text(window.verific).fadeIn("slow");
		valid[3] = 1;
		var email = jQuery("#email").val();
		var reg = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,4})$/;
		if(reg.test(email) == false) {
			jQuery("#emsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				valid[3] = 2;
				jQuery(this).html(window.inreg_email_invalid).addClass('messageboxerror').fadeTo(12,1);
			});
		}else{
			jQuery.post(
				"/validateForm/email/",
				{emailAddress:email},
				function(data) {
					if(data=='no') { //if username not avaiable
					  	jQuery("#emsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
							//add message and change the class of the box and start fading
							valid[3] = 2;
				  			jQuery(this).html(window.inreg_email_nedisponibil).addClass('messageboxerror').fadeTo(12,1);
						});
	          		}
			  		else if(data=='valideaza') { //if username not avaiable
					  	jQuery("#emsgbox").fadeTo(13,0.1,function() { //start fading the messagebox
							//add message and change the class of the box and start fading
							valid[3] = 2;
				  			jQuery(this).html(window.inreg_email_nevalidat).addClass('messageboxerror').fadeTo(12,1);
						});
	          		}
			  		else {
			  			jQuery("#emsgbox").fadeTo(13,0.1,function() {  //start fading the messagebox
				  			//add message and change the class of the box and start fading
				  			valid[3] = 3;
				 		 	jQuery(this).html(window.inreg_email_ok).addClass('messageboxok').fadeTo(12,1);
						});
			  		}
				}

			)
		}
		return false;
	}

	var checkAll = function (){
		if(valid[0]==0) checkUser();
		if(valid[1]==0) checkPass1();
		if(valid[2]==0) checkPass();
		if(valid[3]==0) checkEmail();
		var b1 = 1;var b2 = 1; var b3 = 1;
		var i=0;
		for (i=0;i<4;i++){
			//alert("i"+i+" valoare "+valid[i]);
			if(valid[i] != 3)
				b3 = 0;
			if(valid[i]==2)
				b2 = 0;
			if(valid[i]==0 || valid[i]==1)
				b1 = 0;
		}
		if (b3 == 1) {
			toggleview('form_pasul_2');toggleview('form_pasul_1');
		}
		if(b2 == 1 && b1 == 0){
			//alert('reapelare');
			setTimeout(checkAll,200);
		}else if(b2 == 0){
			alert(window.inreg_campuri_necompletate);
		}
		return false;
	}
    var checkAllNew = function (){ 
		if(valid[0]==0) checkUser();
		if(valid[1]==0) checkPass1();
		if(valid[2]==0) checkPass();
		if(valid[3]==0) checkEmail();
		var b1 = 1;var b2 = 1; var b3 = 1;
		var i=0;
		for (i=0;i<4;i++){
			//alert("i"+i+" valoare "+valid[i]);
			if(valid[i] != 3)
				b3 = 0;
			if(valid[i]==2)
				b2 = 0;
			if(valid[i]==0 || valid[i]==1)
				b1 = 0;
		}
		if (b3 == 1) {
			setTimeout('document.doSubmit();', 50);
		}
		if(b2 == 1 && b1 == 0){
			//alert('reapelare');
			setTimeout(checkAllNew,200);
		}else if(b2 == 0){
			alert(window.inreg_campuri_necompletate);
		}
		return false;
	}
    try{
        var loggedUserName = jQuery("#display_name").val();
        var initialMsg = jQuery("#msg_display_name").html();
    }catch(e){
        
    }
	var checkDisplay = function (display_name) {
		jQuery("#msg_display_name").text(window.verific).removeClass('messageboxok1').fadeIn("slow");
		var display_name = jQuery("#display_name").val();
        var match = /(?!^[0-9]*$)^([a-zA-Z0-9]*?((-|_|.)[a-zA-Z0-9]+)?((-|_|.)[a-zA-Z0-9]+))$/; 
		if(display_name == loggedUserName){
            jQuery("#msg_display_name").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				jQuery(this).html(initialMsg).removeClass('messageboxok1').fadeTo(12,1);
			});
        }else if(display_name.length < 6) {
			jQuery("#msg_display_name").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
				jQuery(this).html(window.display_introduceti).removeClass('messageboxok1').fadeTo(12,1);
			});
		}else if(!match.test(display_name)){
            jQuery("#msg_display_name").fadeTo(13,0.1,function() { //start fading the messagebox
				//add message and change the class of the box and start fading
	  			jQuery(this).html(window.inreg_display_nomatch).removeClass('messageboxok1').fadeTo(12,1);
			});
        }else{
			jQuery.post(
				"/validateForm/display/",
				{user:display_name},
				function(data) {
					if(data=='no') { //if username not avaiable
					  	jQuery("#msg_display_name").fadeTo(13,0.1,function() { //start fading the messagebox
							//add message and change the class of the box and start fading
				  			jQuery(this).html(window.display_indisponibil).removeClass('messageboxok1').fadeTo(12,1);
						});
	          		}
			  		else {
			  			jQuery("#msg_display_name").fadeTo(13,0.1,function() {  //start fading the messagebox
				  			//add message and change the class of the box and start fading
				 		 	jQuery(this).html(window.display_ok).addClass('messageboxok1').fadeTo(12,1);
						});
			  		}
				}

			)
		}
		return false;
	}
	var checkLogin = function () {
        $("#loginUser").attr("disabled","disabled");
        $("#loginPass").attr("disabled","disabled");
        $("#loginButon").attr("disabled","disabled");
        $("#loging_err").hide();
        var h = ($(".loading").parent('form').height())/2 - ($(".loading").height()/2);
        $(".loading").css('top',h);
        $(".loading").show();
        $(".form_modif_date_profil,#comment").fadeTo('slow',0.2);

        
		var username = jQuery('#loginUser').val();username = username.replace(/\//g, '~!~');
		var password = jQuery('#loginPass').val();password = password.replace(/\//g, '~!~');
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var permanent = 0;
		if(document.login_form.retine_parola.checked == true)
			permanent = 1;
        var invizibil = 0;
        if(my_getbyid('loginPrivacy')){
            if(document.login_form.privacy.checked == true)
                invizibil = 1;
        }
		if(username.length<1 || password.length<1){
			alert(window.login_introduceti_user_parola);
            loginFadeOut();
		}else{ //"/validateLogin/login/"+username+"/"+password+"/"+permanent+"-"+invizibil+"/",
			toggleview('input_login');
			toggleview('but_login');
			toggleview('fake_but_login');
			toggleview('loging');
			jQuery.post(
				"/validateLogin/login/",
				{user: username, pass: password, perm: permanent+"-"+invizibil},
				function(data) {
					if(data=='da') {
                        jQuery.post(
                                "/validateLogin/cookie/",
                                {user: username, pass: password, perm: permanent+"-"+invizibil},
                                function(data2) {
                                    $('#cookiz').html(data2); 
                                }
                        )
	          		}else if(data =='stopSpam'){
	          			location.replace('stopSpam/');
	          		}else if(reg.test(data)){
						alert(window.login_email_nevalidat_1+data+window.login_email_nevalidat_2);
						location.replace('/validare_cont/');
					}else loginFailed();
				}
			)
		}
		return false;
	}
    
    var loginFailed = function(){
        if(location.toString().indexOf('login') == -1){
            loginFadeOut();
            $("#loging_err").show();
            }else location.replace('fail/');
    }
    
    var loginFadeOut = function(){
        $(".loading").hide();
        $("#loginUser").attr("disabled","");
        $("#loginPass").attr("disabled","");
        $("#loginButon").attr("disabled","");
        $(".form_modif_date_profil,#comment").fadeTo('slow',1);
    }
    
	var checkLoginLeft = function () {
		var username = jQuery('#loginUserLeft').val();username = username.replace(/\//g, '~!~');
		var password = jQuery('#loginPassLeft').val();password = password.replace(/\//g, '~!~');
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var permanent = 0;
		if(document.login_form_left.retine_parola.checked == true)
			permanent = 1;
		if(username.length<1 || password.length<1)
			alert(window.login_introduceti_user_parola);
		else{
			toggleview('input_login_left');
			toggleview('but_login_left');
			toggleview('fake_but_login_left');
			toggleview('loging_left');
			jQuery.post(
				"/validateLogin/login/"+username+"/"+password+"/"+permanent +"/",
				{},
				function(data) {
					if(data=='da') {
                        jQuery.post(
                                "/validateLogin/cookie/",
                                {user: username, pass: password, perm: permanent},
                                function(data2) {
                                    $('#cookiz').html(data2); 
                                }
                        )
	          		}else if(data =='stopSpam'){
	          			location.replace('/login/stopSpam/');
	          		}else if(reg.test(data)){
						alert(window.login_email_nevalidat_1+data+window.login_email_nevalidat_2);
						location.replace('/validare_cont/');
					}else {
			  			location.replace('/login/fail/');
			  		}
				}
			)
		}
		return false;
	}
	var checkLogout = function () {
		toggleview('input_logout');
		toggleview('but_login');
		toggleview('fake_but_login');
		toggleview('loging');
		ajaxpage('/validateLogin/logout/', 'cookiz');
		return false;
	}
	var checkLogoutLeft = function () {
		toggleview('input_logout_left');
		toggleview('but_login_left');
		toggleview('fake_but_login_left');
		toggleview('loging_left');
		ajaxpage('/validateLogin/logout/', 'cookiz');
		return false;
	}
	jQuery("#username").blur(checkUser);
	jQuery("#password").blur(checkPass1);
	jQuery("#cpassword").blur(checkPass);
	jQuery("#email").blur(checkEmail);
	jQuery("#pasul_doi").click(checkAll);
	jQuery("#register").click(checkAllNew);
	jQuery("#date_personale").click(checkAll);
	jQuery("#display_name").blur(checkDisplay);
	jQuery("#logoutButon").click(checkLogout);
	jQuery("#loginButon").click(checkLogin);
	jQuery("#loginButonLeft").click(checkLoginLeft);
	jQuery("#logoutButonLeft").click(checkLogoutLeft);
	jQuery(".font_termeni").click(function(){
        $('.font_termeni_text').slideToggle('fast');
        $('.font_termeni').toggleClass('active');
    });
    jQuery(".cond_span").click(function(){
        if($(this).prev('input').is(":checked")){
            $(this).prev('input').attr('checked',false);
        }else{
            $(this).prev('input').attr('checked',true);
        }
    });
    
    $("#loginUser, #loginPass").keypress(function(event) {
      if (event.keyCode == '13') {
         checkLogin();
       }
    });
});


function my_getbyid(id)
{
	itm = null;

	if (document.getElementById)
	{
		itm = document.getElementById(id);
	}
	else if (document.all)
	{
		itm = document.all[id];
	}
	else if (document.layers)
	{
		itm = document.layers[id];
	}

	return itm;
}

//==========================================
// Show div
//==========================================

function showview(id)
{
	if ( itm = my_getbyid(id) )
	{
		my_show_div(itm);
	}
}

//==========================================
// Hide div
//==========================================

function hideview(id)
{
	if ( itm = my_getbyid(id) )
	{
		my_hide_div(itm);
	}
}

//==========================================
// Show/hide toggle
//==========================================

function toggleview(id)
{
	if ( ! id ) return;

	if ( itm = my_getbyid(id) )
	{
		if (itm.style.display == "none")
		{
			my_show_div(itm);
		}
		else
		{
			my_hide_div(itm);
		}
	}
}

//==========================================
// Set DIV ID to hide
//==========================================

function my_hide_div(itm)
{
	if ( ! itm ) return;

	itm.style.display = "none";
}

//==========================================
// Set DIV ID to show
//==========================================

function my_show_div(itm)
{
	if ( ! itm ) return;

	itm.style.display = "";
}


function show_div_by_id(id)
{

	if (document.getElementById)
	{
		itm = document.getElementById(id);
	}
	else if (document.all)
	{
		itm = document.all[id];
	}
	else if (document.layers)
	{
		itm = document.layers[id];
	}
	if ( ! itm ) return;

	itm.style.display = "";
}

function hide_div_by_id(id)
{

	if (document.getElementById)
	{
		itm = document.getElementById(id);
	}
	else if (document.all)
	{
		itm = document.all[id];
	}
	else if (document.layers)
	{
		itm = document.layers[id];
	}
	if ( ! itm ) return;

	itm.style.display = "none";
}

