function confirmDelete() {
	var response = confirm("Really delete?");
	if (response) {
		return true;
	} else { 
		return false;
	}
}

function dynamicImages() {
	$('h1').each(function() {
		var headingText = $(this).text();
		$(this).empty().html('<img src="scripts/dynamicheading.php?text=' + headingText +'" alt="' + headingText +'" title="' + headingText +'" />'); 
	});
	$('a.linkBig').each(function() {
		var linkText = $(this).text();
		var linkWidth = $(this).attr('rel');
		preloadImages("scripts/dynamiclink.php?text=' + linkText + '&width=' + linkWidth + '&hover=on");
		$(this).empty().html('<img src="scripts/dynamiclink.php?text=' + linkText + '&width=' + linkWidth + '" alt="' + linkText + '" title="' + linkText + '" />').hover(
		    function() {
			    $(this).children('img').attr({src: 'scripts/dynamiclink.php?text=' + linkText + '&width=' + linkWidth + '&hover=on'});
			},
			function() {
				$(this).children('img').attr({src: 'scripts/dynamiclink.php?text=' + linkText + '&width=' + linkWidth});
			}
	    );
	});
}

function preloadImages() {
	for (var i = 0; i < arguments.length; i++) {
		$("<img>").attr("src", arguments[i]);
	}
}

function countCharacters() {
	$('.charCount').each(function() {
		var maxLength = $(this).attr('title');
		var warnLength = maxLength - 10;
		var safeColor = '#FFFFFF';
		var warningColor = '#FFFF99';
		var dangerColor = '#FF0000';
		// get current count
		var startLength = $(this).val().length;
		$(this).siblings('#counter').children('.counter').val(startLength);
		if (startLength < warnLength) {
			$(this).siblings('#counter').children('.counter').css({'background-color': safeColor});
		} else if (startLength >= warnLength && length < maxLength) {
			$(this).siblings('#counter').children('.counter').css({'background-color': warningColor});
		} else {
			$(this).siblings('#counter').children('.counter').css({'background-color': dangerColor});
		}
		// bind on keyup event
		$(this).keyup(function() {
			var newLength = $(this).val().length;
			$(this).siblings('#counter').children('.counter').val(newLength);
			if (newLength < warnLength) {
				$(this).siblings('#counter').children('.counter').css({'background-color': safeColor});
			} else if (newLength >= warnLength && newLength < maxLength) {
				$(this).siblings('#counter').children('.counter').css({'background-color': warningColor});
			} else {
				$(this).siblings('#counter').children('.counter').css({'background-color': dangerColor});
			}
		});
	});
}

function fetchClients(sortBy) {
	var listClients = "";
	$('div.listClients').html("<div class='loading' style=\"padding: 120px 0 80px 0;\"><img src='images/loading.gif' /><br /><br />loading...</div>");
	$.post('../scripts/fetchclients.php', {'sortBy': sortBy}, function(data){
		$.each(data, function(i, type) {
			listClients = listClients + "<br /><p class='box_heading'>" + type.clientTypeName + "</p><div id='box_dynamic'>";
			$.each(type.clientTypeClients, function(i, client) {
				listClients = listClients + "<div style='float: left;'><p>" + client.clientName + "</p></div><div style='float: right;'><a href='client_edit.php?clientID=" + client.clientID + "'>edit</a> | <a href='client_delete.php?clientID=" + client.clientID + "' onclick='return confirmDelete();'>delete</a></div><div class='break_orange_5'></div>"
			});
			listClients = listClients + "</div>";
		});
		$('div.listClients').html(listClients);
	}, "json");
}

function fetchOffices(officeID) {
	var officeText = "";
	$('div.officeContent').html("<div class=\"loading\" style=\"padding: 68px 0 70px 0;\"><img src=\"images/loading_orange.gif\" /><br /><br />loading...</div>");
	$.post('scripts/fetchoffices.php', {'officeID': officeID}, function(data) {
		if (data.officeMap.length > 0) {
			officeText += "<p style=\"float: left; margin-right: 10px;\">" + data.officeMap + "</p>";
		}
		officeText += "<p>" + data.officeAddress + "</p>";
		officeText += "<p><span>Phone:</span> " + data.officePhone + "</p>";
		if (data.officeFax.length > 0) {
			officeText += "<p><span>Fax:</span> " + data.officeFax + "</p>";
		}
		if (data.officeDirections1Title.length > 0 || data.officeDirections2Title.length > 0) {
			officeText += "<br /><p><a class=\"directionsShow\" href=\"#\">show directions</a><a style=\"display: none;\" class=\"directionsHide\" href=\"#\">hide directions</a></p><div class=\"directions\" style=\"display: none;\">";
			if (data.officeDirections1Title.length > 0) {
				officeText += "<div class=\"break_orange_10\">&nbsp;</div><p><span>" + data.officeDirections1Title + "</span></p><p>" + data.officeDirections1Text + "</p>";
			}
			if (data.officeDirections2Title.length > 0) {
				officeText += "<div class=\"break_orange_10\">&nbsp;</div><p><span>" + data.officeDirections2Title + "</span></p><p>" + data.officeDirections2Text + "</p>";
			}
			officeText += "</div>";
		}
		officeText += "<div class=\"break_orange_5\">&nbsp;</div>";
		$('div.officeContent').html(officeText);
		$('a.directionsShow').bind('click', function() {									 
			$(this).hide();
			$('a.directionsHide').show();
			$('div.directions').show();
			return false;
		});
		$('a.directionsHide').bind('click', function() {
			$(this).hide();
			$('a.directionsShow').show();
			$('div.directions').hide();
			return false;
		});
	}, "json");
}

function validateForm() {
	var hasError = false;
	$('form#frmSubmit').find("input[type='text'], textarea").filter('.validate').each(function() {
		$('label').removeClass('inputError');
		if (this.type == 'text') {
			if (this.value == '') {
				showError(this);
				hasError = true;
				return false;
			} else if (this.id == 'txtEmail') {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if (!emailReg.test(this.value)) {
					showError(this);
					hasError = true;
					return false;
				}
			}
		}		
		if (this.type == 'textarea') {
			if (this.value == '') {
				showError(this);
				hasError = true;
				return false;
			}
		}		
	});
	if (hasError == false) {
		alert("form validated");
		// submit form
	}
}
	
function showError(input) {
	$(input).prev('label').addClass('inputError');
	$(input).fadeOut(100, function() {
		$(this).fadeIn(100, function() {
			$(this).fadeOut(100, function() {
				$(this).fadeIn(100);
			});
		});
	});
}

function clearForm() {
	$('label').removeClass('inputError');
}