﻿// JScript File

// called when the radio button for weight is toggled
//
//function ChangeWeightToPounds(textBoxID)
//{
//	var text_box = document.getElementById(textBoxID);
//	var ounces = text_box.value;
//	var pounds = ounces / 16;
//	text_box.value = pounds;
//}

// called when the radio button for weight is toggled
//
//function ChangeWeightToOunces(textBoxID)
//{
//	var text_box = document.getElementById(textBoxID);
//	var pounds = text_box.value;
//	var ounces = pounds * 16;
//	text_box.value = ounces;
//}

// ToggleRadioCheckBox
//
function ToggleRadioCheckBox(toggleGroupArray, pattern)
{
	for (var index = 0; index < toggleGroupArray.length; index++)
	{
		var input_control = document.getElementById(toggleGroupArray[index]);
		
		if (input_control != null)
		{
			if (pattern.charAt(index) == '0')
			{
				input_control.checked = false;
			}
		}
	}
}

// ToggleRadioGroup
//
function ToggleRadioGroup(toggleGroupArray)
{
	var parent_checked = false;
	
	for (var index = 0; index < toggleGroupArray.length; index++)
	{
		var input_control = document.getElementById(toggleGroupArray[index]);
		
		if (input_control != null)
		{
			if (input_control.type == "radio")
			{
				parent_checked = input_control.checked;
			}
			else
			{
				if (parent_checked == true)
				{
					input_control.disabled = false;
				}
				else
				{
					input_control.disabled = true;
					input_control.checked = false;
				}
			}
		}
	}
}

function IsNumeric(sText)
{
	var valid_chars = "0123456789.";
	var current_char;
	var found_dot = false;
	
	for (var index = 0; index < sText.length; index++)
	{
		current_char = sText.charAt(index);
		
		if (current_char == '.')
		{
			if (found_dot == true)
				return false;
		
			found_dot = true;
		}
     
		if (valid_chars.indexOf(current_char) == -1)
			return false;
	}
	
	return true;
}


// called when a weight is entered or the calculate button is clicked
//
function Calculate(calculatorControlID, calculatorID, mailServiceID)
{
	var panel_error_id = calculatorControlID + "_PanelError";
	var panel_error = document.getElementById(panel_error_id);
			
	if (panel_error != null)
		panel_error.style.display = "none";
		
	// var for this calculation history
	var sub_history = "" + mailServiceID + "|" + calculatorID + "|";

	// get weight
	//
	var weight_ounces = '';
	var text_field_weight_id = calculatorControlID + "_TextBoxWeight";
	var text_field_weight = document.getElementById(text_field_weight_id);
		
	if (text_field_weight != null)
	{
		if (IsNumeric(text_field_weight.value) == true)
		{
			weight_ounces = text_field_weight.value;
		}
		else
		{
			text_field_weight.value = "0";
			
			if (panel_error != null)
				panel_error.style.display = "inline";
			
			document.getElementById(calculatorControlID + "_LabelError").innerHTML = "An invalid weight was entered.";
			
			return;
		}
	}
		
	if (weight_ounces == '')
		weight_ounces = 0;
		
	var panel_single_piece_weight_id = calculatorControlID + "_PanelSinglePieceWeight";
	var panel_single_piece_weight = document.getElementById(panel_single_piece_weight_id);
	var other_weight = "or " + (weight_ounces / 16) + " pounds";
	
	if (panel_single_piece_weight != null)
	{
		var children = panel_single_piece_weight.getElementsByTagName('input');
		
		if ((children != null) && (children.length > 1))
		{
			if (children[2].checked == true)
			{
				weight_ounces = weight_ounces * 16;
				other_weight = "or " + weight_ounces + " ounces";
			}
		}
	}
	
	var other_weight_label_id = calculatorControlID + "_LabelOtherWeight";
	var other_weight_label = document.getElementById(other_weight_label_id);
	if (other_weight_label != null)
	{
		other_weight_label.innerHTML = other_weight;
	}
	
	sub_history = sub_history + weight_ounces + "|";
	
	// get grid view inputs (number of pieces and destination entry) for each element
	//
	var grid_view_id = calculatorControlID + "_GridViewElements";
	var grid_view = document.getElementById(grid_view_id);
	var row_count = grid_view.rows.length - 1;
	var number_of_peices = new Array(row_count);
	var destination_entries = new Array(row_count);
	var index;
	var add_to_history = false;
								
	for (index = 0; index < row_count; index++)
	{
		var row = index + 2;
		var filler = (row < 10) ? "_ctl0" : "_ctl";
		var text_field_id = grid_view_id + filler + row + "_NumberOfPieces";		
		var text_field = document.getElementById(text_field_id);
		var add_row_to_history = false;
		
		if (text_field != null)
		{
			number_of_peices[index] = text_field.value;
						
			if ((number_of_peices[index] != "0") && (number_of_peices[index] != ""))
			{			
				sub_history = sub_history + "{" + index + "," + number_of_peices[index];
				add_to_history = true;
				add_row_to_history = true;
			}
		}
		
		var	drop_down_field_id = grid_view_id + filler + row + "_DestinationEntries";
		var drop_down_field = document.getElementById(drop_down_field_id);
		
		if (drop_down_field != null)
		{
			destination_entries[index] = drop_down_field.value;
			
			if (add_row_to_history == true)
				sub_history = sub_history + "," + destination_entries[index];
		}
		
		if (add_row_to_history == true)
			sub_history = sub_history + "}";
	}
	
	sub_history = sub_history + "|";
		
	// get surchanges and discounts
	//
	var panel_surcharges_id = calculatorControlID + "_PanelSurcharges";
	var panel_surcharges = document.getElementById(panel_surcharges_id);
	var surcharges = null;
	
	if (panel_surcharges != null)
	{
		var children = panel_surcharges.getElementsByTagName('input');
		if (children != null)
		{
			surcharges = new Array(children.length);
		
			for (index = 0; index < children.length; index++)
			{
				surcharges[index] = children[index].checked;
				sub_history = sub_history + surcharges[index] + ",";
			}
		}
	}
	
	sub_history = sub_history + "|";
	
	if (add_to_history == true)
	{
		var history = document.getElementById("ctl00_ContentPlaceHolder1_HiddenHistory");
				
		if (history != null)
		{
			sub_history = sub_history + ";";
			history.value = history.value + sub_history;
		}
	}
						
	AjaxWebService.Calculate(calculatorID, calculatorControlID, weight_ounces, number_of_peices, destination_entries, surcharges, CalculateSuccess, CalculateFailed, null);
}

// called when the AjaxWebService.Calculate call return with success
//
function CalculateSuccess(result, content)
{
	var index;
	for (index = 0; index < result.length; index += 2)
	{
		/* jmf 3/11/2009 -- not need since we are always showing the total
		 *
		var index_of = result[index].indexOf("_LabelTotal");
	
		if (index_of != -1)
		{
			var panel_total_id = result[index].substring(0, index_of) + "_PanelTotal";
			var panel_total = document.getElementById(panel_total_id);
			
			if (panel_total != null)
				panel_total.style.display = "inline";
		}
		*/		
		
		index_of = result[index].indexOf("_LabelError");
	
		if (index_of != -1)
		{
			var panel_error_id = result[index].substring(0, index_of) + "_PanelError";
			var panel_error = document.getElementById(panel_error_id);
			
			if (panel_error != null)
				panel_error.style.display = "inline";
		}
	
		var element = document.getElementById(result[index]);
		
		if (element != null)
		{
			index_of = result[index].indexOf("_NumberOfPieces");
			if (index_of != -1)
				element.value = result[index+1];
			else
				element.innerHTML = result[index+1];
		}
	}			
}

// called when the AjaxWebService.Calculate call return with failer
//
function CalculateFailed(error)
{
	alert("Error: " + error.get_message() + "\r\n" +
          "Status Code: " + error.get_statusCode() + "\r\n" +
          "Exception Type: " + error.get_exceptionType() + "\r\n" +
          "Timed Out: " + error.get_timedOut());
}
