// Search.js
// author Aaron Conway
//
// Display destination search fields
//  + main destinations select menu
//  + sub destinations checkboxes
//
// Class variables ***********************
// (reference) pWrap     : element that wraps parent select menu
// (reference) cWrap     : element that wraps child destination checkboxes
// (Array)     dest      : pre-selected destinations ids
// (reference) pMenu     : parent select menu
// (reference) cMenu     : 
// (int)       oneParent : id for single parent if only one exists
// (JSON)      parent    : all parent destinations
// (JSON)      children  : all the children destinations; associated with parent
// ***************************************

// use loadXMLDoc method
document.write("<script type='text/javascript' src='http://www.adventure-life.com/js/loadXMLDoc.js'></script>");


// CONSTRUCTOR
// .(String) pWrap     : id for pWrap class variable
// ?(Array)  dest      : pre-selected destination ids
// ?(int)    oneParent : id for the single parent
// ?(String) cWrap     : id for cWrap class variable
function Search ( pWrap, dest, oneParent, cWrap )
{
	if ( document.getElementById(pWrap) === undefined
		 || pWrap == null || pWrap == '' || pWrap === undefined ) {
		alert("Please specify a location for the main destination menu"); 
		return;
	}
	
	if ( cWrap != null && cWrap !== undefined
		 && document.getElementById(cWrap) === undefined ) {
		alert("The specified location for the sub-destinations does not exist.");
		return;
	}
	
	this.pWrap = document.getElementById(pWrap);
	this.cWrap = (cWrap!=null&&cWrap!==undefined) ? document.getElementById(cWrap) : false;
	this.dest  = (dest!==undefined||dest.length!=0||dest!='') ? dest : false;
	this.pMenu;
	this.cMenu;
	this.oneParent = (oneParent!=null&&oneParent!==undefined) ? oneParent : false;
	this.parents  = {};
	this.children = {};
	
	this.load();
}

Search.prototype.load = function ()
{
	var that = this;
	
	// xml file location
	// setAll method to run after xml is loaded
	loadXMLDoc("http://www.adventure-life.com/js/destinations/cruise.xml", setAll);
	
	// xml : xml document
	function setAll ( xml, jsn )
	{
		/*
		if ( xml != null ) {
			var dest = xml.getElementsByTagName('destination');
			
			for ( var i=0; i<dest.length; i++ ) {
				var id   = dest[i].getAttribute('id');
				var name = dest[i].getAttribute('name');
				that.parents[id] = name;
	
				if ( dest[i].childNodes.length > 0 ) {
					
					var subDest = dest[i].getElementsByTagName('sub');
					
					that.children[id] = {};
					
					for ( var j=0; j<subDest.length; j++ ) {
						var cID   = subDest[j].getAttribute('id');
						var cName = subDest[j].getAttribute('name');
						that.children[id][cID] = cName;
					}
				}
			}
		} else { */
			for ( var i in jsn ) {
				var id   = jsn[i].id;
				var name = jsn[i].destination;
				that.parents[id] = name;
	
				if ( jsn[i].children ) {
					
					var subDest = jsn[i].children;
					
					that.children[id] = {};
					
					for ( var j in subDest ) {
						var cID   = subDest[j].id;
						var cName = subDest[j].destination;
						that.children[id][cID] = cName;
					}
				}
			}
		//}
		that.build();
	}

}


Search.prototype.build = function ()
{
	var that = this;
	var parent;
	var opt;
	var i = 0;
	
	this.pMenu = document.createElement('SELECT');
	this.pMenu.name = "dest[]";
	if ( this.cWrap !== false ) {
		this.pMenu.onchange = function () {
			that.manageBoxes (this.value);
		}
	}
	
	if ( this.oneParent !== false ) {
		opt = document.createElement('INPUT');
		opt.type = "hidden";
		opt.name = "dest[]";
		opt.value = this.oneParent;
		this.pWrap.appendChild(opt);
		this.pWrap.appendChild(document.createTextNode(this.parents[this.oneParent]));
		parent = this.oneParent;
	
	} else {
		opt = document.createElement('OPTION');
		opt.appendChild(document.createTextNode('Please Select'));
		opt.value = 0;
		this.pMenu.appendChild(opt);
	
		for ( var x in this.parents ) {
			opt = document.createElement('OPTION');
			opt.value = x;
			opt.appendChild(document.createTextNode(this.parents[x]));
			
			if ( this.dest !== false ) {
				for ( i=0; i<this.dest.length; i++ ) {
					if ( this.dest[i] == x ) {
						opt.selected = true;
						parent = x;
						break;
					}
				}
			}
			
			this.pMenu.appendChild(opt);
		}
		this.pWrap.appendChild(this.pMenu);
	}
	
	if ( this.cWrap !== false )
		this.manageBoxes ( parent );
	
}


Search.prototype.manageBoxes = function ( parent )
{
	var that = this;
	var arr = this.cWrap.childNodes;
	var lbl;
	var chk;
	var i=0;
	
	// clear cWrap of all child nodes
	if ( arr.length > 0 ) {
		for ( i=arr.length-1; i>=0; i-- )
			if ( arr[i] ) this.cWrap.removeChild(arr[i]);
	}
	
	i=0;
	for ( var x in this.children[parent] ) {
		
		if ( i==0 ) {
			lbl = document.createElement('LABEL');
			lbl.style.fontWeight = "bold";
			lbl.id = "searchChkAll";
			tmp = document.createElement('INPUT');
			tmp.type = "checkbox";
			tmp.onclick = function () {
				that.checkAll(this);
			}
			lbl.appendChild(tmp);
			lbl.appendChild(document.createTextNode("Select All"));
			this.cWrap.appendChild(lbl);
		}
		
		lbl = document.createElement('LABEL');
		tmp = document.createElement('INPUT');
		tmp.type = "checkbox";
		tmp.name = "dest[]";
		tmp.value = x;
		lbl.appendChild(tmp);
		lbl.appendChild(document.createTextNode(this.children[parent][x]));
		this.cWrap.appendChild(lbl);
		
		if ( !((i+1)%4))
			this.cWrap.appendChild(document.createElement('BR'));
		
		i++;
	}
	
	if ( this.dest !== false )
		this.selectedCheckboxes();
}


Search.prototype.checkAll = function ( chkbx )
{
	if ( chkbx.checked == true ) {
		var input = this.cWrap.getElementsByTagName('INPUT');
			
		for ( var i=0; i<input.length; i++ ) {
			if ( input[i].type == 'checkbox' && input[i].name == 'dest[]' )
				input[i].checked = true;
		}
	}
}


Search.prototype.selectedCheckboxes = function ()
{
	var input = this.cWrap.getElementsByTagName('INPUT');
	
	for ( var i=0; i<input.length; i++ ) {
		if ( input[i].type == 'checkbox' && input[i].name == 'dest[]' ) {
			for ( var j=0; j<this.dest.length; j++ ) {
				if ( input[i].value == this.dest[j] ) {
					input[i].checked = true;
					break;
				}
			}
		}
	}
}



