jQuery.noConflict();

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();
		if(username.length < 6) { //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_scurt).addClass('messageboxerror').fadeTo(12,1);
			});		
  		}
  		else{ 	
			jQuery.post(
				"/validateForm/username/"+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 > 13) {
			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 > 13) {
			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/"+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;
	}
	var checkDisplay = function (display_name) {
		jQuery("#msg_display_name").text(window.verific).fadeIn("slow");
		var display_name = jQuery("#display_name").val();
		if(display_name.length == 0) {
			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).fadeTo(12,1);
			});
		}else{
			jQuery.post(
				"/validateForm/display/"+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).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).fadeTo(12,1);	
						});
			  		}
				}
						
			)
		}
		return false;
	}
	var checkLogin = function () {
		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;		
		if(username.length<1 || password.length<1)
			alert(window.login_introduceti_user_parola);
		else{
			toggleview('input_login');
			toggleview('but_login');
			toggleview('fake_but_login');
			toggleview('loging');
			jQuery.post(
				"/validateLogin/login/"+username+"/"+password+"/"+permanent +"/",
				{},
				function(data) {
					if(data=='da') {
					  	ajaxpage("/validateLogin/cookie/"+username+"/"+password+"/"+permanent +"/", 'cookiz');
	          		}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 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') {
					  	ajaxpage("/validateLogin/cookie/"+username+"/"+password+"/"+permanent +"/", 'cookiz');
	          		}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);
});


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";
}
