/*
	********************** Preforms the advanced search *******************
	** 2008-08-19
	***********************************************************************
*/



/*
	Creates the Advanced Search class
*/
function advSearch(divName) {
	this._divName = divName;
	this.updateCriterias();
}

// Private variables
advSearch.prototype._divName;
advSearch.prototype._divNameList = 'recept_content';
advSearch.prototype._divNameCount = 'antal_resultat';

advSearch.prototype._queryString;
var advSearchWaiting;

advSearch.prototype._ajaxAdvSearchUrl = '/inc/ajax/ajax.adv.search.php';
advSearch.prototype._ajaxAdvSearchListUrl = '/inc/ajax/ajax_sok_resultat.php';
advSearch.prototype._ajaxAdvSearchCountUrl = '/inc/ajax/ajax_sok_antal.php';

/*
	Update the recepie list, recepie count and the links in the critierias div layer
*/
advSearch.prototype.updateHits = function() {
	this.getQueryString();
	
	ajax = new ajaxObject(this._ajaxAdvSearchListUrl, putintodiv, this._divNameList);
	ajax.update(this._queryString + "&urlstring=" + escape(location.href));
	
	ajax = new ajaxObject(this._ajaxAdvSearchCountUrl, putintodiv, this._divNameCount);
	ajax.update(this._queryString);
	
	ajax = new ajaxObject(this._ajaxAdvSearchUrl, this.changeCriterias);
	ajax.update(this._queryString + '&xml=true');
}

/*
	Update the criterias in the div layer
*/
advSearch.prototype.updateCriterias = function() {
	this.getQueryString();
	ajax = new ajaxObject(this._ajaxAdvSearchUrl, putintodiv, this._divName);
	ajax.update(this._queryString);
}

/*
	Generate the query string from the FORM
*/
advSearch.prototype.getQueryString = function() {
	var theForm=document.forms['ajaxSearch'];
	var request = '';
	for (var i=0; i<theForm.length; i++)
		request = request + '&' + theForm.elements[i].id + '=' + escape(theForm.elements[i].value);
		
	this._queryString = request;
}

/*
	Toggle between showing and hiding the property links inside the div layers
*/
advSearch.prototype.toggleLinks = function(obj, property, count) {
	for (var i=4; i<=count; i++) {
		div = document.getElementById("divLink_" + property + "_" + i);
		div.style.display = (div.style.display == 'none') ? '' : 'none';
	}
	obj.childNodes[0].nodeValue = (obj.childNodes[0].nodeValue == 'Visa alla typer') ? 'Visa färre typer' : 'Visa alla typer';
	obj.blur();
}

/*
	Toggle between showing and hiding the div layers with the property links
*/
advSearch.prototype.toggleProperties = function(obj, count) {
	for (var i=2; i<=count; i++) {
		div = document.getElementById("divBox_" + i);
		div.style.display = (div.style.display == 'none') ? '' : 'none';
	}
	obj.childNodes[0].nodeValue = (obj.childNodes[0].nodeValue == 'Visa alla alternativ') ? 'Visa färre alternativ' : 'Visa alla alternativ';
	document.getElementById('propArrow').src = (document.getElementById('propArrow').src.indexOf('/images/arrow_down.gif') > 0) ? '/images/arrow_up.gif' : '/images/arrow_down.gif';
	obj.blur();
}

/*
	Toggle between on/off for the criteria
*/
advSearch.prototype.toggleCriteria = function(obj, type, value) {
	if (obj.className != 'disabledProperty' && advSearchWaiting != true) {
		document.getElementById('offset').value='0';
		var theForm = document.forms['ajaxSearch'];
		eval("input = theForm." + type + ";");
		if (input.value.indexOf(',' + value + ',') > -1) {
			input.value = input.value.replace(',' + value + ',', '');
			obj.className = '';
		} else {
			input.value = input.value + ',' + value + ',';
			obj.className = 'selectedProperty';
		}
		
		advSearchWaiting = true;
		this.updateHits();
	}
	
	obj.blur();
}	

/*
	Update the status of the links inside the div layers
*/
advSearch.prototype.changeCriterias = function(text, status, xml) {
	if (status==200) {			
		links = xml.getElementsByTagName("link");
		
		for (i=0; i<links.length; i++) {
			linkIds = links[i].getElementsByTagName("id");
			counts = links[i].getElementsByTagName("count");
			
			for (j=0; j<counts.length; j++) {
				linkId = linkIds[j].childNodes[0].nodeValue;
				count = counts[j].childNodes[0].nodeValue;
				obj =  document.getElementById(linkId);
			
				if (obj) {
					cName = (obj.className.indexOf('selected') > -1) ? 'selected' : '';
					obj.className = (count > 0) ? cName + 'Property'  : 'disabledProperty' + cName;
				}
			}
		}
		
		advSearchWaiting = false;
	}
}

/*
	Add the ingridients to the FORM and div layer
*/
advSearch.prototype.addIngredient = function(div) {
	ing = document.forms['frmIngredients'].ingredient;
	
	if (ing.value != '' && ing.value != 'ex. Mjölk') {
		document.getElementById('offset').value='0';
		var theForm = document.forms['ajaxSearch'];
		
		if (div == 'addedIngredients')
			theForm.dingadd.value = theForm.dingadd.value + ',' + ing.value + ',';
		else
			theForm.dingrem.value = theForm.dingrem.value + ',' + ing.value + ',';
		
		newText = document.createTextNode(ing.value);
		newLink = document.createElement("A");
		newSpan = document.createElement("SPAN");
		newLink.href = 'javascript: ;';
		newLink.onclick = function(){removeIngredient(this, div);} 
		newLink.appendChild(newText);
		newSpan.appendChild(newLink);
		
		document.getElementById(div).appendChild(newSpan);
		document.getElementById(div).style.display = 'inline';
		
		ing.value = '';
		
		this.updateHits();
	}
}

/*
	************ PUBLIC FUNCTIONS ***********
*/
/*
	Public function called from the onclick in the links
	Remove the ingredient from the FORM and div layer
*/
function removeIngredient(obj, div) {
	if (obj) {
		document.getElementById('offset').value='0';
		var theForm = document.forms['ajaxSearch'];
		
		if (div == 'addedIngredients')
			theForm.dingadd.value = theForm.dingadd.value.replace(',' + obj.childNodes[0].nodeValue + ',', '');
		else
			theForm.dingrem.value = theForm.dingrem.value.replace(',' + obj.childNodes[0].nodeValue + ',', '');
			
		divObj = document.getElementById(div);
		divObj.removeChild(obj.parentNode);
		
		if (divObj.getElementsByTagName("A").length == 0)
			divObj.style.display = 'none';
		
		objSearch.updateHits();
	}
}
