function update_user_rate(id)
{
  var elem = $(id);
  var inner = elem.innerHTML;
  var name = elem.className;
  if (name)
  {
    var elements = $$('.'+name);
    for(var i=0; i<elements.length; i++)
    {
      elements[i].innerHTML = inner;
    }
  }

}
function goto_anchor(anchor)
{
  window.location.hash=anchor; 
}
function goto_focus(element_id)
{
  //console.debug("jump to "+element_id);
  //throw new Error("jump to "+element_id);
  //alert(element_id). 	
  elem=document.getElementById(element_id);
  elem.scrollIntoView(false);
}

function updateUnreadCount(nr)
{
  if (nr != undefined)
  {
    var elem = $('new_msg');
    if(elem){
	if ( nr > 0)
      elem.innerHTML = '('+nr+')';
    else
      elem.innerHTML ='';
	 } 
  }
}

function updateCount(id, type)
{
		
  var elem = $(id);
  var inner = parseInt(elem.innerHTML);
  if (type == '+')
    inner++;
  else if (inner > 0)
    inner--;
  elem.innerHTML = inner;
 
}

function limitTextarea(limitField, limitNum)
{
  if (limitField.value.length > limitNum)
  {
    limitField.value = limitField.value.substring(0, limitNum);
  }
}

// included tags - fill and submit header form
function fillPostHeaderForm(tag_keyword)
{
  var header_form = document.getElementById('search_by_keyword_zones_form');
  header_form.elements["search_text"].value = tag_keyword;
  header_form.submit();
}


function canGetDuplications(copy_event_id)
{
  if (copy_event_id == 0)
  {//alert('in can get duplication');
    var event_form = $('add_event_form1');
	if(event_form!=null){
		if ((event_form.elements["title"].value != '' &&  event_form.elements["date"].value != '' && event_form.elements["county6"].value != '') && (event_form.elements["city6"].value != '' || event_form.elements["new_city"].value != ''))
		 //alert('return true se poate verifica 4 duplicate');	
		 return true;
	  }
	}
	//alert('return false NU se poate verifica 4 duplicate(completeaza toate fieldurile)');
  return false;
}

function getDuplications()
{
  var event_form = $('add_event_form1');
  result = 'title=' + event_form.elements["title"].value + '&date=' + event_form.elements["date"].value + '&end_date=' + event_form.elements["end_date"].value+ '&county6=' + event_form.elements["county6"].value + '&city6=' + event_form.elements["city6"].value + '&new_city=' + event_form.elements["new_city"].value;
 // alert(result);
  return result;
}


// Image upload popup
function imageUploadPopup(event_id, user_id)
{
  window.open("/adauga-imagine-la-eveniment/"+event_id+"/"+user_id,null,"titlebar=no,location=no,menubar=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=480,height=450");
}

function Length_TextField_Validator()
{
	var form_name=document.getElementById("search_by_keyword_zones_form");
	var search_name=document.getElementById("search_text");
	if ((search_name.value.length < 3) || (search_name.value.length > 30))
	{
		mesg = "Ati introdus " + search_name.value.length + " caracter(e)\n"
		mesg = mesg + "Textul  e necesar sa aiba intre  3 si 30 caractere.\n"
		mesg = mesg + "Va rugam verificati textul de cautare."
		alert(mesg);
		search_name.focus();
		// return false to stop further processing
		return (false);
	}
	// If text_name is not null continue processing
return (true);
}

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   //alert(buttonGroup);
   return retArr;
}
 // Ends the "getSelectedCheckbox" function

// Limited Selection Checkbox Groups
// copyright Stephen Chapman, 26th July 2005
// you may copy this function provided that you retain the copyright notice


function boxchk(obj,max) 
{    
	//var box = obj.name.substr(0,obj.name.lastIndexOf('_')+1);
	var box = obj.name;//.substr(0,obj.name.lastIndexOf('['));
	var a=document.getElementsByName(box);
	//alert(a);
	var cnt=0,i=0;
	//alert(eval('document.'+box+'[i].value'));
	while(eval(a[i])) 
	{
		cnt += eval(a[i].checked);
		//alert('cnt='+cnt);
		i++;
	}
	//var checkBoxArr = getSelectedCheckbox(document.forms[0].box);
	//var checkBoxArr = getSelectedCheckbox(obj);
	//alert('cnt='+cnt);
	if (cnt > max) 
	{
		obj.checked = false;
		alert('Selectati doar ' + max + ' optiuni.\nPentru a alege aceasta optiune deselectati una din optiunile selectate anterior.');
	}
}
function valbutton(thisform,radiogroup_name) {
	myOption = -1;
	if (radiogroup_name.lastIndexOf('[')>-1)
	{
		var radio=document.getElementsByName(radiogroup_name);
	}else{	
		var radio=eval('thisform.'+radiogroup_name);
	}
	for (i=radio.length-1; i > -1; i--) {
		if (radio[i].checked) {
			myOption = i; i = -1;
		}
	}
	return myOption;
}	
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Adresa de email invalida.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Adresa de email invalida.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Adresa de email invalida.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Adresa de email invalida.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Adresa de email invalida.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Adresa de email invalida.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Adresa de email invalida.")
		    return false
		 }

 		 return true					
	}
function checkform(form)
{ 
	if (valbutton(form,'varsta')==-1)
	{
		goto_focus('varsta_1');
		//document.getElementById("varsta_1").focus();
		alert('Selecteaza varsta (intrebarea nr. 1).');
		return false;
	}else if (valbutton(form,'sex')==-1)
	{	
		goto_focus('sex_1');
		alert('Selecteaza sexul (intrebarea nr. 2).');
		return false;
	}else if (document.getElementById("county6").value=='')
	{
		goto_focus('county6');
		alert('Va rugam selectati judetul.');
		return false;
	}else if (document.getElementById("city6").value=='')
	{	
		goto_focus('city6');
		alert('Va rugam selectati orasul.');
		return false;
	}else if (valbutton(form,'prima_vizita')==-1)
	{
		goto_focus('prima_vizita_1');
		alert('Alege un raspuns la intrebarea nr. 4.');
		return false;
	}else if (valbutton(form,'back_because[]')==-1)
	{
		goto_focus('back_because_1');
		alert('Alege cel putin un raspuns la intrebarea nr. 5.');
		return false;
	}else if (valbutton(form,'back_because[]')!=-1 && document.getElementById("back_because_7").checked && document.getElementById("back_because_custom").value=="")
	{
		goto_focus('back_because_1');
		alert('Daca tot ai bifat ultima optiune la intrebarea 5, completeaza  motivul revenirii la noi. ');
		return false;
	}else if (valbutton(form,'freq')==-1)
	{
		goto_focus('freq_1');
		alert('Alege un raspuns la intrebarea nr. 6.');
		return false;
	}else if (valbutton(form,'found_out')==-1)
	{
		goto_focus('found_out_1');
		alert('Alege un raspuns la intrebarea nr. 7.');
		return false;
	}else if (valbutton(form,'found_out')!=-1 && document.getElementById("found_out_6").checked  && document.getElementById("found_out_custom").value=="")
	{
		goto_focus('found_out_1');
		alert('Daca tot ai bifat ultima optiune la intrebarea 7, completeaza  modul in care ne-ai depistat. ');
		return false;
	}else if (valbutton(form,'like[]')==-1)
	{	goto_focus('like_1');
		alert('Alege cel putin un raspuns la intrebarea nr. 8.');
		return false;
	}else if (valbutton(form,'like[]')!=-1 && document.getElementById("like_6").checked  && document.getElementById("like_custom").value=="")
	{
		goto_focus('like_1');
		alert('Daca tot ai bifat ultima optiune la intrebarea 8, completeaza  ce iti place la site. ');
		return false;
	}else if (valbutton(form,'dislike[]')==-1)
	{
		goto_focus('dislike_1');
		alert('Alege cel putin un raspuns la intrebarea nr. 9.');
		return false;
	}else if (valbutton(form,'dislike[]')!=-1 && document.getElementById("dislike_7").checked  && document.getElementById("dislike_custom").value=="")
	{
		goto_focus('dislike_1');
		alert('Daca tot ai bifat ultima optiune la intrebarea 9, completeaza  ce nu-ti place la site. ');
		return false;
	}/*else if (document.getElementById("nume").value=='')
	{
		goto_focus('nume');
		alert('Daca iesi castigator si nu ai completat numele? atunci ce ne facem ?');
		return false;
	}*/else if (document.getElementById("email").value=='')
	{
		goto_focus('email');
		alert('Daca iesi castigator si nu ai completat adresa de email? atunci ce faci?');
		return false;
	}else if (echeck(document.getElementById("email").value)==false)
	{
		goto_focus('email');
		return false;
	}else if (!document.getElementById("agree").checked)
	{
		goto_focus('agree');
		alert('Daca nu esti de acord cu regulamentu de ce ai completat ditamai chestionarul?');
		return false;
	}
	

return true;	
}

function resizeText(div_id,multiplier) {
  var div_obj=document.getElementById(div_id);
  if (div_obj.style.fontSize == "") {
    div_obj.style.fontSize = "1.0em";
  }
  if (parseFloat(div_obj.style.fontSize) > 0.8 && multiplier==-1 || parseFloat(div_obj.style.fontSize) < 1.8 && multiplier==1) {
	div_obj.style.fontSize = parseFloat(div_obj.style.fontSize) + (multiplier * 0.2) + "em";
  }
  
}

// styled browse button
/*
@BEGIN - YOU CAN CHANGE THIS
*/
var sbb_input_class   = 'styledbtn';       // define the class for the input you whant to style
var sbb_big_div       = 'brwbutton';       // define the class for the big div
var sbb_big_div_input = 'brwbutton_input'; // define the class for the input
var brwbutton_file    = 'brwbutton_file';  // define the class for the file info
/*
@END - YOU CAN CHANGE THIS
*/

// let`s start
jQuery(document).ready(function(){

    /*jQuery('.'+sbb_input_class).wrap('<div class="'+sbb_big_div+'"><div class="'+sbb_big_div_input+'"><div class="'+brwbutton_file+' file_new"><span>&nbsp;</span></div></div></div>').show();*/
	// file styled
	jQuery('.'+sbb_input_class).wrap("<div class='"+sbb_big_div+"'><div class='"+sbb_big_div_input+"'></div><div class='brwbutton_file'><span>&nbsp;</span></div></div>").bind("change",function(){
        
		// set some vars
		var button = jQuery(this);
		var parent = button.parent();

		// check fine length
		var fileName = button.val();
		fileName = (fileName.length>=25)?fileName.substr(0,23)+' ...':fileName;

		// add the file name
		if(parent.next().is('div'))
        {   
            //alert(parent.next().html());
            parent.next().html('<span>'+fileName+'</span><a>Remove</a>');
        }
		else 
        {
            
            parent.after('<div style="display:none" class="'+brwbutton_file+'"><span>'+fileName+'</span><a>Remove</a></div>');
		    parent.next().fadeIn('fast');
        }

		// create remove link
		var remove = jQuery('a',parent.next());
        
		remove.click(function(){
            
            //var me = jQuery(this).parent();
            
            var parent = jQuery(this).parent();
            jQuery(this).parent().html('');
			jQuery("<span>&nbsp</span>").appendTo(parent);
            
			/*me.fadeOut('fast',function(){

				jQuery(button).val('');
				me.remove();

			});*/
            //jQuery('.'+sbb_input_class).wrap("<div class='"+sbb_big_div+"'><div class='"+sbb_big_div_input+"'></div><div class='brwbutton_file'><span>&nbsp;</span></div></div>");
		});

	});

});
jQuery.fn.center = function () {
    this.css("position","static");
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
}
function array2get(array_name){//sealizeaza pt get un array de inputuri
	var arr = document.getElementsByName(array_name+"[]");
	var arr_length = arr.length;
	var sum ='?' ;

	for(var i = 0; i < arr_length; ++i)
	{
	sum +='i'+i+'='+(arr[i].value)+'&';
	}
return sum;	
}
function validate_form_promo(form){
	var error=false;
	var error_type=0;
	var cost_featured=0;
	var cost_vip=0;
	var total_necesar=0
	var user_puncte_vinsieu=parseInt(jQuery("#user_puncte_vinsieu").html());
	if(jQuery('#aparitie_pe_prima_pagina').is(':checked')) cost_featured=parseInt(jQuery("#popup_cost_puncte_featured").html());
	if(jQuery('#eveniment_vip').is(':checked')) cost_vip=parseInt(jQuery("#popup_cost_puncte").html());
	total_necesar=cost_featured+cost_vip;
	
	//alert(total_necesar);
	if(!jQuery('#eveniment_vip').is(':checked')&&!jQuery('#aparitie_pe_prima_pagina').is(':checked')){	
			error=true;
			error_type=1;
	}else{
		if(user_puncte_vinsieu<total_necesar){	
			error=true;
			error_type=2;
		}
	}	
	if(error_type==2){
		jQuery("#mesaj_eroare").html('<div id="error_for_error1" class="form_error">Credite insuficiente. Pentru a cumpara PuncteVinSiEu apasati butonul "Cumpara PuncteVinSiEu.</div>');
		jQuery("#validare_client_side_box").css("left", 27 + "%");
		
	}else{
		if(error_type==1){
			jQuery("#mesaj_eroare").html('<div id="error_for_error1" class="form_error">Alegeti cel putin o optiune de promovare.</div>');
			jQuery("#validare_client_side_box").css("left", 37 + "%");
		}
	}
	if(error){
		jQuery("#validare_client_side_box").jqm();
		jQuery("#validare_client_side_box").jqmShow();	
		return false;
	}
	return true;
}
