// funcion para imprimir parte de una pagina
var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial(bPintarCab, idioma) {
    if (document.getElementById != null) {
        var html = '<HTML>\n<HEAD>\n';

        if (document.getElementsByTagName != null) {
            var headTags = document.getElementsByTagName("head");
            if (headTags.length > 0)
                html += headTags[0].innerHTML;
        }
        
	      //Estilo para ocultar bloques
	      var iniSty= '<STYLE TYPE=\"text/css\">';
	      var estilo = '.print { display:none; }\n';
	      estilo += '.margen {margin:12px;}\n';
	      estilo += '.body {margin: 0px;	padding: 0px;}\n'
	      var finSty= "</STYLE>";
	      
	      html +=iniSty;
	      html +=estilo;
	      html +=finSty;

        html += '\n</HE' + 'AD>\n<BODY class=\"body\">\n';
        
        var printReadyElem = document.getElementById("printReady");
        
        if (printReadyElem != null) {
            // Si hay que imprimir la cabecera
            if (bPintarCab) {
              html += "<table width=\"580\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
              html += "<tr>";
              html += "<td width=\"580\">";
              html += "<img src=\"/wkn_home/imgs/cab/cabecera580.gif\"></img>";
              html += "</td>";
              html += "</tr>";
              html += "<tr>";
              html += "<td width=\"580px\" align=\"right\" height=\"25px\" class=\"fecha\">";
              html += FechaActual(idioma);
              html += "</td>";
              html += "</tr>";
              html += "</table>\n";
            }
            html += '<div class=\"margen\">\n';
            html += printReadyElem.innerHTML;
            html += '\n</div>';
        } else {
            alert("Could not find the printReady section in the HTML");
            return;
        }
        
        html += '\n</BO' + 'DY>\n</HT' + 'ML>';
        
        var printWin = window.open("","printSpecial","height=550,width=580 ,titlebar=no,toolbar=no");
        printWin.document.open();
        printWin.document.write(html);
        printWin.document.close();
        if (gAutoPrint)
            printWin.print();
    } else {
        //alert("Sorry, the print ready feature is only available in modern browsers.");
        self.print();
    }
}

/***
Basada en printSpecial, pero para imprimir un iframe
****/
function printIFrame(idIframe, urlIframe, idIframe2, urlIframe2 ) {
  
  var html='';
  if (document.getElementById != null) {
      html = '<HTML>\n<HEAD>\n';
      
      
  var iFrame1 = document.getElementById(idIframe);
  var textoBodyIframe1 = iFrame1.contentWindow.document.body.innerHTML;
  var headIframe1 = iFrame1.contentWindow.document.getElementsByTagName("head")[0].innerHTML;
  
  var pestaniaActiva = iFrame1.contentWindow.document.formulario2.activo.value;
  alert('htmlx: '+ idIframe );
  alert('p pestania activa: '+ iFrame1.contentWindow.document.formulario2.activo.value );
  
  
  var textoBodyIframe2='';
  var headIframe2='';
  if( idIframe2 != null  ) {
  //Si hay segundo iFrame aniadir 
      var iFrame2 = document.getElementById(idIframe2);
      textoBodyIframe2 = iFrame2.contentWindow.document.body.innerHTML;   
      idIframe2 = iFrame2.contentWindow.document.getElementsByTagName("head")[0].innerHTML;
  }
  
  var hayFrameSet =textoBodyIframe1.indexOf('FRAME')>0;
  var urlIframe = iFrame1.contentWindow.location.href;
  
  if ( document.getElementsByTagName != null ) {
          var headTags = document.getElementsByTagName("head");
          if (headTags.length > 0)
              html += headTags[0].innerHTML;
  }
  
  html += '\n</HE' + 'AD>';
  html += '\n<BODY>\n';
  
  //borrar boton imprimir
  document.getElementById('tabImprimir').style.display='none';
  
  html += writeCabeceraImpresion(); 
  
  //Esto no vale ya que no funciona cuando el usr a cambiado de pestaña
  html += document.getElementById('divProducto').innerHTML;
  
  //alert('div '+ html );
  
  document.getElementById('tabImprimir').style.display='inline';
  
  /*
  html += headIframe1;      
  html += headIframe2;
  html += writeCabeceraImpresion(); 
  html += textoBodyIframe1;
  html += textoBodyIframe2;
  */
  
  html += '\n</BO'+'DY>\n</HT'+'ML>'; 
  
  gAutoPrint=0;
  var printWin = window.open("","printSpecial");
  printWin.document.open();
  printWin.document.write(html);
  printWin.print();
  printWin.document.close();
  if (gAutoPrint) {
    printWin.print();
  }
  
  return;
  
  if( !hayFrameSet ){ //Si no hay frames (imprimir ya)
    printReadyElem.contentWindow.focus();
    printReadyElem.contentWindow.print();
    return;
  }else { //Decodificar la url del frame que trae &amp; --> &  + -->(espacio)
  
    //document.getElementById(idIframe).contentWindow.focus();
    //document.getElementById(idIframe).contentWindow.print();  
    printReadyElem.contentWindow.document.frames[0].focus;
    printReadyElem.contentWindow.document.frames[0].print();
    return;
  }

   }  
}

/*
  Crea el html de la cabecera de impresion
*/
function writeCabeceraImpresion() {
  
  var htmlCabecera = "";
  //htmlCabecera += " CABECERA IMPRESION ... falta";
  //htmlCabecera += " <P /><P />";
  /**
  document.write(" CABECERA IMPRESION ... falta");
  document.write(<p />"");
  document.write(<p />"");
  **/
  
  htmlCabecera = "<table width='97%'><tr>"
  htmlCabecera += "<td><img src='/wkn_home/imgs/cab/logo_cab.gif'></td>";
  htmlCabecera += "<td align='right'><span class='p50'> / /</span></td>";
  htmlCabecera += "</tr></table></center>";
  htmlCabecera += "<center>";
  
  htmlCabecera2 = "<table width='97%' class='cabfon'><tr>"
  htmlCabecera2 += "<td><img src='/imgs/cab/logo_cab.gif' /></td>";
  htmlCabecera2 += "<td align='right'><span class='p50'></span></td>";
  htmlCabecera2 += "</tr><td></td><td>xxx/hhh</td>";
  htmlCabecera2 += "</tr></table></center>";
  htmlCabecera2 += "<center>";
  
  document.title="Versión para Imprimir";
  
  return htmlCabecera2;
}

function printVentanaFrame( txtHead ,txtFrame ) {
  var html= txtHead + '\n<FRAMESET>\n' + txtFrame;
  html += '\n</FRAMESET>\n</HT' + 'ML>';
  
  var printWin = window.open("","iprintSpecial", "left=20,top=20,width=700,height=500,toolbar=1,resizable=1");
        //printWin.document.open();
        printWin.document.write( html );
        printWin.document.focus();
        printWin.document.print();      
        //printWin.close();
}

// Esta función imprime el frame que se le pase como obj, puede llamarse desde otro frame
function imprimirTirilla(obj){
  var browsername = navigator.appName;
  var browserver = parseInt(navigator.appVersion);
  var browser = "";
  if (browsername == "Netscape" && browserver >= 4) browser="NS4";
  if (browsername == "Microsoft Internet Explorer" && browserver >= 4) browser="IE4";
  if (browser=="IE4") {
    x = navigator.userAgent.lastIndexOf("MSIE");
    version = parseFloat(navigator.userAgent.substring(x+5,x+10)); 
    if (version>=5) browser="IE5";
  }

  if (browser=="NS4"|| browser=="IE5") {
    obj.focus();
    obj.print() ;  
  } else {
    obj.focus();
    if (confirm("Desea imprimir el documento?")){
      var OLECMDID_PRINT = 6;
      var OLECMDEXECOPT_DONTPROMPTUSER = 2;  
      var OLECMDEXECOPT_PROMPTUSER = 1; 
      var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
      document.body.insertAdjacentHTML('beforeEnd', WebBrowser);       
      WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER);
      WebBrowser1.outerHTML = "";
    }
  }
}



function EscribirUnscape( texto ) {
  document.write( unescape( texto ) );   
}

function cambiarCookie(idioma) {
/*
   var expireDate = new Date ();
   var expiredays = 9000;
   expireDate.setTime(expireDate.getTime() + (expiredays * 24 * 3600 * 1000));

   document.cookie = "kn_idioma=" + escape(idioma) + "; expires=" + ExpireDate.toGMTString());
*/
   document.cookie = "kn_idioma=" + idioma + "; expires=Thu, 1 Aug 2100 12:00:00 UTC; path=/";
}

/**
Devuelve un string con la fecha actual formateada segun el idioma (lang).
lang:  es -> dd/mm/aaaa
 eu -> aaaa/mm/dd/
 otro/vacio->aaaa/mm/dd
**/
function FechaActual(lang) {
  var dateAct=new Date();
  
  var dia=dateAct.getDate();
  var mes=dateAct.getMonth()+1;
  var anio=dateAct.getFullYear();
  
  var sep="/";
  var strFecha="";
  if(lang=='es') {
    strFecha = dia + sep + mes + sep +anio;
  }else if( lang='eu') {
    strFecha = anio + sep + mes + sep + dia;
  }else {
    strFecha = anio + sep + mes + sep + dia;
  }
//alert("la fecha es " + strFecha);
  return strFecha;
}


/**
 Imprime un productos LenguetasSolucionesParaTi.jsp mediante el jsp
 /jkn_home/templ/es/ProductosPrint.jsp
 
 idIframe: id iframe lenguetas
 idIframe2: id frema contenido (abajo)
 idioma: es o eu
**/
function printProductoJsp(idIframe, idIframe2, idioma  ) 
{
  var iFrame1 = document.getElementById( idIframe );
  var textoBodyIframe1 = iFrame1.contentWindow.document.body.innerHTML;
  var headIframe1 = iFrame1.contentWindow.document.getElementsByTagName("head")[0].innerHTML;
  
  var pestaniaActiva = iFrame1.contentWindow.document.formulario2.activo.value;
  //alert('Activa: '+ pestaniaActiva );
  
  var productooid = iFrame1.contentWindow.document.formulario2.productooid.value; 
  //alert('producto: '+ productooid );

  var urlPrint = "/jkn_home/tmpl/es/ProductosPrintJS.jsp?activo="+pestaniaActiva+"&productooid="+ productooid;
  urlPrint += "&idioma="+ idioma;
  
  //height=500
  var printWin = window.open(urlPrint,"printSpecial","left=20,top=20,width=603,scrollbars=yes,toolbar=no,resizable=0");
  //printWin.document.open();
  //printWin.document.write(html);
  //printWin.print();
  //printWin.document.close();
  
  return;
}

