// global variable that is used to temporarily set the budget number to edit.
// this is used in circumstances where the user is coming from a dynamically
// generated page and using javascript to directly edit a budget.
// e.g. the user is viewing the budget summary and wants to edit one of the
// budgets
// This var should be cleared immediately after each use.
var discount_type_budget_tmp_edit_number = 0;

function DiscountTypeBudget_CreateNewBudget()
{
	DiscountTypeBudget_HideAddBudgetErrorBox();
	
	var intended_budget_number            = document.getElementById('new_budget_number').value;
	var intended_budget_limit             = document.getElementById('new_budget_limit').value;
	var intended_budget_name              = document.getElementById('new_budget_name').value;
	var intended_supervisor_email_address = document.getElementById('new_supervisor_email_address').value;
	var intended_blanket_po               = document.getElementById('new_blanket_po').value;
	var intended_intended_employee        = document.getElementById('new_intended_employee').value;
	
	if(!DiscountTypeBudget_IsBudgetValidFormat(intended_budget_number))
	{
		DiscountTypeBudget_ShowAddBudgetErrorBox('Invalid Budget Number Format');
	}
	else
	{
		if(!DiscountTypeBudget_IsLimitValidFormat(intended_budget_limit))
		{
			DiscountTypeBudget_ShowAddBudgetErrorBox('Invalid Limit');
		}
		else
		{
			DoHTTPRequest("index.php"
							+"?param=discount_admin"
							+"&operation=discount_budget"
							+"&subop=add_budget"
							+"&new_budget_number="				+ intended_budget_number
							+"&new_budget_limit="				+ intended_budget_limit
							+"&new_budget_name="				+ intended_budget_name
							+"&new_supervisor_email_address="	+ intended_supervisor_email_address
							+"&new_blanket_po="					+ intended_blanket_po
							+"&new_intended_employee="			+ intended_intended_employee
							+"&", DiscountTypeBudget_AddBudgetHandler);
		}
	}
}

function DiscountTypeBudget_EditBudgetInputBoxKeyDown()
{
	document.getElementById('edit_budget_save_change_button').style.display = '';
	document.getElementById('edit_budget_cancel_change_button').style.display = '';
	document.getElementById('edit_budget_save_change_button_disabled').style.display = 'none';
	document.getElementById('edit_budget_cancel_change_button_disabled').style.display = 'none';
}

function DiscountTypeBudget_ShowAddBudgetForm()
{
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_budget&subop=show_add_budget_form&", DiscountTypeBudget_AddBudgetHandler);
}

// if the order by field and direction are blank they will be defaulted in php.
// valid order_by_field values are specified in php.
// valid order_by_direction values are "asc" or "desc".
function DiscountTypeBudget_ShowSummaryBudgetForm(order_by_field, order_by_direction)
{
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_budget&subop=show_budget_summary_form&order_by_field="+order_by_field+"&order_by_direction="+order_by_direction+"&", DiscountTypeBudget_SummaryBudgetHandler);
}
function DiscountTypeBudget_ShowPOSummaryForm()
{
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_budget&subop=show_po_summary_form&", 
	function()
	{
		var http_output;
		// only if req shows "complete"
		if (do_req.readyState == 4)
		{
			// only if "OK"
			if (do_req.status == 200)
			{
				http_output = do_req.responseText;
				document.getElementById('po_summary_page').innerHTML = http_output;
				//alert(http_output);
			}
			else
			{
				alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
			}
		}

		DisplayReadyState((do_req.readyState==4?null:do_req.readyState));
	});
}

function DiscountTypeBudget_IsLimitValidFormat(limit)
{
	var valid_char_string = "1234567890.";
	
	
	if(limit.length < 1 || limit.length > 100)
		return false;
	else
	{
		// make sure there are only numbers and decimal points
		for(var i=0; i<limit.length; i++)
		{
			if(valid_char_string.indexOf(limit.charAt(i)) == -1)
			{
				return false;
			}
		}
	
		// make sure there is only one decimal place
		if(limit.indexOf('.') != limit.lastIndexOf('.'))
			return false;
	}
	return true;
}

function DiscountTypeBudget_IsBudgetValidFormat(budget_code)
{
	var valid_char_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-";
	
	// determine if budget code length is less than 1 or greater than 100
	if(budget_code.length < 1 || budget_code.length > 100)
		return false;
	else
	{
		// make sure there are only letters, numbers, or dashes
		for(var i=0; i<budget_code.length; i++)
		{
			if(valid_char_string.indexOf(budget_code.charAt(i)) == -1)
			{
				return false;
			}
		}
		
	}
	return true;
}

function DiscountTypeBudget_ShowAddBudgetErrorBox(text)
{
	document.getElementById('new_budget_error').innerHTML = text;
	document.getElementById('new_budget_error').style.display = 'block';
}

function DiscountTypeBudget_HideAddBudgetErrorBox()
{
	document.getElementById('new_budget_error').style.display = 'none';
}

function DiscountTypeBudget_ShowEditBudgetErrorBox(text)
{
	document.getElementById('edit_budget_error').innerHTML = text;
	document.getElementById('edit_budget_error').style.display = 'block';
}

function DiscountTypeBudget_HideEditBudgetErrorBox()
{
	document.getElementById('edit_budget_error').style.display = 'none';
}

function DiscountTypeBudget_AddBudgetHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			document.getElementById('new_budget_page').innerHTML = http_output;
			//alert(http_output);
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}
	
	DisplayReadyState((do_req.readyState==4?null:do_req.readyState));
}

function DiscountTypeBudget_ShowEditBudgetForm(budget_number)
{
	if(!budget_number && discount_type_budget_tmp_edit_number)
	{
		budget_number = discount_type_budget_tmp_edit_number;
	}
	discount_type_budget_tmp_edit_number = 0;
	
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_budget&subop=show_edit_budget_form&budget_number="+budget_number+"&", DiscountTypeBudget_EditBudgetHandler);
}



function DiscountTypeBudget_EditBudgetUpdate(budget_number)
{
	DiscountTypeBudget_HideEditBudgetErrorBox();
	
	var intended_budget_limit             = document.getElementById('edit_budget_limit').value;
	var intended_budget_amount_used       = document.getElementById('edit_budget_amount_used').value;
	var intended_supervisor_email_address = document.getElementById('edit_budget_supervisor_email_address').value;
	var intended_blanket_po               = document.getElementById('edit_budget_blanket_po').value;
	var intended_intended_employee        = document.getElementById('edit_budget_intended_employee').value;
	
	if(!DiscountTypeBudget_IsBudgetValidFormat(budget_number))
	{
		DiscountTypeBudget_ShowEditBudgetErrorBox('Invalid Budget Number');
	}
	else
	{
		if(!DiscountTypeBudget_IsLimitValidFormat(intended_budget_limit))
		{
			DiscountTypeBudget_ShowEditBudgetErrorBox('Invalid Limit');
		}
		else
		{
			if(!DiscountTypeBudget_IsLimitValidFormat(intended_budget_amount_used))
			{
				DiscountTypeBudget_ShowEditBudgetErrorBox('Invalid Amount Used');
			}
			else
			{
				DoHTTPRequest("index.php"
								+"?param=discount_admin"
								+"&operation=discount_budget"
								+"&subop=update_budget"
								+"&budget_number="					+ budget_number
								+"&new_budget_limit="				+ intended_budget_limit
								+"&new_budget_amount_used="			+ intended_budget_amount_used
								+"&new_supervisor_email_address="	+ intended_supervisor_email_address
								+"&new_blanket_po="					+ intended_blanket_po
								+"&new_intended_employee="			+ intended_intended_employee
								+"&", DiscountTypeBudget_EditBudgetHandler);
			}
		}
	}
}
function DiscountTypeBudget_EditBudgetRemove(budget_number)
{
	DiscountTypeBudget_HideEditBudgetErrorBox();
	
	if(!DiscountTypeBudget_IsBudgetValidFormat(budget_number))
	{
		DiscountTypeBudget_ShowEditBudgetErrorBox('Invalid Budget Number');
	}
	else
	{
		if(confirm("Are you sure you want to permanently remove budget: "+budget_number+"?"))
			DoHTTPRequest("index.php?param=discount_admin&operation=discount_budget&subop=remove_budget&budget_number="+budget_number+"&", DiscountTypeBudget_EditBudgetHandler);
	}
}

function DiscountTypeBudget_EditBudgetHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			document.getElementById('edit_budget_page').innerHTML = http_output;
			//alert(http_output);
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}
	
	DisplayReadyState((do_req.readyState==4?null:do_req.readyState));
}

function DiscountTypeBudget_SummaryBudgetHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			document.getElementById('budget_summary_page').innerHTML = http_output;
			//alert(http_output);
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}

	DisplayReadyState((do_req.readyState==4?null:do_req.readyState));
}

function tabdisplay_discountbudget_handler_1()
{
	document.getElementById('new_budget_page').innerHTML = 'loading...';
	DiscountTypeBudget_ShowAddBudgetForm();
}
function tabdisplay_discountbudget_handler_2()
{
	document.getElementById('edit_budget_page').innerHTML = 'loading...';
	DiscountTypeBudget_ShowEditBudgetForm();
}

function tabdisplay_discountbudget_handler_3()
{
	document.getElementById('budget_summary_page').innerHTML = 'loading...';
	DiscountTypeBudget_ShowSummaryBudgetForm();
}
