//-------------------------------------------------------
// Координаты курсора мыши
//-------------------------------------------------------
function getScrollPosition() {
        var scrOfX = 0
        var scrOfY = 0
        if(window.pageYOffset && typeof( window.pageYOffset ) == 'number' ) {
                //Netscape compliant         
                scrOfY = window.pageYOffset;         
                scrOfX = window.pageXOffset;     
        } 
        else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                 //DOM compliant         
                 scrOfY = document.body.scrollTop;         
                 scrOfX = document.body.scrollLeft;     
        } 
        else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {         
                //IE6 standards compliant mode         
                scrOfY = document.documentElement.scrollTop;         
                scrOfX = document.documentElement.scrollLeft;     
        }     
        res=new Array(scrOfY, scrOfX)
        return res; 
}

//-------------------------------------------------------
// Логинимся
//-------------------------------------------------------
function loginForm(option) {
        login=document.getElementById("login").value 
        psw=document.getElementById('psw').value
        var error=''
        if (!login) error+='Вы не ввели логин!\n'
        if (!psw) error+='Вы не ввели пароль!\n'
        if (error) alert(error)
        else {
                JsHttpRequest.query(
                        '/functions/login_form.php',
                        {
                                login: login,
                                psw: psw,
                                option: option
                        },
                        function(result, errors) {
                                if (result["error"]) {
                                        document.getElementById("error").style.visibility='visible'
                                        document.getElementById("error").innerHTML = result["error"];
                                        document.getElementById("login").value = '';
                                        document.getElementById("psw").value = '';
                                        document.getElementById("login").focus();
                                }
                                else {
                                        document.getElementById("login_div").innerHTML = result["text"];
                                        if (option=='root') setTimeout("document.location.href='"+site_url+"/root/index'",2000)
                                        else setTimeout("document.location.href='"+site_url+"'",2000)
                                }
                        },
                        false  // do not disable caching
                )
        }
}

//-------------------------------------------------------
// Разлогиниваемся
//-------------------------------------------------------
function logout() {
        document.location.href="/functions/logout.php"
}

//-------------------------------------------------------
// Спраятать div
//-------------------------------------------------------
function closeDiv(divname) {
        document.getElementById(divname).style.visibility='hidden'
}

//-------------------------------------------------------
// Редактировать строчку из таблицы
//-------------------------------------------------------
function editRow(table, id, option, table_desc_path) {
	// Глобальная переменная, чтобы checkForm знала откуда идет редактирование
	edit_style_flag='edit_row';
        //alert('table='+table+', id='+id+', option='+option+', table_desc_path='+table_desc_path)
        JsHttpRequest.query(
                '/functions/edit.php',
                {
                        table: table,
                        id: id,
                        option: option,
                        table_desc_path: table_desc_path
                },
                function(result, errors) {
                        //alert(result["text"])
                        document.getElementById("main").innerHTML = result["text"]

                        // Если редактируем или добавляем, то проверяем нет ли текстовых полей, чтобы инициировать для них TinyMCE
                        if (option!='show' && result["text_fields"]) {
                                split_text=result["text_fields"].split("|")
                                len_split_text=split_text.length-1
                                for (x=0;x<len_split_text; x++) {
                                        initEditor(split_text[x], table, x)
                                }
                                
                        }
                        // Если редактируем или добавляем, то проверяем нет ли полей даты, чтобы инициировать для них календарь
                        if (option!='show' && result["date_fields"]) {
                                split_text=result["date_fields"].split("|")
                                len_split_text=split_text.length-1
                                for (x=0;x<len_split_text; x++) {
					  Calendar.setup(
					    {
					      inputField  : split_text[x],         // ID of the input field
					      ifFormat    : "%d-%m-%Y",    // the date format
					      button      : "calendar_"+split_text[x]       // ID of the button
					    }
					  )
                                }
                                
                        }
                },
                true  // do not disable caching
        )
}
//-------------------------------------------------------
// Удаляем строчку из таблицы
//-------------------------------------------------------
function deleteRow(table, id, table_desc_path) {
	var url=window.location.href
	var str=/root/; 
	// iface - если в url есть /root/ то значит мы в админке.
	iface=str.test(url)?"root":"face"
        if(confirm('Удалить?')) {
        JsHttpRequest.query(
                '/functions/delete.php',
                {
                        table: table,
                        id: id,
                        iface: iface,
                        table_desc_path: table_desc_path
                },
                function(result, errors) {
                        if (result["status"]=='node') {
                        	openNode(result["parent"], 'close', result['div_suffix'], table, table+'_Desc', result['open_func'], 'Context')
                        	openNode(result["parent"], 'open', result['div_suffix'], table, table+'_Desc', result['open_func'], 'Context')
                        }
                        else if (result["status"]=='ok') document.location.href=result["return_url"];
                        else if (result["status"]=='error') alert(result["error_mysql"])
                },
                true  // do not disable caching
        )
        }
}


function initEditor(field, table, x){
  tinyMCE.idCounter=x;
  tinyMCE.execCommand( 'mceAddControl', true, table+'_'+field);
}

function killEditor(field, table) {
  tinyMCE.idCounter=0;
  tinyMCE.execCommand( 'mceRemoveControl', true, table+'_'+field);
}

