/**
Filename: common.js
Last Modified: 5/30/10

Term of Use:
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/

/**
 *displayMsg
 *
 *output message in HTML format.
 *
 *@param msg[string] - message to output.
 *@param ele[string] - the element id to look for.
 *@param icon[string] - used to see if user wants to use an icon or not.
 *
 *@return HTML-friendly message.
 *
*/
function displayMsg(msg, ele, icon){
	obj = document.getElementById(ele);
	
	if (obj){
		//see what type of icon to use, if any.
		if(icon == "err"){
			obj.innerHTML = "<div id=\"error\"><img src=\"error.gif\" alt=\"Error\" />" + msg + "</div>";
		}else if(icon == "info"){
			obj.innerHTML = "<div id=\"info\"><img src=\"info.gif\" alt=\"Information\" />" + msg + "</div>";
		}else if(icon == "ok"){
			obj.innerHTML = "<div id=\"ok\"><img src=\"ok.gif\" alt=\"Ok\" />" + msg + "</div>";
        }else if(icon == "loading"){
			obj.innerHTML = "<div id=\"loading\"><img src=\"loading.gif\" alt=\"Loading...\" />" + msg + "</div>";
		}else if(icon == "none"){
			obj.innerHTML = "<div>" + msg + "</div>";
		}
	}else{
	    alert('No Object Found!');
	}
}

/**
 *clearMsg
 *
 *clear any messages made from previous function.
 *
 *@param ele[string] - the element id to look for.
 *
 *@return nulled html code.
 *
*/
function clearMsg(ele){
	obj = document.getElementById(ele);
	if (obj){
	   obj.innerHTML = "<div></div>";
	}else{
	    alert('No Object Found!');
	}
}

/**
 *gotoUrl
 *
 *Used to redirect a user to a location within the program's directory.
 *
 *@param addr[str] - the url to direct user to.
 *
*/
function gotoUrl(addr){
	window.location = addr;
}

/**
 *confirmDlg
 *
 *Used to display a confirmation dialog to the user.
 *
 *@param msg[str] - the message displayed to the user.
 *@param addr[str] - the url to direct user to.
 *
*/
function confirmDlg(msg, addr){
	if (confirm(msg)){
		gotoUrl(addr);
	}
}

/**
 *reloadElement
 *
 *Used to refresh an element.
 *
 *@param ele[string] - the element id to look for.
 *
*/
function reloadElement(ele) {
	var obj = document.getElementById(ele);
	var src = obj.src;
	var pos = src.indexOf('?');

	if (pos >= 0) {
		src = src.substr(0, pos);
	}

	//create new timestamp.
	var date = new Date();
	obj.src = src + '?v=' + date.getTime();
	return false;
}
