//Legends of Elveron java-script file

// this function should be included by all submit buttons on onClick() to prevent
// double submits. It disables the button.
function _submit(o) {
    o.disabled = true;
    if(o.name != null) {
        addField(o.form, o.name, o.value);
    }
    o.form.submit();
}

// this function should be included by all submit buttons on onClick() to prevent
// double submits. It disables the button. Special function for those buttons that
// have mac IE problems and when it doesn't matter which value is set as long as
// the name is submitted.
function _submit_failsafe(o) {
    if (!((navigator.appVersion.indexOf("Mac")!=-1) && (navigator.appVersion.indexOf("MSIE")!=-1))) {
        //Not Macintosh MSIE
        //Mac IE doesn't support the .appendChild in the DOM it seems like
        _submit(o);
    }
}

// this function is used by _submit. If the submit button have a name and a value
// this isn't posted because the submit button is disabled prior to posting, a
// hidden field is dynamically added to the form as a workaround
function addField (form, fieldName, fieldValue) {
    var input = document.createElement('input');
    if (document.all) {
        //IE
        input.type = "hidden";
        input.name = fieldName;
        input.value = fieldValue;
    }
    else if (document.getElementById) { 
        //MOZILLA
        input.setAttribute('type', "hidden");
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
    }
    form.appendChild(input);
}

// Toggles hidden / visible text mainly used in the FAQ
function toggleVisibility(object) {
    if(object.style.display == "none") 
        object.style.display = 'block'; 
    else 
        object.style.display = 'none';
}

// Toggles bold / normal font weight
function toggleBoldness(object) {
    if(object.style.fontWeight == 'bold') 
        object.style.fontWeight = 'normal'; 
    else 
        object.style.fontWeight = 'bold';
}


//// AJAX FUNCTIONS RELATED TO REALM BOOK DATASERVICE AND OTHER XML DATASERVICES BELOW ////

// populate ops method (in search page)
function populateOps(xmlIds, divTagId) {
	var ids = xmlIds.split(':');
	var divTag = document.getElementById(divTagId);
	var html = '';
	for(var i = 0; i < ids.length; i++) {
		if(ids[i] !== null && ids[i] != '') {
			html += '<center><div id="ew:head:' + ids[i] + '"></div></center><div id="ew:all:' + ids[i] + '"></div><br/><br/>';
		}
	}
	divTag.innerHTML = html;
	documentload();
}

// function to be included in a body's onLoad method when dynamic content should be loaded
function documentload() {
	var HOSTS = Array('/elveron/game/dataservice.jsp', '/elveron-jsp/game/dataservice.jsp', 'http://www.elveron.com/elveron/game/dataservice.jsp');
	var divTags = document.getElementsByTagName('div');
	for(var i = 0; i < divTags.length; i++) {
		if((divTags[i] !== null) && (divTags[i].id !== null)) {
			var divSplit = divTags[i].id.split(':');
			if(divSplit[0] == 'ew') {
				_loadDivTagFromDataService(divTags[i], HOSTS, 0);
			}
		}
	}
}

// internal helper function for documentload()
function _loadDivTagFromDataService(divTag, HOSTS, pos) {
	var divSplit = divTag.id.split(':');
	new Ajax.Request(HOSTS[pos], 
	{
    	method: 'post',
    	parameters: 'xmlid=' + divSplit[2],
    	onSuccess: function(transport) {
    		var elveronXML = transport.responseXML.documentElement;
    		if(divSplit[1] == 'all') {
    			var type = xmlGetHeaderField(elveronXML, 'type') - 0;
    			var tableArr = Array('', 'topbar', 'militarytable', 'militarytable', 'improvetable', 'buildtable', 'landtable', 'maintable', 'maintable', 'magictable', 'productiontable');
    			divSplit[1] = tableArr[type];
    		}
    		eval('ew_' + divSplit[1] + '(elveronXML, divTag)');
    	},
    	onFailure: function() {
    		pos++;
    		if(pos == HOSTS.length) {
    			divTag.innerHTML = 'communication failed with dataservice';
    		} else {
    			_loadDivTagFromDataService(divTag, HOSTS, pos);
    		}
    	}
  	});
}

// add 1000-separators to number
function numberformat(num) {
	num = '' + num;
	var REGEXP = new RegExp('(-?[0-9]+)([0-9]{3})');
	while(REGEXP.test(num)) {
		num = num.replace(REGEXP, '$1,$2');
	}
	return num; 
}

// format a number to 2 decimal points
function decimalformat(num) {
	if(num.toFixed) {	// IE below 5.5 does not support toFixed
		return num.toFixed(2);
	}	
	return Math.round((num - 0) * 100) / 100;
}

// retrieve a header value from an elveron xml document
function xmlGetHeaderField(xmlDom, fieldName) {
	return xmlDom.getElementsByTagName('head')[0].getElementsByTagName(fieldName)[0].firstChild.nodeValue;
}


