/*
 * function: comingsoon
 * -> display the "coming soon" modal window
 */
function comingsoon(htmlContentResource) {

	/* Add default values to params object */
	params = new Object();
	params.ajax = htmlContentResource;
	params.modal = true;
	params.overlay = 95;
	params.toTop = true;
	params.onLoad = function(obj) {
		
		/* Handle focus on input fields */
		var InputEl = $('input[type="text"]', obj.w);
		var ButtonEl = $('input[type="submit"]', obj.w);
		
		InputEl.addClass("blur");
		InputEl.focus(function() {
			$(this).removeClass("blur").addClass("focus");
		    if (this.value == this.defaultValue){ 
		    	this.value = '';
			}
			if(this.value != this.defaultValue){
				this.select();
			}
		});
		InputEl.blur(function() {
		    if ($.trim(this.value) == ''){
		    	this.value = (this.defaultValue ? this.defaultValue : '');
		    	$(this).removeClass("focus").addClass("blur");
			}
		});
		/* Disable autofocus */
		setTimeout(function(){InputEl.blur(); }, 1);
		
		
		/* Ajax contact form submission */
		var contactForm = $(obj.w).find("#contactForm");
		
		
		contactForm.ajaxForm({
    		type: "post",
    		dataType: "json",
    		iframe: false,
            beforeSubmit: function() {
				try {
					var InputEl = $('input[type="text"]', contactForm);
					return validateForm(InputEl);
					
				} catch(e) {
					alert(e);
				}
			},
            success: function(data) {
            	if(data.success) {
            		InputEl.val("").removeClass("focus").addClass("blur").focus().blur();
            		$("#contactError").hide();
            		$("#contactSuccess").show();
            		$("#contactForm").find('input[type="text"], input[type="submit"]').attr('disabled', true);
	            	
            	} else {
            		$("#contactSuccess").hide();
            		$("#contactError").show();
            	}
            },
            error: function() {
            	$("#contactSuccess").hide();
            	$("#contactError").show();
            }
		});
		
		/* Ajax mailing list form submission */
		var mailingListForm = $(obj.w).find("#mailingListForm");
		
		mailingListForm.ajaxForm({
    		type: "post",
    		dataType: "json",
    		iframe: false,
            beforeSubmit:	function() {
				var InputEl = $('input[type="text"]', mailingListForm);
				return validateForm(InputEl);
			},
            success: function(data) {
            	if(data.success) {
            		$("#mailingListError").hide();
	            	$("#mailingListSuccess").show();
	            	InputEl.val("").removeClass("focus").addClass("blur").focus().blur();
	            	
	            	$("#mailingListForm").find('input[type="text"], input[type="submit"]').attr('disabled', true);
	            	
            	} else {
            		$("#mailingListSuccess").hide();
            		$("#mailingListError").show();
            	}
            },
            error: function() {
            	$("#mailingListSuccess").hide();
            	$("#mailingListError").show();
            }
		});

	};
	
	/* create and show modal */
	var modalEl = $("<div></div>");
	modalEl.addClass("jqmWindow").appendTo($(document.body)).jqm(params).jqmShow();

}
/*
 * function validateForm
 * -> validate form text input before submission
 */
function validateForm(els) {
	var noErrors = true;
	
	/* fields have default values, these shouldn't be taken as valid values to submit the form */
	var values = {"website": "Website", "name": "Your Name", "phone": "Your Phone Number"};
	
	/* walk through all inputs */
	els.each(function(){
		var val = $(this).val();
		var name = $(this).attr("name");
		var condition = (name == "email")?(validateEmail(val)):(val != "" && val != values[name]);

		/* check condition */
		noErrors = condition?noErrors:false;
		$(this)[(condition?"remove":"add")+"Class"]("error");
	});
    return noErrors;
}
/*
 * function validateEmail
 * -> validate a string as a valid email
 */
function validateEmail(email) {
	var pattern = /^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\.[_a-zA-Z0-9+!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,})$/;
	return (email.search(pattern) == -1)?false:true;
}

/* Initialization */
$(document).ready(function() {
	comingsoon("html/comingsoon.en.html");
});

