// Kinley functions

    function getElementsByClassName (cl) {
      var retnode = [];
      var myclass = new RegExp('\\b'+cl+'\\b');
      var elem = document.getElementsByTagName('*');
      
      for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)){
          retnode.push(elem[i]);
        }
      }
    return retnode;
    }     
    
    function gebid(inp) { return document.getElementById(inp); }
    
    function setBackground(lk,image,id){
    lk.removeAttribute('href');
    
      var secs = getElementsByClassName('section_image');
      
      for (i = 0;i < secs.length;i++){
        secs[i].style.display = "none";
      }
      
      gebid('desc'+id).style.display = "block";
    }
    
    function swapImage(thumb,idp,main){
      /*var mainimg = gebid('image_'+idp);
      
      var thisimg = new Image();
      thisimg.src = mainimg.src;*/
      
      gebid('section_image').title = thumb.alt;
      gebid('section_image').style.backgroundImage = "url("+thumb.src+")";
      gebid('section_image').style.backgroundPosition = "center center";
      
      // If not then it isn't the product images    
      if (gebid('buttons')){
        if (main){
          gebid('buttons').style.display = 'block';
          gebid('subs').style.display = 'block';
          gebid('adsbar').style.display = 'block';
        } else {
          gebid('buttons').style.display = 'none';
          gebid('subs').style.display = 'none';
          gebid('adsbar').style.display = 'none';      
        }          
      }
    }
    
    function showHide(link,div){
    link.removeAttribute('href');

      if (gebid(div).style.display == 'none'){
        gebid(div).style.display = 'block'
      } else {
        gebid(div).style.display = 'none';
      }
    }
    
    function getImageName(src,section){
      var last = src.lastIndexOf('/');
      if (section == "last"){
        return src.substr(last+1,src.length);
      } else {
        return src.substr(0,last);
      }
    }

    // Create connector for AJAX (browser depending) - how are we going to send the request?
    function connector()
    {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      if (window.XMLHttpRequest)  { xmlhttp=new XMLHttpRequest(); }
      // code for IE6, IE5
      else  { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    
    return xmlhttp;
    }
    
    function get_ajax(url,params,output){
    var xmlhttp = connector();
      
      if (output == "return"){
        var stop = false;
      } else {
        var stop = true;
        
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            gebid(output).innerHTML = xmlhttp.responseText;
          }
        }
      }
      
      xmlhttp.open("GET",url+'?'+params,stop);
      xmlhttp.send();
    
      if (output == "return"){  
        return xmlhttp.responseText;
      }
    }  

    function checkImageExists(show,imagetocheck,ifbgrd,downlink){

      var im = gebid(imagetocheck);
      var image = (ifbgrd == "background") ? im.style.backgroundImage : im.src;
             
      var name = getImageName(image,'last'); // just in case it is a full path      
      name = name.replace(/[")]*/g,"");
      var isfile = get_ajax('checkimage.php',"f="+name,"return");     

      if (isfile == 1){
        gebid(downlink).href = "downloadimage.php?f="+name;
      } 
      
      gebid(show).style.display = (isfile == 1) ? "block" : "none";
    }
    
    function swapHide(thislink,div,hide){
      thislink.removeAttribute('href');
      
      // Tab effect
      var thisimg = thislink.getElementsByTagName('img')[0];
      var otherlink = gebid(hide+'tab');
      var otherimg = otherlink.getElementsByTagName('img')[0];
      
      if (thislink.rel == "off"){
        thisimg.src = thisimg.src.replace('_off','_on');
        otherimg.src = otherimg.src.replace('_on','_off');
        
        thislink.rel = "on";
        otherlink.rel = "off";     
      } 
      
      gebid(div).style.display = 'block';
      gebid(hide).style.display = 'none';      
    }          

    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";  
     }  
    }  
