
// GLOBAL VARS        ----------------------------------------------------------
var offerImages;
var numImages = 0;


function eventosgenerales(){
	// Efecto login

	$("#UpDown").click(	
		function (){
			// si header desplegado
			if (this.src=='http://www.anunciosmvm.com/Images/up.gif'){
				$("#HeaderHome").animate({height:5}, "slow");
				$("#UpDown").attr("src","/Images/down.gif");
				$("#Login").attr("style","display:none");
				$("#beta").attr("style","display:none");
				$(".ContentGrid").attr("style","border:0px solid blue;margin-top:-45px;float:left;");
				
			}
			// si header recogido
			else{
				$("#HeaderHome").animate({height:100}, "slow");
				$("#UpDown").attr("src","/Images/up.gif");	
				$("#Login").attr("style","float:left;border:1px solid green;margin-top:30px;display:block;");
				$("#beta").attr("style","display:block;");
				$(".ContentGrid").attr("style","border:0px solid blue;margin-top:0px;float:left;");
			}
		}
	)
	

	
	$("#USER").focus( function () { if ( this.value == "Email" )	{ this.value="";}});
	$("#USER").blur( function () { if ( this.value == "" )	{ this.value="Email"; }});
	
	$("#PASS").focus( function () { if ( this.value == "Contraseña" ) { this.value=""; } });
	$("#PASS").blur( function () { if ( this.value == "" )	{ this.value="Contraseña";}});
	
	$("#logout").click(function (){logout(document,'http://www.anunciosmvm.com');});
	
	// Comprobamos estado inputs
	$("#Chk_Login").click(
		function (){
			if (document.getElementById("PASS").value=='Contreseña' ||  document.getElementById("PASS").value=='')  alert ('Introduzca passwod');
			if (document.getElementById("USER").value=='Email') alert ('Introduzca email');
			if (document.getElementById("USER").value!='Email' && document.getElementById("PASS").type=='password') {Acceso(document.forms[0]);};
		}
	)
	
	// Elimino info proyecto en home
	$(".CloseProject").click(function (){$('#'+this.parentNode.id+'').remove()})
	if (getCookie("SES_ID")) {
		$('#Filtros').html('&nbsp;');
        $('#Listado').html('&nbsp;');
		$('#ListadoProjects').html('&nbsp;');
		CargaIndicador ('ANUNCIOS',document.forms[0])
	};
	
		
}


function Acceso(form){	
	// Envia monta url y envia peticion de acceso
	var action = new String('');
	var post = new String('');
	
	action = "http://www.anunciosmvm.com/ControlAcceso.xsql";
	
	form.elements['PARAMETRO'].value=modificar(montarCadena(montarCadena(form.elements['USER'].value)+'|'+montarCadena(form.elements['PASS'].value)));
	form.elements['SUBPARAMETRO_1'].value=form.elements['USER'].value;
	form.elements['SUBPARAMETRO_2'].value=form.elements['PASS'].value;
	post +='PARAMETRO=' +form.elements['PARAMETRO'].value+'&SUBPARAMETRO_1='+form.elements['SUBPARAMETRO_1'].value+'&SUBPARAMETRO_2='+form.elements['SUBPARAMETRO_2'].value;	
	
	// Peticion acceso
	sendRequest(action,handleRequestAcceso,post);
	return true;
	
}

// Funcion para montar cadenas
function montarCadena(cadena){
	var VAR='';	
	var i=0; 	
	var j=1;
	for(var n=0;n<cadena.length;n++){
	if(esPar(n)){  
    	  	VAR+=cadena.charAt(i); 	   
    	  	i++;
    		}
    		else{ 
    		  VAR+=cadena.charAt(cadena.length-j);
	    	  j++;
    	}
  }
  return VAR;
}
	
	
	
function ExisteEnArray(valor,ArrayFinal){
	var Result=-1;
	for(var n=0;n<ArrayFinal.length;n++){
		if (ArrayFinal[n]==valor) Result=n;
    	}
    	return Result;
}
	
function modificar(cadena){
var VAR='';
var valor;

for(var n=0;n<cadena.length;n++)
{
valor=cadena.charCodeAt(n);
  if(valor>=65 && valor<=90){
//mayusculas
if(valor<=77){
  VAR+=String.fromCharCode(valor+13);
}
else{ 
  VAR+=String.fromCharCode(valor-13); 
}
  }
  else{
if(valor>=97 && valor<=122){
  // minusculas
  if(valor<=109){ 
	VAR+=String.fromCharCode(valor+13);
  }
  else{ 
	VAR+=String.fromCharCode(valor-13); 
  }
}
else{
  if(valor>=48 && valor<=57){
	//numeros
	if(valor<=52){   
	  VAR+=String.fromCharCode(valor+5);  
	}
	else{  
	  VAR+=String.fromCharCode(valor-5);  
	}
  }else{
	// se queda igual
	VAR+=String.fromCharCode(valor); 
  }
}
  }
}
return VAR;
}
	
function esPar(Nu){
	if(Nu%2==0)  return true;
	else  return false;
}



/**
 * Logout
 * @param {string} doc Which cookie to drop
 * @param {string} url Where to go after logout
 * @return undefined
 * @author Martin Gangkofer gangkofer@gmail.com
 */
function logout(doc, url) {
	dropCookie(doc);
	doc.location.href = url;
	
	return undefined;
}

/*CALENDAR*/
var defaultMinDate = null;
var defaultMaxDate = null;

function setOptions(select) {
	popUpCal.hideCalendar();
	// Set defaults
	popUpCal.clearText = 'Clear';
	popUpCal.closeText = '&nbsp;'; /* pongo un gif en lugar de texto */
	popUpCal.prevText = '&lt; Prev';
	popUpCal.nextText = 'Next &gt;';
	popUpCal.currentText = 'Current';
	popUpCal.dayNames = ['Su','Mo','Tu','We','Th','Fr','Sa'];
	popUpCal.monthNames = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosot','Septiembre','Octubre','Noviembre','Diciembre'];
	popUpCal.dateFormat = 'DMY/';
	popUpCal.showOtherMonths = false;
	popUpCal.minDate = defaultMinDate = null;
	popUpCal.maxDate = defaultMaxDate = null;
	popUpCal.changeMonth = true;
	popUpCal.changeYear = true;
	popUpCal.firstDay = 0;
	popUpCal.changeFirstDay = true;
	popUpCal.closeAtTop = true;
	popUpCal.hideIfNoPrevNext = true;
	popUpCal.customDate = null;
	popUpCal.enableFor($('.calendarRange'));
	// Set selected options
	option = select.options[select.selectedIndex].value;
	switch (option) {
		case '1':  
			popUpCal.minDate = defaultMinDate = new Date(2005, 1 - 1, 26);
			popUpCal.maxDate = defaultMaxDate = new Date(2007, 1 - 1, 26);
			popUpCal.hideIfNoPrevNext = false;
			popUpCal.disableFor($('.calendarRange'));
			break;
		case '2':
			popUpCal.firstDay = 1;
			popUpCal.changeFirstDay = false;
			break;
		case '3':
			popUpCal.changeMonth = false;
			popUpCal.changeYear = false;
			break;
		case '4':
			popUpCal.dateFormat = 'MDY/';
			break;
		case '5':
			popUpCal.showOtherMonths = true;
			break;
		case '6':
			popUpCal.closeAtTop = false;
			break;
		case '7':
			popUpCal.customDate = popUpCal.noWeekends;
			break;
		case '8':
			popUpCal.customDate = nationalDays;
			break;
		case '9':
			popUpCal.clearText = 'Enlevez';
			popUpCal.closeText = 'Fermez';
			popUpCal.prevText = '&lt;Pr�c';
			popUpCal.nextText = 'Proch&gt;';
			popUpCal.currentText = 'En cours';
			popUpCal.dayNames = ['Di','Lu','Ma','Me','Je','Ve','Sa'];
			popUpCal.monthNames = ['Janvier','F�vrier','Mars','Avril','Mai','Juin',
				'Juillet','Ao�t','Septembre','Octobre','Novembre','D�cembre'];
			break;
		case '10':
			setActiveStyleSheet('alt');
			break;
	}
	$('.calendarFocus').val('');
	$('.calendarButton').val('');
	$('.calendarBoth').val('');
	$('.calendarRange').val('');
	$('#more div').hide();
	$('#more' + option).show();
}

function setSpeed(select) {
	popUpCal.speed = select.options[select.selectedIndex].value;
}

natDays = [[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'],
	[7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'], [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']];
	
function nationalDays(date) {
	for (i = 0; i < natDays.length; i++) {
		if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
			return [false, natDays[i][2] + '_day'];
		}
	}
	return [true, ''];
}

function customSettings(input) {
	range = (input.className == 'calendarFocus' ? 5 : 
		(input.className == 'calendarButton' ? 7 : 10));
	return {yearRange: '-' + range + ':+' + range, 
		minDate: (input.id == 'dTo' ? getDate($('#dFrom')) : defaultMinDate), 
		maxDate: (input.id == 'dFrom' ? getDate($('#dTo')) : defaultMaxDate)};
}

function getDate(input) {
	fields = input.val().split('/');
	if (fields.length == 3) {
		return new Date(parseInt(fields[2]), parseInt(fields[1]) - 1, parseInt(fields[0]));
	} else {
		return null;
	}
}

function getPopUpDate(element) {
	
	popUpCal.showStandalone(element,$('#'+element).val(),null, setPopUpDate('#'+element));
	//else  popUpCal.hideCalendar(popUpCal.speed);
	
}

function setPopUpDate(element) {
	$('#'+element).val(popUpCal.date);
}

$(document).ready(function () {
	$('#more div').hide();
	$('#more0').show();
	popUpCal.fieldSettings = customSettings;
	$('.calendarFocus').calendar({appendText: ' (default dd/mm/yyyy)'});
	$('.calendarButton').calendar({appendText: ' ', autoPopUp: 'button'});
	$('.calendarBoth').calendar({autoPopUp: 'both', buttonImageOnly: true, 
		buttonImage: 'img/calendar.gif', buttonText: 'Calendar'});
	$('.calendarRange').calendar(); // Same as above
	$('#enableFocus').toggle(
		function () { this.value = 'Enable'; return popUpCal.disableFor($('.calendarFocus')); }, 
		function () { this.value = 'Disable'; return popUpCal.enableFor($('.calendarFocus')); });
	$('#enableButton').toggle(
		function () { this.value = 'Enable'; return popUpCal.disableFor($('.calendarButton')); }, 
		function () { this.value = 'Disable'; return popUpCal.enableFor($('.calendarButton')); });
	$('#enableBoth').toggle(
		function () { this.value = 'Enable'; return popUpCal.disableFor($('.calendarBoth')); }, 
		function () { this.value = 'Disable'; return popUpCal.enableFor($('.calendarBoth')); });
});

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function RestoreDate(init){
	if (init=='start') $("#popUpDateStart").val($("#FECHA_INICIO_HIDDEN").val());
	if (init=='end') $("#popUpDateEnd").val($("#FECHA_FINAL_HIDDEN").val());
}

$(document).ready(eventosgenerales);

