/*
Dependencias:
	jquery-1.2.6.min.js
	effects.core.js
	effects.highlight.js
*/
var date_regex = /(\d{1,2})\/(\d{1,2})\/(\d{4})/
function clearForm(form){
		$(':input',$(form)).each(function(){
			var type = this.type;
			var tag = this.tagName.toLowerCase();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = "";
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
}

function dateCompare(date1,date2){ 
	//formato dd/mm/aaaa
	var dt1 = date_regex.exec(date1);
	var dt2 = date_regex.exec(date2);
	if ((dt1 == null) || (dt2 == null)) return 2;
	else{
		dt1 = new Date( parseInt(dt1[3]), parseInt(dt1[2], 10) - 1, parseInt(dt1[1], 10) );
		dt2 = new Date( parseInt(dt2[3]), parseInt(dt2[2], 10) - 1, parseInt(dt2[1], 10) );

		if (dt1 < dt2) return -1;
		else if (dt1 == dt2) return 0;
		else if (dt1 > dt2) return 1;
	}
}

function nextDay(fecha){
	var dt = date_regex.exec(fecha);
	if (dt==null) return null;
	else{
		dt = new Date( parseInt(dt[3]), parseInt(dt[2], 10) - 1, parseInt(dt[1], 10) );
	}
	dt.setDate(dt.getDate()+1);
	var dia = dt.getDate();
	var mes = dt.getMonth();
	mes++;
	var any = dt.getFullYear();
	if (dia.toString().length == 1) dia = '0'+dia;
	if (mes.toString().length == 1) mes = '0'+mes;
	return dia + "/" + mes + "/" + any;
}

function prevDay(fecha){
	var dt = date_regex.exec(fecha);
	if (dt==null) return null;
	else{
		dt = new Date( parseInt(dt[3]), parseInt(dt[2], 10) - 1, parseInt(dt[1], 10) );
	}
	dt.setDate(dt.getDate()-1);
	var dia = dt.getDate();
	var mes = dt.getMonth();
	mes++;
	var any = dt.getFullYear();
	if (dia.toString().length == 1) dia = '0'+dia;
	if (mes.toString().length == 1) mes = '0'+mes;
	return dia + "/" + mes + "/" + any;
}

function validar_fechas(campo1, campo2, numcampo){
	numcampo = numcampo || 1; //campo a cambiar
	if (dateCompare($(campo1).val(),$(campo2).val())!=-1){
		if (numcampo == 1){
			$(campo1).val(prevDay($(campo2).val()));
		}else{
			$(campo2).val(nextDay($(campo1).val()));
		}
	}
}

function isEmail(s){
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return false;
	else{
		if (filter.test(s))
			return true;
		else
			return false;
	}
}

function pulsateBg(elem, color1, color2){
		color1 = color1 || '#ff3333';
		color2 = color2 || '#fff'; //color normal
		$(elem)
		.animate({backgroundColor:color1},'fast')
		.animate({backgroundColor:color2},'fast')
		.animate({backgroundColor:color1},'fast')
		.animate({backgroundColor:color2},'fast')
		.animate({backgroundColor:color1},'fast')
		.animate({backgroundColor:color2},'fast')
		.effect("highlight", { color: color1 }, 2000);
}
	
function pulsateBorder(elem, borde1, borde2){
		borde1 = borde1 || '1px #ff3333 solid';
		borde2 = borde2 || '1px #bbdee8 solid'; //borde normal
		$(elem)
		.animate({border:borde1},'fast')
		.animate({border:borde2},'fast')
		.animate({border:borde1},'fast')
		.animate({border:borde2},'fast')
		.animate({border:borde1},'fast')
		.animate({border:borde2},'fast');
}
