///////////////////////////////////////////////////
// musikleser.js
// ************************************************
// description: Contains general used JS-Functions
// (c) 20.08.2005 15:07 - Sven Tappert
///////////////////////////////////////////////////


/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <string> = myJsTrim( str )
// -----------------------------------------------------------------
// description:
//		removes leading and ending whitespaces from a string 'str'
///////////////////////////////////////////////////////////////////*/
function myJsTrim( str ) 
{
	return ( str.replace(/^\s*/,"").replace(/\s*$/,"") );
}

/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <bool> = myJsTrimValues(F)
// -----------------------------------------------------------------
// description:
//		trims all input fields of a given form 'F'; always returns TRUE
///////////////////////////////////////////////////////////////////*/
function myJsTrimValues(F)
{
	for (i=0; i<F.elements.length; i++)
	{
		F.elements[i].value = myJsTrim(F.elements[i].value);
	}
	return true;
}


/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <bool> = ST_JS_valueChanged(V)
// -----------------------------------------------------------------
// description:
//		Checks, if a given form 'V' element has still the default value
///////////////////////////////////////////////////////////////////*/
function ST_JS_valueChanged( V ) 
{
	V.value = myJsTrim( V.value );
	
	var newValue = V.value;
	var oldValue = V.defaultValue;

	if(newValue == oldValue)
	{
		alert("Keine Änderung vorgenommen!");
		return false;
	}
	return true;
}


/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <bool> = myJsCheckMail(email)
// -----------------------------------------------------------------
// description:
//		checks the value of a given element (object!) to be a valid
//		e-mail address.
//		
//		Returns also FALSE, if email.value() is an empty string
///////////////////////////////////////////////////////////////////*/
function myJsCheckMail(email)
{
	email.value = myJsTrim(email.value);
	var regex = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@(([a-zA-Z0-9-]|ü|ö|ä|Ü|Ö|Ä)+\.)+([a-zA-Z]{2,4})$/;
	
	if (regex.test(email.value) == false || email.value.length==0)
	{
		alert('Ungueltige E-Mail Adresse: "'+email.value+'"');
		email.focus();
		email.select();
		
		// NOT OK, EXIT
		return false;
	}
	// OK, EXIT	
	return true;
}


/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <bool> = ST_JS_checkPW(PW)
// -----------------------------------------------------------------
// description:
//		checks if the values of the element PW is a valid 
//		password & if it is too long or too short 
///////////////////////////////////////////////////////////////////*/
function ST_JS_checkPW(PW)
{
	if(PW.value.length < 5 || PW.value.length > 12)
	{
		alert('Passwort muss zwischen 5 und 12 Zeichen lang sein.');
		return false;
	}
	
	var regex = /[\s\\\^°`´]/;
	
	if (regex.test(PW.value) == true)
	{
		alert('Ungueltiges PW! Leerzeichen sowie die Zeichen:\n\\^°´`\nsind nicht erlaubt.');
		
		// NOT OK, EXIT
		return false;
	}
	
	// OK, EXIT	
	return true;
}

function ST_JS_checkUserName(usrName)
{
	if(usrName.value.length < 4 || usrName.value.length > 255)
	{
		alert('Benutzername muss mindestens 4 Zeichen lang sein.');
		return false;
	}
	var regex = /^[a-zA-Z]+([_a-zA-Z0-9-+])*$/

	if (regex.test(usrName.value) == true)
	{
		// OK, EXIT	
		return true;
	}
	
	alert("Ungueltiger Benutzername! \n"+
				"Der Benutzername muss mit einem Buchstaben beginnen und darf sich nur "+
				"aus Buchstaben (keine Umlaute), Zahlen, "+
				"Unterstrich, sowie Plus- und Minuszeichen zusammensetzen.");
		
	// NOT OK, EXIT
	return false;
}

/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <void> = newWindow(file, n, w, h, x, y, extra)
// -----------------------------------------------------------------
// description: 
//		opens a new browser window as popup window, displaying the
//		html/php,etc. page 'file'. 
//
//		The name, width, height and offset of the popup window are 
//		passed to this function.
//
//		By default, the window is resizable and has scrollbars enabled.
//
//		Other additional properties can be switched on, by using the 
//		'extra' string-parameter. Refer to the description of the 
//		JS-standard 'open()'-function to learn how to assemble the
//		'extra' string.
//	
// input:
//		file (string): URI of the file to be displayed.
//		n (string): name of the window.
//		w (string): width (in px) of the window (excl. window frame)
//		h (string): height (in px) of the window (excl. window frame)
//		x (string): window-offset (in px) from the left screen border
//		y (string): window-offset (in px) from the upper screen border
//		extra (string): additional properties (adress-bar, etc.) 
//										passed to the JS open()-function
//
// output: --
///////////////////////////////////////////////////////////////////*/
function newWindow(file, n, w, h, x, y, extra) {

var width=w?w:700;
var height=h?h:500;
var name=n?n:'popup';
var strLeft=x?',left='+x:',left=0';
var strTop=y?',top='+y:',top=0';
var strExtra=extra?','+extra:'';

 infoWin=window.open(file, name,"width="+width+",height="+height+",scrollbars=yes,resizable=yes"+strExtra+strLeft+strTop)
 infoWin.resizeTo(width,height);
 infoWin.moveTo(x,y);
 infoWin.focus();
// window.focus();
	
}

function ST_JS_openHtmlWindow(strMessage, n, w, h, x, y, extra) {

var width=w?w:700;
var height=h?h:500;
var name=n?n:'html_popup';
var strLeft=x?',left='+x:',left=0';
var strTop=y?',top='+y:',top=0';
var strExtra=extra?','+extra:'';

 infoWin=window.open("about:blank",name,"width="+width+",height="+height+",scrollbars=yes,resizable=yes"+strExtra+strLeft+strTop)
 infoWin.resizeTo(width,height);
 infoWin.moveTo(x,y);
 infoWin.document.writeln("<html><head><title>ST Debug - Fenster</title></head><body>");
 infoWin.document.writeln(strMessage);
 infoWin.document.writeln("</body></html>");
 infoWin.focus();
}

/**
 * Displays an confirmation box beforme to submit a "DELETE" query.
 * This function is called while clicking links
 *
 * @param   object   the link
 * @param   object   the action to confirm
 *
 * @return  boolean  whether to run the query or not
 *
 * FUNCTION BASED ON PHPMYADMIN FUNCTION
 */
function confirmLink(theLink, strMsg)
{
	// or browser is Opera (crappy js implementation)
	if (typeof(window.opera) != 'undefined') {
		return true;
	}
	
	var is_confirmed = confirm(strMsg);
	if (is_confirmed) {
		append = (theLink.href.indexOf("?") > -1)?'&':'?';
		append += 'is_js_confirmed=1';
		theLink.href += append;
	}
	
	return is_confirmed;
} // end of the 'confirmLink()' function


/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <bool> = checkEmptyness(theForm,strElements)
// -----------------------------------------------------------------
// description:
// 		Displays an information box, if the listed form elements
//		are empty; trims elements first
//
//		If elements are ok, &is_js_confirmed is appended to the link
//
//		Should be called like this:
// 		'onSubmit="return checkEmptyness(formName,'name1,name2,name3');"'
//	
// input:
//		theForm (object): the Form	
//		@param (string): fields to check as CSV
//
// output:
//		<boolean>: whether to run the query or not
///////////////////////////////////////////////////////////////////*/
function checkEmptyness(theForm,strElements)
{

	// or browser is Opera (crappy js implementation)
	if (typeof(window.opera) != 'undefined') {
		return true;
	}

	// INIT
	var elems = strElements.split(',');

	for (var i=0; i<elems.length; i++) {

		// trim values
		eval('var str = myJsTrim(theForm.'+elems[i]+'.value)');
		eval('theForm.'+elems[i]+'.value=str');
		if (eval('theForm.'+elems[i]+'.value')=="") {
			alert('Bitte alle Textfelder ausfuellen');
			eval('theForm.'+elems[i]+'.focus()');
			return false;
		}
	}
	
	var append = '';

	append = (theForm.action.indexOf("?") > -1)? "&":"?";
	append += "is_js_confirmed=1";
	
	theForm.action += append;

	return true;
}
function checkEmptyness_en(theForm,strElements)
{

	// or browser is Opera (crappy js implementation)
	if (typeof(window.opera) != 'undefined') {
		return true;
	}

	// INIT
	var elems = strElements.split(',');

	for (var i=0; i<elems.length; i++) {

		// trim values
		eval('var str = myJsTrim(theForm.'+elems[i]+'.value)');
		eval('theForm.'+elems[i]+'.value=str');
		if (eval('theForm.'+elems[i]+'.value')=="") {
			alert('Please fill in all fields');
			eval('theForm.'+elems[i]+'.focus()');
			return false;
		}
	}
	
	var append = '';

	append = (theForm.action.indexOf("?") > -1)? "&":"?";
	append += "is_js_confirmed=1";
	
	theForm.action += append;

	return true;
}

function ST_cbSelectAll(strArr,bSelect)
{
	// set standard value for bSelect == true
	bSelect = arguments.length==1?true:bSelect;

	// get Element (array)
	var temp = document.getElementsByName(strArr);
	
	// (un)check all elements
	for(i=0;i<temp.length;i++)
	{
		temp[i].checked=bSelect;
	}
	return false;
}

/*///////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// <void> = openerGoto(strTarget,bClose)
// -----------------------------------------------------------------
// description:
//		opens a dedicated location in opener window and closes the 
//		current window, if bClose is set to true.
// input:
//		strTarget (string): href
//		bClose (bool): flag
// output:
//		<void>
///////////////////////////////////////////////////////////////////*/
function openerGoto(strTarget,bClose)
{
	if(opener)
	{
		opener.location.href = strTarget;
	}
	
	if(bClose==1)
	{
		self.close();
	}
	return;
}

