
// THIS TEMPLATE REQUIRES THE GENERATED DATA FILE BACKROOM_KEYWORDS.JS

// REWRITE THIS ENTIRE SCRIPT AS AN OBJCT
// GETREQUEST() WON'T WORK HERE ANYMORE, SHOULD BE REWRITTEN TO ACCEPT BOTH FORM AND REQUEST VARIABLES

var srchInProgress = 0;
var REQUEST = null;
var MENUFIELDS = null;

function submitSrch(){
	var idTmp = '';
	if(srchInProgress>0) return false;
	srchInProgress = 1;
	with(document){
		var det = '';
		for(var i=0;i<MENUFIELDS.length;i++){
			if(srchonline[MENUFIELDS[i]]){
				with(srchonline[MENUFIELDS[i]]) det = stringAppend(selectedIndex>0?options[selectedIndex].text:'',det);
			}
		}
		if(!det){
			srchonline.goButn.disabled = false;
			srchInProgress = 0;
			return false;
		}
		if(getKwdArrayIndex){
			idTmp = srchonline.titleId.options[srchonline.titleId.selectedIndex].value
			if(idTmp != ''){
				srchonline.dir.value = TITLES[getKwdArrayIndex(idTmp, TITLES)][4];
				srchonline.c.value = getCategoryId(idTmp);
				document.location.href = '/backroom/'+srchonline.dir.value+'/index.asp?c='+srchonline.c.value;
				return false;
			}
		}
		if(srchonline.keywordId.selectedIndex > 0){ // this returns image galleries
			srchonline.det.value = det;
		}
		else { // this returns title listings
			location.href = '/studio_index.asp?model='+escape(det)+'&c='+srchonline.c.value;
			return false;
		}
		srchonline.goButn.disabled = true;
	}
}


function getCategoryId(productId){
	for(i=0;i<Products.length;i++)if(Products[i][2]==productId) return Products[i][4];
	return 0;
}
function getOptions(fld,a,idx){
	with(document){
		srchonline[fld].length = a.length+1;
		for(var i=0; i<a.length; i++) {
			srchonline[fld][(i+1)].value = a[i][0];
			srchonline[fld][(i+1)].text = a[i][1];
		// uncomment this line to make search form retain menu options on subsequent pages.
		//	srchonline[fld][(i+1)].selected=(arrayContainsNoCase(a[i][0],listToArray(REQUEST[fld]))>=0?1:0);
		}
	}
}

function setOptions(fld,a){
	with(document){
		if(srchonline[fld].length == a.length+1) return;
		var idx = srchonline[fld].selectedIndex
		var val = srchonline[fld][idx].value
		srchonline[fld].length = a.length+1;
		var i = a.length-1;
		do{
			srchonline[fld][(i+1)].value = a[i][0];
			srchonline[fld][(i+1)].text = a[i][1];
		} while(i--);
	}
}

// This version restricts form fields to single word search
function restrictFieldNew(f1){
	with(document){
		srchonline.goButn.disabled = false;
		for(i=0;i<MENUFIELDS.length;i++) if(srchonline[MENUFIELDS[i]].name!=f1) srchonline[MENUFIELDS[i]][0].selected=1;
		srchonline.view.value = (f1=='titleId'?'vid':'img');
	}
}

// This version restricts form fields to context sensitive title/model searches
function restrictField(f1,a1,f2,a2){
	with(document){
		srchonline.goButn.disabled = false;
		srchonline.view.value = (f1=='titleId'?'vid':'img');
		if(a1){
			// Reset the both lists if user selects top (empty) item
			if(srchonline[f1].selectedIndex<1){
				setOptions(f1,a1);
				setOptions(f2,a2);
				srchonline[f1][0].text = 'choose a ' + (f1=='modelId'?'model':'title') + 's';
			}
			// Otherwise, restrict list to only show those items that are related to selected title or model
			else{
				var itm = a1[findIndex(a1,0,srchonline[f1].options[srchonline[f1].selectedIndex].value)];
				if(itm[2].length>0){
					var refItm = a1[findIndex(a1,0,srchonline[f1].options[srchonline[f1].selectedIndex].value)];
					var refIdx = arrayContainsNoCase(srchonline[f2].options[srchonline[f2].selectedIndex].value,refItm[2])>=0?srchonline[f2].selectedIndex:0;
					var refVal = srchonline[f2][refIdx].value;
					srchonline[f2].length = itm[2].length+1;
					for(var i=0; i<itm[2].length; i++){
						var val = a2[findIndex(a2,0,itm[2][i])];
						srchonline[f2][(i+1)].text = val?val[1]:'';
						srchonline[f2][(i+1)].value = val?val[0]:'';
						if(val[0]==refVal)srchonline[f2][(i+1)].selected = true;
					}
					if(srchonline[f1].length-1<a1.length) srchonline[f1][0].text = srchonline[f1].selectedIndex>0?'reset':'choose a ' +(f1=='modelId'?'model':'title')
					srchonline[f2][0].text = srchonline[f2].selectedIndex>0?'reset':'choose a '+(f2=='modelId'?'model':'title');
				}
				else { // if the selection has no options clear the options list
					srchonline[f2].length = 2;
					srchonline[f2][0].text = 'reset';
					srchonline[f2][1].text = ''
					srchonline[f2][1].value = '';
					srchonline[f2][1].selected = true;
				}
			}
		}
	}
}


// Creates a global hash array derived from the URL query string. Used to mimic ASP Request object
function getRequest(){
	var arr = [];
	var qs = document.location.search;
	if(qs){
		qs = '&'+qs.substring(1,qs.length);
		while(qs.indexOf('&')>=0){
			qs = qs.substring(qs.indexOf('&')+1,qs.length);
			var key = qs.indexOf('=')>=0?qs.substring(0,qs.indexOf('=')):'';
			if(key!='') arr[key] = qs.substring(qs.indexOf(key)+key.length+1,qs.indexOf('&')>=0?qs.indexOf('&'):qs.length);
		}
	}
	else{
		arr['view'] = srchBarView;
		arr['titleId'] = srchBarTitleId;
		arr['modelId'] = srchBarModelId;
		arr['keywordId'] = srchBarKeywordId;

	}
	return arr;
}

function findIndex(arr,idx,val){ for(var i=0; i<arr.length; i++) if(arr[i][idx]==val) return i; return -1 }
function stringAppend(val,lst){ return (lst.length>0)?lst += val.length>0?', '+val:'':val }
function arrayContainsNoCase(val,arr){ // returns -1 if not found
	if(!arr) return -1;
	var i = arr.length-1;
	if(i>-1) do { if(arr[i].toString().toLowerCase()==val.toString().toLowerCase()) return i } while(i--);
	return -1;
}

function setSearchOptions(){
	REQUEST = getRequest();
	MENUFIELDS = ['titleId','modelId','keywordId'];
	getOptions('titleId',TITLES);
	getOptions('modelId',MODELS);
	getOptions('keywordId',KEYWORD);
	document.srchonline.view.value = REQUEST['view'];
	if(document.srchonline.titleId.selectedIndex>0)restrictField('titleId',TITLES,'modelId',MODELS);
	else if(document.srchonline.modelId.selectedIndex>0) restrictField('modelId',MODELS,'titleId',TITLES);
}

// INITIALIZE THE SEARCHBAR AFTER THE SEARCHBAR FORM HAS BEEN LOADED BY CALLING setSearchOptions()
