// JavaScript Document
function corrigeAlturas(){
	var main_height = document.getElementById('main').offsetHeight;
	var right_height = document.getElementById('right').offsetHeight;
	var header_height = document.getElementById('header').offsetHeight;
	var footer_height = document.getElementById('footer').offsetHeight;
	
	var max_height = (main_height > right_height)?main_height:right_height;
	document.getElementById('main').style.height = (max_height) + 'px';
	document.getElementById('right').style.height = (max_height + 40) + 'px';
	
	var browser = navigator.appName;
	if(browser != 'Microsoft Internet Explorer'){
		document.getElementById('content').style.height = (max_height + 40) + 'px';
	}
	//alert(footer_height);
}

function validarUsuario(){
	var usuario = document.getElementById('user').value;
	var password = document.getElementById('pass').value;
	var login = document.getElementById('login').value;
	
	if(trim(usuario) == ""){alert("Escribe tu nombre de usuario.");}
	else if(trim(password) == ""){alert("Escribe tu contraseña.");}
	else{
		iniciarSesion(login, usuario, password);
	}
}


//Función trim
function trim(cadena){
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}
	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	return cadena;
}

//Hace la conexión a la BD y verifica que exista el usuario e inicia la sesión
//si no existe, manda mensaje de error
function iniciarSesion(login, user, pass){
	ajax = new sack('login.php');
	ajax.setVar('login',login);
	ajax.setVar('user',user);
	ajax.setVar('pass',pass);
	ajax.method = 'POST';
	ajax.onCompletion = iniciarSesion_cb;
	ajax.runAJAX();
}
function iniciarSesion_cb(){
	var msg;
	msg = this.response;
	arreglo = msg.split("@|@");
	if(arreglo[arreglo.length-1] != "ok"){
		alert(arreglo[arreglo.length-1]);
	}
	else {
	window.location.href = "index.php";
	}
}

////////////////
//Función para mostrar una imagen en tamaño completo en una nueva ventana
function verImagenCompleta(src){
	var browser = navigator.appName;
	if(browser == 'Microsoft Internet Explorer'){
		//alert(src);
		window.open(src,"Imagen",'width=930,height=630,toolbar=no,directories=no,menubar=no');
	}
	else{
		var imagen=new Image();
		imagen.src = src;
		window.open(src,'Vista completa','toolbar=no,directories=no,menubar=no,width='+(imagen.width+20)+',height='+(imagen.height+20));
	}
}

//Función para crear el combo que muestra las clasificaciones existentes de los artículos
//Busca en la paágina el formulario 'formaBusqueda' y dentro de el agrega el combo
function crearComboClasificaciones(){
	var contenedorFormulario = document.getElementById('search-box');
	var idioma = contenedorFormulario.input_session_idioma.value;
	var htmlFormulario = '';
	var Buscar,Option;
	
	switch(idioma){
		case 0 : Buscar = "Buscar"; Option = "Todas las clasificaciones"; break;		//español
		case 1 : Buscar = "Search"; Option = "All classifications";  break;     		//inglés
	//	case 2 : Buscar = "Buscar"; Option="Todas las clasificaciones";  break;     //francés
	//	case 3 : Buscar = "Buscar"; Option="Todas las clasificaciones";  break;     //alemán
		default : Buscar = "Buscar";
	}
	
	htmlFormulario += '<form name="formaBusqueda" method="post" action="index.php">';
	htmlFormulario += '  <input type="text" name="b">';
	/* crea el select */
	htmlFormulario += '<select name="select_clasificaciones" id="select_clasificaciones">';
	htmlFormulario += '<option value="0">'+Option+'</option>';
	htmlFormulario += '</select>';
	
	htmlFormulario += '  <input name="Buscar" type="submit" id="Buscar" value="'+Buscar+'"';
	htmlFormulario += '</form>';
	
	/* pasamos el nuevo contenido del formulario a la página */
	contenedorFormulario.innerHTML = htmlFormulario;
	alert(contenedorFormulario.innerHTML);
}