okno = null;

function openwin( src, w, h)
{
  ustawienia= "width=" + w + "," +"height=" + h + "," +"innerWidth=" + w + "," +"innerHeight=" + h + ","
 +"toolbar=no," +"location=center," +"directories=no," +"status=no," +"menubar=no," +"scrollbars=auto," +"resizable=no";

	okno = window.open(src,'',ustawienia);
 	okno.focus();
}

function resizeWindowToFit()
{

	/*if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else */if(document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	else return;

	frameWidth += 40;
	frameHeight += 50;
	alert(frameWidth + " - " + frameHeight);
	/*frameWidth += 400;
	frameHeight += 400;*/
	//if (self.screen.width < frameWidth *2 || self.screen.height < frameHeight *2)
	{
		//newWidth = self.screen.width/2;
		//newHeight = self.screen.height/2;
		/*if (document.layers)
		{
			tmp1 = parent.outerWidth - parent.innerWidth;
			tmp2 = parent.outerHeight - parent.innerHeight;
			newWidth -= tmp1;
			newHeight -= tmp2;
		}*/
		parent.window.resizeTo(frameWidth, frameHeight);
		//parent.window.moveTo(self.screen.width/4,self.screen.height/4);
	}

}

function openwincentered( src, w, h)
{
	var l = (screen.width - w) / 2;
	var t = (screen.height - h) / 2;
	if (document.all) 
	{
   		/* the following is only available after onLoad */
   		l = (document.body.clientWidth - w) / 2;
   		t = (document.body.clientHeight - h) / 2;
	}
	else 
	if (document.layers) 
	{
   		l = (window.innerWidth - w) / 2;
   		t = (window.innerHeight - h) / 2;
	}
		
  ustawienia= "width=" + w + "," +"height=" + h + "," +"innerWidth=" + w + "," +"innerHeight=" + h + ","
 +"toolbar=no," +"location=no," +"directories=no," +"status=no," +"menubar=no," +"scrollbars=yes," +"resizable=yes"
 +",left=" + l + ",top=" + t;

	okno = window.open(src,'',ustawienia);
 	okno.focus();
}


function gotourl(s)
{
	var gourl = s.options[s.selectedIndex].value;
	if (s.selectedIndex != 0)
		location.href = gourl;

}
		

function validateForDelete()
{
 for(i=0; i < document.form.elements.length;i++)
 {
   if (document.form.elements[i].type == "checkbox")
   {
     if (document.form.elements[i].checked)
     {
		var agree=confirm("Are you sure you want to delete this element(s)?");
    	if (agree)
			return true ;
    	else
			return false ;
      }
   }
 }
 alert("You must check at least one checkbox");
 return false;
}


function validateForAction()
{
 for(i=0; i < document.form.elements.length;i++)
 {
   if (document.form.elements[i].type == "checkbox")
   {
     if (document.form.elements[i].checked)
     {
		return true ;
      }
   }
 }
 alert("You must check at least one checkbox");
 return false;
}


function operateOnEntries(script, w, h)
{
	if (validateForAction())
	{
		var s = '';
	 	for(i=0; i < document.form.elements.length;i++)
 		{
 				
   			if ((document.form.elements[i].type == "checkbox") &&
   				(document.form.elements[i].name == "e[]") &&
   				(document.form.elements[i].checked))
   				{
   					s += '&e[]=' + document.form.elements[i].value;
   				}
   		}
   		s = script + s;
   		openwincentered(s, w, h);
	}
	return false;
}

function deleteEntries(script, w, h)
{
	if (validateForDelete())
	{
		var s = '';
	 	for(i=0; i < document.form.elements.length;i++)
 		{
 				
   			if ((document.form.elements[i].type == "checkbox") &&
   				(document.form.elements[i].name == "e[]") &&
   				(document.form.elements[i].checked))
   				{
   					s += '&e[]=' + document.form.elements[i].value;
   				}
   		}
   		s = script + '?id=delete' + s;
   		openwincentered(s, w, h);
	}
	return false;
}

function deleteEntriesOld(id, tab)
{
	if ((tab != '') && (validateForDelete()))
	{
		document.form.action = 'index.php?id=' + id + '&delete=true&tab=' + tab;
		document.form.target = '';
		document.form.submit();
	}

}

function clearCheckBoxes()
{
	for(i=0; i < document.form.elements.length;i++)
 	{
   		if (document.form.elements[i].type == "checkbox")
			document.form.elements[i].checked = false;
	}
}

function selectAll()
{
 	var ids = [];
	var cnt = 0;
 	// collect ids for update
 	var els = document.getElementsByName("e[]");
	for(i=0; i < els.length;i++)
	{
		if ((els[i].type == "checkbox") &&
			(els[i].name == "e[]"))
		{
			els[i].checked = true;
		}
	}
}

function deselectAll()
{
 	var ids = [];
	var cnt = 0;
 	// collect ids for update
 	var els = document.getElementsByName("e[]");
	for(i=0; i < els.length;i++)
	{
		if ((els[i].type == "checkbox") &&
			(els[i].name == "e[]"))
		{
			els[i].checked = false;
		}
	}

}


function applyFilter()
{
	document.form.action = window.location.href;
	if (isInteger(document.form.filter_epp, true))
	{
		document.form.submit();
	}
	//window.location.reload();
}

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
            field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
        if (txtField) { 
            txtField.innerHTML = field.value.length;
        }
}

