// function chkRequiredFields(obj)
//   checks all input fields in form and if there is some
//	 required input fields with empty content then function generate errors
// parameters: obj - form which should be checked
//
function submitOnce(frm){
	for (var i=0;i<frm.elements.length;i++){
		var e = frm.elements[i];
		if ((e.type.toLowerCase()=='submit')||(e.type.toLowerCase()=='reset'))
			e.disabled=true;
	}
}

function chkRequiredFields(obj){
	missinginfo = "";
	firstRequired = null; 
	inpObj = null;
	if (obj != null && obj.elements != null) {
		for(i = 0; i < obj.elements.length; i++){
			inpObj = obj.elements[i];
			if (inpObj.required == "yes" && inpObj.value == ""){
				missinginfo += " - " + inpObj.title +" " +errIsEmpty+"\n";
				if ( firstRequired == null) firstRequired = inpObj;
			}
		}
	};
	if (missinginfo != "") {
		missinginfo = errCannotSubmit+"\n" 
					+ "-----------------------------------------\n"
					+ missinginfo
					+ "-----------------------------------------\n"
					+ "\n"+errPleaseReEnter+"\n";
		alert(missinginfo);
		if ( firstRequired != null) firstRequired.focus();
		return false;
	}
	else return true;
}

// function checkMail(obj)
//   checks input field on email
//	 if input field hasn't email then function generates errors
// parameters: obj - form which should be checked
//
function checkMail(obj) {
	missinginfo = "";

	if (obj.value == "") {
		missinginfo = "Email address is empty!";
	}
	else {
		if (obj.value == "Your email") {
			missinginfo = "Email address is not entered!";
		}
		else {
			if ((obj.value.indexOf('@') == -1) || 
				(obj.value.indexOf('.') == -1)) {
				missinginfo = "Incomplete Email address!";
			}
		}
	} 

	if (missinginfo != "") {
		missinginfo += "\n\nPlease re-enter and submit again.\n";
		alert(missinginfo);
		obj.focus()
		return false;
	}
	else return true;
}

// CheckAll(frm)
//   checks all checkboxed in ��� form
function CheckAll(frm, f)
{
	for (var i=0;i<frm.elements.length;i++)
	{
		var e = frm.elements[i];
		if ((e.name == 'chkrow[]') && (e.type=='checkbox'))
			e.checked = f;
	}
	frm.allbox.checked = f
}
// CheckCheckAll(frm)
//   checks all checkboxed in ��� form
function CheckCheckAll(frm)
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<frm.elements.length;i++)
	{
		var e = frm.elements[i];
		if ((e.name == 'chkrow[]') && (e.type=='checkbox'))
		{
			TotalBoxes++;
			if (e.checked)
			{
				TotalOn++;
			}
		}
	}
	if ((TotalBoxes>0) && (TotalBoxes==TotalOn))
		{frm.allbox.checked=true;}
	else
		{frm.allbox.checked=false;}
}

function selectedCount(form)
{
	cnt=0;
	for(var i=0;i<form.elements.length;i++){
		e = form.elements[i];
		if(e.type=='checkbox' && e.name=='chkrow[]' && e.checked)
			cnt ++;
	}
	return cnt;
}

//submit form with command
function formAction(form,cmd){
	form.cmd.value = cmd;
	form.submit();
	return false;
}
//submit form with command after confirmation
function formConfirmAction(form,sConfirm,cmd){
	if(!sConfirm || confirm(sConfirm))
		formAction(form,cmd);
	return false;
}

//if some rows is selected - submit form with command after confirmation
function formSelectedConfirmAction(form,sConfirm,cmd){
	if(selectedCount(form)>0){
			formConfirmAction(form,sConfirm,cmd);
		}
	return false;
}


function popWin(url) {
	remote = window.open(url,"popwindow","width=380,height=240,resizable=0,menubar=0,status=0,scrollbars=1");
	remote.focus();}

//create <options> from string "1~one~2~two...."
function MkOpts(sOpt,sel){var aOpt=sOpt.split("~"), L=Math.floor(aOpt.length/2)*2; for(var i=0;i<L;i+=2) document.write('<option value="'+aOpt[i]+'"'+((sel==aOpt[i])?' selected':'')+'>'+aOpt[i+1]); }

function FirstClick(inpObj){
	if (inpObj.visited != "yes" && inpObj.value == inpObj.title){
		inpObj.value = ""
		inpObj.visited = "yes"
	}
	inpObj.focus()
}

function setDisplay(objID,value){
	obj = document.getElementById(objID);
	if(!obj) return;
	obj.style.display=(value)?'':'none';
}
function ToggleDiv(e){
	if(e) e.style.display = (e.style.display == 'none')?'block':'none';
}

// *********  CWorldTime ***********************
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function CWorldTime(divName){
	this.divName = divName;
	this.showTime = true;
	this.showDate = true;
	this.interval = 1000; // refresh time each second;
	this.localzone =0; //0=GMT, >0 west, <0 east
	this.timezone = ''; //
	this.delta = 0; // shift time in miliseconds from local time
	this.IsDaylightSavingTime = false; // is daylight saving time now (summer) //not used now

	this.setTimezone = function(tz) {
		this.timezone = parseInt(tz);
		this.delta = isNaN(this.timezone) ? 0 : (this.timezone - this.localzone) * 3600000;
	}
	
	this.stop = function() { var id=this.timerID; this.timerID = null; if(null!=id)window.clearTimeout(id);};
	this.start = function() {if(null!=this.timerID)stop(); var thisObj = this; this.tick();};
//	this.stop = function() { var id=this.timerID; this.timerID = null; if(null!=id)window.clearInterval(id);};
//	this.start = function() {if(null!=this.timerID)stop(); var thisObj = this; this.tick(); this.timerID = window.setInterval( function(){thisObj.tick();},this.interval);};
	this.tick = function() {
		var time = new Date();
		time.setTime(time.getTime()+this.delta);
		this.display(time);
		var thisObj = this;
 		this.timerID = window.setTimeout(function(){thisObj.tick();},this.interval);
	}
	this.display = function(time){
		var stime = this.leaderzero(time.getHours()) + ':' + this.leaderzero( time.getMinutes() ) + ':' + this.leaderzero( time.getSeconds() );
		var sdate = days[time.getDay()] + ' | ' + this.leaderzero(time.getDate()) + '.' +  this.leaderzero(time.getMonth()+1) + '.' + time.getFullYear();
		var oTime = document.getElementById(this.divName);
		s = "";
		if(this.showDate) s += "<span class='gray'>"+sdate+"</span>";
		if(this.showDate && this.showTime) s +="<br>";
		if(this.showDate && this.showTime) {
			s += "<span class='red'>"+stime+"</span>";
		} else {
			s += stime;
		}
		if(oTime) oTime.innerHTML = s;
	}
	this.leaderzero = function(n){return n<=9 ? '0'+n : n;}
	
	this.init = function(){
		var tz = new Date();
		//zonenow = tz.getTimezoneOffset()/60;
		this.localzone = -tz.getTimezoneOffset()/60;
		this.timezone = this.localzone;
		tz = new Date((new Date()).getFullYear(),0,1); //calculate localzone by winter time 
		winterzone = -tz.getTimezoneOffset()/60;
		this.IsDaylightSavingTime = (winterzone != this.localzone);
	}
	this.init();
	return this;
}

function CCalendar(divName, divmonthName){
	this.divName = divName; // DIV where calendar printed;
	this.divmonthName = divmonthName; // DIV where month name printed;
	//date on calendar marked as selected
	this.date = new Date(); this.date = new Date(this.date.getFullYear(),this.date.getMonth(),this.date.getDate()); 
	this.month = new Date(this.date.getFullYear(),this.date.getMonth(),1); //
	this.firstday = 1;			//Monday is first day of week
	this.isshown = false;
	this.link = '/news.html?date=$D';	// template of link result is '?date=20051231'
	this.statuses = new Array(); //which date has links or selected or something else
		//example:
		// calendar.statuses = new Array(
		//		[31,12,2005,'link'],   // here is generated url
		//		[15,12,2005,'link','http://www.convit.de/news.html'], // here is own url
		//		[1,1,2005,'holiday','New Year'],	//
		//		[20,3,2005,'selected'], ...... );
	
	

	this.nextMonth = function(){
		this.goMonth(1);
	}
	this.prevMonth = function(){
		this.goMonth(-1);
	}
	this.goMonth = function(delta){
		this.month.setMonth(this.month.getMonth()+delta);
		if(this.isshown) this.show(); else this.hide();
	}	
	this.setMonth = function(date){
		if(date) this.month = new Date(date.getFullYear(),date.getMonth(),1);
			else this.month = new Date();
		if(this.isshown) this.show(); else this.hide();
	}
	
	this.show = function() { this.isshown = true; this.printMonth(this.divmonthName, this.month); this.print(this.divName, this.month); }
	this.hide = function() { this.isshown = false; this.clear(); }
	
	this.clear = function(divName){
		var oDiv = document.getElementById(this.divName);
		if(oDiv) oDiv.innerHTML = '';
	}
	this.printMonth = function(divName, date){
		var txtMonth = months[date.getMonth()] + ' | ' + date.getFullYear();
		var oDiv = document.getElementById(divName);
		if(oDiv) oDiv.innerHTML = txtMonth;
	}
	this.print = function(divName, date){
		var txtCalendar='', txtWeek='';
		var dateFirst = new Date(date.getFullYear(),date.getMonth(),date.getDate());
		var dateLast = new Date(dateFirst); dateLast.setMonth(dateLast.getMonth()+1); dateLast.setDate(0);
		var dateMarker = new Date(dateFirst); dateMarker.setDate(1-(dateFirst.getDay()+7-this.firstday)%7);

		for(var i=0;i<7;i++){txtWeek += '<th>'+days[(this.firstday+i)%7].charAt(0)+'</th>';}
		txtCalendar += '<tr>'+txtWeek+'</tr>\n';

		var st = this.statuses;

		while(dateMarker<dateLast){
			txtWeek = '';
			for(var i=0;i<7;i++){
				var txtDate = dateMarker.getDate();
				var txtClass = ''; if(dateMarker < dateFirst || dateMarker > dateLast) txtClass = 'oth';
				var txtUrl='';
				var txtTitle='';
				d = dateMarker.getDate();
				m = dateMarker.getMonth()+1;
				y = dateMarker.getFullYear();
				
				for(var j=st.length-1;j>=0;j--){
					if((st[j][0] == d) && (st[j][1] == m) && (st[j][2] == y)){
						if(st[j][3] == 'link'){
							txtUrl = (st[j][4])?st[j][4]:this.prepareUrl(y,m,d);
							txtDate = '<a href="'+txtUrl+'">'+txtDate+'</a>';
						}else{
							txtClass += ' '+st[j][3];
							if(st[j][4]){ txtTitle = st[j][4]; }
						}
					}
				}
				if(txtUrl) txtDate = '<a href="'+txtUrl+'">'+txtDate+'</a>';
				if(txtTitle) txtDate = '<div title="'+txtTitle+'">'+txtDate+'</div>';
				txtWeek += '<td class="'+txtClass+'">'+txtDate+'</td>';
				dateMarker.setDate(dateMarker.getDate()+1);
			}
			txtCalendar += '<tr>'+txtWeek+'</tr>\n';
		}
		txtCalendar = '<table cellspacing="0" border="0" class="calendar">\n'+ txtCalendar + '</table>\n';
		var oDiv = document.getElementById(divName);
		if(oDiv) oDiv.innerHTML = txtCalendar;
	}

	this.prepareUrl = function(y,m,d){
		var txtUrl = this.link;
		txtUrl = txtUrl.replace(/\$D/g, this.leaderzero(y)+this.leaderzero(m)+this.leaderzero(d));
		txtUrl = txtUrl.replace(/\$d/g, this.leaderzero(d));
		txtUrl = txtUrl.replace(/\$m/g, this.leaderzero(m));
		txtUrl = txtUrl.replace(/\$y/g, this.leaderzero(y));
		return txtUrl;
	}

	this.leaderzero = function(n){return n<=9 ? '0'+n : n;}
}


//***** browser ***************************
function CBrowser() {

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  var ua= navigator.userAgent, s, i;
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new CBrowser();

function openWindow(url, width, height)	{
	var left = (screen.width  - width)  / 2;
	var top = (screen.height - height) / 3;
	styles=((arguments.length>3) && arguments[3])?', '+arguments[3]:'';
	remote = window.open(url, "wnd", "width="+width+", height="+height+", top="+top+", left="+left+", toolbar=no, location=no, menubar=no, resizable=no, scrollbars=no"+styles);
	remote.focus();
}

function showHideBlock(objId, blocks) {
	for (i = 1; i < blocks+1; i++) {
		if (i == objId) {
			document.getElementById('link' + i).className = 'red';
			document.getElementById('term' + i).style.display = 'block';
		} else {
			document.getElementById('link' + i).className = '';
			document.getElementById('term' + i).style.display = 'none';
		}
	}
}
