// Droplist controller

var dlStates = new Array();
dlStates[0] = new Image();
dlStates[0].src = "img/dropbtn.gif";
dlStates[1] = new Image();
dlStates[1].src = "img/dropbtn_over.gif";
dlStates[2] = new Image();
dlStates[2].src = "img/dropbtn_pressed.gif";

var dlists = new Array();
var sizey = new Array();
var minx = new Array();
var maxx = new Array();

var drops_enabled = true;

function InitDropList(name) {
	// Registering the droplsit
	// Looking for the droplist if it's present in the registry
	var isReload = false;
	var seekValue = "";
	try {
		var reloadHidden = document.getElementById("hdnPageReload").value.toString();
		if (reloadHidden == "1") {
			isReload = true;
			seekValue = document.getElementById(name+"_hidden").value.toString().toUpperCase();
		}
	} catch(errr) {
	}
	for (var i in dlists) {
		if (document.getElementById(name) == dlists[i]) {
			// Found a match, deleting
			var temparr1 = dlists.slice(0,i-1);
			if (i<dlists.length) {
				var temparr2 = dlists.slice(i+1);
			} else {
				var temparr2 = new Array();
			}
			var combine = temparr2.concat(temparr2);
			dlists = combine;
		}
	}
	dlists.push(document.getElementById(name));
	if (document.getElementById(name).getAttribute("restricted")) {
		sizey.push(parseInt(document.getElementById(name).getAttribute("restricted")));
	} else {
		sizey.push(0);
	}
	if (document.getElementById(name).getAttribute("minx")) {
		minx.push(parseInt(document.getElementById(name).getAttribute("minx")));
	} else {
		minx.push(0);
	}
	if (document.getElementById(name).getAttribute("maxx")) {
		maxx.push(parseInt(document.getElementById(name).getAttribute("maxx")));
	} else {
		maxx.push(0);
	}
	// Setting event handlers
	var dl_btn = document.getElementById(name+"_btn");
	dl_btn.onmouseover = DLMouseOver;
	dl_btn.onmouseout = DLMouseOut;
	dl_btn.onclick = DLClick;
	dl_btn.ondblclick = Dummy;
	var dl_list = document.getElementById(name+"_list");
	if (typeof dl_list.childNodes == "undefined") {
		var dlChildren =  dl_list.children;
	} else {
		var dlChildren =  dl_list.childNodes;
	}
	for (var i=0;i<dlChildren.length;i++) {
		var tempchild = dlChildren[i];
		if (tempchild.tagName && tempchild.tagName.toUpperCase() == "DIV") {
			tempchild.onmouseover = ItemMouseOver;
			tempchild.onmouseout = ItemMouseOut;
			tempchild.onclick = ItemClick;
			if (isReload) {
				if (tempchild.getAttribute("val")!=null && tempchild.getAttribute("val").toString().toUpperCase() == seekValue) {
					tempchild.className = "DropItemSelected";
					tempchild.setAttribute("active","1");
					try {
						document.getElementById(name+"_value").value = strippedHTML(tempchild.innerHTML);
					} catch(eee) {}
					try {
						document.getElementById(name+"_hidden").value = tempchild.getAttribute("val").toString();
					} catch(eee) {}
				} else if (tempchild.getAttribute("val")==null && strippedHTML(tempchild.innerHTML).toString().toUpperCase() == seekValue) {
					tempchild.className = "DropItemSelected";
					tempchild.setAttribute("active","1");
					try {
						document.getElementById(name+"_value").value = strippedHTML(tempchild.innerHTML);
					} catch(eee) {}
					try {
						document.getElementById(name+"_hidden").value = tempchild.getAttribute("val").toString();
					} catch(eee) {}
				} else if (tempchild.tagName.toString().toUpperCase() == "DIV") {
					tempchild.className = "DropItem";
					tempchild.setAttribute("active","0");
				}
			}
		}
	}
	document.body.onclick = HideAllDrops;
	window.onresize = HideAllDropsUncond;
	if (document.getElementById(name).getAttribute("defval")!=null) {
		var vval = document.getElementById(name+"_value");
		vval.value = document.getElementById(name).getAttribute("defval").toString();
	}
}

function ClearAllItems(listname) {
	document.getElementById(listname+"_list").innerHTML = "";
	InitDropList(listname);
}

function DLMouseOver(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	if (source.getAttribute("active") != "1") {
		source.src = dlStates[1].src;
	}
}

function DLMouseOut(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	if (source.getAttribute("active") != "1") {
		source.src = dlStates[0].src;
	}
}

function DLClick(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	// Toggle list
	var theList = document.getElementById(source.getAttribute("mylist")+"_list");
	// Checking items
	var hasItems = (theList.childNodes.length>0);
	if (hasItems && drops_enabled) {
		if (theList.style.display != "block") {
			HideExplicit(theList.id);
			ShowList(theList.id);
			source.src = dlStates[2].src;
			source.setAttribute("active","1");
		} else {
			HideList(theList.id);
			source.src = dlStates[0].src;
			source.setAttribute("active","0");
		}
	}
}

function ItemMouseOver(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	if (source.getAttribute("active") != "1") {
		source.className = "DropItemHover";
	}
}

function ItemMouseOut(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	if (source.getAttribute("active") != "1") {
		source.className = "DropItem";
	}
}

function ItemClick(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	// Mark
	// set txtCreditCardNumber --> focus
	var mySelected = document.getElementById(source.offsetParent.getAttribute("mySelected"))
	if(mySelected != null)
	{
		mySelected.focus();
	}
	
	var mylist = document.getElementById(source.offsetParent.getAttribute("mylist")+"_list")
	if (typeof mylist.childNodes == "undefined") {
		var dlChildren =  mylist.children;
	} else {
		var dlChildren =  mylist.childNodes;
	}
	for (var i=0;i<dlChildren.length;i++) {
		var tempchild = dlChildren[i];
		if (tempchild.tagName && tempchild.tagName.toUpperCase() == "DIV") {
			tempchild.setAttribute("active",((tempchild == source)?"1":"0"));
			tempchild.className = ((tempchild == source)?"DropItemSelected":"DropItem");
		}
	}
	// Copy value to input
	var myval = document.getElementById(source.offsetParent.getAttribute("mylist")+"_value");
	if (myval.tagName.toUpperCase() == "INPUT") {
		myval.value = strippedHTML(source.innerHTML);
	} else {
		myval.innerHTML = source.innerHTML;
	}
	var myhide = document.getElementById(source.offsetParent.getAttribute("mylist")+"_hidden");
	
	if (myhide != null && typeof myhide != "undefined") {
		myhide.value = source.getAttribute("val");
	}
	// Close droplist
	var mybtn = document.getElementById(source.offsetParent.getAttribute("mylist")+"_btn");
	mybtn.src = dlStates[0].src;
	HideList(source.offsetParent.getAttribute("mylist")+"_list");
	// Check specials
	if (source.getAttribute("special")) {
		// Has special function
		switch (source.getAttribute("special")) {
			case "manualevent":
				window.setTimeout(source.getAttribute("eventfunc") + "", 1);
				break;
			case "ajax":
				window.setTimeout(source.getAttribute("ajaxfunc") + "", 1);
				break;
			case "1":
				// Step 2 - show states
				document.getElementById("statelabel").style.display = "inline";
				document.getElementById("state").style.display = "block";
				break;
			case "0":
				// Step 2 - hide states
				document.getElementById("statelabel").style.display = "none";
				document.getElementById("state").style.display = "none";
				break;
		}
	}
}

function strippedHTML(source) {
	var tempstr = "";
	var rx = /&amp;/;
	tempstr = source.replace(rx,"&");
	rx = /&nbsp;/;
	tempstr = tempstr.replace(rx," ");
	rx = /<br>/;
	tempstr = tempstr.replace(rx," ");
	rx = /<br \/>/;
	tempstr = tempstr.replace(rx," ");
	return tempstr;
}

function ShowList(which) {
	var list = document.getElementById(which);
	list.style.display = "block";
	PositionList(list);
}

function HideList(which) {
	var list = document.getElementById(which);
	list.style.display = "none";
}

function getSizeRestriction(name) {
	for (var i=0;i<dlists.length;i++) {
		if (dlists[i] == document.getElementById(name)) {
			// Found
			return sizey[i];
		}
	}
	return 0;
}

function getMinimumX(name) {
	for (var i=0;i<dlists.length;i++) {
		if (dlists[i] == document.getElementById(name)) {
			// Found
			return minx[i];
		}
	}
	return 0;
}

function getMaximumX(name) {
	for (var i=0;i<dlists.length;i++) {
		if (dlists[i] == document.getElementById(name)) {
			// Found
			return maxx[i];
		}
	}
	return 0;
}

function getTotalHeight(dlname) {
	var mylist = document.getElementById(dlname+"_list");
	if (typeof mylist.childNodes == "undefined") {
		var dlChildren =  mylist.children;
	} else {
		var dlChildren =  mylist.childNodes;
	}
	var result = 0;
	for (var i=0;i<dlChildren.length;i++) {
		var tempchild = dlChildren[i];
		if (tempchild.tagName && tempchild.tagName.toUpperCase() == "DIV") {
			result += tempchild.offsetHeight+1;
		}
	}
	return result;
}

function getMaxWidth(dlname) {
	var mylist = document.getElementById(dlname+"_list");
	if (typeof mylist.childNodes == "undefined") {
		var dlChildren =  mylist.children;
	} else {
		var dlChildren =  mylist.childNodes;
	}
	var result = 0;
	for (var i=0;i<dlChildren.length;i++) {
		var tempchild = dlChildren[i];
		if (tempchild.tagName && tempchild.tagName.toUpperCase() == "DIV") {
			if (tempchild.offsetWidth > result) {
				result = tempchild.offsetWidth;
			}
		}
	}
	return result;
}

function PositionList(o) {
	var staticHolder = document.getElementById(o.getAttribute("mylist"));
	if (typeof staticHolder.getAttribute("correction") != "undefined" && staticHolder.getAttribute("correction")!=null) {
		if (YKM.browserType() == "IE") {
			o.style.top = (YKM.getRealY(staticHolder)+12)+"px";
		} else {
			o.style.top = (YKM.getRealY(staticHolder)+22)+"px";
		}
	} else {
		o.style.top = (YKM.getRealY(staticHolder)+22)+"px";
	}
	// Check size
	var sr = getSizeRestriction(o.getAttribute("mylist"));
	if (sr > 0) {
		if (o.offsetHeight >= sr) {
			o.style.height = sr + "px";
		} else {
			o.style.height = (getTotalHeight(o.getAttribute("mylist"))-1) + "px";
		}
	} else {
	}
	// Check X restrictions
	var minXRestriction = getMinimumX(o.getAttribute("mylist"));
	var maxXRestriction = getMaximumX(o.getAttribute("mylist"));
	var maxXWidth = getMaxWidth(o.getAttribute("mylist"));
	if (maxXRestriction>0) {
		// Maximum restricted
		if (minXRestriction>0) {
			// Minimum restricted
			if (maxXWidth<minXRestriction) {
				// Top length less than minimal width
				o.style.width = minXRestriction + "px";
			} else if (maxXWidth>minXRestriction && maxXWidth<maxXRestriction) {
				// Top length between minimal and maximum
				o.style.width = maxXWidth + "px";
			} else {
				// Top length more than maximum
				o.style.width = maxXRestriction + "px";
			}
		}
	} else {
		// Maximum not restricted
		if (minXRestriction>0) {
			// Minimum restricted
			if (maxXWidth<minXRestriction) {
				// Top length less than minimum
				o.style.width = minXRestriction + "px";
			} else {
				o.style.width = maxXWidth + "px";
			}
		} else {
			// No restrictions at all
			o.style.width = maxXWidth + "px";
		}
	}
	o.style.left = (YKM.getRealX(staticHolder) - (o.offsetWidth - staticHolder.offsetWidth))+"px";
	try {
		o.style.overflowX = "hidden";
		o.style.overflowY = "auto";
	} catch(er) {
		o.style.overflow = "auto";
	}
}

function HideAllDrops(e) {
	if (typeof e == "undefined" || e == null) {
		var source = event.srcElement;
	} else {
		var source = e.target;
	}
	var valid = true;
	for (var i=0;i<dlists.length;i++) {
		if (YKM.isChildOf(source,dlists[i]) || YKM.isChildOf(source,document.getElementById(dlists[i].id+"_list"))) {
			valid = false;
			break;
		}
	}
	if (valid) {
		for (var i=0;i<dlists.length;i++) {
			document.getElementById(dlists[i].id+"_list").style.display = "none";
			document.getElementById(dlists[i].id+"_btn").src = dlStates[0].src;
		}
	}
	if (typeof _calHideAllCals != "undefined") {
		_calHideAllCals(source);
	}
}

function HideAllDropsUncond() {
	for (var i=0;i<dlists.length;i++) {
		document.getElementById(dlists[i].id+"_list").style.display = "none";
		document.getElementById(dlists[i].id+"_btn").src = dlStates[0].src;
	}
}

function HideExplicit(exception) {
	var ex = document.getElementById(exception);
	for (var i=0;i<dlists.length;i++) {
		if (document.getElementById(dlists[i].id) != ex) {
			document.getElementById(dlists[i].id+"_list").style.display = "none";
			document.getElementById(dlists[i].id+"_btn").src = dlStates[0].src;
		}
	}
}

function _compileValues(dlname) {
	var l = document.getElementById(dlname+"_list");
	var newval = "";
	if (l.childNodes.length > 0) {
		for (var i=0;i<l.childNodes.length;i++) {
			var thisitem = l.childNodes[i];
			if (thisitem.getAttribute("all")!="1") {
				if (newval == "") {
					newval = thisitem.getAttribute("val").toString();
				} else {
					newval += ","+thisitem.getAttribute("val").toString();
				}
			}
		}
	}
	return newval;
}


function _onLoadSelect(listname,value) {
	if (value != "0") {
		if (listname.indexOf("_")>-1) {
			var ln = listname.substr(0,listname.indexOf("_"));
		} else {
			var ln = listname;
		}
		var theList = document.getElementById(ln+"_list");
		var theBox = document.getElementById(ln+"_value");
		// Look for value
		theBox.value = value.toString();
		for (var i=0;i<theList.childNodes.length;i++) {
			if (theList.childNodes[i].innerHTML == value.toString()) {
				// Found the bastard
				theList.childNodes[i].className = "DropItemSelected";
				theList.childNodes[i].setAttribute("active","1");
			}
		}
	}
}



