// search.js

// From http://www.somacon.com/p355.php
String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() 
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() 
{
	return this.replace(/\s+$/,"");
}

// From http://javascript.about.com/library/blqs1.htm ... And then I changed it
function qs() 
{
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
		else	// if it is set but not equal to anything
		{
			var key = parms[i];
			qsParm[key]=null;
		}
	}
	return qsParm;
} 	

// Checks the keys that are pressed in the search edit, and returns FALSE if the enter key is hit, true otherwise.
function searchSubmitEnter(obj,e,top,path)
{
	if(path == null)
	{
		path='';
	}

	var key;
	if(window.event)
	{
		key=window.event.keyCode;
	}
	else if(e) 
	{
		key=e.which;
	}
	else 
	{
		return true;
	}

	if(key == 13)
	{
		searchSubmit(obj,path,top);
   		return false;
   	}
	else
	{
	   	return true;
	}
}

function searchSubmitButton(path)
{
	var obj=document.getElementById('search');
	
	if(obj.value == "Search All Clips")
	{
		return false;
	}
	
	searchSubmit(obj,path,true);
}

function searchSubmit(obj,path,top)
{
	// If the search is empty, just return false without doing anything
	if(obj.value.trim().length == 0)
	{
		obj.value="";
		return false;
	}

	// Take the value and pull apart the keywords
	var query=obj.value.trim();
	var key="";
	var keys=new Array();
	var in_quotes=false;
	for(i=0;i<query.length;i++)
	{
		var c=query.charAt(i);

		// if it is a quote
		if(c == '"')
		{
			// and we are already in quotes
			if(in_quotes)
			{
				// Then this is the end of a quotes keyword block
				in_quotes=false;
				if(key.trim().length > 0)
				{
					keys.push(key.trim());
					key="";
				}					
			}
			else
			{
				in_quotes=true;
			}
		}
		else if(c == ' ')	// else if it is a space
		{
			// And we are not in quotes
			if(in_quotes == false)
			{
				// Then it is the end of a keyword if the size is > 0
				if(key.trim().length > 0)
				{
					keys.push(key.trim());
					key="";
				}
			}
			else
			{
				key+=c;
			}
		}
		else
		{
			key+=c;
		}
	}
	
	// Get the final keyword, if there is one
	if(key.trim().length > 0)
	{
		keys.push(key.trim());
	}	

	// Now we have all of the keywords	

	// Set the query string to just be the search flag
	var q=new Array();
	q['s']=null; // the top search flag

	// Get all the query strings if this is not a top search. a top search resets all the query strings
	if(!top)
	{
		q=qs();
	}

	var str=path+"browse.php?";
	
	// Get the query strings in an array
	var first=true;
	var done=false;
	for(key in q)
	{
		// See if it is a page setting. We reset the page settings when doing searches
		if(key == 'p')
		{
			continue;
		}

		// See if we need an and
		if(first)
		{
			first=false;
		}
		else
		{
			str+="&";
		}

		if(key == 'k')
		{
			done=true;
			str+=key+"=";
			if(q[key].length > 0)
			{
				str+=q[key]+",";
			}
			
			// Add the new keywords
			for(i=0;i<keys.length;i++)
			{
				if(i > 0)
				{
					str+=",";
				}
				str+=escape(keys[i]);
			}				
		}
		else
		{
			if(q[key] != null)
			{
				str+=key+"="+q[key];
			}
			else
			{
				str+=key;
			}
		}
	}
	
	// If there were no existing keywords
	if(!done)
	{
		// See if we need an and
		if(first)
		{
			first=false;
		}
		else
		{
			str+="&";
		}

		// Add the new keywords
		str+="k=";
		for(i=0;i<keys.length;i++)
		{
			if(i > 0)
			{
				str+=",";
			}
			str+=escape(keys[i]);
		}					
	}
	
	document.location=str;
}

function searchButtonClicked(path)
{
	if(path == null)
	{
		path='';
	}

	var obj=document.getElementById('search');
	
	if(obj.value == "Search All Clips")
	{
		return false;
	}
	
	searchSubmit(obj,path,true);
}