// cache background images on ie
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {
	// do nothing
}

// wait for document to load
$(document).ready(function() {
	// setup event listeners
	$("#sendTBTButtonTemplate").click(function(){
		sendTemplateIR();
	});
	
	$("#sendTBTButtonCustom").click(function(){
		sendCustomIR();
	});
	
	$("#exitButtonTemplate").click(function(){
		window.location = "/recognition.xhtml";
	});
	
	$("#exitButtonCustom").click(function(){
		window.location = "/recognition.xhtml";
	});
	
	showOnlyTemplateIR();
	
	$("#createcustomtbtlink").click(function(){
		showOnlyCustomIR();
	});
	
	$("#createtemplatetbtlink").click(function(){
		showOnlyTemplateIR();
	});
	
	$(".captchaholder").corner("5px");
	$(".captchatext").corner("5px");
	//getCaptchaImage();
	$("#templateverificationbutton").click(function(){
		//getCaptchaImage();
		sendTemplateValidationMail();
	});
	$("#customverificationbutton").click(function(){
		//getCaptchaImage();
		sendCustomValidationMail();
	});
	
	$(".roundinput input").focus(function(){
		if ($(this).val().indexOf("name") != -1 || $(this).val().indexOf("email") != -1) {
			$(this).val("");
		}
	});
	
	$(".roundinput input").blur(function(){
		if($("#recipientnametemplate").val() == ""){
			$("#recipientnametemplate").val("Recipient name");
		}
		if($("#recipientemailtemplate").val() == ""){
			$("#recipientemailtemplate").val("Recipient email");
		}
		if($("#sendernametemplate").val() == ""){
			$("#sendernametemplate").val("Your name");
		}
		if($("#senderemailtemplate").val() == ""){
			$("#senderemailtemplate").val("Your email");
		}
		/*if($("#emailverificationtemplate").val() == ""){
			$("#emailverificationtemplate").val("Your email verification code");
		}*/
		
		if($("#recipientnamecustom").val() == ""){
			$("#recipientnamecustom").val("Recipient name");
		}
		if($("#recipientemailcustom").val() == ""){
			$("#recipientemailcustom").val("Recipient email");
		}
		if($("#sendernamecustom").val() == ""){
			$("#sendernamecustom").val("Your name");
		}
		if($("#senderemailcustom").val() == ""){
			$("#senderemailcustom").val("Your email");
		}
		/*if($("#emailverificationcustom").val() == ""){
			$("#emailverificationcustom").val("Your email verification code");
		}*/
	});
	
	$("#closebutton").click(function(){
		
	});
	
	$(".imghovertemplate").click(function(){
		$("#templateTBTselectedimg").html($(this).html());
	});
	
	$(".imghover").click(function(){
		$("#customTBTselectedimg").html($(this).html());
		$("#customTBTselectedimg").prepend("<h4>Selected picture:</h4>");
	});
	
	$("#previewTBTButtonTemplate").click(function(){
		previewTemplateMail();
	});
	
	$("#previewTBTButtonCustom").click(function(){
		previewCustomMail();
	});
});

  function clearValues() {
    // hiddens
    $("#nomineeparticipantcode").attr("value", "");
    $("#nominationvalueid").attr("value", "");
    $("#nominationcategoryid").attr("value", "");
    $("#recipientlookuptemplate").attr("value", "");
    $("#recipientlookupcustom").attr("value", "");

    // html
    $("#templateTBTselectedimg").html("");
    $("#customTBTselectedimg").html("");

  }

  function showOnlyTemplateIR() {
    clearValues();
    $("#preview").html("");
    $("#customTBTcontainer").hide();
    $("#createtemplatetbtlink h3").removeClass("closed");
    $("#createtemplatetbtlink h3").addClass("open");

    $("#createcustomtbtlink h3").removeClass("open");
    $("#createcustomtbtlink h3").addClass("closed");
     
    $("#templateTBTcontainer").show();

  }

  function showOnlyCustomIR() {
    clearValues();
    $("#preview").html("");
    $("#templateTBTcontainer").hide();

    $("#createtemplatetbtlink h3").removeClass("open");
    $("#createtemplatetbtlink h3").addClass("closed");

    $("#createcustomtbtlink h3").removeClass("closed");
    $("#createcustomtbtlink h3").addClass("open");

    $("#customTBTcontainer").show();
  }

  // have to setup event listeners after data is returned by bean
  // elements don't exist in the document yet.
  function setupIRTemplateListeners() {
    // highlight image on click
    $(".imghovertemplate").click(function(){
        // show selected image and message below recipient lookup
        $("#templateTBTselectedimg").html($(this).html());
        // get the nominationcategoryid and nominationvalueid from the parent id
        var strList =  $(this).parent().attr("id");
        var nomdetail = strList.split("-");
        // get the nomination text
        var tbttext = $(this).text();
        document.getElementById("nominationcategoryid").value = nomdetail[0];
        document.getElementById("nominationvalueid").value    = nomdetail[1];
        // set category and value in nominationsmodule
        $.post("/nominations/0399/ajax/getAjaxBean.php", {bean : "setIRValueBean", valueid : nomdetail[1], categoryid : nomdetail[0], nominationtext: tbttext }, function(data){ processSetValueCallBack(data)}, "json");
       // alert("category:" + nomdetail[0] + "value: " +nomdetail[1]);
       // alert( $(this).parent().attr("id"));
      });
  }

function sendTemplateIR() {
	// validation
	if (confirm("Are you sure you want to send this Say Thank You?")) {
		// check recipient
		if (!$("#recipientnametemplate").val() || $("#recipientnametemplate").val() == "Recipient name") {
			alert ("Recipient not set");
			return false;
		}
		if (!$("#recipientemailtemplate").val()) {
			alert ("Recipient email not set");
			return false;
		}
		if (!$("#sendernametemplate").val() || $("#sendernametemplate").val() == "Your Name") {
			alert ("Sender not set");
			return false;
		}
		if (!$("#senderemailtemplate").val()) {
			alert ("Sender email not set");
			return false;
		}
		
		/*if (!$("#emailverificationtemplate").val() || currentSenderEmail != $("#senderemailtemplate").val()) {
			$("#emailverificationtemplate").val("");
			if (!currentSenderEmail) {
				$(".captchaimage").html("You have not validated the sender email address. Please enter the email address and click the email button to validate.");
			} else if (currentSenderEmail != $("#emailverificationtemplate").val()) {
				$(".captchaimage").html("You have changed the sender email address since validation was done. Please validate the email address again by clicking the email button.");
			}
			
			alert("Sender email not verified");
			return false;
		}*/
		
		// check image selected
		if ($("#templateTBTselectedimg").html() == "") {
			alert ("Picture not selected");
			return false;
		}
		
		$.post(
			"ajax/sendThankYouEmails.php",
			{verification: $("#emailverificationtemplate").val(), recipientname : $("#recipientnametemplate").val(), recipientemail : $("#recipientemailtemplate").val(), sendername : $("#sendernametemplate").val(), senderemail : $("#senderemailtemplate").val(), image : $("#templateTBTselectedimg > img").attr("src"), message : $("#templateTBTselectedimg > span").html()},
			function(data) {
				processSend(data);
			},
			"json"
		);
	}
}

function sendCustomIR() {
	if (confirm("Are you sure you want to send this Say Thank You?")) {
		// check recipient
		if (!$("#recipientnamecustom").val() || $("#recipientnamecustom").val() == "Recipient name") {
			alert ("Recipient not set");
			return false;
		}
		if (!$("#recipientemailcustom").val()) {
			alert ("Recipient email not set");
			return false;
		}
		if (!$("#sendernamecustom").val() || $("#sendernamecustom").val() == "Your Name") {
			alert ("Sender not set");
			return false;
		}
		if (!$("#senderemailcustom").val()) {
			alert ("Sender email not set");
			return false;
		}
		/*if (!$("#emailverificationcustom").val() || currentSenderEmail != $("#senderemailcustom").val()) {
			$("#emailverificationcustom").val("");
			if (!currentSenderEmail) {
				$(".captchaimage").html("You have not validated the sender email address. Please enter the email address and click the email button to validate.");
			} else if (currentSenderEmail != $("#emailverificationcustom").val()) {
				$(".captchaimage").html("You have changed the sender email address since validation was done. Please validate the email address again by clicking the email button.");
			}
			alert ("Sender email not verified");
			return false;
		}*/
		
		// check image selected
		if ($("#customTBTselectedimg").html() == "") {
			alert ("Picture not selected");
			return false;
		}
		
		// check message...
		if (!$("#customtbtmessagetext").val()) {
			alert ("No Message");
			return false;
		}
		
		$.post(
			"ajax/sendThankYouEmails.php",
			{verification: $("#emailverificationcustom").val(), recipientname : $("#recipientnamecustom").val(), recipientemail : $("#recipientemailcustom").val(), sendername : $("#sendernamecustom").val(), senderemail : $("#senderemailcustom").val(), image : $("#customTBTselectedimg > img").attr("src"), message : $("#customtbtmessagetext").val()},
			function(data) {
				processSend(data);
			},
			"json"
		);
	}
}

function processSend(data) {
	if (data.success) {
		// clear fields to send another
		$("#recipientnametemplate").val("");
		$("#recipientemailtemplate").val("");
		$("#recipientnamecustom").val("");
		$("#recipientemailcustom").val("");
		$("#emailverificationtemplate").val("");
		$("#emailverificationcustom").val("");
	}
	
	alert(data.message);
}

// have to setup event listeners after data is returned by bean
// elements don't exist in the document yet.
function setupIRCustomListeners() {
	// highlight image on click
	$(".imghover").click(function(){
		// show selected image and message below recipient lookup
		$("#customTBTselectedimg").html($(this).html());
		$("#customTBTselectedimg").prepend("<h4>Selected Picture:</h4>");
	});
}

function getCaptchaImage() {
	$(".captchaimage").html("<img src='http://al1.awards.co.za/images/extra/3dloader.gif'/>");
	
	var img = new Image();
	$(img).load(function () {
		//$(this).hide();
		$(".captchaimage").html("");
		$(".captchaimage").append(this);
		$(this).fadeIn();
	});
	$(img).error(function () {
		alert("error");
	});
	
	$(img).attr("src", "ajax/captcha/CaptchaSecurityImages.php?bcolor=255.255.255&tcolor=0.0.0&ncolor=88.88.88?rnd="+Math.random());
}

var currentSenderEmail = "";
function sendCustomValidationMail() {
	$(".captchaimage").html("<img src='http://al1.awards.co.za/images/extra/3dloader.gif'/>");
	
	currentSenderEmail = $("#senderemailcustom").val();
	$.post("ajax/sendValidationEmail.php", {email : $("#senderemailcustom").val() }, function(data){ processSendValidationMail(data)});
}

function sendTemplateValidationMail() {
	$(".captchaimage").html("<img src='http://al1.awards.co.za/images/extra/3dloader.gif'/>");
	
	currentSenderEmail = $("#senderemailtemplate").val();
	$.post("ajax/sendValidationEmail.php", {email : $("#senderemailtemplate").val() }, function(data){ processSendValidationMail(data)});
}

function processSendValidationMail(data) {
	if (data == 1) {
		$(".captchaimage").html("Your verification code has been sent to the sender email address above. If you do not receive it, please check the email address and click the email button again.");
		$("#sendTBTButtonTemplate").show();
		$("#sendTBTButtonCustom").show();
	} else {
		currentSenderEmail = "";
		$(".captchaimage").html(data);
		$("#sendTBTButtonTemplate").hide();
		$("#sendTBTButtonCustom").hide();
	}
}

function previewTemplateMail() {
	$.post(
		"ajax/previewThankYouEmail.php",
		{recipientname : $("#recipientnametemplate").val(), sendername : $("#sendernametemplate").val(), image : $("#templateTBTselectedimg > img").attr("src"), message : $("#templateTBTselectedimg > span").html()},
		function(data) {
			processPreviewMail(data);
		},
		"json"
	);
}

function previewCustomMail() {
	$.post(
		"ajax/previewThankYouEmail.php",
		{recipientname : $("#recipientnamecustom").val(), sendername : $("#sendernamecustom").val(), image : $("#customTBTselectedimg > img").attr("src"), message : $("#customtbtmessagetext").val()},
		function(data) {
			processPreviewMail(data);
		},
		"json"
	);
}

function processPreviewMail(data) {
	if (data.success) {
		$("#preview").css("display", "none");
		$("#preview").html("<br/>"+data.message+"<br/><br/>");
		$("#preview").focus();
		
		tb_show("Preview", "#TB_inline?height=420&width=780&inlineId=preview", "");
	} else {
		alert(data.message);
	}
}

function countChars() {
	$("#charcountused").html($("#customtbtmessagetext").val().length);
}

