var ddlChange = false;

function previewMessage(messID, message, topic, doorId, clientId, width, height) {
	var domain = document.galleryForm.urlStr.value;
	var doorStr = document.galleryForm.doorName.value;

	var url = domain + "php/start_player_" + doorStr + "/door=" + doorId + "&cl=" + clientId + "&AID=" + topic + "&id=" + messID + "&message=" + message + (noRanking ? "&exp=1" : "");

	var previewWindow = window.open(url, "previewWindow", "width=550,height=440,resizable");
	if (previewWindow) previewWindow.focus();
}

function postMessage(topicId, width, height){
	if(!width) width = 719;
	if(!height) height = 509;
	
	//var domain = document.galleryForm.dynamicURL.value;
	//var doorStr = document.galleryForm.doorName.value;
	
	var url = (location.href.indexOf('staging') == -1 ? 'http://www.comedycentral.com/events/comedianconstructor/' : 'http://host.staging.oddcast.com/comedycentral')
	parent.location.href = url
	
	/*
	if (window.opener) {
		window.opener.location.href = url;
		self.close();
	} else {
		var createWindow = window.open(url, "createWindow", "width="+(width+200)+",height="+(height+200));
		if (createWindow) createWindow.focus();
	}
	*/
}


function submitForm() {
	var f = document.galleryForm;

	var s = f.urlStr.value + "php/gallery/door=" + f.door.value + "&topicId=" + f.topic.value;
	if (document.galleryForm.keystradv.value.length > 0) {
		if (document.galleryForm.keystradv.value.length <= 2) {
			alert("Search terms must be at least 3 characters long");
			return;
		}
		s += "&sby=tt&keystradv="	+ f.keystradv.value;
	}
	
	s += (f.page.value != '' ? "&page=" + f.page.value : '');
	
	if(f.pageBox.value!= '' && f.page.value == '' && f.pageBox.value.length<8) { 
		if (ddlChange!=true)	s +="&page=" + f.pageBox.value;
	} else if(f.pageBox.value!= '' && f.page.value == '' && f.pageBox.value.length>8) {
		alert('The number can not be larger then 8 digits.');
	} 
	else if(f.page.value != '') {
		if (ddlChange!=true)	s +="&page=" + f.pageBox.value;
	} 
	s += (f.sortBy.options[f.sortBy.selectedIndex].value != 'date' ? "&sortBy=" + f.sortBy.options[f.sortBy.selectedIndex].value : '');
	location.href = s;
}

function gotoFirstPage()
{
	document.galleryForm.pageBox.value = "1";
	submitForm();
}

function gotoLastPage()
{								   
	document.galleryForm.pageBox.value = document.galleryForm.lastPage.value;
	submitForm();
}


function submitSearch() {

	if (document.galleryForm.keystradv.value.length == 0) {
		alert("No search term submitted.");
		document.galleryForm.keystradv.focus();
		return;
	}

	if (document.galleryForm.keystradv.value.length <= 2) {
		alert("Search terms must be at least 3 characters long");
		return;
	}

	submitForm();
}


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 backfromSearch(gotopage)
{
	locationToGo = document.location.toString();
	locationToGo = clearSearchStr(locationToGo);
	//locationToGo = locationToGo.replace(/\&sby=\S*/,'');
	locationToGo = locationToGo +'&page='+gotopage;	
	document.location = locationToGo;	
}


