/*============FUNÇÕES PHP CONVERTIDAS PARA JAVASCRIPT=======*/
function checkdate( month, day, year ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: checkdate(12, 31, 2000);
    // *     returns 1: true
    // *     example 2: checkdate(2, 29, 2001);
    // *     returns 2: false
    // *     example 3: checkdate(03, 31, 2008);
    // *     returns 3: true

    var myDate = new Date();
    myDate.setFullYear( year, (month - 1), day );

    return ( (myDate.getMonth()+1) == month );
}

function mktime() {
    // Get Unix timestamp for a date
    //
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_mktime/
    // +       version: 807.1807
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: baris ozdil
    // +      input by: gabriel paderni
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FGFEmperor
    // +      input by: Yannoo
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: jakes
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: mktime(14, 10, 2, 2, 1, 2008);
    // *     returns 1: 1201871402
    // *     example 2: mktime(0, 0, 0, 0, 1, 2008);
    // *     returns 2: 1196463600

    var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
    d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);

    var dateManip = {
        0: function(tt){ return d.setHours(tt); },
        1: function(tt){ return d.setMinutes(tt); },
        2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
        3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
        4: function(tt){ return d.setDate(tt+mb); },
        5: function(tt){ return d.setYear(tt+ma); }
    };

    for( i = 0; i < argc; i++ ){
        no = parseInt(argv[i]*1);
        if (isNaN(no)) {
            return false;
        } else {
            // arg is number, let's manipulate date object
            if(!dateManip[i](no)){
                // failed
                return false;
            }
        }
    }

    return Math.floor(d.getTime()/1000);
}
/*======FIM DAS FUNÇÕES PHP CONVERTIDAS PARA JAVASCRIPT=====*/

/*==================== VALIDAÇÃO ====================*/
function validarVoto(form) {
  //top.toggleForwardLink();

  if(formcheck(form)) {
  Spry.Utils.submitForm(form,updateResponseDiv,{method:'post', url:'../_actions/actionCadastrarVoto.php'});
    return;
  }

  //if(!formcheck(form, true, '../_actions/actionCadastrarVoto.php', document.name)) {
    //top.toggleForwardLink();
  //  return;
  //}
  //messagebox("Enviando...");
  //form.submit();
}

function validarVotacao(form) {

  if(formcheck(form) && verificarDatas()) {
    form.NOME_VOTACAO.value=escape(form.NOME_VOTACAO.value);
  form.DESCRICAO_VOTACAO.value=escape(form.DESCRICAO_VOTACAO.value);
  Spry.Utils.submitForm(form,updateResponseDiv,{method:'post', url:'../_view/showCriarVotacao.php'});
    return;
  }
}

function editarVotacao(form) {

  if(formcheck(form) && verificarDatas()) {
    form.NOME_VOTACAO.value=escape(form.NOME_VOTACAO.value);
  form.DESCRICAO_VOTACAO.value=escape(form.DESCRICAO_VOTACAO.value);
  Spry.Utils.submitForm(form,updateResponseDiv,{method:'post', url:'../_actions/actionEditarVotacao.php'});
    return;
  }
}

function updateResponseDiv(req) {
  Spry.Utils.setInnerHTML('conteudo', req.xhRequest.responseText);
}

function verificarDatas() {
  var timestampDataAtual=document.getElementById('TS_DTATUAL').value;

  var anoi=document.getElementById('anoInicio').value;
  var mesi=document.getElementById('mesInicio').value;
  var diai=document.getElementById('diaInicio').value;
  var horaTemp=document.getElementById('horaInicio').value;
  var horai=horaTemp.substr(0,2);
  var mini=horaTemp.substr(3,2);

  var anof=document.getElementById('anoFim').value;
  var mesf=document.getElementById('mesFim').value;
  var diaf=document.getElementById('diaFim').value;
  horaTemp=document.getElementById('horaFim').value;
  var horaf=horaTemp.substr(0,2);
  var minf=horaTemp.substr(3,2);

  var timestampDataInicio = mktime(horai,mini,0,mesi,diai,anoi);
  var timestampDataFim = mktime(horaf,minf,0,mesf,diaf,anof);

  if (!checkdate(mesi,diai,anoi)){
  	alert('Data do início não existe!');
  	return false;
  } else {
  	if (!checkdate(mesf,diaf,anof)){
		alert('Data do fim não existe!');
		return false;
	} else {
		if (timestampDataInicio < timestampDataAtual) {
			alert('Data do início não deve ser menor do que a data atual!');
			return false;
  		} else {
			if (timestampDataInicio >= timestampDataFim) {
				alert('Data do início deve ser menor do que a data do fim!');
				return false;
		  	} else {
				return true;
			}
		}
	}
  }

}

/*================== FIM VALIDAÇÃO ==================*/

function votacao() {
  central.zeraPagina(PG_ATIV_EXTRA);
  carregarDados();
}
