/*jslint eqeqeq: true, browser: true */
/*global window, $, jQuery */
/*JP 17/07/09*/
/*  JS Linted - All problems Solved before Minifying apart from 1 Eval on line 190 whcih I don't want to remove as it is someone elses code*/
/*  and I'm not entirely sure what its doing!*/
/*JP END*/
//********************************************************************
function MyObject(name) {
	return document.getElementById(name);
}

function Value(name) {
	return document.getElementById(name).value;
}

// Return the id of the selected ddl
function ddlid(name) {
	var ddl = document.getElementById(name);
	if (ddl !== null && ddl.selectedIndex !== -1) {
		var res = ddl.options[ddl.selectedIndex].id;
		return res;
	} else {
		return null;
	}
}

// Used to toggle the display of a button that invokes an Ajax call and any in-place HTML to 
// display a waiting display.
// This function should be called before the ajax call and once again when the call comes back to the
// client-side i.e via a supplied callback function.
// For example:
// <span id=""myButtonWait"" class=""hide""><img src=""images/ajax-loader.gif""/></span><input id=""myButton"" class=""submit"" type=""button"" value=""do it"" onClick=""javascript:invokeAjaxCall();""/>
// Notes
// 1) The span must have an id of <buttonId> + "Wait"
// 2) The span must be initially hidden (see css class that sets display to be none)
function PlaceWaitButtonToggle(buttonId) {
	var ajaxCmd = MyObject(buttonId);
	var ajaxCmdWaitUI = MyObject(buttonId + 'Wait');

	if (ajaxCmd !== null && ajaxCmdWaitUI !== null) {
		if (ajaxCmd.style.display === '') {
			ajaxCmd.style.display = 'none';
			ajaxCmdWaitUI.style.display = 'inline';
		} else {
			ajaxCmd.style.display = '';
			ajaxCmdWaitUI.style.display = 'none';
		}
	}
}

// Put this information into the HTML and hence screen
// 0|error message
// location|HTML
function Place(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	} else {
		document.getElementById(listTokens[0]).innerHTML = listTokens[1];
	}

	this.UpdateAjaxSessionTimer();
}

function ActionConfirmAndPlace(confirmMessage, res) {
	var listTokens = res.value.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	}
	else {
		alert(confirmMessage);
		document.getElementById(listTokens[0]).innerHTML = listTokens[1];
	}

	this.UpdateAjaxSessionTimer();
}

function PlaceTop(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	}
	else if (listTokens[0] === 'F') {
		document.location.href = listTokens[1];
	}
	else {
		document.getElementById('top').innerHTML = listTokens[1];
	}

	this.UpdateAjaxSessionTimer();
}

function ReDirect(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
		this.UpdateAjaxSessionTimer();
	}
	else {
		document.location.href = listTokens[1];
	}
}

function AlertOnError(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	}

	this.UpdateAjaxSessionTimer();
}

function PlaceLocator(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	}
	else {
		document.getElementById(listTokens[0]).innerHTML = listTokens[1];
	}

	this.UpdateAjaxSessionTimer();
}

function PlaceContent(res) {
	var result = res.value,
	listTokens = result.split('|');

	if (listTokens[0] === '0') {
		alert(listTokens[1]);
	}
	if (listTokens[0] === 'F') {
		document.location.href = listTokens[1];
	}
	else {
		document.getElementById('content').innerHTML = listTokens[1];
		var nlocation = document.location + '#',
		location = nlocation.split('#'),
		aNumber = listTokens[0] - 1;
		if (aNumber < 1) {
			aNumber = 1;
		}
		document.location = location[0] + '#' + aNumber;
	}

	this.UpdateAjaxSessionTimer();
}

// T. Coote 24 Oct 2006
// Lets make the prompt2 box display at where the user's mouse is! 
var downY = undefined,
downX = undefined;

function GetDownCoords(e) {
	//this function will store the coordinates on a mouse down event
	if (!e) { // IE browsers
		downX = window.event.x;
		downY = window.event.y;
	}
	else { // Non IE browsers
		downX = e.clientX;
		downY = e.clientY;
	}
}

//*********************************************************************
// Taken from
// http://javascript.internet.com/text-effects/customizable-javascript-prompt.html
// Provide a small prompt window
// Call Example
// prompt2('btn1p.gif', 'My Prompt','Please enter your name ,if you want to chat with our <B>customer support executive</B>', 'myfunction');
// prompt2('in/close.gif', 'Add a Company','<br /><br />Name of the new company', 'newnode');
function prompt2(promptpicture, prompttitle, message, sendto) {
	promptbox = document.createElement('div');

	promptbox.setAttribute('id', 'prompt');

	document.getElementsByTagName('body')[0].appendChild(promptbox);

	promptbox = eval("document.getElementById('prompt').style");

	promptbox.position = 'absolute';

	promptbox.top = downY - 100;

	promptbox.left = downX + 200;

	promptbox.width = 300;

	promptbox.border = 'outset 1 #bbbbbb';

	document.getElementById('prompt').innerHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%'><tr valign='middle'><td width='22' height='22' style='text-indent:2;' class='titlebar'><img src='" + promptpicture + "' height='18' width='18'></td><td class='titlebar'>" + prompttitle + "</td></tr></table>";

	document.getElementById('prompt').innerHTML = document.getElementById('prompt').innerHTML + "<table cellspacing='0' cellpadding='0' border='0' width='100%' class='promptbox'><tr><td>" + message + "</td></tr><tr><td><input type='text' id='promptbox' onblur='this.focus()' class='promptbox'></td></tr><tr><td align='right'><br><input type='button' class='prompt' value='OK' onMouseOver='this.style.border=\"1 outset #dddddd\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='" + sendto + "(document.getElementById(\"promptbox\").value); document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'> <input type='button' class='prompt' value='Cancel' onMouseOver='this.style.border=\"1 outset transparent\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='" + sendto + "(\"\"); document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'></td></tr></table>";

	document.getElementById("promptbox").focus();

}

function Split_callback(res, imageId) {
	var result = res.value;

	if (result !== "") {
		var listTokens = result.split('|');
		document.getElementById(listTokens[0]).innerHTML = listTokens[1];
	}

	var ldng = document.getElementById(imageId);
	if (ldng) {
		ldng.style.display = 'none';
	}
	this.UpdateAjaxSessionTimer();
}

function UpdateAjaxSessionTimer() {
	if ((typeof ajaxTimer !== 'undefined') && ajaxTimer) {
		ajaxTimer.resetTimer();
	}
}

function SelectTopNav() {
	var sFullUrl = location.href;
	var sURL = sFullUrl.replace(/(^.*\/)|(\?.*$)/ig, '');
	var bSelected = true
	/*false*/
	;
	/*$("#topNav li a").each(function() {
        if ($(this).attr('href') === sURL){
            bSelected = true;
        }; 
    });*/
	if (bSelected) {
		$("#topNav li a").each(function () {
			if ($(this).attr('href') === sURL) {
				$(this).parent('li').addClass('GreyedTopNav');
			}
		});
	}
}