//===============================================================
// 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;
}