function debug(arr) {
	var debug = '';
	var flag = 0;
	for (var i in arr) {
		debug = debug + i + '=' + subdebug(arr[i], '    ') + '\n';
		flag = 1;
	}
	if (flag == 0)
		debug = arr;
	alert(debug);
}

function subdebug(arr, prefix) {
	var debug = '';
	if (typeof(arr) == 'function')
		debug += 'function';
	else if (typeof(arr) == 'object') {
		for (var i in arr) {
			debug += '\n' + prefix + i + '=' + subdebug(arr[i], prefix + '    ');
		}
		debug += '\n';
	}
	else
		debug += arr;
	return debug;
}

function ajaxGetParam(text, find) {
	var sIndex = text.indexOf('^&^' + find + '=');
	if (sIndex >= 0) {
		sIndex = sIndex + find.length + 4;
		var search = text.substr(sIndex);
		
		var eIndex = search.indexOf('^&^');

		search = search.substr(0, eIndex);
		var t = ajaxGetAssoc(search);
		
		return t;
		}
	return null;
}

function ajaxGetAssoc(text) {
	var resultArray = new Array();
	var resultAssoc = new Array();

	var temp = text;
	
	var i =0;
	while (temp != '') {
	 i = i +1;
		if (temp.indexOf('^_^') == 0) {
			var name = temp.substr(3, temp.indexOf('=') - 3);
			var tcloseind = temp.indexOf('=' + name + '^_^');
			var closeind = tcloseind;
			var i = 1;
			var tstartind = temp.substr(1).indexOf('^_^' + name + '=');
			var startind = tstartind;
			while (tstartind > 0) {
				tstartind = temp.substr(startind+2).indexOf('^_^' + name + '=');
				tcloseind = temp.substr(closeind+2).indexOf('=' + name + '^_^');
				
				startind += tstartind + 2;
				closeind += tcloseind + 2;
				i++;
				if (i==5)
					break;
			}
			
			var newtemp = temp.substr(4+name.length, closeind - 4 - name.length);
			resultAssoc[name] = ajaxGetAssoc(newtemp);
			temp = temp.substr(closeind + 4 + name.length);
		}
		else if (temp.indexOf('||') != -1) {
			resultArray = temp.split('**');
			var q = resultArray[0].split('||');
			var otemp = temp;
			temp = temp.substr(resultArray[0].length+2);
			if (q[0] != null && q[1] != null)
				resultAssoc[q[0]] = q[1];
		}
		else {
			resultAssoc = temp;
			temp = '';
		}
	}			
		
	return resultAssoc;
}

function newOpt(inText,inValue) {
  var myOpt = new Option(inText,inValue);
  return myOpt;
}

function ajaxOptionsAdd(assoc, selectID) {
	if (assoc == -1)
		return;
	for (i=document.getElementById(selectID).length-1; i>=0; i--) 
		document.getElementById(selectID).options[i] = null;
	var size = 0;
	for (var key in assoc) {
		if (typeof(assoc[key]) != 'function' && assoc[key] != '' && key.indexOf('array') == -1) {
			document.getElementById(selectID).options[document.getElementById(selectID).length] = newOpt(assoc[key], key);
			size ++;
		}
	}
	if (size < 4 && size > 1)
		document.getElementById(selectID).size = size;
	else if (size == 1)
		document.getElementById(selectID).size = 2;	
	else if (size == 0) {
		document.getElementById(selectID).style.display = 'none';
		return;
	}
	else 
		document.getElementById(selectID).size = 4;	
	document.getElementById(selectID).options[0].selected = true;

	document.getElementById(selectID).style.display = 'block';
}

function setSelectOptions(assoc, selectID) {
	var oldval = document.getElementById(selectID).value;
	for (i=document.getElementById(selectID).length-1; i>=0; i--) 
		document.getElementById(selectID).options[i] = null;
	if (typeof(assoc) == 'object')
		for (var key in assoc)
			if (typeof(assoc[key]) != 'function' && assoc[key] != '' && key.indexOf('array') == -1) {
				document.getElementById(selectID).options[document.getElementById(selectID).length] = newOpt(assoc[key], key);
				if (key == oldval)
					document.getElementById(selectID).options[document.getElementById(selectID).length-1].selected = true;
			}
}

function convertMoney(Money) {
	Money = Math.round(Money * 100) / 100;
	return (Money == Math.floor(Money)) ? Money + '.00' : ((Money*10 == Math.floor(Money*10)) ? Money + '0' : Money);
}

function convertMoney2(Money) {
	Money = Math.round(Money);
	return (Money == Math.floor(Money)) ? Money + '.00' : ((Money*10 == Math.floor(Money*10)) ? Money + '0' : Money);
}


function compareMoney(Money1, Money2) {
	Money1 = convertMoney(Money1) + '';
	Money2 = convertMoney(Money2) + '';
	for (i=Money1.length; i<Money2.length; i++)
		Money1 = '0' + Money1;
	for (i=Money2.length; i<Money1.length; i++)
		Money2 = '0' + Money2;
	if (Money1 > Money2) 
		return -1;
	else if (Money1 == Money2)
		return 0;
	return 1;
}

var oryginalWindowOpen = window.open;

window.open = function (Page, WindowName, Parameters, DoNotCloseOpenedWindows) {
	if (!DoNotCloseOpenedWindows)
		for (var i=0; i<OpenedWindows.length; i++) {
			if (OpenedWindows[i] != null && !OpenedWindows[i].closed)
				OpenedWindows[i].close();
		}
	var re = new RegExp('width\\=(\\w*)');
	var m = re.exec(Parameters);
	width=m[1];
	var re = new RegExp('height\\=(\\w*)');
	var m = re.exec(Parameters);
	height=m[1];
	newwindow = oryginalWindowOpen(Page, WindowName, Parameters + ',left=' + parseInt((screen.width - width) / 2) + ',top=' + parseInt((screen.height - height) / 2));
	OpenedWindows[OpenedWindows.length] = newwindow;
	return newwindow;
}

function createNoteIcon(ID, Title, OnClick, Href, OnMouseOver, OnMouseOut) {
	preloadImage(imagesPath + ID + '_0.png');
	preloadImage(imagesPath + ID + '_1.png');
	preloadImage(imagesPath + ID + '_2.png');
	document.write('<a id="anchor_' + ID + '" href="' + Href + '" onClick="' + OnClick + '">')
	document.write('<img id="' + ID + '" border=0 src="' + imagesPath + ID + '_0.png" onMouseOver="this.src=\'' + imagesPath + ID + '_1.png\'; ' + OnMouseOver + '" onMouseOut="this.src=\'' + imagesPath + ID + '_0.png\'; ' + OnMouseOut + '" onMouseDown="this.src=\'' + imagesPath + ID + '_2.png\';" onMouseUp="this.src=\'' + imagesPath + ID + '_0.png\';" title="'+Title+'" alt="'+Title+'">');
	document.write('</a>')
}	

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function stringToArray(text) {
	var arr = new Array();
	while (text.substring(0,1)) {
		arr[arr.length] = text.substring(0,1);
		text = text.substr(1);
	}
	return arr;
}

function key(eventKey) {
	eventKey = (eventKey) ? eventKey : ((event) ? event : null);
	if(eventKey) {
		return eventKey.keyCode;
	}
	return null;
}

function showValidation() {
	for (var i=0; i<document.forms.length; i++) {
		for (var j=0; j<document.forms[i].elements.length; j++) {
			alt = document.forms[i].elements[j].getAttribute('alt');
			if (alt != null && alt != '') {
				if (alt.indexOf('blank') == 0 || alt.indexOf('file') == 0 || alt.indexOf('email') == 0 || alt.indexOf('equalto') == 0 || alt.indexOf('selectm') == 0)
					document.forms[i].elements[j].style.backgroundColor = '#ffff9b';
				if (alt.indexOf('number') == 0) {
					var temp = alt.split('|');
					if (temp[4] == 0)
						document.forms[i].elements[j].style.backgroundColor = '#ffff9b';
				}
			}
			required = document.forms[i].elements[j].getAttribute('required');
			if (required != null && required != '') 
				document.forms[i].elements[j].style.backgroundColor = '#ffff9b';
		}
	}
}

function getDateArray(OnlyDate) {
	var temp = OnlyDate.split('-');
	var Date = new Array();
	Date['Year'] = temp[2];
	Date['Month'] = temp[1];
	Date['Day'] = temp[0];
	return Date;
}

function compareDates(Date1, Date2) {
	var d1 = getDateArray(Date1);
	var d2 = getDateArray(Date2);
	if (d1['Year'] > d2['Year'] || (d1['Year'] == d2['Year'] && d1['Month'] > d2['Month']) || (d1['Year'] == d2['Year'] && d1['Month'] == d2['Month'] && d1['Day'] > d2['Day']))
		return -1;
	if (d1['Year'] == d2['Year'] && d1['Month'] == d2['Month'] && d1['Day'] == d2['Day'])
		return 0;
	return 1;
}

function in_array(needle, haystack) {
    var found = false;
 
    for (key in haystack) {
        if (haystack[key] == needle) {
            found = true;
            break;
        }
    }
 
    return found;
}

function onlyDigitsAndDot( eventObj, objObj, minus )
{
	// jezeli ma byc minus -> onKeyPress="return onlyDigitsAndDot(event, this, true);"
	// jezeli tylko dodatnie -> onKeyPress="return onlyDigitsAndDot(event, this);"

	if ( eventObj == null || eventObj == undefined )
		eventObj = ( event ) ? event : null;

	if ( eventObj != null && eventObj != undefined  ) {
		if ( document.all )
			var kCode = eventObj.keyCode;
		else
			var kCode = eventObj.which;
	}
	else
		return false;

	var strVal = objObj.value;

	if ( minus != null && minus != undefined ) {	
		if ( kCode == 45 && strVal.length > 0 )
			return false;
	}
	else {
		if ( kCode == 45 )
			return false;
	}

	if ( kCode == 46 || kCode == 44 ) { 
		if ( ( strVal.indexOf('.') >= 0 || strVal.indexOf(',') >= 0 || strVal.length == 0 ) || ( strVal.length == 1 && strVal.indexOf( '-' ) >= 0 ) )
			return false;
	}

	if ( ( kCode < 48 || kCode > 57 ) && kCode != 46 && kCode != 44 && kCode != 45 && kCode != 8 )
		return false;

	return true;
}