var win=null;

function openPopup(myname,mypage,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
		else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
	win.focus();
}
function move(obj,step){
	// used to order combo items 
	d=document.getElementById(obj);
	min=0;
	max=d.options.length-1;
	current=d.selectedIndex;
	if ((current+step>=0)&&(current+step<=max))
	{
		var tmp_value=d.options[current].value;
		var tmp_text=d.options[current].text;
		d.options[current].value=d.options[current+step].value;
		d.options[current].text=d.options[current+step].text;
		d.options[current+step].value=tmp_value;
		d.options[current+step].text=tmp_text;
		d.selectedIndex=current+step;
	}		
}
function moveComboItem(objFrom,wFrom,objTo,wTo) {
	// from combo a to combo b
	if (!wFrom)	{
		var oFrom = document.getElementById(objFrom);
	} else {
		var oFrom = window.opener.document.getElementById(objFrom);
	}
	if (!wTo)	{
		var oTo = document.getElementById(objTo);
		var objChild = document;
	} else {
		var oTo = window.opener.document.getElementById(objTo);
		var objChild = window.opener.document;
	}	

	for (var j=0;j<oFrom.options.length;j++ )	{
		if (oFrom.options[j].selected){
			customAddComboItem(objChild,oTo,oFrom.options[j].value,oFrom.options[j].text,0);
			/*
			var newOpt = objChild.createElement("option");
			newOpt.setAttribute("value",oFrom.options[j].value);
			var texto = objChild.createTextNode(oFrom.options[j].text);
			newOpt.appendChild(texto);

			newOpt.text = oFrom.options[j].text;
			newOpt.value = oFrom.options[j].value;
			var sw = true;
			for (var i=0;i<oTo.options.length;i++ ){
				if (oTo.options[i].value == newOpt.value) {
					sw = false;
					break;
				}
			}
			if (sw)	{
				oTo.appendChild(newOpt);
			}
			*/
		}
	}
}
function customAddComboItem(objChild,obj,id,value,selected) {
			var newOpt = objChild.createElement("option");
			newOpt.setAttribute("value",id);
			var texto = objChild.createTextNode(value);
			newOpt.appendChild(texto);

			newOpt.text = value;
			newOpt.value = id;
			var sw = true;
			for (var i=0;i<obj.options.length;i++ ){
				if (obj.options[i].value == newOpt.value) {
					sw = false;
					break;
				}
			}
			if (sw)	{
				obj.appendChild(newOpt);
			}
}
function select_all(obj,wObj){
	if (!wObj)	{
		d=document.getElementById(obj);
	} else d=window.opener.document.getElementById(obj);
	for (i=0;i<d.options.length;i++) {
		d.options[i].selected=true;
	}
}

function delComboItem(objName,wObj){
	if (!wObj)	{
		var obj = document.getElementById(objName);
	} else var obj = window.opener.document.getElementById(objName);
	for (var i=0;i<obj.options.length;i++ )	{
		if (obj.options[i].selected){
			obj.options[i]=null;
			i--;
		}
	}
}


function emptyElement(objid){
	var obj = document.getElementById(objid);
	switch (obj.type){
		case 'input':
		case 'hidden':
			obj.value='';
			break;
		default:
			if (obj.innerHTML){
				obj.innerHTML = '';
			}
	}
}

function doClick(id){
	var obj = document.getElementById(id);
	try {
		obj.click();
	} catch (e) {
		document.location = obj;
	}
}
/*
window.onfocus=function(){
	if(win&&!win.closed){
	 win.focus()
	}
}

window.onunload=function(){
	if(win&&!win.closed){
	 win.close()
	}
}

document.onmousedown=document.onmouseup=function(){
	if(win&&!win.closed){
		 win.focus()
	}
}
*/
function printArea(prefixUrl){
	openPopup('Print',prefixUrl+'/includes/print.html',600,600,1,'center')
	//window.open(prefixUrl+'/includes/print.html','Print',"width=600,height=600,scrollbars=1");
}