// JavaScript Document
/*************************************/


function Valida(NameForm){
	var Errs = "";
	var thisForm;
	
	if(BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Explorer" || BrowserDetect.browser == "Chrome")
		thisForm = document.getElementById(NameForm);
	else{
		if(BrowserDetect.browser == "Opera" || BrowserDetect.browser == "Safari")
			thisForm = document.forms[NameForm];
		else
			thisForm = document.getElementById(NameForm);
	}

	for(var i=0;i<thisForm.length;i++){
		var str=thisForm[i].value;
		
		if((thisForm[i].type=="text")||(thisForm[i].type=="password")||(thisForm[i].type=="textarea")){
			if(thisForm[i].title == 'filled'){
				
				if(str.length == 0)
					Errs += "- Campo "+thisForm[i].name+" no puede ser nulo \n";
				if(str.length > 0){
					if(thisForm[i].minlength){
						if(str.length < thisForm[i].minlength)
							Errs += "- Campo "+thisForm[i].name+" debe tener minimo "+ thisForm[i].minlength +" caracteres \n";
					}
				}
			}
			
			if(thisForm[i].date && thisForm[i].type !="password" && thisForm[i].type != "textarea"){
				if(str.length == 0)
					Errs += "- Campo "+thisForm[i].name+" no puede ser nulo \n";
				else{
					if(str.length == 10){
						if((str.substr(2,1) != thisForm[i].separador) || (str.substr(5,1) != thisForm[i].separador))
							Errs +=	"- El formato del campo " + thisForm[i].name + " es incorrecto, favor de verificarlo.("+thisForm[i].formato+")";
						else
							Errs += ValidaFecha(str,thisForm[i].formato,thisForm[i].separador);
					}else
						Errs += "- Campo "+thisForm[i].name+" no puede ser menor de 10 caracteres \n";
				}
			}
			
			if(thisForm[i].title == 'email'){
				if(str.length == 0)
					Errs += "- Campo "+thisForm[i].name+" no puede ser nulo \n";
				else{
					if(!isValidEmailAddress(str))
						Errs += "- Campo"+thisForm[i].name+" debe tener @ y . para ser valido \n";
				}
			
			}
			
		}//IF DE TEXT, PASSWORD Y TEXTAREA
		
		if(thisForm[i].type=="radio"){
			var myOption = -1;
			var objRadioBtn = document.getElementsByName(thisForm[i].name);
			for (var a=0; a< objRadioBtn.length; a++) {
				if(objRadioBtn[a].checked)
					myOption = a;
			}
			if (myOption == -1)
				Errs += "- Debes seleccionar un elemento de: "+thisForm[i].name+"\n";
			i = i + objRadioBtn.length-1;
		}
		
	}
	if(Errs.length == 0)
		return true;
	else{
		alert(Errs);
		return false;
	}
}

function ValidaFecha(ValorFecha,Formato,Separador){
	/*Formatos de Fecha Aceptables*/
	// mm/dd/aaaa   --> Mes/Dia/Año
	// dd/mm/aaaa   --> Dia/Mes/Año
	
	
	var dtCh= Separador;
	var minYear=1900;
	var maxYear=2100;

	var daysInMonth = DaysArray(12);
	var pos1 = ValorFecha.indexOf(dtCh);
	var pos2 = ValorFecha.indexOf(dtCh,pos1+1);
	
	var strMonth;
	var strDay;
	var strYear;
	
	if(Formato.toUpperCase() == "MM/DD/AAAA"){
		strMonth=ValorFecha.substring(0,pos1);
		strDay=ValorFecha.substring(pos1+1,pos2);
		strYear=ValorFecha.substring(pos2+1);
	}else{
		if(Formato.toUpperCase() == "DD/MM/AAAA"){
			strDay=ValorFecha.substring(0,pos1);
			strMonth=ValorFecha.substring(pos1+1,pos2);
			strYear=ValorFecha.substring(pos2+1);
		}else
			return "Formato de Fecha No Soportado";
	}
	
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYear);
	
	if (month<1 || month>12)
		return "Mes invalido";
	
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		return "Dia invalido";
	
	if (year==0 || year<minYear || year>maxYear)
		return "Año invalido";
	
	if (ValorFecha.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(ValorFecha, dtCh))==false)
		return "Fecha erronea";
	
	return "";
}

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isValidEmailAddress(emailAddress) {
 	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 	return pattern.test(emailAddress);
}

function ValidarCaracteresEnFormulario(ObjetoFormulario){
	var Formulario = ObjetoFormulario;
	var Exp = /^[A-Za-z0-9]*$/;
	var Patt = /\W/g;
	var CaracteresEncontrados = "";
	var CharCode;
	
	for(var i=0;i<Formulario.length;i++){
		if((Formulario[i].type=="text") || (Formulario[i].type=="password") || (Formulario[i].type=="textarea")){
			var strValue = Formulario[i].value;
			if(strValue.length > 0){
				if(!Exp.test(strValue)){
					var Simbolos = strValue.match(Patt);
					var ArrSimbolos = String(Simbolos).split(',');
					for(var a=0;a<ArrSimbolos.length;a++){
						CharCode = ArrSimbolos[a].charCodeAt(0);
						switch(CharCode){
							case 95: // _
							case 46: // .
							case 38: // &
							case 45: // -
							case 36: // $
							case 64: // @
							case 241: //ñ
							case 209: //Ñ
								break;
							default:
								CaracteresEncontrados += ArrSimbolos[a];
								break;
						}
					}
				}
			}
		}
	}

	return CaracteresEncontrados;
}

function ValidarCaracteresEnCadena(strValue){
	
	var Exp = /^[A-Za-z0-9]*$/;
	var Patt = /\W/g;
	var CaracteresEncontrados = "";
	var CharCode;
	
	if(strValue.length > 0){
		if(!Exp.test(strValue)){
			var Simbolos = strValue.match(Patt);
			var ArrSimbolos = String(Simbolos).split(',');
			for(var a=0;a<ArrSimbolos.length;a++){
				CharCode = ArrSimbolos[a].charCodeAt(0);
				switch(CharCode){
					case 95: // _
					case 46: // .
					case 38: // &
					case 45: // -
					case 36: // $
					case 64: // @
					case 241: //ñ
					case 209: //Ñ
						break;
					default:
						CaracteresEncontrados += ArrSimbolos[a];
						break;
				}
			}
		}
	}
	return CaracteresEncontrados;
}
