var xmlHttp

function showResult(str, key){

var str = str.replace(/^\s+|\s+$/g, '') ;

if ((key==13)&&(document.all.toplink.value!="")){
    top.location='http://www.dcperformance.co.uk'+document.all.toplink.value;
}

if (str.length==0){
 document.getElementById("livesearch").innerHTML="";
 document.getElementById("livesearch").style.borderTop="0px";
 return
}

xmlHttp=GetXmlHttpObject2();

if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
return;
}

var url="/scripts/livesearch.php";
url=url+"?q="+str.replace(/&/,"%26");
url=url+"&sid="+Math.random();
url=url+"&key="+key;
xmlHttp.onreadystatechange=stateChanged2;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showUser(str){
xmlHttp=GetXmlHttpObject2();
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
return;
}
var url="/scripts/getmodel.php";
url=url+"?q="+str.toLowerCase();
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("modelbox").innerHTML=xmlHttp.responseText;
}
}

function stateChanged2(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
 document.getElementById("livesearch").innerHTML=xmlHttp.responseText;
 document.getElementById("livesearch").style.borderTop="1px solid #A5ACB2";
 }
}

function GetXmlHttpObject2(){
var xmlHttp=null;
try {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

matchHeight=function(){

// ############ first sort out the layout ############

     dynamicLayout();

// ############ then check for the mootools stuff

     if (document.getElementById("t1") != null){

          if (document.getElementById("t3") != null){

//################### this starts the mootools scripts if the t1 div is present ########################

  //          window.addEvent('domready', function() {   ############ this was taken out to allow fix the issue with the shrink script to work when loading and refreshing the page.
            var accordion = new Accordion('div.t1', 'div.t2', {
            //start:'all-closed',
            display:document.getElementById('box1').value,
            opacity: false,
            duration: 100,
            alwaysHide: true,
            onActive: function(togglers, stretchers){
            togglers.setStyle('background-image', 'url(/images/background/catbaron.png)');
            },
            onBackground: function(togglers, stretchers){
            togglers.setStyle('background-image', 'url(/images/background/catbar.png)');
            //togglers.setStyle('background-color', '#FFFFFF');
            stretchers.setStyle('height', stretchers.offsetHeight);
            $$('div.t3').setStyle('height','0'); //This closes all sub-accordions.
            }
            }, $('sidebar'));
            var accordion1 = new Accordion('div.t3o', 'div.t3', {
            //start:'all-closed',
            display:document.getElementById('box2').value,
            opacity: false,
            duration: 100,
            alwaysHide: true,
            onActive: function(togglers, stretchers){
            togglers.setStyle('background-image', 'url(/images/background/subcatbaron.png)');
            togglers.getParent().setStyle("height", "auto");
            },
            onBackground: function(togglers, stretchers){
            togglers.setStyle('background-image', 'url(/images/background/subcatbar.png)');
            stretchers.setStyle('height',stretchers.offsetHeight);
            }
            }, $('sidebar'));
  //          });
        } else {

            var accordion = new Accordion('div.t1', 'div.t2', {
            start:'all-closed',
            opacity: false,
            duration: 100,
            alwaysHide: true,
            onActive: function(togglers, stretchers){
            togglers.className='catbaron';
            },
            onBackground: function(togglers, stretchers){
            togglers.className='t1';
            }
            }, $('sidebar'));

        }

     }

// ############ then sort out the heights of the three panes ############

     var divs,contDivs,maxHeight,divHeight,d;
     // get all <div> elements in the document
     divs=document.getElementsByTagName('div');
     contDivs=[];

      var maindivs=new Array();
      maindivs[0]="nh_mm";
      maindivs[1]="rightnav";
      maindivs[2]="leftnav";
      maindivs[3]="middlepad";

     maxHeight=0;

     for(var i=0; i<3; i++){
                d=document.getElementById(maindivs[i]);
                if(d.offsetHeight){
                     divHeight=d.offsetHeight;
                }
                else if(d.style.pixelHeight){
                     divHeight=d.style.pixelHeight;
                } else {
                    d.style.border="solid #FFFFFF 1.0px";
                    if(d.offsetHeight){
                        divHeight=d.offsetHeight;
                    }
                    else if(d.style.pixelHeight){
                        divHeight=d.style.pixelHeight;
                    }
                }
                maxHeight=Math.max(maxHeight,divHeight);
                if (i==0)mainheight=divHeight;
//                alert(maindivs[i]+" "+divHeight);
     }
//     alert(maxHeight);

     if (mainheight<maxHeight){
        for(var i=1; i<4; i++){
            if (i==3){
                newHeight=maxHeight - mainheight;
            } else {
                newHeight=maxHeight;
            }
//            alert(i+" new height="+newHeight);

//            alert (maindivs[i]+" = "+newHeight);
            document.getElementById(maindivs[i]).style.height=newHeight+'px';
        }
     }
}

	function getBrowserWidth(){
		if (window.innerWidth){
			return window.innerWidth;}
		else if (document.documentElement && document.documentElement.clientWidth != 0){
			return document.documentElement.clientWidth;	}
		else if (document.body){return document.body.clientWidth;}
			return 0;
	}

      function dynamicLayout(){
      	var browserWidth = getBrowserWidth();
      	if (browserWidth < 1050){
            document.getElementById("widecss").disabled=true;
            document.getElementById("widetxt").disabled=true;
      	}
      }


function setflash(){
<!--
var MM_contentVersion = 7;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
			for (var i = 0; i < words.length; ++i)
			{
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i];
			}
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
} else if ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("Mac") == -1 &&   navigator.appVersion.indexOf("3.1") == -1) || (navigator.plugins && navigator.plugins["Shockwave Flash"]) || navigator.plugins["Shockwave Flash 2.0"]){
   MM_FlashCanPlay="true";
} else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
    MM_FlashCanPlay="true";
}


  var flashwidth;
    var browserWidth
    browserWidth = getBrowserWidth();
    if (browserWidth < 1050){
        flashwidth=630;
        flashheight=146;
    } else {
        flashwidth=860;
        flashheight=200;
    }
if ( MM_FlashCanPlay ) {
    flashtxt="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+flashwidth+"' height='"+flashheight+"' id='front_page' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='/flash/front_page.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='/flash/front_page.swf' quality='high' bgcolor='#ffffff' width='"+flashwidth+"' height='"+flashheight+"' name='front_page' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>";
} else{
    flashtxt="<img width='"+flashwidth+"px' height='"+flashheight+"px' src='/images/background/frontpage1.jpg' alt='Autoart headlights, taillights incl LED and side repeaters' />";
}
document.getElementById('flashbox').innerHTML = flashtxt;
//-->


}


// changeLayout is based on setActiveStyleSheet function by Paul Sowdon
// http://www.alistapart.com/articles/alternate/
    function changeLayout(description){
       var i, a;
       for(i=0; (a = document.getElementsByTagName("link")[i]); i++){
    	   if(a.getAttribute("title") == description){a.disabled = false;}
    	   else if(a.getAttribute("title") != "default"){a.disabled = true;}
       }
    }

function alloylink(man, model){
    height=document.getElementById('wheelheight').value;
    width=document.getElementById('wheelwidth').value;
    linkval="/alloys/";
    if (man) linkval=linkval+man+"/";
    if (model) linkval=linkval+model+"/";
    if (height=="%") height="";
    if (width=="%") width="";
    linkval=linkval+height+"_"+width+".html";
    if ((height!="")||(width!=""))top.location=linkval;

}

window.onload=function(){
     if(document.getElementsByTagName){
          matchHeight();
     }
}

function changestars(star_no){
    for (x=0;x<=4;x++){
        if (x<=star_no){
            thisopacity("goldstar"+x, 0, 100, 100);
        } else {
            thisopacity("goldstar"+x, 50, 0, 50);
        }
    }
}

function resetstars(star_no){
    star_no=star_no-1;
    for (y=0; y<=4; y++){
        if (y<=star_no){
            thisopacity("goldstar"+y, 0, 100, 100);
        } else {
            thisopacity("goldstar"+y, 50, 0, 50);
        }
    }
}

function thisopacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
    var object = document.getElementById(id).style;

    if (((object.visibility!="hidden") && (opacEnd<1)) || ((object.visibility!="visible") && (opacEnd>0))){
        if(opacStart > opacEnd) {
            if (object.visibility=='hidden'){
                object.visibility='visible';
            }
            for(i = opacStart; i >= opacEnd; i--) {
                setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
            if (opacEnd=='0'){
                object.visibility='hidden';
            }
        } else if(opacStart < opacEnd) {
            if (object.visibility=='hidden'){
                object.visibility='visible';
            }
            for(i = opacStart; i <= opacEnd; i++)
                {
                setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
                timer++;
            }
        }
    }

}


function changeOpac(opacity, id) {
//    alert (document.getElementById("shade").style.width);       //line 339
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
//    document.getElementById(id).filters.item("DXImageTransform.Microsoft.Alpha").Opacity=opacity
    if (opacity==0){
        object.visibility = "hidden";
    } else {
        object.visibility = "visible";
    }
}

function checkTandCs(){
    if(document.getElementById('tandcs').checked==false){
        alert("Please tick the box to confirm that you have read the terms and conditions");
        return false;
    }


}

// ################ this is the new fade script

function getElm(eID) {
    return document.getElementById(eID);
}
function show(eID) {
    getElm(eID).style.display='block';
}
function hide(eID) {
    getElm(eID).style.display='none';
    getElm("whole").style.display='none';
}

function setOpacity(eID, opacityLevel) {
    var eStyle = document.getElementById(eID).style;
    eStyle.opacity = opacityLevel / 100;
    eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}

function fade(eID, startOpacity, stopOpacity, duration) {
    var speed = Math.round(duration / 100);
    var timer = 0;
    if (startOpacity < stopOpacity){ // fade in
        for (var i=startOpacity; i<=stopOpacity; i++) {
            setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
            timer++;
        } return;
    }
    for (var i=startOpacity; i>=stopOpacity; i--) { // fade out
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
        timer++;
    }
}

function fadeIn(eID, speed, start, end) {
    show("whole");
    if (getElm(eID).style.display=="block") return false;
    setOpacity(eID, 0); show(eID); var timer = 0;
    for (var i=start; i<=end; i++) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
        timer++;
    }
}
function fadeOut(eID, speed, start, end) {
    if (getElm(eID).style.display=="none") return false;
    var timer = 0;
    for (var i=start; i>=end; i--) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
        timer++;
    }
   setTimeout("hide('"+eID+"')", 310);
}

function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}