function GotoPage(dropDown) {
    var selectedVal = $("#" + dropDown).val();
    if (selectedVal != '0') window.location = selectedVal;
}

function openQuickClip(page, width, height)
{
    window.open(page,"quicClip", "width=" + width +", height=" + height + ",top=0,left=0");
}

function openWindow(page, width, height)
{
    window.open(page,"Dealer", "directory=no, location=no, resizable=no, status=no, scrollbars=no, toolbar=no, width=" + width +", height=" + height + ",top=0,left=0");
}

function displayText(c, text)
{
    document.getElementById(c).innerText = text;
}

var varShowHideProductDescriptions = false;

function showHideProductDescriptions(p)
{
    if(varShowHideProductDescriptions)
        p.src = hideButtonImage;//'/images/toshiba/button_hide_descriptions.gif';
    else
        p.src = showButtonImage;//'/images/toshiba/button_show_descriptions.gif';

    var root = document.getElementById("contentContainer");
    var elements = root.getElementsByTagName("div");
    
    for(i=0; i<elements.length;i++)
    {
        if(elements[i].id.search("divProductDescription") == -1)
            continue;
        
        elements[i].style.display = varShowHideProductDescriptions ? 'inline' : 'none';
    }
        
    varShowHideProductDescriptions = !varShowHideProductDescriptions;    
}

// --------------------------------------------
// tech tag and comparison widget stuff
// --------------------------------------------

$(document).ready(function() {
    var _Bdy = document.body || document.documentElement;
    _Bdy.onmouseover = doOverOut;
    _Bdy.onmouseout = doOverOut;
    _Bdy.onclick = doOverOut;

    if (typeof (isProductSelector) != "undefined") {
        if (isProductSelector) {
            InitSearch();
            DisableCheckboxes();
        }
    }
});

function EndRequestHandler()
{
   var title = document.getElementsByTagName("title");
   document.title = title[0].innerHTML;
   alert(document.title);
}


// event controllers
function doOverOut(e)
{
    var e = e || window.event;
    var tgt = e.target || e.srcElement;
    
    if (tgt && tgt.className && /\bTechTag\b/.test(tgt.className))
    {
        
        if ('mouseover' == e.type) mOver(tgt, e);
        if ('mouseout' == e.type) mOut(tgt);
    }
    
    if (tgt.tagName.toLowerCase() == 'a' )
    {
        if (tgt.getAttribute('Compare') && !tgt.disabled)
        {
            if ('click' == e.type)
            {
                addToCompareWidget(tgt, e);
            }
        }
    }
   
    if (tgt.tagName.toLowerCase() == 'input' && tgt.type == 'checkbox')
    {
        var tparent = tgt.parentNode;
        if (tparent && tparent.className && /\bTechTag\b/.test(tparent.className))
        {
            if ('mouseover' == e.type) mOver(tgt, e);
            if ('mouseout' == e.type) mOut(tgt);
        }
        if ('click' == e.type)
        {
             DisableCheckboxes();
        }
    }
}

function addToCompareWidget(tgt, e)
{
    var id = "";
    if (tgt.tagName.toLowerCase() == 'a')
        id = tgt.getAttribute('Compare');
    else
        id = tgt.parentNode.getAttribute('Compare');

    if (tgt.innerHTML == addToWidgetText)
    {
        var languageBranch = $get(langBranch);
        
        tgt.innerHTML = removeFromWidgetText;
        tgt.name = "un" + tgt.name;
        ToshibaCPD.Services.ToshibaClientService.AddProductToWidget(id, languageBranch.value, OnAddProductComplete, OnError);
        DisableControls(true);
    }
    else if (tgt.innerHTML == removeFromWidgetText)
    {
        var languageBranch = $get(langBranch);
        
        tgt.innerHTML = addToWidgetText;
        tgt.name = tgt.name.replace("un", "");
        ToshibaCPD.Services.ToshibaClientService.RemoveProductFromWidget(id, languageBranch.value, OnAddProductComplete, OnError);
        DisableControls(true);
    }
}

function OnAddProductComplete(result, userContext)
{
    var count = result;
   __doPostBack(widget, '');
    
    DisableControls(false);
    
    if (count == 4)
        DisableUncheckedLinks(true);
    else 
        DisableUncheckedLinks(false);
}

function UncheckLink(id)
{
    var linkList = document.getElementsByTagName("a");
    for (var i=0; i<linkList.length; i++)
    {
        if (linkList[i].getAttribute('Compare') == id && linkList[i].innerHTML == removeFromWidgetText)
        {
            linkList[i].innerHTML = addToWidgetText;
        }
    }
    DisableUncheckedLinks(false);
}

function DisableUncheckedLinks(disable)
{
    var linkList = document.getElementsByTagName("a");
    
    for (var i=0; i<linkList.length; i++)
    {
        if (linkList[i].getAttribute('Compare') && linkList[i].innerHTML == addToWidgetText)
        {
            disableAnchor(linkList[i], disable);
        }
    }
}

function mOut(el)
{
    var techDiv = $get('techDiv');
    techDiv.style.display = 'none';
}

function mOver(el, ev)
{
    var tag = $(el).attr('techTag'); 
    if (!tag)
        tag = $(el.parentNode).attr('techTag');

    ToshibaCPD.Services.ToshibaClientService.GetTechnologyDescription(tag, $("#" + langBranch).val(), OnComplete, OnError);
    
    var techDiv = $get('techDiv');
    var desc = $get('desc');
    desc.innerHTML = '<small><strong>Loading...<strong></small>';
    techDiv.style.position = 'absolute';
    techDiv.style.top = (ev.clientY + document.documentElement.scrollTop+5) + 'px';
    techDiv.style.left = ev.clientX + 5 + 'px';
    
    var techDivWidth = $("#techDiv").width();
    var techDivHeight = $("#techDiv").height();
    
    if ((ev.clientY + document.documentElement.scrollTop + 5) + techDivHeight > $(document).height())
    {
        techDiv.style.top = (ev.clientY + document.documentElement.scrollTop - techDivHeight) + 'px';
    }
        
    if ((ev.clientX + 5) + techDivWidth > $(document).width())
    {
        techDiv.style.left = ev.clientX - techDivWidth + 'px';
    }
    
    techDiv.style.display = 'block';
}

function OnError(result)
{
    var techDiv = $get('techDiv');
    var desc = $get('desc');
    desc.innerHTML = result.get_message();
}

function OnComplete(result, userContext)
{
    var techDiv = $get('techDiv');
    var desc = $get('desc');
    desc.innerHTML = result[1];
}

function DisableControls(disable)
{
    var chkList = document.getElementsByTagName("a");
    
    for (var i=0; i<chkList.length; i++)
    {
        if (chkList[i].getAttribute('Compare'))
        {
            disableAnchor(chkList[i], disable);
        }
    }
}


// ----------------------------
// product selector scripts
// ----------------------------

function getQueryParameter( name )
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if( results == null )    
        return "";  
    else    
        return results[1];
}

// get product type from query string
function getProductType() {

    var type = 'TV';
    if (getQueryParameter('type') == 'DVD')
        type = 'DVD';

    //alert($("#" + productType).val());
    if (type == 'TV' && $("#" + productType).val() != null)
        type = $("#" + productType).val();
    
    
    return type;
}

function showProducts() {
    window.location = "/LCD-DVD-product-selector-tool/?type=" + getProductType() + (isAdvancedSearch ? "&advanced=true" : "");
}

function showAdvanced(resetAndStore) {

    if (!isAdvancedSearch) {
        $("#" + tblBasic).hide();
        $("#" + tblAdvanced).show();
        isAdvancedSearch = true;
    }
    else {
        $("#" + tblBasic).show();
        $("#" + tblAdvanced).hide();
        isAdvancedSearch = false;
    }

    $(".linkAdvanced").each(function() {
        if (isAdvancedSearch)
            $(this).html(labelBasicSearch);
        else
            $(this).html(labelAdvancedSearch);
    });

    if (resetAndStore) {
        // create cookie
        createCookie("ToshibaProductSelectorType" + getProductType(), isAdvancedSearch, 1);
        ResetSearch();
    }
}

// initialize search
function InitSearch() {

    // get the type of the search from query string
    var type = getProductType();
    
    // initialize search type
    var valueSearchType = readCookie("ToshibaProductSelectorType" + type);

    //isAdvancedSearch = valueSearchType == "true" ? true : false;
    if (valueSearchType == "true") {
        showAdvanced(false);
    }
        
    // read cookie which stores the initial checked boxes
    var value = readCookie("ToshibaProductSelector"+type);
    if (value == null) 
        return false;

    // tick checkboxes
    $("input[type=checkbox]").each(function() {
        if (value.indexOf($(this.parentNode).attr("ProdID") + ",") > -1) {
            this.checked = true;
        }
    });

    return true;
}


function DisableCheckboxes() {

    var type = getProductType();
    var loader = $get('loader');
    loader.style.display = '';
    
    var checkedBoxes = '';
    var uncheckedBoxes = '';
    uncheckedelements = null;

    // store check box values
    $("input[type=checkbox]").each(function() {
        var prodID = $(this.parentNode).attr("ProdID");
        if (this.checked) {
            checkedBoxes += prodID + ",";
        }
        else {
            uncheckedBoxes += prodID + ',';
        }
    });
    
    createCookie("ToshibaProductSelector" + type, checkedBoxes, 1);
    
    if (checkedBoxes == '')
    {
        EnableCheckBoxes();
        $("#productCounter").html("0");

        if (!showOnlySearch) {
            $("#" + checkeds).val("");
            __doPostBack(upProduct, '');
        }
        
        loader.style.display = 'none';
        
        return;
    }
    
    ToshibaCPD.Services.ToshibaClientService.GetDisabledCheckboxes(checkedBoxes, 
                                                                uncheckedBoxes, 
                                                                type, $("#"+langBranch).val(), 
                                                                OnDisabledComplete, 
                                                                OnError);
}

// the disabled checkboxes has been downloaded, lets process the data
function OnDisabledComplete(result, userContext)
{
    var checkedBoxes = '';

    $("input[type=checkbox]").each(function() {    
        var prodID = $(this.parentNode).attr("ProdID");
        if (this.checked) {
            checkedBoxes += prodID + ",";
        }

        for (j = 0; j < result.length - 1; j++) {

            if (prodID == result[j]) {
                this.disabled = true;
                break;
            }
            else {
                this.disabled = false;
            }
        }
    });
    
    if (checkedBoxes == '') 
        EnableCheckBoxes();

    // updating product counter and product list
    $("#productCounter").html(checkedBoxes == "" ? "0" : result[result.length - 1]);

    if (!showOnlySearch) {
        $("#" + checkeds).val(checkedBoxes);
        __doPostBack(upProduct, '');
    }
    
    var loader = $get('loader');
    loader.style.display = 'none';
}

// enable every checkboxes 
function EnableCheckBoxes() {
    $("input[type=checkbox]").each(function() {
        this.disabled = false;
    });
}

// reset the search box
function ResetSearch() {

    $("input[type=checkbox]").each(function() {
        this.checked = false;
        this.disabled = false;
    });


    $("ctl00_cphMainRegion_divHideDescription").hide();
    $("#productCounter").html("0");
    $("#" + checkeds).val("");

    eraseCookie("ToshibaProductSelector" + getProductType());

    if (!showOnlySearch)
        __doPostBack(upProduct, '');
}

//------------------------
// cookie control
// -----------------------

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

function disableAnchor(obj, disable)
{
  if(disable)
  {
      obj.style.display = 'none';
  }
  else
  {
     obj.style.display = '';
  }
}