/* Common functions */
function promptLink(message, url){
	if(confirm(message)){
		location.href = url;
	}
}
function changeSSL(){
	var formAction = document.login.action;
	if(document.login.loginSSL.checked == true){
		document.login.action = formAction.replace("http://", "https://");
	}else{
		document.login.action = formAction.replace("https://", "http://");
	}
}
function checkRegsiterType(){
	var i, registerType = document.getElementsByName('registerType');
	for(i = 0; i < registerType.length; i++){
		if(registerType[i].checked){
			var registerValue = registerType[i].value;
		}
	}
	var trTags = document.getElementsByTagName('tr');
	for(i = 0; i < trTags.length; i++){
		if(trTags[i].getAttribute('rel')){
			if(registerValue !== trTags[i].getAttribute('rel')){
				trTags[i].style.display = 'none';
			}else{
				trTags[i].style.display = '';
			}
		}
	}
	if(document.getElementById('policy')){
		setPolicyHeight();
	}
}
function checkRegisterPolicy(){
	var registerAccept = document.getElementsByName('registerAccept');
	var buttonDisabled = true;
	if(registerAccept.length == 1 && registerAccept[0].checked == true){
		buttonDisabled = false;
	}
	document.getElementById('registerSubmit').disabled = buttonDisabled;
}
function checkEditionId(){
	var i, editionId = document.getElementsByName('editionId');
	var buttonDisabled = true;
	for(i = 0; i < editionId.length; i++){
		if(editionId[i].checked){
			buttonDisabled = false;
		}
	}
	document.getElementById('editionSubmit').disabled = buttonDisabled;
}
function setPolicyHeight(){
	if(document.getElementById('formLarge')){
		document.getElementById('policy').style.height = document.getElementById('formLarge').offsetHeight + 'px';
	}else{
		document.getElementById('policy').style.height = '300px';
	}
}
function setRegsiterMain(){
	checkRegsiterType();
	checkRegisterPolicy();
}
/* Rollover images */
function swapImageRestore(){
	var i, x, a = document.sr;
	for(i=0; a && i < a.length && (x = a[i]) && x.oSrc; i++){
		x.src = x.oSrc;
	}
}
function preloadImages(){
	var d = document; 
	if(d.images){
		if(!d.p){
			d.p = new Array();
		}
	}
    var i, j = d.p.length, a = preloadImages.arguments;
	for(i = 0; i < a.length; i++){
		if(a[i].indexOf("#") != 0){
			d.p[j] = new Image;
			d.p[j++].src = a[i];
		}
	}
}
function findObject(n, d){
	var p, i, x;
	if(!d){
		d = document;
	}
	if((p = n.indexOf("?")) > 0 && parent.frames.length){
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if(!(x = d[n]) && d.all){
		x = d.all[n];
	}
	for(i = 0; !x && i < d.forms.length; i++){
		x = d.forms[i][n];
	}
	for(i = 0; !x && d.layers && i < d.layers.length; i++){
		x = findObject(n, d.layers[i].document);
	}
	if(!x && d.getElementById){
		x = d.getElementById(n);
	}
	return x;
}
function swapImage(){
	var i, j = 0, x, a = swapImage.arguments;
	document.sr = new Array; 
	for(i = 0; i < (a.length - 2); i += 3){
		if((x = findObject(a[i])) != null){
			document.sr[j++] = x;
			if(!x.oSrc){
				x.oSrc = x.src;
			}
			x.src = a[i+2];
		}
	}
}
function sectionToggle(section){
	var divTags = document.getElementsByTagName('div');
	for(i = 0; i < divTags.length; i++){
		if(divTags[i].getAttribute('rel') == 'dynamic'){
			divTags[i].style.display = 'none';
		}
	}
	document.getElementById(section).style.display = '';
}
function nodeToggle(current){
	var ulTags = document.getElementsByTagName('ul');
	for(i = 0; i < ulTags.length; i++){
		if(ulTags[i].getAttribute('rel')){
			if(ulTags[i].getAttribute('rel') == "node_" + current){
				ulTags[i].style.display = '';
			}else{
				ulTags[i].style.display = 'none';
			}
		}
	}
}
/* Combo box */
function addEvent(element, handler, callback, capture){
	if (window.addEventListener) element.addEventListener(handler, callback, capture);
	else element.attachEvent('on' + handler, callback);
}
function getOffsetElement(element){
	var top = left = 0;
	while(element){
		top += element.offsetTop;
		left += element.offsetLeft;
		element = element.offsetParent;
	}
	var offset = [left, top];
	return offset;
}
var currentComboBox = null;
var flagComboBox = false;
var drop_downs = [];
function moveSelected(sender, index){
	if (sender.className == 'selected') return;
	currentComboBox.assoc_dropdown.getElementsByTagName('li')[currentComboBox.currentSelected].className = '';
	sender.className = 'selected';
	currentComboBox.currentSelected = index;
}
function changeSelected(index, text, value){
	if (index == currentComboBox.selectedIndex) return;
	currentComboBox.selectedIndex = index;
	updateTextValue(text, value);
	if (currentComboBox.listener) currentComboBox.listener.call(this, currentComboBox);
}
function showComboBox(combo_dom){
	if (combo_dom != currentComboBox) hideComboBox();
	currentComboBox = combo_dom;
	var combo = currentComboBox.assoc_dropdown;
		moveSelected(combo.getElementsByTagName('li')[combo_dom.selectedIndex], combo_dom.selectedIndex);
		combo.style.display = (combo.style.display != 'block') ? 'block' : 'none';
		flagComboBox = false;
}
function hideComboBox(){
	if (!currentComboBox || !flagComboBox){
		flagComboBox = true;
		return;
	}
	currentComboBox.assoc_dropdown.style.display = 'none';
	currentComboBox = null;
}
function updateTextValue(text, value){
	var obj = currentComboBox.getElementsByTagName('input');
	currentComboBox.value = value;
	for (var i = 0; i < arguments.length; i++){
		obj[i].value = arguments[i];
	}
}
function initializedDropDowns(){
	for (var i = 0; i < drop_downs.length; i++){
		drop_downs[i].style.width = (drop_downs[i].parent_field.offsetWidth - 2) + 'px';
		document.body.appendChild(drop_downs[i]);
	}
}
function positionAllDropDown(){
	for (var i = 0; i < drop_downs.length; i++){
		positionDropDown(drop_downs[i]);
	}
}
function positionDropDown(drop_down){
	var offset = getOffsetElement(drop_down.parent_field);
	drop_down.style.top = offset[1] + drop_down.parent_field.offsetHeight + 1 + 'px';
	drop_down.style.left = offset[0] + 'px';
}
function OptionItem(text, value, selected){
	this.text = text;
	this.value = (!value) ? this.text : value;
	this.selected = selected;
}
function ComboBox(name){
	this.name = name;
	this.options = new Array();
	this.selectedIndex = 0;
	this.onchange_listener = null;
}
ComboBox.prototype.addOption = function(Option){
	this.options.push(Option);
	if (Option.selected) this.selectedIndex = this.options.length - 1;
}
ComboBox.prototype.removeOption = function(index){
	this.options.splice(index, 1);
}
ComboBox.prototype.setOnChangeListener = function(callback){
	if (!(callback instanceof Function)){
		throw new Error('Arguments must be Function!!!');
	}
	this.onchange_listener = callback;
}
ComboBox.prototype.renderComboBox = function(id_element){
	var selectedElement = this.options[this.selectedIndex];
	var combo_root = document.createElement('div');
		combo_root.className = 'comboBox';
		combo_root.selectedIndex = this.selectedIndex;
		combo_root.currentSelected = this.selectedIndex;
		combo_root.value = this.options[this.selectedIndex].value;
		combo_root.listener = this.onchange_listener;
		combo_root.onclick = function(){showComboBox(this);};
		combo_root.innerHTML = '<input type="text" readonly="readonly" value="' + selectedElement.text + '"><input type="hidden" name="' + this.name + '" value="' + selectedElement.value + '">';
		document.getElementById(id_element).appendChild(combo_root);
	var assoc_dropdown = document.createElement('div');
		assoc_dropdown.className = 'dropDown';
		assoc_dropdown.parent_field = combo_root;
		assoc_dropdown.style.position = 'absolute';
		assoc_dropdown.style.zIndex = Number.MAX_VALUE;
		assoc_dropdown.style.display = 'none';
	var htmlCode = '<ul>';
	for (var i = 0, len = this.options.length; i < len; i++){
		htmlCode += '<li onclick="changeSelected(' + i + ',\'' + this.options[i].text + '\',\'' + this.options[i].value + '\')" onmouseover="moveSelected(this, '+ i +')">' + this.options[i].text + '</li>';
	}
	assoc_dropdown.innerHTML = htmlCode;
	combo_root.assoc_dropdown = assoc_dropdown;
	drop_downs.push(assoc_dropdown);
}
addEvent(document, 'click', hideComboBox, false);
addEvent(window, 'resize', positionAllDropDown, false);
addEvent(window, 'load', initializedDropDowns, false);
addEvent(window, 'load', positionAllDropDown, false);