//var AjaxO = new Ajax();
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var ak='';




function toogle_display(id){
	var display = document.getElementById(id).style.display;
	if(display=='none') document.getElementById(id).style.display='';
	if(display=='') document.getElementById(id).style.display='none';
}

function toogle_check(id){
	var display = document.getElementById(id).className;
	if(display=='checkbox_on') document.getElementById(id).className='checkbox_off';
	if(display=='checkbox_off') document.getElementById(id).className='checkbox_on';
}




function toogle_header(is_logged){
	var link_class = document.getElementById('toogle_header').className;
	if(link_class=='off')
		{
			$$('#banner img').invoke('show');
            $('toogle_header').removeClassName('off');
            $('toogle_header').addClassName('on');
            if(is_logged) new Ajax.Request('jax.php?op=settings&header=on');
		}
	if(link_class=='on')
		{
			$$('#banner img').invoke('hide');
            $('toogle_header').removeClassName('on');
            $('toogle_header').addClassName('off');
            if(is_logged) new Ajax.Request('jax.php?op=settings&header=off');
		}
}




function clearMsg(){
	if(document.getElementById('msg'))
	{
    var is_msg = document.getElementById('msg').style.display;
	if (is_msg=="") setTimeout("hide('msg')", 5000);
	}
}

// -----------------------------------------------------------------------------------------------   onload

Event.observe(window, 'load', function() {
        if($('msg')) $('msg').hide.delay(3);
});


function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}


function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

// ---------------------------------------------------------------------------------------------------------------------------------                          Modal login req

function sendLoginInfo(){
        new Ajax.Request('loginAction.php', {
        parameters: $('modal_login_form').serialize(),
        onComplete: function (transport) {
        ak=transport.responseText;
            if(ak==1)
            {
                window.location.href='profil.php';
                return false;
            }
            else{
                $('modal_error').innerHTML="Hmm... ceva nu-i bun, mai incearca. <br/>Daca nu si nu <a href='forgot.php'>reseteaza parola</a>.";
                $('modal_error').className='error';
            }
        }
        });
}

// ---------------------------------------------------------------------------------------------------------------------------------                          Adauga o noua comunitate

function AdaugaComunitate(comunitate){
    new Ajax.Request('jax.php?op=AdaugaComunitate&comunitate='+comunitate, {
        onComplete: function () {
            $('com_con').innerHTML='Comunitatea va deveni activa si veti primi o notificare cand un administrator o va aproba.';
            setTimeout(function (){window.location.href='profil.php?op=edit'; }, 3000);
            }
        });
}

// ---------------------------------------------------------------------------------------------------------------------------------                         Seteaza un mesaj ca citit

function setRead(id){
    new Ajax.Request('jax.php?op=setRead&id='+id, {
        onComplete: function () {
            $('open_'+id).style.fontWeight='normal';
            }
        });
}


// ---------------------------------------------------------------------------------------------------------------------------------                         Adauga inca un mail

function MoreEmails(){
    the_size = $$('#em_ad_container input').size();
    the_size++;
    $('em_ad_container').insert({bottom: '<input type="text" name="email_'+the_size+'" class="field" size="60" />'});

}

// ---------------------------------------------------------------------------------------------------------------------------------                         Adauga inca un mail

function unsubscribe(id, la){
    new Ajax.Request('jax.php?op=unsubscribe&la='+la+'&id='+id, {
        onComplete: function () {
            if(la=='topic') $('subscribe_container').innerHTML='<label><input name="subscribe" type="checkbox" value="1" /> Vreau sa primesc notificari cand se scriu raspunsuri la acest topic.</label>';
            if(la=='stire') $('subscribe_container').innerHTML='<label><input name="subscribe" type="checkbox" value="1" /> Vreau sa primesc notificari cand se scriu comentarii la aceasta stire.</label>';
            if(la=='interviu') $('subscribe_container').innerHTML='<label><input name="subscribe" type="checkbox" value="1" /> Vreau sa primesc notificari cand se scriu comentarii la acest interviu</label>';
            }
        });
}


// ---------------------------------------------------------------------------------------------------------------------------------                          Sterge o fotografie

function StergeFotografia(id){
    new Ajax.Request('jax.php?op=StergeFotografia&id='+id, {
        onComplete: function (transport) {
            $('pic_'+id).hide();
            }
        });
}




// ---------------------------------------------------------------------------------------------------------------------------------                         Inline edit

function showEdit(elem){
    $(elem).down('a').show();
    $(elem).addClassName('hover_row')
}

function hideEdit(elem){
    $(elem).down('a').hide();
    $(elem).removeClassName('hover_row');
}

function EditInline(id, camp, tabela){
    var actual=$(id).down('span').innerHTML;
    if($(id).select('input').size()==0)
    {
        $(id).down('span').innerHTML='<input type="text" name="test" id="current_edit" value="'+actual+'" class="inline_field"  maxlength="254" />';
        $(id).down('a').hide();
        $('current_edit').focus();
        $('current_edit').observe('keydown', function (event) {
            if (event.keyCode == Event.KEY_RETURN) {
                new Ajax.Request('jax.php', {
                    method: 'get',
                    parameters: {
                        op:		'editInline',
                        camp:		camp,
                        tabela:		tabela,
                        valoare:	encodeURIComponent($('current_edit').value)
                        },
                    onComplete: function () {
                         $(id).down('span').update($('current_edit').value);
                        }
                });
                }
            });
    }
}


function validEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
   }
   else return true;
}

//----------------------------------------------------------------------------------------------------- Signup form
function setCountry(val, where){
    if(val=="") {$('other_tara').hide(); return false;}
    if(val=="other")
    {
            //open field
            $('other_tara').show();
            $('other_regiune').show();
            $('other_oras').show();
            $('other_tara').down('input').focus();
            $('selector_regiune').show();
            $('selector_oras').show();
            // disable selectors
            $('selector_regiune').down('select').value='other';
            //$('selector_regiune').down('select').disable();
            $('selector_oras').down('select').value='other';
            //$('selector_oras').down('select').disable();

            $('selector_regiune').down('select').select('option').reject(function (option) {
                return option.value == "" || option.value == 'other';
            }).invoke('remove');
            $('selector_oras').down('select').select('option').reject(function (option) {
                return option.value == "" || option.value == 'other';
            }).invoke('remove');
    }
    else
    {
        $('other_tara').hide();
        $('other_regiune').hide();
        $('other_oras').hide();
        new Ajax.Request('jax.php?op=PopuleazaRegiuni&tara='+val+'&where='+where, {
            onComplete: function (transport) {
                ak=transport.responseText;
                $('selector_regiune').update(ak);
                }
            });

    }
    $('selector_regiune').show();
}

function setRegion(val, where){
    if(val=="") {$('other_regiune').hide(); return false;}
    if(val=="other")
    {
            //open field
            $('other_regiune').show();
            $('other_oras').show();
            $('other_regiune').down('input').focus();
            $('selector_oras').down('select').value='other';
            $('selector_oras').down('select').select('option').reject(function (option) {
                return option.value == "" || option.value == 'other';
            }).invoke('remove');

            //$('selector_oras').down('select').disable();
    }
    else
    {
        $('other_regiune').hide();
        $('other_oras').hide();
        new Ajax.Request('jax.php?op=PopuleazaOrase&regiune='+val+'&where='+where, {
            onComplete: function (transport) {
                ak=transport.responseText;
                $('selector_oras').update(ak);
                }
            });
    }
    $('selector_oras').show();
}

function setCity(val){
    if(val=="") {$('other_oras').hide(); return false;}
    if(val=="other")
    {
            //open field
            $('other_oras').show();
            $('other_oras').down('input').focus();
    }
    else $('other_oras').hide();
}

/////////////   notificari albume

function notificariAlbume(action, id){
    if(action)
    {
       new Ajax.Request('album.php?action='+action+'&id='+id, {
            onComplete: function (transport) {
                ak=transport.responseText;
                $('link_notificare').update(ak);
                }
            });
    }
    return false;
}















