var ruta_imagen='data/assets/mimundo/images/bolitas.gif';
var tab1=0;
var tab2=0;
var tab3=0;

function majax()
		{
			var ObjetoAjax=false;
			try{
					ObjetoAjax=new ActiveXObject("Msxml2.XMLHTTP");
			   }
			 catch(e)  
			 	{
					try{
						ObjetoAjax=new ActiveXObject("Microsoft.XMLHTTP");
						}
					catch(E){ObjetoAjax=false;}	
				}
			if(!ObjetoAjax&& typeof XMLHttpRequest!='undefined'){ObjetoAjax= new XMLHttpRequest();}	
			return ObjetoAjax;
		}
	//ajax=majax();
  /* FUNCION PARA CARGAR EL FORMULARIO DEL ITEM CONTENIDO */
		function fConcurso(){
			
			if(tab3==1 || tab2==1)	
					{
						alert('Lo sentimos Debe acabar la encuesta');
					}
			else
				{
					obj=document.getElementById('contenedor');
					obj.innerHTML='<br><br><br>';
                	obj.innerHTML+='<img src="'+ruta_imagen+'" align="center" />';
                	obj.innerHTML+='Cargando';
				
					ajax=majax();
					ajax.open('POST', '?page=concurso', true);
					ajax.onreadystatechange=function(){
						if(ajax.readyState==4)
								{
								if(ajax.status==200)
									{obj.innerHTML=ajax.responseText;
									}
								else
									{		obj.innerHTML='<br><br><br><img src="'+ruta_imagen+'" align="center" />';
		                                    obj.innerHTML+='Cargando';
									}	
								}
							}
						ajax.send("");	
				}
		}
  
  
  function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
  }
 
 function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
 }
 
 function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
 }
	
	function verifico_usuario()
			{
				usu=document.getElementById('name_usuario').value;	
				if(usu==""||usu==null)
					{return false;}
				else
					{return true;}
			}
			
	 /*funcion valida minima cantidad de palabras*/
	
	 function validarMinPalabras(cadena,cantmax){
		 var i=0;
		 var temp = false;
		 cadena = trim(cadena);
			do {
				cadena = cadena.replace(' ','space');
				i++;
			} while(cadena.indexOf(' ') >= 0);
		
			temp = (i > (cantmax - 2))? true : false;
	return temp;
 	}
	
	
	
	
	
	 function validarSiNumero(numero){
		// alert("ingrese un numero");
    if (!/^([0-9])*$/.test(numero))
	  return false;
	  else
	  return true;
  		}
		

	
	/* FUNCION PARA CARGAR EL FORMULARIO DEL ITEM ESCRIBE DATOS */
    function fecribeDatos(){
		var datos;
		pa_re=document.getElementById('pais_resi').value;
		u_id=document.getElementById('usu_id').value;
		if(verifico_usuario())
			{
				if(pa_re>0)
					{
						//
							if(tab3==1)	
								{
									alert('Lo sentimos sus datos ya fueron guardados');
								}
							else{
									obj=document.getElementById('contenedor');
									obj.innerHTML='<br><br><br>';
									obj.innerHTML+='<img src="'+ruta_imagen+'" align="center" />';
									obj.innerHTML+='Cargando';
		
									ajax=majax();
									ajax.open('POST', '?page=escribetusDatos', true);
									ajax.onreadystatechange=function(){
										if(ajax.readyState==4)
											{
												if(ajax.status==200)
													{	tab1=1;
														obj.innerHTML=ajax.responseText;
													}
												else
													{
															//obj.innerHTML='Cargando...';
														  	obj.innerHTML='<br><br><br><img src="'+ruta_imagen+'" align="center" />';
						                                    obj.innerHTML+='Cargando';
													}	
											}
									}
									//ajax.send("");	
									//ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
									datos='&usu_id='+u_id;
									ajax.send(datos);	
		  
								}
						//
					}
				else
					{
						alert('Lo sentimos, el concurso es sólo para personas que residen en los Estados Unidos.');	
						//document.write('<div id="pmi_note"><span> Lo sentimos, el concurso es sólo para personas que residen en los Estados Unidos.</span><button false;="" return="" onclick="layer_hide(\'pmi_note\');" type="submit">OK</button></div>');
					}
			}
		else
			{
				//alert('Primero debes iniciar sesión. Si aún no eres miembro de Mundo sin Barreras™, regístrate ahora, \n es fácil, rápido y gratis. ');
				document.location.href='http://www.mundosinbarreras.com/temp_latinos';
			}
		}
		
/* */		
		function validar_datos()
			{	
				document.getElementById('direccion').style.border='1px Solid #7F9DB9';
				document.getElementById('ciudad').style.border='1px Solid #7F9DB9';
				document.getElementById('estado_').style.border='1px Solid #7F9DB9';
				document.getElementById('fono1').style.border='1px Solid #7F9DB9';
				document.getElementById('fono2').style.border='1px Solid #7F9DB9';
				document.getElementById('fono3').style.border='1px Solid #7F9DB9';
				document.getElementById('anios_eu').style.border='1px Solid #7F9DB9';
				document.getElementById('estado_civil').style.border='1px Solid #7F9DB9';
				document.getElementById('profesion').style.border='1px Solid #7F9DB9';
				document.getElementById('compania').style.border='1px Solid #7F9DB9';
				document.getElementById('nivel_ingles').style.border='1px Solid #7F9DB9';
				document.getElementById('nivel_academico').style.border='1px Solid #7F9DB9';
				document.getElementById('buscando').style.border='1px Solid #7F9DB9';
				document.getElementById('est_alet').style.display='none';
				document.getElementById('estc_alet').style.display='none';
				document.getElementById('niv_alet').style.display='none';
				document.getElementById('bus_alet').style.display='none';
				document.getElementById('ing_alet').style.display='none';
				document.getElementById('hijos_alet').style.display='none';
				

				direccion=document.getElementById('direccion').value;
				ciudad=document.getElementById('ciudad').value;
				est=document.getElementById('estado_').value;//-1
				fono1=document.getElementById('fono1').value;
				fono2=document.getElementById('fono2').value;
				fono3=document.getElementById('fono3').value;
				anios=document.getElementById('anios_eu').value;
				estado_civil=document.getElementById('estado_civil').value;//0
				profesion=document.getElementById('profesion').value;
				cia=document.getElementById('compania').value;
				nivel_igles=document.getElementById('nivel_ingles').value;//0
				nivel_academico=document.getElementById('nivel_academico').value;//0
				bucando=document.getElementById('buscando').value;//0
				numhijos=document.getElementById('nro_hijos').value;//0
				
				
				
				
				fono=fono1+fono2+fono3;
				fono=validarSiNumero(fono);
				
				if(direccion==""||ciudad==""|| est=="-1" || fono1=="" || fono2=="" || fono3=="" || anios=="" || estado_civil=="0" || profesion=="" || cia=="" || nivel_igles=="0" || nivel_academico=="0" || bucando=="0" || numhijos=="-1" || fono==false)
					{
						document.getElementById('mensaje').style.display="";
						if(direccion==""){document.getElementById('direccion').style.border='1px Solid #FF0000';}
						if(ciudad==""){document.getElementById('ciudad').style.border='1px Solid #FF0000';}
						if(est=="-1"){document.getElementById('estado_').style.border='1px Solid #FF0000';
								document.getElementById('est_alet').style.display='';
							     }
						if(fono1==""){document.getElementById('fono1').style.border='1px Solid #FF0000';}
						if(fono2==""){document.getElementById('fono2').style.border='1px Solid #FF0000';}
						if(fono3==""){document.getElementById('fono3').style.border='1px Solid #FF0000';}
						if(anios==""){document.getElementById('anios_eu').style.border='1px Solid #FF0000';}
						if(estado_civil=="0"){  document.getElementById('estado_civil').style.border='1px Solid #FF0000';
									document.getElementById('estc_alet').style.display='';
								     }
						if(profesion==""){document.getElementById('profesion').style.border='1px Solid #FF0000';}
						if(cia==""){document.getElementById('compania').style.border='1px Solid #FF0000';}
						if(nivel_igles=="0"){document.getElementById('nivel_ingles').style.border='1px Solid #FF0000';
								     document.getElementById('ing_alet').style.display='';	
								    }
						if(nivel_academico=="0"){document.getElementById('nivel_academico').style.border='1px Solid #FF0000';
							                 document.getElementById('niv_alet').style.display='';
								        }
						if(bucando=="0"){document.getElementById('buscando').style.border='1px Solid #FF0000';
								 document.getElementById('bus_alet').style.display='';
								}
								
								
								/**/
							if(numhijos=="-1"){document.getElementById('nro_hijos').style.border='1px Solid #FF0000';
							document.getElementById('hijos_alet').style.display='';
							}
						/**/
						
						
						
				/*-------------------------validando solo numeros------------------------------*/
				
				if(fono==false)
				    {
					document.getElementById('fono1').style.border='1px Solid #FF0000';
					document.getElementById('fono2').style.border='1px Solid #FF0000';
					document.getElementById('fono3').style.border='1px Solid #FF0000';
					document.getElementById('alet_telefono').style.display='inline';
					
					}else{	
					document.getElementById('alet_telefono').style.display='none';
					}
				/*---------------------------------------------------------*/
						
					
						
						return false;	
					}
				else
					{
						return true;	
					}
					

			}
		
		/* FUNCION PARA CARGAR EL FORMULARIO DEL ITEM RESPONDE CUESTIONARIO */		

		function fRespondeCuestionario(){
			var datos;
			if(tab1==1)
				{
					if(tab3==1)
						{
								obj=document.getElementById('contenedor');
								obj.innerHTML='<br><br><br>';
								obj.innerHTML+='<img src="'+ruta_imagen+'" align="center" />';
								obj.innerHTML+='Cargando';
								ajax=majax();
								ajax.open('POST', '?page=respondeCuestionario', true);
								ajax.onreadystatechange=function(){
									if(ajax.readyState==4)
										{
											if(ajax.status==200)
												{	tab2=2;
													obj.innerHTML=ajax.responseText;
												}
											else
												{
													//obj.innerHTML='Cargando...';
														obj.innerHTML='<br><br><br><img src="'+ruta_imagen+'" align="center" />';
						                                obj.innerHTML+='Cargando';
												}	
										}
								}
								ajax.send("");	

						}
					else
						{	if(validar_datos())
								{
									dir=document.getElementById('direccion').value;
									ciu=document.getElementById('ciudad').value;
									est=document.getElementById('estado_').value;
									fono1=document.getElementById('fono1').value;
									fono2=document.getElementById('fono2').value;
									fono3=document.getElementById('fono3').value;
									fono=fono1+fono2+fono3;
									anio=document.getElementById('anios_eu').value;
									e_civil=document.getElementById('estado_civil').value;
									nro_hijos=document.getElementById('nro_hijos').value;
									profesion=document.getElementById('profesion').value;
									compania=document.getElementById('compania').value;
									n_ingles=document.getElementById('nivel_ingles').value;
									n_academico=document.getElementById('nivel_academico').value;
									bus=document.getElementById('buscando').value;									
									
									obj=document.getElementById('contenedor');
									ajax=majax();
									//ajax.open('POST', '?page=respondeCuestionario', true);
									ajax.open('get','?page=respondeCuestionario&v_dir='+dir+'&v_ciu='+ciu+'&v_est='+est+'&v_fono='+fono+'&v_anio='+anio+'&v_civil='+e_civil+'&v_hijos='+nro_hijos+'&v_profesion='+profesion+'&v_com='+compania+'&v_ingles='+n_ingles+'&v_academico='+n_academico+'&v_bus='+bus, '?page=respondeCuestionario', true);
									ajax.onreadystatechange=function(){
										if(ajax.readyState==4)
											{
												if(ajax.status==200)
													{	tab2=2;
														obj.innerHTML=ajax.responseText;
													}
												else
													{
														obj.innerHTML='<br><br><br><img src="'+ruta_imagen+'" align="center" />';
						                                obj.innerHTML+='Cargando';
													}	
											}
									}
									
									ajax.send(null);	
								}	
							
						}
					
				}
			else
				{
					alert('Primero debes llenar el paso 1.');	
				}
		}
		/* FUNCION PARA CARGAR EL FORMULARIO DEL ITEM INCLUYE VIDEO Y FOTO */
		function fIncluyeVideo(cuando,meta,obstaculos,soluciones,motivacion,mejor,consejo){
			//alert("valores mandado "+cuando+meta+obstaculos+soluciones+motivacion+mejor+consejo);
						
				/*·····························Validando no vacios·························*/		
			if(cuando!="" && meta!="" && obstaculos!="" && soluciones!="" && motivacion!="" && mejor!="" && consejo!="")
	         {
			
			
		 if(validarMinPalabras(obstaculos, 45) == false || validarMinPalabras(mejor, 45) == false){ //validar cantidad de palabras
			
			alert("Las respuestas a la tercera y sexta pregunta deben tener 45 palabras como mínimo" );

		 }else
		 {
			
			
			if(tab2==2)
				{	tab1==0;
					obj=document.getElementById('contenedor');
					obj.innerHTML='<br><br><br>';
					obj.innerHTML+='<img src="'+ruta_imagen+'" align="center" />';
					obj.innerHTML+='Cargando';
					ajax=majax();
					
					datos='&v_cuando='+cuando+'&v_meta='+meta+'&v_obstaculos='+obstaculos+'&v_soluciones='+soluciones+'&v_motivacion='+motivacion+'&v_mejor='+mejor+'&v_consejo='+consejo;
					
					/*var dataTodos=document.getElementsByTagName("input");
					alert("array"+dataTodos[15].value);*/
					//datos='&v_cuando=ayer';
					datos='&v_cuando='+cuando+'&v_meta='+meta+'&v_obstaculos='+obstaculos+'&v_soluciones='+soluciones+'&v_motivacion='+motivacion+'&v_mejor='+mejor+'&v_consejo='+consejo;
			//alert("no pasa"+datos);
					ajax.open('get','?page=incluyeVideoFoto'+datos, true);
					ajax.onreadystatechange=function(){
						if(ajax.readyState==4)
							{
								if(ajax.status==200)
									{	tab3=1;
										obj.innerHTML=ajax.responseText;
									}
								else
									{
										obj.innerHTML='<br><br><br><img src="'+ruta_imagen+'" align="center" />';
						                obj.innerHTML+='Cargando';
									}	
								
							}
					}
					ajax.send(null);		
					
				}
			else
				{
					alert('Primero debes llenar el paso 2.');
				}
				

		 } //fin valida minimo caracteres
		 
		 
			//fin expacios vacios	
		}else{ alert("Debes llenar todo el cuestionario para poder continuar.");}
		
		
					  
					  /*······················································*/	
		}
		
	
	function mandar_archivo()
			{	video=document.getElementById('video');
				foto=document.getElementById('foto');
				arch=document.getElementById('archivo').value;
				leer=document.getElementById('checkbox');
				edad=document.getElementById('checkbox2');
				//alert(video.checked);
				//alert(foto.checked);
				if(!video.checked && !foto.checked) 	
					{alert('Selecciona el tipo de archivo que enviarás al concurso.');return false;}
					
				if(arch=="")
					{alert('Debes adjuntar un archivo para participar en el concurso.');return false;}
				
				if(!leer.checked) 	
					{alert('Debes aceptar las Reglas del concurso.');return false;}
					
				if(!edad.checked) 	
					{alert('Debes garantizar que tienes más de 18 años y que has leído el Testimonial Release.');return false;}
				
					/*
					obj=document.getElementById('contenedor');
					ajax=majax();
					ajax.open('POST', '?page=subir_archivo', true);
					ajax.onreadystatechange=function(){
						if(ajax.readyState==4)
							{
								if(ajax.status==200)
									{	tab3=1;
										obj.innerHTML=ajax.responseText;
									}
								else
									{
										obj.innerHTML='Cargando...';
									}	
								
							}
					}
				ajax.send("");	
				*/
				document.frm_video.submit();
			}
