function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function toggleTab(tab) {
	var numTabs = 4;
	for (i=1; i<=numTabs; i++) {
		document.getElementById("tab" + i).className = '';
		document.getElementById("tab" + i + "content").style.display = 'none';
	}

	document.getElementById(tab).className = "sel";
	document.getElementById(tab + "content").style.display = 'block';
}
function CheckSubmit(evt,buttonName)
{
	var keyc = (typeof evt != 'undefined') ? evt.which : event.keyCode;
	if(typeof keyc == 'undefined') { keyc = event.keyCode; }
	if (keyc == 13)
	{
		document.getElementById(buttonName).focus();
		document.getElementById(buttonName).click();
	}
}

var tabName = 'nec';
var page = '1';
function changeProductTab(changeTo){
    tabName = changeTo;
    jQuery('.TabItemActive').removeClass().addClass('TabItemInactive');
    jQuery('#' + tabName).removeClass().addClass('TabItemActive');
    jQuery('.t5').hide();
    jQuery('#' + tabName + 'Table').show();
    jQuery('.pagers').hide();
    jQuery('#' + tabName + 'Pager').show('slow');
    changeProductPage('1');
}

var globalBrandID;
var globalTouchID;
var globalSunlightID;
var globalProtectiveID;
var jsonData;

function changeProductPage(newPage){
    page = newPage;
    jQuery('.holders').hide();
    jQuery('#' + tabName + page).show('slow');
}

function selectBrand(brandID){

    // Checking
    jQuery('#protectivePdf').html('None Available');
    jQuery('#protectiveText').html('None Selected');
    jQuery('#CollectProtective').val(''); 
    jQuery('.protectiveListActive input').attr("checked", false);
    jQuery('.protectiveListActive').hide();
    jQuery('.protectiveListInactive').show();
    globalProtectiveID = '';
    jQuery('#sunlightPdf').html('None Available');
    jQuery('#sunlightText').html('None Selected');
    jQuery('#CollectSunlight').val(''); 
    jQuery('.sunlightListActive input').attr("checked", false);
    jQuery('.sunlightListActive').hide();
    jQuery('.sunlightListInactive').show();
    globalSunlightID = '';
    jQuery('#touchText').html('None Selected');
    jQuery('#CollectTouch').val(''); 
    jQuery('.touchListActive input').attr("checked", false);
    jQuery('.touchListActive').hide();
    jQuery('.touchListInactive').show();
    globalTouchID = '';
    ////////////////////////////

    jQuery('#brandText').html(jQuery('#brandID'+brandID).attr('brand'));
    jQuery('#CollectBrand').val(jQuery('#brandID'+brandID).attr('brand')); 
    globalBrandID = brandID;
    compareTouchList();
}

function compareTouchList(){
    jQuery('.touchListActive').hide();
    jQuery('.touchListInactive').show();

    touchArray = new Array();
    for(x in jsonData){
        if(jsonData[x].Brand != undefined && jsonData[x].Brand.id != undefined && jsonData[x].Brand.id == globalBrandID){
            for(z in jsonData[x].Rule){
                if(jsonData[x].Rule[z].touch_id != undefined){
                    touchArray[jsonData[x].Rule[z].touch_id] = jsonData[x].Rule[z].touch_id;
                }
            }

            var imagePath = '';
            for(y in jsonData[x].Image){
                if(jsonData[x].Image[y] != undefined && jsonData[x].Image[y].type == 'normal'){
                    imagePath = jsonData[x].Image[y].path;
                }
            }

            var pdfPath = '';
            for(w in jsonData[x].Pdf){
                if(jsonData[x].Pdf[w] != undefined && jsonData[x].Pdf[w].type == 'normal'){
                    pdfPath = jsonData[x].Pdf[w].path;
                }
            }
        }
    }

    if(imagePath != ''){
        jQuery('#brandImage').attr('src', imagePath);
    }
    else{
        jQuery('#brandImage').attr('src', 'http://www.horizondisplay.com/image/ade7b680288bd421d9e3ae784b80cd017376c991.jpg');
    }

    if(pdfPath != ''){
        jQuery('#brandPdf').html('<a href="' + pdfPath + '" target="_blank">Download PDF</a>');
    }
    else{
        jQuery('#brandPdf').html('None Available');
    }

    for(a in touchArray){
        jQuery('#inactiveTouchID'+touchArray[a]).hide();
        jQuery('#activeTouchID'+touchArray[a]).show()
    }

}

function selectTouch(touchID){

    // Checking
    jQuery('#protectivePdf').html('None Available');
    jQuery('#protectiveText').html('None Selected');
    jQuery('#CollectProtective').val(''); 
    jQuery('.protectiveListActive input').attr("checked", false);
    jQuery('.protectiveListActive').hide();
    jQuery('.protectiveListInactive').show();
    globalProtectiveID = '';
    jQuery('#sunlightPdf').html('None Available');
    jQuery('#sunlightText').html('None Selected');
    jQuery('#CollectSunlight').val(''); 
    jQuery('.sunlightListActive input').attr("checked", false);
    jQuery('.sunlightListActive').hide();
    jQuery('.sunlightListInactive').show();
    globalSunlightID = '';
    ////////////////////////////

    jQuery('#touchText').html(jQuery('#activeTouchID'+touchID).attr('touch'));
    jQuery('#CollectTouch').val(jQuery('#activeTouchID'+touchID).attr('touch')); 
    globalTouchID = touchID;
    compareSunlightList();
}

function compareSunlightList(){
    jQuery('.sunlightListActive').hide();
    jQuery('.sunlightListInactive').show();

    sunlightArray = new Array();
    for(x in jsonData){
        if(jsonData[x].Brand != undefined && jsonData[x].Brand.id != undefined && jsonData[x].Brand.id == globalBrandID){
            for(z in jsonData[x].Rule){
                if(jsonData[x].Rule[z].touch_id != undefined && jsonData[x].Rule[z].touch_id == globalTouchID){
                    sunlightArray[jsonData[x].Rule[z].sunlight_id] = jsonData[x].Rule[z].sunlight_id;
                }
            }

            var imagePath = '';
            var normalPath = '';
            for(y in jsonData[x].Image){
                if(jsonData[x].Image[y] != undefined && jsonData[x].Image[y].type == 'dst' && globalTouchID == '2'){
                    imagePath = jsonData[x].Image[y].path;
                }
                else if(jsonData[x].Image[y] != undefined && jsonData[x].Image[y].type == 'optical' && globalTouchID == '3'){
                    imagePath = jsonData[x].Image[y].path;
                }
                if(jsonData[x].Image[y] != undefined && jsonData[x].Image[y].type == 'normal'){
                    normalPath = jsonData[x].Image[y].path;
                }
            }

            var pdfPath = '';
            var normalPdfPath = '';
            for(w in jsonData[x].Pdf){
                if(jsonData[x].Pdf[w] != undefined && jsonData[x].Pdf[w].type == 'dst' && globalTouchID == '2'){
                    pdfPath = jsonData[x].Pdf[w].path;
                }
                else if(jsonData[x].Pdf[w] != undefined && jsonData[x].Pdf[w].type == 'optical' && globalTouchID == '3'){
                    pdfPath = jsonData[x].Pdf[w].path;
                }
                if(jsonData[x].Pdf[w] != undefined && jsonData[x].Pdf[w].type == 'normal'){
                    normalPdfPath = jsonData[x].Pdf[w].path;
                }
            }
        }
    }

    if(imagePath != ''){
        jQuery('#brandImage').attr('src', imagePath);
    }
    else if(normalPath != ''){
        jQuery('#brandImage').attr('src', normalPath);
    }
    else{
        jQuery('#brandImage').attr('src', 'http://www.horizondisplay.com/image/ade7b680288bd421d9e3ae784b80cd017376c991.jpg');
    }

    if(pdfPath != ''){
        jQuery('#brandPdf').html('<a href="' + pdfPath + '" target="_blank">Download PDF</a>');
    }
    else if(normalPdfPath != ''){
        jQuery('#brandPdf').html('<a href="' + normalPdfPath + '" target="_blank">Download PDF</a>');
    }
    else{
        jQuery('#brandPdf').html('None Available');
    }

    for(a in sunlightArray){
        jQuery('#inactiveSunlightID'+sunlightArray[a]).hide();
        jQuery('#activeSunlightID'+sunlightArray[a]).show()
    }

}

function selectSunlight(sunlightID){

    // Checking
    jQuery('#protectivePdf').html('None Available');
    jQuery('#protectiveText').html('None Selected');
    jQuery('#CollectProtective').val(''); 
    jQuery('.protectiveListActive input').attr("checked", false);
    jQuery('.protectiveListActive').hide();
    jQuery('.protectiveListInactive').show();
    globalProtectiveID = '';
    ////////////////////////////

    jQuery('#sunlightText').html(jQuery('#activeSunlightID'+sunlightID).attr('sunlight'));
    jQuery('#CollectSunlight').val(jQuery('#activeSunlightID'+sunlightID).attr('sunlight')); 
    globalSunlightID = sunlightID;
    if(globalSunlightID != '6'){
        jQuery('#sunlightPdf').html('<a href="' + homePath + 'pdf/sunlight.pdf" target="_blank">Download PDF</a>');
    }
    compareProtectiveList();
}

function compareProtectiveList(){
    jQuery('.protectiveListActive').hide();
    jQuery('.protectiveListInactive').show();

    protectiveArray = new Array();
    for(x in jsonData){
        if(jsonData[x].Brand != undefined && jsonData[x].Brand.id != undefined && jsonData[x].Brand.id == globalBrandID){
            for(z in jsonData[x].Rule){
                if(jsonData[x].Rule[z].touch_id != undefined && jsonData[x].Rule[z].touch_id == globalTouchID && jsonData[x].Rule[z].sunlight_id != undefined && jsonData[x].Rule[z].sunlight_id == globalSunlightID){
                    protectiveArray[jsonData[x].Rule[z].protective_id] = jsonData[x].Rule[z].protective_id;
                }
            }
        }
    }

    for(a in protectiveArray){
        jQuery('#inactiveProtectiveID'+protectiveArray[a]).hide();
        jQuery('#activeProtectiveID'+protectiveArray[a]).show()
    }

}

function selectProtective(protectiveID){
    jQuery('#protectiveText').html(jQuery('#activeProtectiveID'+protectiveID).attr('protective'));
    jQuery('#CollectProtective').val(jQuery('#activeProtectiveID'+protectiveID).attr('protective')); 
    globalProtectiveID = protectiveID;
    jQuery('#protectivePdf').html('<a href="' + homePath + 'pdf/protective.pdf" target="_blank">Download PDF</a>');
}