var panel1;
var panel2;
function show_panel(titulo,html,w) {

  YAHOO.namespace("example.body");
  
  // Instantiate a Panel from markup
  panel1 = YAHOO.example.body.panel1;
  panel2 = YAHOO.example.body.panel2;
  
  panel1 = new YAHOO.widget.Panel("panel1", { width:w+"px", visible:true, constraintoviewport:true } );
  panel1.render();
  
  // Instantiate a Panel from script
  panel2 = new YAHOO.widget.Panel("panel2", { xy:[488,162], width:w, visible:false, draggable:false, close:false } );
  panel2.setHeader(titulo);
  panel2.setBody(html);
  panel2.render("body");
  
  return panel2;
}
function removeDIV(main_elem,elem) {
  var main = document.getElementById(main_elem);
  var div = document.getElementById(elem);
  main.removeChild(div);
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

var equivalentes;
//Função de carrinho de compras
function carrinho(form){
  if (xmlhttp)
  {
    // tenta estabelecer ligação
    try
    {
      
      var serverPage="carrinho_actions.php";
      
      
      
     
      f
      
      if(opc1!=null || opc2!=null){
        var action = "add_split";
      }else{
        var action = "add_multiple";
      }
      
      
      
      var params = "action=" + action + "&prod=" + prod + "&qt=" + qt + "&id_cliente=" + id_cliente ;
      
      
     
      xmlhttp.open("POST", serverPage,true);
      
      //Send the proper header information along with the request
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      
      xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
        if(xmlhttp.readyState == 1 ) {
              
        }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if(xmlhttp.responseText){
                var resultado = xmlhttp.responseText;
                var panel;
                if(resultado=='1'){ // se tiver sido adicionado com sucesso
                  
                  
                  var  tit = "Produto adicionado com sucesso";
                  var html_panel = "<p style='padding:6px 0 0 6px;'>Referência ( <b>"+ refs +"</b> ) adicionada com sucesso a sua encomenda</p><div class='div_opc_pop' id=hide2><div class='bt_close' onclick=\"panel2.hide();document.getElementById('fundo_opaco').style.display='none';\" ></div><p class='ou'>ou</p><div class='ver_enc'><a href='index.php?id=69'>Ver a minha Encomenda</a></div><div class='clear'></div></div>";
                  
                  document.getElementById('fundo_opaco').style.display='';
                  	 
                  panel = show_panel(tit,html_panel,416);
                  panel.show();		
                  
                 
                 
                }else if(resultado=='2'){
                
                }else if(resultado==''){ //se não tiver sido adicionado
                  
                  var  tit = "Produto não foi adicionado";
                  var html_panel = "<p style='padding:6px 0 0 6px;'>Referência ( <b>"+ refs +"</b> ) não foi adicionada.</p><div class='div_opc_pop' id=hide2><div class='bt_close' onclick=\"removeDIV('body','panel2_c');document.getElementById('fundo_opaco').style.display='none';\" ></div><div class='ver_enc'></div><div class='clear'></div></div>";
                  
                  document.getElementById('fundo_opaco').style.display=''
                  	 
                  panel = show_panel(tit,html_panel,416);
                  panel.show();		
                  
                }
                
            }
        }
      }
     
      xmlhttp.send(params);
      return false;
    
    }catch (e){
      alert("Não foi possivel adicionar ao carrinho:\n" + e.toString());
      return false;
    }
  }else {
    return true;
  }
}




//Função de carrinho de compras
function remove_carrinho(prod_id){
  if (xmlhttp)
  {
    // tenta estabelecer ligação
    try
    {
      
      var confirmacao = window.confirm("Tem a certeza que deseja remover produto do carrinho de compras?", "");
      
      if(confirmacao){
      
      
        var serverPage="carrinho_actions.php";
        
        var params = "action=remove&prod_id=" + prod_id;
        
        xmlhttp.open("POST", serverPage,true);
        
        //Send the proper header information along with the request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
        
        xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
          if(xmlhttp.readyState == 1 ) {
                
          }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              
              if(xmlhttp.responseText){
                  var resultado = xmlhttp.responseText;
                  var panel;
                  if(resultado=='1'){
                    
                    alert("Produto removido com sucesso");	
                    window.location = window.location.href;
                    
                    
                    
                  }else {
                    alert("Não foi possivel remover o produto que solicitou.");		
                  }
                 
                  
              }
          }
        }
        xmlhttp.send(params);
        return false;
      }else {
        return false;
      }
    
    }catch (e){
      alert("Não foi possivel adicionar ao carrinho:\n" + e.toString());
      return false;
    }
  }else {
    return true;
  }
}

//Função de remoção do carrinho de compras
function remove_all(){
  if (xmlhttp)
  {
    // tenta estabelecer ligação
    try
    {
      
      var confirmacao = window.confirm("Tem a certeza que deseja remover todos os produtos do carrinho de compras?", "");
      
      if(confirmacao){
      
      
        var serverPage="carrinho_actions.php";
        
        //var params = "action=remove_all&id_cliente=" + id_cliente;
        var params = "action=remove_all";
        
        xmlhttp.open("POST", serverPage,true);
        
        //Send the proper header information along with the request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
        
        xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
          if(xmlhttp.readyState == 1 ) {
                
          }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              
              if(xmlhttp.responseText){
                  var resultado = xmlhttp.responseText;
                  var panel;
                  if(resultado=='1'){
                    
                    window.location='index.php?id=55';
                    
                  }else {
                    var  tit = "Produtos não foram removidos";
                    var html_panel = "<p style='padding:6px 0 0 6px;'>Os produtos não foram removidos da sua encomenda.Por favor tente novamente.</p><div class='div_opc_pop' id=hide2><div class='bt_close' onclick=\"removeDIV('body','panel2_c')\" ></div><div class='clear'></div></div>";
                    	 
                    panel = show_panel(tit,html_panel);
                    panel.show();	
                  }
                 
                  
              }
          }
        }
        xmlhttp.send(params);
        return false;
      }else {
        return false;
      }
    
    }catch (e){
      alert("Não foi possivel adicionar ao carrinho:\n" + e.toString());
      return false;
    }
  }else {
    return true;
  }
}



