function Account(numShipMethods) {
	this.shipMethods = new Array(numShipMethods);
	this.index = 0;
	this.setShipMethod = setShipMethod;
	this.filterSelect = filterSelect;
	this.filterDefault = filterDefault;
	
	function setShipMethod(value, text, type) {
		this.shipMethods[this.index] = new ShipMethod(value, text, type);
		this.index++;
	}
	
	function filterSelect(state, shipSelectId) {
		//alert("STATE VALUE: " + state);
		if (state == '') {
			return;
		}
		var origValue = document.getElementById(shipSelectId).value;
		document.getElementById(shipSelectId).options.length = 0;
		var counter = 0;
		if (state == 'AE' || state == 'AP' || state == 'AA' ) {
			for (i in this.shipMethods) {
				if (this.shipMethods[i].type == 'military') {
					document.getElementById(shipSelectId).options[counter] = new Option(this.shipMethods[i].text, this.shipMethods[i].value);
					counter++;
				}
			}
		} else if (state == 'AK' || state == 'HI') {
			for (i in this.shipMethods) {
				if (this.shipMethods[i].type == 'akhi') {
					document.getElementById(shipSelectId).options[counter] = new Option(this.shipMethods[i].text, this.shipMethods[i].value);
					counter++;
				}
			}
		} else {
			for (i in this.shipMethods) {
				//alert("TYPE " + this.shipMethods[i].type);
				if (this.shipMethods[i].type == 'standard') {
					document.getElementById(shipSelectId).options[counter] = new Option(this.shipMethods[i].text, this.shipMethods[i].value);
					counter++;
				}
			}
		}
		for (var i = 0; i < document.getElementById(shipSelectId).options.length; i++) {
			if (document.getElementById(shipSelectId).options[i].value == origValue) {
				document.getElementById(shipSelectId).selectedIndex = i;
			}
		}
	}
	function filterDefault(shipSelectId) {
		document.getElementById(shipSelectId).options.length = 0;
		var counter = 0;
		for (i in this.shipMethods) {
			if (this.shipMethods[i].type == 'standard') {
				document.getElementById(shipSelectId).options[counter] = new Option(this.shipMethods[i].text, this.shipMethods[i].value);
				counter++;
			}
		}
	}
}

function ShipMethod(value, text, type) {
	//Type is one of 'military', 'standard' or 'akhi'
	this.text = text;
	this.value = value;
	this.type = type;
}

function DateFilterer() {
	this.years;
	this.months;
	this.days;
	this.filterBySelections = filterBySelections;
	this.loadDateArrays = loadDateArrays;
	
	function loadDateArrays(month, day, year) {
		this.years = new Array(document.getElementById(year).options.length);
		this.months = new Array(document.getElementById(month).options.length);
		this.days = new Array(document.getElementById(day).options.length);
		for (var i = 0; i < document.getElementById(year).options.length; i++) {
			var helper = new Array(2);
			helper[0] = document.getElementById(year).options[i].text;
			helper[1] = document.getElementById(year).options[i].value;
			this.years[i] = helper;
		}	
		for (var i = 0; i < document.getElementById(month).options.length; i++) {
			var helper = new Array(2);
			helper[0] = document.getElementById(month).options[i].text;
			helper[1] = document.getElementById(month).options[i].value;
			this.months[i] = helper;
		}	
		for (var i = 0; i < document.getElementById(day).options.length; i++) {
			var helper = new Array(2);
			helper[0] = document.getElementById(day).options[i].text;
			helper[1] = document.getElementById(day).options[i].value;
			this.days[i] = helper;
		}	
	}

	function filterBySelections(month, day, year) {
		var selectedMonth = document.getElementById(month).value;
		var selectedYear = document.getElementById(year).value;
		var selectedDay = document.getElementById(day).value;
		var selectedYearForComparison;
		if (selectedYear != '-1') {
			selectedYearForComparison = selectedYear.substring(0, selectedYear.indexOf(',')) + selectedYear.substring(selectedYear.indexOf(',') + 1, selectedYear.length) 
		} else {
			selectedYearForComparison = selectedYear;
		}
		
		//alert('My year ' + selectedYearForComparison);
		//alert('My month ' + selectedMonth);
		//alert('My day ' + selectedDay);
		
		var today = new Date();
		var yearSub = 13;
		
		//alert('Today ' + today.getMonth() + ' ' + today.getDate() + ', ' + today.getFullYear());
		
		document.getElementById(year).options.length = 0;
		document.getElementById(month).options.length = 0;
		document.getElementById(day).options.length = 0;
		
		if (selectedMonth != '-1' && selectedMonth >= today.getMonth()) {
			if (selectedMonth == today.getMonth() && selectedDay != '-1' && selectedDay > today.getDate()) {
				yearSub = 14;
			} else if  (selectedMonth != today.getMonth()) {
				yearSub = 14;
			}
		}
		var counter = 0;
		for (var i = 0; i < this.years.length; i++) {
			if (this.years[i][1] == '-1' || this.years[i][0] <= today.getFullYear() - yearSub) {
				document.getElementById(year).options[counter] = new Option(this.years[i][0], this.years[i][1]);
				counter++;
			}
		}
		counter = 0;
		if (selectedMonth != '-1' && selectedMonth == today.getMonth() && selectedYearForComparison != -1 && selectedYearForComparison == today.getFullYear() - 13) {
			for (var i = 0; i < this.days.length; i++) {
				if (this.days[i][1] == '-1' || this.days[i][1] <= today.getDate()) {
					document.getElementById(day).options[counter] = new Option(this.days[i][0], this.days[i][1]);
					counter++;
				}
			}
		} else {
			for (var i = 0; i < this.days.length; i++) {
				document.getElementById(day).options[counter] = new Option(this.days[i][0], this.days[i][1]);
				counter++;
			}
		}
		counter = 0;
		if (selectedYearForComparison != '-1' && selectedYearForComparison == today.getFullYear() - 13) {
			for (var i = 0; i < this.months.length; i++) {
				if (this.months[i][1] == '-1' || this.months[i][1] < today.getMonth() || (this.months[i][1] == today.getMonth() && (selectedDay == '-1' || selectedDay <= today.getDate()))) {
					document.getElementById(month).options[counter] = new Option(this.months[i][0], this.months[i][1]);
					counter++;
				}
			}
		} else {
			for (var i = 0; i < this.months.length; i++) {
				document.getElementById(month).options[counter] = new Option(this.months[i][0], this.months[i][1]);
				counter++;
			}
		}
		
		for (var i = 0; i < document.getElementById(day).options.length; i++) {
			if (document.getElementById(day).options[i].value == selectedDay) {
				document.getElementById(day).selectedIndex = i;
				break;
			}
		}	
		for (var i = 0; i < document.getElementById(month).options.length; i++) {
			if (document.getElementById(month).options[i].value == selectedMonth) {
				document.getElementById(month).selectedIndex = i;
				break;
			}
		}
		for (var i = 0; i < document.getElementById(year).options.length; i++) {
			if (document.getElementById(year).options[i].value == selectedYear) {
				document.getElementById(year).selectedIndex = i;
				break;
			}
		}
		
		filterInvalidDates(document.getElementById(year), document.getElementById(month), document.getElementById(day), false, false);	
	}
}

function filterInvalidDates(yearSelect, monthSelect, daySelect, putBack, cheetah) {
	var selectedMonth = monthSelect.value;
	if (selectedMonth == '') {
		selectedMonth == '-1';
	}
	if (cheetah && selectedMonth != '-1') {
		selectedMonth--;
	}
	var filterDate = 30;
	if (selectedMonth != '-1' && selectedMonth != '8' && selectedMonth != '3' && selectedMonth != '5' && selectedMonth != '10' && selectedMonth != '1') {
		if (!putBack) {
			return;
		} else {
			filterDate = 31;
		}
	} else {
		if (selectedMonth == -1) {
			filterDate = 31;
		}
		var selectedYearForComparison;
			if (yearSelect.value != '-1' && yearSelect.value != '') {
				selectedYearForComparison = yearSelect.value.substring(0, yearSelect.value.indexOf(',')) + yearSelect.value.substring(yearSelect.value.indexOf(',') + 1, yearSelect.value.length) 
			} else {
				selectedYearForComparison = '-1';
			}
		
		if (selectedMonth == '1' && selectedYearForComparison != '-1' && selectedYearForComparison%4 == 0) {
			filterDate = 29;
		} else if (selectedMonth == '1') {
			filterDate = 28;
		} 
		for (var i = 0; i < daySelect.options.length; i++) {
			if (daySelect.options[i].value != '' && daySelect.options[i].value > filterDate) {
				daySelect.options[i] = null;
				i--;
			}
		}
	}
	if (putBack) {
		for (var i = 1; i <= filterDate; i++) {
			var found = false;
			for (var j = 0; j < daySelect.options.length; j++) {
				if (daySelect.options[j].value != '' && daySelect.options[j].value == i) {
					found = true;
					break;
				}
			}
			if (!found) {
				daySelect.options[daySelect.options.length] = new Option(i, i);
			}
		}
	}
}

function changeAllBoxes(checkbox, num) {
	for (var i = 0; i < num; i++) {
		if (document.getElementById('checkbox' + i)) {
			if (checkbox.checked == true) {
				document.getElementById('checkbox' + i).checked = true;
			} else {
				document.getElementById('checkbox' + i).checked = false;
			}
		}
	}
}


