function popUpCompareProductResultsGlossary(url){
	$popUp(url,"Glossary");
}
function popUpGlossaryTermTitle(url){
	$popUp(url,"_new");
}
function popUpWeeklyAd(url, postalCodeElementId, UICulture ){
    var postalcode = $get(postalCodeElementId);
    var re = new RegExp(/([a-zA-Z][0-9][a-zA-Z]\s*-*\s*[0-9][a-zA-Z][0-9])|(\d{5}(\s*-*\s*\d{4})?)/);
    if (re.test(postalcode.value))
        popUpWeeklyAdForPostalCode(url, postalcode.value.replace("-", ""), UICulture);
}
function popUpStoreDetails(url){
	$popUp(url, "_new", { width: 1000, height: 600, scrollbars: true, resizable: true });
}
function popUpHelpDetails(url){
	$popUp(url, "_new", { width: 610, height: 600, scrollbars: true, resizable: true });
}
function popUpPrintablePage(url){
	$popUp(url,"_new");
}
function popUpWeeklyAdForPostalCode(url, postalCode, UICulture ){
	url=url+"?C="+postalCode;
	var Language="EN";
	switch (UICulture)
	{
		case("fr-CA"):
			Language="FR";			
			break;
	}
	url=url+"&L="+Language+"&T=ALL";
	$popUp(url, '_blank', { width: 940, height: 620, scrollbars: true, resizable: true });
	theForm.action="";
}
function OpenContactUsConfirmationPopUp(returnPage)
{
    if (returnPage != null && returnPage.length > 0)
    {
        window.location = returnPage; 
    }
    $popUp('/CompanyInfo/ContactUs/SubmitContactUsPopUp.aspx', null, { resizable: true });
}
function OpenCustomerFeedbackConfirmationPopUp(returnPage) {
    if (returnPage != null && returnPage.length > 0) {
        window.location = returnPage;
    }
    $popUp('SubmitFeedbackPopup.aspx', null, { width: 500, height: 280, resizable: true });
}
function OpenCustomerReviewRatingPopUp(url){
    $popUp(url, 'RateProduct', { width: 730, height: 600, scrollbars: true, resizable: true });
}
function OpenEmailFriendConfirmationPopUp(returnPage, EmailFriendType) {

    window.location = returnPage;
  $popUp('SubmitEmailPopUp.aspx?ET=' + encodeURIComponent(EmailFriendType ) , null, { height: 150, scrollbars: false });
}
function OpenPrivacyInquiryConfirmationPopUp(){
  $popUp('SubmitPrivacyFormPopUp.aspx',null);
}
function OpenQuestionConfirmationPopUp(){
  $popUp('/ResearchCentre/SubmitQuestionPopUp.aspx',null);
}
var popWinAdv
function openWindowAdv(page, width, height, scrollbars, titlebar, resizable, status, menubar, toolbar) {
    if (scrollbars) scrollbars = "yes";
    else scrollbars = "no";

    if (popWinAdv != null && !popWinAdv.closed) popWinAdv.close();
    popWinAdv = window.open(page, "PopUp", "width=" + width + ",height=" + height + ",dependent=yes,resizable,scrollbars=" + scrollbars + ",titlebar=" + titlebar + ",resizable=" + resizable + ",status=" + status + ",menubar=" + menubar + ",toolbar=" + toolbar);
}
function CountLeft(field, count, max){
  if (field.value.length > max)
	field.value = field.value.substring(0, max);
  count.value = field.value.length;
}
function GetValue(primaryElement,secondoryElement){
   secondoryElementDiv = $get("ContactUsSubCategoryTxt");
   if(MultiArray[primaryElement.selectedIndex].length > 1){
	  if (secondoryElementDiv != null && secondoryElementDiv != undefined)
		secondoryElementDiv.style.display="";  
   }
   else{
	  if (secondoryElementDiv != null && secondoryElementDiv != undefined)
		secondoryElementDiv.style.display="none"; 
   }		 
  if(secondoryElement.length > 0 ){
	 for(j = 0; j < secondoryElement.length ; j++){
		secondoryElement.remove(j);
		j-- ;
	 }
  }  
  for (i = 1 ; i < MultiArray[primaryElement.selectedIndex].length ; i++){
	 opt = document.createElement("option");
	 opt.text = MultiArray[primaryElement.selectedIndex][i] ;
	 secondoryElement.options.add(opt);
   }
}
function RefreshTabCustomerReviewOnSort(linkClicked, category, lnkDateId, lnkRatingId, sortOrderAsc, sortOrderDesc){
	var sortOrder;
	var url;
	var existingSortOrder;
	var sortByRequested=linkClicked.attributes["SortBy"].value;
	var existingPageSort=getQueryVariable('SortBy');
	existingSortOrder=getQueryVariable("SortOrder");
	if(linkClicked.id==lnkDateId || linkClicked.id==lnkRatingId){
		if(sortByRequested==existingPageSort){
			if(existingSortOrder==sortOrderAsc)
				sortOrder=sortOrderDesc;
			else
				sortOrder=sortOrderAsc;
		}
		else if(existingPageSort==null){
			sortOrder=sortOrderAsc;
		}
		else{
			sortOrder=sortOrderDesc;
		}
		url = window.location.pathname + '?category='+category + '&TabName=' + 'Tab_custReview' + '&SortBy=' + sortByRequested + '&SortOrder=' + sortOrder;
	}
	else{
		var pageNumber=linkClicked.attributes["PageNumber"].value;
		sortOrder = existingSortOrder ? existingSortOrder : sortOrderDesc;
		url = window.location.pathname + '?category='+category + '&TabName=' + 'Tab_custReview' + '&SortBy=' + sortByRequested + '&SortOrder=' + sortOrder + '&PageNumber=' + pageNumber;			
	}
	window.location.replace(url);
	return false;
}
function getQueryVariable(variable){
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
	var pair = vars[i].split("=");
	if (pair[0] == variable) {
	  return pair[1];
	}
  } 
}
function ExpandStore(divId, imageId){
	var divEle = $get(divId);
	var imageCtl = $get(imageId);
	if (divEle.style.display == "inline"){
		divEle.style.display = "none";
		if (imageCtl != null)
			imageCtl.src = "/images/plus.jpg";
	}
	else{
		divEle.style.display = "inline";
		if (imageCtl != null)
			imageCtl.src = "/images/common/pictures/minus.jpg";
	}
}
function SetHiddenVariable(){
	if(CheckBoxPrice.value == true)
		hndCheckBox.value = "1";   
}

function SavePollResults(pollButton) {
    var pollID = GetPollID('QuestionPanel', pollButton);
    if (pollID != null && pollID.length > 0) {
        var pollClientIDs = GetPollClientIDs(pollID);
        if (pollClientIDs != null && pollClientIDs.length > 0) {
            var selectedPollOption = GetSelectedPollOption(pollID, pollClientIDs[2]);
            var hiddenTotalVotesTextClientID = document.getElementById(pollClientIDs[3]);
            // Call the Web service method
            BestBuy.Mccp.Commerce.Channel.Presenter.SpecialFeatures.Polls.Services.PollWebService.SavePollResults(pollID,
                      selectedPollOption, pollClientIDs[0], pollClientIDs[1],
                      hiddenTotalVotesTextClientID.value, pollClientIDs[4],
                       SucceededCallback, FailedCallback);
        }
    }
}

function GetPollID(parentQuestionPanelSuffix, childObj) {
    var pollID = '';
    var parentObj = childObj.parentNode;
    var count = 1;
    while (count < 20) {
        if (parentObj.getAttribute('id') != null && parentObj.getAttribute('id').endsWith(parentQuestionPanelSuffix)) {
            break;
        }
        else {
            parentObj = parentObj.parentNode;
        }
        count++;
    }


    if (parentObj != null) {
        var pollLinks = parentObj.getElementsByTagName("a");
        if (pollLinks != null) {
            for (var i = 0; i < pollLinks.length; i++) {
                pollID = pollLinks[i].getAttribute("PollID");
                if (pollID != null && pollID.length > 0) {
                    break;
                }
            }
        }
    }

    return pollID;
}

function GetPollClientIDs(pollID) {
    var pollClientIDs = null;
    if (document.pollClientIDArray != null) {
        for (var i = 0; i < document.pollClientIDArray.length; i++) {
            var arrayItems = document.pollClientIDArray[i].split(";");
            if (arrayItems != null && arrayItems.length > 0) {
                if (arrayItems[0] == pollID) {
                    pollClientIDs = arrayItems[1].split("#");
                    break;
                }
                else continue;
            }
        }
    }
    return pollClientIDs;
}

function ShowPollResults(pollLink) {
    var pollID = GetPollID('QuestionPanel', pollLink); 
    if (pollID != null && pollID.length > 0) {
        var pollClientIDs = GetPollClientIDs(pollID);
        if (pollClientIDs != null && pollClientIDs.length > 0) {
            var selectedPollOption = GetSelectedPollOption(pollID, pollClientIDs[2]);
            var hiddenTotalVotesTextClientID = document.getElementById(pollClientIDs[3]);
            // Call the Web service method
            BestBuy.Mccp.Commerce.Channel.Presenter.SpecialFeatures.Polls.Services.PollWebService.ShowPollResults(pollID,
                    pollClientIDs[0], pollClientIDs[1], hiddenTotalVotesTextClientID.value, pollClientIDs[4],
                     SucceededCallback, FailedCallback);
        }
    }
}

function GetSelectedPollOption(pollID, clientIDForHiddenPollOptionFieldname) {
    var optionSelected = "";
    var hiddenPollOptionFieldnameClientID = document.getElementById(clientIDForHiddenPollOptionFieldname);
    if (hiddenPollOptionFieldnameClientID != null) {
        var pollOptions = document.getElementsByName(hiddenPollOptionFieldnameClientID.value + "_" + pollID);
        if (pollOptions != null && pollOptions.length > 0) {
            for (i = 0; i < pollOptions.length; i++) {
                if (pollOptions[i].checked == true)
                    optionSelected = pollOptions[i].value;
            }
        }
    }

    return optionSelected;
}


function ParseAndReplacePollResults(pollData, clientIDForDivPollAnswerPanel, clientIDForHiddenPollQuestionsAndCounts) {
    var totalPollCount = "TotalPollCount";
    var pollQuestionAndPercent = new Array();
    var parsedPollQuestionsAndOldCounts = new Array();
    var parsedTotalPollCount = 0;
    if (pollData != null) {
        var parsedPollData = pollData.split("##");
        if (parsedPollData != null && parsedPollData.length > 1) {
            parsedTotalPollCount = parsedPollData[0];
            for (i = 1; i < parsedPollData.length; i++) {
                var parsedText = parsedPollData[i].split("|");
                pollQuestionAndPercent[parsedText[0]] = parsedText[1];
            }
        }
    }

    var hiddenPollQuestionsAndCountsClientID = document.getElementById(clientIDForHiddenPollQuestionsAndCounts);
    if (hiddenPollQuestionsAndCountsClientID != null) {
        var parsedText = hiddenPollQuestionsAndCountsClientID.value.split("##");
        if (parsedText != null && parsedText.length > 1) {
            for (i = 0; i < parsedText.length; i++) {
                var innerParsedText = parsedText[i].split("|");
                parsedPollQuestionsAndOldCounts[innerParsedText[0]] = innerParsedText[1];
            }
        }
    }

    var divPollAnswerPanelClientID = document.getElementById(clientIDForDivPollAnswerPanel);
    if (divPollAnswerPanelClientID != null) {
        var loadTimeAnswerHTML = divPollAnswerPanelClientID.innerHTML;
        for (var i in pollQuestionAndPercent) {
            for (var j in parsedPollQuestionsAndOldCounts) {
                if ((i.toString() == j.toString()) && i.toString() != "") {
                    var oldValue = parsedPollQuestionsAndOldCounts[j];
                    var re = new RegExp(oldValue);
                    var firstReplacementHTML = loadTimeAnswerHTML.replace(re, pollQuestionAndPercent[i]);
                    var secondReplacementHTML = firstReplacementHTML.replace(re, pollQuestionAndPercent[i]);
                    break;
                }
            }
            loadTimeAnswerHTML = secondReplacementHTML;
        }
        var newAnswerHTML = loadTimeAnswerHTML.replace(totalPollCount, parsedTotalPollCount);
        divPollAnswerPanelClientID.innerHTML = newAnswerHTML;
    }
}


// This is the callback function that processes the [PollResults- type returned] from the Web service.
function SucceededCallback(result) {
     // Read the values returned by the Web service.
    var pollResults = new BestBuy.Mccp.Commerce.Channel.Presenter.SpecialFeatures.Polls.Services.PollResults();
    pollResults = result;
    if (pollResults.errorCode == 0 && pollResults != null) {
        var divPollQuestionPanelClientID = document.getElementById(result.clientIDForDivPollQuestionPanel);
        var divPollAnswerPanelClientID = document.getElementById(result.clientIDForDivPollAnswerPanel);
        if (divPollAnswerPanelClientID != null) {
            var parsedTotalPollCount = 0;
            if (pollResults.pollData != null) {
                var parsedPollData = pollResults.pollData.split("##");
                if (parsedPollData != null && parsedPollData.length > 1) {
                    parsedTotalPollCount = parsedPollData[0];
                }
            }

            if (parsedTotalPollCount == 0) {
                if (pollResults.pollId != null) {
                    var pollClientIDs = GetPollClientIDs(pollResults.pollId);

                    if (pollClientIDs != null) {
                        if (pollClientIDs[6] != null) {
                            var divQuestion = document.getElementById(pollClientIDs[6]);
                            divQuestion.style.display = "none";
                        }
                        if (pollClientIDs[0] != null) {
                            var divQuestionPanelClientID = document.getElementById(pollClientIDs[0]);
                            divQuestionPanelClientID.style.display = "none";
                        }
                        if (pollClientIDs[5] != null) {
                            var divNoPollResultClientID = document.getElementById(pollClientIDs[5]);
                            divNoPollResultClientID.style.display = "block";
                        }
                    }
                }
            }
            else {
                ParseAndReplacePollResults(pollResults.pollData, result.clientIDForDivPollAnswerPanel, result.clientIDForHiddenPollQuestionsAndCounts);
                divPollAnswerPanelClientID.style.display = "block";
            }
            divPollQuestionPanelClientID.style.display = "none";
        }
    }
}

// Callback function invoked on failure of the Web service methods.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) 
    {
        var lblErrorMessageClientID = document.getElementById(document.clientIDForlblErrorMessage);  
        lblErrorMessageClientID.innerHTML = "An error occurred: " +   error.get_message();
    }
}


 
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

function ShowQuestionPanel(lnkNoPollResult) {
    var pollID = GetPollID('QuestionPanel', lnkNoPollResult);
    if (pollID != null && pollID.length > 0) {
        var pollClientIDs = GetPollClientIDs(pollID);
        if (pollClientIDs != null) {
            if (pollClientIDs[5] != null) {
                var divNoPollResultClientID = document.getElementById(pollClientIDs[5]);
                divNoPollResultClientID.style.display = "none";
            }
            if (pollClientIDs[6] != null) {
                var divQuestion = document.getElementById(pollClientIDs[6]);
                divQuestion.style.display = "block";
            }
            if (pollClientIDs[0] != null) {
                var divPollQuestionPanelClientID = document.getElementById(pollClientIDs[0]);
                divPollQuestionPanelClientID.style.display = "block";
            }
        }
    }
}