// Koch Portal JavaScript Routines - 19th July 2007

var EventCost=1;  // Euros per event date

function show_hidden(id){
	//alert(id);
	//if(id != ''){
		//alert(document.getElementById(id).style.display);
		document.getElementById(id).style.display = 'block';
		active_hidden_text = id;
	//}
}

var active_hidden_text = "";
function show_hidden_obj(obj){
	//alert(document.getElementById(obj).style.display);
	if(document.getElementById(obj).style.display == 'block'){
		//document.getElementById(obj).style.display = 'none'; // this line toggles the block on and off
		document.getElementById(obj).style.display = 'none';  // but here we want it to always be on
	}
	else{
		document.getElementById(obj).style.display = 'block';
	}

	if(active_hidden_text != "" && active_hidden_text != obj){
		document.getElementById(active_hidden_text).style.display = 'none';
	}
	
	active_hidden_text = obj;
	
}


function hide_obj(obj){
	//alert(document.getElementById(obj).style.display);
	document.getElementById(obj).style.display = 'none';  // here we want it to always be off
}

function show_obj(obj){
	//alert(document.getElementById(obj).style.display);
	document.getElementById(obj).style.display = 'block';  // here we want it to always be on
}



function addRowToTable1()
{
  var tbl = document.getElementById('tblSample1');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
//  var cellLeft = row.insertCell(0);
//  var textNode = document.createTextNode(iteration);
//  cellLeft.appendChild(textNode);
 
  var cellLeft = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'Menge' + iteration;
  el.id = 'Menge' + iteration;
  el.size = 20;
  el.onblur = function(){ checkQuantity(this); checkZutat(this.parentNode.parentNode); }
  cellLeft.appendChild(el);

  // select cell
  var cellSel = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'Einheit' + iteration;
  sel.options[0] = new Option('Bitte Auswählen', '');
  sel.options[1] = new Option('Becher', 'Becher');
  sel.options[2] = new Option('Blatt', 'Blatt');
  sel.options[3] = new Option('Bund', 'Bund');
  sel.options[4] = new Option('Esslöffel', 'Esslöffel');

  //sel.options[7] = new Option('etwas', 'etwas');
  sel.options[5] = new Option('Flasche', 'Flasche');
  sel.options[6] = new Option('Gramm', 'Gramm');
  sel.options[7] = new Option('Glas', 'Glas');
  sel.options[8] = new Option('Knolle', 'Knolle');
  sel.options[9] = new Option('Liter', 'Liter');
  sel.options[10] = new Option('Milligramm', 'Milligramm');
  sel.options[11] = new Option('Milliliter', 'Milliliter');
  sel.options[12] = new Option('Pfund', 'Pfund');
  sel.options[13] = new Option('Pint', 'Pint');
  sel.options[14] = new Option('Päckchen', 'Päckchen');
  sel.options[15] = new Option('Prise', 'Prise');  // no quantity
  sel.options[16] = new Option('Scheibe', 'Scheibe');
  sel.options[17] = new Option('Spritzer', 'Spritzer');
  sel.options[18] = new Option('Stück', 'Stück');
  sel.options[19] = new Option('Stange', 'Stange');
  sel.options[20] = new Option('Tasse', 'Tasse');
  sel.options[21] = new Option('Teelöffel', 'Teelöffel');
  sel.options[22] = new Option('Tropfen', 'Tropfen');
  sel.options[23] = new Option('Tube', 'Tube');
 // sel.options[24] = new Option('wenig', 'wenig');
  sel.options[24] = new Option('Würfel', 'Würfel');
  sel.options[25] = new Option('Portion', 'Portion');
  sel.options[26] = new Option('Zehe', 'Zehe');
  sel.options[27] = new Option('Zweig', 'Zweig');

  sel.options[28] = new Option('etwas', 'etwas');  // no quantity
  sel.options[29] = new Option('ein wenig', 'ein wenig');  // no quantity
  sel.options[30] = new Option('---------', '---------');  // no quantity

  sel.onblur = function(){ checkZutat(this.parentNode.parentNode); }
  sel.onchange = function(){ checkZutat(this.parentNode.parentNode); }
  cellSel.appendChild(sel);
 
  var cellLeft = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'Zutat' + iteration;
  el.id = 'Zutat' + iteration;
  el.size = 25;
  el.onblur = function(){ checkZutat(this.parentNode.parentNode); }
  cellLeft.appendChild(el);

  // righr cell
  var cellRight = row.insertCell(3);
  var img = document.createElement('img');
  img.src = 'images/_clear.gif';
  img.width = 18;
  img.height = 18;

  cellRight.appendChild(img);
  
  //el.onkeypress = keyPressTest;
  //cellRight.appendChild(el);
  
}




function addRowToTable2()
{
  var tbl = document.getElementById('tblSample2');
  var lastRow = tbl.rows.length;
//alert ('length=' + lastRow)
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow+1; // because we don't have a header for this table, we want the row count to start from 1 not zero
  var row = tbl.insertRow(lastRow);
  
  var cellLeft = row.insertCell(0);
  var el = document.createElement('textarea');
  //el.type = 'text';
  //el.size = 60;
  el.cols = '65';
  el.rows = '4';
 
  el.name = 'Schritt' + iteration;
  el.id = 'Schritt' + iteration;
  cellLeft.appendChild(el);

  //right cell
  var cellRight = row.insertCell(1);
  var textNode = document.createTextNode('Schritt ' + iteration);
  cellRight.appendChild(textNode);
 
  
}







function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}


function removeRowFromTable(tableid,minrows)
{
  //var tbl = document.getElementById('tblSample');
  var tbl = document.getElementById(tableid);
  var lastRow = tbl.rows.length;
  if (lastRow > minrows) tbl.deleteRow(lastRow - 1);
}


function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}

function validateRow(frm)
{
  var chkb = document.getElementById('chkValidate');
  if (chkb.checked) {
    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = document.getElementById('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}





function checkQuantity(qty){

	if(qty.value != ""){

		var regex = /^[\d\-\/,.]+$/;

		if(!qty.value.match(regex)) {
			alert('In diesem Feld dürfen nur Zahlen verwendet werden. Zum Beispiel "2", "1/2", "3,5". ');
			qty.className = 'unfilled';
			qty.value = '';
		}
	}
}





function checkZutat(tr){

	var valid = false;



 	var menge = tr.getElementsByTagName('input')[0];

 	var einheit = tr.getElementsByTagName('select')[0];

 	var zutat = tr.getElementsByTagName('input')[1];

	var einheitIndex=einheit.selectedIndex;

	//document.title='menge=' + menge.value + ', einheit=' + einheit.value + ', zutat=' + zutat.value;
	//document.title='menge=' + menge.value + ', einheit index=' + einheit.selectedIndex + ', zutat=' + zutat.value;
	
	//document.title='menge=' + menge.value + ', einheit ss=' + einheit[einheit.selectedIndex].value + ', zutat=' + zutat.value;
	document.title='menge=' + menge.value + ', einheit nummer=' + einheitIndex + ', zutat=' + zutat.value;


 	var imgtd = tr.getElementsByTagName('td');

 	imgtd = imgtd[imgtd.length-1];



	var img;

	if( !(img = imgtd.getElementsByTagName('img')[0]) ){

		img = document.createElement('img');

		imgtd.appendChild(img)

	}




// exceptions...
//	if(menge.value == '' && einheit.value != '' && zutat.value != ''){
	if(menge.value == '' && einheitIndex != 0 && zutat.value != ''){
		//if (einheit.value=='ein wenig') {
		if (einheitIndex==29) {
			valid = true;
		}
		//if (einheit.value=='etwas') {
		if (einheitIndex==28) {
			valid = true;
		}
		//if (einheit.value=='Prise') {
		if (einheitIndex==15) {
			valid = true;
		}
		//if (einheit.value=='---------') {
		if (einheitIndex==30) {
			valid = true;
		}


	} else if(menge.value != '' && einheitIndex != 0 && zutat.value != ''){

		valid = true;

	} else if(menge.value == '' && einheitIndex == 0 && zutat.value == '') {

		imgtd.removeChild(img);

 	} else  {

 		valid = false;

 	}

	//valid = true;

//	document.title='menge=' + menge.value + ', einheit=' + einheit.value + ', zutat=' + zutat.value;

 	if(valid){

		img.src = 'images/z_ja.gif';

 	} else {

		img.src = 'images/z_nein_s.gif';

 	}

 }




// -----------------------------------------------   event functions --------------------------------------------------------

function removeRowFromEventTable(tableid,minrows,modeAdd)
{
  //var tbl = document.getElementById('tblSample');
  var tbl = document.getElementById(tableid);
  var lastRow = tbl.rows.length;
  if (lastRow > minrows) {
	  tbl.deleteRow(lastRow - 1);
	  if (modeAdd) {
		  var iteration = lastRow-1; // because we don't have a header for this table, we want the row count to start from 1 not zero
		  document.getElementById('PaidEventTotal').innerHTML=EventCost*iteration;
  	  }
  }
}


function addRowToEventTable(modeAdd)
{
  var tbl = document.getElementById('tblEventDates');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow+1; // because we don't have a header for this table, we want the row count to start from 1 not zero
  var row = tbl.insertRow(lastRow);
  
  if (modeAdd) document.getElementById('PaidEventTotal').innerHTML=EventCost*iteration;
  
  // left cell
//  var cellLeft = row.insertCell(0);
//  var textNode = document.createTextNode(iteration);
//  cellLeft.appendChild(textNode);
 
//  var cellLeft = row.insertCell(0);
//  var el = document.createElement('input');
//  el.type = 'text';
//  el.name = 'Day' + iteration;
//  el.id = 'Day' + iteration;
//  el.size = 20;
//  el.onblur = function(){ checkDate(this.parentNode.parentNode); }
//  cellLeft.appendChild(el);



  var cellSel = row.insertCell(0);
  var sel = document.createElement('select');
  sel.name = 'Day' + iteration;
  sel.options[0] = new Option('Tag', '');
  sel.options[1] = new Option('1', '01');
  sel.options[2] = new Option('2', '02');
  sel.options[3] = new Option('3', '03');
  sel.options[4] = new Option('4', '04');
  sel.options[5] = new Option('5', '051');
  sel.options[6] = new Option('6', '06');
  sel.options[7] = new Option('7', '07');
  sel.options[8] = new Option('8', '08');
  sel.options[9] = new Option('9', '091');
  sel.options[10] = new Option('10', '10');
  sel.options[11] = new Option('11', '11');
  sel.options[12] = new Option('12', '12');
  sel.options[13] = new Option('13', '13');
  sel.options[14] = new Option('14', '14');
  sel.options[15] = new Option('15', '15');
  sel.options[16] = new Option('16', '16');
  sel.options[17] = new Option('17', '17');
  sel.options[18] = new Option('18', '18');
  sel.options[19] = new Option('19', '19');
  sel.options[20] = new Option('20', '20');
  sel.options[21] = new Option('21', '21');
  sel.options[22] = new Option('22', '22');
  sel.options[23] = new Option('23', '23');
  sel.options[24] = new Option('24', '24');
  sel.options[25] = new Option('25', '25');
  sel.options[26] = new Option('26', '26');
  sel.options[27] = new Option('27', '27');
  sel.options[28] = new Option('28', '28');
  sel.options[29] = new Option('29', '29');
  sel.options[30] = new Option('30', '30');
  sel.options[31] = new Option('31', '31');

  sel.onblur = function(){ checkDate(this.parentNode.parentNode); }
  sel.onchange = function(){ checkDate(this.parentNode.parentNode); }
  cellSel.appendChild(sel);






  // select cell
  var cellSel = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'Month' + iteration;
  sel.options[0] = new Option('Monat', '');
  sel.options[1] = new Option('Januar', '01');
  sel.options[2] = new Option('Februar', '02');
  sel.options[3] = new Option('März', '03');
  sel.options[4] = new Option('April', '04');
  sel.options[5] = new Option('Mai', '051');
  sel.options[6] = new Option('Juni', '06');
  sel.options[7] = new Option('Juli', '07');
  sel.options[8] = new Option('August', '08');
  sel.options[9] = new Option('September', '091');
  sel.options[10] = new Option('Oktober', '10');
  sel.options[11] = new Option('November', '11');
  sel.options[12] = new Option('Dezember', '12');

  sel.onblur = function(){ checkDate(this.parentNode.parentNode); }
  sel.onchange = function(){ checkDate(this.parentNode.parentNode); }
  cellSel.appendChild(sel);
 


  // select cell
  var cellSel = row.insertCell(2);
  var sel = document.createElement('select');
  sel.name = 'Year' + iteration;
  sel.options[0] = new Option('Jahr', '');
  sel.options[1] = new Option('2007', '2007');
  sel.options[2] = new Option('2008', '2008');
  sel.options[3] = new Option('2009', '2009');
  sel.options[4] = new Option('2010', '2010');

  sel.onblur = function(){ checkDate(this.parentNode.parentNode); }
  sel.onchange = function(){ checkDate(this.parentNode.parentNode); }
  cellSel.appendChild(sel);
 


//  var cellLeft = row.insertCell(2);
//  var el = document.createElement('input');
//  el.type = 'text';
//  el.name = 'Year' + iteration;
//  el.id = 'Year' + iteration;
//  el.size = 25;
//  el.onblur = function(){ checkDate(this.parentNode.parentNode); }
//  cellLeft.appendChild(el);

  
  // righr cell
  var cellRight = row.insertCell(3);
  var img = document.createElement('img');
  img.src = 'images/_clear.gif';
  img.width = 18;
  img.height = 18;

  cellRight.appendChild(img);
  //el.onkeypress = keyPressTest;
  //cellRight.appendChild(el);
  
}




function checkDate(tr){

	var valid = false;
 	var Day = tr.getElementsByTagName('select')[0];
 	var Month = tr.getElementsByTagName('select')[1];
 	var Year = tr.getElementsByTagName('select')[2];

	var DayIndex=Day.selectedIndex;
	var MonthIndex=Month.selectedIndex;
	var YearIndex=Year.selectedIndex;

	document.title='Day nummer=' + DayIndex + ', Month nummer=' + MonthIndex + ', Year nummer=' + YearIndex;
 	var imgtd = tr.getElementsByTagName('td');
 	imgtd = imgtd[imgtd.length-1];

	var img;
	if( !(img = imgtd.getElementsByTagName('img')[0]) ){
		img = document.createElement('img');
		imgtd.appendChild(img)
	}

	if(DayIndex != 0 && MonthIndex != 0 && YearIndex != 0) {
			valid = true;
 	} else  {
 		valid = false;
 	}

 	if(valid) {
		img.src = 'images/z_ja.gif';
 	} else {
		img.src = 'images/z_nein_s.gif';
 	}
}



function checkEndDate(tr){

	var valid = false;
 	var Day = tr.getElementsByTagName('select')[0];
 	var Month = tr.getElementsByTagName('select')[1];
 	var Year = tr.getElementsByTagName('select')[2];

	var DayIndex=Day.selectedIndex;
	var MonthIndex=Month.selectedIndex;
	var YearIndex=Year.selectedIndex;

	document.title='Day nummer=' + DayIndex + ', Month nummer=' + MonthIndex + ', Year nummer=' + YearIndex;
 	var imgtd = tr.getElementsByTagName('td');
 	imgtd = imgtd[imgtd.length-1];

	var img;
	if( !(img = imgtd.getElementsByTagName('img')[0]) ){
		img = document.createElement('img');
		imgtd.appendChild(img)
	}

	if ((DayIndex != 0 && MonthIndex != 0 && YearIndex != 0) || (DayIndex == 0 && MonthIndex == 0 && YearIndex == 0)) {
			valid = true;
 	} else  {
 		valid = false;
 	}

 	if(valid) {
		img.src = 'images/z_ja.gif';
 	} else {
		img.src = 'images/z_nein_s.gif';
 	}
}


function user_popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=685,height=400,left = 170,top = 200');");
}






