var TimeOut = 120;
var TimeRemain = 0;
var RefreshRate = 2;
var ChildWin = null;

var height = screen.height;
var width = screen.width;
var leftpos = width / 2 - 50;
var toppos = height / 2 - 50;

function encode(str){return escape(str.replace(/ /g,'+'));}

function GetPhoto(what, title) {
 var viewer = 'site/popup/viewer.php';
 var url = viewer+'?pic='+encode(what)+'&desc='+title;
 window.open(url,'pictureWindow','scrollbars=0,resizable=1,location=0,status=1,width=200,height=100,left='+(screen.width/2-100)+',top='+(screen.height/2-50));
}

function userBrowser() {
// возвращает переменную содержащую сведения о браузере пользователя
	var BrowserName = navigator.appName;
	if (BrowserName.indexOf("Internet Explorer") > -1)
		BrowserName = "IE";
	else if (BrowserName.indexOf("Netscape") > -1)
		BrowserName = "NN";
	else if (BrowserName.indexOf("Opera") > -1)
		BrowserName = "Opera";
	else
		BrowserName = "other";
	this.BrowserName = BrowserName;
	var pattern = /\d+.\d+/gi;
	var BrowserVersion = navigator.appVersion;
	BrowserVersion = BrowserVersion.match(pattern);
	var maxBV = 0;
	for (var i = 0; i < BrowserVersion.length; i++) { 
		BrowserVersion[i] = parseFloat(BrowserVersion[i]);
		if (maxBV < BrowserVersion[i]) maxBV = BrowserVersion[i];
	}
	this.BrowserVersion = maxBV;
//	alert(navigator.appName + "\n\r" + navigator.appVersion + "\n\r" + BrowserName + "\n\r" + maxBV);
	return this;
}

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
 var i, j;
 var prompt;
 for (i = selectCtrl.options.length; i >= 0; i--) {
  selectCtrl.options[i] = null; 
 }
 prompt = (itemArray != null) ? goodPrompt : badPrompt;
 if (prompt == null) {
  j = 0;
 } else {
  selectCtrl.options[0] = new Option(prompt);
  j = 1;
 }
 if (itemArray != null) {
  for (i = 0; i < itemArray.length; i++) {
   selectCtrl.options[j] = new Option(itemArray[i][0]);
   if (itemArray[i][1] != null) {
    selectCtrl.options[j].value = itemArray[i][1]; 
   }
   j++;
  }
  selectCtrl.options[0].selected = true;
 }
}

function WinPopup(url, name, wid, hei, menu) {
 var startX = (screen.width-wid)/2;
 var startY = (screen.height-hei)/2;
 menu = (menu)?1:0;
 var winProps = "menubar="+menu+",location=0,status=1,scrollbars=1,resizable=1,left="+startX+",top="+startY+",width="+wid+",height="+hei;
 win = window.open(url, name, winProps);
 win.focus();
}

function WinPopup1(url, name, wid, hei) {
 var startX = (screen.width-wid)/2;
 var startY = (screen.height-hei)/2;
 var startX1 = (screen.width-100)/2;
 var startY1 = (screen.height-100)/2;
 var winProps = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,left="+startX+",top="+startY+",width="+wid+",height="+hei;
 var winProps1 = "left="+startX1+",top="+startY1+",width=1,height=1";
 win = window.open("", name, winProps1);
 win.document.open();
 win.document.writeln("<font size=2>Секундочку..</font>");
 win.document.close();
 win.close();
 win = window.open(url, name, winProps);
}

function TimedPopup(url) {
 windowprops = "left=250,top=250,width=250,height=200";
 if (ChildWin)
     return false;
 ChildWin = window.open(url, "ChildWin", windowprops);
 ResetTimer();
 if (TimeOut && RefreshRate)
    setTimeout("CheckClose();",RefreshRate * 1000);
}

function ResetTimer() {
 TimeRemain = TimeOut;
}

function CheckClose() {
 TimeRemain -= (RefreshRate);
 if (TimeRemain > 0)
    {
    if (ChildWin && ChildWin.closed)
       {
	ChildWin = null;
       }
    else
    if (ChildWin)
       {
	setTimeout("CheckClose();",RefreshRate * 1000);
       }
    }
 else
 if (ChildWin)
    {
     if (ChildWin.closed)
	 ChildWin = null;
     else
	{
         ChildWin.close();
	 ChildWin = null;
        }
    }
}

function subEnt(evt) {
 evt = (evt) ? evt : event;
 var target = (evt.target) ? evt.target : evt.srcElement;
 var form = target.form;
 var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
 if (charCode == 13) {
  form.submit();
  return false;
 }
 return true;
}

function ExPoints() { 
 var str = '';
 if (document.cbCache) {
  var len = document.cbCache.elements.length; 
  for(var i=0; i < len; i++) { 
   var chbox = document.cbCache.elements[ i ]; 
   if( chbox.type == 'checkbox' && chbox.name == 'cbExp[]') {
    if (chbox.checked == true) {
     str += chbox.value;
     str += '|';
    } 
   }
  } 
  document.expSelection.selection.value = str;
 } else return false;
}

function init1() {
 mkspellwin('cdata', 'div');
 mkspellwin('hdr', 'h1');
}

function mkspellwin(classList, tag) {
 var list = document.getElementsByTagName(tag);
 var length = list.length;
 var classArray = classList.split(/\s+/);
 var classes = classArray.length;
 var result = [], i,j;

 for (i = 0; i < length; i++) {
   for (j = 0; j < classes; j++)  {
     if (list[i].className.search('\\b' + classArray[j] + '\\b') != -1) {
       if (list[i].addEventListener) {

         list[i].addEventListener("mouseup",
                                  function(e) {
                                    var mainEvent = e ? e : window.event;
                                    makewin(mainEvent);
                                  },
                                  false); 
         list[i].addEventListener("mousedown",
                                  function(e) {
                                    var mid = document.getElementById('.sendm'); 
                                    mid.style.display = 'none';
                                  },
                                  false);

       } else {

         list[i].attachEvent("onmouseup",
                             function(e) {
                               var mainEvent = e ? e : window.event;
                               makewin(mainEvent);
                             });
         list[i].attachEvent("onmousedown",
                             function(e) {
                               var mid = document.getElementById('.sendm'); 
                               mid.style.display = 'none';
                             });
       }

       break;
     }
   }
 }

 return result;

}

function makewin(mainEvent) {
 var str='';
 if ( window.getSelection )
   str = window.getSelection();
 else if ( document.getSelection )
   str = document.getSelection();
 else if ( document.selection )
   str = document.selection.createRange().text;

 str += '';

 if (str != '') {
          
   var posx, posy;
   if( typeof( window.pageYOffset ) == 'number' ) {
       //Netscape compliant
       posy = mainEvent.clientY+window.pageYOffset;
       posx = mainEvent.clientX+window.pageXOffset;
   } 
   else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                     //DOM compliant
       posy = mainEvent.clientY+document.body.scrollTop;
       posx = mainEvent.clientX+document.body.scrollLeft;
   } 
   else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
       //IE6 standards compliant mode
       posy = mainEvent.clientY+document.documentElement.scrollTop;
       posx = mainEvent.clientX+document.documentElement.scrollLeft;
     }
   str=str.replace(/"/g , "&quot;");
   str=str.replace(/'/g , "&lsquo;");
   var mid=document.getElementById('.sendm');

   var uri=location.href;
   var reg = /\&cid=(\d+)/;
   var cid = reg.exec(uri);

   mid.innerHTML='<form method="post" action="?pn=1013&cid='+cid+'"><table border=0><tr><td><input type="hidden" name="hmis" value="'+str+'"><input type="text" name="tmist" value="'+str+'"></td><td><button type="submit"><img src="images/reply_warn.gif" title="Ошибка в тексте? Отправьте сообщение модератору!"></button></td></tr></table></form>';
   mid.style.display='block';
   mid.style.position="absolute";
   mid.style.top=posy+'px';
   mid.style.left=posx+'px';
   mid.style.border="solid black 1px";
 }

}