$(document).ready(function($) {
	
});

var TEMPLATE = TEMPLATE || false;
var Fn = function () {};
var webRoot = '/';
var current_homepage_calculator = '';
var buildURL = function(controller, action) {
	if ( TEMPLATE ) return controller;
	var url = webRoot + '/' + controller;
	if ( !action && !controller )
		throw "No URL Specified";
	if ( action )
		url += '/' + action;
	return url.replace(/[\/]{2,}/, '/');
};

var validateForm = function (form, url)
{
	jQuery(form).submit(function(){
		var $t = jQuery(this);
		var result = true;
		jQuery.ajaxSetup({async: false});
		jQuery.post(url, $t.serialize(), function(data) {
			if (data) {
				result = false;
				$.fn.nyroModalManual({
					content: data
				})
			}
		});
		jQuery.ajaxSetup({async: true});
		if ( result )
			return true;
		return false;
	})
}

function mark_error(id)
{
	$(id).addClass('error_field');
	$(id).removeClass('ok_field');
}
 
function unmark_error(id)
{
	$(id).addClass('ok_field');
	$(id).removeClass('error_field');
}

function checkEmail(value) {
	return value.match(/^[a-z0-9\-_-]+([\.%!][_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/);
}

function checkPhone(value) {
	return value.match(/^[0-9]{1,11}$/);
}

function send_question()
{
	error = false;
	if ($('#question-name').val() == '')
	{
		mark_error($('#question-name'));
		error = true;
	}
	else
	{
		unmark_error($('#question-name'));
	}
	
	if ($('#question-mail').val() == '' || !checkEmail($('#question-mail').val()))
	{
		mark_error($('#question-mail'));
		error = true;
	}
	else
	{
		unmark_error($('#question-mail'));
	}
	
	if ($('#question-text').val() == '')
	{
		mark_error($('#question-text'));
		error = true;
	}
	else
	{
		unmark_error($('#question-text'));
	}
	
	if (!error)
		$('#question-form').submit();
}

function send_order()
{
	error = false;
	if ($('#order-name').val() == '')
	{
		mark_error($('#order-name'));
		error = true;
	}
	else
	{
		unmark_error($('#order-name'));
	}
	
	if ($('#order-mail').val() == '' || !checkEmail($('#order-mail').val()))
	{
		mark_error($('#order-mail'));
		error = true;
	}
	else
	{
		unmark_error($('#order-mail'));
	}
	
	if ($('#order-phone').val() == '' || !checkPhone($('#order-phone').val()))
	{
		mark_error($('#order-phone'));
		error = true;
	}
	else
	{
		unmark_error($('#order-phone'));
	}
	
	if ($('#order-text').val() == '')
	{
		mark_error($('#order-text'));
		error = true;
	}
	else
	{
		unmark_error($('#order-text'));
	}
	
	if (!error)
		$('#order-form').submit();
}
