function getID(id){ return document.getElementById(id) }
function getTagName(tag){ return document.getElementsByTagName(tag) }

//var prod = ['Terra Mail Gigante','Sonora','Wi-Fi','ToonTown','VOIP','Antivírus 2008','Firewall','Controle de Privacidade','PC Seguro 2008','Segurança Total'];
var prod = new Array(12);
prod['PCTR00045285'] = 'Terra Mail Gigante<a href="#" onclick="return excluir_plano(this,\'PCTR00045285\')"><span class="excluir">Excluir</span></a>';
prod['PCTR00046129'] = 'Sonora<a href="#" onclick="return excluir_plano(this,\'PCTR00046129\')"><span class="excluir">Excluir</span>';
prod['PCTR00050329'] = 'Wi-Fi<a href="#" onclick="return excluir_plano(this,\'PCTR00050329\')"><span class="excluir">Excluir</span>';
prod['PCTR00042321'] = 'ToonTown<a href="#" onclick="return excluir_plano(this,\'PCTR00042321\')"><span class="excluir">Excluir</span>';
prod['PCTR00045710'] = 'VOIP Básico<a href="#" onclick="return excluir_plano(this,\'PCTR00045710\')"><span class="excluir">Excluir</span>';
prod['PCTR00045709'] = 'VOIP Completo<a href="#" onclick="return excluir_plano(this,\'PCTR00045709\')"><span class="excluir">Excluir</span>';
prod['PCTR00047480'] = 'VOIP Família<a href="#" onclick="return excluir_plano(this,\'PCTR00047480\')"><span class="excluir">Excluir</span>';
prod['PCTR00047482'] = 'VOIP Negócio<a href="#" onclick="return excluir_plano(this,\'PCTR00047482\')"><span class="excluir">Excluir</span>';
prod['PCTR00046253'] = 'Antivírus 2008<a href="#" onclick="return excluir_plano(this,\'PCTR00046253\')"><span class="excluir">Excluir</span>';
prod['PCTR00046257'] = 'Firewall<a href="#" onclick="return excluir_plano(this,\'PCTR00046257\')"><span class="excluir">Excluir</span>';
prod['PCTR00046261'] = 'Controle de Privacidade<a href="#" onclick="return excluir_plano(this,\'PCTR00046261\')"><span class="excluir">Excluir</span>';
prod['PCTR00046265'] = 'PC Seguro 2008<a href="#" onclick="return excluir_plano(this,\'PCTR00046265\')"><span class="excluir">Excluir</span>';
prod['PCTR00046271'] = 'Segurança Total<a href="#" onclick="return excluir_plano(this,\'PCTR00046271\')"><span class="excluir">Excluir</span>';
prod['PCTR00048043'] = 'Banda Larga Segurança<a href="#" onclick="return excluir_plano(this,\'PCTR00048043\')"><span class="excluir">Excluir</span>';
prod['PCTR00048341'] = 'Banda Larga Básico<a href="#" onclick="return excluir_plano(this,\'PCTR00048341\')"><span class="excluir">Excluir</span>';
prod['PCTR00050354'] = 'Sonora Clube<a href="#" onclick="return excluir_plano(this,\'PCTR00050354\')"><span class="excluir">Excluir</span>';
prod['PCTR00050352'] = 'Sonora Clube PC<a href="#" onclick="return excluir_plano(this,\'PCTR00050352\')"><span class="excluir">Excluir</span>';
prod['PCTR00046099'] = 'Sonora Plus<a href="#" onclick="return excluir_plano(this,\'PCTR00046099\')"><span class="excluir">Excluir</span>';
prod['PCTR00048993'] = 'Terra BL Suporte Online<a href="#" onclick="return excluir_plano(this,\'PCTR00048993\')"><span class="excluir">Excluir</span>';

//array Global que vai guardar a lista de produtos a serem enviados 
var prod_adicionado = new Array();

function viewProd(aba,id){
    var arr = getID('cnt_load').getElementsByTagName('div');

    for (i=0; i<arr.length; i++){
        if (arr[i].className == 'bg_box_top'){
            arr[i].style.display = 'none';
        }
    }
    getID(id).style.display = 'block';

	var links = document.getElementsByTagName('a');
	for (i=0; i<links.length; i++){
        if (links[i].className == 'selected'){
            links[i].className = '';
        }
    }
	if(aba.id == "antivirus_link" ){
		getID("antivirus_link").className = 'selected';
		getID("antivirus_link").blur();
	}else if(aba == "firewall_link" ){
		getID("firewall_link").className = 'selected';
		getID("firewall_link").blur();	
	}else if(aba == "controle_link" ){
		getID("controle_link").className = 'selected';
		getID("controle_link").blur();	
	}else{
		aba.className = 'selected';
		aba.blur();
	}
	return false;
}

function addProduto(id){
	var el = getID('collector');
	var li = document.createElement('li');
	if (typeof prod_adicionado[id] != 'undefined') {
    alert("Você já escolheu esse produto!");
    return false;
	} else if (!permitir_produtos(id,prod_adicionado)) {
    return false;
  }
	//coloca numa input hidden os valor selecionados
  prod_adicionado[id] = id;
  li.innerHTML = prod[id];
	el.appendChild(li);
  return false;
}

//funcao que gera a lista de produtos a partir da valor de um array separado pelo selecionador desejado
function build_produtos(arr,usrComma){
  var str = new String('');
  var comma=new String('');
  //nao usamos join so porque suspeitamos que nao ira funcionar, pois nao tem o teste de undefined
  for (el in arr){
   if (typeof arr[el] != 'undefined'){
     str+=comma+arr[el];
     comma = usrComma;
   }
  }
  return str;
}

//funcao que verifica manualmente se o conjunto de pacotes pode ser comprado
function permitir_produtos(cd,arr) {

  if (cd=='PCTR00050354') {
    if (arr['PCTR00050352']=='PCTR00050352') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
    if (arr['PCTR00046099']=='PCTR00046099') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
  }
  if (cd=='PCTR00050352') {
    if (arr['PCTR00050354']=='PCTR00050354') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
    if (arr['PCTR00046099']=='PCTR00046099') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
  }
  if (cd=='PCTR00046099') {
    if (arr['PCTR00050354']=='PCTR00050354') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
    if (arr['PCTR00050352']=='PCTR00050352') {
      alert('Você não pode comprar dois pacotes Sonora.');
      return false;
    }
  }

  //PC Seguro   Contem : Antivirus e Firewall
  if (cd=='PCTR00046265') {
    // antivirus = PCTR00046253
    if (arr['PCTR00046253']=='PCTR00046253') {
      alert('Para comprar PC Seguro voce precisa retirar o pacote de Antivirus.');
      return false;
    }
    //firewall = PCTR00046257
    if (arr['PCTR00046257']=='PCTR00046257') {
      alert('Para comprar PC Seguro voce precisa retirar o pacote de Firewall.');
      return false;
    }
    //seg total
    if (arr['PCTR00046271']=='PCTR00046271') {
      alert('Os serviços do PC Seguro já estão inclusos no pacote Segurança Total.');
      return false;
    }
    //BL ST
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Banda Larga Seguranca Total.');
      return false;
    }
  }
  
  //Seg Total   av fw controle de priv
  if (cd=='PCTR00046271') {
    // antivirus = PCTR00046253
    if (arr['PCTR00046253']=='PCTR00046253') {
      alert('Para comprar Segurança Total voce precisa retirar o pacote de Antivirus.');
      return false;
    }
    //firewall = PCTR00046257
    if (arr['PCTR00046257']=='PCTR00046257') {
      alert('Para comprar Segurança Total voce precisa retirar o pacote de Firewall.');
      return false;
    }
    //Controle de privacidade PCTR00046261
    if (arr['PCTR00046261']=='PCTR00046261') {
      alert('Para comprar Segurança Total voce precisa retirar o pacote de Controle de Privacidade.');
      return false;
    }
    //PC Seguro
    if (arr['PCTR00046265']=='PCTR00046265') {
      alert('Remova o PC Seguro antes de incluir o Segurança Total.');
      return false;
    }
    //BL ST
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Banda Larga Seguranca Total.');
      return false;
    }
  }
  
  //av
  if (cd=='PCTR00046253') {
    if (arr['PCTR00046271']=='PCTR00046271') {
      alert('Antivirus já está incluso no pacote Segurança Total.');
      return false;
    }
    if (arr['PCTR00046265']=='PCTR00046265') {
      alert('Antivirus já está incluso no pacote Pc Seguro.');
      return false;
    }
    //BL ST
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Banda Larga Seguranca Total.');
      return false;
    }
  }
  
  //fw
  if (cd=='PCTR00046257') {
    if (arr['PCTR00046271']=='PCTR00046271') {
      alert('Firewall já está incluso no pacote Segurança Total.');
      return false;
    }
    if (arr['PCTR00046265']=='PCTR00046265') {
      alert('Firewall já está incluso no pacote Pc Seguro.');
      return false;
    }
    //BL ST
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Banda Larga Seguranca Total.');
      return false;
    }
  }
  
  //controle priv
  if (cd=='PCTR00046261') {
    if (arr['PCTR00046271']=='PCTR00046271') {
      alert('Controle de Privacidade já está incluso no pacote Segurança Total.');
      return false;
    }
    //BL ST
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Banda Larga Seguranca Total.');
      return false;
    }
  }
  
  //BL ST
  if (cd=='PCTR00048043') { 
    if (arr['PCTR00048341']=='PCTR00048341') {
      alert('Você não pode adquirir dois pacotes de acesso.');
      return false;
    }
    // antivirus = PCTR00046253
    if (arr['PCTR00046253']=='PCTR00046253') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Antivirus.');
      return false;
    }
    //firewall = PCTR00046257
    if (arr['PCTR00046257']=='PCTR00046257') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Firewall.');
      return false;
    }
    //seg total
    if (arr['PCTR00046271']=='PCTR00046271') {
      alert('Os serviços do este pacote já estão inclusos no pacote Segurança Total.');
      return false;
    }
    //PCTR00046261=priv
    if (arr['PCTR00046261']=='PCTR00046261') {
      alert('Para comprar este pacote voce precisa retirar o pacote de Controle de Privacidade.');
      return false;
    }
    //PCTR00046265 pcseguro
    if (arr['PCTR00046265']=='PCTR00046265') {
      alert('Para comprar este pacote voce precisa retirar o pacote de PC Seguro.');
      return false;
    }
  }
  
  //BL BASICO
  if (cd=='PCTR00048341') { 
    if (arr['PCTR00048043']=='PCTR00048043') {
      alert('Você não pode adquirir dois pacotes de acesso.');
      return false;
    }
  }
  
  //permitido
  return true;
}

//quando for o voip, verifica qual plano está sendo selecionado
function pega_plano_voip(){
	var inputs = getID('voip').getElementsByTagName('input');
	for (i=0; i<inputs.length; i++){
		if(inputs[i].checked == true){
			addProduto(inputs[i].value);
			return;
		}
	}
	alert('Selecione algum plano!');
}
function pega_plano_sonora(){
	var inputs = getID('sonora').getElementsByTagName('input');
	for (i=0; i<inputs.length; i++){
		if(inputs[i].checked == true){
			addProduto(inputs[i].value);
			return;
		}
	}
	alert('Selecione algum plano!');
}

function excluir_plano(item,id){

  prod_adicionado[id]=undefined;

  var li;
  li = item.parentNode;
  li.parentNode.removeChild(li);
  return false;
}

function verifica_submit(){

  getID('prod_selecionados').value = build_produtos(prod_adicionado,',');
	var produtos = getID('prod_selecionados').value;

	if(produtos == ""){
		alert("Selecione algum produto!");
	}else{
		getID("form").submit();
	}
}


