// ==========================================================================================
// GLOBALS
// ==========================================================================================

var STANDARD_TIME_PATTERN = "%D.%M.%Y";

// ==========================================================================================


// call after every checkbox that needs to be checked or not according to the state of trigger
// trigger can either be 0 or 1 or false or true
// example for call: <input type=checkbox ....><script>checkboxChecker("%module.criteria%")</script>
function checkboxChecker(trigger)
{
if(trigger.substr(0,1)=='%') return;	
document.forms[document.forms.length-1].elements[document.forms[document.forms.length-1].elements.length-1].checked=parseInt(trigger);
}


// call after every inputfield you want to set a property
// prop - name of the propery (like value, name etc)
// value - new value
// example for call: <input ....><script>setProperty("name","hansli")</script>

function setProperty(prop,value)
{
	myField=document.forms[document.forms.length-1].elements[document.forms[document.forms.length-1].elements.length-1];
	eval("myField."+prop+"=\""+value+"\"");
}

// call after every selectbox whitch items need to be selected according to the state of trigger
// trigger can be one or more values (comma-separated). Every item with the same value as on of the triggers will be selected
// example for call: <select ....><script>selectSelecter("%module.criteria%")</script>


function selectSelecter(trigger)
{
	if(trigger.substr(0,1)=='%') return;	

	myselect=document.forms[document.forms.length-1].elements[document.forms[document.forms.length-1].elements.length-1];
	items = trigger.split(",");
	for(i=0;i<items.length;i++)
	{
		for(j=0;j<myselect.options.length;j++)
		{
			if(myselect.options[j].value==items[i])
			{
				myselect.options[j].selected=true;
				break;
			}
			
		}
		
	}
	
}

// call after the last radiobutten whitch items need to be selected according to the state of trigger
// trigger must be one of the values of the radio-buttons. 
// example for call: <input type="radio" value="pic_ico_graph.gif" name="content.data[type]><script>checkRadios("%module.criteria%")</script>

function checkRadios(trigger)
{
	if(trigger.substr(0,1)=='%') return;	
	
	myselect=document.forms[document.forms.length-1].elements[document.forms[document.forms.length-1].elements.length-1];
	myradio = document.forms[document.forms.length-1].elements[myselect.name];
	for(i=0;i<myradio.length;i++)
	{
		if(myradio[i].value==trigger)
		{
			myradio[i].checked=true;
			break;
		}
		
	}
	
}


//-----------------------------------------------------------------------------
// formClearer()
//=============================================================================
// V/LU: 	2.3 / 2.Nov.01, NST.
// Descr: 	Scans all fields of all forms of the current document and clears 
// 			them if value starts AND ends with '%'.
// 
// Insert the following code into your html-file AFTER the </body>-tag:
//	<script>
//	<!--
//	formClearer();
//	if( -1 != navigator.appName.toLowerCase().indexOf("opera") )
//	{	setTimeout( "formClearer();", 500 );	}	// Opera needs this!
//	// -->
//	</script>
//-----------------------------------------------------------------------------
function formClearer()
{
	var i = 0;
	var j = 0;
	for(i=document.forms.length-1;i>=0;i--)
	{
		if(document.forms[i].elements.length > 0)
		{
			for(j=document.forms[i].elements.length-1;j>=0;j--)
			{
				if(	document.forms[i].elements[j].value )
				{
					val = document.forms[i].elements[j].value;
					if(val.search(/^\%([^ ,:;]*)\%$/)>-1)
					{
						document.forms[i].elements[j].value="";
						if ( (
								document.forms[i].elements[j].type=="text" 
								|| document.forms[i].elements[j].type=="textarea" 
								|| document.forms[i].elements[j].type=="password" 
							 )
							&& !document.forms[i].elements[j].disabled)
						{
							document.forms[i].elements[j].focus();
							document.forms[i].elements[j].blur();
						}
					}
				}
			}
		}
	}
	i = 0;
	j = 0;
	while( document.forms[j].elements.length == 0 )
	{	j++;	}
	while( document.forms[j].elements[i] 
			&& ( document.forms[j].elements[i].type != "textarea"
				&& document.forms[j].elements[i].type != "text"
				&& document.forms[j].elements[i].type != "password" ) )
	{	i++;	}
	if( document.forms[j].elements[i] 
		&& ( document.forms[j].elements[i].type == "textarea"
				|| document.forms[j].elements[i].type == "text"
				|| document.forms[j].elements[i].type == "password" ) 
		)
	{	document.forms[j].elements[i].focus();	}
	setTimeout("scrollTo(0,0);", 100);	// scroll to the top of the page
}
// ----------------------------------------------------------------------------



// call after the form element which focus needs to be set
// example for call: <input type=checkbox ....><script>setFocus()</script>
var focusobject=null;

function setFocus()
{
	if(focusobject)
		focusobject.focus();
	else
	{
		if(!document.forms.length) return;
		focusobject=document.forms[document.forms.length-1].elements[document.forms[document.forms.length-1].elements.length-1];
		focusobject.focus();
		
	}
}

// color fields in forms
// used styles: "formactive", "formfull", and "formempty"

function colorIt(myform)
{
	if (document.all)
	myform.className = "formactive";
}

function noColor(myform)
{
	if (document.all)
	myform.className = "formfull";
}		

function checkField(field)
{
	if (document.all)
	{
		if (field.value.length>1)
			field.className = "formfull";
		else
			field.className = "formempty";
	}
}

// stuff for the menu-bar navigation
// used vars
mOff	= "_off";
mMs	= "_ms";
navExt	= ".gif";
navPath	= "images/";
navName	= "nav_";
itemName = "nav";

function hiLight(number,button,status)
{
	if (window.document.images)
	{
		eval("window.document.nav"+number+".src = navPathCorr+navPath+navName+button+mMs+navExt;");
		window.status = status;
	}
}

function nohiLight(number,button)
{
	if (window.document.images)
	{
		eval("window.document.nav"+number+".src = navPathCorr+navPath+navName+button+mOff+navExt;");
		window.status = "";
	}
}

// confirmation to log out
function checkLogout(msg)
{
	check = confirm(msg);
	if(check != false)
	{
		document.location.href = navPathCorr+"logout.html?SID="+SID;
	}
}

// confirmatioon to delete records
function delRecord(msg)
{
	check = confirm(msg);
	if(check != false)
	{
		alert("okay, delete record");
	}
}


// ------------------------------------------------------------------------------------------
// Time and date function to use with icms and forms
// ------------------------------------------------------------------------------------------

// ==========================================================================================
// INPUT: mysqlTimestamp yyyymmddhhmmss
// pattern : string with the following parameter
// capital letter means with additional 0 before values lower than 10
// %d / %D - day of the month
// %m / %M - month (1 - 12)
// %y / %Y - year format 01 / 2001
// OUTPUT:  string according to pattern
// ------------------------------------------------------------------------------------------
function ts2date(mysqlTs_,pattern_)
{
	if( mysqlTs_.length != 14 )
	{
		alert("Invalid mysqlTimestamp length! (should be 14)");
		return("");
	}
	if( !pattern_ )
	{
		pattern_= STANDARD_TIME_PATTERN;
	}
	output = "";
	
	nYear 		= mysqlTs_.slice(0, 4);
	nMonth		= mysqlTs_.slice(4, 6);
	nDay		= mysqlTs_.slice(6, 8);
	nHours		= mysqlTs_.slice(8, 10);
	nMinutes	= mysqlTs_.slice(10, 12);
	nSeconds	= mysqlTs_.slice(12, 14);

	nCurrentIndex = pattern_.indexOf("%", 0);
	while( nCurrentIndex != -1)
	{
		param = pattern_.charAt( nCurrentIndex+1 );
		
		// handle the parameter
		switch( param )
		{
		case 'd':
			output += nDay*1;
			break;
		case 'D':
			output += nDay;
			break;
		case 'm':
			output += nMonth*1;
			break;	
		case 'M':
			output += nMonth;
			break;
		case 'y':
			output += nYear.substring(2, 4);
			break;			
		case 'Y':
			output += nYear;
			break;
		default:
			alert("Wrong parameter supplied to the ts2date function: %" + param);
		}
				
		// check for next occurence of "%"
		nLastIndex = nCurrentIndex+2;
		nCurrentIndex = pattern_.indexOf("%", nCurrentIndex+1);
		
		// fill stuff between parameters
		if( nCurrentIndex != -1 )
		{
			output += pattern_.substring( nLastIndex, nCurrentIndex );
		}
	}

	return(output);
}
// ------------------------------------------------------------------------------------------

// ==========================================================================================
// INPUT: 
// date  -> format: [d]d.[m]m.[yy]yy
// time  -> format: true - sets time of the moment (clients time)
//			     [h]h.[m]m.[s]s
// OUTPUT: mysqlTimestamp yyyymmddhhmmss
// ------------------------------------------------------------------------------------------
function date2ts(date_, time_)
{
	output = "";
	// validate the date_ parameter
	date_ = adjustDate(date_, true);
	
	nYear 		= date_.substr(6, 4);
	nMonth		= date_.substr(3, 2);
	nDay		= date_.substr(0, 2);
	nHours		= 0;
	nMinutes	= 0;
	nSeconds	= 0;
	
	if( time_ == true )
	{
		// use the current time
		today 		= new Date();
		time_		= adjustTime( today.getHours() + "." 
									+ today.getMinutes() + "."
									+ today.getSeconds() );
	}
	else
	{	
		// use suplied time (string)
		time_		= adjustTime( time_ );
	}
	
	nHours 		= time_.substr( 0, 2 );
	nMinutes	= time_.substr( 3, 2 );
	nSeconds	= time_.substr( 6, 2 );
	
	output = nYear + nMonth + nDay + nHours + nMinutes + nSeconds;
	
	return(output);
}
// ------------------------------------------------------------------------------------------

// ==========================================================================================
//INPUT: date: format [d]d.[m]m.[yy]yy
//
//OUTPUT: correct date in format d.m.y or dd.mm.yy according to full
// ------------------------------------------------------------------------------------------
function adjustDate(dateString, full)
{
	if( !full )
	{
		full = false;	
	}
	splitedDate = dateString.match(/(\d+\.)|(\d+$)/g);
	flag=true;
	if(splitedDate)
	{
		if(splitedDate.length!=3)
		{	
			flag=false;
		}
		else
		{
			for(i=0;i<splitedDate.length;i++)
			{
				splitedDate[i]=splitedDate[i]*1;
			}
		}	
	}
	else	
	{
		flag= false;
	}
	if(!flag)
	{
		alert("Feld für Datum: Falsches Format.\nBitte das folgende Format verwenden:\nTT.MM.JJJJ");
		today= new Date();
		year = today.getYear();
		if(year < 1000) year+= 1900;
		
		if( full == true )
		{
			var nDay = today.getDate()+1;
			var nMonth = today.getMonth()+1;
			if( nDay < 10 )
			{
				nDay = "0" + nDay;
			}
			if( nMonth < 10 )
			{
				nMonth = "0" + nMonth;	
			}
			myDate = nDay + "." + nMonth + "." + year;
		}
		else
		{
			myDate = (today.getDate()+1)+"."+(today.getMonth()+1)+"."+year;
		}
	}
	else
	{	
		if(splitedDate[2]<70) splitedDate[2]+=2000;
		myDate = new Date(splitedDate[2],splitedDate[1]-1,splitedDate[0]);
		year =myDate.getYear();
		if(year < 1000) year+= 1900;
		
		if( full == true )
		{		
			var nDay = myDate.getDate();
			var nMonth = myDate.getMonth()+1;
			if( nDay < 10 )
			{
				nDay = "0" + nDay;
			}
			if( nMonth < 10 )
			{
				nMonth = "0" + nMonth;	
			}
			myDate = nDay + "." + nMonth + "." + year;
		}
		else
		{
			myDate = myDate.getDate()+"."+(myDate.getMonth()+1)+"."+year;
		}
	}
	
	return myDate;
}
// ------------------------------------------------------------------------------------------


// ==========================================================================================
//INPUT: time: format [h]h.[m]m.[s]s
//
//OUTPUT: success: 	correct time in format hh.mm.ss
// 		  fail: 	return 00.00.00
// ------------------------------------------------------------------------------------------
function adjustTime(timeString_)
{
	output = "";
	if( !timeString_ )
	{
		return( "00.00.00" );
	}
	
	nLastIndex 		= 0;
	nCurrentIndex 	= timeString_.indexOf(".", 0);
	// hours:
	Value = timeString_.substring(nLastIndex, nCurrentIndex);
	Value %= 24;
	if( (Value * 1) < 10 )
	{
		Value = "0" + Value;
	}
	output += Value + ".";
	
	nLastIndex 		= nCurrentIndex;
	nCurrentIndex 	= timeString_.indexOf(".", nLastIndex + 1);
	// minutes:
	Value = timeString_.substring(nLastIndex + 1, nCurrentIndex) + ".";
	Value %= 60;
	if( (Value * 1) < 10 )
	{
		Value = "0" + Value;
	}
	output += Value + ".";
	
	// seconds:
	Value = timeString_.substring(nCurrentIndex + 1, timeString_.length);
	Value %= 60;
	if( (Value * 1) < 10 )
	{
		Value = "0" + Value;
	}
	output += Value;

	return(output);
}

function openPicWin(pFileName,pWinName,pWidth,pHeight)
			{
				var isNS = (document.layers) ? true : false;
				
				if (isNS) pHeight = pHeight + 19;
					eval("allSettings = 'width="+pWidth+",height="+pHeight+",menubar=yes,scrollbars=no,status=yes,resizable=yes'");
					w=window.open(pFileName,pWinName,allSettings);
					w.window.focus();
			}
// ------------------------------------------------------------------------------------------