// 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("Внимание! Необходимо заполнить все поля формы.");

	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">Some required fields are missing.  See Below.</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 addValueArrayRow(){
	jQuery(".value_array TBODY").append("<tr>" + jQuery(".value_array TR:last").html() + "</tr>");
}

