// JavaScript Document
function chkbln(TheForm){  
	var nFocus, i
	var radioSelected 
	var fieldname 
	var msg
	var ls_contenido = ""
	var ls_pregunta = ""
	var ls_opcion =""
	for (i = 0 ;i < TheForm.elements.length;  i++) {
		//alert(TheForm.elements[i].value);
		//alert(TheForm.elements[i].type);
		switch(TheForm.elements[i].type){
			case "radio":
				fieldname = TheForm.elements[i].name ;
				nFocus = i ;
				radioSelected = false ;
				while (i < TheForm.elements.length && TheForm.elements[i].name == fieldname){
					if (TheForm.elements[i].checked){
						radioSelected = true
					}
					i++
				}
				if (! radioSelected){
					switch (fieldname ){
						case "rbss":
							msg = "No deje de confirmar el track de su interés. Su registro garantizará el acceso al mismo" ;
							break;
						default:
							msg= "";
							continue;
							break;
						};//FIN DE SWITCH
					alert( msg ) ;
					TheForm.elements[nFocus].focus() ;
					return false ;
					}//FIN DE IF
				i--				
				break;	

			case "select-one":
				if(!uf_validaselect(TheForm, i, "Debe seleccionar una opción..."))
					return false;
					break;
			case "textarea":
			case "password":
			case "text":
				fieldname = TheForm.elements[i].name ;
				switch(fieldname){
					case "txtnombre":
						if(!uf_validatext(TheForm, i, "El nombre no es opcional...",false,0))
							return false;
							break;
							
					case "txttel":
						if(!uf_validatext(TheForm, i, "El número telefónico no es opcional...",false,0))
							return false;
							break;
							
					case "txtcargo":
						if(!uf_validatext(TheForm, i, "El cargo/puesto no es opcional...",false,0))
							return false;
							break;
							
					case "txtemail":
						if(!uf_validatext(TheForm, i, "El e-mail no es opcional...",false,0)){
							return false
							break;
						}
						if(!checkAt(TheForm)){
							TheForm.txtemail.focus();
							return false;
							break;
						}
						break;
					case "txtempresa":
						if(!uf_validatext(TheForm, i, "El nombre de la empresa no es opcional...","",false,0))
							return false
							break;
							
					case "txtcalle":
						if(!uf_validatext(TheForm, i, "La calle no es opcional...","La calle proporcionada no es válida...",true,5))
							return false;
							break;
							
					case "txtnum":
						if(!uf_validatext(TheForm, i, "El número no es opcional...","",false,0))
							return false;
							break;
							
					case "txtcolonia":
						if(!uf_validatext(TheForm, i, "La colonia no es opcional...","La colonia proporcionada no es válida...",true,4))
							return false;
							break;
							
					case "txtmunicipio":
						if(!uf_validatext(TheForm, i, "La localidad no es opcional...","El municipio proporcionado no es válido...",true,4))
							return false;
							break;
							
					case "txtcp":
						if(!uf_validatext(TheForm, i, "El Código Postal no es opcional...","No ha especificado un código postal válido...",true,4))
							return false;
							
							if(!checkNumber(TheForm.elements[i].value)){
								alert("El formato del Código Postal no es válido")
								TheForm.elements[i].focus()
								return false;
							}
							break;
					case "txtCodeImage":
						if(!uf_validatext(TheForm, i, "Es necesario capturar el código de la imagen","No ha especificado un código de imagen válido...",true,6))
							return false;
							break;					
				} //FIN DE SWITCH
		} //FIN DE SWITCH
	} //FIN DE FOR
	return true;
} //FIN DE FUNCION

function ue_trim(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}

function checkNumber(valor) {
	 if(isNaN(valor)){
	   return false;
	}
	return true;
} 

function uf_validaselect(form, ai_elemento, as_texto){
	ls_valor = ue_trim(form.elements[ai_elemento].value)	
	if (ls_valor == "" ||  ls_valor == "0" ){
		alert( as_texto ) ;
		form.elements[ai_elemento].focus() ;
		return false;
	}
	return true;
}

function uf_validatext(form, ai_elemento, as_texto,msg,modo,largo){
	ls_valor = ue_trim(form.elements[ai_elemento].value)				
	if (ls_valor == "" ){
		alert(as_texto) ;
		form.elements[ai_elemento].focus();
		return false;
	}else if((ls_valor.length<largo)&&(modo==true)){
			alert( msg );
			form.elements[ai_elemento].focus();
			return false;
	}
	return true;
}
/*
function checkNumber(form) {
	 if(isNaN(form.txtCP.value)){
	   alert("Solo Números");
	   form.txtCP.value='';
	   form.txtCP.focus();
	   return false;
	} 		 
	return true;
}*/ 

function checkAt(form) {
   var correct = 0;
   var  seek   = 0;
   var  alpha  = form.txtemail.value;
   var  ampFind = new String(alpha);
   var  beta   = ampFind.length;
   for (seek = 0; seek<beta; seek++) {
     if(ampFind.charAt(seek)=="@") {
        var correct=1;
     }   
   }
   if (correct==0) {
     alert("Hace falta la  @ en la dirección de email");
	 return false;
   }
   return true;
}

function uf_validacontra(form){
	var  ls_contra  = form.txtContra.value;
	var  ls_recontra  = form.txtRecontra.value;	
	
	if ( ls_contra != ls_recontra) {		
		alert("No es la misma Contraseña!!");
		form.txtContra.value = '';
		form.txtRecontra.value = '';
		form.txtContra.focus();
		return false;
	}
	return true;
}

function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function isDisponible(){	
	var user, contenedor;
	contenedor = document.getElementById('contenedor');
	user = document.frmDatosGenerales.txtEmail.value;
	ajax=nuevoAjax();
	ajax.open("GET", "/app/usuarios/user.php?u=" + user,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)
}
