var rewriteTreeID = -1;

function updateTree(resource, target, action, displayType) {
    var actions = "resourceID=" + resource + " &targetID=" + target + "&action=" + action + "&displayType=" + displayType;
    postAJAX("/modules/filesystem/folders/FolderServlet", actions, rewriteTree, "Text");
    
    rewriteTreeID = resource;
}

function rewriteTree(responseText) {

    document.getElementById("FolderModuleTree_" + rewriteTreeID).innerHTML = responseText;
}

/**
 * this function will bring up a calendar window to select a given date
 * @param obj an object that is used to place the pop up window
 * @param target the variable that is to be set, e.g. myform.myelement.value
 */
function drawCalendar(obj, target) {
	var time = 0;

	if(obj.value.match(/^\d{4}-\d{2}-\d{2}$/)) {
		var dateFields = obj.value.split(/-/);
		var date = new Date();
		// months start at 0 (January) in JavaScript, so take away 1
		date.setFullYear(dateFields[0], dateFields[1] - 1, dateFields[2]);
		time = date.getTime();
	}

	var url = "/includes/calendar.jsp?formTarget=" + target;
	if(time > 0)
		url += "&date=" + time;

	myWindow = window.open(url,
	                       "tinyWindow", 
	                       "width=300, height=300, " +
	                       "left=" + (findPosX(obj) - 100 + window.screenLeft) +
	                       ", top=" + (findPosY(obj) - 100 + window.screenTop + 
	                       "screenX=" + (findPosX(obj) - 100 + window.screenLeft) +
	                       ", screenY=" + (findPosY(obj) - 100 + window.screenTop))); 

	return myWindow;
}

/**
 * this function will bring up a selection window to choose a resource
 * @param obj an object that is used to place the pop up window
 * @param target the variable that is to be set, e.g. myform.myelement.value
 * @param resourceID the resource that the tree will display from
 * @param mode how the resource will be returned, options are LINK or ID

<form name="test">
<input type="text" name="myfield" readonly="readonly" size="20" maxlength="100">
	<a href="#" onclick="win = drawResourceSelection(this, 'test.myfield.value', <%=hdrResourceID%>, 'LINK'); win.focus(); return false;"><img border="0" src="/images/lists/ModuleID_30.gif" alt="Select resource path" /></a>
</form>

 */
function drawResourceSelection(obj, target, resourceID, mode) {
	var windowLeft = findPosX(obj) - 100 + window.screenLeft,
	    windowTop = findPosY(obj) - 100 + window.screenTop,
	    myWindow;

	if(windowLeft < 0 || windowLeft > 800) {
		windowLeft = 0;
	}
	if(windowTop < 0 || windowTop > 600) {
		windowTop = 0;
	}

	myWindow = window.open
	(
		"/includes/resourceSelection.jsp?resourceID=" + resourceID + "&formTarget=" + target + "&resourceSelectMode=" + mode, "_blank",
		"toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=600, height=450, " +
		"left=" + windowLeft + ", top=" + windowTop +
		"screenX=" + windowLeft + ", screenY=" + windowTop
	); 

	return myWindow;
}

function fileBrowserCallBack(field_name, url, type, win) {
    var fileBrowserWindow = new Array();
    fileBrowserWindow['title'] = 'File Browser';
    fileBrowserWindow['file'] = "/includes/resourceSelection.jsp" + "?type=" + type + "&resourceSelectMode=" + type;
    fileBrowserWindow['width'] = '420';
    fileBrowserWindow['height'] = '400';
    fileBrowserWindow['close_previous'] = 'no';
    tinyMCE.openWindow(fileBrowserWindow, {
	window : win,
	input : field_name,
	resizable : 'yes',
	inline : 'yes',
	scrollbars: 'yes'
    });
    //return;   
}

/**
 * this function will bring up a selection window to choose a database field
 * @param obj an object that is used to place the pop up window
 * @param target the variable that is to be set, e.g. myform.myelement.value
 * @param resourceID the resource that the tree will display from

<form name="test">
<input type="text" name="myfield" readonly="readonly" size="20" maxlength="100">
	<a href="#" onclick="win = drawResourceSelection(this, 'test.myfield.value', <%=hdrResourceID%>); win.focus(); return false;"><img border="0" src="/images/lists/ModuleID_30.gif" alt="Select database field" /></a>
</form>

 */
function drawDBFieldSelection(obj, target, resourceID) {
	var windowLeft = findPosX(obj) - 100 + window.screenLeft,
	    windowTop = findPosY(obj) - 100 + window.screenTop,
	    myWindow;

	if(windowLeft < 0 || windowLeft > 800) {
		windowLeft = 0;
	}
	if(windowTop < 0 || windowTop > 600) {
		windowTop = 0;
	}

	myWindow = window.open
	(
		"/includes/tableFieldSelection.jsp?resourceID=" + resourceID + "&formTarget=" + target, "tinyWindow",
		"toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=1, width=600, height=450, " +
		"left=" + windowLeft + ", top=" + windowTop +
		"screenX=" + windowLeft + ", screenY=" + windowTop
	); 

	return myWindow;
}

/**
 * launches a window to display the image that is passed in
 * @param url the url to the image that is to be displayed
 * @param width the width of the window
 * @param height the height of the window
 */
function displayImage(obj, url, width, height) {
	myWindow = window.open("/modules/utility/imageDisplay.jsp?image=" + url, "", 
		"toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + ", left=" + (findPosX(obj) - 100 + window.screenLeft) + ", top=" + (findPosY(obj) - 100 + window.screenTop + 
		"screenX=" + (findPosX(obj) - 100 + window.screenLeft) + ", screenY=" + (findPosY(obj) - 100 + window.screenTop))); 
}

function swapImage(imageName, image) {
    document.images[imageName].src = image.src;
}


function popUp(URL, params) {
    day = new Date();
    id = day.getTime();
    window.open(URL + "?" + params, "", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=550,left=0,top=0");
}

function newWindowPopup(myLink) {
	window.open(myLink,'Images','width=780,height=600,resizable=yes,scrollbars=yes');
}

function checkAll(selector, checkbox) {
    //alert(form + ", " + checkbox);
    if (!checkbox) {
	return;
    }

    for (i = 0; i < checkbox.length; i++) {
	checkbox[i].checked = selector.checked;
    }
}

function articulateDisplay(id, path) {

    var style = "position: absolute; background-color: black; height: 100%; width: 100%; top: 0px; left: 0px;";
    var obj = document.getElementById("container_" + id);
    
    if (obj.style.background) {
	obj.style.background = style;
    } else {
        obj.setAttribute("style", style);
    }

var winW = 630, winH = 460;

    if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      winW = window.innerWidth;
      winH = window.innerHeight;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth;
      winH = document.body.offsetHeight;
     }
    }

    obj.innerHTML = 
	"<center>" +
	"<a href='#' style='background-color: black; color: white; display: block; width: 100%;' onclick=\"document.getElementById('container_" + id + "').style.visibility='hidden';\">Hide</a>" +
	"<iframe width='100%' height='" + (winH - 50) + "' src='/Articulate/" + path + "'></iframe><br />" +
	"</center>";
  
}

function expandCollapseWithImg(idName, idCaller, collapseImg, expandImg) {
    obj = document.getElementById(idName);
    callerObj = document.getElementById(idCaller);

    if (obj.style.visibility === 'hidden' || obj.style.display === 'none') {
	obj.style.visibility = 'visible';
	obj.style.display = 'block';
	callerObj.style.backgroundImage = 'url(' + collapseImg + ')';
    } else {
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
	callerObj.style.backgroundImage = 'url(' + expandImg + ')';
    }
}

function expandCollapse(idName, idCaller) {
    obj = document.getElementById(idName);
    callerObj = document.getElementById(idCaller);

    if (obj.style.visibility === 'hidden' || obj.style.display === 'none') {
	obj.style.visibility = 'visible';
	obj.style.display = 'block';
	callerObj.style.backgroundImage = 'url(/images/collapse.gif)';
    } else {
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
	callerObj.style.backgroundImage = 'url(/images/expand.gif)';
    }
}

/**
 * AJAX Calls
 */

function makeJSONPOSTRequest(url, params) {
    requestNumber = JSONRequest.post(
	"http://phpbb.cmegroove.intratuitive.com/login.php", 
	{
	    username: "mgeorge",
	    password: "new1234",
	    login: "Log in"
	}, 
	function (requestNumber, value, exception) {
	    if (value) {
		processResponse(value);
	    } else {
		processError(exception);
	    }
	}
    );
}

function processResponse(value) {
    alert(value);
}

function processError(exception) {
    alert(exception);
}

function postAJAX(url, parameters, callback_function, docType) {
    var http_request = null;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
	    // set type accordingly to anticipated content type
	    http_request.overrideMimeType('text/xml');
	    //http_request.overrideMimeType('text/plain');
	}
    } else if (window.ActiveXObject) { // IE
	try {
	    http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    try {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	}
    }
    if (!http_request) {
	alert('Cannot create XMLHTTP instance');
	return false;
    }

    http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
	   if (http_request.status == 200) {
               if (docType == null || docType == "XML" || docType == undefined)
		callback_function(http_request.responseXML);
               else
                callback_function(http_request.responseText);   
	   } else {
		alert('There was a problem with the request.(Code: ' + http_request.status + ')');
	   }
       }
    } 
    http_request.open('post', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}


function get(obj) {
    var poststr = "mytextarea1=" + encodeURI( document.getElementById("mytextarea1").value ) +
		"&mytextarea2=" + encodeURI( document.getElementById("mytextarea2").value );
    makePOSTRequest('http://www.captain.at/howto-ajax-post.php', poststr);
}

function swapLanguage(l) {
    window.location = "?resourceID=" + resourceID + "&lang=" + l;
}

function readCookie(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;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
