<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

gbFocus = true;
gcToggle = "yellow";
bgColorSel = "yellow";
bgColorDia = "#E5E5E5";
gcTemp = null;
var arrN = Array();
var arrData = Array();
var arrDataSel = Array();

<!-- Begin
function maxDays(mm, yyyy){
var mDay;
	if((mm == 3) || (mm == 5) || (mm == 8) || (mm == 10)){
		mDay = 30;
  	}
  	else{
  		mDay = 31
  		if(mm == 1){
   			if (yyyy/4 - parseInt(yyyy/4) != 0){
   				mDay = 28
   			}
		   	else{
   				mDay = 29
  			}
		}
  }
return mDay;
}


function writeCalendar(){
if (getObjectFromID("idTipoMidia").value == 0) {
  return
}

var now = new Date
var dd = now.getDate()
var mm = now.getMonth()
var dow = now.getDay()
var yyyy = now.getFullYear()
var arrM = new Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")
var arrY = new Array()
	for (ii=0;ii<=4;ii++){
		arrY[ii] = yyyy + ii
	}
var arrD = new Array("Dom","Seg","Ter","Qua","Qui","Sex","Sab")

var text = ""
text = "<form name=calForm>"
text += "<table border=0 id='outerTable' bgColor='#6699CC' cellspacing=0 cellpadding=0>"
text += "<tr><td>"
text += "<table width=100% border=0 cellspacing=0 bgColor='#DDDDDD' cellpadding=0 style='border-bottom:6px solid #DDDDDD;'><tr>"
text += "<td align=left>"
text += "<select name=selMonth onChange='changeCal()' style='margin-left:3px; margin-top:3px; font-size:10pt;'>"
	for (ii=0;ii<=11;ii++){
		if (ii==mm){
			text += "<option value= " + ii + " Selected>" + arrM[ii] + "</option>"
		}
		else{
			text += "<option value= " + ii + ">" + arrM[ii] + "</option>"
		}
	}
text += "</select>"
text += "</td>"
text += "<td align=right>"
text += "<select name=selYear onchange='changeCal()' style='margin-right:3px; margin-top:3px; font-size:10pt;'>"
	for (ii=0;ii<=4;ii++){
		if (arrY[ii]==yyyy){
			text += "<option value= " + arrY[ii] + " Selected>" + arrY[ii] + "</option>"
		}
		else{
			text += "<option value= " + arrY[ii] + ">" + arrY[ii] + "</option>"
		}
	}
text += "</select>"
text += "</td>"
text += "</tr></table>"
text += "</td></tr>"
text += "<tr><td>"
text += "<table border=0 cellpadding=2 cellspacing=1 style='border:1px solid white'>"
text += "<tr>"
	for (ii=0;ii<=6;ii++){
		text += "<td align=center class='cellLabel'><div class='label'>" + arrD[ii] + "</div></td>"
	}
text += "</tr>"
aDia = 0
	for (kk=0;kk<=5;kk++){
		text += "<tr>"
		for (ii=0;ii<=6;ii++){
			text += "<td align=center class='cellDia'><div id=sp" + aDia + " onclick='changeBg(\"" + aDia + "\")' onmouseover='fMouseOver(this)' onmouseout='fMouseOut(this)'>1</div></td>"
			aDia += 1
		}
		text += "</tr>"
	}
text += "</table>"
text += "</td></tr>"
text += "</table>"
text += "</form>"
getObjectFromID("idCalendarioVigencia").innerHTML = text;
//document.write(text)
changeCal()
}

function changeCal(){
var now = new Date
var dd = now.getDate()
var mm = now.getMonth()
var dow = now.getDay()
var yyyy = now.getFullYear()

var currM = parseInt(document.calForm.selMonth.value)
var currY = parseInt(document.calForm.selYear.value)
//currM = 9
//currY = 2006
var prevY
var proxY
var prevM
if (currM!=0){
	prevM = currM - 1
	prevY = currY
}
else{
	prevM = 11
	prevY = currY - 1
}

var proxM
if (currM!=11){
	proxM = currM + 1
	proxY = currY
}
else{
	proxM = 0
	proxY = currY + 1
}

var mmyyyy = new Date()
mmyyyy.setFullYear(currY)
mmyyyy.setMonth(currM)
mmyyyy.setDate(1)
var day1 = mmyyyy.getDay()
	if (day1 == 0){
		day1 = 7
	}

arrN = new Array(41);
arrData = new Array(41);

var aDia
	for (ii=0;ii<day1;ii++){
		arrN[ii] = maxDays((prevM),currY) - day1 + ii + 1;
		arrData[ii] = prevY + '-' + (prevM+1) + '-' + arrN[ii];
	}
	aDia = 1
	for (ii=day1;ii<=day1+maxDays(currM,currY)-1;ii++){
		arrN[ii] = aDia
		arrData[ii] = currY + '-' + (currM+1) + '-' + arrN[ii];
		aDia += 1
	}
	aDia = 1
	for (ii=day1+maxDays(currM,currY);ii<=41;ii++){
		arrN[ii] = aDia
		arrData[ii] = proxY + '-' + (proxM+1) + '-' + arrN[ii];
		aDia += 1

	}
	for (ii=0;ii<=41;ii++){
  	strData = formataData(arrData[ii]);
    intPos = Pos(strData,arrDataSel);
    if (intPos > -1) {
  		document.getElementById("sp"+ii).style.backgroundColor = bgColorSel;
    }
    else {
  		document.getElementById("sp"+ii).style.backgroundColor = bgColorDia;
    }
	}
  var dCount = 0
	for (ii=0;ii<=41;ii++){
		if (((ii<7)&&(arrN[ii]>20))||((ii>27)&&(arrN[ii]<20))){
			document.getElementById("sp"+ii).innerHTML = arrN[ii]
			document.getElementById("sp"+ii).className = "c3"
		}
		else{
			document.getElementById("sp"+ii).innerHTML = arrN[ii]
			if ((dCount==0)||(dCount==6)){
				document.getElementById("sp"+ii).className = "c2"
			}
			else{
				document.getElementById("sp"+ii).className = "c1"
			}
			if ((arrN[ii]==dd)&&(mm==currM)&&(yyyy==currY)){
      	strData = formataData(arrData[ii]);
        intPos = Pos(strData,arrDataSel);
        if (intPos > -1) {
      		document.getElementById("sp"+ii).style.backgroundColor = bgColorSel;
        }
        else {
  				document.getElementById("sp"+ii).style.backgroundColor="#90EE90"
  			}
			}
		}
  	dCount += 1
		if (dCount>6){
			dCount=0
		}
	}
	atualizaLista();
}

function fMouseOver(t) {
	if (!gbFocus) return;

	gcTemp=t.style.backgroundColor;
	t.style.backgroundColor=gcToggle;
}

function fMouseOut(t) {
	if (!gbFocus||gcTemp==null) return;
	t.style.backgroundColor=gcTemp?gcTemp:"transparent";

}

function changeBg(id){
  var aDatasBloq = getDatasBloq();

  strData = formataData(arrData[id]);

  var nomeObj = 'sp'+id;
  var Agora = new Date();
  var numMilSel = getMilissegundos(strData);

  var numMilSelAtual = Date.UTC(Agora.getFullYear(),Agora.getMonth(),Agora.getDate());
  if (numMilSel < numMilSelAtual) {
    alert('Data não disponível para publicação!')
    return;
  }
  //Verificando se a data está bloqueada
  for (indice = 0; indice < aDatasBloq.length; indice++) {
    //alert(trim(aDatasBloq[indice]) == trim(strData));
    if (trim(aDatasBloq[indice]) == trim(strData)) {
      alert('Data não disponível para publicação!')
      return;
    }
  }

	if (gcTemp != bgColorSel){
		document.getElementById(nomeObj).style.backgroundColor = bgColorSel;
		gcTemp = bgColorSel;
    selecionaData(strData);
	}
	else{
		document.getElementById(nomeObj).style.backgroundColor = bgColorDia;
		gcTemp = bgColorDia;
    removeData(strData);
	}
}

function selecionaData(strData) {
  intPos = Pos(strData,arrDataSel);
  if (intPos == -1) {
    arrDataSel.push(strData);
  }
  if (getObjectFromID('idTipoMidia').value == 1) {
    atualizaLista();
  }
}

function removeData(strData) {
  intPos = Pos(strData,arrDataSel);
  if (intPos > -1) {
    arrDataSel.splice(intPos,1);
  }
  atualizaLista();
}

function Pos(strElemento,aDados) {
  var intPos = -1;
  for (intPosArray in aDados) {
    if (aDados[intPosArray] == strElemento) {
      intPos = intPosArray;
      break;
    }
  }
  return intPos;
}

function atualizaLista() {
  arrDataSel.sort();
  dataSel = getObjectFromID("datasSelecionadas");
  var primeiraData = "";
  var ultimaData = "";
//  alert(dataSel);

  for (i = dataSel.options.length; i >= 0; i--) {
    dataSel.remove(i);
  }

  for (intPos = 0; intPos < arrDataSel.length; intPos++) {
    aData = arrDataSel[intPos].split('-');
    aNovaData = Array(3);
    aNovaData[0] = aData[2];
    aNovaData[1] = aData[1];
    aNovaData[2] = aData[0];
    dataSel.options[dataSel.length] = new Option(aNovaData.join('/'),dataSel.length);
    ultimaData = aNovaData.join('/');
    if (intPos == 0) {
      primeiraData = aNovaData.join('/');
    }
  }
  calculaValorAnuncioVigencia();

  if (getObjectFromID('divPeriodoFiguracao') != undefined) {
    if (primeiraData != "") {
      getObjectFromID('divPeriodoFiguracao').innerHTML = "Seu anúncio irá figurar do dia <br/><b>"+primeiraData+" ao dia "+ultimaData+"</b>";
    }
  }

}

function formatNumber(Numero,intTamanho) {
  var retorno = new String();
  retorno = Numero;
  if ((retorno.length) < intTamanho) {
    retorno = '0' + formatNumber(retorno,intTamanho-1);
  }
  return retorno;
}

function formataData(strData) {
  aData = strData.split('-');
  aData[0] = formatNumber(aData[0],4);
  aData[1] = formatNumber(aData[1],2);
  aData[2] = formatNumber(aData[2],2);
  return(aData.join('-'));
}

/**
 *
 * @access public
 * @return void
 **/
function getMilissegundos(strData){
  aData = strData.split('-');
  return Date.UTC(aData[0],(aData[1]-1),aData[2]);
}

//  End -->
