/****************************************************************************************
*											*
* 	This software has been developed by SOFTENG Corporation. All rights reserved.	*
*											*
* 	The provided source code is for Intersport use, and can not be sold or 		*
*	transferred to third companies.							*
*											*
* 	Warranty voided if source code is modified without SOFTENG permissions.		*
* 											*
*****************************************************************************************/

var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 &&
                parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;

var ns4, ie4;
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;

function layerWrite(id,nestref,text) {
        if (ns4) {
                var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;
                lyr.open();
                lyr.write(text);
                lyr.close();
        }
        else if (ie4) document.all[id].innerHTML = text;
}

function hideLayer(layer) {

  if (isMinNS4)
    layer.visibility = "hide";
  if (isMinIE4)
    layer.style.visibility = "hidden";
}

function showLayer(layer) {

  if (isMinNS4)
    layer.visibility = "show";
  if (isMinIE4)
    layer.style.visibility = "visible";
}

function isVisible(layer) {

  if (isMinNS4 && layer.visibility == "show")
    return(true);
  if (isMinIE4 && layer.style.visibility == "visible")
    return(true);

  return(false);
}

function moveLayerTo(layer, x, y) {

  if (isMinNS4)
    layer.moveTo(x, y);
  if (isMinIE4) {
    layer.style.left = x;
    layer.style.top  = y;
  }
}


var dias = new Array("D","L","M","MI","J","V","S")
var Nombremeses = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
var NombremesesCat = new Array("Gener","Febrer","Març","Abril","Maig","Juny","Juliol","Agost","Setembre","Octubre","Novembre","Desembre");
var NombremesesEng = new Array("January","February","March","April","May","Juny","July","August","September","October","November","December");
var Diasmeses = new Array(31,28,31,30,31,30,31,31,30,31,30,31);


var gl_mesActual="";
var gl_anyoActual="";
var gl_objretorno="";
var gl_idioma = "";
var posdia = -1;
var posmes = -1;

function damePosFecha(){
	if ((gl_idioma == 'esp') || (gl_idioma == 'cat')){
		posdia = 0
		posmes = 1
	}
	else{
		posdia = 1
		posmes = 0
	}
}

function dameNumDiasMes(numMes,anyo){
  if (numMes == 1){
     if (esBisiesto(anyo)) return 29;
	 else return Diasmeses[numMes];
  }
  else return Diasmeses[numMes];
}

function esBisiesto(any) {
	if (((any % 4)==0) && ((any % 100)!=0) || ((any % 400)==0)) return (true);
	else return (false);
}

function verDiaInicial(){
  var factual = new Date();
  //año, mes-1, día
  factual.setFullYear(2008,0,1);
  return factual;
}

function reescribirCalendar(sdia,smes,sany){
   var factual = new Date();
   factual.setFullYear(sany,smes,sdia);
   var nuevaTabla = reescribirTablaCalendar(factual);
   layerWrite('capaCalendar',null,nuevaTabla);
   gl_mesActual = smes;
   gl_anyoActual = sany;
}

function dameTituloMesCalendar(factual){
  var textoTitulo = "";
  var fechanext = new Date();
  var fechaprevious = new Date();
  if (factual.getMonth()+1 > 11) fechanext.setFullYear(factual.getYear()+1,0,1);
  else  fechanext.setFullYear(factual.getYear(),factual.getMonth()+1,1);
  if (factual.getMonth()-1 < 0) fechaprevious.setFullYear(factual.getYear()-1,11,1);   
  else fechaprevious.setFullYear(factual.getYear(),factual.getMonth()-1,1);   
  textoTitulo = "";
  textoTitulo += "<tr width=\"150\" bgcolor=\"#000099\">";
  textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
  textoTitulo += "<td width=\"20\"><a href=\"javascript:reescribirCalendar("+fechaprevious.getDate()+","+fechaprevious.getMonth()+","+fechaprevious.getYear()+");\"><img src=\"/img/fecha_left_blanco.gif\" width=\"5\" border=\"0\"></a></td>";

  if (gl_idioma == 'esp')
  {
    textoTitulo += "<td width=\"100\" colspan=\"5\" align=\"center\"><font class=\"texteMes\">"+Nombremeses[factual.getMonth()]+"&nbsp;"+factual.getYear()+"</font></td>";
  }else
      if (gl_idioma == 'cat')
      {
        textoTitulo += "<td width=\"100\" colspan=\"5\" align=\"center\"><font class=\"texteMes\">"+NombremesesCat[factual.getMonth()]+"&nbsp;"+factual.getYear()+"</font></td>";
      }
      else textoTitulo += "<td width=\"100\" colspan=\"5\" align=\"center\"><font class=\"texteMes\">"+ NombremesesEng[factual.getMonth()] + "&nbsp;"+factual.getYear()+"</font></td>";  
      
  textoTitulo += "<td width=\"20\" align=\"right\"><a href=\"javascript:reescribirCalendar("+fechanext.getDate()+","+fechanext.getMonth()+","+fechanext.getYear()+");\"><img src=\"/img/flecha_right_blanco.gif\" width=\"5\" border=\"0\"></a></td>";  
  textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
  textoTitulo += "</tr>";  
  return textoTitulo;
}

function dameTituloDiasCalendar(){
  var textoTitulo = "";
  if (gl_idioma == "esp")
  {
   textoTitulo = "";
   textoTitulo += "<tr width=\"150\" bgcolor=\"#6666FF\">";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Lu</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Ma</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Mi</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Ju</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Vi</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Sa</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Do</font></td>";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "</tr>";
  }
  else
  if (gl_idioma == "cat"){
   textoTitulo = "";
   textoTitulo += "<tr width=\"150\" bgcolor=\"#6666FF\">";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dl</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dm</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dx</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dj</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dv</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Ds</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Dg</font></td>";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "</tr>";
  }
  else
  {
   textoTitulo = "";
   textoTitulo += "<tr width=\"150\" bgcolor=\"#6666FF\">";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Mo</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Tu</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">We</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Th</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Fr</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Sa</font></td>";
   textoTitulo += "<td width=\"20\" align=\"center\"><font class=\"texte\">Su</font></td>";
   textoTitulo += "<td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
   textoTitulo += "</tr>";
  }  
  return textoTitulo;
}

function calcularDias(fecha){
   var textoTabla = "";
   var fechaInicio = new Date();
   var fechacampo = new Date();
   var datacamp = "";
   var arraydata;
   var anyxifres = "";
   var col, colInicial, i;
   var today = new Date(); 
   damePosFecha()
   if (gl_objretorno != ""){		
		datacamp = document.getElementById(gl_objretorno).value;
		arraydata = datacamp.split("/");
		fechacampo.setFullYear(arraydata[2],Number(arraydata[posmes])-1,Number(arraydata[posdia]));
   }
   fechaInicio.setFullYear(fecha.getYear(),fecha.getMonth(),1);
   colInicial = fechaInicio.getDay();
   if (colInicial == 0) colInicial = 7
   col = 1;
   textoTabla = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
   textoTabla += "<tr width=\"150\">";
   for (col = 1; col< colInicial; col ++){
     textoTabla += "<td width=\"20\" align=\"center\"><img src=\"/img/filer.gif\" width=\"20\"></td>";
   }
   var fin = dameNumDiasMes(fechaInicio.getMonth(),fechaInicio.getYear());
   for (i=1;i <= fin;i++){ 
         if ( (i == (today.getDate()))  && (fecha.getMonth() == today.getMonth()) && (fecha.getYear() == today.getYear())) textoTabla += "<td width=\"20\" align=\"center\"><a href=\"javascript:;\" onclick=\"diaSelec('"+i+"','"+fecha.getMonth()+"','"+fecha.getYear()+"')\"><font class=\"texteSeleccionat\">"+i+"</font></a></td>";		 
		 else {
			if (gl_objretorno != ""){
				if ((fechacampo != "") && (chkdate(document.getElementById(gl_objretorno)) != false)){			 					 		
			 		if ((fechacampo.getMonth() == fecha.getMonth()) && (fechacampo.getYear() == fecha.getYear()) && (Number(arraydata[posdia]) == i)){
						textoTabla += "<td width=\"20\" align=\"center\"><a href=\"javascript:;\" onclick=\"diaSelec('"+i+"','"+fecha.getMonth()+"','"+fecha.getYear()+"')\"><font class=\"texteDiaCamp\">"+i+"</font></a></td>"	
					}
					else textoTabla += "<td width=\"20\" align=\"center\"><a href=\"javascript:;\" onclick=\"diaSelec('"+i+"','"+fecha.getMonth()+"','"+fecha.getYear()+"')\"><font class=\"texte\">"+i+"</font></a></td>";	
				}
				else textoTabla += "<td width=\"20\" align=\"center\"><a href=\"javascript:;\" onclick=\"diaSelec('"+i+"','"+fecha.getMonth()+"','"+fecha.getYear()+"')\"><font class=\"texte\">"+i+"</font></a></td>";
			}
			else textoTabla += "<td width=\"20\" align=\"center\"><a href=\"javascript:;\" onclick=\"diaSelec('"+i+"','"+fecha.getMonth()+"','"+fecha.getYear()+"')\"><font class=\"texte\">"+i+"</font></a></td>";
		 }
	     col ++;
	     if (col > 7) {
		    textoTabla += "</td></tr><tr width=\"150\">";
			col = 1;
		 }	 
   }
   textoTabla += "</table>";
   return textoTabla;
}

function reescribirTablaCalendar(fecha){
  var textoCapa = "";  
  textoCapa += "<table width=\"152\" height=\"110\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
  textoCapa += "<tr valign=\"top\">";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "<td width=\"100%\">";  
	  textoCapa += "<table width=\"150\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
		  textoCapa += dameTituloMesCalendar(fecha);
		  textoCapa += dameTituloDiasCalendar();
		  textoCapa += "<tr><td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";		  
		  textoCapa += "<td colspan=\"7\">";
		       textoCapa += calcularDias(fecha);
		  textoCapa += "</td><td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
		  textoCapa += "</tr>";
	  textoCapa += "</table>";
  textoCapa += "</td>";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "</tr>";
  
  textoCapa += "<tr height=\"1\">";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "<td align=\"right\"><a href=\"javascript:;\" onclick=\"ocultarCalendario()\"><img src=\"/img/asparoja.gif\" width=\"7\" height=\"7\" border=\"0\"></a><img src=\"/img/filer.gif\" width=\"3\" height=\"5\"></td>";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "</tr>";
  
  textoCapa += "<tr height=\"1\"><td height=\"1\" bgcolor=\"#c0c0c0\" colspan=\"3\"><img src=\"/img/filer.gif\" width=\"1\"></td></tr>";
  textoCapa += "</table>";
  return textoCapa;
}

function ocultarCalendario(){
  var micapa;
  micapa = document.getElementById('capaCalendar');
  hideLayer(micapa);
}

function dameTablaCalendar(fecha){
  var textoCapa = "";
  textoCapa += "<!-- capa del calendario -->";
  textoCapa += "<div id=\"capaCalendar\">";
  textoCapa += "<table bgcolor=\"white\" width=\"152\" height=\"110\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
  textoCapa += "<tr valign=\"top\">";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "<td width=\"100%\">";  
	  textoCapa += "<table width=\"150\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
		  textoCapa += dameTituloMesCalendar(fecha);
		  textoCapa += dameTituloDiasCalendar();
		  textoCapa += "<tr><td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";		  
		  textoCapa += "<td colspan=\"7\">";
		       textoCapa += calcularDias(fecha);
		  textoCapa += "</td><td width=\"5\"><img src=\"/img/filer.gif\" width=\"5\"></td>";
		  textoCapa += "</tr>";
	  textoCapa += "</table>";
  textoCapa += "</td>";
  textoCapa += "<td width=\"1\" bgcolor=\"#c0c0c0\"><img src=\"/img/filer.gif\" width=\"1\"></td>";
  textoCapa += "</tr>";
  textoCapa += "<tr height=\"1\"><td height=\"1\" bgcolor=\"#c0c0c0\" colspan=\"3\"><img src=\"/img/filer.gif\" width=\"1\"></td></tr>";
  textoCapa += "</table>";
  textoCapa += "</div>";
  return textoCapa;
}

// Función que concatena ceros a la izquierda del "elemento" hasta conseguir que elemento
// tenga el tamaño de "longitud"
function PonerCeros(elemento, longitud){
while (String(elemento).length<longitud){
     elemento = '0' + elemento ;
}
return elemento;
}

function PonerCerosFecha(obj, formato){
var dia, mes, anyo;
var valor, arrSplit;
   
   valor = obj.value;
   if (valor != ""){
		arrSplit= valor.split("/");
		if (formato=='dd/mm/yyyy'){
			dia = arrSplit[0];
			mes = arrSplit[1];
			anyo = arrSplit[2];
			dia = PonerCeros(dia,2);
			mes = PonerCeros(mes,2);
            if (parseInt(anyo) < 1000) {
               anyo = String(parseInt(anyo)+1900);
            }; 
			obj.value = dia + '/' + mes + '/' + anyo;
		}
		else{ //'mm/dd/yyyy'
			dia = arrSplit[1];
			mes = arrSplit[0];
			anyo = arrSplit[2];
			dia = PonerCeros(dia,2);
			mes = PonerCeros(mes,2);
            if (parseInt(anyo) < 1000) {
               anyo = String(parseInt(anyo)+1900);
           }; 
			obj.value = mes + '/' + dia + '/' + anyo;
		};
   };	
}


function diaSelec(diasel,messel,anysel){
  var miobj, micapa;
  var dia, mes;
  if (gl_objretorno != ""){ 
     miobj = document.getElementById(gl_objretorno);
     dia = PonerCeros(diasel,2);
     mes = PonerCeros((parseInt(messel) + 1),2);
	 if (parseInt(parseInt(anysel)) < 1000) {
		anysel = String(parseInt(anysel)+1900);
	 }
	 if ((gl_idioma == 'esp') || (gl_idioma == 'cat')) miobj.value = dia+'/'+ mes +'/'+anysel;
	 else miobj.value = mes +'/'+ dia +'/'+anysel;
  }  
  micapa = document.getElementById('capaCalendar');
  hideLayer(micapa);
}

function mostrarCalendarioEnPagina(diaActual,mesActual,anyoActual,coorx,coory,objretorno,sidioma){
  var micapa;
  var factual = new Date();
  var datacamp = "";
  var anyxifres= "";
  var arraydata;
  gl_idioma = sidioma;
  damePosFecha()
  if (objretorno != ""){
	datacamp = document.getElementById(objretorno).value;
	if (datacamp != ""){
		arraydata = datacamp.split("/");
		factual.setFullYear(arraydata[2],Number(arraydata[posmes])-1,Number(arraydata[posdia]));
	}
	else factual.setFullYear(anyoActual,mesActual - 1,diaActual);
  } 
  else factual.setFullYear(anyoActual,mesActual - 1,diaActual);
  gl_mesActual = mesActual - 1;
  gl_anyoActual = anyoActual;
  gl_objretorno = objretorno;
  var nuevaTabla = reescribirTablaCalendar(factual);
  layerWrite('capaCalendar',null,nuevaTabla);
  
  micapa = document.getElementById('capaCalendar');
  moveLayerTo(micapa, coorx+15, coory);
  showLayer(micapa);  
}

var txtEstilos;
txtEstilos = "<style>";
txtEstilos += "<!--";
txtEstilos += ".texte { FONT-SIZE: 10px; COLOR: black; FONT-FAMILY: Arial;text-decoration:none }";
txtEstilos += ".texteMes { FONT-SIZE: 11px; COLOR: white; FONT-FAMILY: Arial }";
txtEstilos += ".texteSeleccionat { FONT-SIZE: 10px; COLOR: red; FONT-FAMILY: Arial }";
txtEstilos += ".texteDiaCamp { FONT-SIZE: 10px; COLOR: blue; FONT-FAMILY: Arial }";
txtEstilos += "#capaCalendar {background-color:white;position:absolute; z-index:11; left:1px; top:1px; visibility:hidden;}";
txtEstilos += "-->";
txtEstilos += "</style>";

document.write(txtEstilos);
document.write(dameTablaCalendar(verDiaInicial()));