var postTo = "blog";
var linkTo = "large";
var linkStructure = "";

function validateTags() {
	textareas = $('form').getElementsByTagName('textarea');
	
	item = '';
	for (var index = 0; index < textareas.length; ++index) {
      item = item + textareas[index].value;
  	}

	if (item == '') {
		alert('Please add some tags. At least the name of the celebrity you have uploaded should be added!');
		$('taginput').focus();  
		return false;
	}
	return true;
}

function postToChange() {
	postTo = $("post_to").value;
	updateLink();
}

function linkToChange() {
	linkTo = $("link_to").value;
	updatePost();
}

function linkStructureChange() {
	linkStructure = $("link_structure").value;
	updatePost();
}

function updateLink() {
	switch(postTo) {
		case "blog":
			$("link_structure").value = "<a href=\"*link*\"><img src=\"*image*\" width=\"*width*\" height=\"*height*\" alt=\"*title*\" /></a>";
			break;
		
		case "forum":
			$("link_structure").value = "[URL=*link*][IMG]*image*[/IMG][/URL]";
	}
	
	linkStructureChange();
}

function getLinks(postedTo) {
	var postLinks = '';
	
	for (i = 0; i < pictures.length; i++) {

		link = linkStructure;
		link = link.replace(/\*image\*/i, pictures[i][0].replaceAll(' ','%20'));
		link = link.replace(/\*link\*/i, (linkTo == "large" ? pictures[i][1] + "/large" : pictures[i][1]));
		link = link.replace(/\*width\*/i, pictures[i][2]);
		link = link.replace(/\*height\*/i, pictures[i][3]);
		link = link.replace(/\*title\*/i, pictures[i][4]);
		
		if (postedTo == "blog") {
 			postLinks += link + "\n";
		} else {
 			postLinks += link + " ";
		}
	}
	
	return postLinks;	
}

function updatePost() {
	$("post_text").value = "";
	
	switch(postTo) {
		case "blog":
			$("post_text").value += "<div>\n";
			$("post_text").value += getLinks(postTo);
			$("post_text").value += "</div>";
			break;
		
		case "forum":
			$("post_text").value += getLinks(postTo);
			break;
	}
}

function shareInit() {
	$("post_to").onchange = postToChange;
	$("link_to").onchange = linkToChange;
	$("link_structure").onchange = $("link_structure").onkeyup = linkStructureChange;
	updateLink();
}
