// JavaScript Document

function formValidator(){
	// Establece referencias a los campos
/*01*/	var nombre = document.getElementById('nombre');
/*02*/	var paterno = document.getElementById('paterno');
//*03*/	var materno = document.getElementById('materno');
/*04*/	var email = document.getElementById('email');
//*05*/	var telcasa = document.getElementById('telcasa');
//*06*/	var teloficina = document.getElementById('teloficina');
/*07*/	var cel = document.getElementById('cel');
/*08*/	var calle = document.getElementById('calle');
/*09*/	var numext = document.getElementById('numext');
//*10*/	var col = document.getElementById('col');
/*11*/	var munideleg = document.getElementById('munideleg');
/*12*/	var edoprov = document.getElementById('edoprov');
/*13*/	var pais = document.getElementById('pais');
/*14*/	var postal = document.getElementById('postal');
	
	// Checa cada entrada en el orden en el que aparecen en el formulario
/*01*/	if(isAlphabet(nombre, "Escribe tu nombre.")){
/*02*/	if(isAlphabet(paterno, "Escribe tu apellido paterno.")){
//*03*/	if(isAlphabet(materno, "Escribe tu apellido materno")){
/*04*/	if(emailValidator(email, "Escribe una dirección de correo válida.")){
//*05*/	if(isNumeric(telcasa, "Escribe tu número telefónico.")){
//*06*/	if(isNumeric(teloficina, "Escribe el número de tu oficina.")){
/*07*/	if(isNumeric(cel, "Escribe tu teléfono.")){
/*08*/	if(notEmpty(calle, "Escribe tu calle.")){
/*09*/	if(notEmpty(numext, "Escribe el número exterior de tu casa.")){
/*10*/	if(notEmpty(col, "Escribe tu colonia.")){
/*11*/	if(notEmpty(munideleg, "Escribe tu municipio, delegación o ciudad.")){
/*12*/	if(notEmpty(edoprov, "Escribe tu estado o provincia.")){
/*13*/	if(notEmpty(pais, "Selecciona tu país.")){
/*14*/	if(isNumeric(postal, "Escribe tu código postal")){
	return true;
/*01*/	}
/*02*/	}
//*03*/	}
/*04*/	}
//*05*/	}
//*06*/	}
/*07*/	}
/*08*/	}
/*09*/	}
//*10*/	}
/*11*/	}
/*12*/	}
/*13*/	}
/*14*/	}	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z áéíóúñÁÉÍÓÚÑ]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Código incorrecto. Por favor, verifica el No. de tu boleto (código de barras)");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
