

// SUBMITS NAMED FORM, USED FOR LOADING FORMS FROM LINKS
function submitForm(frmName){ document.forms[frmName].submit(); return false }

// SET VIDEOFORMAT FORM VALUE FOR NAMED FORM
function toggleFormat(frmName, xview,  xvideoFormat, xsrchUrl){
	with(document.forms[frmName]){
		action = xsrchUrl;
		view.value = xview;
		videoFormat.value = xvideoFormat;
	}
	return submitForm(frmName);
}


// GETINDEXPAGEURL
// Used for formatting a link to return the user to the appropriate index page from a given title.
// Set thumbsPerIndexPage to match whatever is set in search_index_pages_generate.asp (numOfRows*numOfCells)
var thumbsPerIndexPage = 24;
function getIndexPageUrl(idx, view, ttl, style){
	var pg = Math.ceil(idx/thumbsPerIndexPage);
	pg=pg<1?1:pg;
	pg=pg>9?pg:'0'+pg; // pad the page number for the filename (i.e 01, 02, etc).
	style = style?style:'';
	var idxPage = (view=='vid'?'index_video':'index_photo')+pg+'.asp';
	return '<a href="/backroom/'+idxPage+'" class="'+style+'" '
	+ 'onMouseOver="return info(\''+ttl+'\')" onMouseOut="return info()">';
}

// FIND POSITION INDEX IN JS FILE BY DATABASE KEYWORD ID
function getKwdArrayIndex(kwdId, arr, idx){
	// This function looks up the array item index in backroom_keywords.js using database title or model ID
	var i=arr.length-1; var id=-1;
	if(idx){do if(kwdId.toLowerCase()==arr[i][idx].toLowerCase()){id=i;break} while(i--)}
	else { do if(kwdId==arr[i][0]){id=i;break} while(i--) }
	return id>arr.length-1?arr.length-1:id;
}


// TRIM WHITESPACE
function trim(str){ return str.replace(/\W*(.*)\W*/g,'$1') }

// STRIP HTML TAGS
function stripHtml(str){ return trim(str.replace(/<[^>]*>/g,'')) }

// WINDOW STATUS
function info(msg){ window.status = msg?msg:''; return true }

// ARRAYTOLIST
function arrayToList(arr,delim){
	var lst = '';
	var i = arr.length-1;
	if(i>=0) do lst += (i<arr.length-1?delim?delim:',':'')+arr[i]; while(i--);
	return lst;
}

// LISTTOARRAY
function listToArray(lst,delim){
	var arr=new Array(0);
	if(!lst) return arr;
	if(!delim)delim=',';
	while(lst.indexOf(delim)>=0){
		arr[arr.length]=lst.substring(0,lst.indexOf(delim));
		lst=lst.substring(lst.indexOf(delim)+1,lst.length);
		if(arr[arr.length-1]==undefined)arr[arr.length-1]='';
	}
	arr[arr.length]=lst;
	return arr;
}
// TOOL TIP DIV OBJECT CONSTRUCTOR
// <div style="position:relative" id="myDiv"></div>
// USAGE:
// var myTip = new toolTipObj('myDiv');
// myTip.write('my content') to set html content
// myTip.show(1) to show
// myTip.show(0) to hide

function toolTipObj(id){
	this.id = id;
	this.obj = getObj(id);
	this.css = getCss(this.obj);
	this.show = setVis;
	this.write = setHtml;
}
function getObj(id){ with(document) return document.getElementById?getElementById(id):document.all?all[id]:null }
function getCss(o){ return o.style?o.style:o }
function setVis(vis){ this.css.visibility=vis?'visible':'hidden' }
function setHtml(htm){ this.obj.innerHTML=htm }


// BEGIN FAVORITES METHODS - requires cookieObj (in global.js)

function favoritesObj(id, objCookie, key){
	this.id = id;
	this.cookie = objCookie;
	this.favKey = key;
	this.append = favObjListAppend;
	this.arrContains = favObjArrContains;
	this.listToArr = favObjListToArr;
	this.isFav = favObjIsFav;
	this.getFav = favObjGetFav;
	this.addFav = favObjAddFav;
	this.delFav = favObjDelFav;
	this.delAllFav = favObjDelAllFav;
	return this;
}

function favObjListAppend(val,lst,delim){ lst+=(val!=''&&val.length>0)?(lst!=''?(delim?delim:','):'')+val:''; return lst }
function favObjListToArr(lst,delim){ return lst!=''?lst.split(delim?delim:','):[] }
function favObjArrContains(v,a,i){
	i = a.length-1; if(i>-1 && a[0]) do{if(a[i].toString().toLowerCase()==v.toString().toLowerCase())return i} while(i--);
	return -1;
}
function favObjGetFav(){ with(this)return cookie.get(favKey) }
function favObjIsFav(cid){ with(this)return arrContains(cid,listToArr(getFav(),'|'))>=0?1:0 }
function favObjAddFav(cid,title){
	with(this){
		var ids = getFav();
		if((ids.indexOf(cid)<0)){
			cookie.set(append(cid,ids,'|'), favKey);
			return '<b>'+ title + ' has been added to your <i>My TitanMen</i> favorites list.</b>';
		}
		return '<b>' + title + ' is already in your <i>My TitanMen</i> favorites list.</b>';
	}
}
function favObjDelFav(cid,title,objId,conf){
//alert(cid+' '+title+' '+objId+' '+conf)
	with(this)
	if(!conf){
		return '<b>Delete '+title+' from your &quot;My TitanMen&quot; favorites list?</b>\n'
		+ '<table cellpadding=0 cellspacing=0 width=240>\n<tr>\n'
		+ '<td width=100 align=center><a href="#" onClick="'+objId+'.delFav(\''+cid+'\',1)" '
		+ 'class=butn>yes</a></td>\n'
		+ '<td width=40>&nbsp;&nbsp;&nbsp;</td>\n'
		+ '<td width=100 align=center><a href="#" onClick="'+objId+'.resetButnBar(\'\',1)" class=butn>no</a>\n</td>\n'
		+ '</tr>\n</table>\n';
	}
	else {
		with(this){
			var fav = listToArr(getFav(),'|'); var ids = ''; var i = fav.length-1;
			if(i>=0) do if(fav[i] != cid && fav[i]!='') ids = append(fav[i],ids,'|'); while(i--);
			cookie.set(ids, favKey);
			return '';
		}
	}
}
function favObjDelAllFav(func,objId,conf){
	var htm = '<div style="width:100%"><div align=center style="padding:6"><b>'
	with(this)
	if(!conf){
		htm += 'Are you sure you want to clear your favorites list?</b></div>\n'
		+ '<table cellpadding=0 cellspacing=0 align=center width=240>\n<tr>\n'
		+ '<td width=100 align=center>'
		+ '<a href="#" onClick="'+func+'(1)" style="width:100" class=butn>yes</a></td>\n'
		+ '<td width=40>&nbsp;&nbsp;&nbsp;</td>\n'
		+ '<td width=100 align=center><a href="#" onClick="'+objId+'.resetButnBar()" style="width:100" '
		+ 'class=butn>no</a>\n</td>\n';
	}
	else if(conf==1){
		htm += 'Clicking below will delete everything from your &quot;My TitanMen&quot; favorites list.</b></div>\n'
		+ '<table cellpadding=0 cellspacing=0 align=center width=240>\n<tr>\n'
		+ '<td width=100 align=center>'
		+ '<a href="#" onClick="'+func+'(2)" style="width:100" class=butn>delete all</a></td>\n'
		+ '<td width=40>&nbsp;&nbsp;&nbsp;</td>\n'
		+ '<td width=100 align=center><a href="#" onClick="'+objId+'.resetButnBar()" style="width:100" '
		+ 'class=butn>cancel</a>\n</td>\n';
	}
	else {
		cookie.set('', favKey);
		window.location.reload();
	}
	htm += '</tr>\n</table>\n</div>';
	return htm;
}

// END FAVORITES METHODS

