var ok = 1;

function validar_hora(obj){

	var patron = "%5E(%5B1-9%5D|1%5B0-9%5D|2%5B0-3%5D):%5B0-5%5D%5Cd$";

	var rex=new RegExp(unescape(patron));

	var t=eval(rex.test(obj.value));;



	if (obj.value != "")

	{	

		if (obj.value.length > 0 && !t)

		{

			ok = 0;

			alert(unescape("La hora '"+obj.value+"' tiene un formato incorrecto [HH:MI]"));

			obj.value = "";

			obj.focus();

		}

		else

		{

			ok = 1;

		}
	}

	else

	{

		ok = 1;

	}



	document.MM_returnValue=ok;

}

function validar_fecha(obj){

	var patron = "%5E(%5B1-9%5D%7C0%5B1-9%5D%7C1%5B0-9%5D%7C2%5B0-9%5D%7C3%5B0-1%5D)/(%5B1-9%5D%7C0%5B1-9%5D%7C1%5B0-2%5D)/(%5B0-9%5D%5B0-9%5D%5B0-9%5D%5B0-9%5D)$";

	var rex=new RegExp(unescape(patron));

	var t=eval(rex.test(obj.value));;



	if (obj.value != "")

	{	

		if (obj.value.length > 0 && !t)

		{

			ok = 0;

			alert(unescape("La fecha '"+obj.value+"' tiene un formato incorrecto [DD/MM/AAAA]"));

			obj.value = "";

			obj.focus();

		}

		else

		{

			ok = 1;

		}

	

		// Compruebo que la fecha es correcta

		if (ok == 1)

		{

			if (!fecha_ok (obj))

			{

				ok = 0;

				alert(unescape("La fecha '"+obj.value+"' es incorrecta"));

				obj.value = "";

				obj.focus();

			}

		}

	}

	else

	{

		ok = 1;

	}



	document.MM_returnValue=ok;

}



function bisiesto(anno) 

{

	if (anno % 100 == 0) 

	{

		if (anno % 400 == 0) 

		{ 

			return true; 

		}

	}

	else 

	{

		if ((anno % 4) == 0) 

		{ 

			return true; 

		}

	}

	return false;

}



function fecha_ok (obj)

{

	var fecha_char, pos1, pos2, dia, mes, anno;

	var fecha = new Date();



	fecha_char = obj.value;

	pos1 = fecha_char.indexOf('/', 0);

	dia = fecha_char.substring(0, pos1);

	if (dia.length == 1)

	{

		dia = "0"+dia;

	}

	pos2 = fecha_char.indexOf('/', pos1 + 1);

	mes = fecha_char.substring(pos1 + 1, pos2);

	if (mes.length == 1)

	{

		mes = "0"+mes;

	}

	anno = fecha_char.substring(pos2 + 1, fecha_char.length);

	// Compruebo que es correcta

	dia = parseInt(dia, 10);

	mes = parseInt(mes,10);    

	anno = parseInt(anno, 10);



	if ((mes == 1) || (mes == 3) || (mes == 5) || (mes == 7) || (mes == 8) || (mes == 10) || (mes == 12))

	{

		if (dia > 31)

		{

			return false;

		}

	}

	else if ((mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))

	{

		if (dia > 30)

		{

			return false;

		}

	}

	else

	{

		if (bisiesto(anno))

		{

			if (dia > 29)

			{

				return false;

			}

		}

		else

		{

			if (dia > 28)

			{

				return false;

			}

		}

	}

	return true;

}





// Funcion para validar un entero

function validar_entero(obj){

	var patron = "(%5E%5Cd%5Cd*$)";

	var rex=new RegExp(unescape(patron));

	var t=eval(rex.test(obj.value));



	if (obj.value != "")

	{	

		if (obj.value.length > 0 && !t)

		{

			ok = 0;

			alert(unescape("El número ''"+obj.value+"'' tiene un formato incorrecto [9999]"));

			obj.value = "";

			obj.focus();

		}

		else

		{

			ok = 1;

		}

	}

	else

	{

		ok = 1;

	}



	document.MM_returnValue=ok;

}



function replaceSubstring(inputString, fromString, toString) {

   // Goes through the inputString and replaces every occurrence of fromString with toString

   var temp = inputString;

   if (fromString == "") {

      return inputString;

   }

   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)

      while (temp.indexOf(fromString) != -1) {

         var toTheLeft = temp.substring(0, temp.indexOf(fromString));

         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);

         temp = toTheLeft + toString + toTheRight;

      }

   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop

      var midStrings = new Array("~", "`", "_", "^", "#");

      var midStringLen = 1;

      var midString = "";

      // Find a string that doesn't exist in the inputString to be used

      // as an "inbetween" string

      while (midString == "") {

         for (var i=0; i < midStrings.length; i++) {

            var tempMidString = "";

            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }

            if (fromString.indexOf(tempMidString) == -1) {

               midString = tempMidString;

               i = midStrings.length + 1;

            }

         }

      } // Keep on going until we build an "inbetween" string that doesn't exist

      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string

      while (temp.indexOf(fromString) != -1) {

         var toTheLeft = temp.substring(0, temp.indexOf(fromString));

         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);

         temp = toTheLeft + midString + toTheRight;

      }

      // Next, replace the "inbetween" string with the "toString"

      while (temp.indexOf(midString) != -1) {

         var toTheLeft = temp.substring(0, temp.indexOf(midString));

         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);

         temp = toTheLeft + toString + toTheRight;

      }

   } // Ends the check to see if the string being replaced is part of the replacement string or not

   return temp; // Send the updated string back to the user

} // Ends the "replaceSubstring" function