// JavaScript Document

function contactFormValidate(data)
{
	var valid = true;
	jQuery("#nav-contact .invalid").removeClass("invalid");
	
	if (data.name == "") {
		valid = false;
		jQuery("#contact-name").addClass("invalid");
	}
	
	if (data.emailreal == "") {
		valid = false;
		jQuery("#contact-email-real").addClass("invalid");
	}
	
	if (data.message == "") {
		valid = false;
		jQuery("#contact-message").addClass("invalid");
	}

	if (valid == false)
		contactShowError(requried_missing);

	return valid;
	
}

function contactFormSend()
{
	jQuery("#nav-contact .msg_error").remove();
	
	
	// assmble the data
	data = {
			reason: jQuery("#contact-reason").val(),
			name: jQuery("#contact-name").val(),
			company: jQuery("#contact-company").val(),
			emailreal: jQuery("#contact-email-real").val(),
			email: jQuery("#contact-email").val(),
			phone: jQuery("#contact-phone").val(),
			onpage: jQuery("#contact-onpage").val(),
			message: jQuery("#contact-message").val()
		};
	
	// validate
	if (contactFormValidate(data)) {
		
		jQuery("#nav-contact .nav_contact_submit").hide();
		jQuery("#nav-contact .nav_contact_loading").show();
	
		jQuery.post("/ajax/email/contact/", data, function(res){
		
			
		
			if (res.status == "error") {
				contactShowError(res.message);
			}
			else {
				contactShowSuccess();
			}
			
		}, "json");
	}

	
	return false;
	
}

function contactShowError(msg)
{
	jQuery("#nav-contact .nav_contact_submit").show();
	jQuery("#nav-contact .nav_contact_loading").hide();
	
	jQuery("#nav-contact .nav_contact_form").prepend('<div class="msg_error">' + msg + '</div>');
}

function contactShowSuccess()
{
	jQuery.get("/_includes/nav_contact_success/", function(res){
			// hide existing form
			jQuery("#nav-contact .nav_contact_col2").fadeOut();
			jQuery("#nav-contact .nav_contact_col1").fadeOut(function(){
				// show success message
				jQuery("#nav-contact .nav_contact_submit").show();
				jQuery("#nav-contact .nav_contact_loading").hide();
			
				jQuery("#nav-contact .nav_contact_form").prepend(res);
			});
	});	
}

function contactShowForm()
{
	jQuery("#nav-contact-success").hide();
	jQuery("#nav-contact FORM").show();
}

function validateForm(elId)
{
    var isValid = true;
	
	jQuery(elId + " .req").each(function(i){
		if (jQuery(this).val() == "") {
			isValid = false;
			jQuery(this).addClass("invalid");
		}
	});
	
	if (isValid == false)
		jQuery(elId).prepend('<div class="msg_error msg">' + requried_missing + '</div>');
	
	return isValid;
}

function expand(section)
{
	jQuery('#exp_' + section).slideToggle('slow', function()
		{
			if (jQuery('#exp_' + section + ':visible').length)
			{
				jQuery('#exp_' + section + '_link').html('Collapse Section').toggleClass('more').toggleClass('less');
			}
			else
			{
				jQuery('#exp_' + section + '_link').html('Expand Section').toggleClass('more').toggleClass('less');
			}
		});
}

function taxToggle(taxState)
{
	if (taxState == "inc")
	{
		jQuery(".tax-toggle-exc").removeClass("selected");
		jQuery(".tax-toggle-inc").addClass("selected");
		jQuery(".price-box").addClass("inc");
		jQuery(".price-box").removeClass("exc");
	}
	else
	{
		jQuery(".tax-toggle-exc").addClass("selected");
		jQuery(".tax-toggle-inc").removeClass("selected");
		jQuery(".price-box").addClass("exc");
		jQuery(".price-box").removeClass("inc");
	}
	setCookie("taxtoggle", taxState, 30);
}

function taxToggleSelect()
{
	taxToggle(jQuery("#tax-toggle-select").val());
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}