function updatePackage (key) {

    theName = document.getElementById('Product[' + key + '][Name]');
    theSaveName = document.getElementById('Product[' + key + '][SaveName]'); // Oszczedzasz:
    thePrice = document.getElementById('Product[' + key + '][Price]');
    theTotal = document.getElementById('Product[' + key + '][Total]');
    theSave = document.getElementById('Product[' + key + '][Save]');

    uniqueName = document.getElementById('Package[' + key + '][]').options[document.getElementById('Package[' + key + '][]').selectedIndex].value;

    theName.innerHTML = document.getElementById('AddProductList[' + uniqueName + '][Name]').value;
    theTotal.innerHTML = document.getElementById('AddProductList[' + uniqueName + '][TotalPrice]').value;
    theSave.innerHTML = document.getElementById('AddProductList[' + uniqueName + '][SavePrice]').value;
    theSaveName.innerHTML = (theSave.innerHTML != '') ? 'Oszczędzasz: ' : '';

    tmp = '';

    if(document.getElementById('AddProductList[' + uniqueName + '][InitialPrice]').value != '') {
        tmp += document.getElementById('AddProductList[' + uniqueName + '][InitialPrice]').value;
    }

    if(document.getElementById('AddProductList[' + uniqueName + '][InitialPrice]').value != '' && document.getElementById('AddProductList[' + uniqueName + '][CyclicPrice]').value != '') {
        tmp += ' + ';
    }

    if(document.getElementById('AddProductList[' + uniqueName + '][CyclicPrice]').value != '') {
        tmp += document.getElementById('AddProductList[' + uniqueName + '][CyclicPrice]').value + '/' + document.getElementById('AddProductList[' + uniqueName + '][CyclicName]').value;
    }

    thePrice.innerHTML = tmp;
}

var timerID = null;
var timerRunning = false;

function changeText (text, name) {
    document.getElementById(name).innerHTML = text;
}

function smsShowMessage (sid) {
    message = document.sms.message.value;
    divideWords = (document.sms.divideWords.checked) ? 1 : 0;

    windowPopup('/sms.php?UniqueName=sms&action=showMessage&message=' + message + '&divideWords=' + divideWords + '&' + sid,300,250,'no','no','no','yes');
}

function smsGetDateParent () {
    parentform = window.opener.document.sms;

    if(parentform.dateyear.value > 0) {
        document.sms.year.value = parentform.dateyear.value;
        document.sms.month.value = parentform.datemonth.value;
        document.sms.day.value = parentform.dateday.value;
        document.sms.hour.value = parentform.datehour.value;
        document.sms.minute.value = parentform.dateminute.value;
    }
}


function smsChangeDateParent () {
    parentform = window.opener.document.sms;

    year = document.sms.year.value;
    month = document.sms.month.value;
    day = document.sms.day.value;
    hour = document.sms.hour.value;
    minute = document.sms.minute.value;

    parentform.dateyear.value = year;
    parentform.datemonth.value = month;
    parentform.dateday.value = day;
    parentform.datehour.value = hour;
    parentform.dateminute.value = minute;

    datetosend = year;
    datetosend += "-";
    datetosend += month;
    datetosend += "-";
    datetosend += day;

    datetosend += " ";

    datetosend += hour;
    datetosend += ":";
    datetosend += minute;
    datetosend += ":00";

    window.opener.document.getElementById("datetosend").innerHTML = datetosend;

    window.close();
}

function smsChangeDateNow (text) {
    parentform = window.opener.document.sms;

    parentform.dateyear.value = "";
    parentform.datemonth.value = "";
    parentform.dateday.value = "";
    parentform.datehour.value = "";
    parentform.dateminute.value = "";
    window.opener.document.getElementById("datetosend").innerHTML = text;

    window.close();
}

function smsChangeMobileParent (iso, prefix, number) {
    parentform = window.opener.document.sms;
    parentform.number.value = number;
    parentform.iso.value = iso;
    parentform.prefix.value = ' + ' + prefix;

    window.close();
}

function smsChangeMobile (cun_id, prefix, number) {
    document.sms.number.value = number;
    document.sms.prefix.value = ' + ' + prefix;
    document.sms.cun_id.value == cun_id;
}

function smsChangeMobile2 (cun_id, prefix, number) {
    document.sms.number.value = number;
    document.sms.prefix.value = ' + ' + prefix;
    document.sms.cun_id.value = cun_id;
}

function smsChangeCountry2 () {
    cun_id = document.sms.cun_id.options[document.sms.cun_id.selectedIndex].value;

    prefix = "";

    if(cun_id.length > 0) {
        for(i = 0; i < cun2iso.length; i++) {
            if(cun2iso[i][0] == cun_id) {
                prefix = cun2iso[i][1];
                break;
            }
        }

        document.sms.prefix.value = ' + ' + prefix;
    } else {
        document.sms.prefix.value = '';
    }
}

function smsChangeCountry () {
    iso = document.sms.iso.options[document.sms.iso.selectedIndex].value

    prefix = "";

    if(iso.length > 0) {
        for(i = 0; i < iso2prefix.length; i++) {
            if(iso2prefix[i][0] == iso) {
                prefix = iso2prefix[i][1];
                break;
            }
        }
        document.sms.prefix.value = ' + ' + prefix;
    } else {
        document.sms.prefix.value = '';
    }
}

function isWhitespace(c) {
    if (c == ' ') return true;
    if (c == '\n') return true;
    if (c == '\t') return true;
}

function smsChangeMessage (max, prefix, postfix, maxword, charsLeft, totalMessages) {
    divideWords = (document.sms.divideWords.checked) ? 1 : 0;

    if(divideWords == 1) {
        smsChangeMessageDivide (max, prefix, postfix, maxword, charsLeft, totalMessages);
    } else {
        smsChangeMessageSimple (max, prefix, postfix, maxword, charsLeft, totalMessages);
    }
}

function smsChangeMessageDivide (max, prefix, postfix, maxword, charsLeft, totalMessages) {
    current = "";
    word = "";

    message = document.sms.message.value;
    if(postfix > 0) {
        message += " ";
        for(i = 1; i < postfix; i++) message += "+";
    }

    if(message.length <= max) {
        left = max - message.length;
        total = 1;
    } else {
        left = max - prefix;
        total = 1;

        for (i = 0; i < message.length; i++) {
            if (isWhitespace(message.charAt(i))) {
                if (current.length + word.length <= max - prefix) {
                    current += word;
                    left = max - prefix - current.length;
                } else {
                    if (word.length <= maxword) {
                        total++;
                        current = word;
                        left = max - prefix - current.length;
                    } else {
                        current += word;

                        while (current.length > max - prefix) {
                            total++;
                            tmp = current;

                            current = current.substring(max - prefix, current.length);

                            if(current.length > 0) {
                                tmp = current;
                            }
                        }

                        left = max - prefix - tmp.length;
                    }
                }

                if ((current.length > 0) && (current.length < max - prefix)) {
                    current += " ";
                }

                word = "";

            } else {
                word += message.charAt(i);
            }
        }

        if ((word.length > 0) || (current.length > 0)) {
            if (current.length + word.length <= max - prefix) {
                current += word;
                //total++;
                left = max - prefix - current.length;
            } else {
                if (word.length <= maxword) {
                    total++;
                    current = word;
                    left = max - prefix - current.length;
                } else {
                    current += word;
                    tmp = current;

                    while (current.length > 0) {
                        total++;

                        if(max - prefix < current.length) {
                            tmpLength = max - prefix;
                        } else {
                            tmpLength = current.length;
                        }

                        current = current.substring(tmpLength, current.length);

                        if(current.length > 0) {
                            tmp = current;
                        }
                    }

                    left = max - prefix - tmp.length;
                }
            }
        }
    }

    document.getElementById(charsLeft).innerHTML = left;
    document.getElementById(totalMessages).innerHTML = total;
    document.sms.noOfSms.value = total;
}

function smsChangeMessageSimple (max, prefix, postfix, maxword, charsLeft, totalMessages) {
    left = 0;
    total = 0;

    chars = document.sms.message.value.length + postfix;

    if(chars <= max) {
        left = max - chars;
        total = 1;
    } else {
        total = Math.ceil(chars/(max-prefix));
        left = total*(max-prefix) - chars;
    }

    document.getElementById(charsLeft).innerHTML = left;
    document.getElementById(totalMessages).innerHTML = total;
    document.sms.noOfSms.value = total;
}

function openPopupImage(ImageSource) {
    PopupTitle = 'popupImage';
    ImageTitle = 'e.pl';

    theImage = new Image();
    theImage.src = ImageSource;

    if(!theImage.complete) {
        if(timerRunning) clearTimeout(timerID);
        timerRunning = true;
        timerID = setTimeout("openPopupImage('" + ImageSource + "')", 100);
    } else {
        timerRunning = false;
        timerID = null;

        ImageWidth = theImage.width;
        ImageHeight = theImage.height;

        if (document.all) {
            var xMax = screen.width;
            var yMax = screen.height;
        } else {
            if (document.layers) {
                var xMax = window.outerWidth;
                var yMax = window.outerHeight;
            } else {
                var xMax = 640;
                var yMax = 480;
            }
        }

        xOffset = (xMax - ImageWidth) / 2;
        yOffset = (yMax - ImageHeight) / 2;

        var popupFeatures = 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,copyhistory=no,width=' + ImageWidth + ',height=' + ImageHeight + ',screenX=' + xOffset + ',screenY=' + yOffset + ',top=' + yOffset + ',left=' + xOffset

        popupWindow = window.open('', PopupTitle,popupFeatures);

        if ( popupWindow.opener == null ) {
             popupWindow.opener = self;
        }

        popupWindow.focus();

        var popupContent = ""
        popupContent += "\n<html><head><title>" + ImageTitle + "</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-2'></head>"
        popupContent += "\n<body style='margin: 0px; padding: 0px;'>"
        popupContent += "\n<div align='center'>"
        popupContent += "<img id='popupimage'>"
        popupContent += "</div>"
        popupContent += "\n</body>"
        popupContent += "\n</html>"
        popupWindow.document.write( popupContent )
        popupWindow.document.close()

        oPopupImg = popupWindow.document.getElementById('popupimage');
        oPopupImg.src = ImageSource;
        oPopupImg.width = ImageWidth;
        oPopupImg.height = ImageHeight;
    }
}

function redirect (url, milisecond) {
    document.URL = url;
}

function selectColor(value, name, key, path) {
    value = document.getElementById(name + "[" + key + "]").value;
    if(path == '') path = '/';
    windowPopup (path + '_webColor.php?action=select&value=' + value + '&name=' + name + '&key=' + key,300,150,'no','no','yes','yes');
}

function saveColor(value, name, key) {
    opener.document.getElementById(name + "[" + key + "]").value = value;
    opener.document.getElementById(name + "[" + key + "][1]").style.color = value;
    opener.document.getElementById(name + "[" + key + "][1]").innerHTML = "<b>"+value+"</b>";
    opener.document.getElementById(name + "[" + key + "][2]").style.color = value;
    opener.document.getElementById(name + "[" + key + "][2]").innerHTML = "<b>"+value+"</b>";
    window.close();
}

function selectMobileProduct(value, name, key, path) {
    if(path == '') path = '/';
    windowPopup (path + '_webMobileProduct.php?action=select&value=' + value + '&name=' + name + '&key=' + key,400,200,'no','no','yes','yes');
}

function selectHosterProduct(value, name, key, path) {
    if(path == '') path = '/';
    windowPopup (path + '_webHosterProduct.php?action=select&value=' + value + '&name=' + name + '&key=' + key,400,200,'no','no','yes','yes');
}

function openEditor(form, name, path) {
    if(path == '') path = '/';
    url = path + 'HTMLArea/index.php?form=' + form + '&name=' + name;
    windowPopup (url,600,600,'no','no','yes','no')
}

function selectFileWeb(obj, folder, filter, current, path) {
    if(path == '') path = '/';
    url = path+'_selectFile.php?obj=' + obj + '&folder=' + folder + '&filter=' + filter + '&current=' + current;
    windowPopup (url,200,300,'no','no','yes','yes')
}

function showFileWeb(folder, file, path) {
    if(path == '') path = '/';
    url = path+'_showFile.php?folder=' + folder + '&file=' + file;
    windowPopup (url,550,400,'no','no','yes','yes')
}

function selectFile(obj, folder, filter, current, path) {
    if(path == '') path = '/';
    url = path+'_selectFile.php?obj=' + obj + '&folder=' + folder + '&filter=' + filter + '&current=' + current;
    windowPopup (url,200,300,'no','no','yes','yes')
}

function showFile(folder, file, path) {
    if(path == '') path = '/';
    url = path+'_showFile.php?folder=' + folder + '&file=' + file;
    windowPopup (url,550,400,'no','no','yes','yes')
}

function saveFile(obj, file) {
    opener.document.getElementByName(obj).value = file;
    window.close();
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
    document.cookie =
        escape(cookieName) + '=' + escape(cookieValue)
        + (expires ? '; expires=' + expires.toGMTString() : '')
        + (path ? '; path=' + path : '')
        + (domain ? '; domain=' + domain : '')
        + (secure ? '; secure' : '');
};

function getCookie(cookieName) {
    var cookieValue = '';
    var posName = document.cookie.indexOf(escape(cookieName) + '=');
    if (posName != -1) {
        var posValue = posName + (escape(cookieName) + '=').length;
        var endPos = document.cookie.indexOf(';', posValue);
        if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
        else cookieValue = unescape(document.cookie.substring(posValue));
    }

    return (cookieValue);
};

function hideDiv(id, plus, minus) {
    var objDiv = document.getElementById('div_' + id);
    var objTd = document.getElementById('td_' + id);
    var objImg = document.getElementById('img_' + id);

    if(objDiv.innerHTML == "") {
        objDiv.innerHTML = objTd.innerHTML;
        objTd.innerHTML = "";
        objImg.src = plus;

        setCookie('newepl_div_' + id, 0);
    } else {
        objTd.innerHTML = objDiv.innerHTML;
        objDiv.innerHTML = "";
        objImg.src = minus;

        setCookie('newepl_div_' + id, 1);
    }
}

function changeAttr(a_move, a_item, a_delete) {
    document.admin.attr_move.value = a_move;
    document.admin.attr_item.value = a_item;
    document.admin.attr_delete.value = a_delete;
    document.admin.submit();
}

function deleteRowParameters(row) {
    document.admin.deleteRowParameters.value = row;
    document.admin.close.value = 0;
    document.admin.submit();
}

function addRowParameters(array) {
    document.admin.addRowParameters.value = array;
    document.admin.close.value = 0;
    document.admin.submit();
}

function showComponentInstance(unique, path) {
    document.admin.action.value = 'show';
    document.admin.show_UniqueName.value = unique;
    document.admin.submit();
}

function changeComponentInstance(unique, where, path) {
    document.admin.action.value = 'change';
    document.admin.change_UniqueName.value = unique;
    document.admin.change_Where.value = where;

    if(path == '') path = '/';

    windowPopup (path + '_webAdminInstance.php?action=edit&change_Where=' + where + '&unique=' + unique + "&parentUnique=" + document.admin.unique.value,400,200,'no','no','yes','yes');
}

function changeComponentInstanceRoot(id) {
    windowPopup ('componentinstance.php?action=choose2&Cin_ID=' + id,400,300,'no','no','yes','yes');
}

function changeComponentInstanceUpdate (value, type) {
    if(document.admin.Com_ID.selectedIndex > 0) {
        Com_ID = document.admin.Com_ID.options[document.admin.Com_ID.selectedIndex].value;
    } else {
        Com_ID = 0;
    }

    if(type == 1) {
        len = instance_list[Com_ID].length;
        document.admin.UniqueName.length = len;

        for (var i = 0; i < len; i++) {
            document.admin.UniqueName.options[i].text  = instance_list[Com_ID][i][1];
            document.admin.UniqueName.options[i].value = instance_list[Com_ID][i][0];
            if(document.admin.UniqueName.options[i].value == value) document.admin.UniqueName.options[i].selected = true;
        }
    } else {
        len = instance_list[Com_ID].length;
        document.admin.Cin_ID.length = len;

        for (var i = 0; i < len; i++) {
            document.admin.Cin_ID.options[i].text  = instance_list[Com_ID][i][1];
            document.admin.Cin_ID.options[i].value = instance_list[Com_ID][i][0];
            if(document.admin.Cin_ID.options[i].value == value) document.admin.Cin_ID.options[i].selected = true;
        }
    }
}

function webProduct(value, name, key, path) {
    value = document.getElementById(name + "[" + key + "]").value;

    re = /\#/;
    value = value.replace(re, "_");

    if(path == '') path = '/';

    windowPopup (path + '_webProduct.php?action=select&value=' + value + '&name=' + name + '&key=' + key,400,250,'no','no','yes','yes');
}

function changeWebProductUpdate (value, type) {
    Category = document.admin.Category.options[document.admin.Category.selectedIndex].value;

    len = product_list[Category].length;
    document.admin.Product.length = len;

    for (var i = 0; i < len; i++) {
        document.admin.Product.options[i].text  = product_list[Category][i][1];
        document.admin.Product.options[i].value = product_list[Category][i][0];
        if(document.admin.Product.options[i].value == value) document.admin.Product.options[i].selected = true;
    }
}

function webProductSave(name, key) {
    if(document.admin.Product.selectedIndex > 0) {
        opener.document.getElementById(name + "[" + key + "]").value = document.admin.Product.options[document.admin.Product.selectedIndex].value;
    } else {
        opener.document.getElementById(name + "[" + key + "]").value = "";
    }
    window.close();
}

function saveComponentInstance() {
    if(opener.document.admin.change_UniqueNameChange) {
        opener.document.admin.change_UniqueNameChange.value = document.admin.UniqueName.value;
        opener.document.admin.submit();
        window.close();
    } else {
        opener.document.admin.Cin_ID.value = document.admin.Cin_ID.value;
        opener.document.admin.submit();
        window.close();
    }
}

function alert(text,url) {
    if(url == null) {
        window.confirm(text);
    } else {
        if (window.confirm(text)) self.location.replace(url);
    }
}

function textareaedit(object,rows,cols,labelobject,label1,label2) {
    if(object.rows == 1) {
        object.rows = rows;
        object.cols = cols;
        labelobject.innerHtml = label2;
    } else {
        object.rows = 1;
        object.cols = 18;
        labelobject.innerHtml = label1;
    }
}

function adminHide(object,label1,label2) {
    var objDiv = document.getElementById(object);
    var objTd = document.getElementById(object + 'Table');
    var objLabel = document.getElementById(object + 'Label');

    if(objDiv.innerHTML == "") {
        objDiv.innerHTML = objTd.innerHTML;
        objTd.innerHTML = "";
        objLabel.innerText = label2;
    } else {
        objTd.innerHTML = objDiv.innerHTML;
        objDiv.innerHTML = "";
        objLabel.innerText = label1;
    }
}

function windowPopup (url,windowWidth,windowHeight,windowToolbar,windowMenu,windowStatus,windowScroll) {
    PosX = 0; PosY= 0;
    ScrWidth = 640; ScrHeight = 480;
    if (window.screen) {ScrWidth = top.screen.width; ScrHeight = top.screen.height}

    PosX = Math.round((ScrWidth - windowWidth)/2);
    PosY = Math.round((ScrHeight - windowHeight)/2);

    var s = new Date();
    win_name = s.getMinutes();
    win_name += s.getSeconds();

    top.open(url, win_name, "menubar="+windowMenu+",toolbar="+windowToolbar+",location=no,directories=no,status="+windowStatus+",scrollbars="+windowScroll+",resizable=no,copyhistory=no,width="+windowWidth+",height="+windowHeight+",left="+PosX+",top="+PosY);
}