var popWin;

/**
 * @author Laurynas Butkus, 2006-01-06
 */
function togglePopMenu(trigger, popmenuid, show)
{
    if (!$(popmenuid)) return;
    
    // position pop menu according to trigger position
    // only on first show
    if (!trigger.__pop_positioned)
    {
        var p = Position.cumulativeOffset(trigger);
        var l = $(popmenuid);
        l.style.left = p[0] + trigger.clientWidth + 5;
        l.style.top = p[1];
        trigger.__pop_positioned = true;
    }
    
    if (trigger.effect)
        trigger.effect.cancel()
    
    if (show)
        trigger.effect = new Effect.Appear(popmenuid, { duration: 0.1 } );
    else
        trigger.effect = new Effect.Fade(popmenuid, { duration: 0.3 } );
}


function openPopWindow(src, options, name)
{
	var x = arguments[3];	
	var y = arguments[4];	

	if (x) options+= ",left="+x+",screenX="+x;
	if (y) options+= ",top="+y+",screenY="+y;

	if (!(src.indexOf("http")==0 || src.indexOf("/")==0)) {
		src = _ABS_PATH+src;
	}

	if (src.search(/\.(jpg|gif|jpeg|png)$/ig) != -1) {
		openImagePopUp(src, options, name, x, y, '', true);
		return;
	}
	
	if (popWin) popWin.close();
	if ( popWin=window.open(src, name, options) )
		popWin.focus();
}

function openImagePopUp(src, options, name)
{
    var matches, width = 0, height = 0, id = 0;
    if (matches = options.match(/width\=(\d+)/i)) width = matches[1];
    if (matches = options.match(/height\=(\d+)/i)) height = matches[1];
    //if (matches = src.match(/neo_image\.php\?id\=(\d+)/i)) id = matches[1];
	var x = arguments[3] || (screen.width-width)/2;
	var y = arguments[4] || (screen.height-height)/2;
	var title = arguments[5];
	var secondary = arguments[6];

	if (!(src.indexOf("http")==0 || src.indexOf("/")==0)) {
		src = _ABS_PATH+src;
	}

	if (!secondary && width && height) {
		if (x) options+= ",left="+x+",screenX="+x;
		if (y) options+= ",top="+y+",screenY="+y;
	}

	if (popWin) popWin.close();
	popWin = window.open( "", name, options);

    var size = width && height ? "width='"+width +"' height='"+height+"'" : "";

    var html = "";
	html+= "<html><head>";
	if (title) html+= "<title>" + title + "</title>";
	//html += '<script>function show(n){image.src = \'tools/neo_image.php?id=\' + imgs[n] + \';sel=n}</script>';
	html += '<style>body { margin:0px; padding:0px; top:0px; left:0px }</style>';
	html += '</head><body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">';
	//html += "<table width='100%'><tr><td>";
	html += "<img id='image' src='"+src+"' "+size+" border='0' onclick='window.close();' style='cursor: pointer;' />";
	//html += "<td><a href='javascript:show(sel + 1);'>a</a></td>";
//	html += "</table";
	html+= "</body></html>";	
	//alert(popWin.imgs.length);
	
	popWin.document.write(html);
	popWin.document.close();
	popWin.focus();

	/*doc = popWin.opener.document;
	elements = doc.getElementsByTagName('*');
	popWin.imgs = new Array();
	var clRegExp = new RegExp('\\bpic\\b');
	k = 0, popWin.sel = 0;
	for (var i = 0; i < elements.length; i++)
	{
		var clName = elements[i].className;
		if (clRegExp.test(clName))
		{
			var imgs = elements[i].getElementsByTagName('img');
			if (imgs.length)
			{
				image = imgs[0];
				imageSrc = image.src;
				if (matches = imageSrc.match(/neo_image\.php\?id\=(\d+)/i))
				{
					popWin.imgs[k] = matches[1];
					if (id == ) popWin.sel = k;
				}
				k++;
			}
		}
	}*/
} 

function openPrintWindow(src)
{
	var printStr = "";	
	printStr = src.lastIndexOf("?") < 0 ? "?" : "&";
	printStr+= "doPrint=yes";
	src+= printStr;
	openPopWindow(src, 'menubar=yes, toolbar=yes, resizable=yes, scrollbars=yes', 'printWin')
}

function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>4) {
	   return false
	}

	if (len<2) {
	   return false
	}

	return true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function trim(s)
{
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
    s = s.substring(1,s.length);
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
    s = s.substring(0,s.length-1);
  return s;
}

//-->

