//Javascript code for Faltbootclub Heidenheims WEB site..
//Written by Carsten L. Birn
//All rights reserved, Copyright 2003-2005 Faltbootclub Heidenheim

function sicherTxtDialog(txt, obj) {
	var val = confirm(txt);
	if (val) { 
		obj.submit();
	}
}

function blank() {
    document.write("&nbsp;");
}

function br() {
    document.write("<br/>");
}

/**
 * Function that extracts an value from the query string object.
 * param:  searchString = nme to get value from.
 * return: Value
 */
function getId(searchString) {
    searchString += "=";

    var queryString = location.search;
    var queryArray = queryString.split("&");

    var resultString = "";
    var queryIdx = -1;
    var idx = -1;
    for (i = 0; i < queryArray.length; i++) {
        idx = queryArray[i].indexOf(searchString,0);
        if( idx != -1) {
            resultString = queryArray[i];
            break;
        }
    }
    var idText = -1
    if (idx != -1) {
        idx += searchString.length;
        idText = resultString.substring(idx);
    }

    return idText;
}

function init() {
    setKurs();+
    setTermin();
}

function setKurs() {
    document.forms[0].kurs.value = unescape(getId("kurs"));
}

function setTermin() {
    var text = unescape(getId("termin"));
    text = trim(text);
    document.forms[0].termin.value = text;
}

function trim(text) {
    var tekst = text;
    while(((tekst.charCodeAt(0)==10) || (tekst.charCodeAt(0)==32) ) && (text != "")) {
        tekst = tekst.substring(1);
    }
    return "" + tekst;
}

function ev(object) {
    object.kurs.value = escape(object.kurs.value)
    object.submit();
}

function validate(object) {
    /*
	if (!object.erk.checked) {
        alert("Teilnahmebedingungen m&uuml;ssen anerkannt werden!");
        object.method="post";
        object.kurs.value = escape(object.kurs.value);
        object.action="buchen.php"

    } else {
        object.method="post";
        object.action="formular.php";

    }
	*/
}

function setProg(id,month,year) {
	document.forms.progpreview.previewid.value = id;
	document.forms.progpreview.month.value = month;
	document.forms.progpreview.year.value = year;
	var val = document.forms.progpreview.action;

	document.forms.progpreview.action = val + "#" + id;
  	document.forms.progpreview.submit();
}

function setProg(object, id, month, year) {
	object.previewid.value = id;
	object.month.value = month;
	object.year.value = year;
	var val = object.action;

	object.action = val + "#" + id;
  	object.submit();
}

function genProg(id,month) {
	document.forms.progpreview.programId.value = "define";
	document.forms.progpreview.editMode.value = "new";
  	document.forms.progpreview.submit();
}

function setMonth(calId) {
  document.forms.progpreview.submit();
}

function setMonth(month,id) {
  document.forms.calendar.month.value = month;
  document.forms.calendar.id.value = id;
  document.forms.calendar.submit();
}

function showPicture(id) {
	object = document.forms.gallerySelect;
    object.galleryId.value = id;
	object.editMode.value = "show";
    object.submit();
}

function editGalerie(object,id) {
    object.form.galleryId.value = id;
	object.form.editMode.value = "set";
    object.form.submit();
}

//------------------------------------------------------------------------


	//Image gallery script

	function showPopupImg(bild,text,comment,width,height) {
		if (width < 500) {
			width = 500;
		}
		if (height < 400) {
			height = 400;
		}
		//bild static/showDialog.php
		var picWin = window.open("","Fenster1","width=" + width + ",height="+ height +",left=0,top=0,status=no,location=no,dependent=yes");

		picWin.document.write("<html><body>");
		picWin.document.write("<div align='center'> ");
		picWin.document.write("<b>" + text + "</b><br/> ");
 		picWin.document.write('<img src='+bild+' border="0" style="border: 1px solid black;" alt=""/><br/>');
		picWin.document.write('<p>' + comment + '</p>');
		picWin.document.write('<p align="left" style="font-weight: bold">&#169; Faltbootclub Heidenheim e.V.</p>');
		picWin.document.write("<a href='javascript: window.close();'>Fenster schlie&szlig;en</a>" );
		picWin.document.write("</div></body></html>");
		picWin.document.close();
		picWin.focus();
	}

	function setComment(obj, path, commentid ) {
		var txtobj = eval('obj.form.' + commentid + '.value');
		obj.form.comment.value = txtobj;
		obj.form.commentFile.value = path;
		obj.form.editMode.value = "txtinsert";
		obj.form.submit();
	}

	function removeImage(obj,bild) {
		var state = confirm("Really remove: " + bild);
		if (state) {
			obj.form.picturePath.value = bild;
			obj.form.submit();
		}
	}

	var imageIndex = 1;
	var selectColor = "";

	function changeslides(which){

		var imghtml=""
		myIndex = (which=="initial") ? 0 : parseInt(which);
		imageIndex = myIndex ;
  		var mode = (which == "initial") ?  "initial" : "";


		imghtml += '<img src="'+imageArray[myIndex].src+'" border="1" width="350"/>';
		if (mode == "initial") {
			document.write('<div>'+imghtml+'</div>');
			changecolors();
		} else{
			document.getElementById("imagecontainer").innerHTML=imghtml;
			changecolors();
		}
	}

	function changecolors(){
		var numberobj = eval(document.getElementById("numberDiv"));
		if (numberobj == null) {
			return;
		}
		numberlinks = numberobj.getElementsByTagName("A");
		for (i=0 ;  i <= numberlinks.length-1;  i++)  {
			if (i == imageIndex) {
				numberlinks[i].style.backgroundColor = "white";

			} else {
				numberlinks[i].style.color = "blue";
				numberlinks[i].style.backgroundColor = "#e27f38";
			}
		}
	}

	function showImage(incVal){
		if (incVal == 1) {
			if (imageIndex < imgArr.length-1) {
				imageIndex = imageIndex  + 1;
				changeslides(imageIndex );
			}
		} else {
			if (imageIndex != 0) {
				imageIndex = imageIndex - 1;
				changeslides(imageIndex);
			}
		}
	}

	function prevButton(state) {
		//alert(imageIndex);
		if (state > 1 || state < 0 || (imageIndex == 0)) {
			state = 0;
		}
		document.getElementById("prevbutton").border = state;
	}

	function nextButton(state) {
		if (state > 1 || state < 0 || (imageIndex == imgArr.length-1)  ) {
			state = 0;
		}
		document.getElementById("nextbutton").border = state;
	}

	//Methods for the Kanuschool
	function removeCourseTimes(object,id) {
		object.courseitemaddrem.value = "remove";
		object.ctid.value = id;
  		object.submit();
	}


	//Methods for the articles

	function setArticleId(object, articleid) {
		object.articleId.value = articleid;
  		object.submit();
	}

	function removeArticleId(object, articleid, name) {
		var state = confirm("Datei: '" + name + "' löschen?");
		if (state) {
			object.articleId.value = articleid;
			object.state.value = "remove";
			object.submit();
		}
	}


	// commands for Program managagement
	function programentry(object, pid) {
		object.programId.value = pid;
		object.submit();
	}

	function courseentry(object, pid) {
		object.courseid.value = pid;
		object.submit();
	}

	// commands for User managagement
	function removeObject(object, fid) {
		object.update.requesttype.value = "remove";
		object.update.fid.value  = fid;
  		object.update.submit();
	}
	
	
	function insert(form, textarea, aTag, eTag) {
  		
  		var input = document.forms[form].elements[textarea];
  		//var input = element;
  		input.focus();
  		
		/* Internet Explorer */
		if(typeof document.selection != 'undefined') {
			/* Einfügen des Formatierungscodes */
    		var range = document.selection.createRange();
		    var insText = range.text;
			range.text = aTag + insText + eTag;
		    /* Anpassen der Cursorposition */
		    range = document.selection.createRange();
			if (insText.length == 0) {
				range.move('character', -eTag.length);
			} else {
				range.moveStart('character', aTag.length + insText.length + eTag.length);      
			}
		    range.select();
		}
		
		/* Gecko based Browsers */
		else if(typeof input.selectionStart != 'undefined') {
    		/* Einfügen des Formatierungscodes */
		    var start = input.selectionStart;
			var end = input.selectionEnd;
			var insText = input.value.substring(start, end);
    		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    		/* Anpassen der Cursorposition */
    		var pos;
    		if (insText.length == 0) {
      			pos = start + aTag.length;
    		} else {
    			pos = start + aTag.length + insText.length + eTag.length;
    		}
    
    		input.selectionStart = pos;
    		input.selectionEnd = pos;
  		} else {
  			/* Other Browsers */
  			
  			/* Abfrage der Einfügeposition */
			var pos;
    		var re = new RegExp('^[0-9]{0,3}$');
    		while(!re.test(pos)) {
      		pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    	}
    	if(pos > input.value.length) {
      		pos = input.value.length;
    	}
    	/* Einfügen des Formatierungscodes */
    	var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    	input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  	}
}
	
	