// JavaScript Document
window.document.write('<script language="javascript" type="text/javascript" src="../js/XHConn.js"></script>');
window.document.write('<script language="javascript" type="text/javascript" src="../js/compruebacampos.js"></script>');
window.document.write('<script language="javascript" type="text/javascript" src="../js/cookies.js"></script>');
var strbussy = '<p style="text-align:center;"><img src="../img/loading.gif" /></p>';
var desde;
var hasta;
var texto;
var lugar;
var area;
//var curso;
var titulo;
var tipo;
//____________________________________________________________________________________

function Enviar(){

	var Campos = new Array();
	var CamposPracticas = new Array();
	Campos['obligatorios'] = new Array('titulo',
									   'empresa',
									   'nombre',
									   'cargo',
									   'cif',
									   'direccion',
									   'ciudad',
									   'cp',
									   'provincia',
									   'telefono',
									   'fecha',
									   'email',
									   'perfil',
									   'candidatos',
									   'area',
									   'preguntas',
									   'recibir_info');



	Campos['emails'] = new Array('email', 'remail');
	Campos['fechas'] = new Array('fecha');


	if (Practicas == 1){

		CamposPracticas = Array( 'responsable',
								   'duracion',
								   'fecha_comienzo',
								   'departamento',
								   'horario_dias',
								   'tutor',
								   'ayuda',
								   'candidatos');

	}else{
		CamposPracticas[CamposPracticas.length] = 'tipo_contrato';
		CamposPracticas[CamposPracticas.length] = 'lugar';
		CamposPracticas[CamposPracticas.length] = 'horario';
		CamposPracticas[CamposPracticas.length] = 'remuneracion';
		if (document.frm.tipo_contrato.value == 5){
			CamposPracticas[CamposPracticas.length] = "tipo_contrato2";
		}
	}

	for(var i=0; i<CamposPracticas.length;i++){
		Campos['obligatorios'][Campos['obligatorios'].length] = CamposPracticas[i];
	}
	/*var texto = "";
	for (var z=0; z<Campos['obligatorios'].length; z++){
		texto += Campos['obligatorios'][z]+"\n";
	}
	texto +=Campos['obligatorios'].length;
	alert(texto);*/
	if (document.frm.email.value != document.frm.remail.value){
		alert('El email es distinto en ambos campos');
		document.frm.remail.focus();
		return;
	}
	if (CompruebaCampos(Campos, document.frm)){
		document.frm.submit();
	}
}
//____________________________________________________________________________________

function CambiaCategoria(pagina){

	document.getElementById('divOfertas').innerHTML = "<img style='width:auto; height:auto' src='../img/calendar/loading.gif' />&nbsp;<br clear='all' />";
	data('./ofertas.cod.php', 'accion=muestraofertas&pagina='+pagina, ImprimeResultadosBusqueda);
}
//___________________________________________________________________________________________________

function CambiaPagina(pagina){

	document.getElementById('divOfertas').innerHTML = "<img style='width:auto; height:auto' src='../img/calendar/loading.gif' />&nbsp;<br clear='all' />";
	data('./ofertas.cod.php', 'accion=muestraofertas&pagina='+pagina, ImprimeResultadosBusqueda);

}
//___________________________________________________________________________________________________

function ImprimeResultadosBusqueda(xml){

	var resultado = xml.getElementsByTagName('resultado');
	var paginado = xml.getElementsByTagName('paginado')[0];

	//Pintamos el Resultado

	var text_resultado = "";

	for(var i=0;i<resultado.length;i++){
		var res = resultado[i];

		var id = res.getElementsByTagName('id')[0].firstChild.data;
		var fecha = res.getElementsByTagName('fecha')[0].firstChild.data;
		var nombre = res.getElementsByTagName('nombre')[0].firstChild.data;
		var lugar = "";//res.getElementsByTagName('lugar')[0].firstChild.data;
		var descripcion = res.getElementsByTagName('descripcion')[0].firstChild.data;
		var categoria = "";//res.getElementsByTagName('categoria')[0].firstChild.data;

		descripcion = replace("&lt;br /&gt;", "<br />", descripcion);

		/*text_resultado += '<img src="../uploads/descargas/'+id+'/'+img+'" />\n';
		text_resultado += '<p>\n<span class="tit">\n<strong>'+nombre+'</strong>\n</span><br /><br />';
		text_resultado += '<strong>Descripcion:</strong><br />'+descripcion+'<br />';
		text_resultado += '<strong>Peso:</strong> '+tamanio+' Kb.<br>';
		text_resultado += '<br />\n<span class="tit">\n<a href="../uploads/descargas/'+id+'/'+url+'"><strong> Descargar</strong></a>\n</span><br />';
		text_resultado += '</p>\n<br clear="all" />\n<div class="line"></div>\n<br />';
		*/

		text_resultado += '<p>\n<!--<a href="'+id+'-oferta.php?" class="tit">-->\n';
		text_resultado += '<strong class="pink">'+fecha+'</strong>\n<strong>'+nombre+'</strong> '+lugar+'\n<!--</a>--><br />\n';
		text_resultado += '\n<strong class="date">'+categoria+'</strong><br />\n';
		text_resultado += descripcion+"\n</p><br />\n";
		text_resultado += "<div class='line'></div><br />";
		/*
		<p>
			<a href="oferta.php" class="tit">
				<strong class="pink">30-01-2007</strong>
				<strong>Diseñador Gráfico.</strong> Madrid.
			</a><br />

			<strong class="date">Beca</strong><br />

			Morbi mollis elementum felis. Aliquam erat volutpat.
			Phasellus ut nunc ac mauris pellentesque bibendum.

		</p>
		<br />
		*/
	}

	if (resultado.length == 0){
		text_resultado = 'No hay ofertas para la categor&iacute;a';
	}

	//document.getElementById('h2Resultados').style.display='';

	document.getElementById('divOfertas').innerHTML = text_resultado;

	document.getElementById('divPaginado').innerHTML = paginado.getElementsByTagName('text')[0].firstChild.data;

}
//___________________________________________________________________________________________________

function GeneraDDLCurso(select){

	//var texto = document.frm.texto.value;
	var area = document.frm.area.value;

	if (select.value.length > 0){
		page('divCursos', 'ofertas.cod.php', 'accion=generaddlcurso&area='+area, '<select name="curso"><option value="-1">Espere...</option></select>');
	}else{
		document.getElementById('divCursos').innerHTML = '<select name="curso"><option value="-1">&nbsp</option></select>';
	}
}
//___________________________________________________________________________________________________

function Buscar(){

	/*if (document.frm.fecha_desde.value.toUpperCase() == "DD/MM/AAAA") document.frm.fecha_desde.value = "";
	if (document.frm.fecha_hasta.value.toUpperCase() == "DD/MM/AAAA") document.frm.fecha_hasta.value = "";*/


	//texto = document.frm.texto.value;
	//lugar = document.frm.lugar.value;
	tipo = document.frm.tipo.value;
	area = document.frm.area.value;
	//curso = document.frm.curso.value;
	titulo = document.frm.titulo.value;
	/*desde = document.frm.fecha_desde.value;
	hasta = document.frm.fecha_hasta.value;*/

	//var campos = Array();
	//campos['fechas'] = Array('fecha_desde', 'fecha_hasta');

	//if (CompruebaCampos(campos, document.frm)){
		document.getElementById('mensajes').style.display='';
		document.getElementById('divResultados').innerHTML = strbussy;
		data('ofertas.cod.php', 'accion=buscar&titulo='+titulo+'&area='+area+'&tipo='+tipo, PintaResultados);
	//}
		guardaCokiees();
}
//___________________________________________________________________________________________________
function CambiaPagina2(n){
	document.getElementById('mensajes').style.display='';
	document.getElementById('divResultados').innerHTML = strbussy;
	data('ofertas.cod.php', 'accion=buscar&titulo='+titulo+'&area='+area+'&tipo='+tipo+'&pagina='+n, PintaResultados);
		guardaCokiees();
}
//___________________________________________________________________________________________________

function PintaResultados(xml){

	var resultado = xml.getElementsByTagName('resultado');
	var paginado = xml.getElementsByTagName('paginado')[0];

	//Pintamos el Resultado

	var text_resultado = "";

	for(var i=0;i<resultado.length;i++){
		var res = resultado[i];
		var fecha = "";//res.getElementsByTagName('fecha')[0].firstChild.data;
		var tipo = res.getElementsByTagName('tipo')[0].firstChild.data;
		if (tipo==1){

			tipo="Oferta Empleo";

		}else{

			tipo="Oferta Practicas";
		}
		var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
		var lugar = res.getElementsByTagName('lugar')[0].firstChild.data;
		var id = res.getElementsByTagName('id')[0].firstChild.data;
		//var fecha = res.getElementsByTagName('fecha')[0].firstChild.data;
		var perfil=res.getElementsByTagName('perfil')[0].firstChild.data;
		perfil=replace('\n','<br/>',perfil);

		//perfil=perfil.replace('\n\gi','<br/>');
		//text_resultado += '<div class="fila">\n <div class="left4"><strong class="date">'+fecha+'</strong>\n</div>\n';
		text_resultado += '<div class="left2">\n<strong class="date">'+titulo+'</strong>\n</div>\n';
		text_resultado += '<div class="left3">\n<strong>'+tipo+'</strong><br/>'+perfil+'<a href="'+id+'-oferta.html" class="pink"> <strong>más info</strong></a></div>\n';
		text_resultado += '<div class="left1">'+lugar+'</div>\n';
		text_resultado += '<br clear="all" />\n</div>\n';
	}

	if (resultado.length == 0){
		text_resultado = '<div class="fila">\nNo hay resultados con los criterios seleccionados</div>';
	}



	document.getElementById('labelResultados').style.display='';

	document.getElementById('divResultados').innerHTML = text_resultado;

	document.getElementById('divPaginado').innerHTML = paginado.getElementsByTagName('text')[0].firstChild.data;


}
//___________________________________________________________________________________________________
function guardaCokiees(){

	setCookie('area',area);
//	setCookie('curso',curso);
	setCookie('titulo',titulo);
	setCookie('tipo',tipo);

}
//___________________________________________________________________________________________________
function RestauraValores(){

	restaurar = true;

	area = getCookie('area');
	//curso = getCookie('curso');
	titulo = getCookie('titulo');
	tipo = getCookie('tipo');
	//pagina = getCookie('pagina');



	//tmp_curso = curso;
	//Marcamos los selects con los valores de la cookie


	PosicionaSelect(document.frm.area, area+"");
	PosicionaSelect(document.frm.tipo, tipo+'');
	//functionlater = 'RestauraCurso('+getCookie('curso')+');';

	//setTimeout(eval(functionlater_), 5000);

//	functionlater = "PosicionaSelect(document.frm.curso, curso+'');CambiaPagina2("+tipo+");";

	CambiaArea(document.frm.area, tipo);
//	curso = tmp_curso;






	document.frm.titulo.value=titulo;



}
//___________________________________________________________________________________________________


// function RestauraCurso(c){
//
// 	//alert("curso="+c+" "+document.frmGaleria.curso.options.length);
// 	//restaurar = false;
//
//
// 	//PosicionaSelect(document.frmGaleria.curso, c+"");
// 	//CambiaCurso(document.frmGaleria.curso);
// 	//curso = c;
//
// 	CambiaPagina2(pagina, tipo);
//
// }
function CambiaArea(select, t){

	//functionlater="CambiaArea2('"+select.value+"','"+t+"')";
	tipo = t;
	area = select.value;
	//curso = document.frm.curso.value;
	//page('divCursos',"./ofertas.cod.php", 'accion=imprimecursos&area='+select.value, "<select><option>Espere...</option></select>");

}
