
// Assign global variables 

	var woutPlus = "http://www.cooksillustrated.com/search/images/btn_plus_OFF.gif";
	var withPlus = "http://www.cooksillustrated.com/search/images/btn_plus_ON.gif";
	var woutMinus = "http://www.cooksillustrated.com/search/images/btn_minus_OFF.gif";
	var withMinus = "http://www.cooksillustrated.com/search/images/btn_minus_ON.gif";
	
	var sform = document.searchform;

	var plusSign = "+";
	var minusSign = "!";
	
function searchIngredients()
{
	var withIngre = '';
	var withoutIngre = '';
	
	document.searchform.sIngre.value = "yesSearch"; 
		
	return false;
}

// Clear the hidden fields 
function clearHiddenFields()
{
	document.searchform.wIngredients.value = "";
	document.searchform.woIngredients.value = "";	
}

//Expand/Collapse functionality for the four filter options on the right side of the search
function toggleDisp( me, divBodyName, collapsedImage, expandedImage )
{
	var categoryList = document.getElementById(divBodyName);	
	var collapsedImagePath = "images/"+collapsedImage;
	var expandedImagePath = "images/"+expandedImage;
	
	if( categoryList.style.display == "none"){ me.src = expandedImagePath; categoryList.style.display = "block";}
	else{ me.src = collapsedImagePath; categoryList.style.display = "none";}
}

//Function to swap image and initiate the filter by ingredient functionality
function swapImage(me)
{
	var withIngredients = document.searchform.wIngredients;
	var withoutIngredients = document.searchform.woIngredients;
	
	if (me.src == woutPlus) { unCheckOpposite(me, withoutIngredients); addIngredient(me, withIngredients); 
	                          me.src = withPlus; common();}
	if (me.src == withPlus) { removeIngredient(me, withIngredients);
							  me.src = woutPlus; common();}
	if (me.src == woutMinus) { unCheckOpposite(me, withIngredients); addIngredient(me, withoutIngredients); 
						      me.src = withMinus; common(); }
	if (me.src == withMinus) { removeIngredient(me, withoutIngredients); 
							  me.src = woutMinus; common();}
}

// This function make sure to uncheck the opposite image 
function unCheckOpposite(me, theList)
{
	var ingreName = me.id.toString();
	
	if (ingreName.indexOf(plusSign) != -1){otherElement = ingreName.replace(plusSign, minusSign);}
	else if (ingreName.indexOf(minusSign) != -1){otherElement = ingreName.replace(minusSign, plusSign);}
	
	uncheckIngre = document.getElementById(otherElement);
	
	if (uncheckIngre.src == withMinus){removeIngredient(uncheckIngre, theList);	uncheckIngre.src = woutMinus;}
	else if (uncheckIngre.src == withPlus){	removeIngredient(uncheckIngre, theList); uncheckIngre.src = woutPlus;}
}

// This function is initiated by swapImage function, it will add the ingredient (the opted ingredient which needs to be included in filter by search option) to the hidden variable "withIngredients"
function addIngredient(selectedIngre, addInput)
{
 	resultedIngredients = addInput.value.toString();
	selectedIngredients = removeExtraSep(resultedIngredients);
	selIngredient = selectedIngre.id.toString();

	if (selIngredient.indexOf(plusSign) != -1)
	{ 
		selIngreArray = selIngredient.split(plusSign);
		selIngreID = selIngreArray[0];
		selIngre =  plusSign+selIngreArray[1];
	}
	else if (selIngredient.indexOf(minusSign) != -1)
	{
		selIngreArray = selIngredient.split(minusSign);
		selIngreID = selIngreArray[0];
		selIngre =  "-"+selIngreArray[1];
	}
	
	if (selectedIngredients != ''){addInput.value = selectedIngredients+','+selIngreID;}
	else {addInput.value = selIngreID;}

	//alert(addInput.value);
	
	selShowIngre = document.searchform.showSelectedIngredients;
	if (selShowIngre.value != ''){selShowIngre.value = selShowIngre.value+','+selIngre;}
	else{selShowIngre.value = selIngre;}
}


// This function is initiated by swapImage function, it will remove the ingredient (the opted ingredient which needs to be included in filter by search option) from the hidden variable "withoutIngredients"
function removeIngredient(selectedIngre, removeInput)
{
	selectedIngredients = removeInput.value.toString();
	selIngredient = selectedIngre.id.toString();
	
	if (selIngredient.indexOf(plusSign) != -1)
	{ 
		selIngreArray = selIngredient.split(plusSign);
		selIngreID = selIngreArray[0];
		selIngreName =  plusSign+selIngreArray[1];
	}
	else if (selIngredient.indexOf(minusSign) != -1)
	{
		selIngreArray = selIngredient.split(minusSign);
		selIngreID = selIngreArray[0];
		selIngreName =  "-"+selIngreArray[1];
	}
	
	if (selectedIngredients != '')
	{
		if(selectedIngredients.indexOf(selIngreID) != -1)
		{
			resultedIngredients = selectedIngredients.replace(selIngreID, '');
			resultedIngredients = removeExtraSep(resultedIngredients);
			removeInput.value = firstExtraSep(resultedIngredients);
		}
	}
	
	selShowIngre = document.searchform.showSelectedIngredients;
	selShowIngreValue = selShowIngre.value.toString();
	if (selShowIngre.value != ''){
		if(selShowIngreValue.indexOf(selIngreName) != -1){
		resultedIngredients = selShowIngreValue.replace(selIngreName, '');
		resultedIngredients = removeExtraSep(resultedIngredients);
		selShowIngre.value = firstExtraSep(resultedIngredients);}}
}

//This is the dumy function to make the check/uncheck functionality on the images work
//TBD : Still need to work on this one, dont know why the swap image functionality really needs this. 
function common()
{
	if( document.searchform.tempATK.style.display == "none"){ document.searchform.tempATK.style.display = "block";}
	else{document.searchform.tempATK.style.display = "none";}
}

// Function to remove the extra seperator in the middle of the string
function removeExtraSep(selectedIngre)
{
	selIngre = selectedIngre;
	while (selIngre.indexOf(',,') != -1)
	{
		selIngredient = selIngre.replace(',,',',');
		selIngre = selIngredient;
	}

	return selIngre;
}


// Function to remove extra seperator at the starting of the string
function firstExtraSep(selectedIngre)
{
	selIngre = selectedIngre;
	if ( Left(selIngre, 1) == ',' )
	{
		selIngre = Right( selIngre, (String(selIngre).length - 1))
	}
	return selIngre
}

// 
function showIngredients(me)
{
	divName = me.id+"Container";
	var categoryListToDisplay = document.getElementById(divName);

	getDisplayedCategoryList = document.searchform.displayedCategoryDiv;
	
	if(getDisplayedCategoryList.value.toString() == ""){ categoryListToDisplay.style.display = "block"; getDisplayedCategoryList.value = divName;}
	else{
	var categoryListDisplayed = document.getElementById(getDisplayedCategoryList.value);
	categoryListDisplayed.style.display = "none";
	categoryListToDisplay.style.display = "block";
	getDisplayedCategoryList.value = "";
	getDisplayedCategoryList.value = divName;}
}


///The Left function returns a specified number of characters from the left side of a string.
function Left(str, n)
{
	if (n <= 0)     
    	return "";
    else if (n > String(str).length)
    	return str;
    else
    	return String(str).substring(0,n);
}

///The Right function returns a specified number of characters from the right side of a string.
function Right(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else { 
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

// JavaScript Document	
function hideCategoryDropdown(d)
{
	document.getElementById(d).style.display = "none";
}

function showCategoryDropdown(d)
{
	document.getElementById(d).style.display = "block";
}
		
function DtypesChange(selectedContent)
{
	if( selectedContent != "Recipes"){hideCategoryDropdown('detailToolsColumn');}
}
	
function searchForTheChange(category)
{
	selectedCategories = document.getElementById(category);
	selectedContent = selectedCategories.options[selectedCategories.selectedIndex].value;
	rsearchCat = document.getElementById("rsearch");
	rsearchCat.value = selectedContent;
	document.searchform.submit();
}


function assignSelectedCategory()
{
	/*selectedValue = document.forms["searchform"].rsearch.value;
	for (i=0;i<document.forms["searchform"].DTYPES.length;i++){
	if (selectedValue == document.forms["searchform"].DTYPES.options[i].value){
			document.forms["searchform"].DTYPES.options[i].selected = true;}
	}	*/	
}
	
function searchForCategory(categorySelected)
{
	//document.searchform.searchcategory.value = categorySelected.toString();	document.searchform.submit();
	var selected = categorySelected.split(',')
	if (selected[0] == 'course')
	{
		document.searchform.course.value = selected[1];
		document.searchform.filter.value = "YES";
	}
	if (selected[0] == 'cuisine')
	{
		document.searchform.cuisine.value = selected[1];
		document.searchform.filter.value = "YES";
	}
	if (selected[0] == 'dishtype')
	{
		document.searchform.dishtype.value = selected[1];
		document.searchform.filter.value = "YES";
	}
	document.searchform.submit();
}

function searchForIngre()
{
	document.searchform.filter.value = "YES";
	document.searchform.submit();
}

function sortit(sorder, rfrom)
{
	if (rfrom == "msearch")
	{
		document.searchform.sortOrder.value = sorder;
		document.searchform.submit();
	}
	else
	{
		document.searchform.sortOrder.value = sorder;
		document.searchform.filter.value = "YES";
		document.searchform.submit();
	}
}

// JavaScript Document