
// trim
function trim(str)
{
	if (!str.match(/\S/))
		return '';
	return str.replace(/^\s+|\s+$/g,'');
}

/*
 * preloading an image. creating an image object with given source
 * imgObj: name of object to be created
 * imgSrc: URL of image 
 */
function preload(imgObj,imgSrc)
{
	if (document.images) {
		eval(imgObj+' = new Image()');
		eval(imgObj+'.src = "'+imgSrc+'"');
    }
}


/*
 * This function receives a comma separated string where each
 * pair is an image object name and an image URL.
 * the function then goes over these pairs array and invokes the
 * preload function to load the images into the browser's memory
*/
function preloadThem(str)
{
	var arr = str.split(/\s*,\s*/)
	for (i = 0; i < arr.length; i += 2)
	{
		//alert(arr[i]+"\n"+arr[i+1])
		preload(arr[i],arr[i+1])
	};
}

/*
 * Used to invoke preloadThem() function.
 * Please read the preloadThem() documentation.
 */
function preloadImages(str)
{
	document.onload = preloadThem(str);
}

/*
 * change a layer's image elemnt's source
 * layer: name of a non nesting layer. if null look for image in document.images
 * img: name of image element or an image object (or input type="image")
 * imgObjName: name of preloaded image object
 * -> image element of layer source would be replaced with image object's source
 */
function changeImage(img,imgObjName,layer)
{
	var imgObjSrc = eval(imgObjName+".src");
	// handle image object
	if (typeof(img) == 'object')
		img.src = imgObjSrc
	// handle image name
	else if (document.images && typeof(img) == 'string') {
        if (document.all || document.getElementById || layer == null)
            document.images[(img)].src = imgObjSrc;
        else eval('document.'+layer+'.document.images["'+img+'"].src = ' + imgObjSrc);
    }
}

// parse file name from file's path
function getFileName(filePath)
{
    var p = Math.max(filePath.lastIndexOf('/'),filePath.lastIndexOf('\\'))
    return filePath.substring(p + 1,filePath.length)
}

// This function opens a new window in the middle of the screen
// Parameters:
// w - the width of the window
// h - the height of the window
// ur - the URL of the window
// target - optional target of the window
// params - optional window parameters
function openwindow(w,h,ur,target,params)
{
	var winleft = screen.width/2 - w/2
	var wintop = screen.height/2 - h/2 - 30
	var fullParams = 'width=' + w + ',height=' + h + ',' + 'top=' + wintop + ',left=' + winleft + ','
	if (!params)
		params = 'scrollbars=auto,menubar=no,resizable=yes,toolbar=no,location=no,status=no'
	fullParams += params
	if (!target) target = ''
	var newWinObj = window.open(ur,target,fullParams)
	if (newWinObj) newWinObj.focus()
	return newWinObj
}

// return reference to an object.
// compatible with IE4 and above, NS6 and above
function getObjectById(id)
{
	if (document.all)
		return document.all(id)
	else if (document.getElementById)
		return document.getElementById(id)
	else
		return null
}

// display number with given precision
// returns a string
function precision(num,p)
{
	if (!p || isNaN(num) || num.length == 0)
		return num
	num *= Math.pow(10,p)
	num = Math.round(num)
	num = num*10 + 1
	num /= Math.pow(10,p+1)
	num = num.toString()
	num = num.substring(0,num.length-1)
	return num
}

// trim long strings to a limit of chars and replace with ...
function RTrimWith3dots(str, nChars)
{
	var reg = new RegExp('^(.{' + (nChars - 3) + '})(.{4,})$')
	if (reg.test(str))
		str = str.replace(reg,'$1...')
	return str
}

//=============================================================
//	Function: Decode()
//
//	description:
//		an inline select case function which accept a variant
//		arguments length. function itereate through arguments list,
//		built of compare value, return value pairs, and compare each
//		compare value to its first argument. if a match is found its
//		following return value is returned. function must get at least
//		3 arguments.
//
//	input parameters:
//		i_vCompare			- variant. all compare values are matched to first argument
//		i_vCompareTo1		- variant. 1st compare value
//		i_vRetVal1			- variant. 1st return value
//		...
//		i_vCompareToN		- variant. N-th compare value
//		i_vRetValN			- variant. N-th return value
//		i_vDefaultRetVal	- variant. optinal default return value. returned if no match is found
//
//	return value:
//		variant
//=============================================================
function Decode(/*
	i_vCompare, 
	i_vCompareTo1, i_vRetVal1, ..., i_vCompareToN, i_vRetValN, 
	i_vDefaultRetVal
	*/)
{
	if (arguments.length < 3)
		return;
	
	var vCompare = arguments[0];
	var vCompareTo, vRetVal;
	
	// go through <compare to, return value> pairs. if a match is found return the
	// following return value
	for (var i = 1; i + 1 < arguments.length; i += 2) {
		vCompareTo = arguments[i];
		vRetVal = arguments[i + 1];
		if (vCompare == vCompareTo)
			return vRetVal;
	}
	
	// return optional default return value
	return arguments[i];
}

function debug()
{
	var str = '';
	do {
		str = prompt('input exp:',str);
		if (str)
			alert(str + ': ' + eval(str));
	} while(str.length > 0);
}

// get html elements offset top and left relative to window
// function returns an object with left and top properties
function getElementOffset(oElement)
{
	var oReturnObj = new Object();
	
	oReturnObj.left = 0;
	oReturnObj.top = 0;
	while(oElement) {
		oReturnObj.left += oElement.offsetLeft;
		oReturnObj.top += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	
	return oReturnObj;
}

// handle ilegal chars in url search parameter's value (?param1=value1&param2=value2)
// ilegal chars are: '&', '+', ' ', ','
// ilegal chars are replaced using the myEscape function
// cannot use the escape function since it converts hebrew characters to utf-8
function urlParam(str)
{
	var tmp = ''
	if (typeof(str) == 'string') {
		for (var i = 0; i < str.length; i++)
			tmp += myEscape(str.charAt(i))
	}
	return tmp
}

function myEscape(ch)
{
	switch(ch) {
	case '&':
	case ' ':
	case ',':
	case '=':
	case '?':
	case '%':
	case '#':
		return escape(ch);
	case '+':
		return '%2b'; // escape('+') == '+' and '+' converted to ' ' in url
	default:
		return ch;
	}
}