
/* replace the font */

Cufon.replace('h2', {
    hover: true
});


 
/* customize the checkbox */

function setCheckboxDisplay(el){
  var objWrap;
  objWrap = document.getElementById(el.id+"Wrap");
      
  if(el) {
      if(el.checked)
      { 
        $('.'+el.id+'link').css('font-weight', 'bold');
        objWrap.className = "styledCheckboxWrap";
      }else
      {
        $('.'+el.id+'link').css('font-weight', 'normal'); 
        objWrap.className = "styledCheckboxWrap wrapChecked";
      }
  }
}



function deleteFromCart(pid, type, deleteall)
{
  $.ajax({
             type: "POST",
             url: "/delfromcart.php",
             data: 'id='+ pid + '&type=' + type + '&deleteall=' + deleteall,
             success: function(msg){
                 $('#cart-products').html(msg);
                 AnnalizeTotals();
             }
             });  
}

function updateUser()  
{
  if(!$('#agree').is(':checked'))
  {
    alert('U bent not niet akkoord gegaan met algemene voorwaarden');
    return false; 
  }
  
  var pricekm = 0; 
  var montage = 0; 
  $('.kmvalue').each(function(){
    if($(this).is(':checked'))
    {
      switch($(this).val())
      {
      
        case 'brons':case 'zilver':
        {
          pricekm = 0;   
          break;
        }
        
        case 'goud':
        {
          pricekm = parseFloat($('#km').val()) * 300;   
          break;
        }
        
        case 'goud+':
        {
          pricekm = parseFloat($('#km').val()) * 300;
          montage = 8900;
          break;
        }
      }
    }
  });
  
  $.ajax({
             type: "POST",
             url: "/updateUser.php",
             data: 'city='+ $('#city').val() + '&address=' + $('#address').val(),
             success: function(msg){
                $.ajax({
                       type: "POST",
                       url: "/order.php",
                       data: 'priceonkm=' + pricekm + '&montage=' + montage,
                       success: function(msg){
                         $('#form1').html(msg);
                         $("#form1").submit();       
                       }
                       });    
             }
             });
  
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function getDistance(e)
{
  $.ajax({
             type: "POST",
             url: "/getDistance.php",
             data: 'city='+ e.value,
             success: function(msg){
                 $('#km').val(msg);
                 AnnalizeTotals();
             }
             });
}

function AnnalizeTotals()
{          
  var totalprice = 0;   
  var pricekm = 0;
  
  if(IsNumeric($('#km').val()) !== true)
  {         
    $('#km').val(1); 
  }  
  
  $('.kmvalue').each(function(){
    if($(this).is(':checked'))
    {
      switch($(this).val())
      {
      
        case 'brons':case 'zilver':
        {
          $('#tvervoerskosten').html('&euro; 0,00');
          pricekm = 0;   
          break;
        }
        
        case 'goud':
        {
          $('#tvervoerskosten').html('&euro; ' + putdot(parseFloat($('#km').val()) * 3 + (parseFloat($('#km').val()) * 3) * 19 / 100));
          pricekm = parseFloat($('#km').val()) * 3; 
          break;
        }
        
        case 'goud+':
        {
          $('#tvervoerskosten').html('&euro; ' + putdot(parseFloat($('#km').val()) * 3 + 89 + (parseFloat($('#km').val()) * 3 + 89) * 19 / 100));  
          pricekm = parseFloat($('#km').val()) * 3 + 89;
          break;
        }
      }
    }
  });
  
  $('.prices').each(function(){
    totalprice = parseFloat(totalprice) + parseFloat($(this).val());  
  });
  
  if(totalprice == 0)
  {
    $('.pay').each(function(){
      $(this).css('display', 'none');
    });
  }
  
  $('#priceitemsbtw').html('&euro; ' + putdot(totalprice + totalprice * 19 / 100));  
  //alert(totalprice + pricekm);    
  $('#total-btw').html('&euro; ' + putdot(totalprice + pricekm));    
  $('#btw').html('&euro; ' + putdot((totalprice + pricekm) * 19 / 100));
  $('#totalprices').html('&euro; ' + putdot(totalprice + pricekm + (totalprice + pricekm) * 19 / 100));      
}

function calcTotal(count, price, type, typeorig, pid)
{
  var total = count * price;
  var totalprice = 0;
  $('#' + type).html('&euro; ' + putdot(total));
  $('#p' + type).val(total);  
  
  $.ajax({
             type: "POST",
             url: "/updateCart.php",
             data: 'id='+ pid + '&type=' + typeorig + '&count=' + count,
             success: function(msg){
                 //$('#cart-products').html(msg);
                 //AnnalizeTotals();
             }
             });  
  
  $('.prices').each(function(){
    totalprice = parseFloat(totalprice) + parseFloat($(this).val());  
  });
  
  $('#totalprice').html('&euro; ' + putdot(totalprice));
  AnnalizeTotals();
}

/* filter of the products */

/* fold in/out the tree when click on name of a category/subcategory or select a single subcategory if don`t have tree */

function foldtree(name, cat)
{
  if($(".tree-"+name).size() > 0)
  {
      var display = $(".tree-"+name).css("display");
      if(display == "none")
        $(".tree-"+name).css('display', 'block'); 
      else
        $(".tree-"+name).css('display', 'none'); 
  }

}

function AnnalizeTitle(e, catid, headers)
{                   
  if($(e).is(':checked'))
  {              
    $(e).attr('checked', false); 
  }
  else
  {
    $(e).attr('checked', true);   
  }
  
  Annalize(e, catid, '', headers);
}

function resetTree(e)
{
  $(".category").each(function(){
    $(this).attr('checked', false);    
    $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked');  
    $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');
    $('.tree-'+$(this).attr('id')).css('display','none');
    $('.tree-'+$(this).attr('id') + ' .subcategory').each(function(){
      $(this).attr('checked', false);    
      $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked');  
      $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');
      $('.tree-'+$(this).attr('id') + ' .subsubcategory').each(function(){  
        $(this).attr('checked', false);    
        $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked');  
        $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');
      });
    });    
  });  
}

function Annalize(e, catid, subcatid, headers)
{         
       
  var categorychecked = '';
  var categorydeleted = '';         
  if($(e).hasClass('category'))
  {      
    resetTree(e);                  
    categorychecked = $(e).val();  
    foldtree($(e).attr("id")); 
    //alert($('#'+$(e).attr('id')+'Wrap').attr('class'));     
    $('#'+$(e).attr('id')+'Wrap').removeClass('wrapChecked'); 
    //alert($('#'+$(e).attr('id')+'Wrap').attr('class'));      
    $("."+$(e).attr("id")+'link').css('font-weight', 'bold'); 
    $(e).attr('checked', true); 
    $("."+$(e).attr("name")).each(function(){
       $('#'+$(this).attr('id')).attr('checked', true);          
       $('#'+$(this).attr('id')+'Wrap').removeClass('wrapChecked');  
       $('.'+$(this).attr('id')+'link').css('font-weight', 'bold');     
      });       
  }
  /*else
  {
    if($(e).hasClass('category') && !$(e).is(':checked'))
    {    alert('a');            
      categorydeleted = $(e).val();
      foldtree($(e).attr("id")); 
      $("."+$(e).attr("name")).attr('checked', false);  
      $("."+$(e).attr("id")+'link').css('font-weight', 'normal');     
      $("."+$(e).attr("name")).each(function(){
       $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked'); 
       $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');
      }); 
    }
  }
  */
  
  if($(e).hasClass('subcategory') && !$(e).is(':checked'))
  {                 
    $("."+$(e).attr("name")).attr('checked', false);
    foldtree($(e).attr("id"));
    $('.'+$(e).attr('id')+'link').css('font-weight', 'normal');   
    $("."+$(e).attr("name")).each(function(){
       $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked'); 
       $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');    
      }); 
  }
  
  if($(e).hasClass('subcategory') && $(e).is(':checked'))
  {                 
    $("."+$(e).attr("name")).attr('checked', true);
    foldtree($(e).attr("id"));  
    $('.'+$(e).attr('id')+'link').css('font-weight', 'bold');
    $('#' + catid + 'Wrap').removeClass('wrapChecked'); 
    $('.' + catid + 'link').css('font-weight', 'bold');  
    $("#"+catid).attr('checked', true);   
    $("."+$(e).attr("name")).each(function(){
       $('#'+$(this).attr('id')+'Wrap').removeClass('wrapChecked'); 
       $('.'+$(this).attr('id')+'link').css('font-weight', 'bold');    
      }); 
  }
  
        
  var category = [];
  var subcategory = [];
  var subsubcategory = [];
  
  
  $('.category:checked').each(function(){
    category.push($(this).val());  
  });
  
  $('.subcategory:checked').each(function(){
    if($(this).hasClass('subcategory'))
      subcategory.push($(this).val());  
  });
  
  $('.subsubcategory:checked').each(function(){
    subsubcategory.push($(this).val());  
  });
  
  if($(e).hasClass('designer')) 
  {
    var designer = category;
    var new_products = 1;
    //alert(designer);
  }
  else
  {
    var new_products = 0;
    var designer = $('#designer').val(); 
  }
  
  if(category.length == 1 && headers != 0)
  {        
    $.ajax({
             type: "POST",
             url: "/getheaders.php",
             data: 'category='+ category,
             success: function(msg){
                 $('#slideshow').html(msg);
                 
                 $('#slideshow').cycle({
                    fx:     'fade',
                    speed:   2000,
                    timeout: 5000,
                    pager:  '#nav',
                    slideExpr: 'img'
                });
             }
             });  
  }
  else
  {
    if(headers != 0)
    {
       $('#slideshow').html("<img src=\"/images/header_producten1.jpg\" alt=\"Suite Wood\" width=\"947\" height=\"117\" /><img src=\"/images/header_producten2.jpg\" alt=\"Suite Wood\" width=\"947\" height=\"117\" /><img src=\"/images/header_producten3.jpg\" alt=\"Suite Wood\" width=\"947\" height=\"117\" />");
                   
       $('#slideshow').cycle({
          fx:     'fade',
          speed:   2000,
          timeout: 5000,
          pager:  '#nav',
          slideExpr: 'img'
          });
    }
  }
  
  if(category.length == 0)
  {
    $('.category').each(function(){
        category.push($(this).val());  
    });
    
    $('.subcategory').each(function(){
    if($(this).hasClass('subcategory'))
      subcategory.push($(this).val());  
    });
  
    $('.subsubcategory').each(function(){
      subsubcategory.push($(this).val());  
    });
    
  }       
   
  $.ajax({
             type: "POST",
             url: "/getprod.php",
             data: 'subsubcategory='+ subsubcategory +'&subcategory='+ subcategory +'&categorychecked='+ categorychecked +'&categorydeleted='+ categorydeleted +'&designer='+ designer + '&producer='+ $('#producer').val() + '&new_products='+ new_products,
             success: function(msg){
                 $('#products-list').html(msg);
                 linkstitle(); 
                 
             }
  }); 
     
}

/* slideshow from home page */

$(function() {         
    $('#slideshow').cycle({
        fx:     'fade',
        speed:   2000,
        timeout: 5000,
        pager:  '#nav',
        slideExpr: 'img'
    });
});

/* selecting only a single subsubcategory */

function selectSubSubCat(subsubcat, subcat, cat)
{
    $('input').each(function(){
       if($(this).attr('id') != cat && $(this).attr('id') != subcat && $(this).attr('id') != subsubcat && $(this).attr('type') == 'checkbox')
       { 
          $(this).attr('checked', false);
          $('#'+$(this).attr('id')+'Wrap').addClass('wrapChecked'); 
          $('.'+$(this).attr('id')+'link').css('font-weight', 'normal');      
       }
    });
    
    if(subsubcat != '')
    {
      $('#'+subsubcat).attr('checked', true); 
      $('.'+$('#'+subsubcat).attr('id')+'link').css('font-weight', 'bold');  
      $('#'+subsubcat+'Wrap').removeClass('wrapChecked');        
    }
    else
    {        
      $('.tree-' + subcat + ' a').css('font-weight', 'bold');
      $('.tree-' + subcat + ' input').each(function(){
        $('#' + $(this).attr('id') + 'Wrap').removeClass('wrapChecked');
        $('#' + $(this).attr('id')).attr('checked', true); 
      }); 
    }
    
    
    $('#'+subcat).attr('checked', true);    
    $('.'+$('#'+subcat).attr('id')+'link').css('font-weight', 'bold');  
    $('#'+cat).attr('checked', true);    
    $('.'+$('#'+cat).attr('id')+'link').css('font-weight', 'bold');    
    $('#'+subcat+'Wrap').removeClass('wrapChecked'); 
    $('#'+cat+'Wrap').removeClass('wrapChecked');  
    
    var category = [];
    var subcategory = [];
    var subsubcategory = [];
    var categorychecked = '';
    var categorydeleted = '';
    
    $('.category:checked').each(function(){
      category.push($(this).val());  
    });
    
    $('.subcategory:checked').each(function(){
      if($(this).hasClass('subcategory'))
        subcategory.push($(this).val());  
    });
    
    $('.subsubcategory:checked').each(function(){
      subsubcategory.push($(this).val());  
    });
     
    $.ajax({
               type: "POST",
               url: "/getprod.php",
               data: 'subsubcategory='+ subsubcategory +'&subcategory='+ subcategory +'&categorychecked='+ categorychecked +'&categorydeleted='+ categorydeleted +'&designer='+ $('#designer').val() + '&producer='+ $('#producer').val(),
               success: function(msg){
                   $('#products-list').html(msg);
                   $(document).ready(function() {
                      $("body").append("<div id='ToolTipDiv'></div>");
                      
                      linkstitle();    
                      
                    });
               }
    }); 
} 

/* get a product details */


function loadProduct(id)
{
  $.ajax({
               type: "POST",
               url: "/getitem.php",
               data: 'id='+ id,
               success: function(msg){ 
                   $('#products-list').html(msg);
               }
    });  
}

/* add to shopping cart the item */

function addtocart(id)
{
  var type = [];
  $('.material').each(function(){
      type.push($(this).attr('id') + '/' + $(this).val() + '/' + $('.' + $(this).attr('id') + 'colors').val());  
    });
  $.ajax({
               type: "POST",
               url: "/addtocart.php",
               data: 'id='+ id + '&count=' + $('#count').val() + '&type=' + type,
               success: function(msg){ 
                   $('#products-number a').html(msg);
               }
    });
}

function fixheight() {    
    if($(window).height() > $('#suitewood').height() + 98)
    {     
      $('footer').css("margin-top", $(window).height() - $('#suitewood').height() - 98 + "px");
    }
}

  
     
function linkstitle()
{
  $("a[titled]").each(function() {
    $(this).hover(function(e) {   
      $(this).mousemove(function(e) {     
        var tipY = e.pageY + 3;
        var tipX = e.pageX + 16;   
        $("#ToolTipDiv").css({'top': tipY, 'left': tipX});
      });
      $("#ToolTipDiv")
        .html($(this).attr('titled'))
        .stop(true,true)
        .fadeIn("fast");
      //$(this).removeAttr('titled');
    }, function() {
      $("#ToolTipDiv")
        .stop(true,true)
        .fadeOut("fast");
      //$(this).attr('titled', $("#ToolTipDiv").html());
    });
  });
}

function putdot(value)
{
  
    var copyvalue = value; 
    var stringvalue = "";   
    var rest = 0;
    var copyrest = "";
    var reststring = "";
    var result = 0; 
    var ok = 0;
    var firstrest = 0;
    if(parseInt(copyvalue / 1000) == 0) 
    {                      
        rest = (copyvalue % 1000);     
        copyrest = (rest.toFixed(2)).toString();
        copyrest = copyrest.replace(".",","); 
        stringvalue = copyrest; 
        ok = 1;       
    }
     
    while(parseInt(copyvalue / 1000) > 0)
    {            
        rest = (copyvalue % 1000); 
        if(firstrest == 0)
        {
            copyrest = (rest.toFixed(2)).toString();
            copyrest = copyrest.replace(".",",");
            firstrest = 1;    
        }else 
            copyrest = rest.toString();   
            
        if(rest < 10)
            reststring = "00" + copyrest;
        else
        {
            if(rest<100)
                reststring = "0" + copyrest;
            else 
                reststring = copyrest;
        }
        result = parseInt(copyvalue / 1000);
        stringvalue = stringvalue + result.toString() + "." + reststring;  
        ok = 1;    
        copyvalue = parseInt(copyvalue / 1000);
    }
      
    if(ok == 1)
        return stringvalue;
    else      
        return value;
}

function makeTotal(val)
{
  var total = parseFloat($('#totalprice').html());   
  $('.prices').each(function(){
                 total = total + parseFloat($(this).html());
                 
              });
  total = total * parseInt(val);
  $('#total').html('&euro; ' + putdot(total));
}

function getColors(mid, tid, productslug)
{
  var total = parseFloat($('#totalprice').html());
  
  $.ajax({
             type: "POST",
             url: "/loadcolors.php",
             data: 'material_id=' + mid + '&type_id=' + tid + '&product_slug=' + productslug,
             success: function(msg){
              $('#t'+tid+'').html(msg);
             },
             error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }    
  });
  $.ajax({
             type: "POST",
             url: "/loadprice.php",
             data: 'material_id=' + mid + '&type_id=' + tid + '&product_slug=' + productslug,
             success: function(msg){
              $('#p'+tid+'').html(msg);
              
              $('.prices').each(function(){  
                 
                if(mid != $("#" + tid + " option:first-child").attr('value'))
                {       
                  total = total + parseFloat($(this).html());
                }
                 
              });
              
              $('#total').html('&euro; ' + putdot(total));
             },
             error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }    
  });
}

function pageload()
{
  fixheight();
  linkstitle();
  externalLinks();
  $("body").append("<div id='ToolTipDiv'></div>");
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
 var anchor = anchors[i];
 if (anchor.getAttribute("href") &&
 anchor.getAttribute("rel") == "external")
 anchor.target = "_blank";
 }
} 

window.onload = pageload
