var tour_groups = new Array();
tour_groups[0] = new tour_group(0,"CORON ISLAND ECOTOURS");
tour_groups[1] = new tour_group(1,"SUN SEA & SAND ECOTOUR"); 
tour_groups[2] = new tour_group(2,"SANGAT IS. CORAL GARDEN & WWII SHIPWRECK ");
tour_groups[3] = new tour_group(3,"CULION HISTORICAL WALKING TOUR");
tour_groups[4] = new tour_group(4,"BUSUANGA ISLANDS ECOTOUR EXPERIENCE");
tour_groups[5] = new tour_group(5,"TREKKING OR MANGROVE KAYAKING & BIRWATCHING AT KINGFISHER PARK");
tour_groups[6] = new tour_group(6,"CALAUIT SAFARI PARK (with side trips BY SEA)");
tour_groups[7] = new tour_group(7,"CALAUIT SAFARI PARK (by LAND)");
tour_groups[8] = new tour_group(8,"CORON TOWN-BASED ECOTOURS");

tour_groups[0].tours[0] = new tour(1,"Siete Pecados Marine Park","Snorkeling, Fish Feeding, Scuba Diving","100");
tour_groups[0].tours[1] = new tour(2,"Kayangan Lake","Minor Hiking, Swimming, Snorkeling, Bird Watching","200");
tour_groups[0].tours[2] = new tour(3,"Twin Lagoons","Swimming, Snorkeling, Kayaking","50");
tour_groups[0].tours[3] = new tour(4,"Barracuda Lake","Minor Climbing, Swimming, Snorkeling, Diving","100");
tour_groups[0].tours[4] = new tour(5,"Banol Beach","Swimming, Snorkeling, Kayaking","100");
tour_groups[0].tours[5] = new tour(6,"Smith Point Whitebeach","Swimming, Snorkeling, Kayaking","100");
tour_groups[0].tours[6] = new tour(7,"Atwayan Beach","Swimming, Snorkeling, Kayaking","100");
tour_groups[0].tours[7] = new tour(8,"Skeleton Wreck","Snorkeling, Fish Feeding, Diving","100");
tour_groups[0].tours[8] = new tour(9,"Beach 91","Swimming, Snorkeling, Kayaking","100");
tour_groups[0].tours[9] = new tour(10,"Calachuchi Beach & Reef","Swimming, Snorkeling, Kayaking","Free");
tour_groups[0].tours[10] = new tour(11,"CYC Island","Snorkeling","Free");
tour_groups[0].tours[11] = new tour(12,"Ecotours Green Lagoon","Swimming, Snorkeling, Kayaking","Free");
tour_groups[0].tours[12] = new tour(13,"Maynuno Beach","Swimming, Snorkeling, Kayaking","Free");
tour_groups[0].tours[13] = new tour(14,"Twin Peaks Reef","Snorkeling","Free");

tour_groups[1].tours[0] = new tour(1,"Malcapuya Island","Swimming, Snorkeling, Kayaking","150");
tour_groups[1].tours[1] = new tour(2,"Banana Island","Swimming, Snorkeling, Kayaking","200");
tour_groups[1].tours[2] = new tour(3,"Malaroyroy Island","Swimming, Snorkeling, Kayaking","Free");
tour_groups[1].tours[3] = new tour(4,"Bulog Island","Swimming, Snorkeling, Kayaking","100");
//tour_groups[1].tours[4] = new tour(5,"Ditaytayan Island","Swimming, Snorkeling, Kayaking","200");
tour_groups[1].tours[4] = new tour(6,"Protected Coral Garden","Snorkeling","Free");

tour_groups[2].tours[0] = new tour(1,"Sangat Gunboat","Snorkeling, Diving","100");
tour_groups[2].tours[1] = new tour(2,"Decalve Marine Park","Snorkeling","100");
tour_groups[2].tours[2] = new tour(3,"Lusong Shipwreck & Coral Garden","Snorkeling, Diving","100");
tour_groups[2].tours[3] = new tour(4,"Ruyukan Mangrove Channel","Sightseeing","Free");
tour_groups[2].tours[4] = new tour(5,"Sangat Coral Garden","Snorkeling","Free");

tour_groups[3].tours[0] = new tour(1,"Old Building & Houses","Walking Tour & Photography","Free");
tour_groups[3].tours[1] = new tour(2,"17th Century Church","Walking Tour & Photography","Free");
tour_groups[3].tours[2] = new tour(3,"Culion Museum","Sightseeing ","200");
//tour_groups[3].tours[3] = new tour(4,"Pulang Lupa Viewdeck","Sightseeing","300");
tour_groups[3].tours[3] = new tour(5,"Bogor Coral Garden","Snorkeling","100");

tour_groups[4].tours[0] = new tour(1,"North Cay Is","Snorkeling, Swimming, BBQ lunch","200");
tour_groups[4].tours[1] = new tour(2,"Calumbuyan Is","Snorkeling, Swimming, BBQ lunch","100");
tour_groups[4].tours[2] = new tour(3,"Lusong Shipwreck & Coral Garden","Snorkeling, Swimming, Diving","100");
tour_groups[4].tours[3] = new tour(4,"Dibutonay Island","Snorkeling, Swimming, BBQ lunch","200");
tour_groups[4].tours[4] = new tour(5,"Ruyukan Mangrove Channel","Sightseeing","Free");

tour_groups[5].tours[0] = new tour(1,"Kingfisher Park Mt Lunes Santo","Hiking, Birdwatching, Photography","1000");
tour_groups[5].tours[1] = new tour(2,"Kingfisher Park Mangroves","Kayaking, Birdwatching","1000");
tour_groups[5].tours[2] = new tour(3,"Fireflies","Firefly Watching","1000");

tour_groups[6].tours[0] = new tour(1,"Calauit Safari Park","Wildlife Safari, Bird watching, Photography","350");
tour_groups[6].tours[1] = new tour(2,"Black Island Caves & Beach","Sightseeing, Swimming","1000");
tour_groups[6].tours[2] = new tour(3,"Panlaitan Fishing Village","Walking Tour in the village, Buy Dried Fish","Free");

tour_groups[7].tours[0] = new tour(1,"Calauit Safari Park ","Wildlife Safari, Birdwatching, Photography","350");
tour_groups[7].tours[1] = new tour(2,"Salvacion Town","Seafood Lunch","Free");
tour_groups[7].tours[2] = new tour(3,"Concepcion Pierhouse Lodge","Sightseeing","Free");

tour_groups[8].tours[0] = new tour(1,"Mt. Tapyas","Hiking, Photography","&nbsp;");
tour_groups[8].tours[1] = new tour(2,"Island Souvenirs Shops","Buy Pasalubong ","&nbsp;");
tour_groups[8].tours[2] = new tour(3,"Cashew Nut Local Producers","Buy Pasalubong","&nbsp;");
tour_groups[8].tours[3] = new tour(4,"Makinit Hot Springs","Therapeutic Bath","100");
tour_groups[8].tours[4] = new tour(5,"Kabu Aplaya Seabreeze","Picnic Lunch, Photography","50");
//tour_groups[8].tours[5] = new tour(6,"Mabentangen Forest Park","Hiking, River Crossing, Birdwatching, Photography","100");
tour_groups[8].tours[5] = new tour(7,"Seafood Market","Buy Pasalubong ","&nbsp;");
//tour_groups[8].tours[7] = new tour(8,"Lambingan Bridge","Sightseeing, Photo Opts","&nbsp;");
tour_groups[8].tours[6] = new tour(9,"Corong Galeri Arts & Crafts","Buy Pasalubong","&nbsp;");
tour_groups[8].tours[7] = new tour(10,"Mt. Tundalara ( 1 DAY )","Hiking, River Crossing, Birwatching, Photography","&nbsp;");

var total_days = 0;
var cover = new Array();

function tour_group(index, label) {
	this.index = index;
	this.label = label;
	this.tours = new Array();
}

function tour(index,destination,activity,price) {
	this.index = index;
	this.destination = destination;
	this.activity = activity;
	this.price = price;
}

function AddNewDay() {		
	if (total_days == 9) return;
	
	var last = document.getElementsByName("tour_group" + (total_days - 1));
	if (total_days > 0) {
		if (last[0].value == -1) return;
	}
	
	var new_div = document.createElement("div");	
	new_div.setAttribute("name","group" + total_days);
	new_div.setAttribute("id","group" + total_days);
	new_div.setAttribute("class","tour_group");
	new_div.style.position = "relative"
	var main_div = document.getElementById("form_holder");
	new_div.innerHTML = "<strong>Day " + (total_days + 1) + "</strong><br /><div id='selGroup" + total_days + "'>" +
		"<select name='tour_group" + total_days + "' style='z-index: 1; position: relative;' onchange='ShowTours(" + total_days + ", this);'>" +
		AddSelections() + "</select><div id='tours" + total_days + "'></div></div>";
					
	main_div.appendChild(new_div);
	//disable last tours
	if (total_days > 0) disableControls(total_days - 1);
	
	total_days ++;
	document.frmtour.total.value = total_days;
}

function AddSelections() {
	var str = "";	
	var main_div = document.getElementById("form_holder");
	var objSelects = main_div.getElementsByTagName("select");
	var used = false;
	var counter = 0;
	
	str += "<option value='-1'> --- </option>";
	for (a=0;a<tour_groups.length;a++) {
		used = false;	//flag for detecting if tour was selected already
		counter = 0;
		for (b=0;b<objSelects.length;b++) {	//iterate through all select objects, check if tour group was already selected
			if (objSelects[b].value == a) {
				used = true;
				counter ++;
			}
			if (a == 0 && counter < 2) used = false;
		}
		if (!used) {	//if this tour group was not used yet, include it in selection ....
			str += "<option value='" + a + "'>" + tour_groups[a].label + "</option>";
		}
	}
	
	return str;
}

function ShowTours(day, obj) {
	var p = document.getElementById("tours" + day);
	var str = "";

	var index = obj.value;
	var checkAll = "<input type='checkbox' onclick='selectAllBoxes(this, " + day + ");' />";
	
	if (index == '-1') {
		str = "";
	} else if (index == 0 && total_days > 1) {	//check if this is "coron island ecotours" - only this tour can be availed twice. remove chosen tours from the first one...
		var main_div = document.getElementById("form_holder");
		var objSelects = main_div.getElementsByTagName("select");
		var prevSelect = false;
		for (var b=0;b<objSelects.length;b++) {	//iterate through all select objects, check if tour group was already selected
			if (objSelects[b].value == 0 && b != day) {	//when "coron island ecotours" is already selected prior to this selection exit loop
				prevSelect = true;	
				break;	
			}
		}
		
		if (prevSelect) {	//if "coron island ecotours" has already been selected prior to this selection check for checkboxes
			var sameGroup = document.getElementById("tours" + b);	//get the DIV of checkboxes for the first "coron island ecotours" selection
			var checkBoxes = sameGroup.getElementsByTagName("input");	//get all the Checkboxes from this DIV for checking what is selected
			
			str = "<table width='100%' class='mytable'>";
			str += "<tr><td class='mytable_head_custom'>" + checkAll + "</td><td class='mytable_head_custom'>Destination</td><td class='mytable_head_custom'>Activities</td><td class='mytable_head_custom'>Price</td></tr>";
			for (a=0;a<tour_groups[index].tours.length;a++) {			
				var selected = false;
				for (b=1;b<checkBoxes.length;b++) {
					if (checkBoxes[b].checked && a + 1 == b) selected = true;
				}
				
				if (!selected) {
					var tour_ = tour_groups[index].tours[a];
					var myname = "tour_" + index + "_" + a + "_" + day;
					str += "<tr><td width='20px'><input type='checkbox' id='" + myname + "' name='" + myname + "' onclick='checkTour(this);' /></td>";
					str += "<td align='left' valign='top' width='40%'><strong>" + tour_.destination + "</strong></td>";
					str += "<td align='left' valign='top' width='40%'>" + tour_.activity + "</td>";
					str += "<td align='right' valign='top'>&nbsp;</td></tr>";
					//str += "<td align='right' valign='top'>" + tour_.price + "</td></tr>";
				}			
			}
			//str += "<tr><td colspan='4'><input type='button' value)' /></td></tr>";
			str += "</table>";
		} else {	//if "coron island ecotours " was not yet selectd show all choices....
			str = "<table width='100%' class='mytable'>";
			str += "<tr><td class='mytable_head_custom'>" + checkAll + "</td><td class='mytable_head_custom'>Destination</td><td class='mytable_head_custom'>Activities</td><td class='mytable_head_custom'>Price</td></tr>";
			for (a=0;a<tour_groups[index].tours.length;a++) {			
				var tour_ = tour_groups[index].tours[a];
				var myname = "tour_" + index + "_" + a + "_" + day;
				str += "<tr><td width='20px'><input type='checkbox' id='" + myname + "' name='" + myname + "' onclick='checkTour(this);' /></td>";
				str += "<td align='left' valign='top' width='40%'><strong>" + tour_.destination + "</strong></td>";
				str += "<td align='left' valign='top' width='40%'>" + tour_.activity + "</td>";
				str += "<td align='right' valign='top'>&nbsp;</td></tr>";
				//str += "<td align='right' valign='top'>" + tour_.price + "</td></tr>";
			}
			str += "</table>";
		}
	} else {
		str = "<table width='100%' class='mytable'>";
		str += "<tr><td class='mytable_head_custom'>" + checkAll + "</td><td class='mytable_head_custom'>Destination</td><td class='mytable_head_custom'>Activities</td><td class='mytable_head_custom'>Price</td></tr>";
		for (a=0;a<tour_groups[index].tours.length;a++) {
			var tour_ = tour_groups[index].tours[a];
			var myname = "tour_" + index + "_" + a + "_" + day;
			str += "<tr><td width='20px'><input type='checkbox' id='" + myname + "' name='" + myname + "' onclick='checkTour(this);' /></td>";
			str += "<td align='left' valign='top' width='40%'><strong>" + tour_.destination + "</strong></td>";
			str += "<td align='left' valign='top' width='40%'>" + tour_.activity + "</td>";
			str += "<td align='right' valign='top'>&nbsp;</td></tr>";
			//str += "<td align='right' valign='top'>" + tour_.price + "</td></tr>";
		}
		str += "</table>";
	}
	
	p.innerHTML = str;
	
}

function RemoveDay() {
	if (total_days == 1) return;
	
	total_days --;
	//alert(lastDiv.getAttribute("name"));
	var main_div = document.getElementById("form_holder");
	main_div.removeChild(main_div.lastChild);	
	enableControls(total_days - 1);
	document.frmtour.total.value = total_days;
}

function checkTour(obj) {
	var tour_parts = obj.name.split("_");
	var day = tour_parts[3];
	var group_index = tour_parts[1];
	var mydiv = document.getElementById("tours" + day);
	var count = 0;
	var checkBoxes = mydiv.getElementsByTagName("input");
	
	if (!obj.checked) return;	//check if checkbox is unchecked... do nothing if it is
	if (group_index > 0) return;
	
	for (a=1;a<checkBoxes.length;a++) {
		if (checkBoxes[a].checked) count ++;
	}
	
	if (count <= 7) return;
	obj.checked = false;
	alert("Sorry, only a maximum of 7 tours can be chosen from this tour group.");
}

function tourWasUsed(obj) {			
	var main_div = document.getElementById("form_holder");
	var objSelects = main_div.getElementsByTagName("select");
	var index = obj.value;
	var count = 0;
	
	for (b=0;b<objSelects.length;b++) {	//iterate through all select objects, check if tour group was already selected
		if (index > 0) {
			if (objSelects[b].value == index && objSelects[b].name != obj.name) return true;
		} else if (index == 0) {
			if (objSelects[b].value == index && objSelects[b].name != obj.name) count ++;
			if (count > 1) return true;
		}
	}
	return false;
}

function selectAllBoxes(obj, i) {
	var mydiv = document.getElementById("selGroup" + i);
	//disable checkboxes from the ith group
	var checkBoxes = mydiv.getElementsByTagName("input");	
	var max = checkBoxes.length;
	var selectBoxes = mydiv.getElementsByTagName("select");
		
	for (a=1;a<checkBoxes.length;a++) {
		checkBoxes[a].checked = false;
	}
	
	if (selectBoxes[0].value == 0) max = 8;
	for (a=1;a < checkBoxes.length && a < max;a++) {
		checkBoxes[a].checked = obj.checked;
	}
}

function disableControls(i) {			
	var main_div = document.getElementById("group" + i);
	var selGroup = document.getElementById("selGroup" + i);	//get ith group of select and checkboxes
	selGroup.style.display = "none";						//hide ith group from view
	var index = selGroup.getElementsByTagName("select")[0].value;	//get selected tour group index
	
	cover.push(document.createElement("div"));				//create new div to display selections
	
	var checkBoxes = selGroup.getElementsByTagName("input");
	var str = "";
		str = "<strong>" + tour_groups[index].label + "</strong>";
		str += "<table width='100%' class='mytable'>";
		str += "<tr><td class='mytable_head_custom'></td><td class='mytable_head_custom'>Destination</td><td class='mytable_head_custom'>Activities</td><td class='mytable_head_custom'>Price</td></tr>";
		//for (a=1; a < tour_groups[index].tours.length; a++) {	//iterates through all tours from this tour group			
		for (a=1; a < checkBoxes.length; a++) {	//iterates through all tours from this tour group			
			if (checkBoxes[a].checked) {						//check tours agains tour checkbox from selGroup DIV
				var cb = checkBoxes[a].id.split("_");
				var tour_ = tour_groups[cb[1]].tours[cb[2]];		//only show the selected checkbox tour 
				str += "<tr><td width='20px'></td>";
				str += "<td align='left' valign='top' width='40%'><strong>" + tour_.destination + "</strong></td>";
				str += "<td align='left' valign='top' width='40%'>" + tour_.activity + "</td>";
				str += "<td align='right' valign='top'>&nbsp;</td></tr>";
				//str += "<td align='right' valign='top'>" + tour_.price + "</td></tr>";
			}
		}
		str += "</table>";
		
	cover[cover.length-1].innerHTML = str;
	main_div.appendChild(cover[cover.length-1]);			//attach new div to form holder DIV
}

function enableControls(i) {
	var main_div = document.getElementById("group" + i);
	var selGroup = document.getElementById("selGroup" + i);
	selGroup.style.display = "block";
	main_div.removeChild(cover.pop());
}

function check_form(obj) {
	var last = document.getElementsByName("tour_group" + (total_days - 1));
	if (total_days > 0) {
		if (last[0].value == -1) {
			alert("Please indicate a tour for the last day.");
			return false;
		}
	}	
}

function toggle_show(id, obj) {
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "block";
		obj.value = "Hide My Tours.";
	} else {
		document.getElementById(id).style.display = "none";
		obj.value = "Show My Tours";
	}
}

