function TogglePutCustomList (content_id, param)
{
	if (param == 'favourite') element_id = "#btn_favourite_" + content_id;
	else if	(param == 'custom_list') element_id = "#btn_custom_list_" + content_id;

	if (jQuery(element_id).hasClass ("button"))
	{
		if (param == 'favourite') url = "post_ajax.php?action=add_favourite&content_id=";
		else if (param == 'custom_list') url = "post_ajax.php?action=add_custom_list&content_id=";

		GrowlShow (jQuery('#custom_list_title').attr('value'), jQuery('#custom_list_add_msg').attr('value'));
		jQuery("#ajax_dummy").load (url + content_id, function(){});

		jQuery(element_id).removeClass ("button");
		jQuery(element_id).addClass ("button_active");
	}
	else
	{
		if (param == 'favourite') url = "post_ajax.php?action=remove_favourite&content_id=";
		else if (param == 'custom_list') url = "post_ajax.php?action=remove_custom_list&content_id=";

		GrowlShow (jQuery('#custom_list_title').attr('value'), jQuery('#custom_list_remove_msg').attr('value'));
		jQuery("#ajax_dummy").load (url + content_id, function(){});

		jQuery(element_id).removeClass ("button_active");
		jQuery(element_id).addClass ("button");
	}
}

function OpenWindow (url, windowname, width, height)
{
	var w = window.open ('' + url + '','' + windowname + '','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + width + ',height=' + height + '');
	MoveWindowToCenter (w, width, height);
}

function OpenOrderWindow (url, windowname)
{
	var width = 612;
	var height = 540;
	var w = window.open ('' + url + '','' + windowname + '','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + width + ',height=' + height + '');
	MoveWindowToCenter (w, width, height);
}

function OpenTrailerWindow (url, windowname)
{
	var width = 380;
	var height = 309;
	var w = window.open ('' + url + '','' + windowname + '','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + width + ',height=' + height + '');
	MoveWindowToCenter (w, width, height);
}

function MoveWindowToCenter (w, width, height)
{
	var Ycor = (screen.height-height) / 2;
	var Xcor = (screen.width-width) / 2;
	w.moveTo (Xcor, Ycor);
}

function NewMessageWindow (url, windowname)
{
	var width = 612;
	var height = 540;
	var w = window.open ('' + url + '','' + windowname + '','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + width + ',height=' + height);
	MoveWindowToCenter (w, width, height);
}

function OpenAvatarUpload (url, windowname)
{
	var width = 612;
	var height = 207;
	var w = window.open ('' + url + '','' + windowname + '','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + width + ',height=' + height);
	MoveWindowToCenter (w, width, height);
}

function CheckAvatarUpload ()
{
	var oForm = document.forms['uploadform'];
	var avatar_file = trim (oForm.avatar.value);
	var avatar_ext = avatar_file.substring (avatar_file.length, avatar_file.length - 4).toLowerCase();

	if (avatar_file.length == 0)
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_avatar_upload_error').attr('value'));
	}
	else if (avatar_ext != '.jpg')
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_avatar_upload_format_error').attr('value'));		
		oForm.avatar.focus ();
	}
	else
	{
		document.forms['uploadform'].submitbutton.disabled = "true";
		document.forms['uploadform'].submit ();
	}

}

function is_user_online (user_id)
{
	jQuery.get("whoisonline.php?action=is_user_online&user_id=" + user_id, function(response)
	{
		if (response == 'Y')
			document.getElementById("user_" + user_id).src = "images/online.gif";
		else
			document.getElementById("user_" + user_id).src = "images/offline.gif"
	});
}

function BuyFormChange ()
{
	radioObj = document.forms['form_buy'].elements['buy_param'];
	radioLength = radioObj.length;
	var hide = 0;
	for (var i = 0; i < radioLength; i++) 
	{
		if (radioObj[i].value == 'none' && radioObj[i].checked)
		{
			hide = 1;
		}
	}
	if (hide == 1) jQuery("#time_selector").hide ();
	else jQuery("#time_selector").show (); 
}

function SubmitBuyForm ()
{
	// check friends
	var friend_error = 0;
	var friend_id_select = document.getElementById ('friend_id');
	var time_error = 0;
	radioObj = document.forms['form_buy'].elements['buy_param'];
	radioLength = radioObj.length;
	hours = document.forms['form_buy'].elements['hours'];
	minutes = document.forms['form_buy'].elements['minutes'];
	hours_value = hours.options[hours.selectedIndex].value;
	minutes_value = minutes.options[minutes.selectedIndex].value;
	
	for (var i = 0; i < radioLength; i++) 
	{
		if (radioObj[i].checked && radioObj[i].value == 'friend' && friend_id_select.value == '') friend_error = 1;
		// check time
		if (radioObj[i].checked && radioObj[i].value == 'member')
		{
			if (hours_value == '' || minutes_value == '') time_error = 1;
		}
	}
	if (friend_error == 1)
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_payment_friend_error').attr('value'));		
	}
	else if (time_error)
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_payment_time_error').attr('value'));
	}
	else
	{
		document.forms['form_buy'].submit ();
	}
}

function SubmitOrderForm ()
{
	var radioObj = document.forms['form_order'].elements['payment_identifier'];
	var radioLength = radioObj.length;
	var payment_identifier = 0;
	
	// several payments
	if (radioLength != null)
	{
		for (var i = 0; i < radioLength; i++) 
		{
			if (radioObj[i].checked) payment_identifier = 1;
		}
	}
	else
	{
		// one payment
		if (radioObj.checked) payment_identifier = 1;
	}

	if (payment_identifier == 0)
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_payment_error').attr('value'));
	}
	else
	{
		document.forms['form_order'].submit ()
	}
}

function SubmitComment (oForm)
{
	if (trim (oForm.message.value) == '')
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_review_error').attr('value'));
		return false;
	}
	return true;
}

function GrowlShow (title, text)
{
	Growl.Smoke
	({
		title: '<strong>' + title + '</strong>',
		text: text,
		duration: 3
	});
}

function CheckNewMessage (oForm)
{
	if (trim(oForm.message.value) == '')
	{
		GrowlShow (jQuery('#js_error').attr('value'), jQuery('#js_message_error').attr('value'));
		return false;
	}
	return true;
}

function InviteFriend (friend_id)
{
	url = 'post_ajax.php?action=invite_friend&receiver_id=' + friend_id;
	GrowlShow (jQuery('#js_action_done').attr('value'), jQuery('#js_message_friend_invitation').attr('value'));	
	jQuery("#ajax_dummy").load (url, function(){});
}

function CouchRequest (author_id, paid_id)
{
	url = 'post_ajax.php?action=film_invitation_request&author_id=' + author_id + '&paid_id=' + paid_id;
	
	jQuery.get(url, function(response)
	{
		var title = '';
		var text = '';
		if (response == 'OK')
		{
			title = jQuery('#js_action_done').attr('value');
			text = jQuery('#js_watch_together_request').attr('value');
		}
		else if (response == 'LIMIT')
		{
			title = jQuery('#js_error').attr('value');
			text = jQuery('#js_watch_together_limit').attr('value');
		}
		else if (response == 'LIMIT_FOR_USER')
		{
			title = jQuery('#js_error').attr('value');
			text = jQuery('#js_watch_together_limit_pro_user').attr('value');
		}
		else if (response == 'TOO_LATE')
		{
			title = jQuery('#js_error').attr('value');
			text = jQuery('#js_watch_together_toolate').attr('value');
		}
			
		Growl.Smoke
		({
			title: title,
			text: text,
			duration: 5
		});
		
	});
}
