function previewMessage(messID, message, topic, doorId, clientId, width, height){
	url = document.galleryForm.urlStr.value;
	doorStr = document.galleryForm.doorName.value;
	previewWindow = window.open( url+"php/start_player_"+doorStr+"/door="+doorId+"&cl="+clientId+"&AID="+topic+"&id="+messID+"&message="+message+(noRanking?"&exp=1":""),"previewWindow","width="+(width+200)+",height="+(height+200)+",toolbar=no,resizable=no,scrollbars=no");
	if(previewWindow)
		previewWindow.focus();
}

function postMessage(topicId,width, height){
	if(!width) width = 719;
	if(!height) height = 509;
	
	url = document.galleryForm.dynamicURL.value;
	doorStr = document.galleryForm.doorName.value;
	createWindow = window.open( url+doorStr+"/?AID="+topicId,"createWindow","width="+width+",height="+height+"");
	createWindow.focus();	
}

function searchGallery(){
	
	if (document.gallerySearchForm.sby[0].checked && document.gallerySearchForm.keystr.value.length<=2){
		alert('Please check your keyword it should be at least 3 characters long');
		return;
	}
	
	searchByOk = 0;
	for(i=0;i<document.gallerySearchForm.sby.length;i++){
		if(document.gallerySearchForm.sby[i].checked){
			searchByString = document.gallerySearchForm.sby[i].value;
			searchByOk = 1;
		}
	}
	
	if(searchByOk==0){
		alert('Please check your search type');
		return;
	}
		
	if (document.gallerySearchForm.keystrSelect && document.gallerySearchForm.sby[1].checked) {
		
		if (document.gallerySearchForm.keystrSelect.selectedIndex == 0) {
			alert('Please select a state.');
			return;
		}
		
		locationToGo = document.location.toString()+'&sby='+searchByString+'&keystr='+(document.gallerySearchForm.keystrSelect.options[document.gallerySearchForm.keystrSelect.selectedIndex].value);
	}
	else {
		locationToGo = document.location.toString()+'&sby='+searchByString+'&keystr='+(document.gallerySearchForm.keystr.value);
	}
	
	document.location = locationToGo;
}



function searchSimpleGallery()
{
	var searchTermValue = "";
	
	if (document.gallerySearchForm.sby[1].checked)
	{	// this is the select box
		if (document.getElementById('keystrSelect').selectedIndex == 0) {
			alert('Please select a state from the dropdown.');
			return;
		} else {
			searchTermValue = document.getElementById('keystrSelect').options[document.getElementById('keystrSelect').selectedIndex].value;
		}
	}
	else
	{	// this is the text box
		if (document.getElementById('keystrText').value.length <= 2) {
			alert('Your search term must be at least three characters long.');
			return;
		} else {
			searchTermValue = document.getElementById('keystrText').value;
		}
	}



	searchByOk = 0;
	for(i=0;i<document.gallerySearchForm.sby.length;i++){
		if(document.gallerySearchForm.sby[i].checked){
			searchByString = document.gallerySearchForm.sby[i].value;
			searchByOk = 1;
		}
	}
	
	if(searchByOk==0){
		alert('Please check your search type');
		return;
	}
	
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	locationToGo = locationToGo.replace(/\&page=\d+/,''); //no longer relevant will use the lastPage to go back
	//locationToGo = locationToGo.replace(/\&sby=\S*/,'');
	locationToGo = locationToGo+'&sby='+searchByString+'&keystr=' + searchTermValue + '&lastPage='+lastViewedPage;
	
	document.location = locationToGo;
}


function goToTopic(doorId){
	x = document.lstArchive.selArchive.selectedIndex;
	document.location = "/php/gallery/door="+doorId+"&topicId="+document.lstArchive.selArchive.options[x].value;
}

function popWin (url, w, h,r){
	if (!r)
		window.open (url,'_popup',config='height='+h+',width='+w+',toolbar=no,menubar=no,scrollbars=auto, resizable=no,location=no,directories=no,status=no')
	else
		window.open (url,'_popup',config='height='+h+',width='+w+',toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no,directories=no,status=no')
}

function changeDate(newdate)
{
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	locationToGo = locationToGo.replace(/\&timeSelect=\d+/,'');
	locationToGo = locationToGo +'&timeSelect='+newdate;	
	document.location = locationToGo;
}

function gotoFirstPage()
{
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	locationToGo = locationToGo.replace(/\&page=\d+/,'');
	locationToGo = locationToGo +'&page=1';	
	document.location = locationToGo;	
}

function gotoLastPage()
{
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	locationToGo = locationToGo.replace(/\&page=\d+/,'');
	//locationToGo = locationToGo +'&page=1';	
	document.location = locationToGo;	
}

function backfromSearch(gotopage)
{
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	//locationToGo = locationToGo.replace(/\&sby=\S*/,'');
	locationToGo = locationToGo +'&page='+gotopage;	
	document.location = locationToGo;	
}

function clearSearchStr(loc)
{
	var new_loc = '';
	new_loc = loc.replace(/\&sby=\S*/,'');
	new_loc = new_loc.replace(/\&keystr=\S*/,'');
	return new_loc;
	
}