// scripts.js
// javascript for aab
//
// function to switch an element style
// input: id of the element, class to switch to
function changeStyle(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}

// generic popup opener
// input: url, width and height of popup to open
// winTarget is the name of the popup
// winResizable defines if popup can be resized, by default it can be resized.
// in allPopUps every opened Window handle is stored
var top = 60;
var left = 80;
var allPopUps=new Array();

function popUpOpen(src, iWidth, iHeight, winTarget, winResizable) {
	
	var winWidth = iWidth;
	var winHeight = iHeight;
	var popUp;
	
	if (winTarget=="") { winTarget="_blank"; }
	if (winResizable!="no") { winResizable="yes"; }
	
	// just check for already open popUps if target is not _blank
	if (winTarget!="_blank") {
		popUp=checkAllPopUps(winTarget);
	} 	
	
	if (popUp!=null && !popUp.closed) {
		popUp.location.href=src;
		popUp.focus();	
		// Windows XP service pack 2 doesn't allow resizing anymore		
		try {
			popUp.resizeTo(winWidth,winHeight);
		} catch (e) {}
	} else {			
		eval('popUp = window.open(src, "' + winTarget + '", "width=' + winWidth + ',height=' + winHeight + ',top=' + top + ',left=' + left + ',toolbar=no,scrollbars=yes,resizable=' + winResizable + ',menubar=no,location=no,dependent=no");');
		popUp.focus();
		allPopUps[allPopUps.length]=new Array({targetName:winTarget, popupHandle:popUp}); // array of arrays simulating a hashtable
	}
}

// check if window winTarget already open, if found, handle is returned. if not found null is returned
function checkAllPopUps(winTarget) {
	var popUpFound=null;
	for (var i=0; i<allPopUps.length; i++) {
	   	var obj = allPopUps[i][0];
     	if (obj.targetName==winTarget) {
     		popUpFound=obj.popupHandle;
     	}
	}
	return popUpFound;
}

// print view opener
// just one single instance of print popup should be visible
function printOpen(src) {
	popUpOpen(src, 650, 510, "print", "yes");
}

// help view opener
function helpOpen(src) {
	popUpOpen(src, 495, 345, "help", "no");
}

//switch the src of an image
function changeImg(imgname,imgsrc) {
	document[imgname].src=imgsrc;
}
// emulates submit button bahavior for form submits
// form needs to have a hidden field buttonid to
// identify which button was pressed (param: btnid) if the 
// form has multiple submit buttons. Not needed for forms
// with a single submit button
function submitForm(formname,btnid) {
	document.forms[formname].elements["buttonid"].value=btnid;
	document.forms[formname].submit();
}
//function to pepare the search query
function parseSearchQuery(theForm, message) {
	var queryValue = theForm.elements["query2"].value;
	var testValue = queryValue.replace(/ /g, "");
	if (testValue.length < 3) {
		alert(message);
		return (false);
	}
	//queryValue = queryValue.replace(/\+/g, "%2b");
	//queryValue = queryValue.replace(/\-/g, "%2d");
	theForm.elements["query"].value = queryValue;
	return (true);
}
