var _aType = 0;
var _vType = 0;
var _sBy = 0;
var _pIndex = 0;
var _compareProds = '';
var _compareProdsCount = 0;
var _imgWaitFlag = false;

function chkThis(linkObj, pfID) {
	if(linkObj.className == 'chkLink_empty') {linkObj.className = 'chkLink_full';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full') {linkObj.className = 'chkLink_empty';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty2') {linkObj.className = 'chkLink_full2';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full2') {linkObj.className = 'chkLink_empty2';methodName = 'RemoveAuctionFromShopper';}

	if(linkObj.className == 'chkLink_empty3') {linkObj.className = 'chkLink_full3';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full3') {linkObj.className = 'chkLink_empty3';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'checkEmpty') {linkObj.className = 'checkFull';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'checkFull') {linkObj.className = 'checkEmpty';methodName = 'RemoveAuctionFromShopper';}

	if(linkObj.className == 'bigCHK_empty') {linkObj.className = 'bigCHK_full';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'bigCHK_full') {linkObj.className = 'bigCHK_empty';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty_nomargin') {linkObj.className = 'chkLink_full_nomargin';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_nomargin') {linkObj.className = 'chkLink_empty_nomargin';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty_new1') {linkObj.className = 'chkLink_full_new1';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_new1') {linkObj.className = 'chkLink_empty_new1';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty_new2') {linkObj.className = 'chkLink_full_new2';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_new2') {linkObj.className = 'chkLink_empty_new2';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty_new3') {linkObj.className = 'chkLink_full_new3';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_new3') {linkObj.className = 'chkLink_empty_new3';methodName = 'RemoveAuctionFromShopper';}
	
	/*if(linkObj.className == 'chkLink_empty_direct') {linkObj.className = 'chkLink_full_direct';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_direct') {linkObj.className = 'chkLink_empty_direct';methodName = 'RemoveAuctionFromShopper';}
	
	if(linkObj.className == 'chkLink_empty_direct_noShipping') {linkObj.className = 'chkLink_full_direct_noShipping';methodName = 'AddAuctionToShopper';}
	else if(linkObj.className == 'chkLink_full_direct_noShipping') {linkObj.className = 'chkLink_empty_direct_noShipping';methodName = 'RemoveAuctionFromShopper';}*/
	
	if (_xmlHttpObj && _xmlHttpObj.readyState != 0 && _xmlHttpObj.readyState != 4)
    return;
  executeServerRequest(methodName, 'pfID=' + pfID);
}

function compareThis(linkObj, pfID) {
  if(linkObj.className == 'chkLink_empty' || linkObj.className == 'bigCHK_empty' || linkObj.className == 'chkLink_empty_nomargin' || linkObj.className == 'chkLink_empty_new1' || linkObj.className == 'chkLink_empty_new2' || linkObj.className == 'chkLink_empty_new3' /*|| linkObj.className == 'chkLink_empty_direct' || linkObj.className == 'chkLink_empty_direct_noShipping'*/) {
    if (_compareProdsCount >= 4)
      alert ('לא ניתן להשוות יותר מארבעה מוצרים בו-זמנית');
    else {
      if (linkObj.className == 'chkLink_empty') linkObj.className = 'chkLink_full';
      else if (linkObj.className == 'bigCHK_empty') linkObj.className = 'bigCHK_full';
      else if (linkObj.className == 'chkLink_empty_nomargin') linkObj.className = 'chkLink_full_nomargin';
      else if (linkObj.className == 'chkLink_empty_new1') linkObj.className = 'chkLink_full_new1';
      else if (linkObj.className == 'chkLink_empty_new2') linkObj.className = 'chkLink_full_new2';
      else if (linkObj.className == 'chkLink_empty_new3') linkObj.className = 'chkLink_full_new3';
      /*else if (linkObj.className == 'chkLink_empty_direct') linkObj.className = 'chkLink_full_direct';
      else if (linkObj.className == 'chkLink_empty_direct_noShipping') linkObj.className = 'chkLink_full_direct_noShipping';*/
	    addCompareProduct(pfID);
	  }
	}
	else if(linkObj.className == 'chkLink_full' || linkObj.className == 'bigCHK_full' || linkObj.className == 'chkLink_full_nomargin' || linkObj.className == 'chkLink_full_new1' || linkObj.className == 'chkLink_full_new2' || linkObj.className == 'chkLink_full_new3' /*|| linkObj.className == 'chkLink_full_direct'*/) {
	  if (linkObj.className == 'chkLink_full') linkObj.className = 'chkLink_empty';
    else if (linkObj.className == 'bigCHK_full') linkObj.className = 'bigCHK_empty';
    else if (linkObj.className == 'chkLink_full_nomargin') linkObj.className = 'chkLink_empty_nomargin';
    else if (linkObj.className == 'chkLink_full_new1') linkObj.className = 'chkLink_empty_new1';
    else if (linkObj.className == 'chkLink_full_new2') linkObj.className = 'chkLink_empty_new2';
    else if (linkObj.className == 'chkLink_full_new3') linkObj.className = 'chkLink_empty_new3';
    /*else if (linkObj.className == 'chkLink_full_direct') linkObj.className = 'chkLink_empty_direct';
    else if (linkObj.className == 'chkLink_full_direct_noShipping') linkObj.className = 'chkLink_empty_direct_noShipping';*/
	  removeCompareProduct(pfID);
	}
}

function addCompareProduct(pfID) {
  _compareProds += (_compareProds == '' ? '' : ',') + pfID;
	_compareProdsCount += 1;
}

function removeCompareProduct(pfID) {
  var arr = _compareProds.split(',');
  _compareProds = '';
  _compareProdsCount = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != pfID)
      addCompareProduct(arr[i]);
  }
}

function selectKind(linx, aType) {
	var linkARR = document.getElementById('saleKind_ul').getElementsByTagName('A');
	for(i=0; i < linkARR.length; i++) linkARR[i].className = "";
	linx.className = "selectedSale";
	_aType = aType;
	_pIndex = 0;
	if (typeof(window['selectKindEx']) == 'function') {
    if (!selectKindEx(linx, aType))
      return;
  }
  getProducts();
}

function selectViewType() {
  if (_vType == 0) {
    _vType = 1;
    document.getElementById('TopPager_lnkViewType').innerHTML = 'עבור לתצוגת גלריה';
    document.getElementById('BottomPager_lnkViewType').innerHTML = 'עבור לתצוגת גלריה';
  }
  else {
    _vType = 0;
    document.getElementById('TopPager_lnkViewType').innerHTML = 'עבור לתצוגת רשימה';
    document.getElementById('BottomPager_lnkViewType').innerHTML = 'עבור לתצוגת רשימה';
  }
  _sBy = 0;
  _pIndex = 0;
  getProducts();
}

function selectSortBy(sBy) {
  _sBy = sBy;
  _pIndex = 0;
  getProducts();
}

function selectPage(obj, pageIndex) {
  _pIndex = pageIndex;
  var parentID = obj.parentNode.parentNode.parentNode.id;
  if (parentID == 'BottomPager_tdPager')
    getProducts(true);
  else
    getProducts();
}

function initProdPageDetails(aType, vType, pIndex) {
  _aType = aType;
  _vType = vType;
  _pIndex = pIndex;
  doLoad();
  if (isFirstLoad())
    savePageContentToHistory();
  else
    restorePageContentFromHistory();
}

function showWaitImage() {
  var img = document.getElementById("imgWait");
  if (img) {
    var windowWidth = getWindowWidth();
    var windowHeight = getWindowHeight();
    img.style.left = (windowWidth ? parseInt(windowWidth / 2) - 50 : 500) + 'px';
    img.style.top = (windowHeight ? parseInt(windowHeight / 2) - 20 : 300) + 'px';
    img.style.display = '';
    _imgWaitFlag = true;
    setTimeout('hideWaitImage()', 600);
  }
}

function hideWaitImage() {
  if (_imgWaitFlag)
    _imgWaitFlag = false;
  else
    document.getElementById("imgWait").style.display = 'none';
}

function getProducts(scrollPage) {
  if (_xmlHttpObj && _xmlHttpObj.readyState != 0 && _xmlHttpObj.readyState != 4)
    return;
  showWaitImage();
  savePageContentToHistory();
  if (typeof(window['getProductsList']) == 'function')
    getProductsList();
  if (scrollPage)
    document.getElementById("divHeader").scrollIntoView();
}

function getResponseMethod(methodName) {
  switch (methodName) {
    case 'AddAuctionToShopper': return addAuctionToShopperResult;
    case 'RemoveAuctionFromShopper': return removeAuctionFromShopperResult;
    default:
      if (typeof(window['getResponseMethodEx']) == 'function')
        return getResponseMethodEx(methodName);
      else
        return doNothing();
  }
}

function doNothing() {
  // not supposed to do anything...
}

function addAuctionToShopperResult() {
}

function removeAuctionFromShopperResult() {
}

function getProductsResult() {
  if (_xmlHttpObj.readyState != 4)
    return;
  var responseXml = getResponseXml();
  if (responseXml) {
    document.getElementById("divProducts").innerHTML = strGetXmlString(responseXml.selectSingleNode('auctionsHtml'));
    var pagerHtml = strGetXmlString(responseXml.selectSingleNode('pagerHtml'));
    document.getElementById("TopPager_tdPager").innerHTML = pagerHtml;
    document.getElementById("BottomPager_tdPager").innerHTML = pagerHtml;
    var auctionsCount = strGetXmlString(responseXml.selectSingleNode('auctionsCount'));
    if (auctionsCount > 0) {
      document.getElementById("TopPager_tdPager").style.display = '';
      document.getElementById("TopPager_pnlViewType").style.display = '';
      document.getElementById("BottomPager_tdPager").style.display = '';
      document.getElementById("BottomPager_pnlViewType").style.display = '';
    }
    else {
      document.getElementById("TopPager_tdPager").style.display = 'none';
      document.getElementById("TopPager_pnlViewType").style.display = 'none';
      document.getElementById("BottomPager_tdPager").style.display = 'none';
      document.getElementById("BottomPager_pnlViewType").style.display = 'none';
    }
    if (_vType == 0)
      showBanners('mainBanner');
    if (typeof(window['getProductsResultEx']) == 'function')
      getProductsResultEx(responseXml);
  }
  hideWaitImage();
}

function compareProducts(url) {
  if (url == '') return;
  else if (_compareProds == '' || _compareProdsCount == 0) {
    alert ('אנא סמן מוצרים להשוואה');
    return;
  }
  
  //url += '?specPfIds=' + _compareProds;
  //window.open(url, null, 'left=10,top=10,width=850,height=785,status=yes,toolbar=yes,titlebar=yes,resizable=yes,scrollbars=yes,location=yes');
  
  if (_pageType && _pageType >= 1 && _pageType <= 3) {
    url += '?PfIds=' + _compareProds + '&pt=' + _pageType + '&mc=' + _mainCatID + '&c=' + _catID + '&sc=' + _subCatID;
    switch (_pageType) {
      case 2 /*shop auctions*/: pageType = 2;url += '&g=' + _gID;break;
      case 3 /*mutag auctions*/: pageType = 3;url += '&m=' + _mutagId;break;
    }
    
    window.location = url;
  }
}

function savePageContentToHistory() {
  historyStorage.put('_aType', _aType);
  historyStorage.put('_vType', _vType);
  historyStorage.put('_sBy', _sBy);
  historyStorage.put('_pIndex', _pIndex);
  
  if (document.getElementById("TopPager_tdRightContent"))
    historyStorage.put('TopPager_tdRightContent', document.getElementById("TopPager_tdRightContent").innerHTML);
  
  if (typeof(window['savePageContentToHistoryEx']) == 'function')
    savePageContentToHistoryEx();
}

function restorePageContentFromHistory() {
  _aType = historyStorage.get('_aType');
  _vType = historyStorage.get('_vType');
  _sBy = historyStorage.get('_sBy');
  _pIndex = historyStorage.get('_pIndex');
  
  if (document.getElementById("TopPager_tdRightContent"))
    document.getElementById("TopPager_tdRightContent").innerHTML = historyStorage.get('TopPager_tdRightContent');
    
  restoreKindFromHistory();
  
  if (typeof(window['restorePageContentFromHistoryEx']) == 'function') {
    if (!restorePageContentFromHistoryEx())
      return;
  }
    
  getProducts();
}

function restoreKindFromHistory() {
  var linkARR = document.getElementById('saleKind_ul').getElementsByTagName('A');
	for(i=0; i < linkARR.length; i++) {
	  if (linkARR[i].getAttribute("kind") == _aType)
	    linkARR[i].className = "selectedSale";
	  else
	    linkARR[i].className = "";
	}
}

function logProductClick(shopName, pfID, prodName, itemIndex, mutagName) {
  var cats = '';
  if (typeof(_mainCatName) != 'undefined' && _mainCatName)
    cats = _mainCatName;
  if (typeof(_catName) != 'undefined' && _catName)
    cats += '/' + _catName;
  if (typeof(_subCatName) != 'undefined' && _subCatName)
    cats += '/' + _subCatName;
  logNuconomyActivity(shopName, '', pfID, prodName, 124, '', cats, itemIndex, mutagName);
}