var searchInProgress = false;

var sc = new Object();

function initSC()
{
	searchInProgress = false;

	with(document.forms[0])
	{
		if(typeof(country) == "object")
		{
			sc.country = new searchCriteria(country, false, "All");
			cntry = new searchCriteria(country, false, "All");
			sc.country = cntry;
		}
		if(typeof(st) == "object")
		{
			sc.st = new searchCriteria(st, false, "Anywhere");
		}
		if(typeof(type) == "object")
		{
			sc.type = new searchCriteria(type, false, "All");
		}
		if(typeof(cat) == "object")
		{
			sc.cat = new searchCriteria(cat, true, "");
		}
		if(typeof(metadata) == "object")
		{
			sc.metadata = new searchCriteria(metadata, true, "");
		}
		if(typeof(date) == "object")
		{
			sc.date = new searchCriteria(date, false, "false");
		}
		if(typeof(sigsale) == "object")
		{
			sc.sigsale = new searchCriteria(sigsale, false, "false");
		}
	}
}

function searchCriteria(elem, useValue, defaultValue) 
{
	this.elem = elem;
	this.useValue = useValue;
	this.defaultValue = defaultValue;

	this.valueChanged = valueChanged;
	this.isDefaultValue = isDefaultValue;

	if(elem.type == "select-one")
	{
		this.getValue = getSelectValue;
	}
	else if(elem.type == "text")
	{
		this.getValue = getTextValue;
	}
	else if(elem.type == "checkbox")
	{
		this.getValue = getCheckboxValue;
	}

	this.currentValue = this.getValue();
	this.previousValue = this.currentValue;
}

function getSelectValue()
{
	if(this.useValue)
	{
		return this.elem.options[this.elem.selectedIndex].value;
	}
	else
	{
		return this.elem.options[this.elem.selectedIndex].text;
	}
}

function getTextValue()
{
	if(this.useValue)
	{
		return this.elem.value;
	}
	else
	{
		return this.elem.text;
	}
}

function getCheckboxValue()
{
	if(this.elem.checked)
	{
		return "true";
	}
	else
	{
		return "false";
	}
}

function valueChanged()
{
	return ( this.previousValue != this.currentValue );
}

function isDefaultValue()
{
	return ( this.defaultValue == this.currentValue || this.currentValue.search(/^\s+$/) != -1 ) 
}

function checkForSearchCriteria(form)
{
	return true;
    var allDefaultValues = true;
	var searchCriteriaChanged = false;

	for(o in sc)
	{
		sc[o].previousValue = sc[o].currentValue;
		sc[o].currentValue = sc[o].getValue();
	}

	for(o in sc)
	{
		if(! sc[o].isDefaultValue())
		{
			allDefaultValues = false;
			break;
		}	
	}

	for(o in sc)
	{
		if(sc[o].valueChanged())
		{
			searchCriteriaChanged = true;
			break;
		}	
	}

	
	if(allDefaultValues)
	{
		alert("Please select at least one search option.");
		return false;
	}
	if(sc.country.currentValue.search(/^\s*USA\s*$/i) != -1)
	{
		var anotherNonDefaultValueFound = false;
		for(o in sc)
		{
			if(o == "country") { continue; }

			if(! sc[o].isDefaultValue())
			{
				anotherNonDefaultValueFound = true;
			}	
		}
		if(! anotherNonDefaultValueFound)
		{
			alert("Please select a state, price range and/or category.");
			return false;
		}
	}
	if(searchInProgress && ! searchCriteriaChanged)
	{
		alert("The search you submitted is still processing please wait for it to finish or change your search criteria and resubmit.");
		return false;
	}

	searchInProgress = true;
	return true;
}

function showSC()
{
	for(o in sc)
	{
		alert("this.useValue = " + sc[o].useValue);
		alert("this.defaultValue = " + sc[o].defaultValue);
		alert("this.currentValue = " + sc[o].currentValue);
		alert("this.previousValue = " + sc[o].perviousValue);
		//alert("this.valueChanged = " + sc[o].valueChanged);
		//alert("this.isDefaultValue = " + sc[o].isDefaultValue);
	}
}
