// JavaScript Document

function validarForm(){
	
	if(document.form1.nombres.value == ""){
		alert("Debe llenar el campo NOMBRE !!!");
		document.form1.nombres.focus();
		return false;
	}
	if(document.form1.apellidos.value== ""){
		alert("Debe llenar el campo APELLIDOS !!!");
		document.form1.apellidos.focus();
		return false;
	}
	if(document.form1.telefono.value== ""){
		alert("Debe llenar el campo TELEFONO !!!");
		document.form1.telefono.focus();
		return false;
	}
	if(document.form1.telefono.value!= ""){
		var t = document.form1.telefono.value;
		var phoneChars = "()-+ ";
		modString = stripCharsInBag(t,phoneChars);
    	if((isInteger(modString))==false){
			return false;
		}
	}
	if(document.form1.correo.value== ""){
		alert("Debe llenar el campo CORREO !!!");
		document.form1.correo.focus();
		return false;
	}
	if(document.form1.correo.value!= ""){
		var m = unescape(document.form1.correo.value);
		var i = 1;
		var mLength = m.length;
		while ((i < mLength) && (m.charAt(i) != "@")){
			i++
		}
		if ((i >= mLength) || (m.charAt(i) != "@")){
			alert("Correo inválido");
			return false;
		} else {
			i += 2;
		}
		while ((i < mLength) && (m.charAt(i) != ".")){
			i++
		}
		if ((i >= mLength - 1) || (m.charAt(i) != ".")){
			alert("Correo inválido");
			return false;
		}
	}	
	if(document.form1.motivo.selectedIndex==0){
		alert("Debe seleccionar un MOTIVO !!!");
		document.form1.motivo.focus();
		return false;
	}
	if(document.form1.mensaje.value == ""){
		alert("Debe llenar el campo MENSAJE !!!");
		document.form1.mensaje.focus();
		return false;
	}
	return true;
	
}

function validarRegister(){
	if(document.form2.nombres.value == ""){
		alert("Debe llenar el campo NOMBRE !!!");
		document.form2.nombres.focus();
		return false;	
	}
	if(document.form2.correo.value== ""){
		alert("Debe llenar el campo CORREO !!!");
		document.form2.correo.focus();
		return false;
	}
	if(document.form2.correo.value!=""){
		var m = unescape(document.form2.correo.value);
		var i = 1;
		var mLength = m.length;
		while ((i < mLength) && (m.charAt(i) != "@")){
			i++
		}
		if ((i >= mLength) || (m.charAt(i) != "@")){
			alert("Correo inválido");
			return false;
		} else {
			i += 2;
		}
		while ((i < mLength) && (m.charAt(i) != ".")){
			i++
		}
		if ((i >= mLength - 1) || (m.charAt(i) != ".")){
			alert("Correo inválido");
			return false;
		}
	}	
	return true;
	
}

function validarCorreo(){
	if(document.form3.correo.value== ""){
		alert("Debe llenar el campo CORREO !!!");
		document.form3.correo.focus();
		return false;
	}
	if(document.form3.correo.value!=""){
		var m = unescape(document.form3.correo.value);
		var i = 1;
		var mLength = m.length;
		while ((i < mLength) && (m.charAt(i) != "@")){
			i++
		}
		if ((i >= mLength) || (m.charAt(i) != "@")){
			alert("Correo inválido");
			return false;
		} else {
			i += 2;
		}
		while ((i < mLength) && (m.charAt(i) != ".")){
			i++
		}
		if ((i >= mLength - 1) || (m.charAt(i) != ".")){
			alert("Correo inválido");
			return false;
		}
	}	
	return true;
	
}

function nuevo_registro(){
	document.getElementById('register').innerHTML='<strong class="text03">Registrese por favor<strong>';
	document.form2.nombres.focus();
}

function stripCharsInBag (s, bag){
	var i;
    var returnString = "";
	
    for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isInteger (n){
	
	var i;
    
    for (i = 0; i < n.length; i++){   
        var c = n.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c)){
				alert("Número de teléfono inválido");
				return false;
			}
        } else { 
            if (!isDigit(c) && (c != "-") || (c == "+")){
				alert("Número de teléfono inválido");
				return false;
			}
        }
    }
}

function isDigit (d){
	return ((d >= "0") && (d <= "9"))
}
