function submitVote(id) {
	if(!makeRequest)
	{
		alert('Cannot submit vote. Please, try later');
		return;
	}

	setVote(id);
	makeRequest('/Tools/MusicContest/ajax.php?a=v&s=' + escape(id), voteHandler, true, false);

	for(var i = 0; i < songs.length; i++)
		if(songs[i][0] == id)
		{
			songs[i][1]++;

			var button = document.getElementById('button_' + songs[i][0]);

			if(button)
				button.innerHTML = '<button class="normalvote"/>';
			break;
		}
}

function voteHandler(xml) {
	if(isCompleted(xml))
		setVotes();
}

function submitPlay(id) {
	if(makeRequest)
		makeRequest('/Tools/MusicContest/ajax.php?a=p&s=' + escape(id), playHandler, true, false);
}

function playHandler(xml) {
}

function setVotes() {
	for(var i = 0; i < songs.length; i++)
	{
		var button = document.getElementById('button_' + songs[i][0]);
		var votes = document.getElementById('votes_' + songs[i][0]);

		if(hasVoted(songs[i][0]))
		{
			if(button && votes)
			{
				button.innerHTML = '<button class="normalvote"/>';
				votes.innerHTML = songs[i][1];
			}
		}
	}
}

function hasVoted(id) {
	var votes = get_cookie(COOKIE_VOTES).split(',');
	var voted = false;

	for(var i = 0; i < votes.length; i++)
		if(votes[i] == id)
		{
			voted = true;
			break;
		}
	
	return voted;
}

function setVote(id) {
	var cookie = get_cookie(COOKIE_VOTES);

	cookie += (cookie ? ',' : '') + id;

	set_cookie(COOKIE_VOTES, cookie);
}

function checkUploadForm(form) {
	var errors = new Array();
	var focus = null;
	var ret = true;

	if(!form.Title.value)
	{
		errors.push('You must enter the field Title');
		if(!focus) focus = form.Title;
	}

	if(!form.URL.value)
	{
		var msg = bIsUploadingMP3 ? 'Wait till the MP3 is completely uploaded' : 'You must upload your Song';

		errors.push(msg);
		//if(!focus) focus = form.URL;
	}

	if(!form.Terms.checked)
	{
		errors.push('You must accept the terms and conditions');
		if(!focus) focus = form.Terms;
	}

	if(errors.length)
	{
		alert('There was an error with your response:\n - ' + errors.join('\n - '));
		ret = false;

		if(focus) focus.focus();
	}
	else
	{
		form.submit.value = 'Uploading...';
		form.submit.disabled = true;
	}

	return ret;
}

function checkCommentsForm(form) {
	var errors = new Array();
	var focus = null;
	var ret = true;

	if(!form.Author.value)
	{
		errors.push('You must enter the field Author');
		if(!focus) focus = form.Author;
	}

	if(!form.Comment.value)
	{
		errors.push('You must enter the field Comment');
		if(!focus) focus = form.Comment;
	}

	/*if(!form.CaptchaCode.value)
	{
		errors.push('You must enter the code in the image');
		if(!focus) focus = form.CaptchaCode;
	}*/

	if(errors.length)
	{
		alert('There was an error with your response:\n - ' + errors.join('\n - '));
		ret = false;

		if(focus) focus.focus();
	}
	else
	{
		form.submit.value = 'Submitting...';
		form.submit.disabled = true;

		alert('Thanks for submitting your comment. Soon will be published...');
	}

	return ret;
}

function checkProfileForm(form) {
	var errors = new Array();
	var focus = null;
	var ret = true;

	if(!form.Bio.value)
	{
		errors.push('You must enter the field Bio');
		if(!focus) focus = form.Bio;
	}

	if(errors.length)
	{
		alert('There was an error with your response:\n - ' + errors.join('\n - '));
		ret = false;

		if(focus) focus.focus();
	}
	else
	{
		form.submit.value = 'Updating...';
		form.submit.disabled = true;
	}

	return ret;
}

function controlTextArea(obj, length, counter)
{
	if(obj.value.length > length)
		obj.value = obj.value.substr(0, length);

	if(document.getElementById(counter))
		document.getElementById(counter).innerHTML = length - obj.value.length;
}

function addPlaylist(id) {
	var pos = posPlaylist(id);

	if(pos == -1)
	{
		var playlist = getPlaylist();
		playlist.push(id);
		setPlaylist(playlist);
	}

	linkPlaylist(id, false);
}

function removePlaylist(id) {
	var pos = posPlaylist(id);
	var playlist = getPlaylist();

	if(pos != -1)
	{
		playlist.splice(pos, 1);
		setPlaylist(playlist);
	}

	var list = document.getElementById('playlist_item_' + id);

	if(list)
	{
		list.style.display = 'none';

		if(emptyPlaylist(playlist))
		{
			var empty = document.getElementById('playlist_empty');
			empty.innerHTML = '<i class="normaldefault">Empty playlist</i>';
		}
	}
	else
		linkPlaylist(id, false);
}

function linkPlaylist(id, write) {
	var link;
	var isInPlaylist = posPlaylist(id) != -1;

	if(!isInPlaylist)
		link = '<a href="javascript:addPlaylist(' + id + ')" class="linkdefault">Add to playlist</a>';
	else
		link = '<a href="javascript:removePlaylist(' + id + ')" class="linkdefault">Remove from playlist</a>';

	if(write)
		document.write(link);
	else
		document.getElementById('playlist_link_' + id).innerHTML = link;
}

function emptyPlaylist(playlist) {
	var ret = true;

	for(var i = 0; i < playlist.length; i++)
		if(playlist[i])
		{
			ret = false;
			break;
		}

	return ret;
}

function posPlaylist(id) {
	var playlist = getPlaylist(id);
	var ret = -1;

	for(var i = 0; i < playlist.length; i++)
		if(playlist[i] == id)
		{
			ret = i;
			break;
		}

	return ret;
}

function getPlaylist() {
	return get_cookie(COOKIE_PLAYLIST).split(',');
}

function setPlaylist(playlist) {
	set_cookie(COOKIE_PLAYLIST,  playlist.join(','));
}

function set_cookie(cookie, value) {
	var expire = new Date();
	expire.setTime(expire.getTime() + (1000 * 60 * 60 * 24 * 365));

	document.cookie = escape(cookie) + '=' + escape(value) + ';expires=' + expire.toGMTString();
}

function get_cookie(cookie) {
	var cookies = document.cookie.split(';');
	var value = '';

	for(i = 0; i < cookies.length; i++)
	{
		var temp_cookie = cookies[i].split('=');

		if(temp_cookie.length == 2)
			if(temp_cookie[0].replace(/^\s+|\s+$/g, '') == cookie)
			{
				value = unescape(temp_cookie[1].replace(/^\s+|\s+$/g, ''));
				break;
			}
	}

	return value;
}

function previewImage(field, preview, width, height, folder) {
	var field = document.getElementById(field);
	var preview = document.getElementById(preview);

	if(field && preview)
	{
		var html = '<i>None</i>';

		if(field.value)
		{
			var file = folder + basename(field.value);

			var image = new Image();

			image.preview = preview;
			image.onload = function() { 
					var constraint = (width / height) > (this.width / this.height) ? 
										'width="' + width + '"' :
										'height="' + height + '"';
					var html =
						'<div class="imagedefault" style="background:Black;width:' + width + 'px;height:' + height + 'px;overflow:hidden">' +
						'<img ' + constraint + '" src="' + this.src + '"/>' +
						'</div>';

					this.preview.innerHTML = html;
				}

			image.src = escape(file);

			html = '<i>Loading...</i>';
		}
		
		preview.innerHTML = html;
	}
}

function basename(file) {
	var tmp = file.split('/');
	var ret = '';

	if(tmp.length > 1)
		ret = tmp[tmp.length - 1];

	return ret;
}

function htmlspecialchars(value) {
	value = value.replace(/&/g, '&amp;');
	value = value.replace(/"/g, '&quot;');
	value = value.replace(/'/g, '&#039;');
	value = value.replace(/</g, '&lt;');
	value = value.replace(/>/g, '&gt;');

	return value;
}

function stopTrack(id) {
	var movie = get_movie('player' + id);

	if(movie)
		movie.stopTrack();
}

function get_movie(name) {
    if(navigator.appName.indexOf("Microsoft") != -1)
        ret = window[name];
    else
        ret = document[name];
	
	return ret;
}
