/***************************/
// @implementing and optimizing for ACA: Alex Baskov, 2010
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusJoinClient = 0;


//loading popup with jQuery magic!
function loadPopupJoinClient()
{
	//loads popup only if it is disabled
	if(popupStatusJoinClient == 0)
	{
		$("#popupJoinClientBackground").css({
			"opacity": "0.4" // set to zero, if you don't want to have a background
		});
		$("#popupJoinClientBackground").fadeIn("slow");
		$("#popupJoinClient").fadeIn("slow");
		popupStatusJoinClient = 1;
	}
	
	$.validator.addMethod('customEmail', function(value, element) {
		return this.optional(element) || /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(element.value);
		},"Enter a valid email"
	);
	$.validator.addMethod('customPhone', function(value, element) {
		return this.optional(element) || /^[0-9\-\(\)+ ]*$/i.test(element.value);
		},"Enter a valid phone number"
	);
	$.validator.addMethod( 
	  "customRequired", 
	  function(value, element) { 
		if (element.value == element.title) { return false; } 
		else return true; 
	  }, 
	  "Please input something." 
	);
	jc_validator = $("#form_join_client").validate({
		errorElement: "div",
		rules: {
			name: {
				customRequired: true,
				maxlength: 200
			},
			email: {
				customRequired: true,
				customEmail: true,
				maxlength: 200
			},
			phone: {
				customRequired: true,
				customPhone: true,
				maxlength: 200
			},
			job: {
				maxlength: 50
			},
			project: {
				maxlength: 50
			},
			company: {
				maxlength: 50
			},
			address: {
				maxlength: 200
			},
			reason: {
				maxlength: 4000
			},
			agree: {
				required: true
			}
		},
		messages: {
			name: {
				customRequired: "<b>Full Name</b> is missing",
				maxlength: jQuery.format("<b>Full Name</b>: Enter less than {0} characters")
			},
			email: {
				customEmail: "<b>Email</b> address appears to be invalid. Please check. ",
				customRequired: "<b>Email</b> address is missing",
				maxlength: jQuery.format("<b>Email</b>: Enter less than {0} characters")
			},
			phone: {
				customRequired: "<b>Phone</b> is missing",
				customPhone: "<b>Phone</b> appears to be invalid. Please check.",
				maxlength: jQuery.format("<b>Phone</b>: Enter less than {0} characters")
			},
			job: {
				maxlength: jQuery.format("<b>Job</b>:Enter less than {0} characters")
			},
			project: {
				maxlength: jQuery.format("<b>Project</b>:Enter less than {0} characters")
			},
			company: {
				maxlength: jQuery.format("<b>Company</b>:Enter less than {0} characters")
			},
			address: {
				maxlength: jQuery.format("<b>Address</b>:Enter less than {0} characters")
			},
			reason: {
				maxlength: jQuery.format("<b>Reason</b>:Enter less than {0} characters")
			},
			agree: {
				required: "You need to <b>agree</b> if you want to join us!"
			}
		},
		errorPlacement: function(error, element) {
			if ($("#form_join_client .popup_error").length == 0) {
				$("#form_join_client .popup_error_holder").append("<div class='popup_error'></div>").show();
			}
			$("#form_join_client .popup_error").show().append(error);
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
			label.hide();

			if ($("#form_join_client .popup_error .checked").length == $("#form_join_client .popup_error .error").length) {
				$("#form_join_client .popup_error").hide();
			}
			else {
				$("#form_join_client .popup_error").show();
			}
		},
		highlight: function(element, errorClass) {
		},
		invalidHandler: function() {
		},
		submitHandler: function() {
		}
	});
	$("input[class^='it_popup_join']").keyup(function(){
		jc_validator.element("#" + $(this).attr('id'));
	});
	$("#form_join_client input.it_popup_join").change(function(){
		jc_validator.element("#" + $(this).attr('id'));
	});
	$("#form_join_client .popup_error_holder").empty();
	$("#form_join_client .popup_message_inprogress").hide();
	$("#form_join_client .popup_lbl").show();
	$("#form_join_client .popup_fld").show();
	$("#form_join_client .popup_join_tos").show();
	$("#form_join_client .popup_join_submit2").show();
}

//disabling popup with jQuery magic!
function disablePopupJoinClient()
{
	//disables popup only if it is enabled
	if(popupStatusJoinClient == 1)
	{
		$("#popupJoinClientBackground").fadeOut("slow");
		$("#popupJoinClient").fadeOut("slow");
		popupStatusJoinClient = 0;
	}
	$("#form_join_client input").val("");
}

//centering popup
function centerPopupJoinClient()
{
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupJoinClient").height();
	var popupWidth = $("#popupJoinClient").width();

	//centering
	$("#popupJoinClient").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupJoinClientBackground").css({
		"height": windowHeight
	});
	
}

function showJoinClientSuccess() {
	if(popupStatusJoinClient == 1) {
		disablePopupJoinClient();
		centerPopupThankYou();
		$("#thank_you_join").show();
		loadPopupThankYou();
	}
}

function showJoinClientError() {
	$("#form_join_client .popup_message_inprogress").hide();
	$("#form_join_client .popup_error_holder").show();
	$("#form_join_client .popup_lbl").show();
	$("#form_join_client .popup_fld").show();
	$("#form_join_client .popup_join_tos").show();
	$("#form_join_client .popup_join_submit2").show();
}

function showJoinClientProgress() {
	$("#form_join_client .popup_message_inprogress").show();
	$("#form_join_client .popup_error_holder").empty().hide();
	$("#form_join_client .popup_lbl").hide();
	$("#form_join_client .popup_fld").hide();
	$("#form_join_client .popup_join_tos").hide();
	$("#form_join_client .popup_join_submit2").hide();
}

function sendJoinClientRequest() {
	if (!(jc_validator.form())) {
		return false;
	}	
	
	showJoinClientProgress();

	var data = "name=" + escape($("#jc_name").val()) +
			   "&phone=" + escape($("#jc_phone").val()) +
			   "&company=" + escape($("#jc_company").val()) +
			   "&job=" + escape($("#jc_job").val()) +
			   "&email=" + escape($("#jc_email").val()) +
			   "&address=" + escape($("#jc_address").val()) +
			   "&project=" + escape($("#jc_project").val()) +
			   "&accessReason=" + escape($("#jc_reason").val()); 

	$.ajax({
		type: "POST",
		url: "/external/joinClient",
		data: data,
		success: function(msg) {
			var response = JSON.parse(msg);
			if (response.status == "error") {
				if (response.errors != undefined && response.errors.length > 0) {
					var description = "";
					for (var i = 0; i < response.errors.length; i++) {
						var code = response.errors[i].code;
						if (code == "global") code = "";
						else if (code == "name") code = "Full Name: ";
						else if (code == "email") code = "Email: ";
						else if (code == "phone") code = "Phone: ";
						else if (code == "company") code = "Company: ";
						else if (code == "accessReason") code = "Reason: ";
						else if (code == "project") code = "Project: ";
						else if (code == "address") code = "Address: ";
						else if (code == "job") code = "Job: ";
						else code = "";

						if (i != 0) description += "<br/>";
						description += "<b>" + code + "</b>" + response.errors[i].description;
					}
					$("#form_join_client .popup_error_holder").append("<div class='popup_error'>"  + description + "</div>");
				}
				else {
					$("#form_join_client .popup_error_holder").html("<div class='popup_error'>Error to submit your request. Please, try again later!</div>");
				}
				showJoinClientError();
			}
			else {
				showJoinClientSuccess();
			}
		},
		error: function (data, status, e) {
			$("#form_join_client .popup_error_holder").html("<div class='popup_error'>Error to submit your request. Please, try again later!</div>");
			showJoinClientError();
		}
	});
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#buttonJoinClient, #buttonLoginCJoin").click(function() {
		disablePopupLogin();
		disablePopupLoginClient();
		
		centerPopupJoinClient();
		loadPopupJoinClient();
	});
	
	$("#popupJoinClientClose, #buttonJoinAClose").click(function() {
		disablePopupJoinClient();
	});
			
	//Click out event!
	$("#popupJoinClientBackground").click(function() {
		disablePopupJoinClient();
	});
	
	$("#buttonJoinCSubmit").click(sendJoinClientRequest);
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatusJoinClient == 1) {
			disablePopupJoinClient();
		}
	});
	
});


