//Site
var Site = {

    Start: function(){
        Site.Navigation2();
        Site.Navigation3();
        //Site.DefaultPage();
        KaUpdateField('Voertuig');
    },

    Navigation2: function(){
        var navigation = document.getElementById("Menu2");
        for(i = 0; i < navigation.childNodes.length; i++){
            if(navigation.childNodes[i].nodeName == "A"){
                var childNodeID = navigation.childNodes[i].getAttribute('id');
                $(childNodeID).addEvent('click', function(e){
                    e = new Event(e).stop();
                    var url = this.getAttribute('href')
                    new Ajax(url, {
                        method: 'get',
                        update: $('ContentL3')
                    }).request();
                    changeObjectVisibility('ContentL0Box2', 'hidden');
                });
            };
        };
    },

    Navigation3: function(){
        var navigation = document.getElementById("Menu3");
        for(i = 0; i < navigation.childNodes.length; i++){
            if(navigation.childNodes[i].nodeName == "A"){
                var childNodeID = navigation.childNodes[i].getAttribute('id');
                $(childNodeID).addEvent('click', function(e){
                    e = new Event(e).stop();
                    var url = this.getAttribute('href')
                    new Ajax(url, {
                        method: 'get',
                        update: $('ContentL3')
                    }).request();
                    changeObjectVisibility('ContentL0Box2', 'hidden');
                });
            };
        };
    },

    DefaultPage: function(){
        new Ajax('home.htm', {
            method: 'get',
            update: $('Content')
        }).request();
        
        document.getElementById("Home").setActive();
    },
    	
    ContactPage: function(){
        new Ajax('contact-verzonden.htm', {
            method: 'get',
            update: $('Content')
        }).request();
        
        document.getElementById("Contact").setActive();
    }
}

//Functions
function changeObjectVisibility(objectId, newVisibility){
  var object = document.getElementById(objectId);
  if(object){
    object.style.visibility = newVisibility;
  } else {
    return false;
  }
};

//Functions Kettingselector
function KaUpdateField(Field){

  var Voertuig_Field = document.getElementById('Voertuig');
  var Velgmaat_Field = document.getElementById('Velgmaat');
  var Bandmaat_Field = document.getElementById('Bandmaat');
  
  var Selected_Field  = document.getElementById(Field);
  
  var Voertuig = ""
  var Velgmaat = ""
  var Bandmaat = ""
  
  // Clear selects from the field to fill on
  switch(Field){
    case 'Voertuig': Voertuig_Field.options.length = 0;
    case 'Velgmaat': Velgmaat_Field.options.length = 0;
    case 'Bandmaat': Bandmaat_Field.options.length = 0;
  };
  
  // Get selected option
  if (Voertuig_Field.options.length > 0){ Voertuig = Voertuig_Field.options[Voertuig_Field.selectedIndex].value; };
  if (Velgmaat_Field.options.length > 0){ Velgmaat = Velgmaat_Field.options[Velgmaat_Field.selectedIndex].value; };
  if (Bandmaat_Field.options.length > 0){ Bandmaat = Bandmaat_Field.options[Bandmaat_Field.selectedIndex].value; };
  
  // Build URL
  var url = "KaData.asp?Voertuig=" + Voertuig + "&velgmaat=" + Velgmaat + "&bandmaat=" + Bandmaat + "";
  //var url = "traditioneel.asp";

  // Get KaData
  objAjax = new Ajax(url, {method: 'get', onComplete: getResponse}).request();
    
  function getResponse(request){
    
    var fieldOptions = request.split(',');
    
    Selected_Field.options[0] = new Option("Selecteer...","");
    
    for(i=1;i<=fieldOptions.length;i++){
      Selected_Field.options[i] = new Option(fieldOptions[i-1], fieldOptions[i-1]);
    };
  };
};

function KaSearch(){

  var Voertuig_Field = document.getElementById('Voertuig');
  var Velgmaat_Field = document.getElementById('Velgmaat');
  var Bandmaat_Field = document.getElementById('Bandmaat');
  
  var Voertuig = ""
  var Velgmaat = ""
  var Bandmaat = ""
  
  // Get selected option
  if (Voertuig_Field.options.length > 0){ Voertuig = Voertuig_Field.options[Voertuig_Field.selectedIndex].value; };
  if (Velgmaat_Field.options.length > 0){ Velgmaat = Velgmaat_Field.options[Velgmaat_Field.selectedIndex].value; };
  if (Bandmaat_Field.options.length > 0){ Bandmaat = Bandmaat_Field.options[Bandmaat_Field.selectedIndex].value; };
  
  // Build URL
  var url = "KaData.asp?Voertuig=" + Voertuig + "&velgmaat=" + Velgmaat + "&bandmaat=" + Bandmaat + "&search=1";

  // Get KaData
  objAjax = new Ajax(url, {method: 'get', update: $('ContentL3')}).request();
  changeObjectVisibility('ContentL0Box2', 'hidden');
};
