var a;
var initSelectBoxes = false;
var selRim;
var selWidth;
var selHeight;

function TabSwitchInit(opts, sel) 
{
    var d = document;
    if(!d.getElementById) return;

    var init = null;
    for(var i = 2; i < opts.length; i++) {
        var link = d.getElementById(opts[i][1]);
        if(link) {
            link.onclick = _TabClick;
            link._tabs = opts;
            if(!init || sel == opts[i][0] || isFinite(sel) && (i - 2) == sel) init = link;
        }
    }
    if(init) init.onclick();
}

function _TabClick() 
{
    var d = document;
    if(!d.getElementById) return;

    if (!initSelectBoxes){	
    	fu();
	initSelectBoxes = true;
    }

    if (document.getElementById('trigger_0') && selRim && selWidth && selHeight){


	document.getElementById('trigger_0').firstChild.nodeValue = selWidth;
	document.getElementById('some_name_0').value = selWidth;

	document.getElementById('trigger_1').firstChild.nodeValue = selHeight;
	document.getElementById('some_name_1').value = selHeight;

	document.getElementById('trigger_2').firstChild.nodeValue = selRim;
	document.getElementById('some_name_2').value = selRim;

	refreshSelectBoxes();
	searchTyre();

	selRim = null;
	selWidth = null;
	selHeight = null;

    }	

    var opts = this._tabs;
    var minh = opts[1];

    var sel = (opts[0]?d.getElementById(opts[0]):null);

    for(var i = 2; i < opts.length; i++) {
        var div = d.getElementById(opts[i][0]);
        var link = d.getElementById(opts[i][1]);


        if(!div || !link) continue;

        if(this == link) {
            div.style.display = 'block';
           if (isIE6 == true){
                        pngfix(opts[i][0]);
            }

            if(opts[i][0] == 'divTab3') {runCarousseldivTab3();}
            if(opts[i][0] == 'divTab5') {runCarousseldivTab5();}
 
            link.parentNode.className = 'active';
            if(minh) {
                div.style.minHeight = minh;
                if(d.all) div.style.height = minh; // IE-Fix
            }
            if(sel) sel.value = div.id;
        }
		else{

            div.style.display = 'none';
            link.parentNode.className = '';
        }
    }

    return false;
}

function tyre(width, height, constuction, rim,load,speed,price,runonflat,design_1){

    this.width = width;
    this.height = height;
    this.constuction = constuction;
    this.rim = rim;
    this.load = load;
    this.speed = speed;
    this.price = price;
    this.runonflat = runonflat;
    this.design_1 = design_1;

}

function sortNumber(a,b){
	return a - b;
}

function pushObj(arr, param, isText){

	var exist = false;
	for (var l=0; l < arr.length; l++){	
		if (param == arr[l]){
			exist = true;
			break; 	 
		}
	}

	if (!exist && param.length > 0){	
 	   	arr.push(param);
		if (isText)
			arr.sort();

		else
			arr.sort(sortNumber);

	}

}

function fu() {

    var results = document.getElementById('resultsSandP');

    while (results.hasChildNodes()) {
        results.removeChild(results.firstChild);
    }

    for (var j = 0; j < a.length; j++) {

        var res = document.createElement("div");
        res.className = "res";
        var ul = document.createElement("ul");

        var li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].design_1));
        ul.appendChild(li);


        var li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].width));
        ul.appendChild(li);


        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].height));
        ul.appendChild(li);

        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].constuction));
        ul.appendChild(li);

        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].rim));
        ul.appendChild(li);

        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].load));
                if(a[j].load=='' || a[j].load==null){li.innerHTML='&nbsp';}
        ul.appendChild(li);

        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].speed));
        ul.appendChild(li);

        li = document.createElement("li");
        li.appendChild(document.createTextNode(a[j].price));
        ul.appendChild(li);


        res.appendChild(ul);
        results.appendChild(res);

    }

    var arr_width = new Array();
    var arr_height = new Array();
    var arr_rim = new Array();
    var arr_runonflat = new Array();
    var arr_speed = new Array();
    var arr_load = new Array();
    var arr_constuction = new Array();


    var allArr = new Array(arr_width, arr_height, arr_rim, arr_speed, arr_load, arr_constuction, arr_runonflat);

        for (var k = 0; k < a.length; k++) {

	    pushObj(arr_width, a[k].width);
	    pushObj(arr_height, a[k].height);
	    pushObj(arr_rim, a[k].rim);
	    pushObj(arr_runonflat, a[k].runonflat, true);
            pushObj(arr_speed, a[k].speed, true);
            pushObj(arr_load, a[k].load);
        pushObj(arr_constuction, a[k].constuction, true);


        }


	for (var l = 0; l < allArr.length; l++){
	
		var selBox = document.getElementById('some_name_' + l);
		if (!selBox || selBox.nodeName != 'SELECT')
			return;


		for (var j = 0; j < allArr[l].length; j++){

			var opt = document.createElement('option');
        		opt.text = allArr[l][j];
			opt.value = allArr[l][j];

			try {
                		selBox.add(opt, null);
            		} catch(ex) {
                		selBox.add(opt);
            		}

		}
	}



}

function searchTyre(){

	var results = document.getElementById('resultsSandP');

	while (results.hasChildNodes()) {
		results.removeChild(results.firstChild);
	}

	var width = document.getElementById('some_name_0').value;
	var height = document.getElementById('some_name_1').value;
	var rim = document.getElementById('some_name_2').value;
	var runonflat = document.getElementById('some_name_6').value;
	var speed = document.getElementById('some_name_3').value;
	var load = document.getElementById('some_name_4').value;
    var constuction = document.getElementById('some_name_5').value;

	for (var j = 0; j < a.length; j++)
		
		if ((!width || width == a[j].width)
                && (!height || height == a[j].height)
                && (!rim || rim == a[j].rim)
                && (!runonflat || runonflat == a[j].runonflat) 
                && (!speed || speed == a[j].speed)
                && (!load || load == a[j].load)
                && (!constuction || constuction == a[j].constuction)){

		
        		var res = document.createElement("div");
       			res.className = "res";
        		var ul = document.createElement("ul");

        		var li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].design_1));
        		ul.appendChild(li);

        		var li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].width));
        		ul.appendChild(li);

        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].height));
        		ul.appendChild(li);
	
        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].constuction));
        		ul.appendChild(li);

        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].rim));
        		ul.appendChild(li);

        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].load));
                            if(a[j].load=='' || a[j].load==null){li.innerHTML='&nbsp';}

        		ul.appendChild(li);

        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].speed));
        		ul.appendChild(li);

        		li = document.createElement("li");
        		li.appendChild(document.createTextNode(a[j].price));
        		ul.appendChild(li);


        		res.appendChild(ul);
        		results.appendChild(res);
		}
}

function refreshSelectBoxes(){

	for (var i=0; i<7; i++){
		var results = document.getElementById('replaceUL'+i);

	    	while (results.hasChildNodes()) {
			if (results.childNodes.length == 1)
				break;
			results.removeChild(results.lastChild);
		}
	}	

	var arr_width = new Array();    
	var arr_height = new Array();
	var arr_rim = new Array();
	var arr_runonflat = new Array();
	var arr_speed = new Array();
	var arr_load = new Array();
    	var arr_constuction = new Array();


    	var allArr = new Array(arr_width, arr_height, arr_rim, arr_speed, arr_load, arr_constuction, arr_runonflat);

	var width = document.getElementById('some_name_0').value;
	var height = document.getElementById('some_name_1').value;
	var rim = document.getElementById('some_name_2').value;
	var runonflat = document.getElementById('some_name_6').value;
	var speed = document.getElementById('some_name_3').value;
	var load = document.getElementById('some_name_4').value;
    	var constuction = document.getElementById('some_name_5').value;

	for (var i = 0; i < window.a.length; i++){

		if ((!width || width == window.a[i].width)
                && (!height || height == window.a[i].height)
                && (!rim || rim == window.a[i].rim)
                && (!runonflat || runonflat == window.a[i].runonflat)
                && (!speed || speed == window.a[i].speed)
                && (!load || load == window.a[i].load)
                && (!constuction || constuction == window.a[i].constuction)){

			pushObj(arr_width, window.a[i].width);
	    		pushObj(arr_height, window.a[i].height);
	    		pushObj(arr_rim, window.a[i].rim);
	    		pushObj(arr_runonflat, window.a[i].runonflat, true);
            		pushObj(arr_speed, window.a[i].speed, true);
            		pushObj(arr_load, window.a[i].load);
            pushObj(arr_constuction, window.a[i].constuction, true);

		}
	}

		for (var l=0; l < allArr.length; l++){

			for (var j=0; j < allArr[l].length; j++){

				var ul = document.getElementById('replaceUL' + l);
				var li = document.createElement('li');
				li.v = allArr[l][j];
				li.elm = document.getElementById('some_name_' + l);
				li.istrigger= document.getElementById('trigger_' + l);
				var a = document.createElement('a');
				a.href="#";
				li.onclick=function(){ 

					function ts_check(o,c){
	 					return new RegExp('\\b'+c+'\\b').test(o.className);
					}

					function ts_swapclass(o,c1,c2){
						var cn=o.className;
						o.className=!ts_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
					}

					function ts_addclass(o,c){
						if(!ts_check(o,c)){o.className+=o.className==''?c:' '+c;}
					}
					
					var ts_triggeron='activetrigger'; 		// class for the active trigger link
					var ts_triggeroff='trigger';			// class for the inactive trigger link
					var ts_dropdownclosed='dropdownhidden'; // closed dropdown
					var ts_dropdownopen='dropdownvisible';	// open dropdown

					this.elm.value=this.v;
					ts_swapclass(this.istrigger,ts_triggeron,ts_triggeroff);
					ts_swapclass(this.parentNode,ts_dropdownopen,ts_dropdownclosed);
					this.istrigger.firstChild.nodeValue=this.firstChild.firstChild.nodeValue;
					refreshSelectBoxes();
					searchTyre();
					return false;
				};

				a.appendChild(document.createTextNode(allArr[l][j]));
        			li.appendChild(a);
				ul.appendChild(li);
		}
	}
}
function TabSwitchInitReadMore(opts, sel) 
{
    var d = document;
    if(!d.getElementById) return;

    var init = null;
    for(var i = 2; i < opts.length; i++) {
        var link = d.getElementById(opts[i][1]);
        if(link) {
            link.onclick = _TabClickReadMore;
            link._tabs = opts;
            if(!init || sel == opts[i][0] || isFinite(sel) && (i - 2) == sel) init = link;
        }
    }
    if(init) init.onclick();
}

function _TabClickReadMore()
{
    var d = document;
    if(!d.getElementById) return;

    var opts = this._tabs;
    var minh = opts[1];

    var sel = (opts[0]?d.getElementById(opts[0]):null);

    var selectedDiv = null;

    for(var i = 2; i < opts.length; i++) {
        var div = d.getElementById(opts[i][0]);
        var link = d.getElementById(opts[i][1]);
        var linkTab = d.getElementById(opts[i][2]);


        if(!div || !link) continue;

        if(this == link) {
            selectedDiv = div;
            div.style.display = 'block';
            if (isIE6 == true){
                        pngfix(opts[i][0]);
            }

  
            if(opts[i][0] == 'divTab3') {runCarousseldivTab3();}
            if(opts[i][0] == 'divTab5') {runCarousseldivTab5();}

            linkTab.parentNode.className = 'active';

            
            if(minh) {
                div.style.minHeight = minh;
                if(d.all) div.style.height = minh; // IE-Fix
            }
            if(sel) sel.value = div.id;
        }
		else{
            if (selectedDiv != div) {
                div.style.display = 'none';
                linkTab.parentNode.className = '';
            }

        }
    }

    return false;
}

function openPrint(href, isNew) {


	var width = document.getElementById('some_name_0').value;

	if (width)	
		href += "&width="+width;

	var height = document.getElementById('some_name_1').value;

	if (height)	
		href += "&height="+height;

	var rim = document.getElementById('some_name_2').value;

	if (rim)	
		href += "&rim="+rim;

	var runonflat = document.getElementById('some_name_6').value;

	if (runonflat)	
		href += "&runonflat="+runonflat;

	var speed = document.getElementById('some_name_3').value;

	if (speed)	
		href += "&speed="+speed;

	var load = document.getElementById('some_name_4').value;

	if (load)	
		href += "&load="+load;

	var constuction = document.getElementById('some_name_5').value;

	if (constuction)	
		href += "&constuction="+constuction;

	if (isNew) {
		window.open(href, '', '');
	} else {
		window.location.href = href;
	}
}
