var i = 0;
var cargo = new Array(10);
var lastNonEmptyCargo = 0;
var txt = "";

function addCargo() 
{
	if (lastNonEmptyCargo < 9)
	{
		lastNonEmptyCargo ++;
		controlId = "cargo" + (lastNonEmptyCargo+1);
		showLayer(controlId);
	}
	document.getElementById("moreCargo").checked = false;
}

function setCargo()
{
	document.getElementById("moreCargo").onclick = function() {addCargo();}
	//document.getElementById("Submit").onclick = function() {resetDisplay();}
	//document.getElementById("Submit2").onclick = function() {resetDisplay();}
	resetCargo();
	resetDisplay();
}

function totalCargo()
{
	var total = 0;
	for (i=0; i < 10; i++) 
	{
		if (!isEmpty(cargo[i].numberOfPieces)) {
			total += parseInt(cargo[i].numberOfPieces);
		}
	}
	return total;
}

function resetCargo()
{
	for (i=0; i < 10; i++) 
	{
		cargo[i] = new Object();
		cargo[i].generalCargoDescription = "";
		cargo[i].numberOfPieces = "";
		cargo[i].hazMat = "";
		cargo[i].unNumber = "";
		cargo[i].classCode = "";
		cargo[i].dims = "";
		cargo[i].weight = "";
		cargo[i].empty = function(i) { return cargoIsEmpty(i);}
	}
}

function cargoIsEmpty(i) {
	if (isEmpty(cargo[i].generalCargoDescription) &&
		isEmpty(cargo[i].numberOfPieces) &&
		isEmpty(cargo[i].unNumber) &&
		isEmpty(cargo[i].classCode) &&
		isEmpty(cargo[i].dims) &&
		isEmpty(cargo[i].weight))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function cargoIsValid() {
	var numberOfTotalPieces = 0;
	var total = 0;
	numberOfTotalPieces = parseInt(document.getElementById("numberOfTotalPieces").value);
	total = parseInt(totalCargo());
	//if (numberOfTotalPieces != total) {
	//	alert("The Number of total Pieces must match Cargo 1-10 no of pcs.");
	//	return false;
	//}
	for (i=0; i < 10; i++)
	{
		if (i == 0) 
		{
			if (cargo[i].empty(i) == true) 
			{
				alert("Please supply the cargo information.");
				return false;
			}
			else
			{
				if (cargo[i].hazMat == "Yes" && (isEmpty(cargo[i].unNumer) || isEmpty(cargo[i].classCode)))
				{
					alert("Please enter both UN Number and Class Code for Haz Mats.");
					return false;
				}
			}
		}
		else
		{
			if (cargo[i].hazMat == "Yes" && (isEmpty(cargo[i].unNumer) || isEmpty(cargo[i].classCode)))
			{
				alert("Please enter both UN Number and Class Code for Haz Mats.");
				return false;
			}
		}
	}
	return true;
}

function getCargoInfo() 
{
	var controlId = "";
	for (var i=0; i < 10; i++) 
	{
		controlId = "generalCargoDescription" + "_cargo" + (i+1);
		cargo[i].generalCargoDescription = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "numberOfPieces" + "_cargo" + (i+1);
		cargo[i].numberOfPieces = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "hazMat" + "_cargo" + (i+1);
		//cargo[i].hazMat = document.getElementById(controlId).options[document.getElementById(controlId).selectedIndex].value;
		if (document.getElementById(controlId).selectedIndex == 0) {
			cargo[i].hazMat = "No";
		} else {
			cargo[i].hazMat = "Yes";
		}
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "unNumber" + "_cargo" + (i+1);
		cargo[i].unNumber = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "classCode" + "_cargo" + (i+1);
		cargo[i].classCode = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "dims" + "_cargo" + (i+1);
		cargo[i].dims = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		controlId = "weight" + "_cargo" + (i+1);
		cargo[i].weight = document.getElementById(controlId).value;
		document.getElementById(controlId).onchange = function() {resetDisplay();}
		if (cargo[i].empty(i) == false) { lastNonEmptyCargo = i; }
	}
}

function resetCargoInfo()
{
	//alert("Resetting cargo info...");
	resetCargo();
	var controlId = "";
	for (var i=0; i < 10; i++) 
	{
		controlId = "generalCargoDescription" + "_cargo" + (i+1);
		cargo[i].generalCargoDescription = "";
		document.getElementById(controlId).value = "";
		controlId = "numberOfPieces" + "_cargo" + (i+1);
		cargo[i].numberOfPieces = "";
		document.getElementById(controlId).value = "";
		controlId = "hazMat" + "_cargo" + (i+1);
		cargo[i].hazMat = "No";
		//document.getElementById(controlId).selectedIndex = 0;
		var list = document.getElementById(controlId);
		for (var intI = 0; intI < list.options.count -1; intI++) {
			list.options[intI].selected = false;
		}
		list.selectedIndex = 0;
		document.getElementById(controlId).onchange = function() {resetDisplay();}

		controlId = "hazMatOpt1" + "_cargo" + (i+1);
		hideLayer(controlId);
		controlId = "hazMatOpt2" + "_cargo" + (i+1);
		hideLayer(controlId);
		
		controlId = "unNumber" + "_cargo" + (i+1);
		cargo[i].unNumber = "";
		document.getElementById(controlId).value = "";
		controlId = "classCode" + "_cargo" + (i+1);
		cargo[i].classCode = "";
		document.getElementById(controlId).value = "";
		controlId = "dims" + "_cargo" + (i+1);
		cargo[i].dims = "";
		document.getElementById(controlId).value = "";
		controlId = "weight" + "_cargo" + (i+1);
		cargo[i].weight = "";
		document.getElementById(controlId).value = "";
	}
	lastNonEmptyCargo = 0;
	resetDisplay();
}

function resetDisplay() 
{
	//alert("Resetting display...");
	var controlId = "";
	getCargoInfo();
	for (var i=0; i < 10; i++) 
	{
		if (cargo[i].empty) {
			if (i > 0 && i > lastNonEmptyCargo) 
			{
				controlId = "cargo" + (i+1);
				hideLayer(controlId);
			}
		}
		else
		{
			controlId = "cargo" + (i+1);
			showLayer(controlId);
		}

		if (cargo[i].hazMat == "No")
		{
			controlId = "unNumber" + "_cargo" + (i+1);
			document.getElementById(controlId).value = "";
			cargo[i].unNumber = document.getElementById(controlId).value;
			controlId = "classCode" + "_cargo" + (i+1);
			document.getElementById(controlId).value = "";
			cargo[i].classCode = document.getElementById(controlId).value;
			controlId = "hazMatOpt1" + "_cargo" + (i+1);
			hideLayer(controlId);
			controlId = "hazMatOpt2" + "_cargo" + (i+1);
			hideLayer(controlId);
		}
		if (cargo[i].hazMat == "Yes")
		{
			controlId = "hazMatOpt1" + "_cargo" + (i+1);
			showLayer(controlId);
			controlId = "hazMatOpt2" + "_cargo" + (i+1);
			showLayer(controlId);
		}
	}
}

function showLayer(layerName)
{
	var targetElement = document.getElementById(layerName);
	targetElement.style.display = 'block';
}

function hideLayer(layerName)
{
 	var targetElement = document.getElementById(layerName);
	targetElement.style.display = 'none';
}

function isEmpty( inputStr ) 
{
	if (inputStr == null || inputStr.trim() == "") {
		return true;
	}
	else
	{
		return false;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
