
var Simulation = {

	field:false,
	procErrMsg:'Neste momento, no � poss�vel realizar esta opera��o. Tente mais tarde ou entre em contacto connosco atrav�s da Linha Seguros Continente 808 100 808.',
	
	Init:function(field){
	
		if(this.field) return false;
		
		function Bind(o){
			var z, i;
			for(i in o){
				if(z){
					o[z].next = o[i];
					o[i].previous = o[z];
				}
				z=i;
			}
			return o;
		}
		
		this.field = field;
		
		return Bind(this.field);
	},
	Transport:function(){
		
		if(this.http && this.http.processing) this.http.abort();
	
		Simulation.Error();
		
		var http = new Ajax.Request();
		http.method = "GET";
		http.timeout = 30;
		http.returnType = "application/json";
		http.url = "/Products/VehicleInsuranceData.ashx";
		
		http.onAbort = http.onTimeout = function(fromServer){
			Simulation.Error(this.procErrMsg);			
		}
		
		http.onLoading = function(){
			compono.appendClassName("loading-true",compono.getByID("loading"));
		}
			
		return this.http = http;
	
	},
	Clear:function(o,defaultValue,recursively){
		switch(o.nodeName.toUpperCase()){
			case "SELECT":
				while(o.options.length) o.remove(0);
				if(defaultValue){
					o.options[0] = new Option(defaultValue,"-");
				}
				break;
			case "RADIO":break;
			case "TEXT":
				o.value = defaultValue || "";
				break;
		}
		if(recursively && o.next){
			arguments.callee.call(this,o.next,o.next.getAttribute("defaultText"),recursively);
		}
	},
	Error:function( t )
	{
		if( t != null ){
			compono.getByID("errorText").innerHTML = t;
			compono.removeClassName("hidden",compono.getByID("errorMsg"));
			compono.appendClassName("hidden",compono.getByID("msgError"));
		} else {
			compono.getByID("errorText").innerHTML = "";
			compono.appendClassName("hidden",compono.getByID("errorMsg"));
		}
	},
	fmtMoney:function( f ) {
		var fieldValue = f.toString();
		var newValue = ""; var inicio = '';  
		var centavos = ''; var resto = '';    
	    
		if (fieldValue != '') {
			//elimina os zeros � esquerda
			while (fieldValue.indexOf('0') == 0 && (fieldValue.indexOf(',') != 1 && fieldValue!='0')) fieldValue = fieldValue.replace("0","");
			//elimina os sinais
			while (fieldValue.indexOf('-') != -1) fieldValue = fieldValue.replace("-","");

			if (fieldValue.indexOf('.') == -1) {  //n�mero sem decimal
				inicio   = fieldValue.substr(0,((fieldValue.length)%3));
				resto = fieldValue.substr((fieldValue.length)%3,fieldValue.length -(fieldValue.length)%3);
			} else {  //n�mero com decimal
				inicio   = fieldValue.substr(0,(fieldValue.indexOf('.'))%3);
				resto = fieldValue.substr(inicio.length,fieldValue.indexOf('.') -inicio.length);
				centavos= fieldValue.substr(fieldValue.indexOf('.')+1,2);
				//completa os centavos com zeros
				while (centavos.length != 2) centavos = centavos + '0';
			} 
			newValue = newValue + inicio;
			//acrescenta o ponto de milhar
			for (i=0; i<resto.length; i++) {
				if (((i>0) && ((i%3)==0)) || ((i==0) && (inicio != ""))) {
					newValue = newValue + '.';
				}
				newValue = newValue + resto.charAt(i);
			}
			if (newValue == '') newValue = '0';
			//acrescenta a v�rgula
			if (centavos != '') newValue = newValue + ',' + centavos;
			else newValue = newValue + ',00';

			return newValue;
		}
	},
	GetBrands:function(o,calledByCallAfter){
	
		
		if (this.field.Month.value == "-" || this.field.Year.value == "-")
			return false;
		
		if(!calledByCallAfter){
			arguments.callee.callAfter(0.5,this,o);
			return false;
		}
		
		this.Init();
		
		var http = this.Transport();
		http.scope = this.field.Brand;

		this.Clear(http.scope,"A carregar...",true);
		
		http.onComplete = function(data){
			if(data && data.ERROR==null){
				if(data.length){
					data.forEach(function(o){
						if (o.value != "" && o.text != "")
							this.options[this.options.length] = new Option(o.text, o.value);
					},this.scope)
					this.scope.options[0].text = "Seleccione";
				}else{
					this.scope.options[0].text = this.scope.getAttribute("defaultText");
				}
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}else{
				Simulation.Error( data.ERROR );
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}
		}
		
		http.params.add("op","BR");
		http.params.add("type", this.field.Type.value);
		http.params.add("month",this.field.Month.value);
		http.params.add("year",this.field.Year.value);
		
		http.process();
	},
	GetModels:function(o,calledByCallAfter){
	
		if (this.field.Month.value == "-" || this.field.Year.value == "-")
			return false;
		
		if(!calledByCallAfter){
			arguments.callee.callAfter(0.5,this,o);
			return false;
		}
		
		this.Init();
		
		var http = this.Transport();
		http.scope = this.field.Model;
		
		this.Clear(http.scope,"A carregar...",true);

		http.onComplete = function(data){
			if( data && data.ERROR==null){
				if(data.length){
					data.forEach(function(o){
						if (o.value != "" && o.text != "")
							this.options[this.options.length] = new Option(o.text, o.value);
					},this.scope)
					this.scope.options[0].text = "Seleccione";
				}else{
					this.scope.options[0].text = this.scope.getAttribute("defaultText");
				}
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}else{
				Simulation.Error( data.ERROR );
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}
		}
		
		http.params.add("op","MO");
		http.params.add("type", this.field.Type.value);
		http.params.add("month",this.field.Month.value);
		http.params.add("year",this.field.Year.value);
		http.params.add("brand",this.field.Brand.value);
		
		http.process();
		
	},
	GetVersions:function(o,calledByCallAfter){
	
		if (this.field.Month.value == "-" || this.field.Year.value == "-" || this.field.Model.value == "-")
			return false;
		
		if(!calledByCallAfter){
			arguments.callee.callAfter(0.5,this,o);
			return false;
		}
		
		this.Init();
		
		if( this.field.Type.value == "Vehicle")
		{
			var http = this.Transport();
			http.scope = this.field.Version;
			
			this.Clear(http.scope,"A carregar...",true);

			http.onComplete = function(data){
				if( data && data.ERROR==null){
					if(data.length){
						data.forEach(function(o){
							if (o.value != "" && o.text != "")
								this.options[this.options.length] = new Option(o.text, o.value);
						},this.scope)
						this.scope.options[0].text = "Seleccione";
					}else{
						this.scope.options[0].text = this.scope.getAttribute("defaultText");
					}
					compono.removeClassName("loading-true",compono.getByID("loading"));
				}else{
					Simulation.Error( data.ERROR );
					compono.removeClassName("loading-true",compono.getByID("loading"));
				}
			}
			
			http.params.add("op","VE");
			http.params.add("type", this.field.Type.value);
			http.params.add("month",this.field.Month.value);
			http.params.add("year",this.field.Year.value);
			http.params.add("brand",this.field.Brand.value);
			http.params.add("model",this.field.Model.value);
			
			http.process();
		}
	},
	GetVehicle:function(o,calledByCallAfter){
	
		if (this.field.Month.value == "-" || this.field.Year.value == "-" || this.field.Model.value == "-" || this.field.Version.value == "-")
			return false;
			
		
		if(!calledByCallAfter){
			arguments.callee.callAfter(0.5,this,o);
			return false;
		}
		
		this.Init();
		
		var http = this.Transport();
		http.scope = this;
		
		this.Clear(this.field.Info);
		this.Clear(this.field.Extra);
		this.Clear(this.field.Total);

		http.onComplete = function(data){
			if( data && data.ERROR==null){
				var f=this.scope.field;
				
				var a = parseFloat( data.price.replace('.', '').replace(',', '.') || 0 );
				var b = parseFloat( f.Extra.value.replace('.', '').replace(',', '.') || 0 );
				
				f.Info.value = Simulation.fmtMoney( a );
				f.Total.value = Simulation.fmtMoney( a + b );
				
				Validation.Init();
				
				ValidatorValidate(Validation.validator.Extra);
				
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}else{
				Simulation.Error( data.ERROR );
				compono.removeClassName("loading-true",compono.getByID("loading"));
			}
		}
		
		http.params.add("op","V1");
		http.params.add("type", this.field.Type.value);
		http.params.add("month",this.field.Month.value);
		http.params.add("year",this.field.Year.value);
		http.params.add("brand",this.field.Brand.value);
		http.params.add("model",this.field.Model.value);				
		http.params.add("version",this.field.Version.value);
		
		http.process();
	}
	
}




Function.prototype.callAfter = function(time,scope){
	
	if(this.CALLAFTER_ID) window.clearTimeout(this.CALLAFTER_ID);
	
	function F(){
		var a = arguments.callee; 
		if(a.run.apply(a.scope,a.args)==false){
			window.clearTimeout(a.run.CALLAFTER_ID);
			a.run.CALLAFTER_ID = -1;
		}
	}
	F.scope = scope||window;	//scope 
	F.run = this;	//function to be executed by timeout
	F.args = Array.prototype.slice.call(arguments,2);	//the rest of args
	F.args[F.args.length] = true;	//flag: executed by 'callAfter'
	
	return this.CALLAFTER_ID = window.setTimeout(F, (time||1)*1000);
}



