
/**
 * 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;
}

//  check for valid numeric strings	
function isNumeric(strString)
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	
	return blnResult;
}


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("El formato de fecha debe ser: dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Ingrese un mes válido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Ingrese un día válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Por favor ingrese un año de 4 dígitos válido entre "+minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Ingrese una fecha válida")
		return false
	}
return true
}


function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function altausuario(form)
{
	var msg = "";

	if (form.nombre.value == "") {
		msg += "Nombre\n";
		form.nombre.focus();
	}
	if (form.apellido.value == "") {
		msg += "Apellido\n";
		form.apellido.focus();
	}
	if (form.email.value == "") {
		msg += "E-mail\n";
		form.email.focus();
	}
	if (form.password.value == "") {
		msg += "Password\n";
		form.password.focus();
	}
	if (form.password2.value == "") {
		msg += "Repetir Password\n";
		form.password2.focus();
	}
	if (form.ciudad.value == "") {
		msg += "Ciudad\n";
		form.ciudad.focus();
	}
	if (form.provincia.value == "") {
		msg += "Provincia\n";
		form.provincia.focus();
	}
	if (form.pais.value == "") {
		msg += "Pais\n";
		form.pais.focus();
	}

	if (msg != "") {
		alert("El formulario tiene los siguientes campos sin ingresar:\n" + msg);
		return;
	}
	
	if (isEmail(form.email.value) == false) {
		alert("El e-mail ingresado es inválido");
		form.email.focus();
		return;
	}
		
	form.submit();
}

function invitacionusuario(form)
{
	var msg = "";

	if (form.email.value == "") {
		msg += "E-mail\n";
		form.email.focus();
	}
	if (form.mensaje.value == "") {
		msg += "Mensaje\n";
		form.mensaje.focus();
	}

	if (msg != "") {
		alert("El formulario tiene los siguientes campos sin ingresar:\n" + msg);
		return;
	}
	
	if (isEmail(form.email.value) == false) {
		alert("El e-mail ingresado es inválido");
		form.email.focus();
		return;
	}
		
	form.submit();
}

function loginusuario(form) {
	
	var msg = "";
	if (form.email.value == "") {
		msg += "E-mail\n";
		form.email.focus();
	}
	if (form.password.value == "") {
		msg += "Password\n";
		form.password.focus();
	}

	if (msg != "") {
		alert("Debe ingresar todos los datos para loguearse, actualmente los campos sin ingresar son:\n" + msg);
		return;
	}
	
	if (isEmail(form.email.value) == false) {
		alert("El e-mail ingresado es inválido");
		form.email.focus();
		return;
	}
		
	form.submit();
}

function contacto(form)
{
	var msg = "";
	if (form.nombre.value == "") {
		msg += "Nombre\n";
		form.nombre.focus();
	}
	if (form.apellido.value == "") {
		msg += "Apellido\n";
		form.apellido.focus();
	}
	if (form.email.value == "") {
		msg += "E-mail\n";
		form.email.focus();
	}
	if (form.asunto.value == "") {
		msg += "Asunto\n";
		form.asunto.focus();
	}
	if (form.mensaje.value == "") {
		msg += "Mensaje\n";
		form.mensaje.focus();
	}

	if (msg != "") {
		alert("Debe ingresar todos los datos obligatorios para enviar un mensaje, actualmente los campos sin ingresar son:\n" + msg);
		return;
	}
	
	if (isEmail(form.email.value) == false) {
		alert("El e-mail ingresado es inválido");
		form.email.focus();
		return;
	}
		
	form.submit();
}

function altanota(form)
{
	var msg = "";

	if (form.seccion.value == "") {
		msg += "Sección\n";
		form.seccion.focus();
	}
	if (form.copete.value == "") {
		msg += "Volanta\n";
		form.copete.focus();
	}
	if (form.titulo.value == "") {
		msg += "Título\n";
		form.titulo.focus();
	}
	if (form.nota.value == "") {
		msg += "Nota Corta\n";
		form.nota.focus();
	}
	if (form.nota_ext.value == "") {
		msg += "Nota extendida\n";
		form.nota_ext.focus();
	}
	if (form.texto_foto.value == "") {
		msg += "Texto Fotos\n";
		form.texto_foto.focus();
	}

	if (msg != "") {
		alert("El formulario tiene los siguientes campos sin ingresar:\n" + msg);
		return;
	}

	
	if (form.pos_pagpri.value == 0)
	{
		alert("Debe seleccionar una ubicación en la portada principal.");
		return;
	}
	if (form.pos_pagsec.value == 0)
	{
		alert("Debe seleccionar una ubicación en la portada de sección.");
		return;
	}
	if (form.cantfotos.value == 1 && form.foto1.value == "")
	{
		alert("Si decidió ingresar una foto, debe seleccionar alguna foto.");
		return;
	}
	if (form.cantfotos.value == 2 && (form.foto1.value == "" || form.foto2.value == ""))
	{
		alert("Si decidió ingresar dos fotos, debe seleccionar dos fotos para subir.");
		return;
	}
 	if (form.cantfotos.value == 3 && (form.foto1.value == "" || form.foto2.value == "" || form.foto3.value == ""))
	{
		alert("Si decidió ingresar tres fotos, debe seleccionar tres fotos para subir.");
		return;
	}

	if (form.cantfotos.value == 0 && (form.foto1.value != "" || form.foto2.value != "" || form.foto3.value != ""))
	{
		alert("Seleccionó archivos de fotos para subir pero la cantidad de fotos es 0.");
		return;
	}

	form.submit();
}

function editnota(form)
{
	var msg = "";

	if (form.seccion.value == "") {
		msg += "Sección\n";
		form.seccion.focus();
	}
	if (form.copete.value == "") {
		msg += "Volanta\n";
		form.copete.focus();
	}
	if (form.titulo.value == "") {
		msg += "Título\n";
		form.titulo.focus();
	}
	if (form.nota.value == "") {
		msg += "Nota Corta\n";
		form.nota.focus();
	}
	if (form.nota_ext.value == "") {
		msg += "Nota extendida\n";
		form.nota_ext.focus();
	}
	if (form.texto_foto.value == "") {
		msg += "Texto Fotos\n";
		form.texto_foto.focus();
	}

	if (msg != "") {
		alert("El formulario tiene los siguientes campos sin ingresar:\n" + msg);
		return;
	}


	if (form.cantfotos.value == 1 && form.foto1.value == "")
	{
		alert("Si decidió ingresar una foto, debe seleccionar alguna foto.");
		return;
	}
	if (form.cantfotos.value == 2 && (form.foto1.value == "" || form.foto2.value == ""))
	{
		alert("Si decidió ingresar dos fotos, debe seleccionar dos fotos para subir.");
		return;
	}
 	if (form.cantfotos.value == 3 && (form.foto1.value == "" || form.foto2.value == "" || form.foto3.value == ""))
	{
		alert("Si decidió ingresar tres fotos, debe seleccionar tres fotos para subir.");
		return;
	}

	if (form.cantfotos.value == 0 && (form.foto1.value != "" || form.foto2.value != "" || form.foto3.value != ""))
	{
		alert("Seleccionó archivos de fotos para subir pero la cantidad de fotos es 0.");
		return;
	}

	form.submit();
}

function checkcomment(form)
{
	var msg = "";

	if (form.texto.value == "") {
		msg += "Sección\n";
		form.texto.focus();
	}

	if (msg != "") {
		alert("Debe escribir algo en el comentario para poder publicarlo");
		return;
	}


	form.submit();
}

function checkabm(form, fields)
{
	var newval = "";
	var arr_campos = fields.split("::");

	for (var i = 0; i < arr_campos.length; i++)
	{
		var arr_separate = arr_campos[i].split("|");

		var campo = arr_separate[0];
		var fantasia = arr_separate[1];
		var min_largo = arr_separate[2];
		var max_largo = arr_separate[3];

		if (document.getElementById(campo).value.length < min_largo)
		{
			if (min_largo > 1)
				append = "es";
			else
				append = "";

			alert("El campo " + fantasia + " esta vacío o no cumple los requisitos. \n(Largo mínimo " + min_largo + " caracter" + append + ")");

			return;
		}

		if (document.getElementById(campo).value.length > max_largo)
		{
			if (max_largo > 1)
				append = "es";
			else
				append = "";

			alert("El campo " + fantasia + " es demasiado largo. \n(Largo máximo " + max_largo + " caracter" + append + ")");

			return;
		}

		if (document.getElementById(campo).value < 0)
		{
			alert("El campo " + fantasia + " posee un valor inválido.");

			return;
		}

	}

	form.submit();
}