//
// +----------------------------------------------------------------------+
// | PHP2Go Web Development Framework                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2004 Marcos Pont                                  |
// +----------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or        |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This library is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// |                                                                      |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library; if not, write to the Free Software  |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA             |
// | 02111-1307  USA                                                      |
// |                                                                      |
// | Przerobka do formatu daty YYYY.mm.dd by Szumek (2005.02.22)          |
// +----------------------------------------------------------------------+
//
// $Header: /www/cvsroot/php2go/resources/javascript/masks/date.js,v 1.10 2004/09/24 15:58:09 mpont Exp $
// $Date: 2004/09/24 15:58:09 $
// $Revision: 1.10 $

//!-------------------------------------------------------
// @function	chkMaskDATE
// @desc		Aplica máscara de data automaticamente
// @param		field Field object		Campo de um formulário
// @param		event Event object		Evento do teclado
// @return		Boolean
// @note		Aplica máscara de data segundo o padrão europeu (YYYY.mm.dd)
//!-------------------------------------------------------
function chkMaskDATE(field,event) {
	var ns = ((document.layers || document.getElementById) && (!document.all));
	var ie = document.all;
	var keySet = '0123456789-';
	var actionSet = '0,8,13';
	var keyCode = (ie) ? window.event.keyCode : event.which;
	var key = String.fromCharCode(keyCode);
	if (keySet.indexOf(key) == -1 && actionSet.indexOf(keyCode) == -1) {
		return false;
	} else {
		if (field.value.length >= 10 && actionSet.indexOf(keyCode) == -1 ) { // && field.selectionEnd == 0) {
			return false;
		} else if (key == '-') {
			// Rezygnujemy chwilowo z moÅ¼liwoÅ›ci autouzupeÅ‚niania o zera (w tym formacie jest to bez sensu)
			// if (((field.value.length == 1) || (field.value.length == 4)) && (field.value.charAt(field.value.length-1) != '0')) {
			//	field.value = field.value.substr(0,field.value.length-1) + '0' + field.value.substr(field.value.length-1,1);
			//	return true;
			//} else 
			if ((field.value.length != 4) && (field.value.length != 7)) {
				return false;
			}
		} else if (actionSet.indexOf(keyCode) != -1) {
			return true;
		} else {
			if ((field.value.length == 4) || (field.value.length == 7)) {
				field.value = field.value + '-';
				return true;
			}
		}
	}
}

//!------------------------------------------------------------------
// @function	chkDATE
// @desc		Verifica se uma data está corretamente construída
// @param		field Field object	Campo de data a ser verificada
// @return		Boolean
// @note		Valida datas no formato europeu (YYYY.mm.dd) a partir de 1000.01.01
//!------------------------------------------------------------------
function chkDATE(field) {
	if (field.value.length == 0) {
		return true;
	}
	// var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	var objRegExp = /^\d{4}(\-|\/|\.)\d{2}\1\d{2}$/;
	if (!objRegExp.test(field.value)) {
		return false;
	}
	y = parseInt(field.value.substr(0, 4), 10);
	m = parseInt(field.value.substr(5, 2), 10);
	d = parseInt(field.value.substr(8, 2), 10);
	bin_m = (1 << (m-1));
	m31 = 0xAD5;
	if	((y < 1000) || (m < 1) || (m > 12) || (d < 1) || (d > 31) ||
		((d == 31 && ((bin_m & m31) == 0))) ||
		((d == 30 && m == 2)) || ((d == 29 && m == 2 && !isLeap(y)))) {
		return false;
	}
	return true;
}

//!----------------------------------------------------
// @function	isLeap
// @desc		Verifica se um ano é bissexto
// @param		year Integer	Ano a ser verificado
// @return		Boolean
//!----------------------------------------------------
function isLeap(year) {
	return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}

//!----------------------------------------------------
// @function	dateToDays
// @desc		Converte uma data em número de dias
// @param		dateValue String	Valor da data
// @return		Integer Número de dias ou zero se a data não estiver no formato DD/MM/YYYY
//!----------------------------------------------------
function dateToDays(dateValue) {
	var day, month, year, century; 
	// var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	var objRegExp = /^\d{4}(\-|\/|\.)\d{2}\1\d{2}$/;
	if (!objRegExp.test(dateValue)) {
		return 0;
	}
	year = parseInt(dateValue.substr(0, 4), 10);
	month = parseInt(dateValue.substr(5, 2), 10);
	day = parseInt(dateValue.substr(8, 2), 10);
	century = parseInt(String(year).substr(0, 2));
	year = String(year).substr(2, 2);
	if (month > 2) {
		month -= 3;
	} else {
		month += 9;
		if (year) {
			year--;
		} else {
			year = 99;
			century--;
		}
	}
	return (Math.floor((146097 * century) / 4) + Math.floor((1461 * year) / 4) + Math.floor((153 * month + 2) / 5) + day + 1721119);	
}