/*
 * Rybazoom.com, 12/10/2007
 * ----
 * Default js file in every page... one of the first created, doesnt do alot
 * besides some focus highlighting
 *
 * Nathan Reed (c) 2008
 */
var SIGN_IN_FORM = 'signinform';
var SIGN_IN_BT = 'signinButton'
var SIGNED_IN = 'signedInName'
var UN_BOX = 'unInput';

var MAIN_PAGE_URL = './hosts.php';

var loggedIn = false;
var glAlertIndex = -1;

function init() 
{
	if(glAlertIndex != -1) {
		// this is such a bad way to do things, but is the only quick way to 
		// set the alert box to already be selecting the current. see the comments
		// in the php
		$('d-alert-in').value = glAlertIndex;
	}
	
}

function showSignIn()
{
	
	$(SIGN_IN_BT).hide();
	MyFadeUtil.fadein($(SIGN_IN_FORM), 20);
		
	// using prototype.js now
	//document.getElementById(SIGN_IN_FORM).style.display = 'inline';
	//document.getElementById(SIGN_IN_BT).style.display = 'none';

	// set focus on the username box;
	document.getElementById(UN_BOX).focus();

}

function hideSignIn()
{
	document.getElementById(SIGN_IN_FORM).style.display = 'none';
	document.getElementById(SIGN_IN_BT).style.display = 'inline';
	MyFadeUtil.fadein($(SIGN_IN_BT), 20);
}

// redirects the page to the main host list
function goToHostList()
{
	location.href = MAIN_PAGE_URL;
}

// sets the background color of the form for the active field.
// assumes the thing you want to change it the parent of the
// calling element
function fF(id)
{
	document.getElementById(id).style.backgroundColor = '#FFFFA8';
}

function fB(id)
{
	document.getElementById(id).style.backgroundColor = 'white';
}

function changeTourImage(imagePath, caption)
{
	$('rz-tour-img').src = imagePath;
	$('rz-tour-img-cap').innerHTML = 'Servralert Tour - ' + caption;
}

// all the api files return a standard xml status thing, this function
// checks the xml to see if the call was successful. returns null if it is
// and an error object if it is not ok
function checkReturnXml(xml)
{
	var result = null;

	statusElement = xml.getElementsByTagName('done')[0];
	
	if(typeof(statusElement) != 'undefined') {
		errCode = statusElement.getAttribute('code');
		errMsg  = statusElement.getAttribute('caption');
	

		if(errCode != '200') {
			result = {'code': errCode, 'text':errMsg };	
		}
	
	} else {
		result = {'code': 601, 'text':'Bad Server Response' };	
	}

	return result;
}

// wrapper to get around IE crazyness. Why so non standard for?
function addEvent(object, event, action)
{
	if(window.addEventListener){
		object.addEventListener(event, action, false);
	} else {
		object.attachEvent('on'+event, action);
	}
}


var MyFadeUtil = {

	fadein: function(element, speed) {
		
		// is this the first time the function has been called?
		if(typeof(this.element) == 'undefined') {
			this.element = new Array();
			this.element[0] = element;
		} else {
			this.element[this.element.length] = element;
		}
		
		// make the element visble, make it see through, and start the loop
		if(!isIE()) {

			element.show();
			element.style.opacity = 0;
		
			MyFadeUtil.fadeLoop(this.element.length-1, 0, 1/speed);
		} else {
			// user has ie, so just show the element
			$(element).show();
		}
		
		return element;
	},
	
	fadeLoop: function(index, opacity, inc) {
		var newOp = opacity+inc;
		var element = MyFadeUtil.element[index];
		element.style.opacity = opacity;
		
		
		if(opacity < 1) {
			setTimeout(' MyFadeUtil.fadeLoop('+index+','+ newOp +','+ inc +');', 30)
		}
	}
}

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

// returns true if we are being run in a shithouse browser
function isIE()
{
	var result = true;

	// add event listener is only found in normal browsers, eg firefox, safari, opera,
	// so if it doesnt exist, then the user most likey has ie
	if(window.addEventListener) {
		result = false;
	}

	return result;
}

function hideNetwork(nid)
{
	// toggle the link
	$('rz-tg-network-'+nid).href = 'javascript:showNetwork('+nid+')';
	$('rz-tg-network-'+nid).innerHTML = '+ show hosts';
	
	// hide the hosts table
	$('hostTable'+nid).hide();
	
	// set the cookie, so it is hidden next time
	Cookie.create('rz-hn-'+nid, '1', 28);
}

function showNetwork(nid)
{
	// toggle the link
	$('rz-tg-network-'+nid).href = 'javascript:hideNetwork('+nid+')';
	$('rz-tg-network-'+nid).innerHTML = '- hide hosts';
	
	// show the table
	$('hostTable'+nid).show();
	
	// delete the hidden cookie flag
	Cookie.create('rz-hn-'+nid, '0', 1);
}

var Cookie = {
	create: function(name,value,days) {
		if (days) {									   
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}						   
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},					   
																				   
	read: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	erase: function(name) {
		createCookie(name,"",-1);
	}
}
