/*
####################################################################################################
# GENERAL FUNCTIONS LIBRARY
# Version: 1.0.0 | Last Update: 09 June 2008
####################################################################################################
# Copyright © by Cristian Leonte (www.crissleonte.com)
# All reproduction of this file or it's content or pieces of it's content with individual functionality, in comercial or any other purpose is strictly forbidden without 
# the written consent of the copyright holder. For more information please read the legal documents on the copyright holder's website.
####################################################################################################
*/

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// GET ABSOLUTE POSITION FUNCTION

function getAbsPos (el)
{
	var curleft = curtop = 0;
	
	if (el.offsetParent)
	{
		do
		{
			curleft += el.offsetLeft;
			curtop += el.offsetTop;
		}
		while (el = el.offsetParent);
	}
	
	return [curleft, curtop];
}


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// SET OPACITY FUNCTION

function setOpacity (el, value)
{
	el.style.opacity = value / 100;
	el.style.filter = 'alpha(opacity=' + value + ')';
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// ARRAY TO STRING FUNCTION

function array_to_string (ARR)
{
	if (!ARR) {alert ('ARRAY TO STRING ERROR: Variable is empty.'); return '';}
	else if (!isArray (ARR) && returnOut) return '';
	else if (!isArray (ARR))  {alert ('ARRAY TO STRING ERROR: Variable is not an array.'); return '';}

	var OUT = "[";
	var justOpened = true;
	for (var idx in ARR)
	{
		if (isArray (ARR[idx])) OUT += array_to_string (ARR[idx]);
		else OUT += (justOpened ? '' : '|') + idx + "=>" + ARR[idx];
		justOpened = false;
	}
	OUT += "]";
	
	return OUT;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// ARRAY SIGNATURE FUNCTION

function array_signature (ARR)
{
	return string_signature (array_to_string (ARR));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// STRING SIGNATURE FUNCTION

/*
	This method will return a numeric signature of the character string. This number should be unique for any string taken the string
	contains only unicode characters within the 0-255 unicode range. In other words, as long as they contain the proper unicode characters
	different strings, nomatter how small the difference is, will always return different numbers.
*/

function string_signature (str)
{
	str = str + '';
	var sign = 0;
	for (i = 0; i < str.length; i ++) sign += (i * 255 + i) + str.charCodeAt (i);
	return sign;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// PRINT FUNCTION

function print (strVal, newWin)
{
	var printrwin = window.open ('','Print ' + (newWin ? Math.random () : ''),'height=400,width=500,scrollbars=1');
	var pwin = printrwin.document;
	pwin.write ('<html><head><title>PrintArray</title><style>body{font-family:verdana;font-size:12px;color:#444444;}</style></head><body>');
	pwin.write (strVal);
	pwin.write ('</body></html>');
	pwin.close ();
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// PRINT ARRAY WINDOW FUNCTION

function printrw (ARR, txtmode, returnOut, subCallLevel)
{
	printr (ARR, txtmode, returnOut, subCallLevel, true);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// PRINT ARRAY FUNCTION

function printr (ARR, txtmode, returnOut, subCallLevel, winmode)
{
	if (!ARR) {alert ('PRINTR ERROR: Variable is empty.'); return '';}
	else if (!isArray (ARR) && returnOut) return '';
	else if (!isArray (ARR))  {alert ('PRINTR ERROR: Variable is not an array.'); return '';}

	var nlEl = txtmode ? "\n" : '<br>';
	var arrowEl = txtmode ? "=>" : '=&gt;';
	var identEl = txtmode ? '     ' : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	var level = (subCallLevel > 0) ? parseInt (subCallLevel) : 1;
	var ident = charString (identEl, level);
	
	var OUT = (subCallLevel > 0) ? '' : 'Array' +  nlEl + '(' + nlEl;
	for (var idx in ARR)
	{
		if (isArray (ARR[idx]))
		{
			OUT += ident + '[' + idx + '] ' + arrowEl + ' Array' + nlEl + ident + '(' + nlEl;
			OUT += printr (ARR[idx], txtmode, true, (level + 1));
			OUT += ident + ')' + nlEl;
		}
		else
		{
			OUT += ident + '[' + idx + ']' + arrowEl + ' ' + ARR[idx] + nlEl;
		}
	}
	
	if (!(subCallLevel > 0)) OUT += ")" + nlEl;
	
	if (returnOut) return OUT;
	else
	{
		var printrwin = window.open ('', 'PrintArray ' + (winmode ? '' : Math.random ()), 'height=600,width=400,scrollbars=1');
		var pwin = printrwin.document;
		pwin.write ('<html><head><title>PrintArray</title><style>body{font-family:verdana;font-size:12px;color:#444444;}</style></head><body>');
		pwin.write (OUT);
		pwin.write ('</body></html>');
		pwin.close ();
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// IS ARRAY FUNCTION

function isArray (obj)
{
	if (!obj || !obj.constructor) return false;
	else if (obj.constructor.toString ().indexOf ("Array") == -1) return false;
	else return true;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHAR STRING FUNCTION

function charString (chr, size)
{
	size = size ? size : 0;
	size = parseInt (size);
	var str = '';
	for (var i = 0; i < size; i ++) str += chr;
	return str;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// HEX TO BIN FUNCTION

function hex2bin (hex, toArray)
{
	var bin = '0000';
	if (hex == '0' || hex == 0) bin = '0000';
	else if (hex == '1' || hex == 1) bin = '0001';
	else if (hex == '2' || hex == 2) bin = '0010';
	else if (hex == '3' || hex == 3) bin = '0011';
	else if (hex == '4' || hex == 4) bin = '0100';
	else if (hex == '5' || hex == 5) bin = '0101';
	else if (hex == '6' || hex == 6) bin = '0110';
	else if (hex == '7' || hex == 7) bin = '0111';
	else if (hex == '8' || hex == 8) bin = '1000';
	else if (hex == '9' || hex == 9) bin = '1001';
	else if (hex == 'A') bin = '1010';
	else if (hex == 'B') bin = '1011';
	else if (hex == 'C') bin = '1100';
	else if (hex == 'D') bin = '1101';
	else if (hex == 'E') bin = '1110';
	else if (hex == 'F') bin = '1111';
	
	if (toArray)
	{
		bin = bin.split ('');
		return [parseInt (bin[0]), parseInt (bin[1]), parseInt (bin[2]), parseInt (bin[3])];
	}
	else return bin;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// BIN TO HEX FUNCTION

function bin2hex (bin)
{
	switch (bin)
	{
		case '0000': return '0';
		case '0001': return '1';
		case '0010': return '2';
		case '0011': return '3';
		case '0100': return '4';
		case '0101': return '5';
		case '0110': return '6';
		case '0111': return '7';
		case '1000': return '8';
		case '1001': return '9';
		case '1010': return 'A';
		case '1011': return 'B';
		case '1100': return 'C';
		case '1101': return 'D';
		case '1110': return 'E';
		case '1111': return 'F';
		default: return '0';
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// LIST CHECK CLICK FUNCTION

function list_check_click ()
{
	var lc = document.getElementById ('list_check');
	var el = document.getElementsByTagName ("input");
	for (idx = 0; idx < el.length; idx ++)
	{
		if (el[idx].id.match (/^checkbox_/)) el[idx].checked = lc.checked;
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// SET DEFAULT SELECT FUNCTION

function set_def_select (sel_id, sel_val)
{
	var sel_el = document.getElementById(sel_id);
	for (i=0; i < sel_el.options.length; i++)
	{
		if (sel_el.options[i].value == sel_val)
		{
			sel_el.selectedIndex = i;
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// SET DEFAULT MULTISELECT FUNCTION

function set_def_multiselect (sel_id, sel_values)
{
	if (!sel_values) return false;

	var values = sel_values.split (',');
	
	var sel_el = document.getElementById (sel_id);
	for (i=0; i < sel_el.options.length; i++)
	{
		if (in_array (sel_el.options[i].value, values))
		{
			sel_el.options[i].selected = true;
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// IN ARRAY FUNCTION

function in_array (value, arr)
{
    for (var i = 0; i < arr.length; i ++)
	{
        if (arr[i] == value) return true;
    }
    return false;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// URL FUNCTION

function url (module, page, params)
{
	if (!url_path) 
	{
		alert("Redirection error: url_path not set");
		return false;
	}

	var url_form = "{url_path}/index.php?request:{module}-{page}{&{param}={value}}";
	var url_addr = url_form;
	var param_block = "";
	var param_block_form = null;
	
	// replace url_path
	url_addr = url_addr.replace(/\{url_path\}/, url_path);
	
	// replace module
	url_addr = url_addr.replace(/\{module\}/, module);
	
	// replace page
	url_addr = url_addr.replace(/\{page\}/, page);
	
	// get param block
	var re = new RegExp(/\{[^\{\}]?\{(param|value)\}[^\{\}]?\{(param|value)\}[^\{\}]?\}/);
	var m = re.exec(url_addr);
	if (m == null)
	{
		alert("Redirection error: url_form could not be processed");
		return false;
	}
	else
	{
		param_block_form = m[0];
    }
	
	// extract params
	if (params)
	{
		var PRM = new Array();
		PRM = params.split("&");
		
		// replace params
		for (i = 0; i < PRM.length; i++)
		{
			var aux = PRM[i].split("=");
			block = param_block_form.replace(/\{param\}/, aux[0]).replace(/\{value\}/, aux[1]);
			block = block.substr(1, block.length - 2);
			param_block += block;
		}
	}
	
	// prep for substitution (escape regexp special chars)
	param_block_form_re = param_block_form.replace(/([^a-zA-Z0-9 _><=])/g, "\\$1");
	
	// replace param block
	url_addr = url_addr.replace(eval("/" + param_block_form_re + "/"), param_block);
	
	return url_addr;
}

/*
####################################################################################################
*/