/*
This file does all the shared stuff for the creately.com website
*/
//some very useful variables
var server = 'creately.com'; //set this to the correct server
var logincheck_url = 'http://' + server + '/jupiter/logincheck.jsp';
var app_url = 'http://' + server + '/app/';
var applogout_url = 'http://' + server + '/app/logout';
var appauthenticate_url = 'http://' + server + '/app/authenticate';
var SJAX = null;

//getSJAX(); //lets try and get an AJAX object

/*
This function tries to get a valid AJAX (XMLHttpRequest) object.
*/
function getAJAX() {
	if (window.XMLHttpRequest) {
		AJAX=new XMLHttpRequest();
	} else {
		AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	}	
}

/*
This function checks for the Enter key being pressed
*/
function checkEnter(event, callbackFunction) {
	if(event.keyCode == 13) {
		callbackFunction();
	}
}

/*
This function redirects the web browser to the Creately URL
*/
function goCreately() {
	if(location.search.indexOf('diagID') == -1) {
		url = app_url;
	} else {
		url = app_url + location.search;
	}
	window.location.replace(url);
	return false;
}

/*
This function redirects the web browser to the Creately logout URL
*/
function logoutCreately() {
	window.location.replace(applogout_url);
	return false;
}

/*
This function reloads the current page
*/
function reload_page() {
	window.location.reload();
}

/*
This is a JS AJAX Object
see http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object
*/
function ajaxObject(url, callbackFunction) {
	var that=this;
	this.updating = false;
	this.abort = function() {
		if (that.updating) {
			that.updating=false;
			that.AJAX.abort();
			that.AJAX=null;
		}
	}
	this.update = function(passData,postMethod) { 
		if (that.updating) { 
			return false;
		}
		that.AJAX = null;
		if (window.XMLHttpRequest) {
			that.AJAX=new XMLHttpRequest();
		} else {
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (that.AJAX==null) {
			return false;
		} else {
			that.AJAX.onreadystatechange = function() {
				if (that.AJAX.readyState==4) {
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;
				}
			}
			that.updating = new Date();
			if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				that.AJAX.setRequestHeader("Content-Length", passData.length);
				that.AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
				that.AJAX.open("GET", uri, true);
				that.AJAX.send(null);
			}
			return true;
		}
	}
	var urlCall = url;
	this.callback = callbackFunction || function () { };
}

/*
Plus and Minus images
*/
var imgout=new Image(18,18);
var imgin=new Image(18,18);
var imgloading=new Image(16,16);
var imgsave=new Image(14,14);
var imgemail=new Image(14,10);
var imgemailsent=new Image(14,10);
var imgdelete=new Image(24,22);

/////////////////BEGIN USER EDITABLE///////////////////////////////
	imgout.src="/custom_inc/images/plus.gif";
	imgin.src="/custom_inc/images/minus.gif";
	imgloading.src="/custom_inc/images/loading.gif";
	imgsave.src="/custom_inc/images/save.jpg";
	imgemail.src="/custom_inc/images/email.jpg";
	imgemailsent.src="/custom_inc/images/emailsent.jpg";
	imgdelete.src="/custom_inc/images/delete.jpg";
///////////////END USER EDITABLE///////////////////////////////////

//this switches expand collapse icons
function filter(imagename, objectsrc){
	if (document.images){
		document.images[imagename].src=eval(objectsrc+".src");
	}
}

//show OR hide funtion depends on if element is shown or hidden
function shoh(id) { 
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = '';
			filter(("img"+id),'imgin');			
		} else {
			filter(("img"+id),'imgout');
			document.getElementById(id).style.display = 'none';			
		}	
	} else { 
		if (document.layers) {	
			if (document.id.display == "none"){
				document.id.display = '';
				filter(("img"+id),'imgin');
			} else {
				filter(("img"+id),'imgout');	
				document.id.display = 'none';
			}
		} else {
			if (document.all.id.style.visibility == "none"){
				document.all.id.style.display = '';
			} else {
				filter(("img"+id),'imgout');
				document.all.id.style.display = 'none';
			}
		}
	}
}

var popWindow;

function openPopupWindow( urlToOpen, windowName, options ) {

	if (  popWindow == null || popWindow == 'undefined' ) {
		popWindow = window.open( urlToOpen, windowName, options );
	} else {
		try {
		
			if(popWindow.name != windowName ) {
				popWindow = window.open( urlToOpen, windowName, options );
			}
		} catch ( error ) {
			popWindow.close();
			popWindow = window.open( urlToOpen, windowName, options );
		}
	}
	popWindow.focus();
	void(0);
}
