function displayById(id, show) {
	var target = document.getElementById(id);
	target.style.display = show;
}

function toggle(id) {
	var target = document.getElementById(id);
	target.style.display = (target.style.display != 'none' ? 'none' : '' );
}

function changeImage(id, url) {
	var target = document.getElementById(id);
	target.src = url;
}

function toggleMore(id, path) {
	var target = document.getElementById(id);
	if(target.src.match(/\more.png$/i)) {
		target.src=path +"/less.png";
	} else {
		target.src=path + "/more.png";
	}
}

function setSource(id, source) {
	var target = document.getElementById(id);
	target.src = source;
}

function setInnerHTML(id, text) {
	document.getElementById(id).innerHTML = text;
}

function setTip(id, tip) {
	var target = document.getElementById(id);
	
	if(target == null) {
		return;
	}
	
	target.onfocus = function() { document.getElementById('tip_' + id).style.display = "block"; };
	target.onblur = function() { document.getElementById('tip_' + id).style.display = "none"; };
	
	var tipDiv = document.createElement('div');
	tipDiv.className = 'tip';
	tipDiv.id = 'tip_' + id;
	
	tipDiv.innerHTML = tip;
	tipDiv.style.display = "none";
	tipDiv.style.position = "absolute";
	
	tipDiv.style.top = target.offsetTop +"px";
	tipDiv.style.left = target.offsetLeft + target.offsetWidth + 10 + "px";

	target.parentNode.appendChild(tipDiv);
}

function search(guestbookId, searchTerm) {
	if(searchTerm.length >= 2) {
		document.getElementById("comments").innerHTML = "<div class=\"loading\">Searching. Please wait...</div>";
		processajax('comments', 'app/searchComments.php?guestbookId=' + guestbookId + '&term=' + searchTerm);
	}
	return false;
}

function addText(id, text) {
	document.getElementById(id).value += text;
}

function draw() {
	cp = this;
	var colorPreview = document.createElement("div");
	colorPreview.className = "colorPreview";
	colorPreview.style.backgroundColor=this.currentColor;
	
	var colorPicker = document.createElement("div");
	colorPicker.className = "colorPicker";
	
	var pickerTable = document.createElement("table");
	pickerTable.onmouseover =
	function(e) {
		// If IE
		if (!e) {
			e = event;
			e.target = e.srcElement;
		}
		// If target is a TD update
		if (e.target.nodeName.toLowerCase() == "a") {
			colorPreview.style.backgroundColor = e.target.style.backgroundColor;
		}
	};
	
	pickerTable.onmouseout = 
	function() {
		colorPreview.style.backgroundColor = cp.currentColor;
	}
	
	inputId = this.inputId;
	pickerTable.onclick = 
		function(e) {
		// If IE
		if (!e) {
			e = event;
			e.target = e.srcElement;
		}
		// If target is a TD update
		if (e.target.nodeName.toLowerCase() == "a") {
			document.getElementById(inputId).value = e.target.title;
			cp.currentColor = e.target.title;
		}
		return false;
	};
	
	for(var i=0; i<this.values.length; i=i+this.step) {
		var colorRow = document.createElement("tr");

		for(var j=0; j<this.values.length; j=j+this.step) {
			for(var k=0; k<this.values.length; k=k+this.step) {
				var color = "#" + this.values[i] + this.values[j] + this.values[k];
				var colorTd = document.createElement("td");
				
				var colorCell = document.createElement("a");
				colorCell.className = "colorPix";
				colorCell.style.backgroundColor = color;
				colorCell.onMouseOver = function() { colorPreview.style.backgroundColor = color };
				colorCell.href = "#";
				colorCell.title = "#" + this.values[i] + this.values[j] + this.values[k];
				
				colorTd.appendChild(colorCell);
				colorRow.appendChild(colorTd);
			}
		}
		pickerTable.appendChild(colorRow);
	}
	
	colorPreview.className = "colorPreview";
	colorPreview.id = this.pickerId + "_preview";
	
	colorPicker.appendChild(pickerTable);
	colorPicker.appendChild(colorPreview);
	
	document.getElementById(this.inputId).parentNode.appendChild(colorPicker);
}

function colorPicker(pickerId, inputId, startColor, step)
{
	this.pickerId = pickerId;
    this.inputId = inputId;
	this.currentColor = startColor;
	this.step = step;
	this.values = new Array("00","11","22","33","44","55","66","77","88","99","AA","BB","CC","DD","EE","FF");
	this.draw = draw;
}

