 function parentForm(control){
  padre=control;
  while(padre.nodeName!="FORM"){
   padre=padre.parentNode;
  } 
  return padre;
 }

function validText(filtro,control){
  filtros=new Array();
  cont=-1;
  while(filtro.indexOf("|")!=-1)
  {
   cont++;
   filtros[cont]=filtro.substring(0,filtro.indexOf("|"));
   filtro=filtro.substring(filtro.indexOf("|")+1,filtro.length);
  }
  filtros[cont+1]=filtro;
  for(f=0;f<filtros.length;f++){
   switch(filtros[f]){
    case "nodata":
     if(control.value==""){
      control.parentNode.getElementsByTagName("B")[0].innerHTML="&nbsp;&nbsp;El campo debe tener un valor";
      tabControl++;
      return false;
     }
    break;
    case "numerico":
     var filter=new RegExp("^(?:\\+|-)?\\d+\(.\\d*$)?");
     if(!filter.test(control.value))
     {
      control.parentNode.getElementsByTagName("B")[0].innerHTML="&nbsp;&nbsp;El campo debe ser numerico";
      tabControl++;
      return false;
     }
    break;
    case "entero":
     var filter=new RegExp("^(?:\\+|-)?\\d+$");
     if(!filter.test(control.value))
     {
      control.parentNode.getElementsByTagName("B")[0].innerHTML="&nbsp;&nbsp;El campo debe ser entero";
      tabControl++;
      return false;
     }
    break;
    case "email":
     var filter=/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
     if(!filter.test(control.value))
     {
      control.parentNode.getElementsByTagName("B")[0].innerHTML="&nbsp;&nbsp;Email invalido";
      tabControl++;
      return false;
     }
    break;
    case "cadena":
    break;
   }
  }
  control.parentNode.getElementsByTagName("B")[0].innerHTML="";
  return true;
 }

function refreshForm(div)
{
 inputs=div.getElementsByTagName("input");
 sels=div.getElementsByTagName("select");
 textareas=div.getElementsByTagName("textarea");
 params="";
 smb="";
 for(j=0;j<inputs.length;j++)
 {
  if(inputs[j].getAttribute("type")=="text" || inputs[j].getAttribute("type")=="password" || inputs[j].getAttribute("type")=="hidden" || inputs[j].getAttribute("type")=="checkbox" || inputs[j].getAttribute("type")=="radio")
  {
   tmp=inputs[j].getAttribute("name")+"="+inputs[j].value;
   if(inputs[j].getAttribute("type")=="checkbox" || inputs[j].getAttribute("type")=="radio")
   {
    if(inputs[j].checked==false)
    {
     tmp="";
     smb="";
    }
   }
   params=params+smb+tmp;
   smb="&";
  }
 }
 for(t=0;t<textareas.length;t++)
 {
  params=params+smb+textareas[t].getAttribute("name")+"="+textareas[t].value;
  smb="&";
 }
 for(s=0;s<sels.length;s++)
 {
  params=params+smb+sels[s].getAttribute("name")+"="+sels[s].options[sels[s].selectedIndex].value;
  smb="&";
 }
 return params;
}

function definelink(section){
 links=document.getElementById(section).getElementsByTagName("A");
 for(l=0;l<links.length;l++){
  if(!links.onclick && links[l].getAttribute("url")){
   links[l].onclick=function(){
     html = $.ajax({
     type: "GET",
     url: this.getAttribute("url"),
     data: this.getAttribute("param"),
     async: false
    }).responseText;
    if(this.getAttribute("target"))
    {
     document.getElementById(this.getAttribute("target")).innerHTML=html;
     definelink(this.getAttribute("target"));
    }
    else
    {
     document.getElementById(section).innerHTML=html;
     definelink(section);
    }
   };
  }
 }
 
 $(".valid2").submit(function() {
    id = $(this).attr("id");
    fr=document.getElementById(id);
    inputs=fr.getElementsByTagName("INPUT");
    tabControl=-1;
    for(i=0;i<inputs.length;i++){
     if(inputs[i].getAttribute("type")=="text" || inputs[i].getAttribute("type")=="password"){
      if(inputs[i].getAttribute("filter")){
       bold=document.createElement("B");
       bold.setAttribute("style","color:red;font-size:11px;");
       div=document.createElement("div");
       div.setAttribute("align","left");
       div.setAttribute("style","width:auto;height:auto;float:bottom;position:relative;left:0px;");
       div.appendChild(bold);
       inputs[i].parentNode.appendChild(div);
       validos=validText(inputs[i].getAttribute("filter"),inputs[i]);
       if(validos==false)
       {
        if(tabControl==0){
         inputs[i].focus();
        }  // fin si

       } // Fin si

      } //Fin si

     } //Fin si

    } //Fin for    

    if(validos==false) {
     return false;
    }
    return true;
  });
 
 fr=document.getElementById(section).getElementsByTagName("FORM");
 for(f=0;f<fr.length;f++){
  btns=fr[f].getElementsByTagName("input");
  for(b=0;b<btns.length;b++){
   if((btns[b].getAttribute("type")=="button" || btns[b].getAttribute("type")=="image") && btns[b].getAttribute("cmd")=="sbt"){
    btns[b].onclick=function(){
       if(this.parentNode.nodeName=="A" && $.browser.msie)
        this.parentNode.click();
       father=parentForm(this);
       inputs=father.getElementsByTagName("INPUT");
       tabControl=-1;
       validos=true;
       for(i=0;i<inputs.length;i++){
        if(inputs[i].getAttribute("type")=="text" || inputs[i].getAttribute("type")=="password"){
         if(inputs[i].getAttribute("filter")){
          bold=document.createElement("B");
          bold.setAttribute("style","color:red;font-size:11px;");
          div=document.createElement("div");
          div.setAttribute("align","left");
          div.setAttribute("style","width:auto;height:auto;float:bottom;position:relative;left:0px;");
          div.appendChild(bold);
          inputs[i].parentNode.appendChild(div);
          validos=validText(inputs[i].getAttribute("filter"),inputs[i]);
          if(validos==false)
          {
           if(tabControl==0){
            inputs[i].focus();
           }  // fin si

          } // Fin si

         } //Fin si

        } //Fin si

       } //Fin for
     if(validos==true){
      params=refreshForm(parentForm(this));
      html = $.ajax({
      type: "POST",
      url: parentForm(this).getAttribute("action"),
      data: params,
      async: false
      }).responseText;
      document.getElementById("derecha").innerHTML=html;
      definelink("derecha");
     }
    }
   }
  }
 }

}

window.onload=function(){
 var fr=null;
 var izquierda=null;
 if(boletin=="si")
 {
  html = $.ajax({
   type: "POST",
   url: "/boletin.php",
   data: "parametro=parametro",
   async: false
  }).responseText;
  document.getElementById("derecha").innerHTML=html;
 }
 if(document.getElementById("menufile"))
 {
  html = $.ajax({
  type: "POST",
  url: "/options.php",
  data: "parametro=parametro",
  async: false
  }).responseText;
  document.getElementById("menufile").innerHTML=html;
  definelink("menufile"); 
 }
 as=document.getElementsByTagName("A");
 for(a=0;a<as.length;a++){
  if(as[a].getAttribute("url")){
   as[a].onclick=function(){
     html = $.ajax({
      type: "GET",
      url: this.getAttribute("url"),
      data: this.getAttribute("param"),
      async: false
     }).responseText;
     document.getElementById("derecha").innerHTML=html;
     definelink("derecha");
   }
  }
 }
 if(document.getElementById("menufile"))
 {
  izquierda=document.getElementById("menufile");
  fr=izquierda.getElementsByTagName("FORM");
  for(f=0;f<fr.length;f++){
   btns=fr[f].getElementsByTagName("input");
   for(b=0;b<btns.length;b++){
    if(btns[b].getAttribute("type")=="button" && btns[b].getAttribute("cmd")=="sbt"){
     btns[b].onclick=function(){
        params=refreshForm(parentForm(this));
        html = $.ajax({
         type: "POST",
         url: parentForm(this).getAttribute("action"),
         data: params,
         async: false
        }).responseText;
        document.getElementById("derecha").innerHTML=html;
        definelink("derecha");
     };//evento 
    }//if
   }//for
  }//for
  html = $.ajax({
   type: "POST",
   url: "/calendarview.php",
   data: "parametro=parametro",
   async: false
  }).responseText;
  document.getElementById("calendario").innerHTML=html;
  definelink("calendario");
  }
  else
  {
   if(document.getElementById("calendario").getAttribute("param")){
    param=document.getElementById("calendario").getAttribute("param");
   }
   else
   {
    param="parametro=parametro";
   }
   html = $.ajax({
    type: "POST",
    url: "/vistaevento.php",
    data: ""+param,
    async: false
   }).responseText;
   document.getElementById("calendario").innerHTML=html;
   definelink("calendario");
  }
  if(document.getElementById("boletin")){
   definelink("boletin");
  }
  //menu
 var div=null;
 var menu=null;
 var link=null;
 var item=null;
 var principales=document.getElementsByName("menue");
 for(m=0;m<principales.length;m++)
 {
  principales[m].onmouseover=function(){
  if(!document.getElementById("m"+this.getAttribute("id")))
  {
   id=this.getAttribute("id");
   var xml = $.ajax({
     type: "POST",
     url: "/submenu.php",
     dataType: "xml",
     data: "idmenu="+id,
     async: false
   }).responseXML;
   items=xml.getElementsByTagName("ITEM");
   div=document.createElement("DIV");
   div.setAttribute("style","z-index:3;clip: rect( );visibility:hidden;padding:2px;");
   div.style.backgroundColor="#074a8b";
   div.style.padding="5px";
   div.style.position="absolute";
   div.style.border="solid 1px white";
   menu=document.createElement("UL");
   menu.setAttribute("style","list-style-type:none;width:170px;font-size:12px;text-align:left;");
   div.setAttribute("id","m"+this.getAttribute("id"));
   for(i=0;i<items.length;i++)
   {
    var item=document.createElement("LI");
    //item.style.paddingLeft="10px";
    //item.style.paddingTop="5px";
    //item.style.paddingBottom="5px";
    item.setAttribute("style","padding-left:10px;padding-top:5px;padding-bottom:5px;font-weight:none;font-size:11px;");
    if(i!=items.length-1)
    {
     item.style.borderBottomStyle="dotted";
     item.style.borderBottomColor="white";
     item.style.borderBottomWidth="1px";
    }
    link=document.createElement("A");
    link.style.color="white";
    //link.style.fontWeight="none";
    //link.style.fontSize="11px";
    link.onmouseover=function(){
     this.style.color="#f7e017";
     this.parentNode.style.backgroundColor="#0a6ec3";
    };
    link.onmouseout=function(){
     this.style.color="white";
     this.parentNode.style.backgroundColor="#074a8b";
    };
    if(items[i].getAttribute("url") && items[i].getAttribute("url")!="")
    {
     link.setAttribute("href",items[i].getAttribute("url"));
    }
    else
    {
     link.setAttribute("href","#");
    }
    nodo=document.createTextNode(items[i].firstChild.data);
    link.setAttribute("style","color:white;font-weight:none;");
    link.appendChild(nodo);
    item.appendChild(link);
    menu.appendChild(item);
   }
   div.appendChild(menu);
   this.parentNode.appendChild(div);
   div.style.left=$("#"+this.getAttribute("id")).position().left+"px";
   div.style.top=(($("#"+this.getAttribute("id")).position().top+$("#"+this.parentNode.getAttribute("id")).height())-2)+"px";
   div.onmouseover=function(){
    this.style.visibility="visible";
   };
  div.onmouseout=function(){
    this.style.visibility="hidden";
   };
  }
  document.getElementById("m"+this.getAttribute("id")).style.visibility="visible";
  };
  principales[m].onmouseout=function(){
   if(document.getElementById("m"+this.getAttribute("id"))){
    //this.removeChild(document.getElementById("m"+id));
    document.getElementById("m"+this.getAttribute("id")).style.visibility="hidden";
   }
  };
 }
};
