// global variable that is used to temporarily set the cost_center 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 cost_center.
// e.g. the user is viewing the cost_center summary and wants to edit one of the
// cost_centers
// This var should be cleared immediately after each use.
var discount_type_cost_center_tmp_edit_number = 0;

function DiscountTypeCostCenter_CreateNewCostCenter()
{
	DiscountTypeCostCenter_HideAddCostCenterErrorBox();
	
	var intended_cost_center_number = document.getElementById('new_cost_center_number').value;
	var intended_cost_center_limit = document.getElementById('new_cost_center_limit').value;
	
	if(!DiscountTypeCostCenter_IsCostCenterValidFormat(intended_cost_center_number))
	{
		DiscountTypeCostCenter_ShowAddCostCenterErrorBox('Invalid Cost Center Number Format');
	}
	else
	{
		if(!DiscountTypeCostCenter_IsLimitValidFormat(intended_cost_center_limit))
		{
			DiscountTypeCostCenter_ShowAddCostCenterErrorBox('Invalid Limit');
		}
		else
		{
			DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=add_cost_center&new_cost_center_number="+intended_cost_center_number+"&new_cost_center_limit="+intended_cost_center_limit+"&", DiscountTypeCostCenter_AddCostCenterHandler);
		}
	}
}

function DiscountTypeCostCenter_ShowAddCostCenterForm()
{
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=show_add_cost_center_form&", DiscountTypeCostCenter_AddCostCenterHandler);
}

// 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 DiscountTypeCostCenter_ShowSummaryCostCenterForm(order_by_field, order_by_direction)
{
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=show_cost_center_summary_form&order_by_field="+order_by_field+"&order_by_direction="+order_by_direction+"&", DiscountTypeCostCenter_SummaryCostCenterHandler);
}

function DiscountTypeCostCenter_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;
	}

	if(parseFloat(limit) < -1)
		return false;

	return true;
}

function DiscountTypeCostCenter_IsCostCenterValidFormat(cost_center_code)
{
	var valid_char_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-";
	
	// determine if cost_center code length is less than 1 or greater than 100
	if(cost_center_code.length < 1 || cost_center_code.length > 100)
		return false;
	else
	{
		// make sure there are only letters, numbers, or dashes
		for(var i=0; i<cost_center_code.length; i++)
		{
			if(valid_char_string.indexOf(cost_center_code.charAt(i)) == -1)
			{
				return false;
			}
		}
		
	}
	return true;
}

function DiscountTypeCostCenter_ShowAddCostCenterErrorBox(text)
{
	document.getElementById('new_cost_center_error').innerHTML = text;
	document.getElementById('new_cost_center_error').style.display = 'block';
}

function DiscountTypeCostCenter_HideAddCostCenterErrorBox()
{
	document.getElementById('new_cost_center_error').style.display = 'none';
}

function DiscountTypeCostCenter_ShowEditCostCenterErrorBox(text)
{
	document.getElementById('edit_cost_center_error').innerHTML = text;
	document.getElementById('edit_cost_center_error').style.display = 'block';
}

function DiscountTypeCostCenter_HideEditCostCenterErrorBox()
{
	document.getElementById('edit_cost_center_error').style.display = 'none';
}

function DiscountTypeCostCenter_AddCostCenterHandler()
{
	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_cost_center_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 DiscountTypeCostCenter_ShowEditCostCenterForm(cost_center_number)
{
	if(!cost_center_number && discount_type_cost_center_tmp_edit_number)
	{
		cost_center_number = discount_type_cost_center_tmp_edit_number;
	}
	discount_type_cost_center_tmp_edit_number = 0;
	
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=show_edit_cost_center_form&cost_center_number="+cost_center_number+"&", DiscountTypeCostCenter_EditCostCenterHandler);
}



function DiscountTypeCostCenter_EditCostCenterUpdate(cost_center_number)
{
	DiscountTypeCostCenter_HideEditCostCenterErrorBox();
	
	var intended_cost_center_limit = document.getElementById('edit_cost_center_limit').value;
	var intended_cost_center_amount_used = document.getElementById('edit_cost_center_amount_used').value;
	
	if(!DiscountTypeCostCenter_IsCostCenterValidFormat(cost_center_number))
	{
		DiscountTypeCostCenter_ShowEditCostCenterErrorBox('Invalid CostC enter Number');
	}
	else
	{
		if(!DiscountTypeCostCenter_IsLimitValidFormat(intended_cost_center_limit))
		{
			DiscountTypeCostCenter_ShowEditCostCenterErrorBox('Invalid Limit');
		}
		else
		{
			if(!DiscountTypeCostCenter_IsLimitValidFormat(intended_cost_center_amount_used))
			{
				DiscountTypeCostCenter_ShowEditCostCenterErrorBox('Invalid Amount Used');
			}
			else
				DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=update_cost_center&cost_center_number="+cost_center_number+"&new_cost_center_limit="+intended_cost_center_limit+"&new_cost_center_amount_used="+intended_cost_center_amount_used+"&", DiscountTypeCostCenter_EditCostCenterHandler);
		}
	}
}
function DiscountTypeCostCenter_EditCostCenterRemove(cost_center_number)
{
	DiscountTypeCostCenter_HideEditCostCenterErrorBox();
	
	if(!DiscountTypeCostCenter_IsCostCenterValidFormat(cost_center_number))
	{
		DiscountTypeCostCenter_ShowEditCostCenterErrorBox('Invalid Cost Center Number');
	}
	else
	{
		if(confirm("Are you sure you want to permanently remove cost center: "+cost_center_number+"?"))
			DoHTTPRequest("index.php?param=discount_admin&operation=discount_cost_center&subop=remove_cost_center&cost_center_number="+cost_center_number+"&", DiscountTypeCostCenter_EditCostCenterHandler);
	}
}

function DiscountTypeCostCenter_EditCostCenterHandler()
{
	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_cost_center_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 DiscountTypeCostCenter_SummaryCostCenterHandler()
{
	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('cost_center_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_discountcostcenter_handler_1()
{
	document.getElementById('new_cost_center_page').innerHTML = 'loading...';
	DiscountTypeCostCenter_ShowAddCostCenterForm();
}
function tabdisplay_discountcostcenter_handler_2()
{
	document.getElementById('edit_cost_center_page').innerHTML = 'loading...';
	DiscountTypeCostCenter_ShowEditCostCenterForm();
}

function tabdisplay_discountcostcenter_handler_3()
{
	document.getElementById('cost_center_summary_page').innerHTML = 'loading...';
	DiscountTypeCostCenter_ShowSummaryCostCenterForm();
}