// дом готов для выполнения js
$(document).ready(function(){
	SG.requestUserInfo();

	if (typeof pub_url == "undefined") {
    	pub_url = location.href;
  	}
	if (typeof pub_name == "undefined") {
		pub_name = document.title
	}

   	$("#bookmarkme").click({page_url: pub_url, title: pub_name}, add_to_favorite);

});


function isChrome() {
    return Boolean(window.chrome);
}

function add_to_favorite(event) {

	var page_url = event.data.page_url;
	var title = event.data.title;
 	if (window.sidebar) { // Mozilla Firefox	 
		window.sidebar.addPanel(page_url,title,"");
		return true;
    } 
    else if(isChrome() == true) { // Google Chrome
		alert('Пожалуйста, нажмите Ctrl+D, чтобы добавить страницу в закладки');
		return false;
	}
	
	else if(window.external) { // IE
		window.external.AddFavorite(page_url,title);	
		return false;
	}
	
    else if(window.opera && window.print) { // Opera
    	this.title=title;
    	return true;
 	}
	
	alert('Пожалуйста, нажмите Ctrl+D, чтобы добавить страницу в закладки');
	return false;	

}

/*******************************************/
/* получить элемент по его id **************/
/*******************************************/
function getObj (id) {
	var obj = document.getElementById(id);
	if(obj){
		return obj;
	}
	else{
		//alert("ID = "+id);
		return null;
	}
}

/*******************************************/
/* спрятать элемент ************************/
/*******************************************/
function hideObj(obj) {
	obj.style.display = 'none';
}

/*******************************************/
/* показать элемент  ***********************/
/*******************************************/
function showObj(obj) {
	obj.style.display = 'block';
}


function SG(){
}

// секунды для сбрасывани кеша
SG.getMiliSeconds = function() {
	var Miliseconds = new Date();
	var x = Miliseconds.getTime();
	return x;
};



/************************************************************/
/* Проверка полей формы на пустоту  *************************/
/************************************************************/
function checkFields(nameForm) {

	var LoginFromInput = document.forms[nameForm].elements['uname'].value;
	var PasswordFromInput = document.forms[nameForm].elements['upsw'].value;
	
	if (LoginFromInput == '' && PasswordFromInput == '') {
		alert('Введите логин и пароль!');
		return false;
	}
	else {
		if (LoginFromInput == '') {
			alert('Введите логин!');
			return false;
		}
		else {
			if (PasswordFromInput == '') {
				alert('Введите пароль!');
				return false;
			}
			else {
				return true;
			}
		}
	}
}


// инфо о пользователе
SG.User = {
	clienttype: "NotAuthorized",
	accesstype: "NotAuthorized"
};

SG.requestUserInfo = function(){
	clearTimeout(SG.timergetUserInfo);
	$.ajax({
	    type:"GET",
	    url: "/getUserInfo-XML.aspx?x="+SG.getMiliSeconds(),
		dataType: "xml",
		timeout: 5000,
	    success: function(xml){
			//alert($(xml).find('clienttype').text());
			var error = $(xml).find('error').text();
			if(error == ""){
			
				SG.User.clienttype = $(xml).find('clienttype').text();
				SG.User.accesstype = $(xml).find('accesstype').text();
				
				// 1 :: показ имени пользователя
				if(SG.User.clienttype != 'NotAuthorized'){
					var today=new Date();
					getObj("clientname").innerHTML = $(xml).find('clientname').text();
					// + " | " + (today.getHours() + ":" + today.getMinutes()+ ":" +today.getSeconds())
					showObj(getObj("user"));
				}
				else {
					hideObj(getObj("user"));
				}
				
				// 2 :: переход в режим обновления
				var isSysUpdate = $(xml).find('MaintenanceOn').text();
				// переходим на страницу обновлений
				if(isSysUpdate == 'true'){
					if(window.location.pathname != ('/updatedb.html')){
						window.location.pathname = '/updatedb.html';
					}
				}
				// если рабочий режим
				else{
					if(window.location.pathname == ('/updatedb.html')){
						window.location.pathname = '/';
					}
				}
			}
			
			// 3 ::  повторный вызов инфо о юзере
			SG.timergetUserInfo = window.setTimeout("SG.requestUserInfo()", 60000);
	    },
		error: function(){
			SG.timergetUserInfo = window.setTimeout("SG.requestUserInfo()", 5000);
			//alert("ошибка в SG.timergetUserInfo");
		}
	});
};







