/*
 *	Validator
 *
 * Esta funcion recorre todos los objetos del tipo text y password
 * verificando que el valor que contengan sean distinto a null o a
 * uno o varios espacios en blanco.
 *
 * Sintaxix: validator();
 *
 * by stylemedia(http://www.stylemedia.com.ar | info@stylemedia.com.ar)
*/

function validator(pObjMailName){	
	for (i=0;i < document.forms[0].elements.length;i++){
		var elemento = document.forms[0].elements[i];


		if(elemento.name == pObjMailName){
			if(ltrim(elemento.value) == ""){
				alert("No se ingreso ningun email o el email ingresado no es valido");
				elemento.value = ltrim(elemento.value);
				elemento.focus();
				return;
				
			}else if(isMail(elemento.value) == false){
				alert("No se ingreso ningun email o el email ingresado no es valido");
				elemento.value = ltrim(elemento.value);
				elemento.focus();
				return;			
			}
		}else if (elemento.type == "text"){
			elemento.value = ltrim(elemento.value);
			if(ltrim(elemento.value) == ""){
				alert("No se ha completado el formulario o los datos ingresados son incorrectos");
				elemento.value = ltrim(elemento.value);
				elemento.focus();
				return;
			}
		}else if(elemento.type == "password"){
			if(ltrim(elemento.value) == ""){
				alert("No se ha ingresado una clave o la clave ingresada no es válida");
				elemento.value = ltrim(elemento.value);
				elemento.focus();
				return;
			}
		}else if(elemento.type == "textarea"){
			if(ltrim(elemento.value) == ""){
				alert("No se a ingresado ningun comentario o los comentarios ingresados son incorrectos.");
				elemento.value = ltrim(elemento.value);
				elemento.focus();
				return;
			}
		}
	}
	document.forms[0].submit();
}

/*
 *	ltrim
 *
 * Esta funcion busca los caracteres en blanco del comienzo
 * de una cadena y los borra
 * 
 * Sintaxix: ltrim("cadena");
 *
 * by stylemedia(http://www.stylemedia.com.ar | info@stylemedia.com.ar)
*/

function ltrim(str){
	var i=0;
	var j=0;
	
	if(str.length > 0){
		for(i=0; i<=str.length-1; i++){
			if(str.substring(i,i+1) != ' ' && str.substring(i,i+1) != '<' && str.substring(i,i+1) != '>'){
				j = i;				
				return str.substring(j, str.length);
				j = 1;
				break;
			}else{
				break;
			}
		}
		if(j == 0){
			return "";
		}	
	}else{
		return str;
	}
}

function isMail(_email) {
     var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
     return emailReg.test(_email);
}
