var _POPUP_FEATURES = 'location=0,status=yes,menubar=0,width=600,height=400,scrollbars=yes';

function isUndefined(v) {
    var undef;
    return v===undef;
}

function raw_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  }
  if (isUndefined(target)) {
    target = '_blank';
  }
  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, features) {
return raw_popup(src.getAttribute('href'),src.getAttribute('target') || '_blank',features);
}

function openImage(fn,x,y) {
	var newwin=window.open('','imgPopup',"menubar=no,scrollbars=1,height="+y+",width="+x);
	if (newwin.document.value);
	newwin.document.write(
	'<html>'+
	'<head>\n'+
	'<style>\n'+
	'html, body {\n'+
	'	margin:0;\n'+
	'	padding:0;\n'+
	'}\n'+
	'a img {border:0;}\n'+
	'</style></head>\n'+
	'<body><a href="Javascript:window.close()"><img src="'+fn+'" alt="'+fn+'" border="0" /></a></body></html>');
	newwin.document.close();
	newwin.focus();
}

function openBeszur(URL,x,y,fname,type) {
	URL='beszur_'+URL+'.php';
	if (x=='') x=500;
	if (y=='') x=300;
	if (fname!='') {
		URL+='?fname='+fname;
		if (type!='') URL+='&type='+type;
	}
	var bewin=window.open(URL,'beszurPopUP',"scrollbars=yes,height="+y+",width="+x);
	bewin.focus();
}

function append(textEl,text) {
	textEl.value  = textEl.value + text; // just append
}

// insert @ caret function az ultimateBB-bol 
function insertAtCaret (textEl, text) {
	if (textEl.createTextRange && textEl.caretPos) {
		// MSIE
		var caretPos = textEl.caretPos;
		caretPos.text =
			caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
			text + ' ' : text;
	} else if(textEl.selectionStart && textEl.setSelectionRange) {
		// Mozilla 1.3+
		var val = textEl.value;
		var cpos = textEl.selectionStart;
		var fpos = cpos + text.length;

		var before = val.substr(0,cpos);
		var after = val.substr(cpos, val.length);
		var aspace = after.charAt(0) == ' ' ? "" : " ";
		var bspace = before.charAt(before.length) == ' ' ? "" : " ";

		textEl.value = before + bspace + text + aspace + after;
		textEl.setSelectionRange(fpos,fpos); // set cursor pos to end of text
		textEl.focus();
	} else {
		textEl.value  = textEl.value + text; // otherwise just append
	}

	return true;
} // end insertAtCaret

function beszur (fieldID,text) {
	myTarget=window.opener.document.getElementById(fieldID);
	insertAtCaret (myTarget,text);
}//end beszur

function resetDateDropdown(DD_id) {
	var d=new Date();
	var ddy=document.getElementById(DD_id+"_Y");
	var ddm=document.getElementById(DD_id+"_M");
	var ddd=document.getElementById(DD_id+"_D");

	y=d.getFullYear();
	m=d.getMonth();m++;
	d=d.getDate();
	
 	found=false;	
	for (i=0;i<ddy.length;i++) {
		if (ddy.options[i].value==y) {
			ddy.selectedIndex=i;
			found=true;
			ddy.value=y;
		}
		if (found) break;
	}
	if (!found) {
		ddy.options[i].value=y;
		ddy.selectedIndex=i;
		ddy.value=y;
	}
	found=false;	
	for (i=0;i<ddm.length;i++) {
		if (ddm.options[i].value==m) {
			ddm.selectedIndex=i;
			found=true;
			ddm.value=m;
		}
		if (found) break;
	}
	if (!found) {
		ddm.options[i].value=m;
		ddm.selectedIndex=i;
		ddm.value=m;
	}
	found=false;	
	for (i=0;i<ddd.length;i++) {
		if (ddd.options[i].value==d) {
			ddd.selectedIndex=i;
			found=true;
			ddd.value=d;
		}
		if (found) break;
	}
	if (!found) {
		ddd.options[i].value=d;
		ddd.selectedIndex=i;
		ddd.value=d;
	}
}
