<?php 
class EVAjax extends AppEvento{
	
	protected $accion;
	protected $_url;
	
	
	
	
	protected $_correcto;
	
	
	public function setCorrecto($valor){
		$this->_correcto = $valor;
	}
	
	protected function renderAccion(){
		return '';
	}
	
	
	public function getCorrectoJQ(){
		
		
		$jq = 'function(data){'
				.((isset($this->_correcto) && strlen($this->_correcto))?$this->_correcto:'')
				."\n".'trataAjax(data);'
				
				.'}';
		
		return '.done('.$jq.')';
		
		
	}
	public function setUrl($valor){
		$this->_url = $valor;
	}
	public function getUrl(){
		return $this->_url;
	}
	
	
	public function renderEvento(){

		
		
		$principioFormulario = <<<HTML
		
		var misdatos = $(this).serializeArray();
				
		var idForm = $(this).attr('id');
		var aceptaFormData = true;
		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			aceptaFormData = false;
		}else{
				
			try{
					contentType = false
				var formData = new FormData($(this)[0]);
				formData.append('formulario', idForm);
		
			}catch(err){
	
					aceptaFormData = false;
			}
		}
		
				
		
		
		if(!aceptaFormData){
			contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
				listado = $(this).serializeArray();

			var formData ={};
			$(listado).each(function(){
				if(this.name.substring(this.name.length-2,this.name.length) == '[]'){
					var posicion = 0;
					if(formData[this.name.substring(0 , this.name.length-2)] != undefined){
						posicion = formData[this.name.substring(0 , this.name.length-2)].length;
					}else{
						formData[this.name.substring(0 , this.name.length-2)] = new Array();
					}
						
					formData[this.name.substring(0 , this.name.length-2)][posicion] = this.value;
				}else{
					formData[this.name] = this.value;
				}
					
				
			});
			
			$('input[type=file]', this).each(function(){
				formData[$(this).attr('name')] = '';
			});
				
				
			formData['formulario'] = idForm;
				
			//var datos = {formulario:idForm, datos:formData};				
	
		}
				
		
		
				
		$('.has-error',this).removeClass('has-error');
		$('.error-formulario',this).remove();
		
HTML;
		if(isset($_REQUEST['destino_autocomplete'])&& $_REQUEST['destino_autocomplete']){
		$principioFormulario .= "
				formData.append('destino_autocomplete', '".$_REQUEST['destino_autocomplete']."');
				";
		}
		
		
		$principioElemento = 'var datos = $(this).serializeArray();';
		
		if($this->padre instanceof AppElementoFormulario)
			$accion = $principioElemento;
		else
			$accion = $principioFormulario;
		
		$accion .='
				
				$.ajax({url:"'.BASEURL.$this->getUrl().'" , 
						type: "POST" , 
						data: formData, 
						async: false, 
						cache: false, 
						contentType: contentType, 
						processData: !aceptaFormData, 
						dataType : "json",})'
		.$this->getCorrectoJQ()
		.'; '.$this->renderAccion() . $this->renderCortarDisparador();
		
		$script = '$("'. $this->padre->identificadorScript() .'").'.$this->disparador.'(function(){'
		.$accion
		.'});';
		
		return $script;
	}
	
	
} 