
var popupStatus = 0;

function HideFeedbackMessages(){
	$("#feedbackMsgError1").hide();
	$("#feedbackMsgError2").hide();
	$("#feedbackMsgError3").hide();
	$("#feedbackMsgError4").hide();
	$("#feedbackMsgError5").hide();
	$("#feedbackMsgNoticeSubmitting").hide();
	$("#feedbackMsgNoticeDone").hide();
}

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundFeedback").css({
			"opacity": "0.7"
		});
		$("#backgroundFeedback").fadeIn("slow");
		$("#feedback").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	HideFeedbackMessages();
	
	$("#feedbackemail").val('Your email address[required]');
	$("#feedbackstart").val('What should we start doing?');
	$("#feedbackcontinue").val('What should we continue doing?');
	$("#feedbackstop").val('What should we stop doing?');
	$("#feedbackcomment").val('Any other comments or complaints');

	$(':input','#feedbackFormId')
	.not(':button, :submit, :reset, :hidden, :text')
	.removeAttr('checked')
	.removeAttr('selected');
	
	$('#feedbacksubmit').removeAttr("disabled");
	if(popupStatus==1){
		$("#backgroundFeedback").fadeOut("slow");
		$("#feedback").fadeOut("slow");
		popupStatus = 0;
	}
}

function IsValidEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#feedback").height();
	var popupWidth = $("#feedback").width();
	//centering
	$("#feedback").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundFeedback").css({
		"height": windowHeight
	});
	
}

$(document).ready( function() {
	var feedback_error = 0;
	//Add the CSS for the feedback form form
	var link = $("<link>");
	link.attr({
		type: 'text/css',
		rel: 'stylesheet',
		href: 'http://www.just-brands.net/feedback/public_html/stylesheets/feedback.css'
	});
	$("head").append( link );
	
	//Add the markup for the feedback form
	$('body').append("<div id=\"feedback\">	<form id=\"feedbackFormId\" method=\"POST\" action=\"#\"><input type="hidden" name="PHPSESSID" value="346201d12dbcc24ee1756d94670cc33a" />		<a id=\"feedbackClose\" title=\"Close\">x</a>		<h1>Feedback: Help us improve</h1>		<hr/>		<p id=\"feedbackArea\">			<div class=\"feedbackflash\">				<div id=\"feedbackMsgError1\" class=\"message error\">					<p>The recommendation rating is required</p>				</div>				<div id=\"feedbackMsgError2\" class=\"message error\">					<p>Your email are required</p>				</div>				<div id=\"feedbackMsgError3\" class=\"message error\">					<p>The email address is invalid</p>				</div>				<div id=\"feedbackMsgError4\" class=\"message error\">					<p>The form you are using is invalid</p>				</div>				<div id=\"feedbackMsgError5\" class=\"message error\">					<p>Something went wrong. Please try again.</p>				</div>				<div id=\"feedbackMsgNoticeSubmitting\" class=\"message notice\">					<p>Submitting...Just a moment 						<img src=\"http://www.just-brands.net/feedback/public_html/images/ajax-loader.gif\" alt=\"\">					</p>				</div>				<div id=\"feedbackMsgNoticeDone\" class=\"message notice\">					<p>Thank you for your feedback</p>				</div>			</div>			<input class=\"feedbackInputText\" type=\"text\" id=\"feedbackemail\" name=\"email\" value=\"Your email address[required]\" onfocus=\"if (this.value=='Your email address[required]') { this.value = ''; }\" onblur=\"if (this.value == '') { this.value = 'Your email address[required]'; }\"/>			<br/>			<p class=\"feedbackQuestion\">How likely is it you would recommend us to a friend?[required]</p>			<div class=\"feedbackRadios\">				<input type=\"radio\" name=\"rate\" value=\"0\" title=\"0\">0				<input type=\"radio\" name=\"rate\" value=\"1\" title=\"1\">1				<input type=\"radio\" name=\"rate\" value=\"2\" title=\"2\">2				<input type=\"radio\" name=\"rate\" value=\"3\" title=\"3\">3				<input type=\"radio\" name=\"rate\" value=\"4\" title=\"4\">4				<input type=\"radio\" name=\"rate\" value=\"5\" title=\"5\">5				<input type=\"radio\" name=\"rate\" value=\"6\" title=\"6\">6				<input type=\"radio\" name=\"rate\" value=\"7\" title=\"7\">7				<input type=\"radio\" name=\"rate\" value=\"8\" title=\"8\">8				<input type=\"radio\" name=\"rate\" value=\"9\" title=\"9\">9				<input type=\"radio\" name=\"rate\" value=\"10\" title=\"10\">10(very)			</div>			<br/>			<br/>			<input class=\"feedbackInputText\" type=\"text\" id=\"feedbackstart\" name=\"start\" value=\"What should we start doing?\" onfocus=\"if (this.value=='What should we start doing?') { this.value = ''; }\" onblur=\"if (this.value == '') { this.value = 'What should we start doing?'; }\"/>			<br/>			<br/>			<input class=\"feedbackInputText\" type=\"text\" id=\"feedbackcontinue\" name=\"continue\" value=\"What should we continue doing?\" onfocus=\"if (this.value=='What should we continue doing?') { this.value = ''; }\" onblur=\"if (this.value == '') { this.value = 'What should we continue doing?'; }\"/>			<br/>			<br/>			<input class=\"feedbackInputText\" type=\"text\" id=\"feedbackstop\" name=\"stop\" value=\"What should we stop doing?\" onfocus=\"if (this.value=='What should we stop doing?') { this.value = ''; }\" onblur=\"if (this.value == '') { this.value = 'What should we stop doing?'; }\"/>			<br/>			<br/>			<input class=\"feedbackInputText\" type=\"text\" id=\"feedbackcomment\" name=\"comment\" value=\"Any other comments or complaints\" onfocus=\"if (this.value=='Any other comments or complaints') { this.value = ''; }\" onblur=\"if (this.value == '') { this.value = 'Any other comments or complaints'; }\"/>			<br/>			<br/>			<input type=\"hidden\" name=\"url\" id=\"feedbackUrl\" value=\"\" /><input type=\"hidden\" name=\"csrf\" id=\"feedbackCsrf\" value=\"\" /><input class=\"feedbackInputButton\" type=\"submit\" id=\"feedbacksubmit\" name=\"submit\" value=\"Done\" title=\"Submit\"/>			<br/>		</p>	<form/></div><div id=\"backgroundFeedback\"></div><div id=\"feedbackTab\"><a href=\"?PHPSESSID=346201d12dbcc24ee1756d94670cc33a#\" id=\"feedbackActivator\" title=\"\"></a></div>");
	
	var pathname = window.location;
	$("#feedbackUrl").val(pathname);
	
	var csrf = 'fbbf6d29b035a67b116bac5dd3d5a2db9b56abb9';
	$("#feedbackCsrf").val(csrf);
	
	HideFeedbackMessages();
	
	//LOADING POPUP
	//Click the button event!
	$("#feedbackActivator").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#feedbackClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundFeedback").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	

	$('#feedbackFormId').live('submit', function (e) {
		e.preventDefault();
		feedback_error = 0;
		HideFeedbackMessages();
		
		if (!$("input[@name='rate']:checked").val()){
			$("#feedbackMsgError1").show();
			feedback_error = 1;
			return false;
		}
		
		if ($("#feedbackemail").val() == '' || $("#feedbackemail").val() == 'Your email address[required]'){
			$("#feedbackMsgError2").show();
			feedback_error = 1;
			return false;
		}
		
		if (!IsValidEmail($("#feedbackemail").val())){
			$("#feedbackMsgError3").show();
			feedback_error = 1;
			return false;
		}

		if (feedback_error == 0) {
			$('#feedbacksubmit').attr("disabled", "true");
			$("#feedbackMsgNoticeSubmitting").show();
			
			$.post('http://www.just-brands.net/feedback/submit.php', $('#feedbackFormId').serialize(), function(feedbackform_data) {
				
				if(feedbackform_data.status == 1) {
					HideFeedbackMessages();
					$("#feedbackMsgNoticeDone").show();
					$('#feedbacksubmit').removeAttr("disabled");
					
					$("#feedbackemail").val('Your email address[required]');
					$("#feedbackstart").val('What should we start doing?');
					$("#feedbackcontinue").val('What should we continue doing?');
					$("#feedbackstop").val('What should we stop doing?');
					$("#feedbackcomment").val('Any other comments or complaints');

					$(':input','#feedbackFormId')
					.not(':button, :submit, :reset, :hidden, :text')
					.removeAttr('checked')
					.removeAttr('selected');
				}else{
					
					HideFeedbackMessages();
					$('#feedbacksubmit').removeAttr("disabled");
					
					if (feedbackform_data.error == 1){
						$("#feedbackMsgError1").show();
					}
					if (feedbackform_data.error == 2){
						$("#feedbackMsgError2").show();
					}
					if (feedbackform_data.error == 3){
						$("#feedbackMsgError3").show();
					}
					if (feedbackform_data.error == 4){
						$("#feedbackMsgError4").show();
					}
					if (feedbackform_data.error == 5){
						$("#feedbackMsgError5").show();
					}
					return false;
				}
			}, "json");
		}

	});
});
