/********************************************************
Common library kazad.net utilities
(c) Kalid Azad 2006
http://www.instacalc.com
********************************************************/

///////////////////////////////////////////////////////////////
/// UTILITY FUNCTIONS: Show/Hide item, clear CSS style, etc.
///////////////////////////////////////////////////////////////
	function hideItem(itemName)
	{		
		var i = document.getElementById(itemName);
		if (i != null){
		  i.style.display = "none";
		  //new Effect.Fade(i);
		}
	}
	
	function showItem(itemName)
	{	
		var i = document.getElementById(itemName);
		if (i != null){
		  i.style.display = "";
		}
	}
	
	function isObjectHidden(obj){
	  if (obj != null){
	    return obj.style.display == "none";
	  }
	  return null;
	}
	
	function isObjectVisible(obj){
	  if (obj != null){
	    return !(obj.style.display == "none");
	  }
	  return null;
	}
	
	function isHidden(itemName){
		var i = document.getElementById(itemName);
		if (i != null){
		  return i.style.display == "none";
		}
		return null;
	}
	
	function isVisible(itemName){
		var i = document.getElementById(itemName);
		
		if (i != null){
		  return i.style.display == "";
		}
		return null;
	}
		
	function hideObject(item)
	{	
		if (item != null)
		  item.style.display = "none";
	}
	
	function showObject(item)
	{	
		if (item != null)
		  item.style.display = "";
	}
	
	// takes raw element to toggle
	function toggle(i){
		if (i.style.display=="none"){
			//new Effect.Highlight(i)
			i.style.display = "";
			
		}
		else{
			i.style.display ="none";
		}
	}
	
	function toggleItem(itemName)
	{
		var i = document.getElementById(itemName);
		if (i.style.display=="none"){
			//fade in the item
			//new EffectPack.BlindDown(i);
			//new Effect.Highlight(i)
			//i.style.display = "";
			//new Effect.Appear(i);
			showObject(i);
		}
		else{
			//i.style.display ="none";
			//new Effect.Fade(i);
			hideObject(i);
		}
	}
	
	function clearItem(itemName){
		var i = document.getElementById(itemName);
		i.innerHTML = "";
	}
	
	function hideAllItems(items){
	   var i = 0;
	   for (i = 0; i < items.length; i++){
	     hideItem(items[i]);
	   }
	}
	
  function changeCSS(id, newClass) {
	var identity=document.getElementById(id);
	identity.className=newClass;
  }

function setFont(element,size){
   var i = document.getElementById(element)
   i.style.display = ParseInt(size)
}

function sleep(ms)
{
	date = new Date();
	var curDate = null;

	do { var curDate = new Date(); }
		while(curDate-date < ms);
} 

function toggleBoolean(i){
  return !i;
}

function toggle(i){ // only toggle integeres
  if (i == 1)
    i = 0
  else if (i == 0)
     i = 1
  return i
}

// return all URL parameters in a hash array (key => value)
// TODO
function getURLParams(str){
	var strReturn;
	
	if (str == null){
	   var strHref = window.location.href;  
	}
	else{
	   var strHref = str;
	}
	
	//var strHref = window.location.href;
	
	if (strHref.indexOf("?") > -1){
	  var a = strHref.split("?");
	  strHref = "?" + a[1];
	}
	else{
	  strHref = "";
	}
	
	return strHref;

}

function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	
	// BUGFIX because of =c= issue in delicious URLs
	strHref = window.location.href.replace(/=([csv])=/g, '&$1=');
	
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	
	return strReturn;
}

function getURLNoParams(){
	var strHref = window.location.href;

	if ( strHref.indexOf("?") > -1 ){
		var a = strHref.split("?");
		strHref = a[0];
	}
	return strHref;
}


function log_old(string){
if (logging > 0 && (statusarea != null)){
  statusarea.innerHTML += string
  statusarea.innerHTML += "<br/>";
}
}

// 0 is off, 4 is most verbose;
function setlogging(i){
  logging = i;
}

/*******
Always add msg to a div with id "log".
*******/
function log_old(msg){
    var logdiv = document.getElementById("log")
    if ( logdiv != null ){
        logdiv.innerHTML += msg;
        logdiv.innerHTML += "<br/>";
    }
}

// Only add msg to log if "logging" > 1. Higher level means more verbose.
function log1(msg){
    if (logging != null && logging >= 1 ) log(msg);
}

function log2(msg){
    if (logging != null && logging >= 2 ) log(msg);
}

function log3(msg){
    if (logging != null && logging >= 3 ) log(msg);
}


/*********
Return time as HH:MM:SS
********/
function getTime(){
    var now = new Date();
    var ampm = "am"
    hours = now.getHours();
    minutes = now.getMinutes();
    seconds = now.getSeconds();
    
    // assume am -- switch to pm otherwise.
    if (hours >= 12){ hours -= 12; ampm = "pm"; }
    // turn :2 into :02
    if (minutes < 10){ minutes = "0" + minutes; }
    
    if (seconds < 10){ seconds = "0" + seconds; }
    
    
    return hours + ":" + minutes + ":" + seconds + " " + ampm;
} 

/**********
 Populate dropdowns
**********/
// remove duplicate items from array
function uniqueArray(a){
  if (a == []) return [];
  var i;
  a = a.sort();
  b = [];
  for(i = 0; i < a.length; i++){
    if (b.length == 0){
		b.push(a[i]);
	}
	
	if (b[b.length -1] != a[i]){
	    b.push(a[i]);
	}
  }
  
  return b;
}

function isdefined( variable)
{
    return (typeof(variable) == "undefined") ?  false: true;
}

// return index of item in the array
function arraySearch(item, a){
  if (! isdefined(a) ){ return -1; }

  for (i = 0; i < a.length; i++){
    if (item == a[i]) // found
	  return i;
  }
  
  return -1; // wasn't found
}

// case insensitive, for text
function objectSearchI(item, obj){
	for (var i in obj)
	{
		if (obj[i].toString().toUpperCase() == item.toString().toUpperCase()){
			return i;
		}
	}
	
	return null;
}

function confirmClick(str)
{
if (str == ""){
  str = "Are you sure you wish to continue?"
}
	var agree=confirm(str);
	if (agree)
		return true ;
	else
		return false ;
}

// convert text into readable URL string
function txt2url(str){
  str = str.replace(/_/g, "%5f"); // replace underscore
  str = str.replace(/ /g, "_"); //swap space for underscore
  str = escape(str);
  
  return str;
}

// inverse - convert readable url into text
function url2txt(str){
  str = unescape(str); // replace underscore
  str = str.replace(/_/g, " ");
  str = str.replace(/%5f/ig, "_");
  
  return str;
}


