// Получение ключа и отрисовка формы для авторизации
var ajax = new Ajax();

function addEventHandler(element, type, func) { //unfortunate hack to deal with Internet Explorer's horrible DOM event model <iehack>
	if(element.addEventListener) {
		element.addEventListener(type,func,false);
	}
	else if (element.attachEvent) {
		element.attachEvent('on'+type,func);
	}
}

function ShowForm () {

	if (!(LF = document.getElementById ('LoginForm')))
		return false;

	LF.style.top = document.body.clientHeight/2-LF.offsetHeight/2;
	LF.style.left = document.body.clientWidth/2-LF.offsetWidth/2;


	if (LF.style.display = 'none') {
		LF.style.display = 'block';



		if (window.event) {
			if (LF.fr) {
				LF.fr.style.display="";
			} else {
				LF.style.zIndex= 2;
				LF.fr = document.createElement('iframe');
				LF.fr.style.position= 'absolute';
				LF.fr.style.zIndex= 1;

				document.body.appendChild(LF.fr);
				addEventHandler(LF, 'resize', ResizeForm);

			}
		}
	}

	LF.style.top = document.body.clientHeight/2-LF.offsetHeight/2;
	LF.style.left = document.body.clientWidth/2-LF.offsetWidth/2;


	ResizeForm ();

	if (LF.innerHTML)
		return false;


	if (!LF.innerHtml) {
		ajax.sendRequest(null, onResponseLogin, {
		        'rnd': Math.random (1,100000),
			'ajaxAction': 'Login'
		}
		);
	}
  
}			

function ResizeForm () {
	
	if (!(LF = document.getElementById ('LoginForm')))
		return false;
	if (window.event) {
		LF.fr.width= LF.offsetWidth;
		LF.fr.height= LF.offsetHeight;
		LF.fr.style.top= LF.offsetTop;
		LF.fr.style.left= LF.offsetLeft;
	}
}

function HideForm () {

	if (!(LF = document.getElementById ('LoginForm')))
		return false;

	LF.style.display = 'none';
	if (window.event && LF.fr) {
		LF.fr.style.display="none";
	}
	
}

function onResponseLogin(e1) {
	if (!(LF = document.getElementById ('LoginForm')))
		return false;

	LF.innerHTML = e1.html;
	if (e1.auth) {
		setCookie ('_auth_', e1.auth, e1.domain);
		document.location = document.location;
	}
}
function _Send (form, pass) {
	if (!form.elements["PASSWORD"]) {
		alert ("Произошла непредвиденная ошибка!\nОбратитесь к администратору или в службу поддержки!");
		return false;
	}
	var p = form.elements["PASSWORD"].value;

	if (MD5) {
//		form.elements["PASSWORD"].value = MD5(pass+MD5(p));
		ajax.sendRequest(null, onResponseLogin, 
		{
		        'rnd': Math.random (1,100000),
			'ajaxAction': 'Login'
		},
		{
		        'PASSWORD': MD5(pass+MD5(p)),
		        'NAME': form.elements["NAME"].value,
		        'LOGIN': form.elements["LOGIN"].value
		});
		if (LF = document.getElementById ('LoginForm')) {
			LF.innerHTML = "";
		}

		return false;
	} else {
		return false;
	}
}

// Установить куки 
function setCookie(name, value, domain) { 
      var valueEscaped = escape(value); 
      var expiresDate = new Date(); 
      expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); 
      var expires = expiresDate.toGMTString(); 
      var newCookie = name + "=" + valueEscaped + "; path=/; domain="+domain+"; expires=" + expires; 
      if (valueEscaped.length <= 4000) document.cookie = newCookie + ";"; 
//      alert (newCookie);
} 

// Получить куки 
function getCookie(name) { 
      var prefix = name + "="; 
      var cookieStartIndex = document.cookie.indexOf(prefix); 
      if (cookieStartIndex == -1) return null; 
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length); 
      if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length; 
      return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)); 
}