document.onkeydown = KeyEvent;

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function enter() {
	if (document.getElementById("divLogin")) {	
		document.getElementById("formLogin").focus();
		return false;
	}
	var container = document.createElement("div");
	container.setAttribute("id","divLogin");
//  container.style.display = "block";
	var formCode = "";
	formCode = "<table style=\"width: 100%; font-size: 9pt; border: 1px Solid Grey;\" cellpadding=\"2\"><form action=\"/engine/reg.html\" method=\"post\" style=\" margin: 10px; padding: px;\">";
	formCode += "<tr><td style=\"width: 100%; font-weight: bold; text-align: center\" colspan=\"2\">Форма авторизации</td></tr>";
	formCode += "<tr><td>Имя:</td><td><input type=\"text\" id=\"formLogin\" name=\"login[login]\"/><td></tr>";
	formCode += "<tr><td>Пароль:</td><td><input type=\"password\" name=\"login[password]\"/><td></tr>";
	formCode += "<tr><td align=\"center\" colspan=\"2\"><input align=\"center\" type=\"submit\" name=\"btnLogin\" value=\"Вход\" style=\"width: 30%; font-size: 8pt; font-weight: normal;\"/></td></tr>";
	formCode += "</form></table>";
	container.innerHTML = formCode;
  document.body.appendChild(container);
  container.style.position = "absolute";
  container.style.width = "220px"
  container.style.top = ""+((document.body.clientHeight - 150) / 2) + "px";
  container.style.left = ""+((document.body.clientWidth - 220) / 2) + "px";
  container.style.background = "silver";
  container.style.border = "5px Solid #f3f3f3";
	document.getElementById("formLogin").focus();
}

function KeyEvent(event) { 
	if(!document.getElementById) return;
  if(window.event) event=window.event; 
  if(event.ctrlKey) { 
  	var link = null;
    switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
      case 0x27: link=document.getElementById('NextPage'); break;
      case 0x25: link=document.getElementById('PrevPage'); break;
      case 0x26: link=document.getElementById('UpPage'); break;
    	case 0x0D: var T=setTimeout('enter();',1); return false;
    }
    if(link && link.href) document.location.href = link.href;
  }
}

function fillFormDogovor(dogovorType) {
	var editDIV = document.getElementById('dogovorEditDiv');
	if (!editDIV) {
		editDIV = document.body.appendChild(document.createElement("div"));
		editDIV.id = "dogovorEditDiv";
		editDIV.style.position = "absolute";
		editDIV.style.width = "960px";
		editDIV.style.height = "90%";
		editDIV.style.top = "5%";
		editDIV.style.left = ""+((document.body.clientWidth - 960) / 2) + "px";
		editDIV.style.border = "3px solid grey";
		var editIFrame = editDIV.appendChild(document.createElement("iframe"));
		editIFrame.id = "dogovorEditDivIFrame";
		editIFrame.width = "100%";
		editIFrame.height="100%";
		editIFrame.src="/engine/dogovorEditor.php?dogovorType=" + dogovorType;
	} else {
		editDIV.style.display = "block";
	}
}
