/********************************************************************************************************************************
 *
 *  Default Script für Umfragetool
 *
 ********************************************************************************************************************************/     

// Zustaetzliche Hilfefunktionen fuer Eingabevalidierung
function checkTextLimit(field, maxlen ) { return (field.value.length <= maxlen); }
function validateText(field, regex, msg) { if (!((regex=='') || (field.value.match(regex)))) { field.focus(); alert(msg); } }

function modfield(name) {
	if ( $("modfields") ) {
	  temp=document.getElementById('modfields').value;
	  if (temp=='') {
	    document.getElementById('modfields').value=name;
	  } else {
	    myarray=temp.split('|');
	    if (!inArray(name, myarray)) {
	      myarray.push(name);
	      document.getElementById('modfields').value=myarray.join('|');
	    }
	  }
	}
	 
	if ( FilterObj ) {
		FilterObj.filterMods(name);
	}
}


function toggleOtherOption( id, sender ) {
	var other = $(id);
	if (!other) return;

	switch ( true ) {
		// Auswahlliste (Radiobutton)
		case ((sender != null) && (sender.nodeName == "INPUT") && (sender.type.toUpperCase() == "RADIO")):
			other.style.visibility = ( $F(sender) == "other" ? "visible" : "hidden" );
		break;

        // Mehrfachauswahl
        case ((sender != null) && (sender.nodeName == "INPUT") && (sender.type.toUpperCase() == "CHECKBOX")):
            if ( sender.checked ) {
                other.style.visibility = "visible";
            } else {
                other.style.visibility = "hidden";
            }
        break;

		// Alle anderen
		default:
			if ( other.style.visibility == "visible" ) {
				other.style.visibility = "hidden";
			} else {
				other.style.visibility = "visible";
			}				
		break;		
	}
	

}


function inArray(needle, haystack) {
  for (h in haystack) {
    if (haystack[h] == needle) {
    return true;
    }
  }
  
  return false;
} 


function radio_unselect(radioObj) {
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++)
	{
		radioObj[i].checked = false;
	}
}


function textLimit(field, maxlen) {
  if (document.getElementById(field).value.length > maxlen)
    document.getElementById(field).value = document.getElementById(field).value.substring(0, maxlen);
}

function checkmaxanswers( checkbox, MaxAnswers, Msg ) {
	if ( MaxAnswers == null ) return true;
	
	//var ContainerID = checkbox.parentNode.parentNode.parentNode.parentNode.id;
	var ContainerID;
	var pNode = checkbox;

	do {
		pNode = pNode.parentNode;		
		if ( pNode != null ) {
			if ( pNode.className == "Container" ) {
				ContainerID = pNode.id;
				break;
			}
		}
	} while ( pNode != null );

	
	var checkboxlist = $$("div#"+ContainerID+" table tr td input[type=checkbox]");
	var counter = 0;
  
	for (i=0; i < checkboxlist.length; i++ ) {
		if ( checkboxlist[i].checked ) counter++;
	}
  
	if ( (MaxAnswers != null) && (counter > MaxAnswers) ) {
		checkbox.checked = false;
		alert(Msg);
		return false;
	}
	
	return true;
}

function insertToList( selbox, maxcount ) {
  if ( (selbox.options.length == 0) || (selbox.options.selectedIndex < 0 ) ) {
    return;
  }

  var nextCount = ( maxcount - selbox.options.length);

  var hbox = null;
  var tbox = null;  
  for (i=nextCount; i > 0; i--) {
      hbox = $("hbox_"+i+"_"+selbox.id);
      tbox = $("tbox_"+i+"_"+selbox.id);
      
      if ( (tbox.value=="") && (hbox.value=="") ) {
        nextCount = i;
      }
  }

  hbox = $("hbox_"+nextCount+"_"+selbox.id);
  tbox = $("tbox_"+nextCount+"_"+selbox.id);
  var option = selbox.options[ selbox.options.selectedIndex ];
  
  tbox.value = option.text;
  hbox.value = option.value;
  
  selbox.options[ selbox.options.selectedIndex ] = null; 
}

function removeFromList( count, selboxid ) {
  var selbox = $(selboxid);
  var tbox = $("tbox_"+count+"_"+selboxid);
  var hbox = $("hbox_"+count+"_"+selboxid);
  var Opt = new Option( tbox.value, hbox.value,false,false );
  
  if ( (tbox.value != "") && (hbox.value != "") ) { 
    selbox.options[ selbox.options.length ] = Opt;
    tbox.value = "";
    hbox.value = "";
  }
}

 function getkey(e){
  if (window.event) return window.event.keyCode;
    else if (e) return e.which; else return null;
}

function goodchars(e, goods) {
  var key, keychar;
  key = getkey(e);
  if (key == null) return true;

  // get character
  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  goods = goods.toLowerCase();

  // check goodkeys
  if (goods.indexOf(keychar) != -1)
    return true;

  // control keys
  if ( key==null || key==0 || key==8 || key==9  || key==27 )
    return true;

  // else return false
  return false;
}


function adjust_remaining( obj, maxAllocation, allocid, msg ) {
	 maxAllocation = parseInt($F(maxAllocation),10);
	 var container = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
  
	 var cid = "";
	
	 if ( container && container.id != "" ) {
	 	cid = container.id;
	 } else {
	 	cid = "Container";
	 }
  
    var eleList = $$("div#"+cid+" table tr td input[type=text]");
  	var currentCount = 0;
  
	  for (i=0; i < eleList.length; i++ ) {
	    if ( isNaN(parseInt(eleList[i].value,10)) ) { continue; }
	    currentCount += parseInt(eleList[i].value,10);
	  }
	  
	  if ( currentCount > maxAllocation ) {
	    obj.value = "";
	    obj.focus();
	    alert(msg);
	  } else {
	   $(allocid).innerHTML = (maxAllocation - currentCount); 
	  }
}




var Highlighter = [];

function highlightcontrol( event, ctrl, state ) {
	if ( state == false ) {
		for (i=0; i < Highlighter.length; i++ ) {
			if ( Highlighter[i].Instance == ctrl) {
				ctrl.style.backgroundColor = "";
				Event.stopObserving( ctrl, "blur", Highlighter[i].Handle );
				Highlighter.splice(i,1);
				break;		
			}
		}
	} else if (state == true) {
		ctrl.style.backgroundColor = "#FFFF99";
		var info = {
			Instance : ctrl,
			Handle : highlightcontrol.bindAsEventListener(null,ctrl,false)
		};		
		Highlighter.push(info);	
		Event.observe( ctrl, "blur", info.Handle);	
	}
}
