/* if (window.addEventListener) window.addEventListener('load', loadFeaturedExtras, false); else if (window.attachEvent) window.attachEvent('onload', loadFeaturedExtras); */

function loadDealsListExtras() {
   var introScroll=document.getElementById('home-intro-scroll');   if (introScroll!=null)  { loadHomeIntroSlideShow();}
   var featuredPanel=document.getElementById('featured');          if (featuredPanel!=null) { loadFeaturedPanel();}
   var dealsDiv=document.getElementById('deals');                  if (dealsDiv!=null) {loadDealsList('categories'); loadDealsList('list');}
   // var monthSpecialOffer=document.getElementById('month-special');      if (monthSpecialOffer!=null) {monthSpecialOffer.getElementsByTagName("strong")[0].innerHTML=getUpdatedDate(0).split(' ')[1]+" Offer:"; }
}

var xmlDoc=null;
function loadDealsList(moreDialsRequestType) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // Internet Explorer 5/6
      if (moreDialsRequestType!='categories') {
            var orderBySelector=document.getElementById('order-by').getElementsByTagName("option")[document.getElementById('order-by').selectedIndex].value;
            var orderByDirection=document.getElementById('order-deals').getElementsByTagName("input")[0].value;
            var choosenDialGroup=document.getElementById('order-deals').getElementsByTagName("input")[1].value;
            var preferredPaging=goPaged('get-preferred-paging');
            var pagedResultsPageNo=Math.round(document.getElementById('paging-group-numbers').innerHTML.split('-')[0]); if (pagedResultsPageNo>1) pagedResultsPageNo=(pagedResultsPageNo-1)/preferredPaging+1;
            moreDialsRequestType+="&order-by="+orderBySelector+"&order-direction="+orderByDirection+"&selected-dial-group-id="+choosenDialGroup+"&requested-paging="+preferredPaging+"&page="+pagedResultsPageNo;
      }
      var url="../bs-deals-data-loader.xml"; url=url+"?deals-list-request="+moreDialsRequestType+"&sid="+Math.random();
      xhttp.open("GET",url,false); xhttp.send(""); xmlDoc=xhttp.responseXML;
      if (xmlDoc!=null) {
            if (moreDialsRequestType!='categories') { 
                  var x=xmlDoc.getElementsByTagName("deal"); 
                  var y=xmlDoc.getElementsByTagName("deals_paging");
                  var dealAdd; var dealSaving; var dealImage; var dealContent;
                  var dealsList=document.getElementById('deals-list'); dealsList.innerHTML="";
                  if (x.length>0) {
                        for (i=0;i<x.length;i++) { 
                                    dealAdd=document.createElement("div");
                                    dealAdd.setAttribute("id",x[i].getElementsByTagName("d_id")[0].childNodes[0].nodeValue);
                                    dealsList.appendChild(dealAdd);
                                    if(x[i].getElementsByTagName("d_deposit").length>0) { dealBuy="<div><button onclick=\"location.assign('?deal-id="+x[i].getElementsByTagName("d_id")[0].childNodes[0].nodeValue+"')\">Deposit: "+x[i].getElementsByTagName("d_deposit")[0].childNodes[0].nodeValue+"</button></div>\n";
                                    } else { dealBuy="<div><button onclick=\"location.assign('?deal-id="+x[i].getElementsByTagName("d_id")[0].childNodes[0].nodeValue+"')\">Buy Now: "+x[i].getElementsByTagName("d_price")[0].childNodes[0].nodeValue+"</button></div>\n"; }                                    dealImage="<img src=\""+x[i].getElementsByTagName("d_photo_url")[0].childNodes[0].nodeValue.toLowerCase()+"\" alt=\"deal-image\"/>\n";
                                    if(x[i].getElementsByTagName("d_sl_youtube")[0].childNodes[0].nodeValue==="yes") {dealImage+="<img src=\"[img]/bizcounters-deal-video-link-middle.png\" class=\"video-icon\"/>"}
                                    dealContent=dealBuy+dealImage+"<h4>"+x[i].getElementsByTagName("dd_title")[0].childNodes[0].nodeValue+"</h4>\n";
                                    dealContent+="<p>"+x[i].getElementsByTagName("dd_shortly")[0].childNodes[0].nodeValue+"</p>\n";
                                    dealContent+="<a href=\""+x[i].getElementsByTagName("d_url")[0].childNodes[0].nodeValue+"\"><span>view details . . . </span></a>\n";
                                    dealAdd.innerHTML=dealContent;
                        }
                        document.getElementById('paging-group-numbers').innerHTML=y[0].getElementsByTagName("deals_group_current")[0].childNodes[0].nodeValue;
                        document.getElementById('paging-total').innerHTML=y[0].getElementsByTagName("deals_total")[0].childNodes[0].nodeValue;
                        goPaged('fix-display');
                  } else { dealAdd=document.createElement("div"); dealAdd.setAttribute("id","deals-list-message"); dealsList.appendChild(dealAdd); dealAdd.innerHTML="No deals matching the search criteria."; }
            } else {
                  var x=xmlDoc.getElementsByTagName("category");
                  if (x.length>0) {
                        categorySelectorList=document.getElementById('categories').getElementsByTagName('div')[0];
                        var currentCategoryId;
                        for (i=0;i<x.length;i++) {
                                    categoryAdd=document.createElement("a");
                                    if (document.getElementById('order-deals').getElementsByTagName("input")[1].value==x[i].getElementsByTagName("dc_id")[0].childNodes[0].nodeValue) currentCategoryId=x[i].getElementsByTagName("dc_id")[0].childNodes[0].nodeValue;
                                    categoryAdd.setAttribute("id",x[i].getElementsByTagName("dc_id")[0].childNodes[0].nodeValue);
                                    categoryAdd.setAttribute("href","javascript:filterDealsList('"+x[i].getElementsByTagName("dc_id")[0].childNodes[0].nodeValue+"')");
                                    categoryAdd.setAttribute("data-category-type",x[i].getElementsByTagName("dc_type")[0].childNodes[0].nodeValue);
                                    categoryAdd.innerHTML=x[i].getElementsByTagName("dc_title")[0].childNodes[0].nodeValue;
                                    categorySelectorList.appendChild(categoryAdd);
                        }
                        if(window.location.search.toLowerCase().search('dc=')>0) { currentCategoryId="dc-"+readGetVar('dc'); document.getElementById('order-deals').getElementsByTagName("input")[1].value=currentCategoryId; }
                        correctCategoriesDisplay(currentCategoryId);
                        if (document.getElementById('order-by').getAttribute("onchange")==null) document.getElementById('order-by').setAttribute("onchange","loadDealsList('deals')");
                  }
            }
      } else { alert("Error loading Deals Data(xml-deals-data-file)"); }
}
function readGetVar(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]; } } return 'empty'; }

function goPaged(request) {
      if (document.getElementById('paging-group').options[document.getElementById('paging-group').selectedIndex].value!='All') { var preferredPaging=Math.round(document.getElementById('paging-group').options[document.getElementById('paging-group').selectedIndex].value);
      } else {preferredPaging='All';}
      switch(request) {
            case 'fix-display':
                  var pagingGroupStart=Math.round(document.getElementById('paging-group-numbers').innerHTML.split('-')[0]);
                  if (pagingGroupStart>1) {document.getElementById('paging-prev').className="visible";} else {document.getElementById('paging-prev').className="hidden-paging-nav";} 
                  var pagingGroupEnd=Math.round(document.getElementById('paging-group-numbers').innerHTML.split('-')[1]);
                  if (pagingGroupEnd<Math.round(document.getElementById('paging-total').innerHTML)) {document.getElementById('paging-next').className="visible";} else {document.getElementById('paging-next').className="hidden-paging-nav";}
                  var paggingBottomDiv=document.getElementById('paging-bottom'); if (paggingBottomDiv) { try {document.getElementById('content').removeChild(paggingBottomDiv);} catch(err){paggingBottomDiv.removeNode(true);}}
                  if( document.getElementById('deals-list').childNodes.length > 5) {
                        if (preferredPaging!='All') {
                              var pagingBottom=document.createElement("div"); pagingBottom.setAttribute("id","paging-bottom"); document.getElementById('content').appendChild(pagingBottom);
                              var pagingBottomPrev=document.createElement("a"); pagingBottomPrev.setAttribute("id","paging-bottom-prev"); pagingBottomPrev.setAttribute("href",document.getElementById('paging-prev').getAttribute('href')); pagingBottomPrev.innerHTML="&nbsp;"; pagingBottomPrev.className=document.getElementById('paging-prev').className; pagingBottom.appendChild(pagingBottomPrev);
                              var pagingBottomNumbers=document.createElement("span"); pagingBottomNumbers.setAttribute("id","paging-bottom-numbers"); pagingBottomNumbers.innerHTML=document.getElementById('paging-group-numbers').innerHTML;  pagingBottom.appendChild(pagingBottomNumbers);
                              var pagingBottomNext=document.createElement("a"); pagingBottomNext.setAttribute("id","paging-bottom-next"); pagingBottomNext.setAttribute("href",document.getElementById('paging-next').getAttribute('href')); pagingBottomNext.innerHTML="&nbsp;"; pagingBottomNext.className=document.getElementById('paging-next').className; pagingBottom.appendChild(pagingBottomNext);
                              pagingBottom.innerHTML+="of "+document.getElementById('paging-total').innerHTML;
                        }
                  }
            break;
            case 'prev':
                  var pagingGroupStart=Math.round(document.getElementById('paging-group-numbers').innerHTML.split('-')[0])-preferredPaging;
                  var pagingGroupEnd=pagingGroupStart-1+preferredPaging;
                  document.getElementById('paging-group-numbers').innerHTML=pagingGroupStart+'-'+pagingGroupEnd;
                  loadDealsList('list');
            break;
            case 'next':
                  var pagingGroupStart=Math.round(document.getElementById('paging-group-numbers').innerHTML.split('-')[0])+preferredPaging;
                  var pagingGroupEnd=pagingGroupStart-1+preferredPaging; if(pagingGroupEnd>Math.round(document.getElementById('paging-total').innerHTML)) pagingGroupEnd=Math.round(document.getElementById('paging-total').innerHTML);
                  document.getElementById('paging-group-numbers').innerHTML=pagingGroupStart+'-'+pagingGroupEnd;
                  loadDealsList('list');
            break;
            case 'set-preferred-paging':
                  document.getElementById('paging-group-numbers').innerHTML="1-"+preferredPaging;
                  loadDealsList('list');
            break;
            case 'get-preferred-paging':
                  if (document.getElementById('paging-group').getAttribute("onchange")==null) document.getElementById('paging-group').setAttribute("onchange"," goPaged('set-preferred-paging')");
                  return preferredPaging;
            break;
            default: alert('wrong goPaged JS function request');
      }
}

function filterDealsList(filterRequest) {
      // var categoriesSelectorList=document.getElementById('categories').getElementsByTagName('div')[0].getElementsByTagName('a');
      // for (i=0;i<categoriesSelectorList.length;i++) { if (categoriesSelectorList[i].className="current") categoriesSelectorList[i].removeAttribute("class"); }
      // document.getElementById(filterRequest).className="current";
      document.getElementById('order-deals').getElementsByTagName("input")[1].value=filterRequest;
      correctCategoriesDisplay(filterRequest);
      loadDealsList('list');
}

function correctCategoriesDisplay(filterRequest) { // Show/Hide active/inactive categy types and selects the apropriate current category
      var categoriesSelectorList=document.getElementById('categories').getElementsByTagName('div')[0].getElementsByTagName('a');
      var activeCategoryType=document.getElementById(filterRequest).getAttribute("data-category-type"); 
      if(activeCategoryType=='title') { if(document.getElementById(filterRequest).innerHTML.search(/business/i)>0) { activeCategoryType='b'; } else {activeCategoryType='s';} }
      for (i=0;i<categoriesSelectorList.length;i++) { var currentCategoryType=categoriesSelectorList[i].getAttribute("data-category-type");
            if (currentCategoryType!=activeCategoryType) { 
                  if(currentCategoryType!='title') { categoriesSelectorList[i].className="hidden"; } else { 
                        if(categoriesSelectorList[i].getAttribute('id').substr(3,1)!=activeCategoryType) { categoriesSelectorList[i].className="hidden"; 
                        } else { if(categoriesSelectorList[i].getAttribute('class')!=null) categoriesSelectorList[i].removeAttribute("class"); }
                  }
            } else { if(categoriesSelectorList[i].getAttribute('class')!=null) categoriesSelectorList[i].removeAttribute("class"); }
      } document.getElementById(filterRequest).className="current";
}

function changeSortOrder() {
      var sortOrder=document.getElementById('order-deals').getElementsByTagName("input")[0];
      if(sortOrder.value=='desc') { sortOrder.value='asc';} else {sortOrder.value='desc';}
      document.getElementById('order-deals').getElementsByTagName('a')[0].className=sortOrder.value;
      loadDealsList('list');
}

function loadLiveChat() {
 lhnAccountN=9163; //LiveHelpNow account #
 lhnButtonN = 2206; //Button #
 lhnInviteEnabled = 1; //Invite visitor to chat automatically
 lhnInviteChime = 0; //1 = disable invite beep sound,0 = keep invite beep sound enabled
 lhnWindowN = 0; //Chat window #, leave 0 to open default window setup for your account
 lhnDepartmentN = 0; //Department #, leave 0 to not route by department
 lhnCustomInvitation = ''; //change to 1 to use custom invitation, see this article for customization instructions: http://help.livehelpnow.net/article.aspx?cid=1&aid=1739
 lhnCustom1 = ''; //Custom1 feed value please use encodeURIComponent() function to encode your values
 lhnCustom2 = ''; //Custom2 feed value please use encodeURIComponent() function to encode your values
 lhnCustom3 = ''; //Custom3 feed value please use encodeURIComponent() function to encode your values
 lhnTrackingEnabled = 't'; //change to 'f' to disable visitor tracking
 lhnVersion = 5.3; //LiveHelpNow version #
 lhnJsHost = (("https:" == document.location.protocol) ? "https://" : "http://"); //to force secure chat replace this line with: var lhnJsHost = 'https://';
 lhnScriptSrc = lhnJsHost + 'www.livehelpnow.net/lhn/scripts/livehelpnow.aspx?lhnid=' + lhnAccountN + '&iv=' + lhnInviteEnabled + '&d=' + lhnDepartmentN + '&ver=' + lhnVersion + '&rnd=' + Math.random();
 lhnScript = document.createElement("script"); lhnScript.type = "text/javascript";lhnScript.src = lhnScriptSrc;
document.getElementById('lhnContainer').appendChild(lhnScript);
}

function loadFeaturedPanel() {
      var featuredDiv=document.getElementById('featured');
      var helpContainerDiv=document.createElement("div"); helpContainerDiv.setAttribute("id","lhnContainer"); var helpBtnDiv=document.createElement("div"); helpBtnDiv.setAttribute("id","lhnChatButton"); helpContainerDiv.appendChild(helpBtnDiv); featuredDiv.appendChild(helpContainerDiv);
      var categoriesDiv=document.createElement("div"); categoriesDiv.setAttribute("id","categories"); var categoriesContentDiv=document.createElement("div"); categoriesDiv.appendChild(categoriesContentDiv); featuredDiv.appendChild(categoriesDiv);
      var dealsOrder=document.createElement("div"); dealsOrder.setAttribute("id","order-deals"); dealsOrder.innerHTML="Order by:"; featuredDiv.appendChild(dealsOrder);
            var dealsOrderSelector=document.createElement("select"); dealsOrderSelector.setAttribute("id","order-by"); dealsOrder.appendChild(dealsOrderSelector);
            var dealsOrderOption1=document.createElement("option"); dealsOrderOption1.setAttribute("value","date"); dealsOrderOption1.setAttribute("selected","selected"); dealsOrderOption1.innerHTML="Date Added"; dealsOrderSelector.appendChild(dealsOrderOption1);
            var dealsOrderOption2=document.createElement("option"); dealsOrderOption2.setAttribute("value","price"); dealsOrderOption2.innerHTML="Price"; dealsOrderSelector.appendChild(dealsOrderOption2);
            //var dealsOrderOption3=document.createElement("option"); dealsOrderOption3.setAttribute("value","popularity"); dealsOrderOption3.innerHTML="Popularity"; dealsOrderSelector.appendChild(dealsOrderOption3);
            var dealsOrderDirectionDiv=document.createElement("div"); var dealsOrderDirectionLink=document.createElement("a"); dealsOrderDirectionLink.setAttribute("href","javascript:changeSortOrder();"); dealsOrderDirectionLink.setAttribute("title","Change Deals sort order (Asc/Desc)"); dealsOrderDirectionLink.innerHTML="&nbsp;"; dealsOrderDirectionDiv.appendChild(dealsOrderDirectionLink); dealsOrder.appendChild(dealsOrderDirectionDiv);
            var dealsSortOrderField=document.createElement("input"); dealsSortOrderField.setAttribute("type","hidden"); dealsSortOrderField.setAttribute("name","sort-order"); dealsSortOrderField.setAttribute("value","desc"); dealsOrder.appendChild(dealsSortOrderField);
            var dealsSelectedField=document.createElement("input"); dealsSelectedField.setAttribute("type","hidden"); dealsSelectedField.setAttribute("name","selected-deal-group"); dealsSelectedField.setAttribute("value","dc-b-all"); dealsOrder.appendChild(dealsSelectedField);
      var dealsPaging=document.createElement("div"); dealsPaging.setAttribute("id","paging"); dealsPaging.innerHTML="Display:"; featuredDiv.appendChild(dealsPaging);
            var dealsPagingGroupSelector=document.createElement("select"); dealsPagingGroupSelector.setAttribute("id","paging-group"); dealsPaging.appendChild(dealsPagingGroupSelector);
            var pagingGroups=[10,25,50,'All']; for (var i=0; i<pagingGroups.length; i++) {var pagingGroupOption=document.createElement("option"); pagingGroupOption.setAttribute("value",pagingGroups[i]); pagingGroupOption.innerHTML=pagingGroups[i]; dealsPagingGroupSelector.appendChild(pagingGroupOption);}
            var prevGroupLink=document.createElement("a"); prevGroupLink.setAttribute("id","paging-prev"); prevGroupLink.setAttribute("href","javascript:goPaged('prev');"); prevGroupLink.setAttribute("title","Previous Deal Group"); prevGroupLink.innerHTML="&nbsp;"; dealsPaging.appendChild(prevGroupLink);
            var pagingGroupCurrent=document.createElement("span"); pagingGroupCurrent.setAttribute("id","paging-group-numbers"); dealsPaging.appendChild(pagingGroupCurrent);
            var nextGroupLink=document.createElement("a"); nextGroupLink.setAttribute("id","paging-next"); nextGroupLink.setAttribute("href","javascript:goPaged('next');"); nextGroupLink.setAttribute("title","Next Deal Group"); nextGroupLink.innerHTML="&nbsp;"; dealsPaging.appendChild(nextGroupLink);
            dealsPaging.innerHTML+="of "; var pagingGroupTotal=document.createElement("span"); pagingGroupTotal.setAttribute("id","paging-total"); dealsPaging.appendChild(pagingGroupTotal);
      var eGuideDiv=document.createElement("div"); eGuideDiv.setAttribute("id","eguide"); var eGuideDivInput=document.createElement("input"); eGuideDivInput.setAttribute("type","text"); eGuideDivInput.setAttribute("value","Email me Free Start-Up eGuide"); eGuideDivInput.setAttribute("onclick","fPlaceholder(this,'');"); eGuideDiv.appendChild(eGuideDivInput); var eGuideDivBtn=document.createElement("input"); eGuideDivBtn.setAttribute("type","button"); eGuideDivBtn.setAttribute("value","Ok"); eGuideDivBtn.setAttribute("onclick","goForm('newsletter');"); eGuideDiv.appendChild(eGuideDivBtn); featuredDiv.appendChild(eGuideDiv);
      loadLiveChat();
}



function fPlaceholder(placeholderElement,elementAction) {
      if (placeholderElement.getAttribute("title")!=null) {
            if (elementAction!="blurMe") { if (placeholderElement.value==placeholderElement.title) placeholderElement.value="";
            } else { if (placeholderElement.value=="") placeholderElement.value=placeholderElement.title; }
      } else {
            if (placeholderElement.parentNode.id==="subscribe-panel")  placeholderElement.setAttribute("name","subscribe");
            placeholderElement.setAttribute("title",placeholderElement.value); placeholderElement.setAttribute("onblur","fPlaceholder(this,'blurMe')"); placeholderElement.value="";
      }
}

function loadHomeIntroSlideShow() {
      var SlideShow=document.getElementById('home-intro-scroll'); SlideShow.innerHTML='';
      var newImg = document.createElement("img"); newImg.src = "[img]/base-home-intro-scroll-img.jpg"; SlideShow.appendChild(newImg);
      var newVid = document.createElement("div"); newVid.setAttribute("id","intro-video-img"); newVid.innerHTML="&nbsp;"; SlideShow.appendChild(newVid);
      var newLink = document.createElement("a"); newLink.setAttribute("id","intro-video-link"); newLink.innerHTML="&nbsp;"; newLink.setAttribute("href","javascript:goVideo('bizstar-business')"); SlideShow.appendChild(newLink);
/*    for (i=1; i<=3; i++) { var newImg = document.createElement("img"); newImg.src = "[img]/base-home-intro-scroll-img-0"+i+".png"; SlideShow.appendChild(newImg); }
      if (SlideShow.getElementsByTagName('img').length>1) {
            var script = document.createElement("script"); script.type = "text/javascript"; script.src = "[incl]/[home]-intro-slideshow.js";
            if (script.readyState){ script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ LoadSideShow(); script.onreadystatechange = null; } }; //IE
            } else { script.onload = function(){ LoadSideShow(); }; } //Others
            document.getElementsByTagName("head")[0].appendChild(script);
      } */      
}
