function server_url_prefix() {
  var prefix = '';
  if (playit_server_url_prefix_) {
    prefix = playit_server_url_prefix_;
  }
  return prefix;  
}

function gebi(id){
	return document.getElementById(id)
}

function jsquote(str){
	return str.replace(/'/g,'&#39;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/&/g,'&amp;') //'
}

function open_window(url, title) {
  window.open(
      url,
      title,
      'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes'
    )
}

function open_window_refresh(url, title) {
  setTimeout(function() {location.href = location.href}, 1000);
  open_window(url, title);
}

function check_submit(){
	if(window.start_refresh) return false
	window.start_refresh=true
	return true
}

function confirm_submit(text){
	if(window.start_refresh) return false;
	if (!text) text= "Вы уверены?";
	if(!confirm(text)) {
		return false;
	}
	window.start_refresh=true
	return true
}

function go_url(url){
	if(window.start_refresh) return false
	window.start_refresh=true
	location.href=url
}

function copyBoard(txt){
	if(document.body.createTextRange) {
		var d=document.createElement('INPUT')
		d.type='hidden'
		d.value=txt
		document.body.appendChild(d).createTextRange().execCommand("Copy")
		document.body.removeChild(d)
		return
	} else try {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect')
		var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper)
		gClipboardHelper.copyString(txt)
	} catch (e) {}
}

function error_show(error) {
	var win = window;
	if (win.showModelessDialog) {
		var sFeatures = 'dialogWidth:480px; dialogHeight:340px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
		win.showModelessDialog("error.php?error="+error, win, sFeatures);
	} else {
		win.open("error.php?error="+error, "", "width=480,height=300,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
	}
	return false;
}

function error_show_ex(errmod, errno, lang_id) {
//  var win = window;

	var div = gebi("error_div");
    div.style.display = "";
	div.style.top = document.body.scrollTop + (document.body.clientHeight - div.offsetHeight) / 2;
	div.style.left = document.body.scrollLeft + (document.body.clientWidth - div.offsetWidth) / 2;
	gebi("error_frame_id").src = "/error.php?errmod="+errmod+"&errno="+errno;

//  IE closes modeless dialog after we left its page.
//  if (win.showModelessDialog) {
//    var sFeatures = 'dialogWidth:480px; dialogHeight:340px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
//    win.showModelessDialog(server_url_prefix() + "error.php?errmod="+errmod+"&errno="+errno+"&lang_id="+lang_id, win, sFeatures);
//  } else {
//    win.open(server_url_prefix() + "error.php?errmod="+errmod+"&errno="+errno+"&lang_id="+lang_id, "", "width=480,height=300,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
//  }
  return false;
}

function show_screenshot(screenshot_id) {
  var win = window;
  var url = server_url_prefix() + "screenshot.php?screenshot_id="+screenshot_id;
  open_window(url, '');
  /*
  if (win.showModelessDialog) {
    var sFeatures = 'dialogWidth:860px; dialogHeight:700px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
    win.showModelessDialog(url, win, sFeatures);
  } else {
    win.open(url, "", "width=860,height=700,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
  }
  */
  return false;
}

function vote_show(project_id) {
  var win = window;
  var url = server_url_prefix() + "vote.php?project_id="+project_id;
  var width = 300;
  var height = 500;
//  if (win.showModelessDialog) {
//    var sFeatures = 'dialogWidth:'+width+'px; dialogHeight:'+height+'px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
//    win.showModelessDialog(url, win, sFeatures);
//  } else {
    win.open(url, "", "width="+width+",height="+height+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
//  }
  return false;
}

function account_info_show(nick) {
  var win = window;
  var url = server_url_prefix() + "accountinfo/"+nick;
  var width = 1000;
  var height = 700;
  win.open(url, "", "width="+width+",height="+height+",location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showMsg(url, title, w, h) {
	w=w||480
	h=h||300
	var win = top.window;
	if (win.showModelessDialog) {
		var sFeatures = 'dialogWidth:' + w + 'px; dialogHeight:' + h + 'px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
		return win.showModelessDialog("msg.html", {win: win, src: url, title: title}, sFeatures);
	} else {
		return win.open(url, "", 'width=' + w + ',height=' + h + ',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
	}
}

//	Encodes string to win1251 instead of Unicode/UTF-8.
//	Borrowed from http://xpoint.ru/forums/thread/21220.xhtml.
//	Note this would be slooow on large strings.
function escape_1251(str) {
	var ret = '';
	for (i = 0; i < str.length; i++) {
		var n = str.charCodeAt(i);
		if (n >= 0x410 && n <= 0x44F) {
			n -= 0x350;
		} else if (n == 0x451) {
			n = 0xB8;
		} else if (n == 0x401) {
			n = 0xA8;
		}

		if ((n < 65 || n > 90) && (n < 97 || n > 122) && (n < 48 || n > 57) && n < 256) {
			if (n < 16) {
				ret += '%0'+n.toString(16);
			} else {
				ret += '%'+n.toString(16);
			}
		} else {
			ret += String.fromCharCode(n);
		}
	}

	return ret;
}

function checkbox_set(pfx, val) {
  var chk=document.getElementsByTagName('INPUT');
  for(var i=0;i<chk.length;i++){
    if(chk[i].name.indexOf(pfx)==0 || chk[i].getAttribute('grp')==pfx){
      chk[i].checked = (val == undefined ? !chk[i].checked: val);
    }
  }
}

function window_opener_reload() {
  if (window.opener && !window.opener.closed) {
    window.opener.location.href = window.opener.location.href;
  }
}

function self_reload() {
  location.href = location.href;
}

function open_href_in_opener() {
  var href_not_handled = true; 
  try {
    if (this && this.getAttribute) {
      var href = this.getAttribute('href');
      if (!this.getAttribute('target') && !window.opener.closed) {
        window.opener.location.href = href;
        href_not_handled = false;
      }
    }
  } catch(e) {
    //  Do nothing
  }
  return href_not_handled;
}

function common_hrefs_to_opener() {
  var links = document.getElementsByTagName('A');
  for (var i in links) {
    var link = links[i];
    if (!link.onclick) {
      link.onclick = open_href_in_opener;
    }
  }
}
