/*document.write("<script type='text/javascript' src='/js/ui/ui.dialog.js'></script>");*/
document.write("<script type='text/javascript' src='/js/ui/ui.core.js'></script>");
document.write("<script type='text/javascript' src='/js/ui/ui.datepicker.js'></script>");
document.write("<script type='text/javascript' src='/js/ui/ui.datepicker-ru.js'></script>");

function getPageSize() {
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
                xScroll = window.innerWidth + window.scrollMaxX;
                yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) {	// all except Explorer
                if(document.documentElement.clientWidth){
                        windowWidth = document.documentElement.clientWidth;
                } else {
                        windowWidth = self.innerWidth;
                }
                windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
                pageHeight = windowHeight;
        } else {
                pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
                pageWidth = xScroll;
        } else {
                pageWidth = windowWidth;
        }
        pageHeight+=210;
        return [pageWidth,pageHeight];
}

function empty( mixed_var ) {    // Determine whether a variable is empty
        return ( mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined );
}

/* ================= JQuery ======================== */
$(document).ready(function() {

     /**************************************************
	 ***              'INVOICE' script              ***
	 **************************************************	*/

        /** Перезагружаем список маршрутов в зависимости от */
        function chRoute(__client) {
                $.get('index.php',{
                        'callback':'',
                        'tool':'invoice.route',
                        'act':'view',
                        'invoice_id': $('#inv_id001').val(),
                        'client_id': __client
                }, function(data) {
                        $('#commission').html(data);
                });
        }
	

        function chkReady() {
        //alert($('input#f_priznak').val()); // RQ-0030526
        }

        /* **** BEGIN **** invoice: блок "Доверенность" **** BEGIN **** */

        //// помечаем блок "Доверенность" как изменённый (надпись + граница становится красной.)
        function chCommission() {
                $("div#commission_change span").css("visibility","visible");
                $("div#commission").css("border-color","#FF0000");
        }

        function ClearRoute() {
                document.f01.f_route_out.value = '';
                document.f01.f_route_title.value = '';
                document.f01.act.value='edit';
                document.f01.submit();
        }


        // получаем содержимое области "Доверенность"
        $.get('index.php', {
                'callback':'',
                'tool':'invoice.commission',
                'act':'view',
                'invoice_id':$('#inv_id001').val()
                }, function(data) {
                $('#commission').html(data);

                // ПризнакЧП=1, ""Юр. лицо"", ?(ПризнакЧП=2, ""Физ. лицо"", ?(ПризнакЧП=3, ""Предприниматель"
                chkReady();
  
        });

        // при изменении полей НОмер и Имя, помечаем блок как изменённый
        $('input#fp_number').live("keyup", function() {
                chCommission(); chkReady();
        });
        $('input#fp_name').live("keyup", function() {
                chCommission(); chkReady();
        });

        // при наезде мышкой на поле "доверенность от", открывается календарь
        $('input#fp_date_from').live("mouseover", function() {
                $("input#fp_date_from").datepicker({
                        changeMonth: true,
                        changeYear: true
                });
                $('input#fp_date_from').datepicker('option', {
                        dateFormat: "dd.mm.yy"
                });
                $('input#fp_date_from').datepicker('option', $.extend({
                        showMonthAfterYear: false
                }, $.datepicker.regional['ru']));
        });
        // при выборе даты помечаем блок как изменённый
        $('input#fp_date_from').live("change", function() {
                chCommission();
        });

        // при наезде мышкой на поле "доверенность ПО", открывается календарь
        $('input#fp_date_to').live("mouseover", function() {
                $("input#fp_date_to").datepicker( {
                        changeMonth: true,
                        changeYear: true
                });
                $('input#fp_date_to').datepicker('option', {
                        dateFormat: "dd.mm.yy"
                });
                $('input#fp_date_to').datepicker('option', $.extend({
                        showMonthAfterYear: false
                }, $.datepicker.regional['ru']));
        });
        // при выборе даты помечаем блок как изменённый
        $('input#fp_date_to').live("change", function() {
                chCommission();
        });
/*
        // при выборе маршрута
        $('select#f_shipment_type').live("change", function() {
                ClearRoute();
        });

        $('select#f_agent').live("change", function() {
                ClearRoute();
        });
*/
        // при изменении контрагента помечаем блок как изменённый и проверяем признак
        $('select#id_choose_agent').live("change", function() {
                if ($('input#f_priznak').val()==2) {
                        chCommission();
                }
                ClearRoute();
                chkReady();
                
                $.get('index.php',{
                        'callback':'',
                        'tool':'invoice.commission',
                        'act':'save',
                        'invoice_id': $('#inv_id001').val(),
                        'fp_name': $('#fp_name').val(),
                        'fp_priznak1': $('#f_priznak').val(),
                        'fp_number': $('#fp_number').val(),
                        'fp_date_from': $('#fp_date_from').val(),
                        'fp_date_to': $('#fp_date_to').val()
                }, function(data) {
                        $('#commission').html(data);
                        $("div#commission").css("border-color","lightgrey");
                });
                
        });

        // сохранение/обновление блока
        $('#commission_save').live("click", function() {
                $.get('index.php',{
                        'callback':'',
                        'tool':'invoice.commission',
                        'act':'save',
                        'invoice_id': $('#inv_id001').val(),
                        'fp_name': $('#fp_name').val(),
                        'fp_number': $('#fp_number').val(),
                        'fp_date_from': $('#fp_date_from').val(),
                        'fp_date_to': $('#fp_date_to').val()
                }, function(data) {
                        $('#commission').html(data);
                        $("div#commission").css("border-color","lightgrey");
                });
        });
        /* **** END **** invoice: блок "Доверенность" **** END ****  */

        /* **** BEGIN **** invoice: блок "Основное" **** BEGIN **** */
        $('#f_shipment_type').change(function() {
                document.f01.f_route_out.value = '';
                document.f01.f_route_title.value = '';
                document.f01.act.value='edit';
                document.f01.submit();
        });

/*	Диалоговое окно выбора маршрута. 
	$(function(){
		$("div#invoice_route").dialog({
			autoOpen: false
		});
		
		//alert("sfdghghjk");
		
		$("input#sel_route").click(function(){
			$("#invoice_route").dialog("open");
		});	  	
	});
  */
/* **** END **** invoice: блок "Основное" **** END **** */

});
