/* ---------------------------------------------------------------------------------

	General JavaScript Variables

---------------------------------------------------------------------------------*/
var startURL1 = 'http://www.ipem.rug.ac.be/'
var startURL2 = 'http://ipem.rug.ac.be/'
var startURL3 = 'http://www.ipem.ugent.be/'
var startURL4 = 'http://ipem.ugent.be/'

/* ---------------------------------------------------------------------------------

	General JavaScript Functions

--------------------------------------------------------------------------------- */
function popUp(URL) {
	id = 'instruments';
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=0,statusbar=1,menubar=1,resizable=1,width=800,height=600');");
}

// this function is indirectly called by noRightClick()
function click(e) {
	// this is the copyright message that will appear
	var message="Copyright by IPEM";
	if (document.all) {
		if (event.button==2||event.button==3) {
			alert(message);
			return false;
		}
	}
	if (document.layers) {
		if (e.which == 3) {
			alert(message);
			return false;
		}
	}
}

// noRightClick()
// when you call this function it is not permitted to "right-click" in that page
// only seems to work in IE :-(
function noRightClick()
{
	if (document.layers) {
		document.captureEvents(Event.MOUSEDOWN);
	}
	document.onmousedown=click;
}


// changeImageSource(place, imageString)
// + changes the source of an image
// Input:
// + place: name of the image to change
// + imageString: string with the source of the new image
// Output:
// + return: true
function changeImageSource(place, imageString) {
	place.src = imageString ; 
	return true;
} 


// replaceImage (place, newPlace)
// + replaces an image with another
// Input:
// + place: name of the image to replace
// + newPlace: name of the new image
// Output:
// + return: true
function replaceImage(place, newPlace) {
	place.src = newPlace.src ; 
	return true;
} 


// Replaces text with by in string
function replaceText(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) 
		return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) 
		return string;
    if (i == -1) 
		return string;

	var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replaceText(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


// openDocumentInFrames(URL,newDocument,standardDocument)
// + This functions writes out the frameset as defined in the opening page of the site
//   and opens newDocument in the main-frame
// Input:
// + URL: the URL of the website
// + newDocument: the document to open in the main frame 
// + standardDocument: the standard document of the website, normally 'index.html'
function openDocumentInFrames(URL,newDocument,standardDocument){
 	
	// newWindowName is dereived from newDocument, to obtain a correct name,
	// the characters '.', '\', '/', '#' and '?'	 are replaced by '_'
	newWindowName = newDocument.replace(/\./g,"_");
	newWindowName = newWindowName.replace(/\\/g,"_");
	newWindowName = newWindowName.replace(/\//g,"_");
	newWindowName = newWindowName.replace(/#/g,"_");
	newWindowName = newWindowName.replace(/\?/g,"_");


		// checks if newDocument contains a '.', if not append 'index.html' to overcome bug in IE otherwise append a '?' to fix another bug in IE
	if (newDocument.lastIndexOf(".")<0)	{
		newDocument=URL+newDocument + standardDocument; 
	}
	else{
	// if newDocument contains a '#' (anchor), than the '?' must be placed before the '#'
		if (newDocument.lastIndexOf("#")<0)
		{
			newDocument=URL+newDocument +'?';
		}
		else
		{
			newDocument = newDocument.replace(/#/g,"?#");
			newDocument=URL+newDocument;
		}
	}
//	alert('newDocument= '+newDocument);
//	alert('URL= '+URL+standardDocument);

	// opens a pop-up window
	var popWin = window.open(URL+standardDocument, newWindowName,"location=yes,scrollbars=yes,menubar=yes,resizable=yes,toolbar=yes,status=yes");
	popWin.focus();
	// opens & writes the content  in the pop-up window
	popWin.document.open();
	popWin.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
	popWin.document.writeln('<HTML><HEAD>'); 
	popWin.document.writeln('<TITLE>IPEM: Institute for Psychoacoustics and Electronic Music - Dept. of Musicology, Ghent University, Belgium</TITLE>'); 
	popWin.document.writeln('<script language="JavaScript">');
	popWin.document.writeln('<\/script>');
	popWin.document.writeln('</HEAD>'); 
	// remove /_newsite/ once the new site is activated
	if(screen.width<=800 || screen.height <= 600){
		popWin.document.writeln('<FRAMESET rows="60,*" BORDER=0 frameborder=0 framespacing=0>');	
		popWin.document.writeln('	<FRAME SRC="'+URL+'/top800x600.html" NAME="top" NORESIZE frameborder=0 framespacing=0 scrolling=no>');
		popWin.document.writeln('<FRAMESET COLS="100,*" BORDER=0 frameborder=0 framespacing=0>');
		popWin.document.writeln('		<FRAME NAME="menu" SRC="'+URL+'/menu800x600.html"  NORESIZE scrolling=no>');
	}
	else{
		popWin.document.writeln('<FRAMESET rows="74,*" BORDER=0 frameborder=0 framespacing=0>');
		popWin.document.writeln('	<FRAME SRC="'+URL+'/top.html" NAME="top" NORESIZE frameborder=0 framespacing=0 scrolling=no>');
		popWin.document.writeln('	<FRAMESET COLS="150,*" BORDER=0 frameborder=0 framespacing=0>');
		popWin.document.writeln('		<FRAME NAME="menu" SRC="'+URL+'/menu.html"  NORESIZE scrolling=no>');
	}
	popWin.document.writeln('		<FRAME NAME="main" SRC="'+newDocument+'">');
	popWin.document.writeln('	</FRAMESET>');
	popWin.document.writeln('</FRAMESET>');
	popWin.document.write('<BODY>');	
	popWin.document.writeln('<NOFRAMES>Sorry, You need a frames compatible browser to view this site !</NOFRAMES>');
	popWin.document.write('</BODY></HTML>');
	popWin.document.close();
	// places the pop-up window in the upper left corner
	popWin.moveTo(0,0); 
 }

// Check4Frames()
// + This functions checks if the document is opened inside a frameset
//   if not, it calls openDocumentInFrames()
function Check4Frames(){
	if (parent.frames.length == 0) {
		currentURL = location.href;
		
		//alert('ugent: '+currentURL.indexOf('ugent'));
		//alert('www.: '+currentURL.indexOf('www.'));
		
		// startURL1 or startURL2
		if(currentURL.indexOf('ugent') <0)
		{
			
			if (currentURL.indexOf('www.') <0){
				currentDocument = currentURL.substring(startURL2.length,currentURL.length);
				startURL=startURL2;
			}
			else{
				currentDocument = currentURL.substring(startURL1.length,currentURL.length);
				startURL = startURL1;
			}
		}
		// startURL3 or startURL4
		else
		{
			if (currentURL.indexOf('www.') <0)
			{
				currentDocument = currentURL.substring(startURL4.length,currentURL.length);
				startURL = startURL4;
			}
			else{
				currentDocument = currentURL.substring(startURL3.length,currentURL.length);
				startURL = startURL3;
			}
		}
	//	alert('openDocumentInFrames('+startURL+','+currentDocument+','+'index.html)');		
 
		openDocumentInFrames(startURL,currentDocument,'index.html');
		window.self.close();
	}
}