// Contiene funciones utilitarias
// TODO: Validación del Area Javascript

var P_ART = true;
var P_SECCION = '';

// -----------------------------------------------------------------------------
// Crea URL No friendly
function creaUrlOld(urlfriendly){
    var arr= urlfriendly.split("/");
    var oldUrl="";
    pat = /\/site\/artic\/\d{8}\/pags\/\d{14}\.html$/;
    if (pat.test(urlfriendly)){
      return urlfriendly;
    } else{
      if (arr.length > 0) {
        var artic=arr[5].replace(/-/g,"");
        oldUrl=arr[0]+"//"+arr[2]+"/"+arr[4]+"/site/artic/"+artic+"/pags/"+artic+arr[6];
      }
    }
    return oldUrl;
}
// -----------------------------------------------------------------------------
// Abre la ventana para enviar un artículo por mail
function enviar(titu) {
  var titular;
  titular = (typeof titu == 'undefined') ? '' : titu;
  var dirUrl = creaUrlOld(document.URL);
  var loc = '/prontus_nots/stat/enviar/formulario.html?_URL=' + escape(dirUrl);
      loc = loc + '&_TITULAR=' + escape(titular);
  var envia = window.open(loc,'enviar', 'toolbar=0,status=0,menubar=0,scrollbars=0,resizable=1,location=0,directories=0,width=420,height=445');
  envia.focus();
}
// -----------------------------------------------------------------------------
// Imprime noticia actual.
function imprimir() {
    var dirUrl = creaUrlOld(document.URL);
    var loc = '/cgi-bx/imprimir.cgi?_URL=' + escape(dirUrl);
    var alto=screen.availHeight;
    var ancho=screen.availWidth/2;
    var imprime = window.open(loc, 'imprimir', 'toolbar=0,status=1,menubar=0,scrollbars=1,resizable=1,location=0,directories=0,width='+ ancho+',height='+ alto);
    imprime.moveTo(0,0);
    imprime.focus();
}
// -----------------------------------------------------------------------------
// Para Abrir la Radio a la Carta
function subWincarta() {
    var options = "width=418,height=330,top=50,left=50,screenX=50,screenY=50,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no";
    var popwin = window.open('http://ts.cooperativa.cl/ts.html', 'popwin', options);
    if (typeof popwin === 'undefined') {
        alert("Debes habilitar las ventanas emergentes en tu navegador para acceder a esta funcionalidad.");
    }
}

// -----------------------------------------------------------------------------
// Para Abrir la Pop Up diarioi de cooperativa
function subWinDiarioPodCast(){
  var options = "width=410,height=760,top=10,left=50,screenX=50,screenY=50,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no";
    var popwin = window.open('/prontus_media/site/edic/base/port/pod_diario.html', 'popwin', options);
    if (typeof popwin === 'undefined') {
        alert("Debes habilitar las ventanas emergentes en tu navegador para acceder a esta funcionalidad.");
    }
}

// -----------------------------------------------------------------------------
// Para Abrir la Radio en Vivo
function subWinNotiMail() {
    var options = "width=480,height=500,top=50,left=50,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no";
    var popwin = window.open('http://mailcenter.cooperativa.cl/cgi-bin/mc_webside_ficha.cgi?CLTE_ID=24', 'notimail', options);
    if (typeof popwin === 'undefined') {
        alert("Debes habilitar las ventanas emergentes en tu navegador para acceder a esta funcionalidad.");
    }
}

// -----------------------------------------------------------------------------
// Para Abrir la Radio en Vivo
function openWindowGeneral(thisurl, thisname, thisparams) {
    var popwin = window.open(thisurl, thisname, thisparams);
    if (typeof popwin === 'undefined') {
        alert("Debes habilitar las ventanas emergentes en tu navegador para acceder a esta funcionalidad.");
    }
}

// -----------------------------------------------------------------------------
// Abre pop desde Flash.
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
// -----------------------------------------------------------------------------
//
function addLoadEvent(func, params) {
    var oldonload = window.onload;
    window.onload = function() {
        if (oldonload) {
            oldonload();
        }
        func(params);
    };
}

// -----------------------------------------------------------------------------
// Completa email de autor.
function autorE(nombre) {
	if(nombre != ''){
	    nombre = nombre+'@cooperativa.cl';
	    location.href = "mailto:"+nombre;
	}
	return nombre;
}
// -----------------------------------------------------------------------------
// Destilda un str.
function destilda(texto) {
  var txt = texto;
  txt = txt.replace(/Á/g,'a');
  txt = txt.replace(/É/g,'e');
  txt = txt.replace(/Í/g,'i');
  txt = txt.replace(/Ó/g,'o');
  txt = txt.replace(/Ú/g,'u');
  txt = txt.replace(/á/g,'a');
  txt = txt.replace(/é/g,'e');
  txt = txt.replace(/í/g,'i');
  txt = txt.replace(/ó/g,'o');
  txt = txt.replace(/ú/g,'u');
  txt = txt.replace(/Ñ/g,'n');
  txt = txt.replace(/ñ/g,'n');
  txt = txt.replace(/Ü/g,'u');
  txt = txt.replace(/ü/g,'u');
  txt = txt.replace(/á/g,'a');
  txt = txt.toLowerCase();
  txt = txt.replace(/[^0-9a-z\_\-]/g,'');
  return txt;
} // destilda
// -----------------------------------------------------------------------------
// Obtiene un parametro del URL.
function getParam(nom) {
  var valor = '';
  var request = window.location.href;
  if ((nom !== null) && (typeof nom !== 'undefined') && (nom != '')) {
    var re = new RegExp(nom + '=([^&]*)');
    var found = request.match(re);
    if((found!==null) && (found.length >= 1)) {
      valor = found[1];
    }
  }
  return valor;
} // getParam

// -----------------------------------------------------------------------------
// Agregar a favoritos
function agregarFavoritos() {
    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion, 10)>3)) {
        window.external.AddFavorite("http://www.cooperativa.cl","Cooperativa.cl");
    } else {
        alert("Su navegador no dispone de esta opción");
    }
}

// -----------------------------------------------------------------------------
// Hacer página de Inicio
function hacerPaginaDeInicio(obj) {
    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion, 10)>3)) {
        obj.style.behavior="url(#default#homepage)";
        obj.setHomePage("http://www.cooperativa.cl");
    } else{
        alert("su navegador no dispone de esta opción");
    }
}
