$(function() {

	initClearDefaultValue();
	
	rollovers();
	
	$("#font_increase").fontscale("#contentarea p,#contentarea h1,#contentarea h2,#contentarea li,#contentarea h3","up",{useCookie: false});
	$("#font_decrease").fontscale("#contentarea p,#contentarea h1,#contentarea h2,#contentarea li,#contentarea h3","down",{useCookie: false});
	
	if($('#home_box_a').length)
	{
		$('#home_box_a').click(function() { window.location.href = 'services.html#sv1'; });
		$('#home_box_b').click(function() { window.location.href = 'services.html#sv2'; });
		$('#home_box_c').click(function() { window.location.href = 'services.html#sv3'; });
	}
	
	$('li.submenu_hd').mouseenter(function() {
		$(this).find("div.submenu").slideDown(300);
		$(this).find("a:first").addClass("on");
	});
	$('li.submenu_hd').mouseleave(function() {
		$(this).find("div.submenu").slideUp(300);
		if(!$(this).find("a:first").hasClass("remain_on"))
			$(this).find("a:first").removeClass("on");
	});

});

function initClearDefaultValue() {
	var defaultColour = "c5c5c5";
	var activeColour = "000000";

	$('.clearDefaultValue').focus(function() {
		el = $(this);

		if(el.val() == el[0].defaultValue) {
			el.val("");
			el.css("color","#"+activeColour);
		}
	});
	
	$('.clearDefaultValue').blur(function() {
		el = $(this);

		if(el.val() == "") {
			el.val(el[0].defaultValue);
			el.css("color","#"+defaultColour);
		}
	});
}

function validateEmail(emailVal) {
	
	if(emailVal.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;

	return false;
}

function rollovers()
{
	$(".rollover").hover(
		function () {
			imgSrc = $(this).attr("src");
			if(imgSrc == undefined)
			{
				imgSrc = $(this).css("background-image");
				newImgSrc = imgSrc.replace("_0","_1");
				$(this).css("background-image",newImgSrc);
			}
			else
			{
				newImgSrc = imgSrc.replace("_0","_1");
				$(this).attr("src",newImgSrc);
			}
		}, 
		function () {
			imgSrc = $(this).attr("src");
			if(imgSrc == undefined)
			{
				imgSrc = $(this).css("background-image");
				newImgSrc = imgSrc.replace("_1","_0");
				$(this).css("background-image",newImgSrc);
			}
			else
			{
				if(!$(this).hasClass("no_rollover"))
				{
					newImgSrc = imgSrc.replace("_1","_0");
					$(this).attr("src",newImgSrc);
				}
			}
		}
	);
}

//expand('expand01', this);
function expand(div, btn, linkText)
{
	hideText = "Read less...";
	showText = "Learn more...";

	if(linkText != undefined)
	{
		hideText = linkText[0];
		showText = linkText[1];
	}
	
	div = $('#' + div);
	btn = $(btn);
	
	if(div.css("display") == "block")
	{
		div.slideUp();
		div.fadeOut();
		btn.text(showText);
	}
	else
	{
		div.slideDown();
		div.fadeIn();
		btn.text(hideText);
	}
}

function validateForm()
{
	form = $('form#cform');
	
	fullname = form.find("input[name='fullname']");
	email = form.find("input[name='email']");
	contact_number = form.find("input[name='contact_number']");
	message = form.find("textarea[name='message']");
	error = false;
	
	$("span.cform_error").hide();
	
	if(fullname.val() == "")
	{
		fullname.parents("tr:first").find("span.cform_error").show();
		if(!error)
			fullname.focus();
		error = true;
	}	
	if(email.val() == "")
	{
		email.parents("tr:first").find("span.cform_error").text("* Required");
		email.parents("tr:first").find("span.cform_error").show();
		if(!error)
			email.focus();
		error = true;
	}	
	else if(!validateEmail(email.val()))
	{
		email.parents("tr:first").find("span.cform_error").text("* Invalid email");
		email.parents("tr:first").find("span.cform_error").show();
		if(!error)
			email.focus();
		error = true;
	}
	
	if(contact_number.val() == "")
	{
		contact_number.parents("tr:first").find("span.cform_error").show();
		if(!error)
			contact_number.focus();
		error = true;
	}
	
	if(message.val() == "")
	{
		message.parents("tr:first").find("span.cform_error").show();
		if(!error)
			message.focus();
		error = true;
	}

	if(error)
		return false;
	
	return true;
}
