/* Script for Buttons on LH-Website
   Required: script.js - for cookie check
*/
// Form handler generic (static)
function gf() { this.frm = ""; } //gf is generic form, f is the form
gf.prototype = new Object();
gf.prototype.rf = function(o) {
	var so = o.parentNode; // search object
	if(so.tagName == "FORM") this.frm = so; // if form is found, this is it 
	else this.rf(so); // else, keep on searching
}
// submitForm generic (static)
function fs(o) { this.rf(o); } //fs:form submit, rf:recurse form
fs.prototype = new gf();
fs.prototype.submitMe = function() { this.frm.submit(); }
function submitForm(obj) { // this function will be called from the buttons
	form = new fs(obj);
	form.submitMe();
}

/* START: Button Object -----------------------------------------------------------------------
Example:
	var b1 = new Button({ 
		action:'http://link',	 					// submit|back|link|close|popup|print|func
		func:'alert(\"param1, param2\");',			// function to be called (after submit)
		valFunc:'formValidationMethod',				// function that returns true|false before onsubmit
		url:'http://de.wikipedia.org/wiki/Button',  // url
		label:'Zur?ck',								// any character but " ' and \
		setHidden:'id',
		setHiddenVal:'123',
		setFormId:'id',
		setFormAction:'action',
		cssclass:'CSSclassname'						// multiple class names space separated
		disabled:'true'								// button will be disabled
		nocookieurl:'http://de.wikipedia.org/wiki/Cookie' // url for a browser without cookie support (but with JS support :-) )
		trackFunc: 'dcsMultiTrack(..)' // function call for webreporting before other actions, but after a successfull validation with 'valFunc'
	});
// all attributes are optional. the default-button is a back-button
*/
function Button(p) {
	this.a = "back"; // default-button is always a back-button
	this.t = "Back (Default)"; // TODO: should this var rather be empty?
	this.z = "<div>[No button defined!]</div>"; // just a backup, this should never be seen...
	this.ds = this.c = this.u = this.f = this.shid = this.shv = this.vf = ""; // if a function/link is not specified btn is still being painted, but nothing happens
	this.dis=false;		// button ist enabled as default. use disabled to make button inactive (grey & not clickable)
	if(typeof p == "object") {
		
		this.a = p.action?p.action:"back";
		this.c = p.cssclass?p.cssclass:"lnk-btn"; // css class name
		this.f = p.func?p.func:"";
		this.vf = p.valFunc?p.valFunc:"";
		this.u = p.url?p.url:"";
		this.t = p.label?p.label:"";
		this.i = p.tabindex?p.tabindex:"";
		this.id = p.id?p.id:"";
		this.shid = p.setHidden?p.setHidden:"";
		this.shv = p.setHiddenVal?p.setHiddenVal:"";
		this.fid = p.setFormId?p.setFormId:"";
		this.fa = p.setFormAction?p.setFormAction:"";
		this.dis = p.disabled?p.disabled:false;
		this.ds = p.submitId?p.submitId:""; // work around the generic form handler
		this.tf = p.trackFunc?p.trackFunc+";":"";
		this.ncurl = p.nocookieurl?p.nocookieurl:"";
		this.fh = p.fillhidden?p.fillhidden:"";
		if (this.ncurl.indexOf("http") == -1) {
			this.ncurl = "";
		}
	}
	this.init();
	this.paint();
}
Button.prototype.init = function() {
	// prod version
	if(this.shid!="") this.f += "document.getElementById(\""+this.shid+"\").value=\""+this.shv+"\";"; // sets the hidden field + value
	if(this.fh!=""){
		for(var i=0;i<this.fh.length;i++){
			this.f += "document.getElementById(\""+this.fh[i].id+"\").value=\""+this.fh[i].value+"\";"; 	
		}
	}
	if(this.fid!="" && this.fa!="") this.f += "document.getElementById(\""+this.fid+"\").action=\""+this.fa+"\";"; // sets the form id + form action 
	if(this.dis=="true") //if button is disabled just show a styled div
		this.z = "<div class=\"lnk-btn-disabled\">"+this.t+"</div>"
	else{
		// only when a nocookieurl is given me must test if the cookies are disable
		if(this.ncurl!="")
		{
			if(this.ncurl != "" && !cookieEnable() && this.a == "submit")
			{
				// cookies are disable and a form must be submitted, therefor the formaction is changed.
				if(this.ds == "") this.f += "if(!cookieEnable()){fr = new fs(this);fr.frm.action=\""+this.ncurl+"\"};";
				if(this.ds != "") this.f += "if(!cookieEnable()){document.getElementById(\""+this.ds+"\").action=\""+this.ncurl+"\"};";
			}
		}
		// Validation Handler
		var v1=v2=""; if(this.vf!=""){v1="if("+this.vf+"()){";v2 = "}";}
		var xid=" "; if(this.id!=""){xid=" id='"+this.id+"' ";}
		var ti=""; if(this.i!=""){ti=" tabindex='"+this.i+"' ";}
		
		if (this.a == "back") { // default
			this.z = "<a"+xid+"class='"+this.c+"' href='#' onclick='"+this.tf+"history.back(); return false;'"+ti+">"+this.t+"<\/a>"
		} else if (this.a == "close") {
		  	this.z = "<a"+xid+"class='"+this.c+"' href='#' onclick='"+this.tf+"window.close(); return false;'"+ti+">"+this.t+"<\/a>";
		} else if (this.a == "func") { 
		  	this.z = "<a"+xid+"class='"+this.c+"' href='#' onclick='"+v1+this.tf+this.f+v2+"; return false;'"+ti+">"+this.t+"<\/a>";
		} else if (this.a == "submit" && this.ds == "") {
			this.z = "<a"+xid+"class='"+this.c+"' href='#' onclick='"+v1+this.tf+this.f+"submitForm(this)"+v2+"; return false;'"+ti+">"+this.t+"<\/a>";
		} else if (this.a == "submit" && this.ds != "") {
			
			this.z = "<a"+xid+"class='"+this.c+"' href='#' onclick='"+v1+this.tf+this.f+"document.getElementById(\""+this.ds+"\").submit()"+v2+"; return false;'"+ti+">"+this.t+"<\/a>";
		} else if (this.a == "link") {
			if((this.vf=="")&&(this.tf=="")){
				this.z = "<a"+xid+"class='"+this.c+"' href='"+this.u+"'"+ti+">"+this.t+"<\/a>";
			}else if((this.vf!="")&&(this.tf=="")){
				this.z = "<a"+xid+"class='"+this.c+"' href='"+this.u+"' onclick='return "+this.vf+"()'"+ti+">"+this.t+"<\/a>";
			}else if((this.vf!="")&&(this.tf!="")){
				this.z = "<a"+xid+"class='"+this.c+"' href='"+this.u+"' onclick='"+v1+this.tf+" return true;}else{return false};'"+ti+">"+this.t+"<\/a>";
			}else if((this.vf=="")&&(this.tf!="")){
				this.z = "<a"+xid+"class='"+this.c+"' href='"+this.u+"' onclick='"+this.tf+" return true;'"+ti+">"+this.t+"<\/a>";
			}
		}
	}
}

Button.prototype.paint = function() { document.write(this.z); } // Button.paint();
Button.prototype.setLink = function(v){ // not in use; might be handy later on; sets the link
	function gT() { window.location.href = v; return false; }
	if (this.id != "") {
		var a = document.getElementById(this.id);
		a.onclick = gT;
	}
}
/* END: Button Object ----------------------------------------------------------------------- */