/**
 * ADJ Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://adj.com.br/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@adj.com.br so we can send you a copy immediately.
 *
 * @category
 * @package    jscript
 * @copyright  Copyright (c) 2007-2008 ADJ Tecnologia da Informação Ltda. (http://www.adj.com.br)
 * @license    http://adj.com.br/license/new-bsd     New BSD License
 * @version    $Id: date.js 6589 2007-10-05 15:09:28Z $
 */


	function toDate(thisField) {
	/* Submete a data recebida no formato DDMMYYYY à validação
	* do método checkDate. Envia mensagem de erro, caso inválida, 
	* ou converte para DD/MM/YYYY, caso seja válida.
	*/
		var a = thisField.value;
		if (a != '') {
			if (checkDate(a)) {
				if (a.length == 8) thisField.value = convDate(a);
				else return thisField.value = a;
			} else displayMsg('displayNull', 'Data inválida!', false);
		}
	}
	
	function toSmallDate(thisField) {
	/* Submete a data recebida no formato DDMM à validação
	* do método checkDate. Envia mensagem de erro, caso inválida, 
	* ou converte para DD/MM, caso seja válida.
	*/
		var a = thisField.value;
		if (a != '') {
			if (a.length == 4) a = a + '2000';
			else if (a.length == 5) a = a + '/2000';
			if (checkDate(a)) {
				if (a.length == 8) thisField.value = convDate(a).substring(0,5);
				else return thisField.value = a.substring(0,5);
			} else displayMsg('displayNull', 'Data inválida!', false);
		}
	}
	function dataTab(original, destination, qtdBarras) {
	/* 
	* Assistente de auto-tabulação para campos data.
	* Ao atingir o 8º caracter, move o cursor para o próximo campo do formulário.
	*/
		if (original.getAttribute && original.value.length == original.getAttribute("maxlength") - qtdBarras) {
			destination.focus();
		}
	}
	function autoTab(original, destination) {
		if (original.getAttribute && original.value.length == original.getAttribute("maxlength")) {
			destination.focus();
		}
	}
	function cleanSlash(element) {
		var a = element.value;
		while (a.lastIndexOf('/') != -1) {
			a = a.replace('/', '');
		}
		element.value = a;
	}
	function doDateCheck(from, to) {
	// Compara duas datas em que a segunda deve ser posterior a primeira
		if (Date.parse(from.value) > Date.parse(to.value)) return false;
		else return true;
	}
	
	// Não testada
	function isFuture(a) {
		var now = new Date(); // Data atual
		var d = new Date(toValidDate(a));
		if (d.getTime() > now.getTime()) return true
		else return false
	}
	
	// Retorna a idade em anos da data a
	function getAge(a) {
		var now = new Date(); // Data atual
		var d = new Date(toValidDate(a));
		var age = now.getFullYear() - d.getFullYear();
		d.setFullYear(now.getFullYear());
		if (d > now) {
			age--;
		}
		return age;
	}
	
	/* Converte data recebida no formato DDMMYYYY ou DD/MM/YYYY para vetor 
	* de 3 posições de inteiros, caso os campos sejam numéricos, ou falso,
	* caso contrário.*/
	function formatDate(t) {
		v = new Array(3);
		if (t.length == 8) {
			d = parseInt(t.substring(0,2), 10);
			m = parseInt(t.substring(2,4), 10);
			y = parseInt(t.substring(4,8), 10);
			if (! isNaN(d)) v[0] = d; else return false;
			if (! isNaN(m)) v[1] = m; else return false;
			if (! isNaN(y)) v[2] = y; else return false;
			return v;
		} else if (t.length == 10 && t.substring(2,3) == "/" && t.substring(5,6) == "/") {
			d = parseInt(t.substring(0,2), 10);
			m = parseInt(t.substring(3,5), 10);
			y = parseInt(t.substring(6,10), 10);
			if (! isNaN(d)) v[0] = d; else return false;
			if (! isNaN(m)) v[1] = m; else return false;
			if (! isNaN(y)) v[2] = y; else return false;
			return v;
		} else return false;
	}
	
	/* Valida data recebida no formato DDMMYYYY ou DD/MM/YYYY */
	function checkDate(date) {
		var a = formatDate(date);
		if (a) {
			if (a[2]<1900) return false;
			if (a[1]<0 || a[1]>12) return false;
			if (a[1]==1 || a[1]==3 || a[1]==5 || a[1]==7 || a[1]==8 || a[1]==10 || a[1]==12) {
				if (a[0]>31 || a[0]<0) return false;
			} else if (a[1]==4 || a[1]==6 || a[1]==9 || a[1]==11) {
				if (a[0]>30 || a[0]<0) return false;
			} else if (a[2]%4==0) {
				if (a[0]>29 || a[0]<0) return false;
			} else if (a[0]>28 || a[0]<0) return false;
		} else return false;
		return true;
	}
	
	// Converte a data recebida no formato DDMMYYYY para DD/MM/YYYY
	function convDate(d) {
		return d.substring(0,2)+'/'+d.substring(2,4)+'/'+ d.substring(4,8);
	}
	
	// Converte a data recebida no formato DD/MM/YYYY para YYYY/MM/DD
	function toValidDate(d) {
		return d.substring(6,10)+'/'+d.substring(3,5)+'/'+ d.substring(0,2);
	}
