// std.js 
// author: mw@arsnavigandi.de

// popup window definition
	popupWindow = new Array(); 
	popupWindow['QuickEdit'] = {'width':1000,'height':700,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};

	popupWindow['help'] = {'width':900,'height':600,'xPos':20,'yPos':20,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};

	popupWindow['small'] = {'width':300,'height':200,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['medium'] = {'width':800,'height':600,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['full'] = {'width':1000,'height':800,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['adminInfo'] = {'width':500,'height':580,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['richEditor_getDownloadTarget'] = {'width':1008,'height':740,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};
	popupWindow['markForDownload'] = {'width':800,'height':600,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};

	popupWindow['emailStack'] = {'width':800,'height':600,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['groupAdmin'] = {'width':500,'height':400,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['generateItem'] = {'width':788,'height':604,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};
	popupWindow['simLesson'] = {'width':900,'height':605,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};
	popupWindow['filesystemSelector'] = {'width':840,'height':480,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['xmlEditor'] = {'width':840,'height':480,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['dataform'] = {'width':840,'height':605,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};

	popupWindow['homeMetanaviPopup'] = {'width':650,'height':500,'xPos':40,'yPos':40,'windowOptions':'dependent=yes,scrollbars=yes,resizable=no'};
	popupWindow['allLessonPopup'] = {'width':900,'height':605,'xPos':40,'yPos':40,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};
	popupWindow['allEnclyclopediaPopup'] = {'width':615,'height':450,'xPos':40,'yPos':40,'windowOptions':'dependent=yes,scrollbars=yes,resizable=no'};
	popupWindow['allStandardPopup'] = {'width':650,'height':500,'xPos':40,'yPos':40,'windowOptions':'dependent=yes,scrollbars=yes,resizable=no'};

	popupWindow['defaultPopup'] = {'width':800,'height':600,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	popupWindow['stats'] = {'width':950,'height':600,'xPos':2,'yPos':2,'windowOptions':'dependent=yes,scrollbars=yes,resizable=yes'};
	/* tinymce 3.x file selector (FileManageer): */
	popupWindow['fileSelector'] = {'width':980,'height':650,'xPos':10,'yPos':10,'windowOptions':'dependent=yes,scrollbars=no,resizable=yes'};

// get values for createWindow
function getPopupWindowData(windowType,windowParam) {
	windowType = windowType.replace(/(.*)Content|Structure/g, "$1");
	if(typeof popupWindow_style=='undefined') {
		alert("Variable 'popupWindow_style' nicht definiert. Bitte wenden Sie sich an den Systemadministrator.");	
	}
	return (popupWindow_style[windowType] && popupWindow_style[windowType][windowParam]) || (popupWindow[windowType] && popupWindow[windowType][windowParam]) || (popupWindow['medium'] && popupWindow['medium'][windowParam]);
}


// createWindow(): creates a new Window - a subtitution for newWindow
function createWindow(e,data) {

if (typeof data == 'undefined') {
	data = e; /* this is no event call */
}

	var width; var height; var xPos; var yPos; var windowOptions;
	if (typeof data['internTitle'] == 'undefined') { data['internTitle']=data['type']; }
	if (typeof data['width'] == 'undefined') { width=getPopupWindowData(data['type'],'width'); } else { width=data['width']; }
	if (typeof data['height'] == 'undefined') { height=getPopupWindowData(data['type'],'height'); } else { height=data['height']; }
	if (typeof data['xPos'] == 'undefined') { xPos=getPopupWindowData(data['type'],'xPos'); } else { xPos=data['xPos']; }
	if (typeof data['yPos'] == 'undefined') { yPos=getPopupWindowData(data['type'],'yPos'); } else { yPos=data['yPos']; }
	if (typeof data['windowOptions'] == 'undefined') { windowOptions=getPopupWindowData(data['type'],'windowOptions'); } else { windowOptions=data['windowOptions']; }

	if (data['url'].match(/\?.+/)) {
		data['url'] = data['url']+'&createdWindow=1';
	} else {
		data['url'] = data['url']+'?createdWindow=1';
	}

	//width and height must always be inner size of window
	var fwidth = width;
	var fheight = height;
	
	var createdWin;
	if (data['realOpener']) {
		createdWin = data['realOpener'].open(data['url'],data['internTitle'],'width='+fwidth+',height='+fheight+',screenX='+xPos+',screenY='+yPos+','+windowOptions);
		createdWin.realOpener = data['realOpener'];
	} else {
		createdWin = open(data['url'],data['internTitle'],'width='+fwidth+',height='+fheight+',screenX='+xPos+',screenY='+yPos+','+windowOptions);
		createdWin.realOpener = window;
	}

	try {
		createdWin.moveTo(xPos,yPos);	
		//createdWin.resizeTo(fwidth,fheight); 
		createdWin.self.focus();
	} catch (ex) {
		//
	}

	if (data['returnCreatedWindow']) {return createdWin;}
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
	
//	alert("setting Cookie -- name: "+cookieName+", value: "+cookieValue+", expires: "+expires.toGMTString());
	path='/';
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
		//alert(document.cookie);
};


function getCookie(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	//setStatusTextDtree('GETTING: cookieName:'+cookieName+', cookieValue:'+cookieValue);
	//alert('getting');
	return (cookieValue);
};

// a substitution for document.getElementByID('')
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}

// get value of radio input
function getRadioValue(rObj) {
  for (var i=0; i<rObj.length; i++) {
  alert("1: "+rObj[i].value);
  	if (rObj[i].checked) {
  		return rObj[i].value;
  	}
	}
  return false;
}

function getRadioValueByNames(name) {
	for (var i=0; i<document.getElementsByName(name).length; i++) {
		if (document.getElementsByName(name)[i].checked) {
			return document.getElementsByName(name)[i].value;
		}
	}
	return false;
}



// get layout data of current window
function getWindowLayout(what) {
	var data = new Array;
	var windowLayoutInnerDimensions = getWindowLayoutInnerDimensions();
	var windowLayoutScrolled = getWindowLayoutScrolled();
	var windowLayoutTotalPage = getWindowLayoutTotalPage();

	data['innerWidth'] = windowLayoutInnerDimensions[0];
	data['innerHeight'] = windowLayoutInnerDimensions[1];
	data['scrollWidth'] = windowLayoutScrolled[0];
	data['scrollHeight'] = windowLayoutScrolled[1];
	return data;
}


// The inner dimensions of the window or frame.
function getWindowLayoutInnerDimensions () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return ([x,y]);
}

//How much the page has scrolled.
function getWindowLayoutScrolled () {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return ([x,y]);
}



//The height of the total page (usually the body element).
function getWindowLayoutTotalPage () {
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
	     //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	return ([x,y]);
}

function encode_base64(d) {
	var r=[]; var i=0; var dl=d.length;
	// Padding vorbereiten
	if ((dl%3) == 1) {
		d[d.length] = 0; d[d.length] = 0;
	}
	if ((dl%3) == 2) {
		d[d.length] = 0;
	}
	// Konvertieren
	while (i<d.length) {
		r[r.length] = b64[d[i]>>2];
		r[r.length] = b64[((d[i]&3)<<4) | (d[i+1]>>4)];
		r[r.length] = b64[((d[i+1]&15)<<2) | (d[i+2]>>6)];
		r[r.length] = b64[d[i+2]&63];
		if ((i%57)==54)
		r[r.length] = "\n";
		i+=3;
	}
	// padding abschließen
	if ((dl%3) == 1) {
		r[r.length-1] = r[r.length-2] = "=";
	}
	if ((dl%3) == 2) {
		r[r.length-1] = "=";
	}
	// Array in text zusammenführen
	var t=r.join("");
	return t;
}

function decode_base64(t) {
	var d=[]; var i=0;
	// zur decodierung die Umbrueche killen
	t=t.replace(/\n|\r/g,""); t=t.replace(/=/g,"");
	while (i<t.length) {
		d[d.length] = (f64[t.charAt(i)]<<2) | (f64[t.charAt(i+1)]>>4);
		d[d.length] = (((f64[t.charAt(i+1)]&15)<<4) | (f64[t.charAt(i+2)]>>2));
		d[d.length] = (((f64[t.charAt(i+2)]&3)<<6) | (f64[t.charAt(i+3)]));
		i+=4;
	}
	if (t.length%4 == 2) {
		d = d.slice(0, d.length-2);
	}
	if (t.length%4 == 3) {
		d = d.slice(0, d.length-1);
	}
	return d;
}

function arscms_registerEvent () {
	// this is just a dummy for compatibility - will be overridden by evolution.js
}

function startLoading() {}
function stopLoading() {}
