/* Support Page Javascript */

/* prevent conflict with scriptaculous, prototype */
jQuery.noConflict();





/* Initial Page Loader */
jQuery(document).ready(function()
{
	/* === Send Button Event === */
	jQuery("input[name=submit]").bind("click", speaktoxpert);
	
	jQuery(".helpbtn").bind("mouseover mouseout", function() 
	{
		var src = jQuery(this).attr("src");
		var dot	= src.lastIndexOf(".");
		var ext	= src.substring(dot, src.length);
		var src = src.substring(0, dot);
		var ovr	= src.lastIndexOf("_over");

		if (src.lastIndexOf("_over") < 0)
			jQuery(this).attr("src", (src + "_over" + ext));
		else
			jQuery(this).attr("src", (src.substring(0, ovr) + ext));
	});
});





/* Align Loading Screen */
function alignLoader()
{
	var winsize = {	h:jQuery("#loading").height(), w:jQuery("#loading").width() };			// browser size
	var size	= { h:jQuery("#loader").height(), w:jQuery("#loader").width() };			// loader size
	
	jQuery("#loader").css({ "left":((winsize.w - size.w) / 2) +"px", "top":((winsize.h - size.h) / 2) +"px"});
}





/* Hide and clear error and status */
function resetErr()
{
	jQuery("div.state").hide();									// hide status panel
	jQuery("span.status").text("");
	jQuery("td.centercenter").find("input[type=text]").css("background", "");
	repaint();
}





function speaktoxpert()
{
	var firstname	= jQuery("input[name=firstname]");
	var surname		= jQuery("input[name=surname]");
	var company		= jQuery("input[name=company]");
	var jobtitle	= jQuery("input[name=jobtitle]");
	var country		= jQuery("input[name=country]");
	var postal		= jQuery("input[name=postalcode]");
	var email		= jQuery("input[name=email]");
	var contact		= jQuery("input[name=contact]");
	
	function reseterr()
	{
		jQuery("span.status").text("").parent().parent().hide();
	}
	
	// validate the required fields
	function validate()
	{
		var ret = true;
		
		// check for empty field and prompt error
		function isBlank(elemt)
		{
			if (elemt.val().length < 1)
				elemt.css("background", "#ffcccc");
		}
		
		// validate fields
		resetErr();
		isBlank(firstname);
		isBlank(surname);	
		isBlank(company);
		isBlank(jobtitle);
		isBlank(country);
		isBlank(postal);	
		isBlank(email);
		isBlank(contact);
		repaint();
		
		jQuery("td.centercenter").find("input[type=text]").each(function()
		{
			if ((jQuery(this).css("backgroundColor") == "#ffcccc") || (jQuery(this).css("backgroundColor") == "rgb(255, 204, 204)"))
				ret = false;
		});
		
		return ret;
	}
	
	// build query string
	var query = 
	{
		"type"		:	"xpert",
		"firstname"	:	firstname.val(),
		"surname"	:	surname.val(),
		"company"	:	company.val(),
		"jobtitle"	:	jobtitle.val(),
		"country"	:	country.val(),
		"postal"	:	postal.val(),
		"email"		:	email.val(),
		"contact"	:	contact.val()
	};
	
	// pass validation
	if (validate())
	{
		jQuery.ajax(
		{
			type		: "POST",					// post method
			async		: false,					//
			url			: "ajax/marketing.php",		// server url fo retrieval
			data		: query,					// query string
			beforeSend	: function()				// before message send out
			{
				// fade in loading screen
				alignLoader();
			},
			datatype	: "json",					// return type
			success		: function(xhr, stat)		// successful receive data
			{
				var json = (typeof xhr == "object") ? xhr :  eval("(" + xhr + ")");
				
				var first_err 	= json.firstname;	// <firstname>
				var sur_err		= json.surname;		// <surname>
				var coy_err		= json.company;		// <company>
				var job_err		= json.jobtitle;	// <jobtitle>
				var cry_err		= json.country;		// <country>
				var postal_err	= json.postal;		// <postal>
				var email_err	= json.email;		// <email>
				var cont_err	= json.contact;		// <contact>
				var mail_err	= json.mail;		// <mail>
				
				// check xml result and prompt error
				function isValid(elemt, err)
				{
					if (err == "invalid")
						elemt.css("background", "#ffcccc");
				}
				
				// validate xml result
				isValid(firstname, first_err);
				isValid(surname, sur_err);
				isValid(company, coy_err);
				isValid(jobtitle, job_err);
				isValid(country, cry_err);
				isValid(postal, postal_err);
				isValid(email, email_err);
				isValid(contact, cont_err);
				
				// only if mail is send, status will be show
				if (json.mail != null)
				{
					mail_err = json.mail;
					
					// prompt fail or success if email sent
					if (mail_err == "success")
						jQuery("span.status").text("Your details has been sent out successfully. We will response shortly.").parent().css({ borderColor : "#00ff00", color : "#00ff00" }).parent().show();
					else
						jQuery("span.status").text("Your enquiry has been failed to send out. Please try again.").parent().css({ borderColor : "#ff0000", color : "#ff0000" }).parent().show();
						
					jQuery("td.centercenter").find("input[type=text]").val("");
				}
				
				// hide loading screen
				jQuery("#loading").stop(false, true).hide();
				repaint();
			}
		});
	}
}