﻿var wantsDemo = true;
var visistatTitleShow = 'Basic Form - ';
var visistatTitleShowSuccess = 'Basic Form Success - ';
var returningUser = false;

function checkReturningUser() {
	var productlabel = $('#simpleFormType').val();
	var cookieVal = readCookie('simpleform-' + productlabel);
	if (cookieVal != null && cookieVal != undefined) {
		if (cookieVal == 'success')
			returningUser = true;
	}
}

function ShowSimpleForm(demo) {
	wantsDemo = demo;
	var productlabel = $('#simpleFormType').val();
	checkReturningUser();

	InsertExtraVisistat(DID, visistatTitleShow + productlabel);

	$('#screen-white-master, #simpleform').css({ opacity: "0" });
	$('#simpleBoxFormThankyou').css({ opacity: "0" });
	$('#simpleBoxform').hide();
	$('#simpleBoxFormThankyou').hide();

	$('.simpleformerror').html('');
	$('#screen-white-master').css({ display: "block" }).fadeTo(500, 0.6);

	if (returningUser == true)
		simplFormReturningUser();
	else {
		$('#simpleBoxform').show();
		$('#simpleBoxform').css({ opacity: "1" });
	}
	$("#simpleform").css({ display: "block" }).fadeTo(600, 1.0);

	$('a.simpleclose').click(
	function () {
		$('#simpleform, #screen-white-master').fadeTo(500, 0.0, hideBoxes);
	}
	);

	function hideBoxes() {
		$('#simpleform, #screen-white-master, #simpleBoxform, #simpleBoxFormThankyou').css({ display: "none" });
		$('#simpleform-messaging').animate({ top: "-300px" }, { duration: 800, easing: 'easeInExpo' });

		$("#simpleform-firstname").val('')
		$("#simpleform-lastname").val('')
		$("#simpleform-state").val('')
		$("#simpleform-email").val('')
		$("#simpleform-phone").val('')
	}
}

function simplFormReturningUser() {
	$("#simpleBoxFormThankyou").show(function () {
		$("#simpleBoxFormThankyou").fadeTo(100, 100.0);
	});
}

function SimpleFormValidation() {
	var errormsg = '';
	var invalidMsg = '';

	var firstname = $("#simpleform-firstname").val()
	var lastname = $("#simpleform-lastname").val()
	var state = $("#simpleform-state").val()
	var email = $("#simpleform-email").val()
	var phone = $("#simpleform-phone").val()
	var country = ''; // $('#country_' + surveyid).val();


	if (firstname == '') errormsg = constructSimpleFormErrorMsg(errormsg, 'First Name');
	if (lastname == '') errormsg = constructSimpleFormErrorMsg(errormsg, 'Last Name');
	if (state == '') errormsg = constructSimpleFormErrorMsg(errormsg, 'State or Province');

	if (email == '') errormsg = constructSimpleFormErrorMsg(errormsg, 'Email Address');
	else if (email.length <= 3) errormsg = constructSimpleFormErrorMsg(errormsg, 'Email Address');
	else if (!(/^\w+([\.\-]\w+)*@\w+([\.\-]\w+)*\.\w{2,4}$/.test(email))) {
		invalidMsg += "The E-mail Address is incorrect\n";
	}

	if (errormsg != '') {
		showSimpleFormJsError(errormsg);
	}
	else if (invalidMsg != '') {
		showSimpleFormJsErrorFinal(invalidMsg, '');
	}
	else {
		com.realprosystems.web.corporate.Utils.SaveSimpleForm(firstname, lastname, state, country, email, phone, '', wantsDemo,
            function (arg) { simpleFormSubmitSuccess(arg); },
            function (arg) { simpleFormSubmitError(arg); },
            function (arg) { simpleFormSubmitError(arg); }
            );
	}
}

function constructSimpleFormErrorMsg(errormsg, addme) {
	if (errormsg != '') {
		errormsg = errormsg + ', ';
	}
	errormsg = errormsg + addme;

	return errormsg;
}

function showSimpleFormJsError(errormsg) {
	if (errormsg != '') {
		showSimpleFormJsErrorFinal(errormsg, 'The following fields cannot be left blank: ');
	}
}

function showSimpleFormJsErrorFinal(errormsg, msgStart) {
	if (errormsg != '') {
		$('.simpleformerror').html(msgStart + errormsg);
	}
	$('#simpleform-messaging').show().animate({ top: "0" }, { duration: 800, easing: 'easeOutExpo' }).animate({ top: "-50px" }, { duration: 300, easing: 'easeInExpo' });
	setTimeout('$("#simpleform-messaging a.simplecloseerror").fadeTo(800, 1.0);', 1200);
	$('#simpleform-messaging a.simplecloseerror').click(
			   function () {
			   	$("#simpleform-messaging a.simplecloseerror").fadeTo(800, 0.0);
			   	$('#simpleform-messaging').animate({ top: "-300px" }, { duration: 800, easing: 'easeInExpo', compete: hideErrorMessaging });
			   }
		    );
	function hideErrorMessaging() {
		$('#simpleform-messaging').hide();
	}
}

function simpleFormSubmitSuccess(arg) {
	if (arg == "true") {
		var productlabel = $('#simpleFormType').val();
		createCookie('simpleform-' + productlabel, 'success', 720);
		//$('#simpleBoxform').hide();
		$("#simpleBoxform").fadeTo(800, 0.0, function () {
			$("#simpleBoxFormThankyou").show(function () {
				$("#simpleBoxFormThankyou").fadeTo(800, 100.0);
			});
		});
		InsertExtraVisistat(DID, visistatTitleShowSuccess + productlabel);
		//$('#simpleBoxFormThankyou').show();

	}
	else {
		simpleFormSubmitError(arg);
	}
}

function simpleFormSubmitError(arg) {
	showSimpleFormJsErrorFinal("There was an error processing your request. Please try again later.", "");
}

