// jQuery used

$(document).ready(function() {
	changeOptions( $("#topQChoice"), $("#topGroup").val());
	changeOptions( $("#topQChoice_bottom"), $("#topGroup_bottom").val());
});

function submitSearch ( form )
{
	var
		mode = form.searchtypeid.value,
		value = form.searchText.value;

	if (form.itemGroup.value.indexOf('VOD') >= 0 ) {
		if (value.length < 1) return false;
		url = 'http://vod.adultemart.com/dispatcher/movieSearch?mode=MODE&query=QUERY';
		if (form.itemGroup.value == 'GAYVOD') {
			url = 'http://vod.gayemart.com/dispatcher/movieSearch?mode=MODE&query=QUERY';
		}
		mode = form.searchtypeid.value;

		if( mode == '3') mode = 'Title';
		else if ( mode == '0') mode = 'StarName';
		else if ( mode == '1') mode = 'DirectorName';
		else if ( mode == '2') mode = 'StudioName';

		url = url.replace('MODE', mode);
		url = url.replace('QUERY', value);
     location.href = url;
		return false;
	}
	return true;
}

function changeOptions( searchBar, value ) {
	if(searchBar.length == 0) return;
	var
		oSelect = searchBar.get(0);
		position = $("option[selected]", searchBar).get(0).index,
		options = new Array;

	if( oSelect && oSelect.options ) {
		oSelect.length = 0;
		if( value.indexOf('DVD') >= 0 ) {
			options[ 0 ] = new Array( "3", "Keyword" );
			options[ 1 ] = new Array( "0", "Title" );
			options[ 2 ] = new Array( "1", "Performer" );
			options[ 3 ] = new Array( "2", "Director" );
		}
		else if ( value.indexOf('NOVELTY') >= 0 ) {
			options[ 0 ] = new Array( "3", "Keyword" );
			options[ 1 ] = new Array( "0", "Title" );
		}
		else if ( value.indexOf('VOD') >= 0 ) {
			options[ 0 ] = new Array( "3", "Title" );
			options[ 1 ] = new Array( "0", "Performer" );
			options[ 2 ] = new Array( "1", "Director" );
			options[ 3 ] = new Array( "2", "Studio" );
		}

		for( var i = 0; i < options.length; i ++ ) {
			var	oOption = document.createElement( "OPTION" );
			oSelect.options.add( oOption );
			oOption.value = options[ i ][ 0 ];
			oOption.text = options[ i ][ 1 ];
			if(i == position ) oOption.selected = "selected";
		}
	}
}
