$(document).ready(function(){
	var url = $('#url').text();
	//CAPTCHA LOGIN
	var captchaLoginA = 1 + Math.floor(Math.random()*9);
	var captchaLoginB = 1 + Math.floor(Math.random()*9);
	$('#captchaA').html("<img src='" + url + "img/captcha/" + captchaLoginA + ".png' />");
	$('#captchaB').html("<img src='" + url + "img/captcha/" + captchaLoginB + ".png' />");
		//captcha
	$('#loginByCaptcha').bind('click', function(){
		var captchaResult = $('#captchaLogin');
		if ((captchaLoginA + captchaLoginB) == captchaResult.val()){
			return true;
		}
		else {
			alert('Eroare captcha!');
			return false;
		}
	});
	//CAPTCHA LOGIN
	
	var popupStatus = 0;
	var count = function ( mixed_var, mode ) {
	    var key, cnt = 0;
	    if( mode == 'COUNT_RECURSIVE' ) mode = 1;
	    if( mode != 1 ) mode = 0;
	    for (key in mixed_var){
	        cnt++;
	        if( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
	            cnt += count(mixed_var[key], 1);
	        }
	    }
	    return cnt;
	}
	var filtruParam = function (param) {
		var oldValue = param;
		var newValue = oldValue.replace(/ /g, '-');
		return newValue;
	};
	var returnChunks = function (param) {
		chunks = param.split("_");
		if(count(chunks, 'COUNT_RECURSIVE') > 1) {
			return chunks;
		} else {
			return param;
		}
	};
	
	//loading popup with jQuery magic!
	function loadPopup(popupName, idGroup){
		//loads popup only if it is disabled
		if(popupStatus==0){
			$("#BG"+popupName).css({
				"opacity": "0.7"
			});
			if (popupName == 'popupEditeazaAvatarGrup') $("#groupID").attr('value', idGroup);
			$("#BG"+popupName).fadeIn("slow");
			$("#"+popupName).fadeIn("slow");
			popupStatus = 1;
		}
	}

	//disabling popup with jQuery magic!
	function disablePopup(popupName){
		//disables popup only if it is enabled
		if(popupStatus==1){
			$("#BG"+popupName).fadeOut("slow");
			$("#"+popupName).fadeOut("slow");
			popupStatus = 0;
		}
	}

//centering popup
function centerPopup(popupName){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

	var pageScrolledBy = getScrollXY();

	var popupHeight = $("#"+popupName).height();
	var popupWidth = $("#"+popupName).width();
	//centering

	$("#"+popupName).css({
		"position": "absolute",
		"top": myHeight/2+pageScrolledBy[1]-popupHeight/2,
		"left": myWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#BG"+popupName).css({
		"height": windowHeight
	});
}

//LOADING POPUP
//Click the button event!
$("#creeazaGrup").click(function(){
	//centering with css
	centerPopup('popupCreeazaGrup');
	//load popup
	loadPopup('popupCreeazaGrup');
});

$("#viewPhotoAlbum").bind('click', function(){
	centerPopup('popupCreeazaGrup');
	loadPopup('popupCreeazaGrup');
	return false;
});

$("#trimiteMesajNelogat").bind('click', function(){
	centerPopup('popupCreeazaGrup');
	loadPopup('popupCreeazaGrup');
	return false;
});

$("#getFriendList").bind('click', function(){
	centerPopup('popupPrieteniiMei');
	loadPopup('popupPrieteniiMei');
	return false;
});

$("#userIDforAddFriend").bind('click', function(){
	centerPopup('popupCreeazaGrup');
	loadPopup('popupCreeazaGrup');
	return false;
});

$('#modificaAvatar').bind('click', function(){
	centerPopup('popupModificaAvatar');
	loadPopup('popupModificaAvatar');
	return false;
});
//CLOSING POPUP
//Click the x event!
$("#popupContactClose, #cancelGroup").click(function(){
disablePopup('popupCreeazaGrup');
});

$('#popupEditeazaAvatarGrupClose').click(function(){
	disablePopup('popupEditeazaAvatarGrup');
});

$("#popupPrieteniiMeiClose, #cancelPrieteniiMei").click(function(){
disablePopup('popupPrieteniiMei');
});

$("#popupModificaAvatarClose, #cancelModificaAvatar").click(function(){
disablePopup('popupModificaAvatar');
});

//Click out event!
$("#BGpopupCreeazaGrup").click(function(){
disablePopup('popupCreeazaGrup');
});

$("#BGpopupEditeazaAvatarGrup, #cancelEditAvatarGroup").click(function(){
	disablePopup('popupEditeazaAvatarGrup');
});

$("#BGpopupPrieteniiMei").click(function(){
disablePopup('popupPrieteniiMei');
});

$("#BGpopupModificaAvatar").click(function(){
disablePopup('popupModificaAvatar');
});
//Press Escape event!
$(document).keypress(function(e){
	if(e.keyCode==27 && popupStatus==1){
		disablePopup();
	}
});

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
	
	function limitChars(textid, limit, infodiv)
	{
		var text = $('#'+textid).val(); 
		var textlength = text.length;
		if(textlength > limit)
		{
			$('#' + infodiv).html('Nu poti scrie mai mult de '+limit+' caractere!');
			$('#' + textid).val(text.substr(0,limit));
			return false;
		}
		else
		{
			$('#' + infodiv).html('Ti-au ramas '+ (limit - textlength) +' caractere.');
			return true;
		}
	}
	
	$('#JStextAreaMyStatus').each(function(){
		var length = $(this).val().length;
		$(this).keyup(function(){
			limitChars('JStextAreaMyStatus', 255, 'counterMyStatus');
		});
	});
	
	$('#JStextAreaAboutMe').each(function(){
		var length = $(this).val().length;
		$(this).keyup(function(){
			limitChars('JStextAreaAboutMe', 255, 'counterAboutMe');
		});
	});
	
	$('#JStextAreaHobby').each(function(){
		var length = $(this).val().length;
		$(this).keyup(function(){
			limitChars('JStextAreaHobby', 255, 'counterHobby');
		});
	});

	$('#JStextAreaMusic').each(function(){
		var length = $(this).val().length;
		$(this).keyup(function(){
			limitChars('JStextAreaMusic', 255, 'counterMusic');
		});
	});

	$('#JStextAreaMovie').each(function(){
		var length = $(this).val().length;
		$(this).keyup(function(){
			limitChars('JStextAreaMovie', 255, 'counterMovie');
		});
	});

	
	$('#invitaInGrup').toggle(
		function(){
			var idUser = $(this).attr('class');
			$('#jsGrupurileMele_'+idUser).slideDown();
		},
		function(){
			var idUser = $(this).attr('class');
			$('#jsGrupurileMele_'+idUser).slideUp();
		}
	);
	
	
	$('#showMesaje').click(function(){
	var stil = getStyle(document.getElementById("arataMesaje"), "display");		
		if(stil == 'none') { 
 			$('#arataMesaje').slideDown();
		}
		else {$('#arataMesaje').slideUp();}
 	});
 	
 	$('#anabsolvireYear').change(function(){
 		var url = String(document.location);
 		var length = url.length-4;
 		var newUrl = url.substr(0, length);
 		newUrl = newUrl + this.value;
 		document.location = newUrl;
	});
	
	//var i = 0;
	$('#ProfileJudet').change(function(){
		$('#ProfileOras').empty();
		$('#ProfileOras').append("<option value=\"\">-=Incarc Orasele=-</option>");
		$.ajax({
			url: url+'membri/getOrase/' + $(this).val(),
            type: "POST",
            dataType: 'json',
            error: function(error) {
            	$('#ProfileOras').empty();
			    $('#ProfileOras').append("<option value=\"\">Fara orase</option>");
            },
            success: function(json) {
            	$('#ProfileOras').empty();
			    $('#ProfileOras').append("<option value=\"\">Alege orasul</option>");
			    $.each(json, function(optionIndex, option) {
			   	   	var html = '<option value="' + optionIndex + '">' + option + '</option>';
			        $('#ProfileOras').append(html);
			   });
            }
       });
	});
	
	$('#SchoolJudet').change(function(){
		$('#SchoolOras').empty();
		$('#SchoolOras').append("<option value=\"\">-=Incarc Orasele=-</option>");
		$.ajax({
			url: url+'membri/getOrase/' + $(this).val(),
	        type: "POST",
	        dataType: 'json',
	        error: function(error) {
	        	$('#SchoolOras').empty();-undefined
				    $('#SchoolOras').append("<option value=\"\">Fara orase</option>");
	        },
	        success: function(json) {false
	         	$('#SchoolOras').empty();
			    $('#SchoolOras').append("<option value=\"\">Alege orasul</option>");
			    $.each(json, function(optionIndex, option) {
			   	   	var html = '<option value="' + optionIndex + '">' + option + '</option>';
			        $('#SchoolOras').append(html);
			   });
	       }
	   });
	});
	
	$('#InstitutionParentId').livequery(function() {
		$(this).change(function(){
			$('#NumeScoala').empty();
			$('#NumeScoala').append("<option value=\"\">-=Incarc unitatile de invatamant=-</option>");
			$.ajax({
				url: url+'membri/getScoli/' + $(this).val() + '/' + $('#ProfileOras').val(),
	            type: "POST",
	            dataType: 'json',
	            error: function(error) {
	            	$('#NumeScoala').empty();
				    $('#NumeScoala').append("<option value=\"\">Fara unitati de invatamant</option>");
	            },
	            success: function(json) {
	            	$('#NumeScoala').empty();
				    $('#NumeScoala').append("<option value=\"\">Alege unitatea de invatamant</option>");
				    $.each(json, function(optionIndex, option) {
				   	   	var html = '<option value="' + optionIndex + '">' + option + '</option>';
				        $('#NumeScoala').append(html);
				   });
	            }
	       });
		});
	});
	
	$("a[id^='linkMesaj']").each(function(option) {
		$(this).bind("click", function(){
			var chunks = $(this).attr("id").split('_');
			$.ajax({
				url: url+'membri/getMessage/' + chunks[1],
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('#message_'+chunks[1]).html(data);
					$('#viewMessage_'+chunks[1]).slideDown('slow');
	            }
	       });			
		});
	});
	$("a[id^='close']").each(function(option){
		$(this).bind("click", function(){
			var chunks = $(this).attr("id").split('_');
			$('#viewMessage_'+chunks[1]).slideUp('slow');
		});
	});
	$('#galerie .img a').lightBox();
	$('#CautaSubmit').click(function(){
	   var mesaj='';
	   cityID = $('#ProfileOras').val();
	   schoolID = $('#NumeScoala').val();
		if (($('#ProfileJudet').val()=='')) {
			mesaj=mesaj+'Trebuie sa selectezi judetul\r\n';
		}
		if (($('#ProfileOras').val()=='')) {
			mesaj=mesaj+'Trebuie sa selectezi orasul\r\n';
		}
		if (($('#InstitutionParentId').val()=='')) {
			mesaj=mesaj+'Trebuie sa selectezi tipul de institutie\r\n';
		}
		if (($('#NumeScoala').val()=='')) {
			mesaj=mesaj+'Trebuie sa selectezi o scoala\r\n';
		}
		if (mesaj!='') {
			alert(mesaj);
			return false;
		} else {
			judet = filtruParam($('#ProfileJudet :selected').text());
			oras = filtruParam($('#ProfileOras :selected').text());
			tipInstitutie = filtruParam($('#InstitutionParentId :selected').text());
			institutie = filtruParam($('#NumeScoala :selected').text());
			if($('#CityAnabsolvireYear').val() != '') {
				var anpromotie = $('#CityAnabsolvireYear').val();
			} else {
				var anpromotie = '';
			}
			redirectURL = url+'cauta/'+judet+'/'+oras+'/'+cityID+'/'+tipInstitutie+'/'+institutie+'/'+schoolID+'/'+anpromotie;
			document.location = redirectURL;
			return false;
		}
	});
	
	$('#AdminInstitutionParentId').bind("change", function() {
		judet = filtruParam($('#ProfileJudet :selected').text());
		oras = filtruParam($('#ProfileOras :selected').text());
		tipInstitutie = filtruParam($('#AdminInstitutionParentId :selected').text());
		institutie = filtruParam($('#NumeScoala :selected').text());
		redirectURL = url+'admin/institutions/'+judet+'/'+oras+'/'+tipInstitutie;
		document.location = redirectURL;
		return false;
	});
	
	$('#invitaPrieten').click(function() {
		if($('#mailPrieten').val().length > 0) {
			$('#mailPrieten').removeClass().addClass('textField');
			$.ajax({
				url: url+'users/inviteFriend/' + $('#idUser').val() + '/' + $('#mailPrieten').val(),
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('.invite_message').html(data).slideDown();
	            }
	       });	
		}
		else {
			$('#mailPrieten').removeClass().addClass('error_textField');
			alert('Acest camp nu poate ramane necompletat!');
		}
		return false;
	});
	$('#trimiteMesaj').click(function() {
		var idUser = $(this).attr('class');
		$('#invite_message_'+idUser).slideUp();
		if($('#user-id').text() == '') {
			$('#invite_message_'+idUser).html('<span id="error_invite">Trebuie sa fiti autentificat.</span>').slideDown(1500,function() {
				$(this).slideUp(500);
			});
			return false;
		}
		else {
			return true;
		}
		return false;
	});
		
	$("a[id^='trySendMessage_']").each(function(option){
		$(this).bind('click', function(){
			var userNELogat = $("#userLogatAjax_").get();
			if (userNELogat != ''){
				centerPopup('popupCreeazaGrup');
				loadPopup('popupCreeazaGrup');
				return false;
			}
		})
	});
	
	$("a[id^='tryViewGallery_']").each(function(option){
		$(this).bind('click', function(){
			var userNELogat = $("#userLogatAjax_").get();
			if (userNELogat != ''){
				centerPopup('popupCreeazaGrup');
				loadPopup('popupCreeazaGrup');
				return false;
			}
		})
	});
	
	$("a[id^='editGroupAvatar_']").each(function(option){
		$(this).bind('click', function(){
			var idGroup = $(this).attr('class');
			if (idGroup != ''){
				centerPopup('popupEditeazaAvatarGrup');
				loadPopup('popupEditeazaAvatarGrup', idGroup);
				return false;
			}
		})
	});
	
	$("a[id^='addFriend_']").each(function(option){
		$(this).bind("click", function(){
			var idUser = $(this).attr('class');
			var userNELogat = $("#userLogatAjax_").get();
			if (userNELogat != ''){
				centerPopup('popupCreeazaGrup');
				loadPopup('popupCreeazaGrup');
				return false;
			}
			else
			$.ajax({
				url: url+'users/addFriend/' + idUser,
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('#invite_message_'+idUser).html(data).slideDown(1500,function() {
						$(this).slideUp(500);
					});
	            }
	       });
		});
	});
	
	$("a[id^='addFriendForMessage_']").each(function(option){
		$(this).toggle(
			function(){
				var idUser = $(this).attr('class');
				$(this).css("border","3px solid #add");
				$('#messageToName').attr("value", idUser);
				$('#announceAddedUser').html(idUser + ' a fost selectat').fadeIn('slow');
			},
			function(){
				$(this).css("border","3px solid #fff"); 
				$('#messageToName').attr("value", "");
				$('#announceAddedUser').html('').fadeIn('slow');
			}
		)
	});
	
	$("a[id^='addColleague_']").each(function(option){
		$(this).bind("click", function(){
			var idUser = $(this).attr('class');
			$.ajax({
				url: url+'users/addColleague/' + idUser,
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('#invite_message_'+idUser).html(data).slideDown(1500,function() {
						$(this).slideUp(500);
					});
	            }
	       });
		});
	});
	
	$("a[id^='acceptFriendship_']").each(function(option){
		// idInvitator_idInvitat
		$(this).bind("click", function(){
			chunks = returnChunks($(this).attr('id'));
			var idUser = chunks[1];
			var idFriend = chunks[2];
			$.ajax({
				url: url+'users/acceptFriend/' + idFriend + '/' + idUser,
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('#inviteMessage').html(data).slideDown();
					$('#wrapper_' + idUser + '_' + idFriend).slideUp();
	            }
	       });
		});
	});
	
	$("a[id^='acceptColleagueship_']").each(function(option){
		$(this).bind("click", function(){
			chunks = returnChunks($(this).attr('id'));
			var idUser = chunks[1];
			var idColleague = chunks[2];
			$.ajax({
				url: url+'users/acceptColleague/' + idColleague + '/' + idUser,
	            type: "POST",
	            error: function(error) { alert(error)},
	            success: function(data) {
					$('#inviteMessage').html(data).slideDown();
					$('#wrapper_' + idUser + '_' + idColleague).slideUp();
	            }
	       });
		});
	});
	
	$('#forgotPassword').click(function(){
		var forgotLink = $('#ForgotPasswordSpan');
		var IRemembered = $('#RememberedPasswordSpan');
		var loginForm  = $('#loginForm');
		var forgotPasswordForm = $('#forgotPasswordForm');
		loginForm.slideUp();
		forgotPasswordForm.slideDown();
		forgotLink.hide();
		IRemembered.show();
	});
	$('#IRemembered').click(function(){
		var forgotLink = $('#ForgotPasswordSpan');
		var IRemembered = $('#RememberedPasswordSpan');
		var loginForm  = $('#loginForm');
		var forgotPasswordForm = $('#forgotPasswordForm');
		forgotPasswordForm.slideUp();
		loginForm.slideDown();
		IRemembered.hide();
		forgotLink.show();
	});
	$('#resetPassword').click(function(){
		if($('#UserEmail').val() == '') {
			alert('Campul email nu poate fi gol.');
			return false;
		}
		else {
			email = $('#UserEmail').val();
			$.ajax({
				url: url+'/../users/recoverPass/'+email,
	            type: "POST",
	            success: function(data) {
					if(data) {
	            		$('#formMessages').html(data).fadeIn();
					}
	            }
	       });
		}
	});
	
	$('#ProfileJudet').change(function(){
		$('#ProfileOras').empty();
		$('#ProfileOras').append("<option value=\"\">-=Incarc Orasele=-</option>");
		$.ajax({
			url: url+'membri/getOrase/' + $(this).val(),
            type: "POST",
            dataType: 'json',
            error: function(error) {
            	$('#ProfileOras').empty();
			    $('#ProfileOras').append("<option value=\"\">Fara orase</option>");
            },
            success: function(json) {
            	$('#ProfileOras').empty();
			    $('#ProfileOras').append("<option value=\"\">Alege orasul</option>");
			    $.each(json, function(optionIndex, option) {
			   	   	var html = '<option value="' + optionIndex + '">' + option + '</option>';
			        $('#ProfileOras').append(html);
			   });
            }
       });
	});
	
	/* EMAIL DUPS */
	$('#UserEmail').bind('blur', function(){
	    $('#submitRegisterForm').attr('disabled', 'disabled');
		email = $('#UserEmail').val();
		$.ajax({
			url: url+'/../users/checkEmailDups/'+email,
	        type: "POST",
	        success: function(data) {
				if(data) {
					var valid = false;
	        		if(data == 'false') {
	        			$('#errorMessageEmail img').attr('title', 'Email existent in baza de date');
	        			$('#errorMessageEmailText').html('Email existent in baza de date').fadeIn('slow');
	        			$('#errorMessageEmail').fadeIn('slow');
	        		} else {
	        			$('#errorMessageEmail img').removeAttr('title');
	        			$('#errorMessageEmailText').hide();
	        			$('#errorMessageEmail').hide();
	        			$('#errorMsg').text('Apasati butonul \'Trimite\'').show();
	        			$('#submitRegisterForm').removeAttr('disabled');
	        		}
				}
	        }
	   	});
	});
	/* END EMAIL DUPS */
	
	$('#submitRegisterForm').bind('click', function() {
		if(checkRegisterFields() == false) {
			return false;
		}
		$('#submitRegisterForm').removeAttr('disabled');
	});
	
	$('#submitPropuneScoala').bind('click', function() {
		if(checkPropuneScoalaFields() == false) {
			return false;
		}
		$('#submitPropuneScoala').removeAttr('disabled');
	});
	
	$('#submitClass').bind('click', function() {
		if(checkSubmitClassFields() == false) {
			return false;
		}
		$('#submitClass').removeAttr('disabled');
	});
	
	$('#submitGroup').bind('click', function() {
		if(checkSubmitGroupFields() == false) {
			return false;
		}
		$('#submitGroup').removeAttr('disabled');
	});
	
	$('#submitComment').bind('click', function() {
		if(checkSubmitCommentFields() == false) {
			return false;
		}
		$('#submitComment').removeAttr('disabled');
		var captchaResult = $('#captchaComment');
		if ((captchaLoginA + captchaLoginB) == captchaResult.val()){
			return true;
		}
		else {
			alert('Eroare captcha!');
			return false;
		}
	});

	$('#submitContactForm').bind('click', function() {
		if(checkContactFields() == false) {
			return false;
		}
		$('#submitContactForm').removeAttr('disabled');
	});
	
	$('#submitMessage').bind('click', function() {
		if(checkSubmitMessageFields() == false) {
			return false;
		}
		$('#submitMessage').removeAttr('disabled');
	});
	
	$('#submitScoala').bind('click', function() {
		if(checkSubmitSalveazaScoalaFields() == false) {
			return false;
		}
		$('#submitScoala').removeAttr('disabled');
	});
	
	$('#submitPoza').bind('click', function() {
		if(checkSubmitPozaFields() == false) {
			return false;
		}
		$('#submitPoza').removeAttr('disabled');
	});
	
	$('#submitAlbum').bind('click', function() {
		if(checkSubmitAlbumFields() == false) {
			return false;
		}
		$('#submitAlbum').removeAttr('disabled');
	});
	
	$('#submitAvatar').bind('click', function() {
		if(checkSubmitAvatarFields() == false) {
			return false;
		}
		$('#submitAvatar').removeAttr('disabled');
	});
	
	$('#submitAdminSchool').bind('click', function() {
		if(checkSubmitAdminSchoolFields() == false) {
			return false;
		}
		$('#submitAdminSchool').removeAttr('disabled');
	});
	
	$('#submitAdminUserForm').bind('click', function() {
		if(checkSubmitAdminUserFields() == false) {
			return false;
		}
		$('#submitAdminUserForm').removeAttr('disabled');
	});
	
	/* SHOUTBOX */
	$('#doShout').click(function() {
		var message = $('#shoutText').val();
		var userId = $('#user-id').text();
		if(userId == '') {
			$('#error_shout').html('Trebuie sa fiti autentificat.').fadeIn(1000,function() {
				$(this).fadeOut(3000);
			});
			return false;
		}
		$.ajax({
			url: url+'/../messages/newShout/',
			data: {idUser: userId, msg: message},
            type: "POST",
            error: function(error){ },
            success: function(xml) {
            	$("shout", xml).each(function(id) {
            		var nume = $('user-nume', this).text() + ' ' + $('user-prenume', this).text();
            		var avatar = '../' + $('user-avatar', this).text();
            		var mesaj = $('mesaj', this).text();
            		var data = $('data', this).text();
            		//var shoutBox = '<div class="form" style="padding-bottom:20px; background:url(/clasata/img/shout_bubble2.png) top right no-repeat"><div style="border-top: 1px solid rgb(51, 51, 51); overflow: auto;" class="row"><img align="left" width="70" height="40" alt="" src="/clasata/img/../'+avatar+'"/><p><strong>'+nume+'</strong> a strigat:</p>'+mesaj+'<br/>'+data+'</div></div></div>';
            		var shoutBox = '<div style="padding-bottom:20px; height:140px; background:url(/clasata/img/shout-bg-clasata.png) top left no-repeat"><div style="height:46px; margin-left:20px; padding-top:10px; margin-right:40px; overflow:hidden">'+mesaj+'</div><div style="float:left; width:120px;"><img align="left" width="75" height="57" style="margin-left:17px; margin-top:0px; margin-right:20px" alt="" src="/clasata/img/'+avatar+'"/><span class="DnumeShout">'+nume+'</span></div><div style="width:150px; float:left; padding-top:10px; font-size:10px; position:relative; left:-15px">'+data+'</div></div>';
            		if(id == 0)
            			$('#shoutResponse').empty().append(shoutBox).show();
            		else 
            			$('#shoutResponse').append(shoutBox).show();
            	});
            }
       });
	});
	$('#doShout').bind('mouseover', function(){
		var userId = $('#user-id').text();
		if(userId == '') {
			$('#error_shout').html('Trebuie sa fiti autentificat.').fadeIn(1000,function() {
				$(this).fadeOut(3000);
			});
			return false;
		}
		return true;
	});
	/* END SHOUTBOX */
	/* REFRESH SHOUTS */
	refreshShouts();
	/* END REFRESH SHOUTS */
});

function refreshShouts() {
	var message = $('#shoutText').val();
	var url = $('#url').text();
	var mesaj = $('mesaj', this).text();
	$.ajax({
			url: url + 'shouts.xml',
			type: "POST",
			//dataType: "xml",
            success: function(xml) {
            	$("shout", xml).each(function(id) {
            	//$(xml).find('shout').each(function(){
            		//var id = $(this).attr('id');
					var nume = $('user-nume', this).text() + ' ' + $('user-prenume', this).text();
            		var avatar = '../' + $('user-avatar', this).text();
            		if (avatar == '../') {
            			var sex = $('user-sex', this).text();
            			if (sex == 1) avatar = 'boy.gif';
            			else avatar = 'girl.gif';
            		}
            		var mesaj = $('mesaj', this).text();
            		var data = $('data', this).text();
            		var shoutBox = '<div style="padding-bottom:0px; height:140px; background:url(/clasata/img/shout-bg-clasata.png) top left no-repeat"><div style="height:46px; margin-left:20px; padding-top:10px; margin-right:40px; overflow:hidden">'+mesaj+'</div><div style="float:left; width:120px;"><img align="left" width="75" height="57" style="margin-left:17px; margin-top:0px; margin-right:20px" alt="" src="/clasata/img/'+avatar+'"/><span class="DnumeShout">'+nume+'</span></div><div style="width:150px; float:left; padding-top:10px; font-size:10px; position:relative; left:-15px">'+data+'</div></div>';
            		if(id == 0)
            			$('#shoutResponse').empty().append(shoutBox).show();
            		else 
            			$('#shoutResponse').append(shoutBox).show();
            	});
            }
       });
       setTimeout('refreshShouts()', 15000);
       
/*       
function update(id) {
				$.ajax({
					type: "GET",
					url: "update.php",
					data: "id=" + id,
					dataType: "xml",
					success: function(xml) {
						$(xml).find('shout').each(function(){
							var id_text = $(this).find('id').text();
							var name_text = $(this).find('name').text();
							var date_text = $(this).find('date').text();
							var message_text = $(this).find('message').text();
							$("#chat").append(
								"<div class='id' id='" + id_text + "'>" + 
									"<div class='name'>" + name_text + "</div>" + 
									"<div class='date'>" + date_text + "</div>" + 
									"<div class='message'>" + message_text + "</div>" + 
								"</div>"
							);
						});
					}	
				});      
*/        

/*
$.ajax({
			url: url,
			type: "POST",
			error: function(error){ },
            success: function(xml) {
            	$("shout", xml).each(function(id) {
            		var nume = $('user-nume', this).text() + ' ' + $('user-prenume', this).text();
            		var avatar = $('user-avatar', this).text();
            		var mesaj = $('mesaj', this).text();
            		var data = $('data', this).text();
            		var shoutBox = '<div class="form" style="padding-bottom:20px; background:url(/clasata/img/shout_bubble.png) top right no-repeat"><div style="border-top: 1px solid rgb(51, 51, 51); overflow: auto;" class="row"><img align="right" width="70" height="40" style="padding-right: 13px; padding-top:9px; margin:0 0 20px 10px" alt="" src="/clasata/img/../'+avatar+'"/><p><span class="numeShout">'+nume+'</span> a strigat:</p>'+mesaj+'<br/>'+data+'</div></div></div>';
            		if(id == 0)
            			$('#shoutResponse').empty().append(shoutBox).show();
            		else 
            			$('#shoutResponse').append(shoutBox).show();
            	});
            }
       });
*/
}

function checkRegisterFields() {
	var nume = $('#UserNume');
	var prenume = $('#UserPrenume');
	var sex = $('#UserSex');
	var username = $('#UserUsername');
	var email = $('#UserEmail');
	
	$('#errorMessageNume').hide();
	$('#errorMessagePrenume').hide();
	$('#errorMessageSex').hide();
	$('#errorMessageUsername').hide();
	$('#errorMessageEmail').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageNume').fadeIn('slow');
		mesaj = true;
	}
	if(prenume.val() == '') {
		$('#errorMessagePrenume').fadeIn('slow');
		mesaj = true;
	}
	if(sex.val() == '') {
		$('#errorMessageSex').fadeIn('slow');
		mesaj = true;
	}
	if(username.val() == '') {
		$('#errorMessageUsername').fadeIn('slow');
		mesaj = true;
	}
	if(email.val() == '') {
		$('#errorMessageEmail').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkContactFields() {
	var nume = $('#ContactNume');
	var email = $('#ContactEmail');
	var mesajX = $('#ContactMesaj');
	
	$('#errorMessageNume').hide();
	$('#errorMessageEmail').hide();
	$('#errorMessageMesaj').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageNume').fadeIn('slow');
		mesaj = true;
	}
	if(email.val() == '') {
		$('#errorMessageEmail').fadeIn('slow');
		mesaj = true;
	}
	if(mesajX.val() == '') {
		$('#errorMessageMesaj').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkPropuneScoalaFields() {
	var judet = $('#ProfileJudet');
	var oras = $('#ProfileOras');
	var tipScoala = $('#InstitutionType');
	var numeScoala = $('#InstitutionName');
	
	$('#errorMessageJudet').hide();
	$('#errorMessageOras').hide();
	$('#errorMessageInstitutionType').hide();
	$('#errorMessageInstitutionName').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(judet.val() == '') {
		$('#errorMessageJudet').fadeIn('slow');
		mesaj = true;
	}
	if(oras.val() == '') {
		$('#errorMessageOras').fadeIn('slow');
		mesaj = true;
	}
	if(tipScoala.val() == '') {
		$('#errorMessageInstitutionType').fadeIn('slow');
		mesaj = true;
	}
	if(numeScoala.val() == '') {
		$('#errorMessageInstitutionName').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitClassFields() {
	var nume = $('#className');
	
	$('#errorMessageName').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageName').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitGroupFields() {
	var nume = $('#groupName');
	
	$('#errorMessageName').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageName').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}


function checkSubmitCommentFields() {
	var comment = $('#commentText');
	
	$('#errorMessageComment').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(comment.val() == '') {
		$('#errorMessageComment').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Comentariul nu poate fi gol').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitMessageFields() {
	var to = $('#MessageMailTo');
	var subject = $('#MessageSubiect');
	var message = $('#MessageMessage');
	
	$('#errorMessageTo').hide();
	$('#errorMessageSubject').hide();
	$('#errorMessageMessage').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(to.val() == '') {
		$('#errorMessageTo').fadeIn('slow');
		mesaj = true;
	}
	if(subject.val() == '') {
		$('#errorMessageSubject').fadeIn('slow');
		mesaj = true;
	}
	if(message.val() == '') {
		$('#errorMessageMessage').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitSalveazaScoalaFields() {
	var judet = $('#ProfileJudet');
	var localitate = $('#ProfileOras');
	var tip = $('#InstitutionParentId');
	var numeScoala = $('#NumeScoala');
	var anStart = $('#UserSchoolAnStartYear');
	var anEnd = $('#UserSchoolAnEndYear');
	
	$('#errorMessageJudet').hide();
	$('#errorMessageOras').hide();
	$('#errorMessageTipInstitutie').hide();
	$('#errorMessageInstitutie').hide();
	$('#errorMessageAnInscriere').hide();
	$('#errorMessageAnAbsolvire').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(judet.val() == '') {
		$('#errorMessageJudet').fadeIn('slow');
		mesaj = true;
	}
	if(localitate.val() == '') {
		$('#errorMessageLocalitate').fadeIn('slow');
		mesaj = true;
	}
	if(tip.val() == '') {
		$('#errorMessageTipInstitutie').fadeIn('slow');
		mesaj = true;
	}
	if(numeScoala.val() == '') {
		$('#errorMessageInstitutie').fadeIn('slow');
		mesaj = true;
	}
	if(anStart.val() == '') {
		$('#errorMessageAnInscriere').fadeIn('slow');
		mesaj = true;
	}
	if((anEnd.val() == '') && (!$('#UserSchoolInCurs').attr('checked'))) {
		$('#errorMessageAnAbsolvire').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitPozaFields() {
	var albumID = $('#GalleryAlbumId');
	var nume = $('#UserNume');
	var link = $('#UserFoto');
	
	$('#errorMessageGalleryAlbumID').hide();
	$('#errorMessageGalleryNumePoza').hide();
	$('#errorMessageGalleryPoza').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(albumID.val() == '') {
		$('#errorMessageGalleryAlbumID').fadeIn('slow');
		mesaj = true;
	}
	if(nume.val() == '') {
		$('#errorMessageGalleryNumePoza').fadeIn('slow');
		mesaj = true;
	}
	if(link.val() == '') {
		$('#errorMessageGalleryPoza').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitAlbumFields() {
	var nume = $('#UserNumealbum');

	$('#errorMessageNume').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageNume').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Introduceti un nume pentru album').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitAvatarFields() {
	var link = $('#UserAvatar');

	$('#errorMessageAvatar').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(link.val() == '') {
		$('#errorMessageAvatar').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campul marcat').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitAdminSchoolFields() {
	var judet = $('#ProfileJudet');
	var oras = $('#ProfileOras');
	var tip = $('#InstitutionParentId');
	var nume = $('#InstitutionNume');

	$('#errorMessageJudet').hide();
	$('#errorMessageOras').hide();
	$('#errorMessageType').hide();
	$('#errorMessageNume').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(judet.val() == '') {
		$('#errorMessageJudet').fadeIn('slow');
		mesaj = true;
	}
	if(oras.val() == '') {
		$('#errorMessageOras').fadeIn('slow');
		mesaj = true;
	}
	if(tip.val() == '') {
		$('#errorMessageType').fadeIn('slow');
		mesaj = true;
	}
	if(nume.val() == '') {
		$('#errorMessageNume').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function checkSubmitAdminUserFields() {
	var nume = $('#UserUsername');
	var parola = $('#UserPassword');
	
	$('#errorMessageNume').hide();
	$('#errorMessageParola').hide();
	$('#errorMsg').hide();
	
	var mesaj = false;
	if(nume.val() == '') {
		$('#errorMessageNume').fadeIn('slow');
		mesaj = true;
	}
	if(parola.val() == '') {
		$('#errorMessageParola').fadeIn('slow');
		mesaj = true;
	}
	if(mesaj == true) {
		$('#errorMsg').text('Verificati campurile marcate').show();
		return false;
	} else {
		return true;
	}
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
