﻿
    // closes popup shown via serverside code informing user if the Item link no longer exists 
    function CloseModalNoLinkPopup()
    {
	    ShowHideObj("NoItemLinkPopup","hidden");
	    ShowHideObj("ModalBg","hidden");
    }
    
    // user help messages
    var NotSupportedMsg = "<br /><br />Do not type 'AND' / 'OR' between your Keywords or enter single/double quotes around your Keywords.";
    
    var TypeHelpTip = "<div class='ItemInfoCloseBtnWrapper'></div><h6>Product Type / Category Tip</h6><p>Enter the type of Products(s) you are looking for here. You can search for muiltiple Products by seperating each type of Product with a comma. For example entering phone,laptop,computer will retrieve ALL Products matching phone and laptop and computer." + NotSupportedMsg + "</p>";
    
    var BrandHelpTip = "<div class='ItemInfoCloseBtnWrapper'></div><h6>Make / Brand Name Tip</h6><p>Enter the Make(s) / Brand Name(s) of the Product you are looking for here e.g. Apple. You can search on muiltiple Makes / Brand Names by seperating each Make / Brand Name with a comma. For example entering Apple,Microsoft,Sony will retrieve ALL Products from Sony and Microsoft and Apple.<br /><br />Sellers may post products using abreviations or variations of a Make/ Brand Name e.g. MS for Microsoft or Mac instead of Apple - it is therefore usful to also included variations of a Make / Brand Name in your Search Criteria." + NotSupportedMsg + "</p>";
    
    var ModelHelpTip = "<div class='ItemInfoCloseBtnWrapper'></div><h6>Model Name / Number Tip</h6><p>Enter the Model Name / Number of the Product you are looking for here e.g. MacBook Pro. You can search for muiltiple Model Names / Numbers by seperating each Model Name / Number with a comma e.g. Vaio 17 inch, Vaio 15 inch, VGN-FZ485U/B, FZ will retrieve ALL Vaio 17 inch and Vaio 15 inch models, and model number VGN-FZ485U/B and any laptop of FZ series." + NotSupportedMsg + "</p>";
    
    var LocationHelpTip = "<div class='ItemInfoCloseBtnWrapper'></div><h6>Location Tip</h6><p>If required, enter the selles Location (Town/City/Area) or your preferred uplift location here. You can search on muiltiple Locations by seperating each Location with a comma. For example, if you are located in Edinburgh, you may only be interested in areas in and near Edinburgh and the Lothian region e.g. Edinburgh,Glasgow,Stirling,Lothian.<br /><br />Leaving Location blank will retrieve ALL Products for sale irrespective of the sellers location." + NotSupportedMsg + "</p>";
	
	
  
    // global variables related to display of Item
    
    var CurrentlyViewingItem = "";
    var CurrentItemId = "";
    var ItemId = 0; // default 0 = no ids yet set
    var PopupH = 500; // default 500
    var CachedItemIdsArray = new Array();  // ids of all divs added to viewed list
    
    var SearchInProgress = false;
    
    // Apply btns image preload
    var applyBtnOver = new Image();
    var applyBtn= new Image();
    applyBtn.src = "Navigation/NavButtonApplyForItem.png" ;
    applyBtnOver.src = "Navigation/NavButtonApplyForItemOver.png";
 
 
 // ================== RUN Item SEARCH / DISPLAY SEARCH RESULTS ================================
    
    function SearchBtnClick()
    {
        GoToPage = 1;
        ItemsPerPage=17;
        document.getElementById("txtItemsPerPage").value = ItemsPerPage;
        FetchSearchResults();
    }
    
    
    function AjaxGetData()
    {
        // get paging parameters via calls to get functions defined in Scripts/Ajax/AjaxGridAndPager.js
        var paramSortExpression = _getSortExpression();
//        var productType = document.getElementById("txtProductType").value.Trim();
//        var brandName = document.getElementById("txtBrandName").value.Trim();
//        var modelName = document.getElementById("txtModelName").value.Trim();
//       // var description = document.getElementById("txtDescription").value.Trim();
//        var location = document.getElementById("txtLocation").value.Trim();
//        
        var productType = getSearchInputValue("txtProductType");
        var brandName = getSearchInputValue("txtBrandName");
        var modelName = getSearchInputValue("txtModelName");
       // var description = document.getElementById("txtDescription").value.Trim();
        var location = getSearchInputValue("txtLocation");
        
        
         var startPageNumber = GoToPage;
        var pageSize = 17;
        
//        if( countryListSelectedValue == "0" )
//        {
//            countryListSelectedValue = "";
//        }
        
        ShowHideAnimatedMsgBox("visible","Searching Items");
	    //DisplayHideObj("SearchingMessage","block");//new
        //DisableEnableSearchElements(true);  // new
        SearchInProgress = true;
        PageMethods.GetSearchResults(productType, brandName, modelName, location, paramSortExpression, GoToPage, pageSize, ItemSearchCompletedHandler, SearchRequestError, 'elot Search');
    }
    
    
    function ItemSearchCompletedHandler(responseHtml,context)
    {
        SearchInProgress = false;
       // DisableEnableSearchElements(false);//new
	   // DisplayHideObj("CancelSearchInfo","none");//new
	   // DisplayHideObj("SearchingMessage","none");//new
        ShowHideAnimatedMsgBox("hidden","");
        document.getElementById("IntroTextWrapper").style.display = "none";
        document.getElementById("SearchResultsWrapper").innerHTML = responseHtml;
        document.getElementById("SearchResultsWrapper").style.display = "block";
    }
    
    // not used at moment - will be used if we dont use modal bg during searches
    function CancelSearch()
    {
        SearchInProgress = false;
        DisableEnableSearchElements(false);
        DisplayHideObj("SearchingMessage","none");
    }
    
    
// ====================== SHOW Item INFO POPUP RELATED CODE ============================

    function ShowItem(id, itemDetailsDivId, itemDescription, addToList)
    {
        CurrentItemId = itemDetailsDivId;
        ItemId = id;
        CurrentlyViewingItem = itemDescription;
        document.title = "IFA Elot Item : " + CurrentlyViewingItem;
        
        if( HashBookmarkSupport == true )
	    {
		    window.location.hash = id;
	    }   
        
        document.getElementById("ItemDescriptionWrapper").innerHTML = "";
       
        HorizontallyCenterDiv("ViewItemPopup",674,20)
        ShowHideObj("ItemPopupModalBg","visible");
        
        // calculate height as Available height - 60px;
        GetAvailableDisplayArea();
        PopupH = DisplayAreaHeight - 80;
       // alert("pop h = " + PopupH);
        DisplayHideObj("ItemDescriptionScrollWrapper","none");
        
        // size, add text and display
        var ItemInfoHtml = document.getElementById(CurrentItemId).innerHTML;
        
        // SET ItemDescriptionScrollWrapper height
        var headerBarOffsetHeight = document.getElementById("ViewItemHeaderBar").offsetHeight + 33; // (+33 for top row corner)
        var scrollH = PopupH - headerBarOffsetHeight;

    //alert("pop h = " + PopupH + " scroll h = " + scrollH);
        document.getElementById("ItemDescriptionScrollWrapper").style.height = scrollH + "px";
        document.getElementById("ItemPreviewMenuWrapper").style.height = scrollH + "px";  
        DisplayHideObj("ItemDescriptionScrollWrapper","block");
        document.getElementById("ItemDescriptionWrapper").innerHTML = ItemInfoHtml; 
    
        SetPopupYpos("ViewItemPopup");
        
        if(IsLessIE7 == true)
            { ShowHidePageLists("hidden"); }
            
        ShowHideObj("ViewItemPopup","visible");
        
     // add Item to viewed Items list   
        if(addToList == true)
        {
            // CHECK if item already cached - if so do not add div id html again!
            var isCachedItem = isItemCached();
            if(isCachedItem == false)
            {
                // add item id to cached item ids
                CachedItemIdsArray.push(ItemId);
                // copy Item info div with new div id into cache area
                 var cachedDivId = "CachedItem" + ItemId;
                 var cachedDiv = '<div id="' + cachedDivId + '" class="cachedDiv">' + ItemInfoHtml + '</div>';
            
                 document.getElementById("ItemCachingWrapper").innerHTML += cachedDiv;
                var nextOptionIndex = document.getElementById("ddlViewedItems").length;
                var listText = CurrentlyViewingItem;
                var opt =  new Option(listText,ItemId);
                document.getElementById("ddlViewedItems").options[nextOptionIndex] = opt;
            }
           // else  // TESTING
           // { alert("already cached item id = " + ItemId + " NOT ADDED"); }
        }
    }
    
    
   function isItemCached()
   {
        var isAlreadyCached = false;
        var cacheArrayLength = CachedItemIdsArray.length;
        for(i=0; i < cacheArrayLength; i++)
        {
            if(CachedItemIdsArray[i] == ItemId)
            {
                isAlreadyCached = true;
            }
        }
        return isAlreadyCached;
   }
   
    
    function CloseItemPopup()
    {
            ShowHideObj("ViewItemPopup","hidden");
            ShowHideObj("ItemPopupModalBg","hidden");
            if(IsLessIE7 == true)
            { ShowHidePageLists("visible"); }
    }
    

// ================== TOGGLE CHECKBOX SELECTION =================================

    function SelectCheckbox( selectObj, deselectObj1, deselectObj2 )
	{
		document.getElementById( selectObj ).checked = true;
		document.getElementById( deselectObj1 ).checked = false;
		document.getElementById( deselectObj2 ).checked = false;
	}
    

// =================== ACCOMODATE PAGE SCROLL OFFSET =============================
    
    // set all (to accomodate any of them currently being displayed) popup style divs top pos on scroll
    
    function SetPopupDivsYpos()
    {
        SetPopupYpos("ViewItemPopup"); // function defined in universal.js
		SetSendToYpos();  // defined in SendToFRiend.js
    }
    
  window.onscroll = SetPopupDivsYpos;
 
 
 
 // =================== HIDE MODAL BG AND ANY POPUP DIVS =========================
 
 function CloseModalBgAndAllPopupDivs()
 {
        ShowHideObj("ModalBg","hidden");
        CloseItemPopupModalBg(); 
        OpenCloseSavePopup('hidden');
        if(IsLessIE7 == true)
        { ShowHidePageLists("visible"); }
    // add additional divs e.g. send to friend etc here
 }
 
 
 function CloseItemPopupModalBg()
 {
        ShowHideObj("ItemPopupModalBg","hidden");
        //if( HashBookmarkSupport == true ){window.location.hash = "";} 
        ShowHideObj("ViewItemPopup","hidden"); // function defined in universal.js
        if(IsLessIE7 == true)
        { ShowHidePageLists("visible"); }
 }
 

 
 // =================== AJAX MESSAGE BAR RELATED CODE ==============================
 
 function autoCloseAjaxMsgBar()
 {
    FadeOpacity("AjaxUpdateMessageBar",100,0,25,true);
 }
 


// ====================== VIEWED ItemS LIST INDEX CHANGED ===========================

function ViewedItemsListChange()
{
    var listRef = document.getElementById("ddlViewedItems");
    var indx = listRef.selectedIndex;
    if(indx > 0)
    {
        var itemId = listRef.options[indx].value;
        var itemDesc = listRef.options[indx].text;
        var divId = "CachedItem"+itemId;
        listRef.selectedIndex = 0;
        ShowItem(itemId, divId, itemDesc, false);
    }
}


// ======================== SAVE SEARCH CRITERIA TO COOKIE ON EXIT ==========================


// ------------------ reset search criteria controls ---------------

function ResetSearchCriteria()
{
    document.getElementById("txtProductType").value = "";
     document.getElementById("txtBrandName").value = "";
     document.getElementById("txtModelName").value = "";
     // document.getElementById("txtDescription").value = "";
     document.getElementById("txtLocation").value = "";
}


// --------------- save and restore page state --------------------

function SaveSearchToCookie()
{
    if(SaveSearchCriteriaToCookie == true)
    {
        var productType = getSearchInputValue("txtProductType");
        var brandName = getSearchInputValue("txtBrandName");
        var modelName = getSearchInputValue("txtModelName");
       // var description = document.getElementById("txtDescription").value.Trim();
        var location = getSearchInputValue("txtLocation");
        var helpText =document.getElementById("HelpBalloonOnOffText").innerHTML;
        
        // also add a sort by list - or do sorting via grid?
        var cookieString = productType + "^" + brandName + "^" + modelName + "^" + location + "^" + helpText;
          //  alert(cookieString);
        //SetCookieWithDate("ElotItemSearchCookie", cookieString,1);
        SetCookie("ElotItemSearchCookie",cookieString); // expire on close while developing
    }
}

function getSearchInputValue(objId)
{
    var inputValue = document.getElementById(objId).value.Trim();
    if(inputValue.indexOf("e.g.") != -1)
    { inputValue = ""; }
    return inputValue;
}

window.onbeforeunload = SaveSearchToCookie;

//alert("Get Cookie VALUE = " + GetCookie("ItemSearchCookie"));



// =========================== EMAIL item LINK =============================

function SendPageLink(friendsEmail,sendersEmail,linkUrl)
{
    pageLinkUrl = "http://ifa-voice.com/ElotSearch.aspx?id=" + ItemId;
    PageMethods.EmailItemLink(friendsEmail,sendersEmail,pageLinkUrl,CurrentlyViewingItem);
    ShowSendingPageLinkMessage();   // defined in SendPageLink.js
}


   
   // =================================== DISABLE SEARCH ELEMENTS  ===============================
   
   var SearchElementsArray = new Array("txtProductType","txtBrandName","txtModelName","txtDescription","txtLocation");
     
   function DisableEnableSearchElements(boolDisable)
   {
        for(i=0; i < SearchElementsArray.length; i++)
        {
            EnableDisableObj(SearchElementsArray[i],boolDisable);
        }
   }
      

// ============================= HOVER TIPs ===========================

function RunSearchHoverTip()
{
    if(SearchInProgress == true)
    {
        //DisplayTip(tipText, tipWidth, moveWithMouse)
    }
}


// added 13/08/08 new show help function utilised to show either search help, how to buy or how to sell help -------

  function ShowHelpBox(getTextFromDivId)
  {
        HorizontallyCenterDiv("HelpBoxWrapper", 610, 10);
        //if( document.getElementById("HelpBoxText").innerHTML.Trim() == "")
        //{
            var helpText = document.getElementById(getTextFromDivId).innerHTML;
            document.getElementById("HelpBoxText").innerHTML = helpText;
        //}
        
        if(IsLessIE7 == true)
        {
            ShowHidePageLists("hidden");
        }
        
        ShowHideObj("ModalHelpBg","visible");
        ShowHideObj("HelpBoxWrapper","visible");
  }
  
  // added 13/08/08 new general function to hide help box
   function HideHelpBox()
  {
        document.getElementById("HelpBoxText").innerHTML = "";
        if(IsLessIE7 == true)
        {
            ShowHidePageLists("visible");
        }
        
        ShowHideObj("ModalHelpBg","hidden");
        ShowHideObj("HelpBoxWrapper","hidden");
  }

  
  var FocusHelpTipBg = "#66cc33";
  
  function ShowFocusHelpTip(tiptext,tipwidth,xpos,ypos,controlid)
  {
    if(ShowHelpBalloon == true)
    {
        document.getElementById(controlid).style.backgroundColor = FocusHelpTipBg;
        FocusTip(tiptext,tipwidth,xpos,ypos);
    }
  }
  
  function HideFocusHelpTip(controlid)
  {
    document.getElementById(controlid).style.backgroundColor = "#ffffff";
    HideTip();
  }
  
  
    
//  function ShowHideHelpBox(vis)
//  {
//        HorizontallyCenterDiv("HelpBoxWrapper", 610, 10);
//        if( document.getElementById("HelpBoxText").innerHTML.Trim() == "")
//        {
//            var helpText = document.getElementById("HelpInfoTextWrapper").innerHTML;
//            document.getElementById("HelpBoxText").innerHTML = helpText;
//        }
//        ShowHideObj("HelpBoxWrapper",vis);
//  }



function HelpBalloonCheckboxClick()
{
    if(document.getElementById("chkHelpBalloon").checked == true)
    {
        ShowHelpBalloon = true;
        document.getElementById("HelpBalloonOnOffText").innerHTML = "ON";
    }
    else
    {
        ShowHelpBalloon = false;
        document.getElementById("HelpBalloonOnOffText").innerHTML = "OFF";
    }
}
