var appName = navigator.appName;
var IE = 'Microsoft Internet Explorer';
var isIe = appName == IE;

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");

function exploreObject(obj) {
    debugWindow = window.open('', 'debugWindow', 'menubar=no,width=500,height=350, scrollBars=yes');
    debugWindow.document.write('<table>');
    for (var property in obj) {
        debugWindow.document.write('<tr><td>' + property + '</td><td>' + obj[property] + '</td></tr>');
    }
    debugWindow.document.write('</table>');
    debugWindow.document.close();
}



//Funkcja sprawdza czy ktorakolwiek z opcji radiogroup zostala wybrana
function isRadioSelected(radio){
    for (var i = 0; i < radio.length; i++){
        if (radio[i].checked) {
            return true;
        }
    }
    return false;
}

function unimplemented(functionName) {
    window.alert('Funkcja ' + functionName + ' nie jest jeszcze zaimplementowana!');
}

//wraper dla skrocenia kodu
function getElem(id) {
    return document.getElementById(id);
}

function reportError(functionName, e) {
    window.alert('Error in ' + functionName + ' function!');
    throw e;
}

function hide(id) {
    document.getElementById(id).style.display = 'none';
}

function show(id) {
    var field = document.getElementById(id);
    if (isIe) {
        field.style.display = 'inline';
    } else {
        var display = 'inline';
        if (field.tagName == 'TD') {
            display = 'table-cell';
        } else if (field.tagName == 'TR') {
            display = 'table-row';
        }
        field.style.display = display;
    }
}

function findCity(msg) {
    var field = document.getElementById('regionName');
    field.value = field.value.trim();
    
    if (field.value.length > 2) {
        document.getElementById('findCityForm').submit();
        return true;
    } else {
        alert(msg);
        return false;
    }
}

function findPosX(obj) {
   var curleft = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curleft += obj.offsetLeft;
         curleft -= obj.scrollLeft;
         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;
         curtop -= obj.scrollTop;
         obj = obj.offsetParent;
      }
   } else if (obj.y) curtop += obj.y;
   
   return curtop;
}

function round(value, digits) {
    try {
        var multiple = Math.pow(10, digits);
        return Math.round(value * multiple) / multiple;
    } catch (e) {
        reportError('round', e);
    }
}

function roundAmount(value) {
    try {
        value = round(value, 2);
        var decimal = Math.floor(value);
        var fraction = Math.round((value - decimal) * 100);
        var result = decimal + ".";
    
        if (fraction == 0) {
            result += '00';
        } else {
            if (fraction > 0 && fraction < 10) {
                result += '0' + fraction;
            } else {
                result += fraction;
            }
        }
    
        return result;
    } catch (e) {
        reportError('roundAmount', e);
    }
}

function addToFav(url, label) {
	if (isIe) {
		window.external.AddFavorite(url, label);
	} else {
		window.sidebar.addPanel(label, url, '');
	}
}

function jsLog(msg, on) {
	if (on == null || on == undefined) {
		on = true;
	}
	
	if (on) {
		var w = window.open('', 'logWindow', 'menubar=no,width=' + (window.screen.width - 10)+ ',height=200,resizable=yes,scrollbars=yes,left=0,top=' + (window.screen.height-260));
		if (!w.hasBody) {
			w.document.write('<html><body><table id="mainTable" style="width:100%"></table></body></html>');
			w.document.close();
			w.hasBody = true;
			w.i = 1;
		}
		var now = new Date();
		var color = w.i % 2 ? '#eee' : '#fafafa';
		var tab = w.document.getElementById('mainTable');
		var row = tab.insertRow(tab.rows.length);
		row.style.backgroundColor = color;
		var c = row.insertCell(0)
		c.innerHTML = w.i++;
		c.style.width = '50px';
		row.insertCell(1).innerHTML = msg;
		w.focus();
	}
}

function createElem(type, parent){
	var el = document.createElement(type);
	if (parent != null) {
		parent.appendChild(el);
	}
	return el;
}