function submitForm(f)
{
	f.submitbutton.value = pleaseWait;
	f.submitbutton.disabled = true;
	f.submit();
}

Array.prototype.contains = function(key)
{
	for (var i = 0; i < this.length; i++)
		if (this[i].id == key)
			return true;
	return false;
}
Array.prototype.remove = function(key)
{
	for (var i = 0; i < this.length; i++)
		if (this[i].id == key)
			this.splice(i, 1);
}

domEl = function(s){return document.createElement(s);}

var categories = new Array();

populateCategories = function(holderId)
{
	var hObj = $(holderId);

	while (hObj.childNodes.length > 0)
		hObj.removeChild(hObj.lastChild);
	
	hObj.appendChild(domEl("A"));

	for (var c = 0; c < categories.length; c++){
		hObj.appendChild(categories[c].obj);
	}
}
addCategory = function(id, holderId, noPopulate)
{
	if (id == 0)
		return;
	if (categories.length >= maxCategories)
	{
		if (!noPopulate)
		{
			alert(maxCategoriesReached);
			return;
		}
	}
	var theSpan = domEl("DIV");
	theSpan.className = "category";
	theSpan.innerHTML = $("t" + id).innerHTML + "&nbsp;";

			var theA = domEl("A");
			theA.href = "javascript:void(0)";
			theA.title = clickToRemove;
			theA.onclick = function()
			{
				categories.remove(id);
				populateCategories(holderId);
				return false;
			}
			theA.innerHTML = '<img src="'+ q.staticUrl +'design/clz.gif" alt="'+categoryRemove+'" />';
		theSpan.appendChild(theA);
		
		var theInput = domEl("INPUT");
		theInput.type = "hidden";
		theInput.name = "categories[]";
		theInput.value = id;
		
		theSpan.appendChild(theInput);
	
	theSpan.appendChild(domEl("BR"));

	if (!categories.contains(id))
	{
		categories.push({"id" : id, "obj" : theSpan});
		if (!noPopulate)
			populateCategories(holderId);
	}
	else
		alert(categoryIsSelected);
}

highlightItem = function(item)
{
	jQuery(item).effect("highlight", {}, 1000);
}

//addPhoto = function(containerId)
addPhoto = function()
{
	hardLimit++;
	if (hardLimit == maxPhotos)
		$("add-photo-link").style.display = "none";
	if (hardLimit > maxPhotos)
		return;
		
	var origEl = jQuery('.addphoto')[0];
	
	jQuery(origEl).clone()
		.appendTo('.manage-photos')
		.show();
}

isFormValid = function(onCreate)
{
	
	var fe = $("form-errors");
	fe.style.display = "";

	var errors = new Array();
	if ($("register-subject").value == "")
	{
		errors.push("subject");
	}
	if (tinyMCE.activeEditor.getContent() == "")
	{
		errors.push("body");
	}
	category = 0;
	
	category = $("register-categories").value;
	

	if(category !=0){
		category = 1;
	}
	
	categoriesSelected = document.getElementsByName("categories[]").length;
	
	chCategory = 0;
	
	if(category !=0 || categoriesSelected !=0){
		chCategory = 1;
	}
	
	if (chCategory ==0){
		errors.push("categories");
	}
	if (onCreate)
	{
		if ($("register-on-index").checked)
		{
			var h = new XmlHttp();

			h.open("GET", "/do/diaries/gotCreditsForIndex", true);

			h.onreadystatechange = function()
			{
				if (h.readyState == 4)
				{
					var response = h.responseText;
					if (response == "0")
					{
						errors.push("on-index");
					}
				}
			};

			h.send(null);
		}
	}

	if (errors.length == 0)
	{
		fe.style.display = "none";
		return true;
	}
	else
	{
		
		while (fe.childNodes.length > 0)
		{
			fe.removeChild(fe.lastChild);
		}
		var theH2 = domEl("div");
		theH2.className = "levtopic levreg topicerror";
		var theOuter = domEl("div");
		theOuter.className = "lev regblank";
		theH2.innerHTML = formErrors;
		var theUl = domEl("UL");
		for (var e = 0; e < errors.length; e++)
		{
			var theLi = domEl("LI");
			var theA = domEl("A");
			theA.href = requestURI + "#register-" + errors[e];
			theA.onclick = function()
			{
				return $run("error-focus", this.href);
			}
			theA.innerHTML = errorMessages[errors[e]];
			theLi.appendChild(theA);
			theUl.appendChild(theLi);
		}
		fe.appendChild(theH2);
		fe.appendChild(theOuter);
		theOuter.appendChild(theUl);
		//location.hash = "form-errors";
		setTimeout('document.location.href = requestURI + "#form-errors"', 100); // del operos...
		return false;
	}
}

voteHandler = function(id, value, el) {
	// show loader
	var loader = jQuery(new Image());
	loader.addClass('ratingLoader');
	loader.load(function() {
		loader.insertAfter('div#ratingContainer').show();

		// send voted value
		jQuery.getJSON("/do/diaries/addVote?id=" + diaryId + "&vote=" + value, function(data, textStatus) {
		
			var msg = jQuery('<div class="ajaxmsg" style="width: 129px">' + msgVoteAdded + '</div>');
			jQuery('div#ratingPlaceholder').hide();
			//el.setRating(0, -1);
			el.setRating(data.rating, -1);

			loader.remove();
			
			jQuery('div#ratingPlaceholder span#votesCount').html(data.votes_count);

			msg.insertAfter('div#ratingContainer')
				.fadeIn(600)
				.animate({opacity: 1.0}, 3000) // pause
				.fadeOut(800, function() {
					jQuery('div#ratingPlaceholder')
						.fadeIn(300, function() {
							msg.remove();
							//el.setRating(data.rating, 2000);
						});
			});
		});
	});
		
	loader.attr('src', loaderUrl);
}

diaryImageChange = function(linkId)
{
	$photo = jQuery('#photo');
	$link = jQuery('#img-link-'+linkId);
	$thumb = jQuery('#img-'+linkId);
	$imageContainer = jQuery('#image-container');
	
	if ($photo.attr('src') == $link.attr('href'))
		return false;
	
	desc = $link.attr('imtitle');

	$imageContainer.css('height', $photo.attr('height'));
	$photo.hide();
	
	$photo.load(function()
	{
		jQuery('#dnedesc').remove();	
		$imageContainer.css('height', '');
		$photo.show();
		
		if (desc.length) {
			$dsc = jQuery('<div id="dnedesc">'+desc+'</div>');
			$dsc.width(jQuery('#photo').outerWidth());
			$dsc.hide().insertAfter('#photo').corners("9px bottom").fadeIn(300);
			
			pos = $photo.position();
			
			$dsc.css('left', pos.left);
			$dsc.css('top', pos.top + $photo.outerHeight());
		}
		
		$photo.unbind('load');
	});
	
	jQuery('#dnedesc').hide();
	$photo.attr('src', $link.attr('href'));

	jQuery('#thumbnails img').removeClass('active');
	$thumb.addClass('active');
	
	return false;
}

manageAdminPick = function()
{
	jQuery(function() {
		jQuery('#pick-on-index').change(function() {
			if (jQuery(this).attr('checked')) 
			{
				jQuery('#cant-put-on-index, #need-pay-for-index').slideUp(100);
				jQuery('#register-on-index').attr('checked', '');
			}
			else
				jQuery('#cant-put-on-index, #need-pay-for-index').slideDown(200);
		});
	});
}
