$(document).ready(function(){
	// GESTION DEL BOTON REGISTRO
	//$('#movil').numeric();
	$('#btn_registro_01').click(function(){
		
		// LEER DATOS DEL FORM
		var rut = $("input[id='rdo_rut']:checked").val();
		var id_emprendedor = $('#rut_pasaporte').val();			
		var nombres = encodeURI($('#nombres').val());
		var apellidos = encodeURI($('#apellidos').val());
		var email = $('#mail').val();
		var validacion_mail = $('#validacion_mail').val();
		var movil = $('#movil').val();
		var empresa = encodeURI($('#trabajo_actual').val());
		var profesion = encodeURI($('#trabajo_actual').val());
		var dondeTrabaja = encodeURI($('#donde_trabajas').val());
		var independiente = encodeURI($('#independiente').val());
		var dependiente = encodeURI($('#dependiente').val());
		var postData = false;
		var navigator_description = $('#navigator_description').val();		
		var navigator_is_chrome = false;
		var direccion =" ";
		


		// VALIDACION DE FORMULARIO
		reg = /^\s+/
		reg_num = /^\n+/
		nombre = $('#nombres').val();

		// COMPROBAR SI NAVEGADOR ES CHROME
		if(navigator_description.indexOf("Chrome") != -1)
		{
			navigator_is_chrome = true;
		}
		
		if( reg.test(nombre))
		{
			formAlert("Por favor escribe tus nombres correctamente", "nombres")
			return;
		}		
		if( nombres == "")
		{
			formAlert("Por favor escriba sus nombres", "nombres")
			return;
		}
		apellido = $('#apellidos').val();		
		if( reg.test(apellido) )
		{
			formAlert("Por favor escribe tus apellidos correctamente", "apellidos")
			return;
		}		
		if( apellidos == "")
		{
			formAlert("Por favor escriba sus apellidos", "apellidos")
			return;
		}			
		if( email == "")
		{
			formAlert("Por favor escriba mail de contacto", "mail")
			return;
		}			
		if( !validarEmail( email ) )
		{
			formAlert("Por favor escriba un mail de contacto v\u00e1lido", "mail")
			return;			
		}
		
		if( validacion_mail == "")
		{
			formAlert("Por favor reescriba su mail de contacto", "validacion_mail")
			return;
		}
		if( !validarEmail( validacion_mail ) )
		{
			formAlert("Por favor escriba un mail de contacto de confirmaci\u00f3n v\u00e1lido", "validacion_mail")
			return;			
		}		
		if( validacion_mail != email)
		{
			formAlert("Su mail de contacto debe coincidir con el reingreso del mail de contacto.", "validacion_mail")
			return;
		}	
		if( reg_num.test(movil))
		{
			formAlert("Por favor escribe un n\u00famero de contacto correcto", "movil")
			return;			
		}
		if( movil == "")
		{
			formAlert("Por favor escriba su n\u00famero de contacto", "movil")
			return;
		}	
		/*empresa2 = $('#trabajo_actual').val();
		if( reg.test(empresa2) )
		{
			formAlert("Por favor escribe el nombre de tu empresa correctamente", "trabajo_actual")
			return;
		}	*/		
		/*if( empresa == "")
		{
			formAlert("Por favor escriba el nombre de su empresa", "trabajo_actual")
			return;
		}	*/
		if( id_emprendedor == "")
		{
			formAlert("Por favor escriba su Rut o Pasaporte", "rut_pasaporte")
			return;
		}	
		if( rut == "on" ){
			if ( !Rut( id_emprendedor ) ){
				formAlert("Por favor escriba un Rut v\u00e1lido", "rut_pasaporte")
				return;
			}
		}

		if (profesion == "")
		{
			formAlert("Por favor escriba su Profesi\u00f3n", "trabajo_actual")
			return;
			}
		
		
				
		if (dondeTrabaja == "0")
		{ alert("Por favor seleccione su Lugar de Trabajo"); 
		return;
		}
		
		
		if (dondeTrabaja == "Independiente")
		{
			//alert (" independiente");
			if (independiente == "")
			{
				formAlert("Por favor ingrese nombre de la industria", "independiente") 
			return;
				}else{
					//alert ("else independeinte")
				direccion = independiente;
				//alert ("0direccion " + direccion);
				
				
				}			}
		
		
		if (dondeTrabaja == "Dependiente")
		{
			if (dependiente == "")
			{
				formAlert("Por favor ingrese nombre de la empresa", "dependiente") 
				return;
				}else{
				direccion = dependiente;
				
				}  
				
			}
			
			
				/*alert ("1direccion " + direccion);*/
		
		var postData = 
			"nombres="+nombres
			+"&apellidos="+apellidos
			+"&email="+email
			+"&movil="+movil
			+"&empresa="+empresa
			+"&id_emprendedor="+id_emprendedor
			+"&navigator_description="+navigator_description
			+"&profesion="+profesion
			+"&donde_trabajas="+dondeTrabaja
			+"&direccion="+direccion;
			
			
			/*alert ("direccion " + direccion);*/
		
		/*alert("Postdata: " + postData);return;*/
		
		$.ajax(
		{
			type: "POST",
			url: "process/act_registrar_emprendedor.asp",
			data: postData,
			success: function(serverMsg)
			{
				var respuesta = new String(serverMsg);
				//alert("Server Response: " + respuesta);return;
				
				if ( respuesta == "USER_ALREADY_EXIST" )
				{
					alert("El Rut o Pasaporte ingresado ya se encuentra registrado. Por favor revise sus datos.");
					return;
				}
				else if ( respuesta == "ERROR" )
				{
					alert("No es posible crear el registro en estos momentos. Por favor intentelo m\u00e1s tarde.");
				}
				else if ( respuesta == "SUCCESS" )
				{					
					resetForm("form");
					id_emprendedor = id_emprendedor.replace( ".", "").replace(".", "");
					//id_emprendedor = replace( trim(request.Form("id_emprendedor")) , "-", "")
					idemprendedor = id_emprendedor.split("-");
					id_emprendedor = idemprendedor[0]+idemprendedor[1];
					
					longitud_pwd = id_emprendedor.length;
					password = id_emprendedor.substring(longitud_pwd - 4)
	
					
					roll = "'btn_registro_03','','images/btn_finalizar_idea_roll.jpg',1";
					
					$.ajax(
					{
						type: "POST",
						url: "registro_end.asp",
						data: postData,
						success: function(pagina){
						
							$("#registro").html(
												'<div style="padding-left:15px;"><p><strong>Tu datos ya fueron registrados!</strong></p><p><strong>Ahora puedes ingresar para inscribir tus ideas con los siguientes datos:</strong></p><p><strong>Usuario : '+id_emprendedor+'</strong></p><p><strong>Password : '+password+'</strong></p><br /><a href="http://www.movistarinnova.cl"><img href="http://www.movistarinnova.cl" src="images/btn_finalizar_idea.jpg" alt="Finalizar" name="btn_registro_01" border="0" id="btn_registro_03" style="cursor:pointer;" onMouseOver="MM_swapImage('+roll+')" onMouseOut="MM_swapImgRestore()" /></a></div>'
												);
						
						}
					})
				} 
				else if ( respuesta == "CANT_CREATE_LOG" )
				{
					alert("Registro creado correctamente. No se pudo guardar el log de sistema");
				}
				else {
					alert("En estos momentos el servidor se encuentra ocupado. Por favor intentelo m\u00e1s tarde");
				}					
			}
		});
	});

});
