var URL = "${qas_site.path}products/email-validate-demo/index.htm";
var level = 3;
var LEVEL_INFO = ['check syntax only','check syntax and domain with domain database','check syntax and check domain in real-time','check syntax, domain in real-time and if domain can receive email','check syntax, domain, and if user exists'];

$(document).ready(function(){
	
	//SHOW VALIDATION LEVEL INFO
	$("#validationLevelSelect").change(function(){
		showValidationLevelInfo();		
	});
	
	//SUBMIT FORM
	$("#submit").click(function() {	
			if(simpleValidation()){
				submitForm();
			}
	}); 
	
	//SHOW TOOLTIPS
	$("a.tooltip").mouseover(function(){
			if(level != 0){
     			$("#valTooltip").hide().fadeIn("slow").html("<p>You have chosen to " + LEVEL_INFO[level-1] + ".</p>");	
    		}
    }).mouseout(function(){
      		$("#valTooltip").hide();
    });	
    
});

function showValidationLevelInfo(){	
	level = parseInt($("#validationLevelSelect option:selected").val());
	$("#validationLevelInfo").hide().fadeIn("slow").html("<p>You have chosen to " + LEVEL_INFO[level-1] + ".</p>");	
}

function submitForm(){
		showLoading();
		var emailAddress = $("#emailAddress").val();
		//Set comments field to utilise spam checker
		$("#comments").val(emailAddress);
		var options = { 
	    	target:     "#emailValidateDemoDiv", 
	 	    url:   URL, 
	 	    success:    function () {
	 	    	hideLoading();
	 	    	$("#completionDiv").css("display","block");
	 	    },
	 	    error:    function () {
	 	    	location.reload(true);
	 	    }
		}; 
		$("#validate").ajaxForm(options); 
		// Make call
		$("#validate").submit();	
}

function manualSubmitForm(){
		if(simpleValidation()){
			submitForm();
		}
}
	
function simpleValidation(){

		if (checkEmail()){
			return true;
		} else {
			askForEmail();			
		}	
}

function checkEmail(){
		var emailAddress = $("#emailAddress").val();
		if(emailAddress == ""){
			return false;
		}
		$("#email-validation").html("");
		return true;
}

function askForEmail(){
		$("#email-validation").html("<span class='red'>Please enter an email address.</span>");
}

// Helpers
function showLoading(){
		$("#email-validation").html("");
		$("#tick").css("display","none");
		$("#spinner").css("display","");	
		//$("#file-loading-panel").css("display","block");	
		$("#submit").attr("disabled", "disabled");
		$("#success").remove();	
}

function hideLoading(){
		$("#spinner").css("display","none");
		//$("#file-loading-panel").css("display","none");
		$("#submit").removeAttr("disabled");	
}

function newDemo(){
		$("#spinner").css("display","none");
		$("#completionDiv").css("display","none");
		$("#validationLevelInfo").css("display","none");
		$("#success").remove();
		$("#emailAddress").val("");
		$("#validationLevelSelect").val(3);
}

function trySuggestion(email, level){
		$("#completionDiv").css("display","none");
		$("#validationLevelInfo").css("display","none");
		$("#success").remove();
		$("#emailAddress").val(email);
		$("#validationLevelSelect").val(parseInt(level));
}

function showThenFade(id, duration) {
	$(id).fadeIn('slow')
    	.animate({opacity: 1.0}, duration)
		.fadeOut('slow', function() {		
    	  $(id).css('display', 'none');    		
    	});
}