﻿Wipfli.FraudReport = function()
{
	var _companyID = -1;
	var _reportID = -1;
	var _companyName = "";
	var _companyPhone = "";
	var _companyImage = "";
	var _newReport = true;
	var _defaultPageTitle = "";
	Ext.onReady(this.Initialize.createDelegate(this));
},

	
Ext.extend(Wipfli.FraudReport, Ext.util.Observable,
{
    Initialize : function ()
    {
    	this._defaultPageTitle = document.title;
		$(".fraudReportingOverlay").height($(window).height());
		$(".fraudReportingOverlay").center();
		$(".fraudReportingOverlay").show();
		$(window).scroll(function () { 
										$(".fraudReportingOverlay").height($(window).height());
										$(".fraudReportingOverlay").width($(window).width());
										$(".fraudReportingOverlay").center();
										$("#reminderWindow").center($("#ContentArea"));
		                                $("#thankYouWindow").center($("#ContentArea"));
		                                $("#thankYouCommentWindow").center($("#ContentArea"));
		                                $("#fileReportWindow").center($("#ContentArea"));
										});
    },
    SetReportID : function (reportID)
    {
		
		if(!isNaN(reportID) && reportID != -1)
		{
			this._reportID = parseInt(reportID);
			$("#reportIDHolder").show();
			$(".reportIDText").text(this._reportID);
		}
		else
		{
			this._reportID = -1;
			$("#reportIDHolder").hide();
			$(".reportIDText").text("");
		}	
    },
    ShowFraudReportBox : function ()
	{
		this.CenterOverlayDivs();
		
		$("#submitPhoneButton").click(this.Submit800Number.createDelegate(this));
		$("#reportLoginButton").click(this.ReportRetrieve.createDelegate(this));
		
		$("#phAreaCode").keyup(this.PhoneKeypress.createDelegate(this, [$("#phAreaCode"), $("#phExchange")]));
		$("#phExchange").keyup(this.PhoneKeypress.createDelegate(this, [$("#phExchange"), $("#phNumber")]));
		
		$("#newReportYes").click(this.HideOverlay.createDelegate(this));
		$("#newReportWrongNumber").click(this.WrongCompany.createDelegate(this));
		
		$("#step1Continue, .LeftNav li[name='fraudReportStep2']").click(this.ShowStep2.createDelegate(this));
		
		
		$("#step2Back, .LeftNav li[name='fraudReportStep1']").click(this.ShowStep1.createDelegate(this));
		
		$("#confirmationSubmitButton").click(this.ConfirmationSubmit.createDelegate(this));
		$("#confirmationBackButton").click(this.ConfirmationBack.createDelegate(this));
		
		$("#reminderContinueButton").click(this.CreationContinue.createDelegate(this));
		$("#thankYouContinueButton").click(this.ThankYouContinue.createDelegate(this));
		$("#thankYouCommentContinueButton").click(this.ThankYouCommentContinue.createDelegate(this));
		
		$("#commentAddDetails").click(this.AddDetails.createDelegate(this));
		$("#commentSaveButton").click(this.SaveComment.createDelegate(this));
		$("#commentCancelButton").click(this.CancelComment.createDelegate(this));
		$("#commentDone").click(this.CommentDone.createDelegate(this));
		
		$(".LeftNav li[name='fraudReportStatus']").click(this.ShowStatusSection.createDelegate(this));
	},
	ShowErrorMessage : function (xml)
	{
		var errorNode = $(xml.value).find("r");
		if(errorNode.length > 0)
		{
			alert($(errorNode).attr("Error"));
		}
	},
	CenterOverlayDivs : function()
	{
	    $("#reminderWindow").center($("#ContentArea"));
		$("#thankYouWindow").center($("#ContentArea"));
		$("#thankYouCommentWindow").center($("#ContentArea"));
		$("#fileReportWindow").center($("#ContentArea"));
	},
	Submit800Number : function ()
	{
		var phAreaCode = $("#phAreaCode").val();
		var phExchange = $("#phExchange").val();
		var phNumber = $("#phNumber").val();
		
		if (phAreaCode.length > 0 && phExchange.length > 0 && phNumber.length > 0)
    		Wipfli.AjaxAPI.FraudReporting.Submit800Number(phAreaCode, phExchange, phNumber, this.Submit800Number_callback.createDelegate(this));
    	else
    	    alert("You must enter the phone number");
	},
	Submit800Number_callback : function (res)
	{
		if (res.error && res.error.Message != 'Unknown')
		{
			alert($(res.error).attr("Message"));
		}
		else
		{
			var companyNode = $(res.value).find("r").find("Company");
			if(companyNode.length > 0)
			{
				this.SetReportInfo(companyNode);
				
				$(".newReportForm").slideUp();
				$(".newReportConfirm").slideDown();
				$(".existingReportDiv").slideUp();
				$("#step2Continue").click(this.CreateReport.createDelegate(this));
				this.SetReportID(-1);
			}
			else
			{
				this.ShowErrorMessage(res);
			}
		}
	},
	PhoneKeypress : function(from, to)
	{
	    if (from.val().length == 3)
	        to.select().focus();
	},
	SetReportInfo : function (companyXmlNode)
	{
		this._companyID = $(companyXmlNode).attr("CompanyID");
		this._companyName = $(companyXmlNode).attr("CompanyName");
		this._companyPhone = $(companyXmlNode).attr("Phone");
		this._companyImage = $(companyXmlNode).attr("Img");
		
		$(".newReportCompanyName").text(this._companyName);
		$(".newReportPhoneNumber").text(this._companyPhone);
		if(this._companyImage != "" && this._companyImage)
		{
			$(".newReportImage").show();
			$(".newReportImage").attr("src", this._companyImage).attr("alt", this._companyName);
		}
		else
		{
			$(".newReportImage").hide();
		}
	},
	ShowOverlay : function ()
	{
		$(".fraudReportingOverlay").height($(window).height());
		$(".fraudReportingOverlay").width($(window).width());
		$(".fraudReportingOverlay").center();
		$(".fraudReportLightbox").show();
	},
	HideOverlay : function ()
	{
		$(".fraudReportLightbox").hide();
	},
	WrongCompany : function ()
	{
		$(".newReportCompanyName").text("");
		$(".newReportPhoneNumber").text("");
		$(".newReportImage").attr("src", "").attr("alt", "");
		$(".newReportForm").slideDown();
		$(".newReportConfirm").slideUp();
		$(".existingReportDiv").slideDown();
		this.SetReportInfo("");
	},
	ReportRetrieve : function (res)
	{
		this.SetReportID($("#reportLoginID").val());
		var password = $("#reportLoginPassword").val();
		
		if (this._reportID > 0 && password.length > 0)
		{
    		Wipfli.AjaxAPI.FraudReporting.Retrieve(this._reportID, password, this.ReportRetrieve_callback.createDelegate(this));
        }
    	else
    	    alert('The Report ID and Password are required.');
	},
	ReportRetrieve_callback : function (res)
	{	
		if (res.error && res.error.Message != 'Unknown')
		{
			alert($(res.error).attr("Message"));
		}
		else
		{
			this._newReport = false;
			document.title = "View/Edit an Existing Report";
			
			$("#step2Continue").click(this.ShowStatusSection.createDelegate(this));
			
			var rootNode = $(res.value).find("r");
			if(rootNode.length > 0)
			{
				$(".today").text( rootNode.attr("CurrentDate") );
				
				var companyNode = rootNode.find("Company");
				var reportDataNode = rootNode.find("ReportData");
				
				//Load inputs
				for(var i = 1; i < 14; i++)
				{
					var qNode = $(rootNode).find("Q"+i);
					if(qNode.length > 0)
					{
						$("input[id='q"+i+"'], textarea[id='q"+i+"']").val($(rootNode).find("Q"+i).text());
					}
				}
				
				$("input[id='name']").val($(rootNode).find("Name").text());
				$("input[id='address']").val($(rootNode).find("Address").text());
				$("input[id='phoneNumber']").val($(rootNode).find("PhoneNumber").text());
				$("input[id='emailAddress']").val($(rootNode).find("EmailAddress").text());
				
				if(companyNode.length > 0 && reportDataNode.length > 0)
				{
					this.SetReportInfo(companyNode);
					
					this.ShowStatusSection();
					this.HideOverlay();
					
					this.SetReportID($(reportDataNode).attr("ReportID"));
					
					var nameNode =  reportDataNode.find("Name");
					$(".newCommentName").text( nameNode.text() );
					
					var statusNode = reportDataNode.find("Status");
					$(".statusText").text( statusNode.text() );
					
					var commentsNode = $(reportDataNode).find("Comments");
					if(commentsNode.length > 0)
					{
						$("#commentsFoot").empty();
						var commentsList = $(commentsNode).find("Comment");
						for(var i = 0; i < commentsList.length; i++)
						{
							var commentTr = this.GenerateCommentTR(commentsList.eq(i));
							$("#commentsFoot").append(commentTr);
						}
					}
					
					//Disable inputs
					$("#fraudReportStep1 input, #fraudReportStep2 input, #fraudReportStep1 textarea, #fraudReportStep2 textarea").each(function() {
						$(this).replaceWith("<em>" + $(this).val() + "</em>");
					});					
					
				}
				else
				{
					this.SetReportID(-1);
					alert('Report not found');
				}
			}
		}
	},
	GenerateCommentTR : function (commentNode)
	{
		var tr = $(document.createElement("tr"));
		var date = $(commentNode).attr("Date");
		var author = $(commentNode).attr("Author");
		var dateTD = $(document.createElement("td")).text(date);
		var authorTD = $(document.createElement("td")).text(author);
		var commentTD = $(document.createElement("td")).text($(commentNode).text());
		$(tr).append(dateTD);
		$(tr).append(authorTD);
		$(tr).append(commentTD);
		return tr;
	},
	ShowSection : function (sectionName)
	{
		$(".fraudReportSection").hide();
		$("#" + sectionName).show();
		this.ActivateLeftNavItem(sectionName);
	},
	ShowStep1 : function ()
	{
		this.ShowSection("fraudReportStep1");
		document.title = this._defaultPageTitle;
	},
	ShowStep2 : function ()
	{
		this.ShowSection("fraudReportStep2");
		document.title = "Reporter Info (optional)";
	},
	ShowStatusSection : function ()
	{
		this.ShowSection("fraudReportStatus");
	},
	ActivateLeftNavItem : function (name)
	{
		$("li.current").removeClass("current");
		$("li[name='"+name+"']").addClass("current");
		$("li[name='"+name+"']").show();
	},
	CreateReport : function ()
	{
		var q1 = $("#q1").val();
		var q2 = $("#q2").val();
		var q3 = $("#q3").val();
		var q4 = $("#q4").val();
		var q5 = $("#q5").val();
		var q6 = $("#q6").val();
		var q7 = $("#q7").val();
		var q8 = $("#q8").val();
		var q9 = $("#q9").val();
		var q10 = $("#q10").val();
		var q11 = $("#q11").val();
		
		var name = $("#name").val();
		var address = $("#address").val();
		var phoneNumber = $("#phoneNumber").val();
		var emailAddress = $("#emailAddress").val();
		var contactMethod = $("#contactMethod").val();
		var shareContactInfo = "No";
		if ($(".shareContactInfo")[1].checked)
		{
		    shareContactInfo = "Yes";
		}
		
		Wipfli.AjaxAPI.FraudReporting.CreateReport(this._reportID, q1, q2, q3, q4, q5, q6
                                                , q7, q8, q9, q10, q11, name, address, phoneNumber
                                                , emailAddress, contactMethod, shareContactInfo, this._companyID, this.CreateReport_callback.createDelegate(this));
	},
	CreateReport_callback : function (res)
	{
		if (res.error && res.error.Message != 'Unknown')
		{
			alert($(res.error).attr("Message"));
		}
		else
		{
		    var rootNode = $(res.value).find("r");
            var reportDataNode = rootNode.find("ReportData");
		    
			this.ShowSection("fraudReportConfirmation");
			
			$("#password1, #password2").keyup(this.CheckPasswords.createDelegate(this));
			this.SetReportID($(reportDataNode).attr("ReportID"));
		}
	},
	CheckPasswords : function ()
	{
		var password1 = $("#password1").val();
		var password2 = $("#password2").val();
		if(password1 != password2)
		{
			$(".passwordErrorMsg").text("*This password does not match what was entered above");
		}		
		else
		{
			$(".passwordErrorMsg").text("");
		}
	},
	ConfirmationSubmit : function ()
	{
		var password1 = $("#password1").val();
		var password2 = $("#password2").val();
		var checked = $("#confirmationCheckbox").is(":checked");
		
		var errorMsg = "";
		if(!checked)
		{
			errorMsg = 'You must acknowledge that if you lose the report ID number or password, you will have no way of reviewing this information due to confidentiality rules.';
		}
		if(password1 != password2)
		{
			errorMsg = 'Your passwords do not match.';
		}
		if(password1 == "")
		{
			errorMsg = 'To continue you must enter a password';
		}
		if(errorMsg == "")
		{
			Wipfli.AjaxAPI.FraudReporting.UpdatePassword(this._reportID, password1);
			this.ShowReportReminder();
		}
		else
		{
			alert(errorMsg);
		}
	},
	ConfirmationBack : function ()
	{
		this.ShowStep2();
	},
	CreationContinue : function ()
	{
		this.ShowThankYou();
	},
	ThankYouContinue : function ()
	{
	    window.location = 'Index.aspx';
	},
	ThankYouCommentContinue : function ()
	{
	    $("#thankYouCommentWindow").hide();
	    this.HideOverlay();
	},	
	ShowReportReminder : function ()
	{
		this.ShowOverlay();
		
		$("#fileReportWindow").hide();
		$("#reminderWindow").show();
		$("#thankYouWindow").hide();
		$("#thankYouCommentWindow").hide();
		
		
		$(".fraudReportingOverlay").height($(window).height());
		$(".fraudReportingOverlay").width($(window).width());
		$(".fraudReportingOverlay").center();
		
		$(".fraudReportOverlay").center($("#ContentArea"));
	},
	ShowThankYou : function ()
	{
		this.ShowOverlay();
		
		$("#fileReportWindow").hide();
		$("#reminderWindow").hide();
		$("#thankYouWindow").show();		
		$("#thankYouCommentWindow").hide();
		
		$(".fraudReportingOverlay").height($(window).height());
		$(".fraudReportingOverlay").width($(window).width());
		$(".fraudReportingOverlay").center();
		
		$(".fraudReportOverlay").center($("#ContentArea"));
	},
	ShowThankYouComment : function ()
	{
		this.ShowOverlay();
		
		$("#fileReportWindow").hide();
		$("#reminderWindow").hide();
		$("#thankYouWindow").hide();		
		$("#thankYouCommentWindow").show();
		
		$(".fraudReportingOverlay").height($(window).height());
		$(".fraudReportingOverlay").width($(window).width());
		$(".fraudReportingOverlay").center();
		
		$(".fraudReportOverlay").center($("#ContentArea"));
	},		
	AddDetails : function ()
	{
		$("#commentsBody").show();
		$("#newComment").text("");
	},
	SaveComment : function ()
	{
		var comment = $("#newComment").val();
		Wipfli.AjaxAPI.FraudReporting.AddComment(this._reportID, comment, this.SaveComment_callback.createDelegate(this))
	},
	SaveComment_callback : function (res)
	{
		if (res.error && res.error.Message != 'Unknown')
		{
			alert($(res.error).attr("Message"));
		}
		else
		{
			var commentTr = this.GenerateCommentTR($(res.value).find("Comment"));
			$("#commentsFoot").prepend(commentTr);
			this.CancelComment();
			this.ShowThankYouComment();
		}
	},
	CancelComment : function ()
	{
		$("#commentsBody").hide();
		$("#newComment").val("");
	},
	CommentDone : function()
	{
	    this.ShowThankYou();
	}
});
