//===============================================================
// popup
function popup(theURL, winName, features, myWidth, myHeight, isCenter) 
{
	var popwin;

	features += (features!='')?',':'';
	if( window.screen )
		if( isCenter == "true" )
		{
			var myLeft = (screen.width-myWidth)/2;
			var myTop = (screen.height-myHeight)/2;
			features += 'left='+ myLeft+ ',top='+ myTop;
		}
	features += ',width='+myWidth+',height='+myHeight+',toolbar=no, location=no, menubar=no, status=no, scrollbars=yes, resizable=yes'

	popwin = window.open(theURL, winName, features);
	if (popwin) {
		popwin.location.replace(theURL);
		popwin.focus();
	}
	return popwin;
}

//===============================================================
// print page
function print_page(element)
{
	if( document.getElementById(element) ) 
	{
		document.getElementById(element).style.display = 'none';
	}
	window.print();
	if( document.getElementById(element) ) 
	{
		document.getElementById(element).style.display = '';
	}
}

//===============================================================
// equivalent with php function in_array 
function in_array(needle, haystack) 
{
	for (var i = 0; i < haystack.length; i++) 
	{
		if (haystack[i] == needle) 
		{
			return true;
		}
	}
	return false;
}

//===============================================================
// removes : leading and trailing spaces, consecutive spaces( replaces them with one space ) from a string. 
// if something besides a string is passed in ( null, custom object, etc... ) then return the input.
function trim(inputString) 
{
	if( typeof inputString != "string" ) 
	{ 
		return inputString; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	// check for spaces at the beginning of the string
	while( ch == " " ) 
	{ 
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	// check for spaces at the end of the string
	while( ch == " " ) 
	{ 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	// note that there are two spaces in the string - look for multiple spaces within the string
	while( retValue.indexOf("  ") != -1 ) 
	{ 
		// again, there are two spaces in each of the strings
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}

	// return the trimmed string back to the user
	return retValue;
}

//===============================================================
// encrypt with md5
function encrypt(frm, field, salt, result)
{
	var encrypted1 = hex_md5(field);
	var encrypted2 = hex_md5(encrypted1+salt);
	result = encrypted2;
}

//===============================================================
// show / hide a div html element based on browser
function showhide_elem(elem_name, style_class)
{
	if(	document.layers ) 
	{
		document.layers[elem_name].className=style_class; 
	} 
	else if ( document.getElementById )
	{
		document.getElementById(elem_name).className=style_class;
	} 
	else if( document.all ) 
	{
		document.all(elem_name).className=style_class;
	}
}

//===============================================================
//== load JavaScript
function loadScript(src) 
{
	var script = document.createElement("script");
	script.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(script);
	script.src = src;
}

//===============================================================
//== getElementsByClassName - version I
function xGetElementsByClassName(clsName, parentEle, tagName) 
{
  var found = new Array();
  var re = new RegExp('\\b'+clsName+'\\b', 'i');
  var list = xGetElementsByTagName(tagName, parentEle);
  for (var i = 0; i < list.length; ++i) {
    if (list[i].className.search(re) != -1) {
      found[found.length] = list[i];
    }
  }
  return found;
}
function xGetElementsByTagName(tagName, parentEle)
{
  var list = null;
  tagName = tagName || '*';
  parentEle = parentEle || document;
  if (xIE4 || xIE5) {
    if (tagName == '*') list = parentEle.all;
    else list = parentEle.all.tags(tagName);
  }
  else if (parentEle.getElementsByTagName) list = parentEle.getElementsByTagName(tagName);
  return list || new Array();
}

//===============================================================
//== getElementsByClassName - version II
// Call it like this - var anchors = getElementsByClassName("external","a");. This will return all the anchors that are in the 'external' class. Or use just var anchors = getElementsByClassName("external"); this will return all elements that are in the 'external' class
function getElementsByClassName(classname, tag) 
{
	if(!tag) tag = "*";
	var anchs =  document.getElementsByTagName(tag);
	var total_anchs = anchs.length;
	var regexp = new RegExp('\\b' + classname + '\\b');
	var class_items = new Array()
 
	for(var i=0;i<total_anchs;i++)
	{ 
		//go thru all the links seaching for the class name
		var this_item = anchs[i];
		if(regexp.test(this_item.className)) 
		{
			class_items.push(this_item);
		}
	}
	return class_items;
}

//===============================================================
//== Dump() Function - Javascript equivalent of PHP's print_r() 
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
/**
* Calling the function... : This is how the function is called. In this example we will give a complex array as the argument.
function init() 
{
	var arra = new Array("Hloo",'s',23,23.1,"Hello World");
	var assoc = {
 		"val"  : "New",
 		"number" : 14,
 		"theting" : arra
	};
	alert(dump(assoc));
}
window.onload=init;

* The result will be shown in the following format.
'val' => "New"
'number' => "14"
'theting' ...
   '0' => "Hloo"
   '1' => "s"
   '2' => "23"
   '3' => "23.1"
   '4' => "Hello World"
*/
function dump(arr,level) 
{
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') 
	{ 
		//Array/Hashes/Objects
 		for(var item in arr) 
 		{
  			var value = arr[item];
 
  			if(typeof(value) == 'object') 
  			{ 
  				//If it is an array,
   				dumped_text += level_padding + "'" + item + "' ...\n";
   				dumped_text += dump(value,level+1);
  			} 
  			else 
  			{
   				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  			}
 		}
	} 
	else 
	{ 
		//Stings/Chars/Numbers etc.
 		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	
	return dumped_text;
}

function dag_dump(arr)
{
	var msg = "";
	msg += "Array nb elements: "+arr.length;
	msg += "\n";
	msg += "Array contents\n";
	for(i=0; i<arr.length; i++) { msg += arr[i] + "\n"; }
	
	return msg;
}