function change_ip_src () {
	$('email_image_protection').src = '/image_protection_' + (new Date()).getTime() + '.png';
	$('email_protection_code').value = '';
}

function send_recommendation(id, name) {
	showBackground();
	$('recommend_id').value = id;
	$('sending_recommendation').style.display = 'none';

	if (!window._recommendation_box_ip) {
		window._recommendation_box_ip = document.createElement('img');
		window._recommendation_box_ip.id = 'email_image_protection';
		$('recommend_img_protection_container').appendChild(window._recommendation_box_ip);
		window._recommendation_box_ip.title = 'Human/Robot Protection';
		change_ip_src();
	}

	$('recommend_dialog').style.display = 'block';
	if (user_email) {
		$('from_email').value = user_email;
		$('recommend_email_tr').style.display = "none";
	} else {
		$('recommend_email_tr').style.display = "";
	}
	if (user_name && $('from_name').value == '')
		$('from_name').value = user_name;

	var sender = ($('from_name').value == ''? 'A friend':$('from_name').value);
	var subject = '';
	if (/playlist:/.test(id)) {
		link = '?watch_pl=' + /playlist: (.*)/.exec(id)[1];
		subject = sender + ' recommends you watch these videos in their playlist at iDesktop.tv';
	} else {
		link = '?watch=' + id;
		subject = sender + ' recommends you watch this video on iDesktop.tv';
	}
	subject = escape(subject);
	var email_body = escape(name) + '%0Dhttp://www.idesktop.tv/' + link;
	$('recomment_email_client').href = 'mailto:?Subject='+subject+'&Body='+email_body;

	change_ip_src();
}

function send_recommendation_playlist(id, name, onClose) {
	send_recommendation("playlist: " + id, name);
	if (arguments.length == 3)
		_on_recommendation_close = onClose;
}

function submit_recommendation() {
	var to_email = $('sent_to_email').value;
	var from_name = $('from_name').value;
	var from_email = $('from_email').value;
	var email_text = $('email_text').value;
	var code = $('email_protection_code').value;
	var id = $('recommend_id').value;
	if (to_email == '')
	{
		alert ('There are no recipients!');
		return;
	}
	to_email = to_email.replace(/\s+|;|\n/g, ",").replace(/,+/g, ",");
	var to_emails = to_email.split(",");

	for (var i = 0; i < to_emails.length; i ++)
		if (!email_filter.test(to_emails[i])) {
			alert ('There is invalid recipient!');
			return;
		}
	if (from_name == '') {
		alert ('Enter your name!');
		return;
	}
	if (from_email == '' || !email_filter.test(from_email)) {
		alert ('Invalid sender email!');
		return;
	}
	$('sending_recommendation').style.display = 'block';

	if (/playlist:/.test(id))
		urchinTracker('recommend/playlist');
	else
		urchinTracker('recommend/video');

	var params = $H({from_email:from_email,
		from: from_name,
		to: to_email,
		id: id,
		additional_text: email_text,
		pcode: code,
		send_copy: $('sent_copy_to_me').checked });
	new Ajax.Request ('recommend_by_email.php', {
		method:'post',
		parameters: params.toQueryString(),
		onSuccess: recommend_handler });
}

function recommend_handler (ajaxResponse) {
	var rt = ajaxResponse.responseText;
	var t = null;
	eval ("t = " + rt);
	if (t != null && t.result == 'OK') {
		alert ('Email sent!');
		change_ip_src();
		close_recommendation();
	} else {
		change_ip_src();
		$('sending_recommendation').style.display = 'none';
		alert (t.error);
	}
}

function close_recommendation() {
	hideBackground();
	$('sending_recommendation').style.display = 'none';
	$('recommend_dialog').style.display = 'none';
	if (typeof _on_recommendation_close == "function") {
		_on_recommendation_close();
		_on_recommendation_close = null;
	}
}

function clear_recommendation () {
	$('email_text').value = '';
	$('sent_to_email').value = '';
}