var DHTML = 0, DOM = 0, MS = 0, NS = 0, OP = 0, browserOK = 1;

function DHTML_init() {

 if (window.opera) {
     OP = 1;
 }
 if(document.getElementById) {
   DHTML = 1;
   DOM = 1;
 }
 if(document.all && !OP) {
   DHTML = 1;
   MS = 1;
 }
if (window.netscape && window.screen && !DOM && !OP){
   DHTML = 1;
   NS = 1;
 }
}

function getElem(p1,p2,p3) {
 var Elem;
 if(DOM) {
   if(p1.toLowerCase()=="id") {
     if (typeof document.getElementById(p2) == "object")
     Elem = document.getElementById(p2);
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="name") {
     if (typeof document.getElementsByName(p2) == "object")
     Elem = document.getElementsByName(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="tagname") {
     if (typeof document.getElementsByTagName(p2) == "object" ||
        (OP && typeof document.getElementsByTagName(p2) == "function"))
     Elem = document.getElementsByTagName(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
 else if(MS) {
   if(p1.toLowerCase()=="id") {
     if (typeof document.all[p2] == "object")
     Elem = document.all[p2];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="tagname") {
     if (typeof document.all.tags(p2) == "object")
     Elem = document.all.tags(p2)[p3];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="name") {
     if (typeof document[p2] == "object")
     Elem = document[p2];
     else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
 else if(NS) {
   if(p1.toLowerCase()=="id" || p1.toLowerCase()=="name") {
   if (typeof document[p2] == "object")
     Elem = document[p2];
     else Elem = void(0);
     return(Elem);
   }
   else if(p1.toLowerCase()=="index") {
    if (typeof document.layers[p2] == "object")
     Elem = document.layers[p2];
    else Elem = void(0);
     return(Elem);
   }
   else return void(0);
 }
}

function getCont(p1,p2,p3) {
   var Cont;
   if(DOM && getElem(p1,p2,p3) && getElem(p1,p2,p3).firstChild) {
     if(getElem(p1,p2,p3).firstChild.nodeType == 3)
       Cont = getElem(p1,p2,p3).firstChild.nodeValue;
     else
       Cont = "";
     return(Cont);
   }
   else if(MS && getElem(p1,p2,p3)) {
     Cont = getElem(p1,p2,p3).innerText;
     return(Cont);
   }
   else return void(0);
}

function getAttr(p1,p2,p3,p4) {
   var Attr;
   if((DOM || MS) && getElem(p1,p2,p3)) {
     Attr = getElem(p1,p2,p3).getAttribute(p4);
     return(Attr);
   }
   else if (NS && getElem(p1,p2)) {
       if (typeof getElem(p1,p2)[p3] == "object")
        Attr=getElem(p1,p2)[p3][p4]
       else
        Attr=getElem(p1,p2)[p4]
         return Attr;
       }
   else return void(0);
}

function setCont(p1,p2,p3,p4) {
   if(DOM && getElem(p1,p2,p3) && getElem(p1,p2,p3).firstChild)
     getElem(p1,p2,p3).firstChild.nodeValue = p4;
   else if(MS && getElem(p1,p2,p3))
     getElem(p1,p2,p3).innerText = p4;
   else if(NS && getElem(p1,p2,p3)) {
     getElem(p1,p2,p3).document.open();
     getElem(p1,p2,p3).document.write(p4);
     getElem(p1,p2,p3).document.close();
   }
}

DHTML_init();


var pics = new Array();
var objCount = 0; // number of (changing) images on web-page
var activePic = '';

function preload(name, first, second) {  

  // preload images and place them in an array

  if (browserOK) {     
    pics[objCount] = new Array(3);
    pics[objCount][0] = new Image();
    pics[objCount][0].src = first;
    pics[objCount][1] = new Image();
    pics[objCount][1].src = second;
    pics[objCount][2] = name;
    objCount++;
  }
}

function on(name){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      if (document.images[pics[i][2]] != null)
        if ((name != pics[i][2]) & (document.images[pics[i][2]] != activePic)) { 
          // set back all other pictures
          document.images[pics[i][2]].src = pics[i][0].src;
        } else {
           // show the second image because cursor moves across this image
           document.images[pics[i][2]].src = pics[i][1].src;
        }
    }
  }
}

function off(){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      // set back all pictures
      if ((document.images[pics[i][2]] != null) & (document.images[pics[i][2]] != activePic))
        document.images[pics[i][2]].src = pics[i][0].src;
    }
  }
}

function loadpage(url) { //v3.0
  window.location.href=url;
}

function toggleAllCheckboxesInForm(formName, isChecked) {
  var targetForm = document.forms[formName];
	var earray = document.forms[formName].elements;
	for (var i = 0; i < earray.length; i++) {
		if (earray[i].type == 'checkbox') {
		    earray[i].checked = isChecked;
		}
	}  
}

function toggleIFrame(containerId, iframeUrl) {
	var container = document.getElementById("container_"+containerId);
	var iframe = document.getElementById("iframe_"+containerId);
	// Toggle the visibility of the container
	container.style.display = (container.style.display=="block" ? "none" : "block");
//		alert("#"+iframe.src+"#");
	if (iframe.src=="") {
		iframe.src=iframeUrl;
	}
}

function openwin(x,y){ var PD = open(x,y,'width=780,height=550'); }
function openwin2(x,y){ var PD = open(x,y,'width=780,height=400,scrollbars=yes'); }
function openinfo(x,y){ var PD = open(x,y,'width=730,height=260,scrollbars=yes'); }

function setHelpText(text) {
	document.getElementById("helptext").innerHTML=text;
}

function validatePositiveNumber(inputId) {
  var field = document.getElementById(inputId);
	var value = field.value;

	if (value.indexOf(".")>=0 || value.indexOf(",")>=0) {
		alert("Bitte geben Sie einen Wert ohne Kommastelle ein.");
		field.focus();
		return false;
	}
	else if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
		alert("Ungültige Nummer");
		field.focus();
		return false;
	}
	else if (value < 0) {
		alert("Der Eingabewert darf nicht kleiner als 0 sein");
		field.focus();
		return false;
	}
	return true;
}

function validatePositiveNumberOrEmpty(inputId) {
    var field = document.getElementById(inputId);
	var value = field.value;
	if(value==""){			
			return true;
	}
	if (value.indexOf(".")>=0 || value.indexOf(",")>=0) {
		alert("Bitte geben Sie einen Wert ohne Kommastelle ein.");
		field.focus();
		return false;
	}
	else if (isNaN(value) || value=="" ||  value.indexOf(" ")>=0) {
		alert("Ungültige Nummer");
		field.focus();
		return false;
	}
	else if (value < 0) {
		alert("Der Eingabewert darf nicht kleiner als 0 sein");
		field.focus();
		return false;
	}
	return true;
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function setInfoText(htmlContent) {
	//document.getElementById('infobox').innerHTML=htmlContent;
}

// sets the one time discount option to false if the discount option is set to false
function setDiscountOption(){
	var discount = document.getElementById("discount_rec_select").value;
	var discount_sel=document.getElementById("discount_select");
	var discount_label=document.getElementById("discount_label");
	if(discount=="false"){		
		discount_sel.value="false";
		//discount_sel.style.visibility = 'hidden';
		//discount_label.style.visibility = 'hidden';
	}
	else if(discount=="true"){
		discount_sel.value="true";
		//discount_sel.style.visibility = 'visible';
		//discount_label.style.visibility = 'visible';
	}
}
// sets the discount option to false if the one time discount option is set to false
function setDiscountRecOption(){
	var discount = document.getElementById("discount_select").value;
	
	if(discount=="true"){
		document.getElementById("discount_rec_select").value="true";		
	}
}

function trim(str) { 
	return str.replace(/^\s+|\s+$/g, ''); 
}

function setIframeHeight(iframeId, height){
	var iframe=document.getElementById(iframeId);
	iframe.height=height;	
}		


function closeLayers(){
	Windows.closeAll();
}

function showLayer(width, height, url){
	var win = new Window({
				draggable:false, 
				minimizable:false,
				maximizable:false,
				resizable:false,
				title: "",
				top: 0,
				left: 0,
				width: width,
				height: height,
				url: url,
				destroyOnClose: true
			});
	win.showCenter(true);
}


function disableDojoSelects(selectIds){
	for(var i=0; i<selectIds.length; i++){
		var s = dojo.widget.byId(selectIds[i]);
		if (s) {
//			s.textInputNode.disabled = true;
			s.textInputNode.readOnly = true;
		}
	}
}

function setDojoSelect(widgetId, label, value){
	var widget = dojo.widget.byId(widgetId);
	if(widget){
		widget.comboBoxValue.value = value;
		widget.textInputNode.value = label;
		widget.comboBoxSelectionValue.value = label;
	}
}


function addOnChangeListenerToFormElements(listener){
	var y = document.forms[0];
	if(y){
		
		for (i=0;i<y.length;i++) {
			if (y[i].addEventListener){
			  y[i].addEventListener('change', listener, false); 
			} else if (y[i].attachEvent){
			  y[i].attachEvent('onchange', listener);
			}
		}
	}
	/*		y[i].addEventListener('change', listener, false);
			alert(y[i].getAttribute('onchange'));
			if(y[i].getAttribute('onchange'))				   
				y[i].setAttribute('onchange',(y[i].getAttribute('onchange')+listener));
			else
				y[i].setAttribute('onchange',listener);
			alert(y[i].getAttribute('onchange'));*/
		
}


//************************* cookies ********************************//

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


//************************* ajax *******************************//

function getSessionVariable(name){
	var call = new Ajax.Request(('SessionService.ajax?function=getVariable&name='+name), {
		method: 'post',
		asynchronous:false,
		onSuccess: function(transport){
			response = transport.responseText || "no response text";
			//alert("Success! \n\n" + response);
		},
		onFailure: function(){
			response = null;
			//alert('Something went wrong...')
		}
	});
	return call.transport.responseText;
}
function getVnbName(id){
	var call =new Ajax.Request(('SessionService.ajax?function=getVnbName&vnbId='+id), {
		method: 'post',
		asynchronous:false,
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			//alert("Success! \n\n" + response);
		},
		onFailure: function(){
			//alert('Something went wrong...')
		}
	});
	
}

function setSessionVariable(name, value){
	//alert("saving session var: "+name +"|"+value);
	var call = new Ajax.Request(('SessionService.ajax?function=setVariable&name='+name+'&value='+value), {
		method: 'post',
		asynchronous:true,
		onSuccess: function(transport){
			//response = transport.responseText || "no response text";
			//alert("Success! \n\n" + response);
		},
		onFailure: function(){
			//response = null;
			//alert('Something went wrong...')
		}
	});
}

function saveDojoToSession(widgetId, varLabelName, varValueName){
	if(initDone){
		var widget = dojo.widget.byId(widgetId);
		if(widget){
			setSessionVariable(varLabelName, widget.textInputNode.value);
			setSessionVariable(varValueName, widget.comboBoxValue.value);
		}
	}
}

