/* 
	JavaScript for the site
	@author: Beau Durrant (http://zoomwebvideo.com)
*/

function addView( webcast_id )
{
	var url = '/lobby/addppvview/' + webcast_id;
	new Ajax.Request( url );
}

function addEcommerceWebcast( ecommerceid )
{
	var webcastName = $('webcastName').value;
	var start = 0; var end = webcastName.indexOf('.');
	var webcastid = webcastName.substr(start, end);
	var url = '/ecommerce/addwebcast?webcastid=' + webcastid + '&ecommerceid=' + ecommerceid;
	new Ajax.Request( url, 
	{ 
		onComplete: function()
		{ 
			document.location.reload(); 
		},
		on404: function()
		{
			alert("Error adding webcast, please try again")
		}
	});
}

function checkLogin()
{
	var url = '/lobby/checklogin';
	new Ajax.Request( url , { onComplete: function(t)
	{ 
		if( t.responseText == "refresh" )
		{
			document.location = '/user/login';
		}
	}} );
}

function toggleSearch()
{
	var searchBox = document.getElementById('searchbox');
	if(searchBox.style.display == "none") searchBox.style.display = "block"; else searchBox.style.display = "none";
}

function addToSelect( textElement , selectElement )
{
	if( textElement.value == "" ) return;
	var option = new Option( textElement.value , textElement.value );
	selectElement.options[selectElement.options.length] = option;
	textElement.value = '';
	populateHiddenSelectValues( selectElement );
}

function removeFromSelect( selectElement )
{
	for (var i = 0; i < selectElement.options.length; i++) if (selectElement.options[i].selected) selectElement.remove(i);
	populateHiddenSelectValues( selectElement );
}

function populateHiddenSelectValues( selectElement )
{
	var hiddenName = selectElement.name + '_values';
	var selectHiddenValues = document.getElementById( hiddenName ); var value = '';
	for (var i = 0; i < selectElement.options.length; i++) value += selectElement.options[i].value + ',';
	value = value.substring( 0 , value.length - 1 );
	selectHiddenValues.value = value;
}

function emailToFriend( friendsemail, friendsname, yourname, webcast_id )
{
	var url = '/lobby/emailtofriend?friendsemail=' + friendsemail + '&friendsname=' + friendsname + '&yourname=' + yourname + '&webcast_id=' + webcast_id;
	new Ajax.Request( url , { onComplete: function(){ alert('Email Sent'); } } );
}

function submitQuestion( question, webcast_id )
{
	var url = '/lobby/submitquestion?question=' + escape(question) + '&webcast_id=' + webcast_id;
	new Ajax.Request( url , { onComplete: function(){ alert('Question Submitted'); } } );
}

function openSlideWindow( slide_number, webcast_id )
{
	window.open('/lobby/showslide?slide=' + slide_number + '&webcast_id=' + webcast_id, 'slideWindow', 'location=0,status=0,scrollbars=0,width=800,height=630');	
}

function openSlideControlWindow( webcast_id )
{
	window.open('/webcasts/controlslides/' + webcast_id , 'controlSlideWindow', 'location=0,status=0,scrollbars=0,width=615,height=590');	
}

function openBannerUploadWindow( webcast_id )
{
	window.open('/webcasts/bannerimage/' + webcast_id , 'bannerWindow', 'location=0,status=0,scrollbars=0,width=920,height=390');	
}

function openRequirements()
{
	window.open('/lobby/requirements', 'requirementsWindow', 'location=0,status=0,scrollbars=0,width=400,height=450');
}

function toggleQuestionsEmail()
{
	var qAndATrue = document.getElementById('qAndATrue').checked;
	var questionsEmail = document.getElementById('qAndAEmail');
	var disabled;
	if( !qAndATrue ) disabled = "disabled";
	questionsEmail.disabled = disabled;
}

function togglePassword()
{
	var publicRadio = document.getElementById('publicRadio').checked;
	var passwordElement = document.getElementById('password');
	var disabled;
	if( publicRadio ) disabled = "disabled";
	passwordElement.disabled = disabled;
}

function sendInvites(webcast_id)
{
	var send = confirm('Are you sure you want to send out invites for this webcast?');
	if( !send ) return;
	new Ajax.Request( '/webcasts/sendinvites/' + webcast_id, { onComplete: function(){ alert('Invites Sent'); } } );
}

function displayError(error)
{
	document.getElementById("error").innerHTML = '<ul class="errors"><li>' + error + '</li></ul>';
}

function addSlides(webcast_id)
{
	document.location.href = "/webcasts/powerpoint/" + webcast_id;
}

function saveSlides(string)
{
	openProgressWindow();
	var postVarArray = string.split('?');
	new Ajax.Request( postVarArray[0] , { method: 'post', parameters: postVarArray[1], onComplete: function(){ closeModalWindow(); } } );
}

function saveDesign(string)
{
	openProgressWindow();
	new Ajax.Request( string , { method: 'post', onComplete: function(){ closeModalWindow(); } } );	
}

function openAudioImageWindow(webcast_id)
{
	window.open('/webcasts/audioimage/' + webcast_id, 'audioImageWindow', 'location=0,status=0,scrollbars=0,width=460,height=630')
}

function trace( string )
{
	alert( string );
}