/**
photo admin - all the functions deal with the inline change of foto name.
submiting the fotos with ajax, and closing the edit box
*/
function checkenter(e){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		//alert(globalel)//globalel=$(globalel);
		allow();
		submitThePhotoNameAndCloseTheInputText();
		return false
	}
	else{
		return true
	}

}
function submitThePhotoNameAndCloseTheInputText(){
	if(!allowed){
		return;
	}
	if(!$(globalel))
		return;
	allow();
	globalel=$(globalel);
	el=new Element('div');
	el.innerHTML=iner;
	el.childNodes[0].innerHTML=globalel.value;
	$('h'+globalel.id.substr(1)).innerHTML=el.innerHTML;//iner;//type
	new Ajax.Request('/change-photo?format=json&pid='+globalel.id.substr(2),
	{
		method:'post',
		asynchronous:true,
		parameters:{value:globalel.value}
	}
	)
	started=0;
}
var allowed=1;
function nallow(){
	allowed=0;
	setTimeout(allow,300);
}
function allow(){
	allowed=1;
}
var started=0;
var iner;
var globalel;
function inlinechange(el){
	if(started)
	return
	nallow();
	started=1;
	i=el.innerHTML;
	iner=el.innerHTML;
	a=el.childNodes[0].innerHTML;
	el.innerHTML='<input id="a'+el.id.substr(1)+'" onclick="nallow();" onkeypress="checkenter(event)" type="text" value="'+a+'"/>';
	globalel='a'+el.id.substr(1)+''
	$(globalel).focus();
	//	/el.innerHTML=i
}

function del(id){
	new Ajax.Request('/del-photo?format=json&id='+id,
	{
		onSuccess:function(result){
			r=result.responseJSON;
			
			$('div'+id).remove();

		}
	})
}

/**
these are for the create account,
they start by getting the right 
cities with Ajax
by Marius Bucur
*/
function selecteazaoras(){
	//if($('country').options[$('country').selectedIndex].innerHTML=="Romania"){
		filloras();
		s=$('scity')
		o=s.options
		for(i=0;i<o.length;i++){
			if(o[i].value==sc){
				o[i].selected="selected";
			}
		}
	//}
}
function changetara(){
/*
	if($('country').options[$('country').selectedIndex].innerHTML=="Romania"){
		$('tcounty').value="";
		$('tcity').value="";
		$('tcounty').hide();
		$('tcity').hide();
		$('scounty').show();
		$('scity').show();
	}
	else{
		$('tcounty').show();
		$('tcity').show();
		$('scounty').hide();
		$('scity').hide();
	}
	*/
}
function filloras(){
	c=$('scounty');
	o=$('scounty').options;
	i=c.selectedIndex;
	v=o[i].className;
	$('scity').innerHTML="";
	new Ajax.Request('/get-cities?format=json&judet='+v,
	{
		asynchronous:false,
		onSuccess:function(result){
			r=result.responseJSON.orase;
			for(i=0;i<r.length;i++){
				a=new Element('option')
				a.value=r[i].oras
				a.innerHTML=r[i].oras
				$('scity').appendChild(a);	
			}
		}
	})
}
//alert();
function reload(){
	$("captcha").src="/images/cod.png?rand="+Math.random();
}
var poza=1
function anotherpic(){
	if(poza==3){a=$("another").innerHTML;$("another").innerHTML="<font color=\"red\"><!--adaugati --><br/>&nbsp;maxim 3 poze</font>";setTimeout(function(){$("another").innerHTML="<br/>&nbsp;adauga o poza"},3000);return}
	poza++;
	t=new Element('tr');
	t.id="poza"+(poza);
	//a=$('poza'+(poza-1));
	x=new Element("td");
	x.innerHTML="Poza:";
	t.appendChild(x);
	x=new Element("td");
	$("another").remove();
	x.innerHTML="<input type=\"file\" name=\"poza"+poza+"\" style=\"background-color:#ffffff;color:#792811;border:1px #792811 solid\"/>"
	x.innerHTML=x.innerHTML+"<a id=\"another\" href=\"javascript:anotherpic()\"><br/>&nbsp;adauga inca o poza</a>"
	t.appendChild(x);
	a=$("s1");
	a.parentNode.insertBefore(t,a);
}
/**
the vote function
*/
function vote2(id){
	code=$('code').value;
	$('votezone').innerHTML='<img src="/images/ajax-loader.gif"/>'
	new Ajax.Request('/vote/?format=json&code='+code+'&id='+id,
	{
		onSuccess:function(response){
			$('votezone').innerHTML=response.responseJSON.response;
			if(response.responseJSON.ok)
			$('votecount').innerHTML=parseInt($('votecount').innerHTML)+1+" vot(uri)"
		}
	}
	)
}
function vote(id){
	$('votezone').innerHTML='<img src="/images/ajax-loader.gif"/>'
	new Ajax.Request('/vote/?format=json&id='+id,
	{
		onSuccess:function(response){
			$('votezone').innerHTML=response.responseJSON.response;
			if(response.responseJSON.ok)
			$('votecount').innerHTML=parseInt($('votecount').innerHTML)+1+" vot(uri)"
		}
	}
	)
}
