// Yedra 3.0 Javascript Support

// Include internacionalization resources
document.write("<script src='resources/js/languages.jsp'></script>");

// Include AJAX support by Dojo Framework
document.write("<script language='JavaScript1.2' src='resources/js/dojo.js'></script>");


// *********************************************************************************
  function ajaxLoadIntoElement(event, params, id_element, id_master, xsl, sync)
  {
 // alert("ajaxLoadIntoElement("+event+","+params+","+id_element+","+xsl+","+id_master+","+xsl+","+sync+")");

  if ((id_master!=null) && (id_master.length>0) && (document.getElementById(id_master)!=null)) document.getElementById(id_master).disabled=true;

  element=document.getElementById(id_element);
  element.innerHTML="<div name='ajax_msg' id='ajax_msg'>"+javascript_loading_data+"</div>";

  if (document.yedra_form==null)
     {
     alert(javascript_form_not_found);
     return;
     }

  if ((xsl==null) || (xsl.length<=0)) xsl="none";
  
  document.yedra_form.yedra_event.value=event;
  document.yedra_form.yedra_origin.value=document.yedra_form.getAttribute("origin");
  document.yedra_form.yedra_xsl.value=xsl;

 if (params!="")
     {
     items=params.split(";");

     // Set params values  
     for(i=0; i<items.length; i++) 
        {
        expresion=items[i].split("=");	
        value=expresion[1]
        
        if (value=="null") value="''";
        if (value=="reset") value="' '";
        if (items[i]!="") eval("document.yedra_form."+expresion[0]+".value="+value+";");
        }
     }
          
  formaction=document.yedra_form.action;
          
  oldaction=document.yedra_form.action;
  document.yedra_form.action=formaction;

  dojo.io.bind({
    formNode: document.yedra_form,
    load: function(type, data, evt)
      { 
        if ((id_master!=null) && (id_master.length>0) && (document.getElementById(id_master)!=null)) document.getElementById(id_master).disabled=false;
        element=document.getElementById(id_element);
   	element.innerHTML=data;
      },
    error: function(type, error)
      {       
        if ((id_master!=null) && (id_master.length>0) && (document.getElementById(id_master)!=null)) document.getElementById(id_master).disabled=false;
        element=document.getElementById(id_element);
        element.innerHTML="<div name='ajax_msg' id='ajax_msg'>"+javascript_error_loading_data+"</div>";	  
      },
    mimetype: "text/plain",
    sync: sync
    });

  document.yedra_form.action=oldaction;
  document.yedra_form.yedra_event.value="";  
  document.yedra_form.yedra_xsl.value=null;    
  }
  
// *********************************************************************************
  function raiseInternalEvent(event, target, params, confirm_message)
  {
  raiseInternalEvent(event, target, params, confirm_message, "");
  }

// *********************************************************************************
  function raiseInternalEvent(event, target, params, confirm_message, jscript)
  {
  // alert("raiseInternalEvent("+event+","+target+","+params+","+confirm_message+","+jscript+")");
  
  if (document.yedra_form==null)
     {
     alert(javascript_form_not_found);
     return;
     }

  if (document.yedra_form.yedra_refresh_opener!=undefined && target=="") document.yedra_form.yedra_refresh_opener.value="no";
     
  if (confirm_message!="") if (!confirm(confirm_message)) return false;	     
     
  formaction=document.yedra_form.action;

  if (jscript!="") eval(jscript);
    
  if (document.yedra_form.getAttribute("validation")=="yes") 
     if (!check_form(document.yedra_form.getAttribute("confirm"))) return;
  
  document.yedra_form.yedra_event.value=event;
  document.yedra_form.yedra_origin.value=document.yedra_form.getAttribute("origin");

  if (params!="")
     {
     items=params.split(";");

     // Set params values  
     for(i=0; i<items.length; i++) 
        {
        expresion=items[i].split("=");	
        value=expresion[1];
        
        if (value!=null)
        {
	       value=value.replace("}","'");
	       value=value.replace("{","'");
	      }
             
        if (value=="null") value="''";
        if (value=="reset") value="' '";
        if (items[i]!="") eval("document.yedra_form."+expresion[0]+".value="+value+";");
        }
     }

  if (event.indexOf("jscript:")==0) 
     {
     eval("on"+event.substring(event.indexOf(":")+1)+"();");
     return true;
     }
     
  if (target.indexOf("modalwindow")==0) 
     {
     agt=navigator.userAgent.toLowerCase();
     
     //Modificacion para que funcione con IE7. La versión se recoge de otra forma.
     //is_major=parseInt(navigator.appVersion);
     is_ie=((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

     strversion=navigator.appVersion;
     cadena="MSIE";
     pos=strversion.indexOf(cadena);
     is_major=parseInt(strversion.substring(pos+cadena.length,pos+cadena.length+2));

     if (is_ie && is_major<7)
       {
       eval(target);
       return true;
       }

     target=target.replace(/\modalwindow/gi,"modalwindow_mozilla");
     eval(target);
     return true;
     }

  if (target.indexOf("normalwindow")==0) 
     {
     eval(target);
     document.yedra_form.yedra_event.value="";     
     return true;
     }

  if (target.indexOf("frame")==0) 
     {
     eval(target);
     document.yedra_form.yedra_event.value="";     
     return true;
     }

  document.yedra_form.target=target;
  
  oldaction=document.yedra_form.action;
  document.yedra_form.action=formaction;

  if (target.indexOf("ajax")==0) 
     {
     ydrwaitlayer=document.createElement("div");
     ydrwaitlayer.setAttribute("id","ydrwaitlayer");
     ydrwaitlayer.style.cssText="text-align: left; left:"+(document.body.clientWidth/2-150)+"; z-index:10; position: absolute; top:"+(document.body.clientHeight/2-30)+"px; width: 300px; height:60px;  padding:10px; margin: 0 auto; background-color:#FFFFFF; border-width: 2px; border-style: solid; border-color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; opacity:0.8;";
     ydrwaitlayer.innerHTML="<table border=\"0\" cellspacing=\"10\"><tr><td><img src='resources/js/ajax_waiting.gif'/></td><td>"+javascript_ajax_sending_data+"</tr></table>";
     document.body.appendChild(ydrwaitlayer);

     dojo.io.bind({
     formNode: document.yedra_form,
     encoding: "utf-8",
     method: "post",
     load: function(type, data, evt)
      {
      document.body.removeChild(ydrwaitlayer); 
      if (data.length>0) 
      {      
      	if (data.toLowerCase().substring(0,5)=='<xml>')
      	{
      		// Remove XML tags
      		data=data.substring(5);
      		data=data.substring(0,data.length-6);
      		
      		// Invoke return function
      	  eval(event+"_return(data);");
      	}
      	else
          alert(data);
      }
      try
      {
       notify_ajax_finish(event);
      }
      catch(err){};
      },
     error: function(type, error)
      {       
      alert("ERROR: "+error.message);
      },
     mimetype: "text/plain",
     sync: true
     });
     return true;  
     }
  
  document.yedra_form.submit();   
  
  document.yedra_form.action=oldaction;
  document.yedra_form.yedra_event.value="";
  }

// *********************************************************************************
  function modalwindow(width,height,resize)
  {  
  formaction=document.yedra_form.action+"?";
  form_elements=document.yedra_form.getElementsByTagName("input");

  for(i=0; i<form_elements.length; i++)
     {
     var obj=form_elements[i];
     if (obj.tagName.toUpperCase()=="INPUT") 
        {
        if (i>0) formaction+="|";
        formaction+=obj.name+"="+obj.value;
        }
     }

  arguments=new Object();
  arguments.opener=self;
  
  document.yedra_form.yedra_event.value="";     
  
  url="controller.ydr?yedra_event=yedra_show_modal&action="+formaction;
  window.showModalDialog(url,arguments,"center:yes;dialogWidth:"+width+"px;dialogHeight:"+(height+60)+"px;status:yes;help:no;resizable:"+resize);
  }

// *********************************************************************************
  function modalwindow_mozilla(width,height,resize)
  {
  formaction=document.yedra_form.action;
  posleft=(screen.width-width)/2;
  postop=(screen.height-height)/2;  
  
  d=new Date();
  time=d.getTime()
  v=window.open("","normalwindow"+time,"top="+postop+",left="+posleft+",toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=no,modal=yes,width="+width+",height="+height);
  v.focus();
  
  oldtarget=document.yedra_form.target;
  oldaction=document.yedra_form.action;
  
  document.yedra_form.target="normalwindow"+time;
  document.yedra_form.action=formaction;
     
  document.yedra_form.submit();   
  
  document.yedra_form.target=oldtarget;
  document.yedra_form.action=oldaction;
  }
  
// *********************************************************************************
  function normalwindow(width,height,resize,menubar,toolbar)
  {
  formaction=document.yedra_form.action;
  posleft=(screen.width-width)/2;
  postop=(screen.height-height)/2;  
  
  d=new Date();
  time=d.getTime()
  v=window.open("","normalwindow"+time,"top="+postop+",left="+posleft+",toolbar="+toolbar+",location=0,directories=0,status=1,menubar="+menubar+",scrollbars=0,resizable="+resize+",width="+width+",height="+height);
  
  oldtarget=document.yedra_form.target;
  oldaction=document.yedra_form.action;
  
  document.yedra_form.target="normalwindow"+time;
  document.yedra_form.action=formaction;
     
  document.yedra_form.submit();   
  
  document.yedra_form.target=oldtarget;
  document.yedra_form.action=oldaction;
  }

// *********************************************************************************
  function frame(fra)
  {
  formaction=document.yedra_form.action;
  
  oldtarget=document.yedra_form.target;
  oldaction=document.yedra_form.action;
    
  document.yedra_form.target=fra;
  document.yedra_form.action=formaction;

  document.yedra_form.submit();   
  
  document.yedra_form.target=oldtarget;
  document.yedra_form.action=oldaction;
  }

// *********************************************************************************
  function check_form(confirm_message)
  {	
  	
  if (document.yedra_form==null) 
    {
    alert(javascript_form_not_found);
    return;
    }
 	
  form_elements=document.yedra_form.getElementsByTagName("input");
 	
  for (var i=0; i<form_elements.length; i++)
     {     	
     var form_element=form_elements[i];
     var value=form_element.value;
     var comment=form_element.getAttribute("comment");

     if (value==null)   value="";
     if (comment==null || comment=="") comment=form_element.getAttribute("name");
    
     if ((form_element.getAttribute("mandatory")!=null) && (value=="")) 
     	{
        alert(javascript_field_error+" '"+comment+"' "+javascript_is_mandatory);
        return false;
        }
     
     if ((form_element.getAttribute("validation")!=null) && (!((form_element.getAttribute("mandatory")==null) && (value==""))))
        {        	
        if ((form_element.getAttribute("validation")=="numeric") && (isNaN(value)))
           {
           alert(javascript_field_error+" '"+comment+"' "+javascript_is_numeric);
           return false;
           }        
	}

     if ((form_element.getAttribute("minnumericvalue")!=null) && (!((form_element.getAttribute("mandatory")==null) && (value==""))))
        {        
        var x=parseFloat(form_element.value);
        var y=parseFloat(form_element.getAttribute("minnumericvalue"));
        
        if (x<y)
           {
           alert(javascript_field_error+" '"+comment+"' "+javascript_great_or_equal+" '"+y+"'");
           return false;           	
           }        
	}

     if ((form_element.getAttribute("maxnumericvalue")!=null) && (!((form_element.getAttribute("mandatory")==null) && (value==""))))
        {    
	var x=parseFloat(form_element.value);
        var y=parseFloat(form_element.getAttribute("maxnumericvalue"));
                	    	
        if (x>y)
           {
           alert(javascript_field_error+" '"+comment+"' "+javascript_less_or_equal+" '"+y+"'");
           return false           	
           }        
	}
     }		
	
  if (confirm_message!=null && confirm_message!="") if (!confirm(confirm_message)) return false;	
  
  return true;
  }


// *********************************************************************************
  function disable_form_validation()
  {	  	
    document.yedra_form.setAttribute("validation", "0");
  }

