var win = false;
var s_win = null;
var t_win = null;

function winOpen(theURL, Name, popW, popH, scroll, resize, spanid, index) {
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	var winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable='+resize+'';
	var type = theURL.replace("_js.php", "");

	if (!win) {
		win = window.open(theURL, Name, winProp);
	}
	else
	{
		if (type != getCookie("type"))
		{
			win = window.open(theURL, Name, winProp);
		}
		else
		{
			win.document.getElementById(type).value = index;
		}
	}
	win.window.focus();
	setCookie("spanid", spanid);
	setCookie("index", index);

	setCookie("type", type);
}

function winOpen_subjects(popW, popH, scroll, resize, spanid, index) {
	var s_winleft = (screen.width - popW) / 2;
	var s_winUp = (screen.height - popH) / 2;
	var s_winProp = 'width='+popW+',height='+popH+',left='+s_winleft+',top='+s_winUp+',scrollbars='+scroll+',resizable='+resize+'';

	if (s_win && s_win.open)
	{
		s_win.document.getElementById("subjects").value = index;
	}
	else
	{
		s_win = window.open("subjects_js.php", "subjects", s_winProp);
	}
	/*
	if (!s_win) {
		s_win = window.open("subjects_js.php", "subjects", s_winProp);
	}
	else
	{
		if (s_win.open) { s_win.document.getElementById("subjects").value = index; }
		else { s_win = window.open("subjects_js.php", "subjects", s_winProp); }
	}*/
	s_win.window.focus();

	setCookie("s_spanid", spanid);
	setCookie("s_index", index);
}

function winOpen_teachers(popW, popH, scroll, resize, spanid, index) {
	var t_winleft = (screen.width - popW) / 2;
	var t_winUp = (screen.height - popH) / 2;
	var t_winProp = 'width='+popW+',height='+popH+',left='+t_winleft+',top='+t_winUp+',scrollbars='+scroll+',resizable='+resize+'';

	if (!t_win) {
		t_win = window.open("teachers_js.php", "teachers", t_winProp);
	}
	else
	{
		if (t_win.open) { t_win.document.getElementById("teachers").value = index; }
		else { t_win = window.open("teachers_js.php", "teachers", t_winProp); }
	}
	t_win.window.focus();

	setCookie("t_spanid", spanid);
	setCookie("t_index", index);
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function setFields(what, spanid, text, index)
{
	spanid = spanid.replace("s_", "");
	spanid = spanid.replace("t_", "");
	var hidden_id = spanid.replace("s", "");
	if (what == 't' && text != '-' && text != 'none')
	{
		text = ShortenName(text);
	}
	window.opener.document.getElementById(spanid).innerHTML = text;
	window.opener.document.getElementById(hidden_id).value = index;
}

function ShortenName(name)
{
	var temp = name.split(' ');
	var text = name.substring(0,1);
	var all = text + '. ' + temp[1];
	return all;
}

function UnsetAll(what)
{
	if (what == 's')
	{
		deleteCookie('s_spanid');
		deleteCookie('s_index');
	}
	else
	{
		deleteCookie('t_spanid');
		deleteCookie('t_index');
	}
	window.close();
}

function addRight()
{
	var selectBox = document.forms['UsersEdit'].rights_list;
	var textBox = document.forms['UsersEdit'].rights;

	if (selectBox.selectedIndex >= 0)
	{
		var r = selectBox.options[selectBox.selectedIndex].value + ";";
		removeRight();
		removeRight("all;");

		if (r == "all;")
    {
      textBox.value = r;
    }
    else
    {
      textBox.value = textBox.value + r;
    }
	}
}

function removeRight(rightt)
{
	var selectBox = document.forms['UsersEdit'].rights_list;
	var textBox = document.forms['UsersEdit'].rights;

	if (selectBox.selectedIndex >= 0)
	{
		var txt = textBox.value;
		var r = selectBox.options[selectBox.selectedIndex].value+";";

		if (rightt) { txt = txt.replace(rightt, ""); } else { txt = txt.replace(r, ""); }		
		textBox.value = txt;
	}
}

function DeleteAlert()
{
  var answer = confirm('Сигурен ли си, че искаш да изтриеш този запис?');
  
  return answer;
}


function getSelectedItem(object)
{
  var sobj = object;
  var val;
  
  if (object.length)
  {
    for (var i=0; i<object.length; i++)
    {
      if (object.item(i).checked) { val = object.item(i).value; break; }
    }
  }
  else
  {
    val = object.value;
  }
  
  return val;
}
