﻿// Run script on page load

$(document).ready(function()
{
	SetupRowEffects();

});

function SetupRowEffects()
{
	//This adds the hover over to a darker blue
	// on all the reports
	var stripeRow = $(".stripeMe tr");
	stripeRow.mouseover(function()
	{
		$(this).addClass("over");
	});

	stripeRow.mouseout(function()
	{
		$(this).removeClass("over");
	});

	stripeRow.click(function()
	{
		//remove highlighing on old row
		$(".selected").removeClass("selected");

		//highlight the new row
		$(this).addClass("selected");
	});
}

var rowCount = 1;
// This method handles a submition of a Placement approval or denial
function SubmitConfirmation(webPartID, approvalType)
{
	rowCount = 1;

	// get the ID of the table the grid view resides in
	var tblClass = webPartID + "table";

	//Load modal popup
	$("#" + approvalType).modal();

	var approvalCount = 0; // count the number of things that were actually marked for approve/deny

	//foreach row in the table, check to see if the Approval/Denial
	//drop down has been set.
	$("." + tblClass + " tr").each(function()
	{
		// get drop down object
		var approvedDenied = $(this).children("td.ApproveDeny").children("#ApproveDeny");

		// get approval comments text box object
		var approvalText = $(this).children("td.approvalText").children(".approvalText").val();

		// If the drop down has been selected
		if (approvedDenied.val() >= 0)
		{
			approvalCount++;

			//If the drop down is set to "Approved"
			if (approvedDenied.val() == 1)
			{
				//Get the order number out of the table cell
				var orderNumber = $(this).children("td.ordernumber").html();
				//Set table columns in the popup
				$("#" + approvalType + "popupo" + rowCount).html(orderNumber);
				$("#" + approvalType + "popupa" + rowCount).html("Approve");

				// call into callback to execute the stored procedure that approves the placement
				SaveConfirmation(orderNumber + "~" + approvalType + "~" + approvalText + "~true", approvalType + "popupr" + rowCount);
			}

			//If the drop down is set to "Deny"
			else if (approvedDenied.val() == 0)
			{
				//Get the order number out of the table cell
				var orderNumber = $(this).children("td.ordernumber").html();

				//Set table columns in the popup
				$("#" + approvalType + "popupo" + rowCount).html(orderNumber);
				$("#" + approvalType + "popupa" + rowCount).html("Deny");

				// Check to ensure comments were entered, if not, don't deny the order
				if (approvalText == null || approvalText == "")
				{
					$("#" + approvalType + "popupr" + rowCount).html("Failed. Order Number " + orderNumber + " could not be denied because a comment was not specified.");
				}
				else
				{
					SaveConfirmation(orderNumber + "~" + approvalType + "~" + approvalText + "~false", approvalType + "popupr" + rowCount);
				}
			}

			rowCount++;
		}

	});

	// if we didn't actually approve anything in the loop, show that
	if (approvalCount == 0)
	{
		$("#ModalPopupHeader").hide();
		$("#" + approvalType + "popupr" + rowCount).html("<div style='padding: 5px'>Nothing to process. Please approve or deny records before processing.</div>");
	}

}

String.prototype.beginsWith = function(t, i)
{
	if (i == false)
	{
		return (t == this.substring(0, t.length));
	}
	else
	{
		return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
	}
}

String.prototype.endsWith = function(t, i)
{
	if (i == false)
	{
		return (t == this.substring(this.length - t.length));
	}
	else
	{
		return
		(t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
	}
}