var sErrIsEmpty  = " es requerido.\n";
var sErrNoValid  = " tiene caracteres invalidos .\n";
var sErrValidateText2  = " debe contener solamente numeros y letras .\n";
var sErrNoValidDate    = " tiene un formato de fecha invalido .\n" ;
var sErrNoValidTime	   = " tiene un formato de hora invalido .\n" ;
var sErrEmpty = " no debe contener valor .\n";
var sErrNoNumber = " debe ser numerico .\n"
var sErrNoValidMail = " es una direccion de correo electronico invalida .\n"



function validateText(sText, sName, bAllowEmpty)
{
   var sErrorMsg = "";
   
   
       if(bAllowEmpty && sText == "") // If empty
       {
       sErrorMsg = "- " + sName + sErrIsEmpty;
       }
       else {
	     var n = sText.length;
	     for (var i = 0; i < n; i++){
                var c = sText.charAt(i);
                if (c == ' ' || c == '\t' || c == '\n'){
                    sErrorMsg = "- " + sName + sErrNoValid ;
                } else
                    break;
            }
	   
	   
	   
	   }
  
   return sErrorMsg;
}

function validateEmpty(sText, sName)
{
   var sErrorMsg = "";
   
   
       if(sText != "") // If empty
       {
          sErrorMsg = "- " + sName + sErrEmpty;
	   }
  
   return sErrorMsg;
}


function validateText2(sText, sName, bAllowEmpty)
{
   var sErrorMsg = "";

   if(bAllowEmpty && sText == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sText != "") // else if to short or to long string or nonvalid characters
   {
      sErrorMsg = validateText(sText, sName, bAllowEmpty);
      
      if(sErrorMsg == "")
      {
		   for(var i=0; i<sText.length; i++)
		   {
		      if(!(sText.charAt(i) <= "9" && sText.charAt(i) >= "0" || 
		           sText.charAt(i) <= "z" && sText.charAt(i) >= "a" || 
		           sText.charAt(i) <= "Z" && sText.charAt(i) >= "A"))
		      {
		         sErrorMsg += "- " + sName + sErrValidateText2;
		         break;
		      }
		   }
      }
   }
   
   return sErrorMsg;
}

function validateDate(sText,sName,bAllowEmpty){
        
       var date = sText.split("-");
       var nyear  = parseFloat(date[0]);
       var nmonth = parseFloat(date[1]);
       var nday   = parseFloat(date[2]);
	   
	   var sErrorMsg = "";
	   
	   //alert(nday+","+nmonth+","+nyear);
	   
	   if (!isNaN(nmonth) && !isNaN(nyear) && !isNaN(nday)) {
            
          // alert(nday+","+nmonth+","+nyear);
            //el mes debe estar entre 1 y 12
            if (nmonth < 1 || nmonth > 12) {
                sErrorMsg += "- " + sName + sErrNoValidDate;
             
            }
            //comprueba que el aņo este entre 1900 y 2099
            if (nyear < 1900 || nyear > 2099) {
                sErrorMsg += "- " + sName + sErrNoValidDate;
            }
            //comprueba el numero de dias dependiendo del mes
            if (nmonth == 1 || nmonth == 3 || nmonth == 5 || nmonth == 7 || nmonth == 8 || nmonth == 10 || nmonth == 12) {
                if (nday <=0 || nday > 31) {
                   sErrorMsg += "- " + sName + sErrNoValidDate;
                }
            }
            if (nmonth == 4 || nmonth == 6 || nmonth == 9 || nmonth == 11) {
                if (nday <=0 || nday > 30) {
                    sErrorMsg += "- " + sName + sErrNoValidDate;
                }
            }
            if (nmonth == 2) {
                if (nyear % 4 > 0) {
                    if (nday > 28) {
                        sErrorMsg += "- " + sName + sErrNoValidDate;
                    }
                }else {
                    if (nday > 29) {
                        sErrorMsg += "- " + sName + sErrNoValidDate;
                    }
                }
            }
        }
        
     else {
     if (sText.length == 0) {
         if (bAllowEmpty)
            sErrorMsg += "- " + sName + sErrIsEmpty;
     }
	 else {
		sErrorMsg += "- " + sName + sErrNoValidDate; 
	 }
	}
	return sErrorMsg;
}


 function validateTime(sText,sName,bAllowEmpty){
        
     var hora = sText.split(":");
    var h = parseFloat(hora[0]);
    var m = parseFloat(hora[1]);
	 
    var sErrorMsg = "";
	 
    if (!isNaN(h) && !isNaN(m)) {        
        if (h < 0  || h > 23){
            sErrorMsg += "- " + sName + sErrNoValidTime
        }
        if (m < 0  || m > 60){
            sErrorMsg += "- " + sName + sErrNoValidTime
        }
    }
    else {

        if (sText.length == 1) {
            if (bAllowEmpty)
                sErrorMsg += "- " + sName + sErrIsEmpty;
        }
        else {
            sErrorMsg += "- " + sName + sErrNoValidTime;
        }
    }

    return sErrorMsg;
	
 }
 


function All(ind){
  var lflag
    if (ind == "H")
      lflag = true;
    else
     lflag = false;
                
  for(var i = 0;i < document.form2.checkbox.length;i++) {
    document.form2.checkbox[i].checked = lflag;         
  }  
                         
}

function validateNumber(sText,sName,bAllowEmpty){
             
         var sErrorMsg = "";   
         var number = new Number(sText);
		 
         if (sText.length==0){
             if (bAllowEmpty){
               sErrorMsg += "- " + sName +  sErrIsEmpty;
             }
         } else if (isNaN(number)){ 
		   sErrorMsg += "- " + sName + sErrNoNumber;
         }
		 
		return sErrorMsg; 
   
 }
 function validateInt(sText,sName,bAllowEmpty){
             
         var sErrorMsg = "";   
         var number = parseInt(sText);
		 
         if (sText.length==0){
             if (bAllowEmpty){
               sErrorMsg += "- " + sName +  sErrIsEmpty;
             }
         } else if (isNaN(number)){ 
		   sErrorMsg += "- " + sName + sErrNoNumber;
         }
		 
		return sErrorMsg; 
   
 }
 
 function validateEmail(sText,sName,bAllowEmpty) {
	
  var sErrorMsg = "";    	
  //var s =  "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/";	
  
  if (sText.length == 0){
	  if (bAllowEmpty)
	     sErrorMsg += "- " + sName +  sErrIsEmpty; 

   } else {
      if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(sText)))
	 sErrorMsg += "- " + sName + sErrNoValidMail;
   }		
		
   return sErrorMsg;		
  
} 

 


function ButtonPressed(button){
	document.form1.buttonpressed.value = button;
}


function Click(action){

	document.form1.action.value = action;
}

function confirmar(){

  if (confirm("Esta seguro de eliminar este registro ..")){
     document.form1.button.value = 'Del';
	 document.form1.submit();
  }
      
 }	
 
 function eliminar(){
	 
	 flag = false;
	
	 if (confirm("Esta seguro de eliminar este registro ..")){
		execute("Eliminar");
		flag = true;
	 }
         
    return flag;
	
 }


// JavaScript Document

/*
function  : open_windows
author    : Ivan Gallo Peņa
date      : 24/04/2004

abre una nueva ventana en la pantalla alineada al centro, izquierda y derecha
las propiedades que recibe son las siguientes

url -> pagina que va a mostrar en la ventana
name -> nombre de la ventana
toolbar   -> (yes/no) muestra la barra de herramienta
statusbar -> (yes/no) muestra la barra de estado
width     -> ancho de la ventana
height    -> alto de la ventana
aligment  -> (center,rigth,left) -> alineacion de la ventana


*/

function help_instituciones(){
  open_window('help_instituciones.jsp','help','no','yes',525,350,'right','yes');
}
function help_codigocad(){
  open_window('help_codigoscad.jsp','help','no','yes',525,350,'right','yes');
}

function help_actividad(actual){
 open_window('help_actividades.jsp?actual='+actual,'help','no','yes',525,350,'right','yes');
}

function help_actividad_biblioteca(actual){
 open_window('help_actividades_biblioteca.jsp?actual='+actual,'help','no','yes',765,450,'center','yes');
}

function help_actividad_presupuesto(actual){
 open_window('help_actividades_presupuesto.jsp?actual='+actual,'help','no','yes',765,450,'center','yes');
}


function help_codigosector(){
  open_window('help_sectores.jsp','help','no','yes',525,350,'right','yes');
}

function open_window(url,name,toolbar,statusbar,width,height,aligment,scroll) {
	
  var win ;
  var posx ;
  var posy ;
  var window_features;

  if (aligment == 'center') {
	  posx = (screen.availWidth - width) / 2;
	  posy = (screen.availHeight- height) / 2;
  }
  else {
      if  (aligment == 'left') {
  		posx = 0;
        posy = 0;
	  }
	  else {
		posx = (screen.width - width-10) ;
	    posy = 0;
	  }

  }

  window_features = 'status='+statusbar+',toolbar='+toolbar+',top='+posy+',left='+posx+',width='+width+',height='+height+',scrollbars='+scroll;

  win = window.open(url,name,window_features);

  win.focus();

}

function help(paramURL,width,height){
  open_window(paramURL,'help','no','yes',width,height,'right','yes');
}


function submitform(boton){
  
  	document.form1.button.value = boton;
	
	if(document.form1.button.value == 'Eliminar'){
		
	  if (confirm("esta seguro de eliminar este registro ..")){
	     document.form1.submit();   
      }
	}
	else {
	  document.form1.submit();    	
	}
}

function execute(botonPulsado){
 
  var action = document.getElementById("button");
  action.value = botonPulsado;

}

function useLoadingMessage(message) {
  var loadingMessage;
  if (message) loadingMessage = message;
  else loadingMessage = "Loading";

  dwr.engine.setPreHook(function() {
    var disabledZone = $('disabledZone');
    if (!disabledZone) {
      disabledZone = document.createElement('div');
      disabledZone.setAttribute('id', 'disabledZone');
      disabledZone.style.position = "absolute";
      disabledZone.style.zIndex = "1000";
      disabledZone.style.left = "0px";
      disabledZone.style.top = "0px";
      disabledZone.style.width = "100%";
      disabledZone.style.height = "100%";
      document.body.appendChild(disabledZone);
      var messageZone = document.createElement('div');
      messageZone.setAttribute('id', 'messageZone');
      messageZone.style.position = "absolute";
      messageZone.style.top = "0px";
      messageZone.style.right = "0px";
      messageZone.style.background = "#FFFF9F";
      messageZone.style.color = "#000000";
      messageZone.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
	  messageZone.style.fontSize  = "12px";
	  messageZone.style.fontWeight = "bold";
	  messageZone.style.padding = "4px";
      disabledZone.appendChild(messageZone);
      var text = document.createTextNode(loadingMessage);
      messageZone.appendChild(text);
    }
    else {
      $('messageZone').innerHTML = loadingMessage;
      disabledZone.style.visibility = 'visible';
    }
  });

  dwr.engine.setPostHook(function() {
    $('disabledZone').style.visibility = 'hidden';
  });
}




