var	addLoadEvent=function(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function showAndHide(d,effect){
	if(!effect){
		effect = 'appear';
	}
	new Effect.toggle(d,effect);
	return false;
}

function getCheckedValue(radioObj){
	if(!radioObj){
		return '';
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		if(radioObj.checked){
			return radioObj.value;
		}else{
			return '';
		}
	}
	for(var i = 0; i < radioLength; i++){
		if(radioObj[i].checked){
			return radioObj[i].value;
		}
	}
	return '';
}
function validaNumeros(e) {
	if (browserName=navigator.appName.charAt(0)=="N"){
		var Key=e.which;	
	}else{
		var Key=event.keyCode; 
	}
	var realkey = String.fromCharCode(Key);
	if((realkey=='.')&&(this.value.indexOf('.')<0)){
		return true;
	}
	if (!(((realkey>='0')&&(realkey<='9'))||(Key==8)||(Key==0)) ){
		return false;
	}
	return true;
}
function validaLetras(e) {
	if (browserName=navigator.appName.charAt(0)=="N"){
		var Key=e.which;	
	}else{
		var Key=event.keyCode; 
	}
	var realkey = String.fromCharCode(Key).toUpperCase();
	
	if (!(((realkey>='A')&&(realkey<='Z'))||(Key==8)||Key==32||(Key==0)) ){
		return false;
	}
	
}

function cargarCSS(url){
	var etiquetaStyle=document.createElement("link");
	etiquetaStyle.href=url;
	etiquetaStyle.type="text/css";
	etiquetaStyle.rel="stylesheet";
	document.getElementsByTagName("head")[0].appendChild(etiquetaStyle);
}

function guardar_favorito(anuncio_id){

	new Ajax.Request('/favoritos/add/'+anuncio_id+'/',{
		onSuccess:function(request){
			eval("var response = "+request.responseText+";");
			alert(response['message']);
			if(response['message_code'] == 0){
				goToLogin();
			}
		}
	});
	return false;
}

function goToLogin(){
	window.location = '/usuarios/login/?dest=' + window.location;
}

function getStyle(el,styleProp)
{
	var x = el;
	var sty=styleProp.replace(/(.)([A-Z])/,'$1-$2');
	sty=sty.toLowerCase();
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(sty);
	return y;
}
	function limpiaCampo(field){
		var field = Event.element(field);
		if(field.id == 'LoginContrasena'){
			field.type="password";
		}
		if(field.value == field.title){
			field.value='';
		}else{
			field.select();
		}
	 }
	 function llenaCampo(field){
		var field = Event.element(field);
		if(field.value == ''){
			if(field.id == 'LoginContrasena'){
				field.type="text";
			}
			field.value=field.title;
		
		}
	 }
	addLoadEvent(function(){
		if(document.getElementById('LoginContrasena')){
			var campoPass = document.getElementById('LoginContrasena');
			campoPass.value = '';
			try{
				campoPass.type = 'text';
			}catch(e){}
		}
		var inputs = document.getElementsByTagName('input');
		 for(i in inputs){
		 	if(inputs[i].type == 'text'){
			 	var campo = inputs[i];
			 	Event.observe(campo,'focus',limpiaCampo.bindAsEventListener(campo));
				Event.observe(campo,'blur',llenaCampo.bindAsEventListener(campo));
				if(campo.value==''){
					campo.value = campo.title;
				}
			}
		 }
	 });
	 

timeOutIdioma=0;
function showIdioma(){
	var idiomaActual=$('idiomaActualContenedor');
	idiomaActual.onmouseover=function(){clearTimeout(timeOutIdioma)};
	idiomaActual.onmousemove=function(){clearTimeout(timeOutIdioma)};
	idiomaActual.onmouseout=function(){timeOutIdioma=setTimeout(showIdioma,5000);};
	effect_1 = Effect.toggle('selectTagIdioma','slide',{duration:0.5}); 
	return false;
}

