
var TIE = { Version: '2.0.0-Client' };

TIE.Calculadora = function(idPeticion, idioma){
	this._id = idPeticion;
	this._idioma = idioma;
	this._key = 'TIE';
	this._sitios = new Array();
	this._destinosNoTarifables = new Array();
	this._parents = new Array();
	
	this.addRangeSitios = function(){
		for(var i=0;i<arguments.length;++i) {
			if(arguments[i]!=undefined && arguments[i]>0){ this._sitios.push(arguments[i]); };
		};
	};
	
	this.addDestinoNoTarifableRange = function(){
		for(var i=0;i<arguments.length;i+=3) {
			if(arguments[i]!=undefined && arguments[i]>0 && arguments[i+1]!=undefined && arguments[i+1]>0){
				var identificador = arguments[i] + '_' + arguments[i+1] + '_' + arguments[i+2];
				this._destinosNoTarifables[identificador] = true;
			};
		};
	};
	
	this.esValido = function(){
		var currentSitioID = this._getActiveSitio();
		var currentDestinoID = this._getActiveDestino(currentSitioID);
		var currentMetodoID = this._getActiveMetodo(currentSitioID);		
		if(currentSitioID==undefined || currentSitioID<=0){
			alert(Utils.getResourceString(this._idioma, this._key, 'strMsgJsSitioInvalido'));
			return false;
		}
		else if(currentDestinoID==undefined || currentDestinoID<=0){
			alert(Utils.getResourceString(this._idioma, this._key, 'strMsgJsDestinoInvalido'));
			return false;
		}
		else if(currentMetodoID==undefined || currentMetodoID<=0){
			alert(Utils.getResourceString(this._idioma, this._key, 'strMsgJsMetodoInvalido'));
			return false;
		}
		else if(this._checkDestinoNoTarifable(currentSitioID, currentDestinoID, currentMetodoID)){
			alert(Utils.getResourceString(this._idioma, this._key, 'strMsgJsDestinoNoTarifa'));
			return false;
		}
		else { return true; };
	};
	
	this.registerParent = function(){
		if(arguments!=undefined && arguments.length>0){
			var parent = new Array();
			parent.push(arguments[0]);
			var metodos = new Array();
			for(var i=1;i<arguments.length;++i) {
				if(arguments[i]!=undefined && arguments[i]>0){ metodos.push(arguments[i]); };
			};
			parent.push(metodos);
			this._parents.push(parent);
		};
	};
	
	this.clickSitio = function(current){
		//si clicka en un sitio, todos los demas sitios y metodos se deseleccionarán
		//buscar el padre como sitio i actuar en consequancia al encontrarlo
		for(var i=0;i<this._parents.length;i++){
			var idSitio = this._parents[i][0];
			var id ='_' + this._id + '_sitio_' + idSitio
			if(id!=current.name){
				this._uncheckObject(id);
				//desmarcar todos los metodos
				var metodos = this._parents[i][1];
				for(var z=0;z<metodos.length;z++){ 
					this._uncheckObject('_' + this._id + '_' + idSitio + '_metodo_' + metodos[z]);
				};
			}
			else {
				//si es el mismo sitio y solo tiene un metodo, lo marcamos
				var metodos = this._parents[i][1];
				if(metodos.length==1){
					Utils.getElement('_' + this._id + '_' + idSitio + '_metodo_' + metodos[0]).checked = true;
				};
			};
		};
		this._setCurrentAll();
	};
	
	this._uncheckObject = function(id){
		var obj = Utils.getElement(id);
		if(obj!=undefined && obj.type=='checkbox' && obj.checked) { 
			obj.checked = false; 
		};
	};
	
	this.clickMetodo = function(current){
		var idSitio = 0;
		for(var i=0;idSitio==0 && i<this._parents.length;i++){
			var metodos = this._parents[i][1];
			for(var z=0;z<metodos.length;z++){ 
				var id ='_' + this._id + '_' + this._parents[i][0] + '_metodo_' + metodos[z];
				if(id==current.name){ idSitio = this._parents[i][0]; break;};
			};
		};
		
		//tenemos el sitio al que pertenece el metodo, ahora desmarcaremos todos los sitios, excepto el
		//que pertenece al metodo(que marcaremos) 
		for(var i=0;i<this._parents.length;i++){
			var sitio = Utils.getElement('_' + this._id + '_sitio_' + this._parents[i][0]);
			if(this._parents[i][0]==idSitio && current.checked){ sitio.checked = true; }
			else { sitio.checked = false; };
			//desmarcar todos los metodos
			var metodos = this._parents[i][1];
			for(var z=0;z<metodos.length;z++){ 
				if(idSitio!=this._parents[i][0]){
					this._uncheckObject('_' + this._id + '_' + this._parents[i][0] + '_metodo_' + metodos[z]);
				}
				else if('_' + this._id + '_' + this._parents[i][0] + '_metodo_' + metodos[z]!=current.name){
					this._uncheckObject('_' + this._id + '_' + this._parents[i][0] + '_metodo_' + metodos[z]);
				};
			};
		};
		
		this._setCurrentAll();
	};
	
	this._setCurrentAll = function(){
		this._setCurrentSitio('');
		this._setCurrentMetodo('');
		//recorrer los sitios, i el seleccionado, marcarlo
		//recorrer los metodos dels sitio y el seleccionado marcarlo
		for(var i=0;i<this._parents.length;i++){
			var objSitio = Utils.getElement('_' + this._id + '_sitio_' + this._parents[i][0]);
			if(objSitio!=undefined && objSitio.checked){
				var metodos = this._parents[i][1];				
				for(var z=0;z<metodos.length;z++){ 
					var objMetodo = Utils.getElement('_' + this._id + '_' + this._parents[i][0] + '_metodo_' + metodos[z]);
					//---COMENTADO 09/04/08 PORQUE EN ESTE PROYECTO NO APLICA 
					//if(objMetodo!=undefined && objMetodo.checked){ 						
					if(objMetodo!=undefined){ 
						this._setCurrentMetodo(metodos[z]); 
					};
				};
				this._setCurrentSitio(this._parents[i][0]);
			};
		};
	};
	
	this._setCurrentSitio = function(value)	{ this._setCurrent('_' + this._id + '_sitio', value); };
	this._setCurrentMetodo = function(value){ this._setCurrent('_' + this._id + '_metodo', value); };
	this._setCurrent = function(id, value)	{ var obj = Utils.getElement(id); if(obj!=undefined){ obj.value = value; }; };
	
	this._checkDestinoNoTarifable = function(idSitio, idDestino, idMetodo){
		var identificador = idSitio + '_' + idDestino + '_' + idMetodo;
		if(this._destinosNoTarifables[identificador]!=null && this._destinosNoTarifables[identificador]==true){ return true; }
		else {return false; };
	};
	
	this._getActiveSitio = function(){
		var obj = Utils.getElement('_' + this._id + '_sitio');
		if(obj!=undefined) { return obj.value; }
		else { return null; };
	};
	
	this._getActiveDestino = function(idSitio){
		var sitio = (idSitio!=undefined ? idSitio : this._getActiveSitio());
		if(sitio==undefined) {return null;};
		var destinos = Utils.getElement('_' + this._id + '_' + idSitio + '_destino');
		if(destinos!=null ) {
			if(destinos.type=='select-one'){
				var d = this._getSelectedElement(destinos);
				if(d!=undefined) { return d.value; }
				else { return null; };
			}
			else if(destinos.type=='hidden'){
				return destinos.value;
			};
		};
	};
	
	this._getActiveMetodo = function(idSitio){
		var obj = Utils.getElement('_' + this._id + '_metodo');
		if(obj!=undefined) { return obj.value; }
		else { return null; };
	};

	this._getSelectedElement = function(select){
		if(select!=null && select.type=='select-one') {
			for(var x=0;x<select.length;x++){
				if(select[x].selected) {return select[x];};
			};
		};
	};
	
	this._dropDestinosOnChange = function(select){
	    
	    /* las opciones pueden ser:
	            Federacion Farmaceutica:    Mostrar campo de ruta.   (108)
	            Galénica:                   Mostrar campo de ruta.   (109)
	            Cerf:                       Mostrar campo de ruta.   (110)
	            Hefame:                     NO mostrar campo de ruta.(111)
	    */
	    var option = this._getSelectedElement(select);
	    var div = Utils.getElement('_' + this._id + '_div_ruta_mayorista');
	    var input = Utils.getElement('_' + this._id + '_ruta_mayorista');
	    
	    if(option.value != 111){
	        div.style.display = 'block';
	    } else {
	        // Ha seleccionado Hefame, por lo que ocultaremos el campo de ruta
	        div.style.display = 'none';
	        input.value = '';
	    }
    };
    
    this._checkRutaMayorista = function(){
	    
	    /*  Comprueba si el campo de ruta esta rellenado si la opcion de mayorista está seleccionada. */
	    var currentSitioID = this._getActiveSitio();
	    if(currentSitioID == 113){
	        // si el sitio es 106 (mayorista)
		    var currentDestinoID = this._getActiveDestino(currentSitioID);
	        if(currentDestinoID != 118){
	            // si la opcion seleccionada es  Federacion Farmaceutica(108), Galénica(109) o Cerf(110)
	            var input = Utils.getElement('_' + this._id + '_ruta_mayorista');
	            if(input.value==''){
	                // si el campo ruta esta en blanco
	                alert(Utils.getResourceString(this._idioma, this._key, 'strMsgJsNoRuta'))
	                return false;
	            }
	        }
	    }
	    return true;
    };
};

/*	Gestor de Paises y Provincias	*/
TIE.SitiosManager = function(dropPaises, dropProvincias, txtProvincias){
	this._paises = new Array();
	this._provincias = new Array();
	
	this._pID = dropPaises;
	this._prID = dropProvincias;
	this._tID = txtProvincias;
		
	this.addPaises = function(){
		for(var i=0;i<arguments.length;i+=2){
			this._paises[arguments[i]] = arguments[i+1];
		}
	};
	
	this.addProvinciasPais = function(){
		if(arguments.length>0) {
			var idPais = arguments[0];
			this._provincias[idPais] = new Array();
			for(var i=1;i<arguments.length;i+=2){
				this._provincias[idPais][arguments[i]] = arguments[i+1];
			}
		}
	};
	
	this.dropPrShow = function(flag){
		var dropProvincias = Utils.getElement(this._prID);
		var txtProvincias = Utils.getElement(this._tID);
		dropProvincias.style.display =  flag ? 'inline' : 'none';
		txtProvincias.style.display =  flag ? 'none' : 'inline';
		if(!flag){txtProvincias.value = '';}
		else{txtProvincias.value = dropProvincias.options[dropProvincias.selectedIndex].value;}
	};
	
	this.onPaisesChange = function(){
		var drop = Utils.getElement(this._pID);
		var dropProvincias = Utils.getElement(this._prID);
		//get the selected pais, mirar si tiene alguna correpondencia, si la tiene esconder txt
		//provincias y mostrar drop. Si no las tiene a la inversa.
		var idPais = drop.options[drop.selectedIndex].value;
		if(this._provincias[idPais]!=undefined){
			var prov = this._provincias[idPais];
			var cont = 0;
			dropProvincias.options.length = 0;
			for(var p in prov){
				dropProvincias.options[cont] = new Option(prov[p], p);
				++cont;
			}
			this.dropPrShow(true);
		}
		else { this.dropPrShow(false);}
	};
	
	this.onProvinciasChange = function(){
		var dropProvincias = Utils.getElement(this._prID);
		var txtProvincias = Utils.getElement(this._tID);
		var idProvincia = dropProvincias.options[dropProvincias.selectedIndex].value;
		txtProvincias.value = idProvincia;
	};
	
	this.onProvinciasBlur = function(){
		var dropProvincias = Utils.getElement(this._prID);
		var txtProvincias = Utils.getElement(this._tID);
		var provinciaName = txtProvincias.value;
		dropProvincias.options.length = 0;
		dropProvincias.options[0] = new Option(provinciaName, provinciaName);
	};
	
	this.init = function(){
		var drop = Utils.getElement(this._pID);
		var cont = 0;
		drop.options.length = 0;
		for(var p in this._paises){
			drop.options[cont] = new Option(this._paises[p], p);
			++cont;
		}
	};
	
	this.selectPais = function(intIdPais){
		var dropPaises = Utils.getElement(this._pID);
		for(var i=0;i<dropPaises.options.length;i++){
			if(dropPaises.options[i].value==intIdPais){
				dropPaises.options[i].selected = true;
				break;
			}
		}
	};
	
	this.selectProvincia = function(intIdProvincia, strProvincia){
		var drop = Utils.getElement(this._pID);
		var dropProvincias = Utils.getElement(this._prID);
		var txtProvincias = Utils.getElement(this._tID);
		this.onPaisesChange();
		var flag = false
		for(var i=0;i<dropProvincias.options.length;i++){
			if(dropProvincias.options[i].value==intIdProvincia){
				dropProvincias.options[i].selected = true;
				flag = true;
				break;
			}
		}
		if(flag){this.dropPrShow(true);}
		else { this.dropPrShow(false);}
		txtProvincias.value = strProvincia;
	};
}


TIE.PagoManager = function(radioFormasPagoID, hiddenIdPedidoID){
	this._fp = new Array();
	this._rID = radioFormasPagoID;
	this._hID = hiddenIdPedidoID;
	
	this.addFormaPago = function(intIdFormaPago, evalfunction, idPedido){
		var formaPago = new Array();
		formaPago['id'] = intIdFormaPago;
		formaPago['evalfunction'] = evalfunction;
		formaPago['idpedido'] = idPedido;
		this._fp[this._fp.length] = formaPago;
	};
	
	this.onButtonClick = function(formulario){
		var radio = formulario[this._rID];
		var idFP = 0;
		if(radio.length!=undefined && radio.length>0)
		{
			for(var i=0;i<radio.length;i++){
				if(radio[i].checked){
					idFP = radio[i].value;
					break;
				}
			}
		}
		else { idFP = radio.value;}
		//obtener la forma de pago de las formas guardadas en el array i actuar segun las necesidades
		var formaPago = this._getFormaPago(idFP);
		var hIdPedido = Utils.getElement(this._hID);
		hIdPedido.value = formaPago['idpedido'];
		eval(formaPago['evalfunction']);
		return false;
	};
	
	this._getFormaPago = function(intIdFormaPago){
		for(var i=0;i<this._fp.length;i++){
			var auxfp = this._fp[i];
			if(auxfp['id']==intIdFormaPago){
				return auxfp;	
			}
		}
		return null;
	};
}
