/***********************************************
* Omni Slide Menu script - © John Davenport Scheuer: http://home.comcast.net/~jscheuer1/
* very freely adapted from Dynamic-FX Slide-In Menu (v 6.5) script- by maXimus
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full original source code
***********************************************
* Modified by Dominic Robinson for Thoughtbubble Ltd.
* Note that this relies on jQuery
***********************************************/

var winHeight = 0;
var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
var d = document;
var AllowSubmit = 0;

function ml(id){
	var el=d.getElementById(id);
	el.style.visibility="visible"
	if(d.layers)el.style=el;
	el.sP=function(x,y){this.style.left=x+"%";this.style.top=y+"px";};
	el.x = startX;
	el.y = startY;
	return el;
}

function goCenter(){
	ftlObj = ml("ErrorDisplay");
	var pY = ns ? pageYOffset : iecompattest().scrollTop;
	ftlObj.y = pY + startY;
	ftlObj.sP(ftlObj.x, ftlObj.y);
}

function posCheck() {
	if (winHeight < 600) {
 		goCenter();
	} else {
		staticbar()
	}
}

function onLoadEvents() {
	getWinHeight()
	staticbar()
}

function getWinHeight() {
	if( typeof( window.innerHeight ) == 'number' ) {
	  //Non-IE
	  winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientHeight ) ) {
	  //IE 4 compatible
	  winHeight = document.body.clientHeight;
	}
}

var startX = 50 //set x offset of bar in %
var startY = 75 //set y offset of bar in pixels

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ErrorClose(){
	document.getElementById("ErrorDisplay").style.display="none"
}
function ErrorOpen() {
	document.getElementById("ErrorDisplay").style.display="block";
	staticbar();
}

function staticbar(){	
	window.stayTopLeft=function(){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		var errHeight = document.getElementById("ErrorDisplay").offsetHeight
		if (typeof(errHeight)=="undefined") { errHeight=1 }
		if (winHeight > errHeight+100) {
			ftlObj.y += (pY + startY - ftlObj.y)/8;
			ftlObj.sP(ftlObj.x, ftlObj.y);
			setTimeout("stayTopLeft()", 10);
		} else {
			ftlObj.y = pY + startY;
			ftlObj.sP(ftlObj.x, ftlObj.y);
		}
	}
	ftlObj = ml("ErrorDisplay");
	stayTopLeft();
}

if (window.addEventListener) {
window.addEventListener("load", onLoadEvents, false)
window.addEventListener("resize", getWinHeight, false)
} else if (window.attachEvent) {
window.attachEvent("onload", onLoadEvents)
window.attachEvent("onresize", getWinHeight)
} else if (document.getElementById) {
window.onload=onLoadEvents
window.onresize=getWinHeight
}

// ------ ---------------------- ------
//  Error Functions
//  Written by Dominic @ Thoughtbubble
// ------ ---------------------- ------

function nowt() { tempVar = 1; }

function fDimPage() {
	$("#mainWrapper").fadeTo("normal",0.3);
	$("#pageWrapper").fadeTo("normal",0.3);
	$("#fadeOnError").fadeTo("normal",0.3);
}

function fRestorePage() {
	$("#mainWrapper").fadeTo("normal",1);
	$("#pageWrapper").fadeTo("normal",1);
	$("#fadeOnError").fadeTo("normal",1);
}

function fShowError() {
	document.getElementById("ErrorDisplay").style.display = "block";
	staticbar();
}

function fRemoveHTML(str) {
	// Strip HTML if resorting to JS alerts.
	str = str.replace(/<strong>/g,"")
	str = str.replace(/<\/strong>/g,"")
	str = str.replace(/<em>/g,"")
	str = str.replace(/<\/em>/g,"")
	return str;
}

// =--  Validation Alert  --=
// Used for Validation alerts, converts the JS format to an HTML bulleted list.
function fErrorValidation(vMsg) {
	if (document.getElementById("ErrorDisplay")) {
		vMsg = vMsg.replace(/\n/g,"</li>")
		vMsg = "<ul class=\"black\">"+vMsg.replace(/-/g,"<li>")+"</ul>"
		vMsg = "<strong>You are required to fill in the following field(s).</strong>"+vMsg;
		fErrorAlert(vMsg)
	} else {
		alert("You are required to fill in the following field(s).\n"+vMsg);
	}
}


// =--  Basic Alert Replacement  --=
// Displays only a close button
function fErrorAlert(vMsg) {
	if (document.getElementById("ErrorDisplay")) {
		vMsg = HTMLizeJS(vMsg)
		//stayTopLeft()
		fDimPage()
		document.getElementById("ErrorHeading").innerHTML = "<h3>Please Note...</h3>";
		document.getElementById("ErrorDisplayInner").innerHTML = "<p>"+vMsg+"</p>";
		document.getElementById("ErrorButtons").innerHTML = '<a href="javascript:fErrorClose()" class="noborder"><img src="/images/system/btn/btn_close.gif" width="71" height="22" alt="" border="0" /></a>';
		fShowError()
	} else {
		vMsg = fRemoveHTML(vMsg)
		alert(vMsg);
	}
}

// =--  Confirm Alert Replacement  --=
// Displays a Confirm and Cancel button.
// This doesn't work like a JS confirm as the code that calls it doesn't wait for a return.
// Therefore code to call on cancel can be put into the vCode variable, which can also be a function.
function fErrorConfirm(vMsg,vCode) {
	if (document.getElementById("ErrorDisplay")) {
		vMsg = HTMLizeJS(vMsg)
		//stayTopLeft()
		fDimPage()
		document.getElementById("ErrorHeading").innerHTML = "<h3>Please Note...</h3>";
		document.getElementById("ErrorDisplayInner").innerHTML = "<p>"+vMsg+"</p>";
		document.getElementById("ErrorButtons").innerHTML = '<a href="javascript:fErrorClose()" style="margin-right:50px;" class="noborder"><img src="/images/system/btn/btn_cancel.gif" width="88" height="22" alt="Cancel" border="0" /></a><a href="javascript:fErrorClose();'+vCode+'" class="noborder"><img src="/images/system/btn/btn_confirm.gif" width="87" height="22" alt="Confirm" border="0" /></a>';
		fShowError()
	} else {
		confirm(vMsg);
	}
}

function fErrorConfirmLink(vMsg,vLink) {
	if (document.getElementById("ErrorDisplay")) {
		vMsg = HTMLizeJS(vMsg)
		//stayTopLeft()
		fDimPage()
		document.getElementById("ErrorHeading").innerHTML = "<h3>Please Note...</h3>";
		document.getElementById("ErrorDisplayInner").innerHTML = "<p>"+vMsg+"</p>";
		document.getElementById("ErrorButtons").innerHTML = '<a href="javascript:fErrorClose()" style="margin-right:50px;" class="noborder"><img src="/images/system/btn/btn_cancel.gif" width="88" height="22" alt="Cancel" border="0" /></a><a href="'+vLink+'" class="noborder"><img src="/images/system/btn/btn_confirm.gif" width="87" height="22" alt="Confirm" border="0" /></a>';
		fShowError();
		return false;
	} else {
		return confirm(vMsg);
	}
}

/*=- As Above but reversed so confirm does nothing -=*/
function fErrorConfirmReversed(vMsg,vCode) {
	if (document.getElementById("ErrorDisplay")) {
		vMsg = HTMLizeJS(vMsg)
		//stayTopLeft()
		fDimPage()
		document.getElementById("ErrorHeading").innerHTML = "<h3>Please Note...</h3>";
		document.getElementById("ErrorDisplayInner").innerHTML = "<p>"+vMsg+"</p>";
		document.getElementById("ErrorButtons").innerHTML = '<a href="javascript:fErrorClose();'+vCode+'" style="margin-right:50px;" class="noborder"><img src="/images/system/btn/btn_cancel.gif" width="88" height="22" alt="Cancel" border="0" /></a><a href="javascript:fErrorClose()" class="noborder"><img src="/images/system/btn/btn_confirm.gif" width="87" height="22" alt="Confirm" border="0" /></a>';
		fShowError()
	} else {
		confirm(vMsg);
	}
}



// =--  Close the error  --=
function fErrorClose() {
	document.getElementById("ErrorDisplay").style.display = "none"
	fRestorePage();
	setTimeout("fErrorClear()", 100); // We clear the content so it's not shown momentarily when reshown.
}

function fErrorClear() { document.getElementById("ErrorDisplayInner").innerHTML = ""; }
function fPressSubmit() { AllowSubmit = 1; document.getElementById('submit').click(); }
function fAllowSubmit() { AllowSubmit = 1; }

// =--  docLoad - a simple JavaScript page load.Useful in a confirm dialog.
function docLoad(vURL) {
	document.location.href = vURL;
}
// =--  replaces Javascript new lines with HTML breaks.
function HTMLizeJS(vMsg) {
	vMsg = vMsg.replace(/\n/g,"<br />")
	return vMsg;
}

// =--  HIGHLIGHT MISSING FIELDS  --=
// Give it a list of field ids. The label needs an id of for_field_name
function showMissingFields(vFieldList) {
	var arFields = vFieldList.split(",");
	for (var i = 0; i < arFields.length; ++i) {
		var item = arFields[i];
		if (item != "") {
			if($(item)) $('#'+item).addClass('missing')
			if($('#for_'+item)) $('#for_'+item).addClass('missing')
		}
	}
}


// =--  REMOVE MISSING FIELDS HIGHLIGHTING  --=
// Accepts a form ID and removes the missing class from ALL elements on the form.
// To be run before vallidation.
function resetFormStyles(vFormName) {
	arFields = $("#"+vFormName+" > *").removeClass('missing')
}
