﻿    // JScript File
var AuxCnp;


function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
   
};

function getXMLurl(url, cnp )
{     
//alert ('funcion -->' + url);
//var sUrlDecode =URLDecode(url); 
//url=sUrlDecode 
//alert('sUrlDecode-->' + sUrlDecode);
    AuxCnp = cnp;
    //AjaxExec(url);
	//Si es Mozilla, Opera o safari (entre otros que lo soportan como objeto nativo del navegador)			
    if (window.XMLHttpRequest)
    {	        
	    httpReqContent = new XMLHttpRequest();
    }
    else	//Internet Explorer lo expone como control Active X
    {
	    httpReqContent = new ActiveXObject('Microsoft.XMLHTTP');
	    //httpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }

    //Ya debería tener una referencia al objeto
       
    if (httpReqContent != null)
    {        
	    //httpReqContent.onreadystatechange = FinishXML;
	    //httpReq.open('GET', url, true);	//El true del final es para que lo solicite de forma asíncrona 
	    // Nota: Se ha cambiado a false ya que no procesa correctamante dos peticiones seguidas como es el caso de rellenar el
	    // Combo de Marcas y Modelos. No Cambiar.
	    httpReqContent.open('GET', url, false);	//El true del final es para que lo solicite de forma asíncrona
	    httpReqContent.send(null);	//al ser una petición GET no va nada en el cuerpo de la petición, de ahí el null
	    
	    FinishXML();
	    
    }    	
}



//Función que se ejecuta cuando cambia el estado de la carga del objeto httpReq
function FinishXML()
{
 
    var oItem;
     
    
    if (httpReqContent.readyState == 4)	//4: completado, 3: en curso, 1:cargado, 0: no iniciado
    {
	    if (httpReqContent.status == 200)	//200: OK (código HTTP, podría haber sido 404 (no encontrado), 500 (error), etc...)
	    {		
	        items = httpReqContent.responseXML.getElementsByTagName('item');	//Coge sólo los elementos "item"            	                

            if (items.length>0)
            {
                oItem  =Number(items[0].getAttribute('value'));

                if (oItem ==  -1 ) 
                {
                    alert (vErrPhoneRegister);
                    
                }
                else if (oItem == -2 ) 
                
                {
                                
                      
                  
                        alert (vErrPass);
               
                    
                }
                else if (oItem == -3)
                {
                    alert (vErrSendContent);
                    
                }
                else if (oItem == -4)
                {
                    alert (vErrYaSendContent);
                   
                    document.getElementById(sIdenMaster + 'Modo').value = 5  ; 
                    if (document.getElementById('SelectContent').value!='')
                    {
                        document.getElementById(sIdenMaster + 'c').value = document.getElementById('SelectContent').value  ; 
                    }
                    document.getElementById(sIdenMaster + 'cnp').value = AuxCnp; 
                   
//                    location.href = location.pathname + '?Modo=5&c=' + document.getElementById('SelectContent').value + '&cnp='+ AuxCnp;
                    
                }
                else if (oItem == -5)
                {
                    alert (vErrLlamadaWebH3G);
                    
                }
                 else if (oItem == -9)
                {
                    alert (vErrYaParticipo);
                   document.getElementById(sIdenMaster + 'Modo').value ='1';
                    
                }
                   else if (oItem == -7)
                {
                    alert (vErrEnvioFakeMo);
                   document.getElementById(sIdenMaster + 'Modo').value ='1'
                    
                }
                   else if (oItem == -8)
                {
                    alert (vErrYaSendContent);
                    
                }
                else if (oItem == -105){
                  document.getElementById(sIdenMaster + 'Modo').value = 11 ; 
                }
                else if (oItem == 0)
                {
                    
                    document.getElementById(sIdenMaster + 'Modo').value = 3  ;
                    if (document.getElementById('SelectContent').value!='')
                    {
                        document.getElementById(sIdenMaster + 'c').value = document.getElementById('SelectContent').value  ; 
                    }
                    document.getElementById(sIdenMaster + 'cnp').value = AuxCnp; 
                    
//                    location.href = location.pathname + '?Modo=3&c=' + document.getElementById('SelectContent').value + '&cnp=' + AuxCnp;
                  
                }
                
            }
	    }
	    else	//Se produjo un error
	    {
		    alert("Err" + httpReqContent.statusText);
	    }
	    if (document.getElementById('txtPass')!= null)
	    {
	        document.getElementById('txtPass').value='';
	    }
	    if ( document.getElementById(sIdentificador +'btnSend')!=null)
	    {
	        if (oItem != 0)
	        {
	            document.getElementById(sIdentificador +'btnSend').disabled ='';
	        }
	    }
	    if ( document.getElementById('ImgSendPass')!=null)
	    {
	        if (oItem != 0)
	        {
	            document.getElementById('ImgSendPass').disabled ='';
	        }
	    }
    }	
}



