function invertInt(myint) {
	if (myint > 0) {
		var newint = parseInt(myint) - (parseInt(myint) * 2);
	} else
	if (myint < 0) {
		var newint = Math.abs(parseInt(myint));
	}
	
	return newint;
}

function gettime() {
	new Ajax.Request('time.php?tz='+my_time_zone, {
		asynchronous: true,
		method: 'get',
		onComplete: function(response) {
			document.getElementById('time').innerHTML = response.responseText;
		}
	});
}

function isset(varname) {
	return(typeof(window[varname])!= 'undefined');
}

function arrowhover(obj) {
	obj.className = 'arrownopadding';
}

function arrowout(obj) {
	obj.className = '';
}

function dialog(url) {
	Control.Modal.open('<center>Are you sure?<br /><input type="button" class="button" value="Yes" onclick="Control.Modal.close; return true;" />&nbsp;&nbsp;<input type="button" class="button" value="No" onclick="Control.Modal.close; return false;" />');
}

function tooltip(id) {
	var tooltips = new Array();
	
	tooltips["skills"] 		= 'Skills listed in bold are major skills that will easily outlevel your other skills. Red skills are Combat skills, blue skills are Magic skills, and green skills are Agility skills.';
	tooltips["stats"] 		= 'These stats contribute to your character\'s abilities in battle. For a breakdown of each stat, check the Wiki.';
	tooltips["techlearn"]	= 'You may double-click on a tech icon to see what that tech does. Drag icons to the spellset boxes below and click Save Changes to use the techs in battle.';
	tooltips["travel"]		= 'Be careful while exploring - you may not always reach your destination unscathed.';
    tooltips["equip"]       = 'Drop items here to auto-equip them.';
    tooltips["unequip"]     = 'Drop items here to unequip them.';
    tooltips["trash"]       = 'Drop items here to discard them.';
    tooltips["flee"]		= '<strong>Flee</strong><br />Try to escape from battle. Your success rate is based on your level vs. your opponent\'s level.';
	tooltips["attack"]		= '<strong>Attack</strong><br />Attempt to attack your opponent with your equipped weapon.';
	tooltips["quests"]		= '<strong>Quest Objectives</strong><br />Hold your mouse over an objective to see details about that objective, including description and current progress';
	tooltips["synthesis"]	= 'Items represented with a red box are items that you cannot purchase at this time.';
	tooltips["mail"]		= 'Blue envelopes represent unread mail. Grey envelopes are messages you have already read. On the Sent Mail page, the color represents whether the recipient has read the message or not.';
	tooltips["spoils"]		= '<strong>Spoils</strong><br />Double click on item icons in this box, then select Loot This Item to take the item. Gold is added to your character automatically.';
	tooltips["useitem"]		= 'Brings up a list of items that can be used in battle. Unless the item is marked as Persistent, it will be discarded once used.';
	
	return tooltips[id];
}

function redirect(url) {
	setTimeout("window.location = '" + url + "';", 4000);
}

function message(text) {
	var node = document.getElementById('msg');
	
	node.innerHTML = '';
	
	var imgnode = document.createElement('img');
	
	imgnode.setAttribute('src', 'images/misc/msg.gif');
	
	node.appendChild(imgnode);
	
	var textnode = document.createTextNode(' ' + text);
	
	node.appendChild(textnode);
}
		
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

var IeFixes = {
    getElementsBTN: function(element, tagname) {
    	nodes 		= new Array();
        element 	= $(element);
        children 	= element.getElementsByTagName('*');
        
        for (i = 0; i < children.length; i++) {
        	var elem = children[i];
    		if (elem.tagName == tagname) {
    			nodes.push(elem);
    		}
        };
        
        return nodes;
    }
}

Element.addMethods(IeFixes);

function useitem(itemid) {
	window.location.replace('pg.php?p=use&itemid='+itemid);
}

function showdiv(div) {
	$(div).style.display = 'block';
}

function hidediv(div) {
	$(div).style.display = 'none';
}

function togglediv(divid, bgid) {
	div = document.getElementById(divid);
	
	if (div.style.display == 'none') {
		showdiv(divid);
		if (bgid) {
			$(bgid).style.backgroundImage = "url('images/misc/expanded.gif');";
			$(bgid).style.backgroundRepeat = "no-repeat;";
		}
	} else {
		hidediv(divid);
		if (bgid) {
			$(bgid).style.backgroundImage = "url('images/misc/collapsed.gif');";
			$(bgid).style.backgroundRepeat = "no-repeat;";
		}
	}
}

function isArray(testObject) {   
	return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function modalredirect(text, url) {
	
	var div = "<div align='center'>";
	
	if (typeof text == undefined) text = "You are being redirected, please wait.";
	
	var img = "<img src=\"images/misc/loading.gif\" style=\"float: left;\" />&nbsp;";
	
	var output = div + img + text + "<br />Click <a class='tinyfont' href='" + url + "'>here</a> if you are not redirected in 5 seconds.</div>";
	
	Control.Modal.open(output, {
		overlayCloseOnClick: false,
		imageCloseOnClick: false
	});
	
	setTimeout("window.location = '" + url + "'", 5000);
	
}

function modal(text) {
	Control.Modal.open(text, {
		width: 300
	});
}

function closemodal() {
	Control.Modal.close();
}

function manual(article) {
	Control.Modal.open("<div onclick=\"closemodal();\" style=\"cursor: pointer; float: right;\"><strong>Close This</strong></div><iframe src='http://www.herorpg.net/manual/"+article+"' frameborder='0' width='700' height='500'></iframe>" , {
		width: 720,
		height: 520
	});
}

function toggleReferrals() {
	new Ajax.Request('includes/ajax_control.php?do=togglereferrals', {
		asynchronous: true,
		method: 'get',
		onComplete: function(response) {
			document.getElementById('referralbutton').value = response.responseText;
		}
	});	
}

function set_cookie(name, value, expires) {
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

function delete_cookie(name) {
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

function get_cookie(name) {
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length) {
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1) {
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0) {
			break;
		}
	}
	
	return false;
}