//CONSTANT JAVASCRIPT : AJAX IN MODE ASINCRON O SINCRON A NO SE QUE S'ESPECIFIQUI
var sync=false;
var debug=false;

var xmlreqs = new Array();


function setDIV(divName,visible){
    try
    {
	document.getElementById(divName).style.visibility=visible;
    }
    catch(err)
    {
    }
}

function CXMLReq(divRetorno, afterAction,xmlhttp) {
    this.divRetorno = divRetorno;
    this.xmlhttp = xmlhttp;
    
    if (typeof afterAction!='undefined' )
        this.afterAction=afterAction;
    else
        this.afterAction=null;
}

//Function fas error de getAjaxDetallUpdate
function hasError(divDetallmsg)
{
    var obj=document.getElementById(divDetallmsg);
    
    for (i=0;i<obj.getElementsByTagName("input").length; i++) {
        if (obj.getElementsByTagName("input")[i].type == "hidden") {
           if (obj.getElementsByTagName("input")[i].name =="PRIMARYID") {
               return false;
           }
        }
    }
    return true;
}


function validateField(fieldId,type,required,size)
{
    var field=document.getElementById(fieldId);
    
    str="value="+field.value+"&type="+type;
    if (typeof required!='undefined' && required!=null ) 
    {
        if(required)
            str+="&required=true";
        else
            str+="&required=false";
    }
    if (typeof size!='undefined' && size!=null ) 
            str=str+"&size="+size;
            
    if (debug)        
        return true;
        
    ajSync("./Classes/Validation.php",str,fieldId+"_ERR");
}

//For front end
function validateFieldFE(fieldId,type,required,size)
{
    var field=document.getElementById(fieldId);
    
    str="value="+field.value+"&type="+type;
    if (typeof required!='undefined' && required!=null ) 
    {
        if(required)
            str+="&required=true";
        else
            str+="&required=false";
    }
    if (typeof size!='undefined' && size!=null ) 
            str=str+"&size="+size;
            
    if (debug)        
        return true;
        
    ajSync("./Core/Classes/Validation.php",str,fieldId+"_ERR");
}


function delayAlerts(divName,timeMilis)
{
    setTimeout("document.getElementById('"+divName+"_msg').innerHTML=''",timeMilis);
}

function aj(strUrl,strParams,divRetorno,beforeAction,afterAction){
	

    if (sync==true)
        ajSync(strUrl,strParams,divRetorno,beforeAction,afterAction);
    else
        ajAsync(strUrl,strParams,divRetorno,beforeAction,afterAction);
}

function ajSync(strUrl,strParams,divRetorno,beforeAction,afterAction){
    var xmlHttpReq=false;
    var self=this;
    
    if (typeof beforeAction!='undefined' && beforeAction!=null )
    {
        result=eval(beforeAction);
    }                                     
    else 
        result=true;
    
    if (result) 
    {
        if (window.XMLHttpRequest){     /*objeto XHR , menos en IE */
            self.xmlHttpReqSync= new XMLHttpRequest();
        }
        else if (window.ActiveXObject){     /* XHR solamente en IE*/
            self.xmlHttpReqSync = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReqSync.open('POST',strUrl,false);
        self.xmlHttpReqSync.setRequestHeader('Content-Type','application/x-www-form-urlencoded');   /*formulario HTML*/
        self.xmlHttpReqSync.send(strParams);
        updatePage(self.xmlHttpReqSync.responseText,divRetorno);
        
         if(typeof afterAction!='undefined' && afterAction!=null)
         {
            eval(afterAction);
         }
    }
}

function ajSyncFunction(strUrl,data){
    var xmlHttpReq=false;
    var self=this;

    if (window.XMLHttpRequest){     /*objeto XHR , menos en IE */
        self.xmlHttpReqSync= new XMLHttpRequest();
    }
    else if (window.ActiveXObject){     /* XHR solamente en IE*/
        self.xmlHttpReqSync = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReqSync.open('POST',strUrl,false);
    self.xmlHttpReqSync.setRequestHeader('Content-Type','application/x-www-form-urlencoded');   /*formulario HTML*/
    self.xmlHttpReqSync.send(data);
    return self.xmlHttpReqSync.responseText;
}


function ajAsync(url,data,divRetorno,beforeAction,afterAction) {
    var xmlhttp=false;
    
    if (typeof beforeAction!='undefined' && beforeAction!=null )
    {
        result=eval(beforeAction);
    }
    else
        result = true;
        
    if (result) 
    {
        if (xmlreqs.length==0)
        {
		    setDIV("loading","visible");
        }
        
        if (window.XMLHttpRequest) { // Mozilla etc.
            xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=xmlhttpChange;
        } else if (window.ActiveXObject) { // IE
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            if (xmlhttp) {                                        
                xmlhttp.onreadystatechange=xmlhttpChange;
            }
        }
        var xmlreq = new CXMLReq(divRetorno, afterAction,xmlhttp);
        xmlreqs.push(xmlreq);
        
        xmlhttp.open("POST",url,true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
        xmlhttp.send(data);
    }
} 
function xmlhttpChange() {
    var divRefresh;
    if (typeof(window['xmlreqs']) == "undefined") return;
    var xmldoc = null;
    var count=0;
    var executeAction;
    var numObj=xmlreqs.length;
    
    for (var i=0; i < xmlreqs.length; i++) {
		if (xmlreqs[i].xmlhttp.readyState == 4) {
            afterAction=xmlreqs[i].afterAction;
            updatePage(xmlreqs[i].xmlhttp.responseText,xmlreqs[i].divRetorno);    
                
            xmlreqs.splice(i,1); i--;
            count++;                    
                
            if(typeof afterAction!='undefined' && afterAction!=null)
            {
                eval(afterAction);
            }
        }
    }
    if (count==numObj)
    {
		setDIV("loading","hidden");	   
    }
}

function updatePage(strResponse,divRetorno){
    document.getElementById(divRetorno).innerHTML = strResponse;
}


function getAjaxParams(obj) {
        
  getstr="";
  
  for (i=0;i<obj.getElementsByTagName("INPUT").length; i++) {
        var str;
        if (obj.getElementsByTagName("INPUT")[i].type == "text") {
           str=obj.getElementsByTagName("INPUT")[i].value;
           /*str=str.replace(/’/g,"'");
		   str=str.replace(/€/g,"&euro;");
		   str=str.replace(/•/g,"·");*/
           getstr += "&" + obj.getElementsByTagName("INPUT")[i].name + "=" + encodeURIComponent(str);
        }
        if (obj.getElementsByTagName("input")[i].type == "password") {
           str=obj.getElementsByTagName("input")[i].value;
           /*str=str.replace(/’/g,"'");
           str=str.replace(/€/g,"&euro;");
           str=str.replace(/•/g,"·");*/
           getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + encodeURIComponent(str);
        }
        if (obj.getElementsByTagName("input")[i].type == "hidden") {
           str=obj.getElementsByTagName("input")[i].value;
           /*str=str.replace(/’/g,"'");
           str=str.replace(/€/g,"&euro;");
           str=str.replace(/•/g,"·");*/
           getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + encodeURIComponent(str);
        }
        if (obj.getElementsByTagName("input")[i].type == "checkbox") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=1";
           } else {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=0";
           }
        }
        if (obj.getElementsByTagName("input")[i].type == "radio") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value ;
           }
     }  
  }
  for (i=0;i<obj.getElementsByTagName("select").length; i++) {
    var sel = obj.getElementsByTagName("select")[i];
    if(sel.selectedIndex!=-1)
        getstr += "&"+ sel.name + "=" + encodeURIComponent(sel.options[sel.selectedIndex].value);
  }
  for (i=0;i<obj.getElementsByTagName("textarea").length; i++) {
    var ta = obj.getElementsByTagName("textarea")[i];
    str=ta.value;
    /*str=str.replace(/’/g,"'");
    str=str.replace(/€/g,"&euro;");
    str=str.replace(/•/g,"·");*/
    getstr += "&"+ ta.name + "=" + encodeURIComponent(str);
  }
  return getstr;
}



