// JavaScript para validar los formularios  //


window.onload = function(event){
		
    //comprobar que existe el elemento
	if (document.getElementById('comoNosConocioEspecificar')){
		//ocultar comoNosConocioEspecificar
		 if (document.getElementById('comoNosConocio').value == 'Web' || document.getElementById('comoNosConocio').value == 'Others')
			document.getElementById('comoNosConocioEspecificar').style.visibility = 'visible';
		 else
			document.getElementById('comoNosConocioEspecificar').style.visibility = 'hidden';

		//si se selecciona web u others mostar el campo comoNosConocioEspecificar para que el usuario lo cumplimente
	    document.getElementById('comoNosConocio').onchange = function(event){
	        if (document.getElementById('comoNosConocio').value == 'Web' || document.getElementById('comoNosConocio').value == 'Others'){
				document.getElementById('comoNosConocioEspecificar').style.visibility = 'visible';
			}else{
				document.getElementById('comoNosConocioEspecificar').style.visibility = 'hidden';
			}
	    }
	}
}


var f = document.form1;

function requeridoTxt(txtAlert, elementoForm, txtCampo){
	if (elementoForm.value==""){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoCbo(txtAlert, elementoForm, txtCampo){
	if (elementoForm.selectedIndex==0){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoMail(txtAlert, elementoForm, txtCampo){
	if (!esMail(elementoForm.value)){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoFecha(txtAlert, elementoForm, txtCampo){
	if (!isDate(elementoForm.value)){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoEntero(txtAlert, elementoForm, txtCampo){
	if (!esEntero(elementoForm.value)){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoEnteroRango(txtAlert, txtY, txtParaElCampo, elementoForm, txtCampo, vMin, vMax){

	var valor=parseInt(elementoForm.value);

	if ((valor < vMin) || (valor > vMax)) {
		alert(txtAlert + " " + vMin + " " + txtY + " " + vMax + " " + txtParaElCampo + " \"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoReal(txtAlert, elementoForm, txtCampo){
	if (!esReal(elementoForm.value)){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoRadio(txtAlert, elementoForm, txtCampo){
	for (i = 0; i < elementoForm.length; i++){
		if (elementoForm[i].checked) {
			return true;
		}
	}
	alert(txtAlert + "\"" + txtCampo + "\"");
	elementoForm[0].focus();
	return false;
}

function requeridoNumMaxPalabras(maxPalabras, txtAlert01, txtAlert02, txtAlert03, txtAlert04, elementoForm, txtCampo){
	var numPalabras = elementoForm.value.split(" ");
	if ((numPalabras.length-1) > maxPalabras){
		alert(txtAlert01 + maxPalabras + txtAlert02 + txtAlert03 + "\"" + txtCampo + "\"" + txtAlert04 + (numPalabras.length) + txtAlert02);
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoNumMaxCaracteres(maxCaracteres, txtAlert01, txtAlert02, txtAlert03, txtAlert04, elementoForm, txtCampo){
	var numCaracteres = elementoForm.value.length;
	if (numCaracteres > maxCaracteres){
		alert(txtAlert01 + maxCaracteres + txtAlert02 + txtAlert03 + "\"" + txtCampo + "\"" + txtAlert04 + numCaracteres + txtAlert02);
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function requeridoLongCaracteresExacta(txtAlert, elementoForm, txtCampo, longCaracteres){
	if (elementoForm.value.length!=longCaracteres){
		alert(txtAlert + "\"" + txtCampo + "\"");
		elementoForm.focus();
		return false;
	}
	else
		return true;
}

function esMail(cadena){
	return cadena.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/);
} 

function esEntero(cadena){
	return cadena.match(/^\d+$/);
}

function esReal(cadena){
	cadena=cadena.replace(".",",");
	return cadena.match(/^\d$|^\d+\,?\d+$/);
}

var numdecimales=0;
function isANumber(cadena) {
	var j=0;
	numdecimales=0;
	indice=0;
	if (cadena.charAt(0) == ',')
  	return false;

	for(i = 0;i<cadena.length;i++){
		if (!isADigit(cadena.charAt(i)))
			return false;
	}

	for(i = 0;i<cadena.length;i++){
		if (cadena.charAt(i) == ','){
				j = i;
				i = cadena.length;
		}
	}

	if (j>0){
		for(i = j;i<cadena.length;i++)
			numdecimales = numdecimales + 1;
		if (numdecimales > 3){
			return false;
		}
	}
	return true;
}

function NumDecimales(cadena,separador){
  var numdecimales=0;
  var contardecimales=0;
  for(i=0;i<cadena.length;i++){
    if (contardecimales)
      numdecimales=numdecimales + 1;
    if (cadena.charAt(i) == separador)
      contardecimales = 1;
  }
  return eval(numdecimales);
}

function isADigit(caracter) {
	if (indice > 1){
		return false;
	}
	else if ((caracter != ',') && (caracter < '0' || caracter > '9' )) {
		return false;
	}
	else {
		if (caracter == ','){
  		if (indice == 1)
    		return false;
  		else
    		indice = indice + 1;
		}
		return true;
	}
}

/*****************************************************************
 * DHTML date validation script for dd/mm/yyyy. 
 *Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 *****************************************************************/
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

/* CHARACTER COUNT ///////////////////////////////////
This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Steve | http://jsmadeeasy.com/ */
function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function toCount(entrance,exit,text,characters) {
  var entranceObj=getObject(entrance);
  
  var exitObj=getObject(exit);
  var length=characters - entranceObj.value.length;
  if(length <= 0) {
	length=0;
	text='<span class="disable"> '+text+' </span>';
	entranceObj.value=entranceObj.value.substr(0,characters);
  }
  exitObj.innerHTML = text.replace("{CHAR}",length);
}

/* END ****************************************/



