
var defaultValueSearchInput = 'eg brand, description, code, keyword (or combo of)' ;
function fnClearDefaultSearchInput() {
	var searchBox = document.getElementById( 'searchBox' );
	if ( searchBox != null ) {
		if ( searchBox.value.trim() == defaultValueSearchInput) {
			searchBox.value = '';
			searchBox.className = '';
		}
	}
};
var searchTerm = null;
doSearch = function( start ) {
	var sortSel = -1;
	var sort = document.getElementsByName( 'sort' );
	if ( sort != null ) {
		for ( var i = 0; i < sort.length; ++i ) {
			var opt = sort[i];
			if ( opt != null ) {
				if ( opt.checked ) {
					sortSel = opt.value;
					break;
				}
			}
		}
	}
		
	var capSel = '50';
	var cap = document.getElementById( 'displayLimits' );
	if ( cap != null ) {
		capSel = cap.options[ cap.options.selectedIndex ].text; 
	}
	var searchBox = document.getElementById( 'searchBox' );
	if ( searchBox == null ) return;
	
	var restrict = null;
	var brandBox = document.getElementById( 'resultBrands' );
	if ( brandBox != null ) {
		if ( searchTerm.trim() == searchBox.value.trim() ) {
			if ( brandBox.selectedIndex > 0 ) {
				var brandOpt = brandBox.options[ brandBox.selectedIndex ];
				if ( brandOpt != null ) {
					restrict = '&restrict=ProdGrp&restrictTo=' + brandOpt.value; 
				}
			}
		}
	}
	if ( searchBox.value.trim() != '' ) {
		location.href = dbPath() + '/0/013B4B14F30F5D06CC25747D007D2778?openDocument' 
			+ ( start!= null ? '&start=' + start : '' ) + '&cap=' + capSel  
			+ ( sortSel > -1 ? '&sort=' + sortSel : '' ) + ( restrict != null ? restrict : '' ) + '&search=' + searchBox.value;
	} else {
		alert( 'please enter search terms' );
	}
};
searchKeyPress = function(ev) {
	switch ( ev.keyCode ) {
		case 10:
		case 13:
			if ( ev.preventDefault) ev.preventDefault();
                                         if ( ev.stopPropagation ) ev.stopPropagation();
			doSearch();
			break;
		default:
			//propagate
			break;
	}
}
sortSelected = function() {
	var searchBox = document.getElementById( 'searchBox' );
	if ( searchBox == null ) return;
	if ( ( searchBox.value.trim() != '' ) && ( searchBox.value.trim() != defaultValueSearchInput ) ) { doSearch(); }
}
clearSearch = function() {
	var searchBox = document.getElementById( 'searchBox' );	
	if ( searchBox != null ) searchBox.value = '';
	
	var brandBox = document.getElementById( 'resultBrands' );
	if ( brandBox != null ) brandBox.selectedIndex = 0;
}
searchLoad = function() {
		
	var searchBox = document.getElementById( 'searchBox' );
	
	//register keypress listener
	if ( searchBox != null ) {
		if ( searchBox.addEventListener ) {
			searchBox.addEventListener( "keypress", searchKeyPress, true );
		} else {
			searchBox.attachEvent( "onkeypress", searchKeyPress );
		}
	}
	
	//load query to search box
	var sort = document.getElementsByName( 'sort' );
	
	var qparams = location.search.split( '&' );
	for ( var i = 0; i < qparams.length; ++i ) {
		var nvpair = qparams[i].split( '=' );
		if ( nvpair.length == 2 ) {
			if ( nvpair[0].toLowerCase() == 'search' ) {
				if ( searchBox != null ) searchBox.value = decodeURI( nvpair[1] );
				searchTerm = decodeURI( nvpair[1] );
			}
			if ( nvpair[0].toLowerCase() == 'sort' ) {
				if ( sort != null ) {
					for ( var j = 0; j < sort.length; ++j ) {
						var opt = sort[j];
						if ( opt != null ) opt.checked = ( nvpair[1] == opt.value );
					}
				
				}
			}
		}
	}
	
	//regsiter change listener
	if ( sort != null ) {
		for ( var j = 0; j < sort.length; ++j ) {
			var opt = sort[j];
			if ( opt != null ) { 
				if ( opt.addEventListener ) {
					opt.addEventListener( "click", sortSelected, true );
				} else {
					opt.attachEvent( "onclick", sortSelected );
				}
			}
		}
	}
};
