// strings
var successText = "Thank you for requesting a demo username and password. A Sales Representative will contact you within one business day with instructions on how to access the Navigator Institute.";
var failedText = "There was a problem while the form information sending";
var firstNameError = "Please include your First Name.";
var lastNameError = "Please include your Last Name.";
var companyError = "Please include your Company.";
var emailError = "Please include your E-mail Address.";
var phoneNumberError = "Please include your Phone Number.";

function checkFields()
{
	// cleanup errorbox
	var _errors = 0;
	$('#status-info').html('');

	// check firstname field
	if($("#field-firstname").length) {
		if($("#field-firstname").attr("value").length == 0) {
			_errors++;
			$('#status-info').append('<strong class="error-text">'+firstNameError+'</strong><br>');
		}
	}
	// check lastname field
	if($("#field-lastname").length) {
		if($("#field-lastname").attr("value").length == 0) {
			_errors++;
			$('#status-info').append('<strong class="error-text">'+lastNameError+'</strong><br>');
		}
	}
	// check company field
	if($("#field-company").length) {
		if($("#field-company").attr("value").length == 0) {
			_errors++;
			$('#status-info').append('<strong class="error-text">'+companyError+'</strong><br>');
		}
	}
	// check email field
	if($("#field-email").length)
	{
		if (!$("#field-email").attr("value").match(new RegExp('^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$')))
		{
			_errors++;
			$('#status-info').append('<strong class="error-text">'+emailError+'</strong><br>');
		}
	}
	// check email field
	if($("#field-phonenumber").length)
	{
		// calc minuses
		var _phone = "";
		var _mincount = 0;
		_phone = $("#field-phonenumber").attr("value");
		if(_phone.length) {
			for (var k=0; k<_phone.length; k++)
				if(_phone.substring(k,k+1)=="-") _mincount++;
		}
		// check for error
		if(!_phone.length || _mincount!=2) {
			_errors++;
			$('#status-info').append('<strong class="error-text">'+phoneNumberError+'</strong><br>');
		}
	}

	// output errors results
	if(_errors > 0)
	{
		return true;
	}
	else
	{
		$('#status-info').html('');
		$('#status-info').append('<strong class="success-text">'+'sending...'+'</strong><br>');
		return false;
	}
}

// clear input fields
function resetForm() {
	$("#input-email").val("");
	$("#input-username").val("");
	$("#textarea-message").val("");

	$("#warning-label").css("display", "none");
	$("#popup-form").css("display", "block");
	$("#message-posted .thank-you").css("top", "-9999px");
}

// check form values
function initForm() {
	var _submit = $("#contactform-submit");
	if(_submit) {
		_submit.click(function () {
			if(!checkFields()) {
				var _postdata = $("#contact-form form").serialize();
				$.ajax({
					type: "POST",
					url: "mail.php",
					data: _postdata,
					success: function(msg){
						$('#status-info').html('');
						if(msg=="sent") {
							$('#status-info').append('<strong class="success-text">'+successText+'</strong><br>');
						}
						else {
							$('#status-info').append('<strong class="error-text">'+failedText+'</strong><br>');
						}
					}
				});

				return false;
			}
			return false;
		});
	}
	initFormInner();
}










var innerSuccess = 'Thank you for contacting us. A Sales Representa-<br />tive will respond within one business day to add-<br />ress your questions and comments.';
var innerFailedText = "There was a problem while the form information sending";
var innerFirstNameError = "Please include your First Name.";
var innerLastNameError = "Please include your Last Name.";
var innerEmailError = "Please include your E-mail Address.";
var innerMessageError = "Please include your message.";

function checkFieldsInner() {
	// cleanup errorbox
	var _errors = 0;
	var _statusholder = $('#form-status');
	_statusholder.html('');

	// check firstname field
	if($("#first-name").length) {
		if($("#first-name").attr("value").length == 0) {
			_errors++;
			_statusholder.append('<strong class="error-text">'+innerFirstNameError+'</strong><br>');
		}
	}
	// lastname field
	if($("#last-name").length) {
		if($("#last-name").attr("value").length == 0) {
			_errors++;
			_statusholder.append('<strong class="error-text">'+innerLastNameError+'</strong><br>');
		}
	}
	// check email field
	if($("#mail").length) {
		if (!$("#mail").attr("value").match(new RegExp('^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$')))
		{
			_errors++;
			_statusholder.append('<strong class="error-text">'+innerEmailError+'</strong><br>');
		}
	}
	// check firstname field
	if($("#textarea").length) {
		if($("#textarea").attr("value").length == 0) {
			_errors++;
			_statusholder.append('<strong class="error-text">'+innerMessageError+'</strong><br>');
		}
	}

	// output errors results
	if(_errors > 0)
	{
		return true;
	}
	else
	{
		$('#form-status').html('');
		$('#form-status').append('<strong class="green">'+'sending...'+'</strong><br>');
		return false;
	}

	return false;
}


function initFormInner() {
	var _submit = $("input#send");
	if(_submit) {
		_submit.click(function () {
			if(!checkFieldsInner()) {
				var _postdata = $("#contactform2 form").serialize();
				$.ajax({
					type: "POST",
					url: "mail2.php",
					data: _postdata,
					success: function(msg){
						$('#form-status').html('');
						if(msg=="sent") {
							$('#form-status').append('<strong class="green">'+successText+'</strong><br>');
						}
						else {
							$('#form-status').append('<strong class="error-text">'+failedText+'</strong><br>');
						}
					}
				});

				return false;
			}
			return false;
		});
	}
}


if (window.addEventListener) window.addEventListener("load", initForm, false);
else if (window.attachEvent) window.attachEvent("onload", initForm);