/*  Searchable Dropdown component/widget code */


function Element(eid, label) {
	this.eid = eid;
	this.label = label;
	this.active = false;
}

function ElementList(name, div, inputClass, searchClass, elementClass, buttonClass, fullListClass, returnCount, startWith) {
	this.elements = new Array();
	this.paramName = name;
	this.div = div;
	this.inputClass = inputClass;
	this.searchClass = searchClass;
	this.input = null;
	this.search = null;
	this.hidden = null;
	this.filteredElements = new Array();
	this.returnCount = returnCount;
	this.startWith = startWith;
	this.buttonClass = buttonClass;
	this.fullButton = null;
	this.fullListClass = fullListClass;
	this.fullList = null;
	this.bottomDiv = null;                       
}
                          
ElementList.prototype.redraw = function() {
	var topDiv = document.createElement("div");
	var bottomDiv = document.createElement("div"); 
	var hidden = document.createElement("input");
	hidden.id = this.paramName;
	hidden.name = this.paramName;
	hidden.style.display = "none";
	this.div.appendChild(hidden);
	this.hidden = hidden;
	                                         
	var o = this;
	var input = document.createElement("input");
	input.id = "input_"+this.paramName;
	input.name = input.id;
	input.className = this.inputClass;
	input.onkeyup = function(ev) { return o.updateSearch(ev, o); };
	input.onclick = function(ev) { return o.reopenSearch(ev, o); };
	this.input = input;
	
	var fButton = document.createElement("input");
	fButton.type = "button";
	fButton.id = "button_"+this.paramName;
	fButton.name = fButton.id;           
	fButton.value = "select";
	fButton.className = this.buttonClass;
	fButton.onclick = function(ev) { return o.openFullList(ev, o); };
	this.fullButton = fButton;	
	                            
	topDiv.appendChild(input);
	topDiv.appendChild(fButton);
	this.div.appendChild(topDiv);
	this.div.appendChild(bottomDiv);
	this.bottomDiv = bottomDiv;
	                               
	if ((this.input.value == "") && (this.startWidth != "")) {
		this.input.value = this.startWith;
		var i;
		for (i = 0; i < this.elements.length; i++) {
			if (this.elements[i].label == this.startWith) this.hidden.value = this.elements[i].eid;
		}
	}	
	//if (input.value != "") input.value = this.startWith; 
}

ElementList.prototype.reopenSearch = function(ev, o) {
	if ((o.input.value.length > 0) && (o.search == null)) {
		o.updateSearch(ev, o);
	}
} 

ElementList.prototype.updateSearch = function(ev, o) {
	if (o.input.value.length < 1) {
		o.destroySearch();
		return 0;
	}
	if (o.search == null) {
		o.destroyFullList();
		o.createSearch();
		o.filterElements();
		o.rebuildElements();
	} else {
		o.filterElements();
		o.rebuildElements();
	}
}

ElementList.prototype.createSearch = function() {
	this.bottomDiv.style.position = "absolute";
	var X = this.bottomDiv.offsetLeft;
	var Y = this.bottomDiv.offsetTop;
	var search = document.createElement("div");
	search.style.position = "absolute";
	search.style.left = X+"px";
	search.style.top = Y+"px";
	search.className = this.searchClass;
	search.id = this.paramName+"_search";
	document.body.appendChild(search);
	this.search = search;
}

ElementList.prototype.createFullList = function() {
	this.bottomDiv.style.position = "absolute";
	var fullList = document.createElement("div");
	fullList.style.position = "absolute";
	fullList.className = this.fullListClass;
	fullList.style.overflowY = "auto";
	fullList.id = this.paramName+"_fullList";
	
	this.fullButton.style.position = "absolute";
	var X = this.fullButton.offsetLeft;
	this.fullButton.style.position = "relative";
	
	var Y = this.bottomDiv.offsetTop;
	fullList.style.left = X+"px";
	fullList.style.top = Y+"px";
	document.body.appendChild(fullList);
	this.fullList = fullList;
}

ElementList.prototype.destroySearch = function() {
	if (this.search != null) {
		document.body.removeChild(this.search);
		this.search = null;
	}
}

ElementList.prototype.destroyFullList = function() {
	if (this.fullList != null) {
		document.body.removeChild(this.fullList);
		this.fullList = null;
	}
}	

ElementList.prototype.rebuildElements = function() {
	if (this.filteredElements.length < 1) {
		this.destroySearch(); 
		return 0;
	}
   	this.search.innerHTML = "";
	var i = 0;                        
	var set = this.filteredElements.slice(0, this.returnCount);
	for (i = 0; i < set.length; i++) {
		var e = document.createElement("div");
		e.className = this.elementClass;
		e.id = this.paramName+"_"+this.filteredElements[i].eid+"_element";
		e.innerHTML = this.filteredElements[i].label;
		e.style.display = "block";
		this.search.appendChild(e);
		var o = this;
		e.onclick = function(ev) { return o.selectElement(ev, o, o.filteredElements, o.search, "_element"); };
		e.onmouseover = function(ev) { return o.highlightElement(ev, o, o.filteredElements, o.search, "_element", "high"); };
		e.onmouseout = function(ev) { return o.highlightElement(ev, o, o.filteredElements, o.search, "_element", "low"); };
	}                                                  
                                                            
	var r = findAbsolute(this.search);
	var baseH;
	if (typeof(window.innerHeight) == 'number') {
		baseH = window.innerHeight;
		ofY = window.pageYOffset;
	} else {
		baseH = document.documentElement.clientHeight;
		ofY = document.body.scrollTop;
	}
	var bottomOfList = r[1] + this.search.clientHeight;
	var bottomOfPage = baseH + ofY;   
	if (bottomOfList > bottomOfPage) {
		window.scrollTo(this.search.offsetLeft, bottomOfList);
	}
} 

ElementList.prototype.openFullList = function(ev, o) {
	if (o.fullList == null) {
		o.createFullList();
		o.drawFullList();
		o.destroySearch();
	} else {
		o.destroyFullList();
	}
}

ElementList.prototype.drawFullList = function() {
	this.fullList.innerHTML = "";
	
	var list = document.createElement("div");
	list.id = this.name+"_select";
	list.name = list.id;
	var i = 0;
    for (i = 0; i < this.elements.length; i++) {
		var e = document.createElement("div");
		e.className = this.elementClass;
		e.id = this.paramName+"_"+this.elements[i].eid+"_elementFL";
		e.innerHTML = this.elements[i].label;
		e.style.display = "block";
		list.appendChild(e);
		var o = this;
		e.onclick = function(ev) { return o.selectElement(ev, o, o.elements, o.fullList, "_elementFL"); };
		e.onmouseover = function(ev) { return o.highlightElement(ev, o, o.elements, o.fullList, "_elementFL", "high"); };
		e.onmouseout = function(ev) { return o.highlightElement(ev, o, o.elements, o.fullList, "_elementFL", "low"); };
    }              
	list.style.width = "100%";
	this.fullList.appendChild(list); 
	                               
	var r = findAbsolute(this.fullList);
	var baseH;
	if (typeof(window.innerHeight) == 'number') {
		baseH = window.innerHeight;
		ofY = window.pageYOffset;
	} else {
		baseH = document.documentElement.clientHeight;
		ofY = document.body.scrollTop;
	}
	var bottomOfList = r[1] + this.fullList.clientHeight;
	var bottomOfPage = baseH + ofY;   
	if (bottomOfList > bottomOfPage) {
		window.scrollTo(this.fullList.offsetLeft, bottomOfList);
	}	                                       
}

function findAbsolute(obj) {
	var ofL = 0;
	var ofT = 0;
	var parentIs = obj;
	while(parentIs){
		ofL += parentIs.offsetLeft;
		ofT += parentIs.offsetTop;
		parentIs = parentIs.offsetParent;
	}
	return [ofL, ofT];
}	

ElementList.prototype.filterElements = function() {
	var input = this.input.value;
	this.filteredElements = [];
	var i = 0;
	var r = new RegExp("^"+input, "i");
	for (i = 0; i < this.elements.length; i++) {
		var m = this.elements[i].label.match(r);
		if (m != null) this.filteredElements.push(this.elements[i]);
	}	
}

ElementList.prototype.selectElement = function(ev, o, elements, src, tag) {
	if (ev == null) ev = window.event;
	var target;        
	if (navigator.appName.indexOf("Microsoft") != -1) {
		target = ev.srcElement;
	} else {      
		target = ev.target;
	} 
	var selectedElement = null;  
	var r = new RegExp(o.paramName+"_([^_]+)"+tag);
	var m = target.id.match(r);
	if (m != null) {
		var i = 0;  	
		for (i = 0; i < elements.length; i++) {
			if (elements[i].eid == m[1]) selectedElement = i;
		}
	}
	if (selectedElement != null) {
		o.input.value = elements[selectedElement].label;
		o.hidden.value = elements[selectedElement].eid;
		if (o.search != null) o.destroySearch();
		if (o.fullList != null) o.destroyFullList();
	}	
		
}

ElementList.prototype.highlightElement = function(ev, o, elements, src, tag, mode) {
	if (ev == null) ev = window.event;
	var target;        
	if (navigator.appName.indexOf("Microsoft") != -1) {
		target = ev.srcElement;
	} else {      
		target = ev.target;
	} 
	var selectedElement = null;  
	var r = new RegExp(o.paramName+"_([^_]+)"+tag);
	var m = target.id.match(r);
	if (m != null) {
		var i = 0;  	
		for (i = 0; i < elements.length; i++) {
			if (elements[i].eid == m[1]) selectedElement = i;
		}
	}
	if (selectedElement != null) {
		if (mode == "high") {
			document.getElementById(o.paramName+"_"+elements[selectedElement].eid+tag).style.backgroundColor = "#E6E6E6";
		} else if (mode == "low") {
			document.getElementById(o.paramName+"_"+elements[selectedElement].eid+tag).style.backgroundColor = "#FFFFFF";
		}
	}	
		
} 

/*  Cascade question code to support questions which affect subsequent questions in the survey */
/*  by save and reloading question changes on the fly */

function UpdateableQuestions() {
	this.questions = new Array();
}                                

function UpdateableQuestion(cid, orderVal, src, type) {
	this.cid = cid;
	this.orderVal = orderVal;
	this.src = src;
	this.obj = document.getElementById(src);
	this.peids = new Array();
	this.options = new Array();
	this.type = type;
	
	var o = this;
	var obj = document.getElementById(src);
	obj.onchange = function (e) { o.apply(e, o); }; 
	var i;
	if (this.type == "dropdown") {
		for (i = 0; i < obj.options.length; i++) {
			this.options.push(new Option(obj.options[i].text, obj.options[i].value, false, false));
		}
	} else if (this.type == "radio") {
		
	}
}

function CascadeInfo(cid, src) {
	this.ceids = new Array();
	this.cid = cid; 
	this.src = src;
}

UpdateableQuestion.prototype.apply = function(e, o) { 
	applyQuestion(o, false);
}   	             

function applyQuestion(o) {
	if (updateableQuestions.questions[o.cid].peids.length < 1) return;    
	var i;  
    
	var x;
	for (x in updateableQuestions.questions[o.cid].peids[o.obj.value]) {
		var oo = document.getElementById(updateableQuestions.questions[o.cid].peids[o.obj.value][x].src);
		oo.innerHTML = "";
		if (updateableQuestions.questions[updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid].options[0].value == "") {              
			if (navigator.userAgent.indexOf("MSIE") != -1) {
				oo.add(new Option("Select", ""));
			} else {
				oo.add(new Option("Select", ""), null);
			}
		}
		for (i = 0; i < updateableQuestions.questions[updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid].options.length; i++) {
			if (updateableQuestions.questions[o.cid].peids[o.obj.value][x].ceids[updateableQuestions.questions[updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid].options[i].value] == 1) {
				if (navigator.userAgent.indexOf("MSIE") != -1) {
					oo.add(updateableQuestions.questions[updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid].options[i]);
				} else {
					oo.add(updateableQuestions.questions[updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid].options[i], null);
				}
			}
		}
		rc1ApplyQuestion(updateableQuestions.questions[o.cid].peids[o.obj.value][x].cid, new Array());
	}      	
}

function rc1ApplyQuestion(p, cl) {
	var x;
	var y;                                           
	for (x in updateableQuestions.questions[p].peids) {
		for (y in updateableQuestions.questions[p].peids[x]) {
			var c = updateableQuestions.questions[p].peids[x][y].cid;
			if (cl[""+c] != 1)	{                                                 
				var oo = document.getElementById(updateableQuestions.questions[c].src);
				var jj = oo.options[0].value = "" ? 1 : 0;
				var j;
				for (j = oo.options.length; j > jj ; j--) {
					oo.remove(1);
				}
				rc1ApplyQuestion(updateableQuestions.questions[p].peids[x][y].cid, cl, x);
				cl[""+c] = 1;
			}
	   	}
	}
}

function applyQuestion2(o, skipSave) {
	dataTable = new Array();
	displayCallback = {
		"updateableQuestions": updateableQuestions,
		"updateableQuestion": o, 
		update: function(){
			if (dataTable.length > 0) {
				if (dataTable[0] != "") {        
					for (x in dataTable) {
						var localDataTable = dataTable[x];
						var index = 0;
						var i = 0;
						if (localDataTable[0] != null) {
							for (i = 0; i < updateableQuestions.questions.length; i++) {
								if (updateableQuestions.questions[i].cid == localDataTable[0][2]) {
									updateableQuestions.questions[i].obj.options.length = 0;
									index = i;
								}
							}
						}
						for (x in localDataTable) { 
							// eid, label, cid, order_val, selected, rid
							var row = localDataTable[x];                             
							var selected = row[4] == "1" ? true : false;
				   		   	updateableQuestions.questions[index].obj.add(new Option(row[1], row[0], false, selected), null);
							if (row[5] != "") document.surveyForm.rid.value = row[5]; 
						}
					}
				}                                     
			}
		} 
	};            
	var p = getParams(document.surveyForm, new Array());
	p = p.replace(/\&command\=/, "&command=apply_question");
	var skipSave = skipSave == true ? "1" : "0";
	var cid = skipSave == false ? "&cid="+o.cid : "";
	var cookie = getCookie("ss_history");
	if (cookie != "") cookie = "&cookie="+cookie;
	//alert(""+"/sarge?submit=1"+p+"&skipSave="+skipSave+cid+cookie);
	getInfo("/sarge?submit=1"+p+"&skipSave="+skipSave+cid+cookie);
} 

function getParams(o, types) {
	var params = new Array();
	var mapping = new Array();
	var j;                 
	if (types.length > 0) {
		for (j = 0; j < types.length; j++) {
			mapping[types[j]] = true;
		}
		for (j = 0; j < o.elements.length; j++) {
			if (mapping[o.elements[j].type]) params.push(o.elements[j].name+"="+o.elements[j].value);
		}
	} else {
		for (j = 0; j < o.elements.length; j++) {
			params.push(o.elements[j].name+"="+o.elements[j].value);
		}
	}
	var params_string = params.join("&");
	params_string = params_string != "" ? "&"+params_string : params_string;
	return params_string;
}

function getCookie(name) {
	var cookies = document.cookie;
	var pos = cookies.indexOf(name+"=");
	if (pos != -1) {
		var start = pos + name.length + 1;
		var end = cookies.indexOf(";", start);
		if (end == -1) end = cookies.length;
		var val = cookies.substring(start, end);
		return decodeURIComponent(val);
	}
} 

