// *********************************************************************************************
// Shipdate Functions
// *********************************************************************************************      

// Filter shipdate input
function ShipDateTextBoxKeyPress(e)
{
	var keynum = GetKeyNumFromEvent(e);
	var keychar = GetKeyChar(keynum);
//	return IsNumeric(keychar) || keychar == '/' || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            
	return IsNumericKey(keynum) || keychar == '/' || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            	
}

// *********************************************************************************************
// Pallets Functions
// *********************************************************************************************      

// Filter pallets input
function PalletsTextBoxKeyPress(e)
{
	var keynum = GetKeyNumFromEvent(e);
//	var keychar = GetKeyChar(keynum);
//	return IsNumeric(keychar) || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            
	return IsNumericKey(keynum) || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            	
}

// *********************************************************************************************
// List (Locations, Line Items) Functions
// *********************************************************************************************

function AddLocationClick()
{
	// Validate the location
	if (!IsValidLocationFormat("tbLocationPostalCode","tbLocationCity","selLocationState"))
	{
		return;
	}

	var tbLocationPostalCode = document.getElementById("tbLocationPostalCode");
	var tbLocationCity = document.getElementById("tbLocationCity");
	var selLocationState = document.getElementById("selLocationState");

	// Get the currently selected postal code, city, and state
	var postalCode = tbLocationPostalCode.value;
	var city = tbLocationCity.value;
	var stateAndCountry = selLocationState.options[selLocationState.selectedIndex].value;

	stateAndCountryArray = stateAndCountry.split(",");

	postalCode = trim(postalCode);
	city = trim(city);
	var state = trim(stateAndCountryArray[0]);
	var country = trim(stateAndCountryArray[1]);

	var selLocationList = document.getElementById("selLocationList");

	// This is an arbitrary limit. StarRate can theoretically take an unlimited number of locations, but more than 50 is totally impractical.
	if (selLocationList.options.length == 50)
	{
		alert("You have hit the limit of 50 locations. If you really need to add more, please contact Tranzact.");
		return;
	}

	// Add it to the location list
	selLocationList.options[selLocationList.options.length] = new Option(postalCode + " " + city + ", " + state + " (" + country + ")", postalCode + "," + city + "," + state + "," + country);
}

function AddLineItemClick()
{
	// Validate the line item
	if (!IsValidLineItem("tbWeight", "selClass", "tbPieces"))
	{
		return;
	}

	var tbWeight = document.getElementById("tbWeight");
	var selClass = document.getElementById("selClass");
	var tbPieces = document.getElementById("tbPieces");

	// Get the current weight, class, and pieces
	var weight;
	var shipclass;
	var pieces; 

	weight = trim(tbWeight.value);
	if (selClass)
	{
		shipclass = trim(selClass.options[selClass.selectedIndex].value);
	}
	else
	{
		shipclass = "";
	}
	if (tbPieces)
	{
		pieces = trim(tbPieces.value);
	}
	else
	{
		pieces = "";
	}

	var selLineItemList = document.getElementById("selLineItemList");

	// This is an arbitrary limit. StarRate can theoretically take an unlimited number of locations, but more than 50 is totally impractical.
	if (selLineItemList.options.length == 50)
	{
		alert("You have hit the limit of 50 line items. If you really need to add more, please contact Tranzact.");
		return;
	}

	// Add it to the line item list
	var weightdisplay;
	var shipclassdisplay;
	var piecesdisplay;

	weightdisplay = weight + " lbs.";
	if (shipclass != "")
	{
		shipclassdisplay = "class " + shipclass;
	}
	else
	{
		shipclassdisplay = "default class";
	}
	if (pieces != "" && pieces > 1)
	{
		piecesdisplay = pieces + " pieces";
	}
	else
	{
		piecesdisplay = "1 piece";
	}

	var displaytext = weightdisplay + ", " + shipclassdisplay + ", " + piecesdisplay;
	selLineItemList.options[selLineItemList.options.length] = new Option(displaytext, weight + "," + shipclass + "," + pieces);
}

function ClearAllClick(id)
{
	document.getElementById(id).options.length = 0;
}

function DeleteSelectedClick(id)
{
	var selList = document.getElementById(id);
	var selectedCount = 0;

	// Find out how many items are selected
	for (var i = 0; i < selList.options.length; i++)
	{
		if (selList.options[i].selected)
		{
			selectedCount++;
		}
	}

	// Delete the selected items 
	// Each deletion compresses the array and screws up the count so we have to do a new loop for each.
	for (var j = 0; j < selectedCount; j++)
	{
		for (var k = 0; k < selList.options.length; k++)
		{
			if (selList.options[k].selected)
			{
				// Delete the item and start another loop
				selList.options[k] = null;
				break;
			}
		}
	}
}

// Filter weight input
function WeightKeyPress(e)
{
	var keynum = GetKeyNumFromEvent(e);
	var keychar = GetKeyChar(keynum);
//	return IsNumeric(keychar) || keychar == '.'  || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            
	return IsNumericKey(keynum) || keychar == '.'  || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            	
}

// Filter pieces input
function PiecesKeyPress(e)
{
	var keynum = GetKeyNumFromEvent(e);
//	var keychar = GetKeyChar(keynum);
//	return IsNumeric(keychar) || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            
	return IsNumericKey(keynum) || keynum == 46 || keynum == 40 || keynum == 13 || keynum == 8 || keynum == 9;            	
}

function SettingsOnChange()
{
    // Get the new setting id
	var ddlSettings = document.getElementById("ddlSettings");
	var newSettingsId = ddlSettings.options[ddlSettings.selectedIndex].value;
	
	// Clear the document
	document.close();
	document.open();
	document.write("<html><head><title>StarRate</title></head><strong>Loading...</strong></html>");
	
	var altCookieName = readCookieSubkey("StarRateSettings","AlternateCookieName");
	
	if (altCookieName == "")
	{
	    // Send to settings form to recreate page
        window.location = "SettingsForm.aspx?id=" + newSettingsId;
    }
    else
    {
        // Vendor Site - Send to login form to relogin with preselected id
        window.location = "LoginForm.aspx?login=" + readCookieSubkey("StarRateSettings","Login") + "&id=" + newSettingsId;
    }
}

function NewShipment()
{
    document.getElementById("form1").reset();
    var temp = document.getElementById("selLineItemList");
    if (temp != null)
    {
        ClearAllClick("selLineItemList");
    }
    temp = document.getElementById("selLocationList");
    if (temp != null)
    {
        ClearAllClick("selLocationList");
    }
}

function ddlOriginSitesOnChange()
{
    // If the individual postal code, city, and state controls exist. Populate them with the drop down selection.
    if (document.getElementById("tbOriginPostalCode") != null)
    {
        var ddlOriginSites = document.getElementById("ddlOriginSites");
        var record = ddlOriginSites.options[ddlOriginSites.selectedIndex].value.split(",");
        
        if (record[0] != "")
        {
            document.getElementById("tbOriginPostalCode").value = record[0];
            document.getElementById("tbOriginCity").value = record[1].toUpperCase();
            var stateAndCountry = record[2] + "," + record[3]
            var stateSelect = document.getElementById("selOriginState");
	        for (i = 0; i < stateSelect.options.length; i++)
	        {
		        if (stateSelect.options[i].value == stateAndCountry)
		        {
			        stateSelect.options[i].selected = true;
			        break;
		        }
	        }
	    }
	    else
	    {
	        document.getElementById("tbOriginPostalCode").value = "";
	        document.getElementById("tbOriginCity").value = "";
	        document.getElementById("selOriginState").options[0].selected = true;
	    }
    }
}

function ddlDestinationSitesOnChange()
{
    // If the individual postal code, city, and state controls exist. Populate them with the drop down selection.
    if (document.getElementById("tbDestinationPostalCode") != null)
    {
        var ddlDestinationSites = document.getElementById("ddlDestinationSites");
        var record = ddlDestinationSites.options[ddlDestinationSites.selectedIndex].value.split(",");

        if (record[0] != "")
        {
            document.getElementById("tbDestinationPostalCode").value = record[0];
            document.getElementById("tbDestinationCity").value = record[1].toUpperCase();
            var stateAndCountry = record[2] + "," + record[3]
            var stateSelect = document.getElementById("selDestinationState");
	        for (i = 0; i < stateSelect.options.length; i++)
	        {
		        if (stateSelect.options[i].value == stateAndCountry)
		        {
			        stateSelect.options[i].selected = true;
			        break;
		        }
	        }
	    }
	    else
	    {
	        document.getElementById("tbDestinationPostalCode").value = "";
	        document.getElementById("tbDestinationCity").value = "";
	        document.getElementById("selDestinationState").options[0].selected = true;
	    }
    }
}

