var ventana;
var fnumeros="0123456789";
var Sep="/";
var ferror= new creaferror();  
var ferrores= new Array();

ferrores[1]="Campo vacio no contiene ningún valor";
ferrores[2]="Carácter ilegal en una fecha";
ferrores[3]="Faltan separadores en una fecha";
ferrores[4]="Año incorrecto en una fecha";
ferrores[5]="Mes incorrecto en una fecha";
ferrores[6]="Día incorrecto en una fecha";

// Crea un objeto que guarda un indice a la posición y al mensaje de ferror
function creaferror()
{
	this.valor=0;
	this.posicion=0;
	return this
}
function muestraFotoDialog(img)
{
    ventana = showModelessDialog("Foto.aspx?img=" + img,window,"status:no; location:yes; scrollbars:no; dialogWidth:300px; dialogHeight: 300px;");
    return false;
}

function redimensionar(ancho,alto)
{
ventana.resizeTo(ancho+12, alto+28);
ventana.moveTo((screen.width-ancho)/2,(screen.height-alto)/2);
}

function abrirVentana(url,width,height) 
{    
    var condiciones = "width=" + width + ",height=" + height + ",toolbar=no,directories=no,menubar=no,status=no";
    window.open(url,"", condiciones);
    return false;
}

function abrirVentanaToolBar(url,width,height) 
{   
    var condiciones = "width=" + width + ",height=" + height + ",scrollbars=yes,toolbar=no,directories=no,menubar=no,status=no";
    window.open(url,"", condiciones);
    return false;
}

function WindowOpen(url)
{
    //url
    //window.showModalDialog("http://localhost/Flick-ACM/ACM_upload/202XEB5782006.doc", "modal", caracteristicas);
    window.open(url);
    return false;
}
function centrar() 
{    
    eje_x=(screen.width-document.body.clientWidth) / 2;    
    eje_y=(screen.height-document.body.clientHeight) / 2;    
    moveTo(eje_x,eje_y);
}  

function isWhitespace (s)
{   
    var i;

    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

// Determina si un caracter es un número
function fnumero(car)
{
return (fnumeros.indexOf(car)>=0)
}

// Comprueba si una fecha es correcta
function isDate(contenido,ferror)
{
    if ((contenido.length==0))
    {
      ferror.valor=1;
       ferror.posicion=1; 
       return false;
    } 
    var nsep=0;

    // Comprobación de la sintáxis de una fecha 
    for (var i=0; i<contenido.length; ++i)
    {
	    var car=contenido.charAt(i);
	    if (!fnumero(car)&&car!=Sep)
	    {	
		    ferror.valor=2;
		    ferror.posicion=i+1; 
		    return false;
	    } 
	    if (car==Sep)
	      nsep++ 
    }
    
    if (nsep!=2)
    {
	    ferror.valor=3;
	    ferror.posicion=i+1; 
	    return false;
    }


	// Comprobación de la semántica de una fecha
	var pos1=contenido.indexOf(Sep);
	var dia=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(Sep,pos1+1);
	var mes=contenido.substring(pos1+1,pos2); 
	var anio=contenido.substring(pos2+1,10);

	if (anio<0||anio>9999)
	{
		ferror.valor=4;
		ferror.posicion=6; 
		return false;
	} 
	if (mes<1||mes>12)
  {
		ferror.valor=5;
		ferror.posicion=4;
		return false;
	}
	if ((dia<1 || dia>31)||(mes==4&&dia>30)||(mes==6&&dia>30)||(mes==9&&dia>30)||(mes==11&&dia>30)||(mes==2&&esBisiesto(anio)&&dia>29) || (mes==2&&!esBisiesto(anio)&&dia>28))
	{
		ferror.valor=6;
		ferror.posicion=2;
		return false;
	} 
	return true 
}

function FormatoFecha(campo,ferror)
{
    if (campo.value != "")
    {
        var correcto= isDate(campo.value,ferror);
	    if (!correcto)
	    {
		    alert("El campo fecha no es válido. Error: "+ferrores[ferror.valor]+ " en la posicion " +ferror.posicion+ ". Revise su contenido.");
		    campo.focus();
		    return false ;
	    }
	}
}

function Validar()
{
	if (!CampoVacio ("txtTitular","OB_Titular", vTitulo )) { return false ;}   
    if (!CampoVacio ("txtEntradilla","OB_Entradilla",vEntradilla)) { return false ;}    
	if (!CampoVacio ("txtFuente","OB_Fuente",vFuente)) { return false ;} 	
	if (!CampoComboVacio ("selClasificacion","OB_Clasificacion",vClasificacion)) { return false ;} 

	if (!CampoVacio ("txtObservaciones","OB_Observaciones", vObservaciones)) { return false ;} 
	if (!CampoVacio ("txtURL","OB_Url",vUrl )) { return false ;}  
	if (!CampoVacio ("txtCuerpo","OB_Cuerpo", vCuerpo)) { return false ;}
	
	if (!CampoFechaVacio ("dtFechaInicio","OB_FechaInicio",vFechaInicio)) { return false ;}
	if (!CampoFechaVacio ("dtFechaFin","OB_FechaFin", vFechaFin)) { return false ;}
	
 	if (!CampoImagenVacio ("txtUIDImagen1","OB_ImagenNivel1","txtImagenNivel1", vImagenNivel1) ) { return false ;} 
 	if (!CampoVacio ("txtPieNivel1","OB_PieNivel1", vPieNivel1) ) { return false ;} 

  	if (!CampoImagenVacio ("txtUIDImagen2","OB_ImagenNivel2","txtImagenNivel2", vImagenNivel2) ) { return false ;} 
	if (!CampoVacio ("txtPieNivel2","OB_PieNivel2",vPieNivel2) ) { return false ;} 
	
   if (!CampoImagenVacio ("txtUIDImagen3","OB_ImagenNivel3","txtImagenNivel3", vImagenNivel3) ) { return false ;} 
	if (!CampoVacio ("txtPieNivel3","OB_PieNivel3",vPieNivel3) ) { return false ;}  


	if (!CampoVacio ("txtKeyWords","OB_KeyWords",vKeyWords)) { return false ;} 	
 	
	if (!CampoFechaVacio ("dtFechaPublicacion","OB_FechaPublicacion", vFechaPublicacion)) { return false ;}	
	if (!CampoFechaVacio ("dtFechaBaja","OB_FechaBaja", vFechaBaja)) { return false ;}
	
	if (!CampoVacio ("txtTelefono","OB_Telefono", vTelefono )) { return false ;}
	if (!CampoVacio ("txtTextoLargo1","OB_TextoLargo1", vTextoLargo1 )) { return false ;}
	if (!CampoVacio ("txtTextoLargo2","OB_TextoLargo2", vTextoLargo2 )) { return false ;}
	if (!CampoVacio ("txtNifCif1","OB_NifCif1", vNifCif1 )) { return false ;}
	if (!CampoVacio ("txtNifCif2","OB_NifCif2", vNifCif2 )) { return false ;}
	if (!CampoVacio ("txtTexto1","OB_Texto1", vTexto1 )) { return false ;}
	if (!CampoVacio ("txtTexto2","OB_Texto2", vTexto2 )) { return false ;}
	if (!CampoVacio ("txtTexto3","OB_Texto3", vTexto3 )) { return false ;}
	if (!CampoVacio ("txtTexto4","OB_Texto4", vTexto4 )) { return false ;}
	if (!CampoVacio ("txtTexto5","OB_Texto5", vTexto5 )) { return false ;}
	if (!CampoVacio ("txtTexto6","OB_Texto6", vTexto6 )) { return false ;}
	if (!CampoVacio ("txtTexto7","OB_Texto7", vTexto7 )) { return false ;}
	if (!CampoVacio ("txtTexto8","OB_Texto8", vTexto8 )) { return false ;}
	if (!CampoVacio ("txtTexto9","OB_Texto9", vTexto9 )) { return false ;}
	if (!CampoVacio ("txtTexto10","OB_Texto10", vTexto10 )) { return false ;}
	if (!CampoVacio ("txtTexto11","OB_Texto11", vTexto11 )) { return false ;}
	if (!CampoVacio ("txtTexto12","OB_Texto12", vTexto12 )) { return false ;}

	return true;
}

function CampoVacio (Campo, CampoOBL, Literal )
{
    if (Campo == "dtFechaInicio")
    {
    return true
    }
	if (eval("document.all." + Campo))
	{
		if (eval("document.all." + CampoOBL  + ".value==1"))
		{
		    //if (isWhitespace(eval("document.all."+ Campo + ".value")))
		    if (document.getElementById(Campo).value == "")
		    {
                alert("El campo '" + Literal + "' es obligatorio. Revise su contenido.");
                eval("document.all." + Campo + ".focus();")
                return false ;
            } 
		}
		return true ;
	}	 
	return true ;
}

function CampoImagenVacio (CampoDatos, CampoOBL, CampoFoco,Literal )
{
	if (eval("document.all." + CampoDatos  )){	
		if (eval("document.all." + CampoOBL  + ".value==1")){
			if (isWhitespace(eval("document.all."+ CampoDatos + ".value"))){
				alert("El campo '" + Literal + "' es obligatorio. Revise su contenido.");
				eval("document.all." + CampoFoco + ".focus();")
				return false ;
			} 
		} 
		return true ;
	}	  
	return true ;
}

//----------------------------------------------------------------------------------------------------------------------------

function CampoFechaVacio (Campo, CampoOBL,Literal )
{
 	if (eval("document.all." + Campo ))
 	{
		if (eval("document.all." + CampoOBL  + ".value==1"))
		{
			var sField = "document.all." + Campo + ".value"
			var correcto= isDate(eval(sField),ferror);
			if (!correcto)
			{
				alert("El campo '" + Literal + "' no es válido. Error: "+ferrores[ferror.valor]+ " en la posicion " +ferror.posicion+ ". Revise su contenido.");
				eval("document.all." + Campo  + ".focus();")
				return false ;
			}
		}
	}
	return true ;
}				

//----------------------------------------------------------------------------------------------------------------------------
function CampoComboVacio (Campo, CampoOBL,Literal )
{	
	if (eval("document.all." + Campo ))
	{
		if (eval("document.all." + CampoOBL  + ".value==1")){
			x=0 ;
			for (var i = 1; i < eval("document.all." + Campo + ".options.length"); ++i) 
			{
				if (eval("document.all." + Campo + ".options[i].selected"))	{ x = x +1; }
			}
			if (x == 0 ){
				alert("Es obligatorio asociar al menos un '" + Literal + "' a este contenido.");
				eval("document.all." + Campo  + ".focus();")
				return false ;		
			}
		}
	}
	return true ;
}
//-------------------- E - MAIL --------------------------------------------------------------------------------------------------
    function isEmail (s)
    {
        if (isEmpty(s)) return false;
        var i = 1;
        var sLength = s.length;
        while ((i < sLength) && (s.charAt(i) != "@"))
        { i++
        }
        if ((i >= sLength) || (s.charAt(i) != "@")) return false;
        else i += 2;

        while ((i < sLength) && (s.charAt(i) != "."))
        { i++
        }

        if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
        else return true;
    }
    
    function isEmpty(s)
    {   return ((s == null) || (s.length == 0))
    }
