var helpbox = null;

if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(val, fromIndex) {
		if (typeof(fromIndex) != 'number') fromIndex = 0;
		for (var index = fromIndex,len = this.length; index < len; index++)
			if (this[index] == val) return index;
		return -1;
	}
}

function showhelp(name) {
	if (helpbox != null) helpbox.style.display = 'none';
	helpbox = document.getElementById(name);
	helpbox.style.display = 'block';
}

function nobubble(event) {
	if (window.event) {
		window.event.cancelBubble = true;
	} else {
		event.stopPropagation();
	}
}

function getKeycode(event) {
	if (window.event) {
		return event.keyCode;
	} else if (event.which) {
		return event.which;
	}
}

function getEvent(event) {
	if (window.event) return window.event;
	else return event;
}

function comboboxKeypress(textbox, list, event) {
	var keynum = getKeycode(event);
	if (keynum == 13) {
		return false;
	}
	return true;
}

function comboboxKeyup(textbox, list, event) {
	var keynum = getKeycode(event);
	event = getEvent(event);

	if (keynum == 13) {
		textbox.value = list.options[list.selectedIndex].text;
		list.parentNode.style.display = 'none';
		return false;
	} else if (keynum == 40) {
		if ((list.selectedIndex + 1) < list.options.length) {
			list.options[list.selectedIndex + 1].selected = true;
		}
	} else if (keynum == 38) {
		if (list.selectedIndex > 0) {
			list.options[list.selectedIndex - 1].selected = true;
		}
	} else {
		selectMatch(list,textbox.value);
		list.parentNode.style.display = 'block';
	}
}

function selectMatch(list, text, topoffset, bottomoffset) {
	if (text == '') return;
	
	text = text.toUpperCase();
	if (topoffset == null) topoffset = 0;
	if (bottomoffset == null) bottomoffset = list.options.length - 1;

	offset = topoffset + Math.floor((bottomoffset - topoffset) / 2)

	//console.log(list.options[topoffset].text + '(' + topoffset + ') < ' + list.options[offset].text + ' (' + offset + ')' + ' > ' + list.options[bottomoffset].text + '(' + bottomoffset + ')');

	if (list.options[offset].text.substring(0,text.length).toUpperCase() == text) {
		while ((offset >= 0) && (list.options[offset].text.substring(0,text.length).toUpperCase() == text)) {
			offset--;
		}
		list.options[offset + 1].selected = true;
	} else {
		oldtop = topoffset;
		if (list.options[offset].text > text) {
			bottomoffset = offset;
		} else {
			topoffset = offset;
		}
		if ((topoffset >= bottomoffset) || (oldtop == offset)) {
			return false;
		} else selectMatch(list, text, topoffset, bottomoffset);
	}
}

function returnObjById( id ) {
	if (document.getElementById)
		var returnVar = document.getElementById(id);
	else if (document.all)
		var returnVar = document.all[id];
	else if (document.layers)
		var returnVar = document.layers[id];
	return returnVar;
}

function swapVisibility( from, to, inline ) {
	if (document.getElementById('thedealedit') != null) {
		if ((from != 'thedealedit') && (to != 'thedealedit') && (document.getElementById('thedealedit').style.display == 'block')) {
			if (document.getElementById('chkDealAvailable')) updateTheDeal();
			else updateMyDeal();
		}
	}
	if (document.getElementById('theroomedit') != null) {
		if ((from != 'theroomedit') && (to != 'theroomedit') && (document.getElementById('theroomedit').style.display == 'block')) {
			updateTheRoom();
		}
	}
	if (document.getElementById('preferencesedit') != null) {
		if ((from != 'preferencesedit') && (to != 'preferencesedit') && (document.getElementById('preferencesedit').style.display == 'block')) {
			updatePreferences();
		}
	}
	if (document.getElementById('aboutmeedit') != null) {
		if ((from != 'aboutmeedit') && (to != 'aboutmeedit') && (document.getElementById('aboutmeedit').style.display == 'block')) {
			updateAboutMe();
		}
	}

	if (typeof(validateListing) != 'undefined') validateListing();
	try {
		if (inline == true) {
			returnObjById( to ).style.display   = "inline";
		} else {
			returnObjById( to ).style.display   = "block";
		}
		returnObjById( from ).style.display = "none";
	} catch(e) { }
	try {
		returnObjById( to + "data" ).focus();
	} catch(e) { }
}

function populateDropbox(dropbox, array, init) {
	dropbox.innerHTML = '';
	var opt = document.createElement('option');
	opt.value = '';
	opt.text = init;
	try {
		dropbox.add(opt, null); // standards compliant; doesn't work in IE
	} catch(ex) {
		dropbox.add(opt); // IE only
	}
	for (var key in array) {
		if (isIntVal(key)) {
			var opt = document.createElement('option');
			opt.value = key;
			if (array[key]['Name'] != null) {
				opt.text = array[key]['Name'];
			} else {
				opt.text = array[key];
			}
			try {
				dropbox.add(opt, null); // standards compliant; doesn't work in IE
			} catch(ex) {
				dropbox.add(opt); // IE only
			}
		}
	}
}

function setDropboxValue(widg, val) {
	for (var i = 0; i < widg.options.length; i++) {
		if (widg.options[i].value == val) {
			widg.options[i].selected="selected";
			break;
		}
	}
}
