// initialiZe variables...
var ppcIE=((navigator.appName == "Microsoft Internet Explorer") || ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion)==5)));
var ppcNN6=((navigator.appName == "Netscape") && (parseInt(navigator.appVersion)==5));

var todayDate	= new Date;
var curDate		= new Date;
var maxYearList	= todayDate.getFullYear()+3;
var minYearList	= 1999;

var names     = new makeArray0('Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic');
var namesI    = new makeArray0('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var namesJ    = new makeArray0('１月','２月','３月','４月','５月','６月','７月','８月','９月','１０月','１１月','１２月');
var days      = new makeArray0( 31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31);
var dow       = new makeArray0('L','M','X','J','V','S','D');
var dowI      = new makeArray0('M','T','W','T','F','S','S');
var dowJ      = new makeArray0('月','火','水','木','金','土','日');
var idioma	= ""

var contador;
var mes2;


function cargarDatos(dia) {
	document.getElementById("fecha").value = dia;
	document.busqueda.submit();
}

function cargarMes(mes) {
	document.getElementById("fechaIni").value = mes;
	document.getElementById("fechaFin").value = mes2;
	document.busqueda.submit();
}

function showCalendar(laX,laY,lan) {
	idioma = lan;
	ppcX = laX;
	ppcY = laY;
	domlay('popupcalendar',1,ppcX,ppcY,Calendar(todayDate.getMonth(),todayDate.getFullYear()));    
}

function Calendar(whatMonth,whatYear) {
	contador = 0;
	var output = '';
	var startMonth;
	var startYear;
	startMonth=whatMonth;
	startYear=whatYear;

	curDate.setMonth(whatMonth);
	curDate.setFullYear(whatYear);
	curDate.setDate(todayDate.getDate());

	auxOutput='<p><SELECT class="mes" NAME="cboMonth" onchange="changeMonth()">';

	for (month=0; month<12; month++) {
		if (month == whatMonth)	{
			if (idioma=="es") {
				auxOutput += '<OPTION VALUE="' + month + '" SELECTED>' + names[month] + '</OPTION>';
			} else if (idioma=="ja") {
				auxOutput += '<OPTION VALUE="' + month + '" SELECTED>' + namesJ[month] + '</OPTION>';
			} else {
				auxOutput += '<OPTION VALUE="' + month + '" SELECTED>' + namesI[month] + '</OPTION>';
			}
		} else {
			if (idioma=="es") {
				auxOutput += '<OPTION VALUE="' + month + '">' + names[month] + '</OPTION>';
			} else if (idioma=="ja") {
				auxOutput += '<OPTION VALUE="' + month + '">' + namesJ[month] + '</OPTION>';
			} else {
				auxOutput += '<OPTION VALUE="' + month + '">' + namesI[month] + '</OPTION>';
			}
		}
    }

	auxOutput += '</SELECT><SELECT class="anyo" NAME="cboYear" onchange="changeYear()">';

	for (year=minYearList; year<maxYearList; year++) {
		if (year == whatYear){
			auxOutput += '<OPTION VALUE="' + year + '" SELECTED>' + year + '</OPTION>';
		} else {
			auxOutput += '<OPTION VALUE="' + year + '">'+ year + '</OPTION>';
		}
	}

	if (whatMonth<9) {
		d="0";
	} else {
		d="";
	}

	var mes = d+(whatMonth+1)+"/01/"+whatYear;

	auxOutput+='</SELECT><a class="buscarCal" href="javascript:cargarMes(\''+mes+'\',\''+mes2+'\');">';

	if (idioma=="es") {
		auxOutput+='buscar</p>';
	} else if (idioma=="ja") {
		auxOutput+='<img src=/icex/Horizontales/ModaEspana/cdaFase2/images/buscar_jp.gif border="0" align="absmiddle"></p>';
	} else {
		auxOutput+='search</p>';
	}

    firstDay = new Date(whatYear,whatMonth,1);
    startDay = firstDay.getDay();
	//startDay me devuelve el dia donde comienza el mes que le he pasado ejem, si empiaza un lunes = 1, si martes = 2 ,etc

	//para el calendario en espa𮪬 si empieza un domingo nos devuelve un 0 y nosotros lo cambiamos a un 7.
	if (startDay==0) {
		startDay=7
	}

	
	//Compruebo si el a𭟥s bisiesto y si lo es el days[1] (mes de febrero) le pongo con 29 d젳, si no se queda con 28
    if (((whatYear % 4 == 0) && (whatYear % 100 != 0)) || (whatYear % 400 == 0)) {
		days[1] = 29;}
    else{
		days[1] = 28;}
		
		var mes;
		
		if (idioma=="es"){
			mes=names[whatMonth];	
		} else if (idioma=="ja") {
			mes=namesJ[whatMonth];
		}else{
			mes=namesI[whatMonth];
		}

    output = '<form name="Cal"><table class="calendario"><caption>' + mes + ' ' + whatYear + '</caption><tbody><tr>';

    for (i=0; i<7; i++) {
		if (idioma=="es") {
			output += '<th>'+ dow[i] +'</th>';
		} else if (idioma=="ja") {
			output += '<th>'+ dowJ[i] +'</th>';
		} else {
			output += '<th>'+ dowI[i] +'</th>';
		}
	}

	output += '</tr>';

	var column = 0;
	var lastMonth = whatMonth - 1;
    var lastYear = whatYear;
    if (lastMonth == -1) {
		lastMonth = 11;
		lastYear=lastYear-1;
	}

	//rellena los espacios a blanco del calenadorio donde todavia no hay dias

	for (i=1; i<startDay; i++, column++) {
        output += getDayLink((days[lastMonth]-startDay+1),true,lastMonth,lastYear);
    }

	//dibuja los d젳 que tiene el mes
	var numSemanas=0;
    for (i=1; i<=days[whatMonth]; i++, column++) {
		output += getDayLink(i,false,whatMonth,whatYear);
		if (column == 6) {
			output += '</tr>\n<tr>';
			numSemanas++;
			column = -1;
        }
    }

    var nextMonth = whatMonth+1;
    var nextYear = whatYear;

	if (nextMonth==12) {
		nextMonth=0;
		nextYear=nextYear+1;
	}

	//rellena el calendario sin dias
	if (column > 0) {
		for (i=1; column<7; i++, column++) {
			output +=  getDayLink(i,true,nextMonth,nextYear);
        }
		if (numSemanas<5){
			output += '<tr>';
			for (j=1;j<=7;j++){output += '<td class="vc">&nbsp;</td>';}
			output += '</tr>';
		}
		output += '</tr></table>';
    } else {
		output = output.substr(0,output.length-4); // remove the <tr> from the end if there's no last row
		if (numSemanas==5){
			output += '<tr>';
			for (j=1;j<=7;j++){output += '<td class="vc">&nbsp;</td>';}
			output += '</tr>';
		}
		if (numSemanas==4 && startDay==1){
			output += '<tr>';
			for (j=1;j<=7;j++){output += '<td class="vc">&nbsp;</td>';}
			output += '</tr>';
			output += '<tr>';
			for (j=1;j<=7;j++){output += '<td class="vc">&nbsp;</td>';}
			output += '</tr>';
		}
		output += '</table></td></tr></table>';
	}

	output += "" + auxOutput + '</form>';

	mes2 = d+(whatMonth+1)+"/"+days[whatMonth]+"/"+whatYear;

    curDate.setDate(1);
    curDate.setMonth(startMonth);
    curDate.setFullYear(startYear);


    return output;
}

function domlay(id,trigger,lax,lay,content) {
	// Hacer visible el calendario
	document.getElementById(''+id+'').style.visibility = "visible"

	// Posicionar el calendario Horizontal
	document.getElementById(''+id+'').style.left=lax+"px"

    // Posicionar el calendario Vertical
	document.getElementById(''+id+'').style.top=lay+"px"

    // Cambiar el contenido


    if (content){
		if (document.all){
			document.all[''+id+''].innerHTML = content;
		} else if (document.getElementById){
			//Thanx Reyn!
			rng = document.createRange();
			el = document.getElementById(''+id+'');
			rng.setStartBefore(el);
			htmlFrag = rng.createContextualFragment(content)
			while(el.hasChildNodes()) {
				el.removeChild(el.lastChild);
			}
			el.appendChild(htmlFrag);
			// end of Reyn ;)
        }
    }

}

function getDayLink(linkDay,isGreyDate,linkMonth,linkYear) {

	if (linkDay<10)	{
		var d = "0";
	} else {
		var d = "";
	}
	if (linkMonth<9) {
		var m = "0";
	} else {
		var m = "";
	}
	var dia = m + (1+linkMonth)+ "/" +  d + linkDay + "/" + linkYear;
	var templink;

	//si el valor del parametro es true, no le asigna ningun valor, ya que son huecos en blanco
	if (isGreyDate) {
		templink='<td class="vc">&nbsp;</td>';
	}
	//en el caso de que sea un d쟬 pregunto si es el d쟠de hoy y lo pongo en otro estilo, en nuestro caso lo asignaremos a si hay ferias ese d쟍
	else {
		if (isDayToday(linkDay)) {
			templink='<td>' + '<a style="color: #B00000" onmouseover="self.status=\' \';return true" href="javascript:cargarDatos(\''+dia+'\');">' + linkDay + '</a>' +'</td>';
		} else {
			templink='<td>' + '<a onmouseover="self.status=\' \';return true" href="javascript:cargarDatos(\''+dia+'\');">' + linkDay + '</a>' +'</td>';
		}
	}
    return templink;

}

//obtengo el d쟠de hoy
function isDayToday(isDay) {
	if ((curDate.getFullYear() == todayDate.getFullYear()) && (curDate.getMonth() == todayDate.getMonth()) && (isDay == todayDate.getDate())) {
		return true;
	} else {
        return false;
    }
}

function changeMonth() {
    curDate.setMonth(document.forms["Cal"].cboMonth.options[document.forms["Cal"].cboMonth.selectedIndex].value);
	domlay('popupcalendar',1,ppcX,ppcY,Calendar(curDate.getMonth(),curDate.getFullYear()));

}

function changeYear() {
    curDate.setFullYear(document.forms["Cal"].cboYear.options[document.forms["Cal"].cboYear.selectedIndex].value);
	domlay('popupcalendar',1,ppcX,ppcY,Calendar(curDate.getMonth(),curDate.getFullYear()));

}

function makeArray0() {
    for (i = 0; i<makeArray0.arguments.length; i++)
        this[i] = makeArray0.arguments[i];
}
