var cod_solicitud;	  
var numero;	  
var aux_destinos='';


//------------------------------------------------------------------------------------------------------------//	  
  function js_validaUsuario(nif, url){  // esta funcion llama al controlador validausuarioAction js_AjaxenviaForm
//------------------------------------------------------------------------------------------------------------//
	  if (!checkNIFNIE(nif)){
		  alert('NIF no valido');
		  $('frm').txt_nif.focus();
		  return		  
	  }//end if
	  if (verificaDatos()){   
			  var parametros=$(frm).serialize(); 
			    Dialog.info("Procesando... ",
			   				{width:250, height:100, showProgress: true});
			   
			    
					new Ajax.Request(url, {
						  method: 'post',
						  asynchronous: true,
						  parameters: parametros,
						  onSuccess: cargapagina
						 //onFailure: muestraError
						}); 
		}// end if 
		 
  } // end function  js_validaUsuario
	   
//------------------------------------------------------------------------------------------------------------//	
  function cargapagina(transport){ // esta funcion se ejecuta  si js_validaUsuario se realizo correctamente 
//------------------------------------------------------------------------------------------------------------//
		var tipo='';
		aux_destinos = (transport.responseText).split(';'); // del controlador devuelvo el cod_solicitud, el aleatorio que se envio por mail, el nif y el tip_contacto
		cod_solicitud=aux_destinos[0];
	  
		if (aux_destinos[3]==1){  tipo='Email';  }
		if (aux_destinos[3]==2){  tipo='Movil';  }
		if(aux_destinos[1]=='N' && cod_solicitud!=''){
			alert('No se puede realizar esta operacion');
			Dialog.closeInfo();	
			return;
		}
	   if (cod_solicitud>0){ // si el tip_contacto es > 0 es cuando se le envia un codigo al mail o al celular segun tip_contacto
		   if (window.confirm('Para poder modificar se enviará un código a su '+tipo )){
			   var div = document.getElementById('clave');		
			   div.style.display = 'inline';
		   }
	   }else{
	        if(cod_solicitud==-1){ // del controlador devuelvo -1 para controlar que si tip_contacto es 0 entonces para modificar tiene que hacerloe en Vimcorsa
				
				if (window.confirm('Para poder modificar debe ingresar el Nro. Borrador')){
					var div = document.getElementById('clave');		
					div.style.display = 'inline';
				}
			  //location.href='/solicitud/index?est_consulta=1&cod_solicitud='+aux_destinos[4]+'&nif='+aux_destinos[5]+'&var_upd=1';
			  Dialog.closeInfo();	
			  return;
			}else{
			   if (cod_solicitud==-2){ // del controlador devuelvo -2 para validar cuando no tiene creditos y no puede enviar sms
				   if (window.confirm('Para poder modificar se enviará un código a su '+tipo )){
					   alert('Ya no Tiene Créditos para envio de sms');
					   Dialog.closeInfo();	
			           return;
				   }// end if
				    
			   }else{
				   if(cod_solicitud==-3){
					     Dialog.closeInfo();	
					     js_ventanaPopup('/index/solicitudPopup')
			             return;
				   }else{
					     if (cod_solicitud==-4){
							 alert('Ha execedido el número de modificaciones para modificar su solicitud debe dirigirse a las instalaciones de Vimcorsa'); 
							  Dialog.closeInfo();	
						 }else{
						   var div = document.getElementById('clave');		
						   div.style.display = 'none';
						   location.href='/solicitud/index?est_consulta=0';
					   }// end if 
				   }// end if 
			   }
			}// end if    
	   }// end if 
	    Dialog.closeInfo();	
	  
  }// end function cargapagina
	  
//------------------------------------------------------------------------------------------------------------//	 
  function consultaSolicitud(){ // esta funcion se ejecuta cuanod se da clic en entrar despues que se ha colocado el codigo que se envio al mail o celular si son 		                                   iguales entonces se consulta la solicitud 
//------------------------------------------------------------------------------------------------------------//
	var clave = document.getElementById('txt_clave').value;		
    //var aleatorio= aux_destinos[4];
	var aleatorio= aux_destinos[6];
	//alert(aleatorio);
	
	  if (aleatorio == clave){
	    //location.href='/solicitud/index?est_consulta=1&cod_solicitud='+aux_destinos[0]+'&nif='+aux_destinos[2]
		location.href='/solicitud/index?est_consulta=1&cod_solicitud='+aux_destinos[4]+'&nif='+aux_destinos[5]+'&var_upd=1';
	  }else{
	    alert('El Nro. Borrador no se asocia al NIF/NIE');
	  }
	
  }// end function consultaSolicitud

//------------------------------------------------------------------------------------------------------------//
  function verificaDatos(){ // verfica que se hayan ingresados el nick 
//------------------------------------------------------------------------------------------------------------//
			 if($('frm').txt_nif.value==''){
				alert('Ingrese Nif');
				$('frm').txt_nif.focus();
				return false;
			  }//end if
		  
			
		
	return true;
	
  }// end function verificaDatos
  
//--------------------------------------------------------------------------------------------------------//  
  function pressEnter(evento, opcion){
//---------------------------------------------------------------------------------------------------------// 
	   var key =  validaEvento(evento);//window.event.keyCode? window.event.keyCode :window.event.which;
	   
	   //-----------------VALIDA CARACTERES DE IDENTIFICACION DEL NIE Y NIF----------------
	   if (opcion=1){
		  
		   
		  //---------------------VALIDA EL INGRESO DE CARACTERES PARA EL NIF-------------*/ 
		  //alert('paso -1*'+$('chk_nif').checked);
		  
		  if ($('frm').chk_nif.checked==true){
			  //alert($('txt_nif').value.length);
		  	  if(key == 8){ return true; }			  
			  
			  if ($('frm').txt_nif.value.length<8){
					if ((key < 48 || key > 57)&&(key!=46)){
						if(window.event){ 
							evento.keyCode = 0;
						}//end if
						return false;
					}//end if
			  }//end if
			  
			  if ($('frm').txt_nif.value.length==7){
				var letra=String.fromCharCode(key);
				//window.event.keyCode = 0;
				$('frm').txt_nif.value = $('frm').txt_nif.value + letra;
				valor=$('frm').txt_nif.value;
			  	var letra_nie = letra_NIF(valor);
				$('frm').txt_nif.value=$('frm').txt_nif.value+letra_nie;
				
				if(window.event){ 
					evento.keyCode = 0;
				}//end if
				return false;				
			  }//end if
			  
			  if ($('frm').txt_nif.value.length==8){
				  if ((key < 97 || key > 122)&&(key<65 || key>90)){
						if(window.event){ 
							evento.keyCode = 0;
						}//end if
					 	return false;
				  }//end if
			  }//end if
		  }//end if
		  //-----------------------------------------------------------------------------*/
		  //---------------------VALIDA EL INGRESO DE CARACTERES PARA EL NIE-------------*/ 		  
		  if ($('frm').chk_nie.checked==true){
			  //alert('key:'+ key);
  	   		  if(key == 8){ 
			  	 	return true;	  
			  }//end if
	   
			  if ($('frm').txt_nif.value.length==0){ 
				if(key!=88 && key != 89 && key != 120 && key != 121){
					return false;
				}  
			  }//end if
			
			  if ($('frm').txt_nif.value.length>0 && $('frm').txt_nif.value.length<8){
					if ((key < 48 || key > 57)&&(key!=46)){
						if(window.event){ 
							window.event.keyCode = 0;
						}//end if
						return false;
					}//end if
			  }//end if

			  //Pone la letra del NIE
			  if ($('frm').txt_nif.value.length==7){
				var letra=String.fromCharCode(key);		
				$('frm').txt_nif.value = $('frm').txt_nif.value + letra;
				valor=$('frm').txt_nif.value.substring(1,9);
			  	var letra_nie = letra_NIF(valor);
				$('frm').txt_nif.value=$('frm').txt_nif.value+letra_nie;

				if(window.event){ 
					window.event.keyCode = 0;
				}//end if
				return false;
			  }//end if
			  
			  if ($('frm').txt_nif.value.length==8){
				  if ((key < 97 || key > 122)&&(key<65 || key>90)){
						if(window.event){ 
							window.event.keyCode = 0;
						}//end if
						return false;
				  }//end if
			  }//end if

		  }//end if		  
		  //-----------------------------------------------------------------------------*/		  
	   }//end if
	   //----------------------------------------------------------------------------------	   
	   
		if(key==13){
			if (opcion==1){
			js_validaUsuario($('frm').txt_nif.value, '/index/validausuario');
			}// end if 
			if(opcion==2){
				consultaSolicitud();
			}// end if 
		}// end if 
 }// end fuction cambiarEnter

//--------------------------------------------------------------------------------------------------------//  
  function js_cambiaNIF(origen){
//---------------------------------------------------------------------------------------------------------//
	if (origen=='NIE'){
		$('frm').txt_nif.value = 'X';			
		$('frm').chk_nif.checked = 0;			
		$('divNIF').innerHTML = 'NIE:';
	}//end if

	if (origen=='NIF'){
	 	$('frm').txt_nif.value = '';;						
		$('frm').chk_nie.checked = 0;
		$('divNIF').innerHTML = 'NIF:';		
	}//end if
 }// end fuction js_cambiaNIF
 
 
 
 
 
 
 //------------------------------------------------------------------------------------------------------------------//
 function js_ventanaPopup(pagina){
//------------------------------------------------------------------------------------------------------------------//
  var  titulo = 'Solicitud Vivenda' ; 
  var  ancho  =  800 ; 
  var  largo  =  300  ;
	var winPlancta = new Window({className: "alphacube", title: titulo, 
						  width:ancho, height:largo, 					  
						  maximizable: false, resizable: false,minimizable: false,
						  url: pagina, showEffectOptions: {duration:0.5}})
			  
	winPlancta.showCenter();  
}//end function MostrarVentana
 
