﻿var _updatePanel = 'ctl00_ContentPlaceHolder1_SearchResultsUpdatePanel';
function ReplaceSpecialCharacters(text) {
    var retText = text.replace(/\"/g, 'dQuote').replace(/-/g,'SearchHyphen');
    //.replace(/'/g, "sQuote");
    retText = retText.replace(/[^a-zA-Z 0-9]+/g, '');
    retText = retText.replace(/dQuote/g, '"').replace(/SearchHyphen/g, '-'); //.replace(/sQuote/g, "'");
    retText=checkDQuotesinText(retText)    //added to remove the unmatched double quotes
    return (retText);
}
function ValidateSearchTxtBox(applicationPath) {
    var textBoxValue = ''; var detailUrl = '';
    if (document.getElementById('ctl00_ContentPlaceHolder1_searchUserControl_txtSearchTerm') != null) {
        document.getElementById('ctl00_ContentPlaceHolder1_browseUserControl_ddlMstrFrmt').value = 'choose a division';
        textBoxValue = CheckMultipleSpaces(ReplaceSpecialCharacters(document.getElementById('ctl00_ContentPlaceHolder1_searchUserControl_txtSearchTerm').value));
        if (textBoxValue.trim().length > 0) {
            document.getElementById('ctl00_ContentPlaceHolder1_searchUserControl_lblError').style.display = 'none';
            detailUrl = applicationPath+"/WebPages/SetSession.aspx?SearchText=" + escape(textBoxValue) + "&MhcOrWeb=mhc&Offset=0&LandingPage=1&GoButton=SearchGo&IsNavigatorCall=false&" +
            "NavigatorString=&NavigatorFilter=&DropDownListValue=&BrowseByValue=Product&SearchMode=NewSearch&FilterFactor=Normal-Search" +
            "&SearchOrBrowse=Search&SearchSource=Keyword&CommunitySelected=&Refresh=No&MF04DivisionName=null&WMNavValue="+null+"&WMNav2Value="+null;
            SetSession(detailUrl, applicationPath + "/SearchResults/Internal/" + textBoxValue, 'true');
            window.location = applicationPath + "/SearchResults/Internal/" + escape(textBoxValue);
        } else { ReturnFalse('ctl00_ContentPlaceHolder1_searchUserControl_') }
    }
    else if (document.getElementById('ctl00_ctl00_ContentPlaceHolder1_SearchContentPlaceHolder_searchUserControl_txtSearchTerm') != null) {
        document.getElementById('ctl00_ctl00_ContentPlaceHolder1_SearchContentPlaceHolder_browseUserControl_ddlMstrFrmt').value = 'choose a division';
        textBoxValue = CheckMultipleSpaces(ReplaceSpecialCharacters(document.getElementById('ctl00_ctl00_ContentPlaceHolder1_SearchContentPlaceHolder_searchUserControl_txtSearchTerm').value));
        if (textBoxValue.trim().length > 0) {
            document.getElementById('ctl00_ctl00_ContentPlaceHolder1_SearchContentPlaceHolder_searchUserControl_lblError').style.display = 'none';
            detailUrl = applicationPath+"/WebPages/SetSession.aspx?SearchText=" + escape(textBoxValue) + "&MhcOrWeb=mhc&Offset=0&LandingPage=1&GoButton=SearchGo&IsNavigatorCall=false&" +
        "NavigatorString=&NavigatorFilter=&DropDownListValue=&BrowseByValue=Product&SearchMode=NewSearch&FilterFactor=Normal-Search" +
        "&SearchOrBrowse=Search&SearchSource=Keyword&CommunitySelected=&Refresh=Yes&MF04DivisionName=null&WMNavValue=" + null + "&WMNav2Value=" + null;
            SetSession(detailUrl, applicationPath+"/SearchResults/Internal/" + textBoxValue, 'true');
            window.location = applicationPath+"/SearchResults/Internal/" + escape(textBoxValue);
        } else { ReturnFalse('ctl00_ctl00_ContentPlaceHolder1_SearchContentPlaceHolder_searchUserControl_') }
    }
    else if (document.getElementById('ctl00_ContentPlaceHolder1_searchDivSecUserControl_txtSearchTerm') != null) {
    document.getElementById('ctl00_ContentPlaceHolder1_browseDivSecUserControl_ddlMstrFrmt').value = 'choose a division';
    textBoxValue = CheckMultipleSpaces(ReplaceSpecialCharacters(document.getElementById('ctl00_ContentPlaceHolder1_searchDivSecUserControl_txtSearchTerm').value));
        if (textBoxValue.trim().length > 0) {
            detailUrl = applicationPath+"/WebPages/SetSession.aspx?SearchText=" + escape(textBoxValue) + "&MhcOrWeb=mhc&Offset=0&LandingPage=1&GoButton=SearchGo&IsNavigatorCall=false&" +
            "NavigatorString=&NavigatorFilter=&DropDownListValue=&BrowseByValue=Product&SearchMode=NewSearch&FilterFactor=Normal-Search" +
            "&SearchOrBrowse=Search&SearchSource=Keyword&CommunitySelected=&Refresh=Yes&MF04DivisionName=null&WMNavValue=" + null + "&WMNav2Value=" + null;
            SetSession(detailUrl, applicationPath+"/SearchResults/Internal/" + textBoxValue, 'true');
            window.location = applicationPath+"/SearchResults/Internal/" + escape(textBoxValue);
    } else { ReturnFalse('ctl00_ContentPlaceHolder1_searchDivSecUserControl_') }
    }
    else if (document.getElementById('ctl00_ContentPlaceHolder1_homeSearchUserControl_txtSearchTerm') != null) {
    document.getElementById('ctl00_ContentPlaceHolder1_homeBrowseUserControl_ddlMstrFrmt').value = 'choose a division';
    textBoxValue = CheckMultipleSpaces(ReplaceSpecialCharacters(document.getElementById('ctl00_ContentPlaceHolder1_homeSearchUserControl_txtSearchTerm').value));
        if (textBoxValue.trim().length > 0) {
            detailUrl = applicationPath+"/WebPages/SetSession.aspx?SearchText=" + escape(textBoxValue) + "&MhcOrWeb=mhc&Offset=0&LandingPage=1&GoButton=SearchGo&IsNavigatorCall=false&" +
            "NavigatorString=&NavigatorFilter=&DropDownListValue=&BrowseByValue=Product&SearchMode=NewSearch&FilterFactor=Normal-Search" +
            "&SearchOrBrowse=Search&SearchSource=Keyword&CommunitySelected=&Refresh=Yes&MF04DivisionName=null&WMNavValue=" + null + "&WMNav2Value=" + null;
            SetSession(detailUrl, applicationPath+"/SearchResults/Internal/" + textBoxValue, 'true');
            window.location = applicationPath+"/SearchResults/Internal/" + escape(textBoxValue);
        } else { ReturnFalse('ctl00_ContentPlaceHolder1_homeSearchUserControl_') }
    }
}
function ReturnFalse(errorBoxID) {
    var boxId = document.getElementById(errorBoxID + 'lblError');
    boxId.style.display = "";
    document.getElementById(errorBoxID + 'txtSearchTerm').focus();
    return false; 
}
function RRSetNavigatorSearchPref(navigatorFilter, url, navigatorString, prevFilter, breadCrumbNavValues, breadCrumbSearchValue,appPath) {    
    var wmNavValue = GetNavValue(breadCrumbNavValues.toUpperCase());
    var navigatorText = prevFilter + " pL" + navigatorFilter + ":DIC" + navigatorString + "DIC";
    var navigatorFilter = '';
    var url = '';
    if (breadCrumbSearchValue != '' && (breadCrumbSearchValue != undefined || breadCrumbSearchValue != 'undefined')) {
        navigatorFilter = breadCrumbSearchValue + "|" + breadCrumbNavValues;
    }
    else {
        navigatorFilter = breadCrumbNavValues;  //NavigatorFilter
    }
    
    url = appPath + "/WebPages/SetSession.aspx?SearchType=Related&IsNavigatorCall=true&NavigatorString=" + escape(navigatorText) +
    "&NavigatorFilter=" + escape(navigatorFilter) + "&Refresh=Yes&WMNavValue=" + escape(wmNavValue) + "&WMNav2Value=" + escape(breadCrumbNavValues) +
    "&LandingPage=1&Offset=0";
    SetSession(url, _updatePanel, 'false');       
}
function GetNavValue(navigatorText)
{
   if (navigatorText == "CAD/BIM" || navigatorText == "CATALOG" || navigatorText == "GALLERY"
                            || navigatorText == "SPECS" || navigatorText == "3D" || navigatorText == "GREEN")
        return ("content");
    else
        return ("category");
}
function findSpanPosition(spanID) {   
}
function CallIconClickedWebMetrics(iconClicked) {
    var wmUrl = document.getElementById('ctl00_ContentPlaceHolder1_hdnWebMetricsUrl').value;
    var wmParams = document.getElementById('ctl00_ContentPlaceHolder1_hdnWebMetricsParams').value;
    wmParams = wmParams + ",nav,nav2";
    var wmValues = document.getElementById('ctl00_ContentPlaceHolder1_hdnWebMetricsValue').value;
    wmValues = wmValues + ",grid," + iconClicked;
    InsertWebMetricsDataCommon(wmParams, wmValues, wmUrl, ",")
    var cpWmParams= document.getElementById('ctl00_ContentPlaceHolder1_hdnCPWebMetricsParams').value;
    cpWmParams+="|grid|"+iconClicked;
}
function CheckMultipleSpaces(searchKeyWord)
{
    var strSplitSearchKey = searchKeyWord.split(" ");
    var strNewSearchKey = '';
    var i;
    if(strSplitSearchKey.length>1)
    {
        for(i=0;i<strSplitSearchKey.length;i++)
        {
            if(strSplitSearchKey[i]!="")
            {
               if (i > 0)
                   strNewSearchKey = strNewSearchKey + " " + strSplitSearchKey[i];
               else
                   strNewSearchKey = strSplitSearchKey[i];
            } 
        }
        if (strNewSearchKey.substring(0, 1) == " ")
          strNewSearchKey = strNewSearchKey.substring(1, strNewSearchKey.length);
        else
            strNewSearchKey = strNewSearchKey;
        return strNewSearchKey;
    }
    else {
        return searchKeyWord;
    }
}
//Start:added to remove the unmatched double quotes
function checkDQuotesinText(searchText) {
    
    var bMatch = true;
    var bFirstTime = true;
    var finalText = '';
    for (var i = 0; i < searchText.length; i++) {
        if (searchText.charAt(i) == '"') {
            if (bFirstTime) {
                bMatch = false;
                bFirstTime = false;
            }
            else {
                bMatch = true;
                bFirstTime = true;
            }
        }

    }
    if (!bMatch) {
        var lastIndex = searchText.lastIndexOf('"');
        if (lastIndex >= 0) {
            if (lastIndex == searchText.length) {
                finalText = searchText.substring(0, searchText.lastIndexOf('"') - 1)
            }
            else {
                finalText = searchText.substring(0, searchText.lastIndexOf('"')) + searchText.substring(searchText.lastIndexOf('"') + 1);
            }
        }
        else {
            finalText = searchText;
        }
    }
    else {
        finalText = searchText;
    }

    return finalText;
}
//End:added to remove the unmatched double quotes


//New Method to validate TextBox For Sections Page


function ValidateSearchTxtBoxforSections(applicationPath) {

    //alert("dd");
    var textBoxValue = ''; var detailUrl = '';
    if (document.getElementById('txtSearchTerm') != null) {

        textBoxValue = CheckMultipleSpaces(ReplaceSpecialCharacters(document.getElementById('txtSearchTerm').value));
        //alert(textBoxValue);
        if (textBoxValue.trim().length > 0) {

            document.getElementById('txtSearchTerm').value = textBoxValue;
            //alert(document.getElementById('txtSearchTerm').value);
            document.getElementById('lblError').style.display = 'none';

        } else {
             
               ReturnFalse('')
        
             }
    
       }
   
     }
                                                       

