function confirmLink(theLink, theText){
    if (theText == '' || typeof(window.opera) != 'undefined') {return true;}
    var is_confirmed = confirm(theText);
    if (is_confirmed) {
        theLink.href += '&confirmado=1';
    }
    return is_confirmed;
}
//------------------------------------------------------------------------
function doPreview(wich, where){
	var objFile = document.getElementById(wich); //imagen
	var objImg = document.getElementById(where); //preview

	objImg.src = objFile.value;
}
//------------------------------------------------------------------------
function openWin(url, ancho, alto) {
	var day = new Date();
	var id  = day.getTime();
	var w   = screen.width;
  var h   = screen.height;
	var leftPos = (w-ancho)/2;
	var topPos = (h-alto)/2;

	eval("page" + id +
		 " = window.open(url, '" + id + "', '" +
		 "toolbar=0,"   +
		 "scrollbars=1,"+
		 "statusbar=0," +
		 "menubar=0,"   +
		 "resizable=0," +
		 "width="+ ancho + "," +
		 "height="+ alto + "," +
		 "top="+ topPos + ","  +
		 "left="+ leftPos +"');");
}
//------------------------------------------------------------------------
function highlight_div(checkbox_node){
    label_node = checkbox_node.parentNode;

    if (checkbox_node.checked){
		label_node.style.backgroundColor='#5B646C';
		label_node.style.color='#ffffff';
	} else {
		label_node.style.backgroundColor='#ffffff';
		label_node.style.color='#000000';
	}
}
//------------------------------------------------------------------------
function checkAll(caja){
	var qEstado = false;
	var cantidad= caja.length;
	if (document.getElementById('check_todos').checked){
		qEstado = true;
	}
	for (i=0; i<cantidad; i++){caja[i].checked = qEstado;}
}
//------------------------------------------------------------------------
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
	}}}
//------------------------------------------------------------------------
function emailCheck(emailStr){
	var emailPat = /^(.+)@(.+)$/
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars = "\[^\\s" + specialChars + "\]"
	var quotedUser = "(\"[^\"]*\")"
	var ipDomainPat= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom = validChars + '+'
	var word = "(" + atom + "|" + quotedUser + ")"
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray = emailStr.match(emailPat)

	if (matchArray == null){
		return false;
	}
	var user = matchArray[1]
	var domain = matchArray[2]
	if (user.match(userPat) == null){
	    return false;
	}
	var IPArray = domain.match(ipDomainPat)
	if (IPArray!=null){
		for (var i=1; i<=4; i++){
			if (IPArray[i] > 255){return false;}
		}
    return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null){
		return false;
	}
	var atomPat = new RegExp(atom,"g")
	var domArr = domain.match(atomPat)
	var len = domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3){
	  return false;
	}
	if (len < 2){
	   return false;
	}
	return true;
}
//------------------------------------------------------------------------
function validacionRegistrarTutoria(form){
	var errores ="";

  if( document.getElementById('tipo_accion').value != "Modificar" ) {
	  // CATEGORIA
	  if( document.getElementById('principal').value == -1 ) {
	    errores+="Seleccione una Categoria Principal.";
	    errores+="\n";
	  }
  }

  // TITULO
  if( vacio(form.titulo.value) == false ) {

    errores+="Introduzca el Titulo.";
    errores+="\n";
    /*
    alert("Introduzca el  Titulo.");
    return false;
    */
  }
  // DETALLE
  if( vacio(form.detalle.value) == false ) {
    errores+="Introduzca el Detalle.";
    errores+="\n";
    /*
    alert("Introduzca el Titulo.");
    return false;
    */
  }
  if (document.getElementById('precio').value <=4 && document.getElementById('contenido').value != "Video"){
    errores+='El "precio" de la Tutoria no debe ser inferior a 5 Euros.';
    errores+="\n";
  }
  // VIDEO* <==> si cargamos por carga_video.php , no se aplica al modificar
  if( document.getElementById('contenido').value == "Video" && document.getElementById('tipo_accion').value == "Nuevo" && document.getElementById('mivideo').value == "" ) {
    errores+="Debe subir un Video de tipo FLV.";
    errores+="\n";
  }
  // VIDEO** (tanto para tutoria como video)
  var resultado = document.getElementById('mivideo').value.split(".");
  var indice=0;
  if(resultado.length>1){
    var indice = resultado.length - 1 ;
  } else {
		resultado[indice]='';
	}
  if( document.getElementById('mivideo').value != "" && resultado[indice]!='flv' ) {
    errores+="El Video debe ser de tipo FLV.";
    errores+="\n";
  }


  if ( document.getElementById('mivideo').value != "") {
		/*
		img = document.createElement("IMG");
		img.src = document.getElementById("mivideo").value;
		img.id = "archivoFinal";
		img.style.visibility = "hidden";
		document.body.appendChild(img);
		setTimeout("alert(Math.round((document.getElementById('archivoFinal').fileSize / 1024)*Math.pow(10,2)) / Math.pow(10,2)); ",500);


		path =document.getElementById('mivideo').value;
	  var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
	  file.initWithPath(path);
		alert(file.fileSize);
		*/
	}


  // DURACION NUMERICO POSITIVO
  if( document.getElementById('contenido').value == "Tutoria" ) {
    try {
      var miValor = parseFloat(document.getElementById('duracion').value);
      if ( isNaN(miValor) || miValor <= 0 || miValor > 1000 || miValor+"" == "undefined" || miValor+"" == "null" || miValor+"" == "") {
        miValor = 0;
        errores+="Introduzca una Duracion Aproximada en horas.";
        errores+="\n";
      } else {
        miValor = redondear(miValor,2);
      }
    } catch(e) {
      miValor = 0;
      errores+="Introduzca una Duracion Aproximada en horas.";
      errores+="\n";
    }
    document.getElementById('duracion').value = miValor;
  }





  // PRECIO NUMERICO NONEGATIVO
  try {
    var miValor = parseFloat(document.getElementById('precio').value);
    if ( isNaN(miValor) || miValor < 0 || miValor > 1000 || miValor+"" == "undefined" || miValor+"" == "null" || miValor+"" == "") {
      miValor = 0;
      errores+="Introduzca un Precio correcto.";
      errores+="\n";
    } else {
      miValor = redondear(miValor,2);
    }
  } catch(e) {
    miValor = 0;
    errores+="Introduzca un Precio correcto.";
    errores+="\n";
  }

  document.getElementById('precio').value = miValor;

  // ARCHIVOS


	if (errores==""){
		return true;
	} else {
		alert(errores);
		return false;
	}


}
//------------------------------------------------------------------------
function msgSMS() {
  // VIDEO* <==> si cargamos por carga_video.php , no se aplica al modificar
  if( document.getElementById('contenido').value == "Video" && document.getElementById('tipo_accion').value == "Nuevo" ) {
    alert("Recuerde, que un precio neto de IVA\nigual a 0.90 le permite cobrar via SMS");
  }
}
//------------------------------------------------------------------------
function redondear(cantidad, decimales) {
  var cantidad = parseFloat(cantidad);
  var decimales = parseFloat(decimales);
  decimales = (!decimales ? 2 : decimales);
  return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
}
//------------------------------------------------------------------------
function vacio(q) {
        for ( i = 0; i < q.length; i++ ) {
                if ( q.charAt(i) != " " ) {
                        return true
                }
        }
        return false
}
//------------------------------------------------------------------------
function getFileSize(path) {
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  var unArchivo = Components.classes["@mozilla.org/file/local;1"].createInstance();
	var nsLocalFile = Components.Constructor("@mozilla.org/file/local;1","nsILocalFile", "initWithPath");
	var file = new nsLocalFile(path);
  alert(String(file.fileSize));
}