
var isW3C, isIE, isCSS;
var currentSectionId = 2;
var filterStrings = new Array();
var filterAlternativeNames = new Array();
var filterPropertyIds = new Array();
var filterComparisons = new Array();
var filterLimits = new Array();
var filterTables = new Array();
var xmlRequest;
// Following two globals used to keep track of filter strings used by guide. Needed to be able to delete same filters, e.g. when user changes radio box value 
var guideFilterIds = new Array(); 
var guideFilterStrings = new Array();
var briefPattern = ":</b>&nbsp;";

/**
 * Alias for addGuideFilter
 */
function aGF(questionId, filterId, filterString, briefText, textField) {
  return addGuideFilter(questionId, filterId, filterString, briefText, textField, 0);
}

/**
 * Adds a selection filter to the list of filters used to sort out products
 */
function addGuideFilter(questionId, filterId, filterString, briefText, textField, checkbox) {
  // If text field, check that filter value is not empty, if so, do not add it!
  if (textField) {
    var valueStart = filterString.indexOf(",", filterString.indexOf(",", filterString.indexOf(",") + 1) + 1) + 1;
    if (valueStart >= filterString.length - 1) {
	  return;
	}
	briefText = gEI(questionId).value;
  }
  
  // Set question brief text
  var brief = gEI(questionId + 'Brief');
  if (checkbox && brief.innerHTML.toLowerCase().indexOf("-") != brief.innerHTML.length - 1) {
    // Since checkboxes can have multiple answers, check that Brief text contains all the selected values.
	if (brief.innerHTML.toLowerCase().indexOf(briefText.toLowerCase()) < 0) {
      brief.innerHTML = brief.innerHTML.concat(lang("buyingaid", "and") + briefText);
	}
  }
  else {
    brief.innerHTML = "&nbsp;" + briefText;
  }
  
  // Set filter values in selString box and start dimming product list out.
  guideFilterIds = guideFilterIds.concat(filterId);
  guideFilterStrings = guideFilterStrings.concat(filterString);
  var guideSelStringBox = gEI('gSelString');
  guideSelStringBox.value = guideSelStringBox.value + filterString;
}

/**
 * Alias for removeGuideFilter
 */
function rGF(questionId, filterId, textField) {
  return removeGuideFilter(questionId, filterId, textField, 0, null);
}

/**
 * Removes the specified filter from filter string and from lists of filters used 
 */
function removeGuideFilter(questionId, filterId, textField, checkbox, briefText) {
//  if (textField && (window.event && window.event.keyCode != 13)) { 
//    return;
//  } 

  // Set question brief text
  var brief = gEI(questionId + 'Brief');
  if (checkbox) {
    var bIHL = brief.innerHTML.toLowerCase();
    // Since checkboxes can have multiple answers, check that Brief text contains all the selected values.
	if (bIHL.indexOf(briefText.toLowerCase()) >= 0) {
      if (bIHL == "&nbsp;" + briefText.toLowerCase()) {
//      if (bIHL.substr(bIHL.indexOf("&nbsp;" + briefText)+briefText.length) == briefText.toLowerCase()) {
	    // If this is the only checkbox value in the brief text, simply remove it.
	    brief.innerHTML = "&nbsp;-";
	  }
	  else {
	    var position = brief.innerHTML.indexOf(briefText);
		if (position == 6) {
		  // directly after &nbsp;.
		  brief.innerHTML = "&nbsp;" + brief.innerHTML.substring(position + briefText.length + lang("buyingaid", "and").length);
		}
		else {
		  brief.innerHTML = brief.innerHTML.substring(0, position - lang("buyingaid", "and").length) + brief.innerHTML.substring(position + briefText.length);
		}
//		tmpPattern = briefText.replace(/\(/, "\\(").replace(/\)/, "\\)");
//	    brief.innerHTML = brief.innerHTML.replace(new RegExp(lang("buyingaid", "and") + tmpPattern + "|" +
//		                                                     "&nbsp;" + tmpPattern + lang("buyingaid", "and") + ")", "gi"), "");
	  }
	}
  }
  else {
    // If not checkbox, just remove brief
//    brief.innerHTML = brief.innerHTML.replace(new RegExp(":</b>&nbsp;.*", "gi"), ":</b>&nbsp;-");
    brief.innerHTML = brief.innerHTML = "&nbsp;-";
  }
  gEI("startNo").value = "1";  // Always show first page of search result
  
  // Check position of filter to remove.
  var filterPos = -1;
  for (var filterNo = 0; filterNo < guideFilterIds.length; filterNo++) {
    if (filterId == guideFilterIds[filterNo]) {
	  filterPos = filterNo;
	  break;
	}
  }
  if (filterPos < 0) {
    return;
  }
  // Create filter string without the filter we are removing.
  var filterStringToRemove = guideFilterStrings[filterPos];
  var filterString = gEI('gSelString').value;
  var removePos = filterString.indexOf(filterStringToRemove);
  filterString = filterString.substring(0, removePos) +
                          filterString.substring(removePos + filterStringToRemove.length, filterString.length);
  gEI('gSelString').value = filterString;
  
  // Remove removed string from global arrays
  guideFilterIds = guideFilterIds.slice(0, filterPos).concat(guideFilterIds.slice(filterPos + 1));
  guideFilterStrings = guideFilterStrings.slice(0, filterPos).concat(guideFilterStrings.slice(filterPos + 1));
  
//alert("r2: " + guideFilterStrings);
}

/**
 * Alias for setBriefText
 */
function sBT(questionId, briefText) {
  return setBriefText(questionId, briefText);
}

/**
 * Sets brief text (= accordion header) of a guide section
 */
function setBriefText(questionId, briefText) {
  // Set question brief text
  var brief = gEI(questionId + 'Brief');
  brief.innerHTML = "&nbsp;" + briefText;
}

/**
 * Alias for checkboxClickRedirect
 */
function cCR(questionId, filterId, filterString, briefText, checkboxRef) {
  return checkboxClickRedirect(questionId, filterId, filterString, briefText, checkboxRef);
}

/**
 * If a checkbox is selected, this method adds a filter, using the method used by text fields.
 */
function checkboxClickRedirect(questionId, filterId, filterString, briefText, checkboxRef) {
//alert("ccr: " + filterId + ", " + filterString); 
  if (checkboxRef.checked) {
    gEI("startNo").value = "1";
    addGuideFilter(questionId, filterId, filterString, briefText, 0, 1);
  }
  else {
    removeGuideFilter(questionId, filterId, 0, 1, briefText);
  }
  
}

/**
 * Shows clicked guide section and hides other.
 */
function showSection(sectionId) {
				var hideThis = gEI('guide' + currentSectionId + '-content');
				hideThis.style.display = 'block';
				hideThis.maxh =sh(hideThis);
				hideThis.s=(s==undefined)? 7 : s;
				cl(hideThis);
//hideThis.style.display = 'none';
//  hEI('section' + currentSectionId);
				gEI('guide' + currentSectionId + "-header").className = "bT";
				sIEI('guide' + sectionId + "-content");
				gEI('guide' + sectionId + "-header").className = "bTActive";
				currentSectionId = sectionId;
} 
 
function init() {
  initPage();
  if (document.images) {
    isCSS = (document.body && document.body.style) ? true : false; 
    isW3C = (isCSS && document.getElementById) ? true : false;
    isIE = (isCSS && document.all) ? true : false;
  }
  if (gEI("qform") != null) {
    gEI("qform").reset();
  }
  var selStringBox = gEI("selString"); 
  if (selStringBox != null) {
    selStringBox.value = selStringBox.defaultValue;
  }
  var gSelStringBox = gEI("gSelString"); 
  if (gSelStringBox != null) {
    gSelStringBox.value = gSelStringBox.defaultValue;
  }
}

function thumbs(way, id) {
				thumbsUrl = encodeURI("thumbs.php?way=" + way + "&id=" + id );
				if (window.XMLHttpRequest) {
								thumbAjaxRequest = new XMLHttpRequest();
								thumbAjaxRequest.onreadystatechange = doNothing;
								thumbAjaxRequest.open("GET", thumbsUrl, true);
								thumbAjaxRequest.send(null);
				}
				else if (window.ActiveXObject) {
								thumbAjaxRequest = new ActiveXObject("Microsoft.XMLHttp");
								if (thumbAjaxRequest) {
												thumbAjaxRequest.onreadystatechange = doNothing;
												thumbAjaxRequest.open("GET", thumbsUrl);
												thumbAjaxRequest.send();
								}
				}
				
}

function doNothing() {
}