function xajax_updateElement(url, name) {
	new ajax( url+'&amp;{/literal}{$ajax_rand}{literal}',{method: 'GET', update: $(name)});
}
function xajax_updatePostElement(url, name, postData) {
	new ajax( url+'&amp;{/literal}{$ajax_rand}{literal}',{postBody: postData, update: $(name)});
}


function ajaxPost(url, name, postData) {
    $.post(url,{postBody: postData},
    function(data){
        $('#content').html(data.returnValue);
    });

}

function postAjaxForm(url, formName, respName) {
    var form = $("#"+formName);
    var options = {
        target:     '#'+respName,
        url:        url,
        beforeSubmit: validate
    };
    form.ajaxForm(options);
}


function validate(formData) {
    for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
            alert('Pole nie może być puste!');
            return false;
        }
    }
    $("#response").html('<div class="center"><img src="/img/cms/loading.gif"></div>');
    return true;
}

function opisSerweryVPS() {
    $("#serwery").show();
    $("#promocjeVPS").hide();
    $("#specyfikacjaVPS").hide();

    usunStyleSerweryVPS();

    $("#serwerLeft").addClass("activeLeft");
    $("#serwerCenter").addClass("activeCenter");
    $("#serwerRight").addClass("activeRight");
    $("#specyfikacjaLeft").addClass("unactiveLeft");
    $("#specyfikacjaCenter").addClass("unactiveCenter");
    $("#specyfikacjaRight").addClass("unactiveRight");
    $("#promocjeLeft").addClass("promoLeft");
    $("#promocjeCenter").addClass("promoCenter");
    $("#promocjeRight").addClass("promoRight");
}

function specyfikacjaSerweryVPS() {
    $("#serwery").hide();
    $("#promocjeVPS").hide();
    $("#specyfikacjaVPS").show();

    usunStyleSerweryVPS();

    $("#serwerLeft").addClass("unactiveLeft");
    $("#serwerCenter").addClass("unactiveCenter");
    $("#serwerRight").addClass("unactiveRight");
    $("#specyfikacjaLeft").addClass("activeLeft");
    $("#specyfikacjaCenter").addClass("activeCenter");
    $("#specyfikacjaRight").addClass("activeRight");
    $("#promocjeLeft").addClass("promoLeft");
    $("#promocjeCenter").addClass("promoCenter");
    $("#promocjeRight").addClass("promoRight");
}

function promocjeSerweryVPS() {
    $("#serwery").hide();
    $("#promocjeVPS").show();
    $("#specyfikacjaVPS").hide();

    usunStyleSerweryVPS();

    $("#serwerLeft").addClass("unactiveLeft");
    $("#serwerCenter").addClass("unactiveCenter");
    $("#serwerRight").addClass("unactiveRight");
    $("#specyfikacjaLeft").addClass("unactiveLeft");
    $("#specyfikacjaCenter").addClass("unactiveCenter");
    $("#specyfikacjaRight").addClass("unactiveRight");
    $("#promocjeLeft").addClass("activeLeft");
    $("#promocjeCenter").addClass("activeCenter");
    $("#promocjeRight").addClass("activeRight");
}

function usunStyleSerweryVPS() {
    $("#serwerLeft").removeClass();
    $("#serwerCenter").removeClass();
    $("#serwerRight").removeClass();

    $("#specyfikacjaLeft").removeClass();
    $("#specyfikacjaCenter").removeClass();
    $("#specyfikacjaRight").removeClass();

    $("#promocjeLeft").removeClass();
    $("#promocjeCenter").removeClass();
    $("#promocjeRight").removeClass();
}

function addAnotherPerson(domainID) {
    $("#costam".domainID).colorbox({
        transition: "fade",
        speed: 400,
        inline: true,
        href: "#addAnotherPersonForm",
        width:"342px",
        height:"110px",
        overlayClose: false,
        onLoad: function() {
            $("#addAnotherPersonForm").show();
            $(".newInvoiceData").click(function(){
                $("#saveSubmitBtn").show();
                var new_invoice_data = $(this).attr('id');
                new_invoice_data = (new_invoice_data == 'newInvoiceData0') ? 0 : 1;
                $("#invoiceForm").html('<div class="center"><img src="/img/cms/loading.gif"></div>');
                $.get(
                    '/index.php',
                    {event: 'anotherInvoiceData', new_invoice_data:new_invoice_data},
                    function(data) {
                        $("#new_invoice_data_"+new_invoice_data).attr('checked','checked');
                        $("#invoiceForm").html(data);
                        changeCompanyCommon();
                        otherRegion();
                        $("#domainID").val(domainID);
                        if (new_invoice_data == 0) {
                            hideOtherData();
                        }
                        $.fn.colorbox.resize();
                    }
                );
            });
        },
        onComplete: function() {
//            bindSAIDButtonClick();
            var options = {
                target:        '#invoiceForm',   // target element(s) to be updated with server response
                beforeSubmit:  showRequest,  // pre-submit callback
                success:       showResponse  // post-submit callback
            };

            $('#anotherInvoiceForm').ajaxForm(options);
        },
        onCleanup: function() {
            $(".newInvoiceData").unbind("click").click();

            $("#invoiceForm").html('');
            $("#addAnotherPersonForm").hide();
            $("#saveSubmitBtn").hide();
        }
    });
}


function otherRegion() {
    var regionSelectedVal = $('#invoice_region :selected').val();
    if (regionSelectedVal == 'inny') {
        $('#otherRegionRow').show();
        $('#invoice_otherRegion').removeAttr('disabled');
        $('#invoice_otherRegion').removeClass('disabled');
        $('#invoice_region').attr('disabled','disabled');
        $('#invoice_region').addClass('disabled');
        $('#invoice_region_disabled').val('inny');
        $('#invoice_region_disabled').removeAttr('disabled');
    } else {
        $('#otherRegionRow').hide();
        $('#invoice_otherRegion').attr('disabled','disabled');
        $('#invoice_otherRegion').addClass('disabled');
        $('#invoice_region').removeAttr('disabled');
        $('#invoice_region').removeClass('disabled');
        $('#invoice_region_disabled').val('');
        $('#invoice_region_disabled').attr('disabled','disabled');
    }
    $.fn.colorbox.resize();
}

function selectCountry() {
    var selectedVal = $('#invoice_countryCode :selected').val();
    if (selectedVal == 'PL') {
        $('#invoice_region').val('0');
        $('#invoice_region').removeAttr('disabled');
        $('#invoice_region').removeClass('disabled');
        $('#invoice_region_disabled').val('');
        $('#invoice_region_disabled').attr('disabled','disabled');
        $('#invoice_otherRegion').attr('disabled','disabled');
        $('#invoice_otherRegion').addClass('disabled');
    } else {
        $('#invoice_region').val('inny');
        $('#invoice_region').attr('disabled','disabled');
        $('#invoice_region').addClass('disabled');
        $('#invoice_region_disabled').val('inny');
        $('#invoice_region_disabled').removeAttr('disabled');
        otherRegion();
    }
}

function changeCompanyCommon() {
    var businessVal = $("input[name='business']:checked", "#anotherInvoiceForm").val();
    
    if (businessVal == 1) {
        $("#commonName").hide();
        $("#statusCommonName").hide();
        $("#companyName").show();
        $("#statusCompanyName").show();
    } else if (businessVal == 0) {
        $("#companyName").hide();
        $("#statusCompanyName").hide();
        $("#commonName").show();
        $("#statusCommonName").show();
    }
}

function bindSAIDButtonClick() {
    $("#saveAnotherInvoiceDataButton").click(function() {
        var serializedFormData = $("#anotherInvoiceForm").serialize();

        $.post(
            '/index.php?event=anotherInvoiceData',
            {formData: serializedFormData},
            function(data){
                $("#invoiceForm").html(data);
            }
        );

    });
}

// pre-submit callback
function showRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
    return true;
}

// post-submit callback
function showResponse(responseText, statusText, xhr, $form)  {
    $("#invoiceForm").html(responseText);
    changeCompanyCommon();
    otherRegion();
//    selectCountry();
    $.fn.colorbox.resize();
}


function hideOtherData() {
    $("#clientPublish").hide();
    $("#clientCountry").hide();
    $("#clientFax").hide();
    $("#clientRegion").hide();
    $("#otherRegionRow").hide();
}

function closeColorbox() {
    $.fn.colorbox.close();
    location.reload();
}

/**
 * Przenoszenie usług
 */

function transferSelect() {
    if ($("#przenoszenieUslugForm").length) {
        var service = $("input[name='transfer_serviceType']:checked").val()
        var selectDomena = $("#transfer_selectDomena");
        var selectDomenaRow = $("#transfer_selectDomenaRow");
        var selectSerwer = $("#transfer_selectSerwer");
        var selectSerwerRow = $("#transfer_selectSerwerRow");

        if (service == 1) { // wybrana domena
            selectSerwer.attr("disabled", "disabled");
            selectSerwerRow.hide();
            selectDomena.removeAttr("disabled");
            selectDomenaRow.show();
        } else if (service == 2) { // wybran serwer
            selectDomena.attr("disabled", "disabled");
            selectDomenaRow.hide();
            selectSerwer.removeAttr("disabled");
            selectSerwerRow.show();
        }
    }
}

function transferSubmit() {
    var errorObj = $("#transfer_errorMessage");
    var errorText = '';
    var service = $("input[name='transfer_serviceType']:checked").val()
    var selectedDomena = $("#transfer_selectDomena :selected").val();
    var selectedSerwer = $("#transfer_selectSerwer :selected").val();
    var login = $("#transfer_login");
    var loginReply = $("#transfer_loginReply");
    var userName = $("#userName").val();
 
    if (selectedDomena == 0 && service == 1) {
        errorText = '- Nie wybrano domeny!<br />';
        $("#transfer_selectDomena").addClass('redInput');
    } else if (selectedDomena != 0 && service == 1) {
        $("#transfer_selectDomena").removeClass('redInput');
    }

    if (selectedSerwer == 0 && service == 2) {
        errorText += '- Nie wybrano serwera!<br />';
        $("#transfer_selectSerwer").addClass('redInput');
    } else if (selectedSerwer != 0 && service == 2) {
        $("#transfer_selectSerwer").removeClass('redInput');
    }

    if (login.val() == '' && loginReply.val() == '') {
        errorText += '- Proszę wpisać login nowego właściciela!';
        login.addClass('redInput');
        loginReply.addClass('redInput');
    } else if(login.val() == userName && loginReply.val() == userName) {
        errorText += '- Nie można wpisać swojego loginu!';
        login.addClass('redInput');
        loginReply.addClass('redInput');
    } else if (login.val() != loginReply.val()) {
        errorText += '- Loginy nie zgadzają się!';
        login.addClass('redInput');
        loginReply.addClass('redInput');
    } else {
        login.removeClass('redInput');
        loginReply.removeClass('redInput');
    }

    if (errorText != '') {
        errorObj.html(errorText);
        errorWindow("Komunikat:", "transfer_errorMessage");
        return false;
    }

    $("#przenoszenieUslugForm").submit();
}

function errorWindow(titleText, elementID) {
    var errorObj = $("#"+elementID);
    $.fn.colorbox({
        transition: "fade",
        initialWidth: "150px",
        initialHeight: "50px",
        overlayClose: false,
        escKey: false,
        opacity: 0.45,
        title: titleText,
        inline: true,
        href: "#"+elementID,
        onLoad: function() {
            errorObj.show();
        },
        onCleanup: function() {
            errorObj.html(false).hide();
        }
    });
}

function komunikatWyplac() {
    $.fn.colorbox({
        transition: "fade",
        initialWidth: "100px",
        initialHeight: "100px",
        overlayClose: false,
        escKey: false,
        title: "Komunikat",
        inline: true,
        href: "#komunikatWyplac",
        onLoad: function() {
            $("#komunikatWyplac").show();
            $.post(
                '/index.php?event=partnerKomunikatWyplacenia',
                function(data) {
                    $("#wygenerowanyObrot").html(data);
                }
            );
        },
        onCleanup: function() {
            $("#komunikatWyplac").hide();
            location.reload();
        }
    });
}

function zgloszeniePP() {
    var porozumienie_pp = $("#porozumienie_pp:checked").val()
    var reprezentant = $("#imienazwisko").val();
    var regon = $("#regon").val();

    var regon_resp = form_check_text_regon(regon);

    if (!porozumienie_pp) {
        alert('Nie zaakceptowano porozumienia!');
    }

    if (regon == '') {
        alert('Prosz\u0119 wpisać regon!');
        form_highlight("regon");
    } else if (!regon_resp){
        alert('B\u0142ędny regon: '+regon);
        form_highlight("regon");
    }
    if (reprezentant == '') {
        alert('Prosz\u0119 wpisać Imię Nazwisko!');
        form_highlight("imienazwisko");
    }

    if (regon_resp && porozumienie_pp && reprezentant) {
        $("#zgloszenie_pp").submit();
    }
}

function setCheckedDomainOnChange(input_id) {
    var selectedVal = $('#dys_'+input_id+' :selected').val();

    if (selectedVal > 0)
        $("#domain_"+input_id).attr("checked", "checked");
    else
        $("#domain_"+input_id).removeAttr("checked");
}

function setDomainYears(input_id) {
    var inputVal = $("#domain_"+input_id+":checked").val();

    if (inputVal)
        $('#dys_'+input_id).val('1');
    else
        $('#dys_'+input_id).val('0');

}

function showCS(name) {
    if (name == 'lays') {
        $("#lays").show();
        $("#iplex").hide();
    } else if (name == 'iplex') {
        $("#iplex").show();
        $("#lays").hide();
    }
}

function getDomianDelegation(domainID) {
    $.post(
        '/index.php?event=ajaxGetDomainDelegation',
        {domain_id: domainID},
        function(data) {
            $("#delegation_"+domainID).html(data);
        }
    );
}

function zSerwerem() {
    var z_serwerem = $("#z_serwerem:checked").val();
    if (z_serwerem == 'on') {
        $("#z_serwerem_tabela").show();
    } else {
        $("#z_serwerem_tabela").hide();
    }
}

function orderVPS() {
    alert("W celu z\u0142ożenia zamówienia na usługę zostaniesz przeniesiony na strone www.provps.pl");
}

function setServerSale_TimePeriod(item) {
    var selectedVal = $('#'+item.id+' :selected').val();
    
    if (selectedVal != 12) {
        $("#server_sale_11").removeAttr("checked").attr("disabled","disabled");
        $("#server_sale").attr("checked","checked");
        $("#sever_sale_11_tr").hide();
    } else {
        $("#server_sale_11").removeAttr("disabled").attr("checked","checked").show();
        $("#server_sale").removeAttr("checked");
        $("#sever_sale_11_tr").show();
    }
}
