window.onload=function(){
	Initialise();
	setCombobox(true);
}
window.onresize=function(){
	setCombobox(false);
}

// ----------------------------------------------------------------- //
// Loop through each select on the page, and check its class.  If it //
// has a class of comboBox, then attach the needed elements       //
// ------------------------------------------------------------------//

var nTop;
var nLeft;
var detect = navigator.userAgent.toLowerCase();

function setCombobox(bMethod) {
	combos=getElementsByClassName('select', "comboBox");
 	for(i=0; i<combos.length; i++) {
		nTop = findPosY(combos[i])-1;
		nLeft = findPosX(combos[i]);
		if(bMethod == true) {
			inittextfield(combos[i]);
			//Use iframe hack for Internet Explorer
			if(!(detect.indexOf("opera") + 1) && (detect.indexOf("msie") + 1)) {
			initIframe(combos[i]);
			}
		}
		else{
			textfield = document.getElementById("txt" + combos[i].name);
			textfield.style.top = nTop + "px";
			textfield.style.left = nLeft + "px";
			if((detect.indexOf("msie") + 1)) {
			hackFrame = document.getElementById("frame" + combos[i].name);
			hackFrame.style.top = nTop + "px";
			hackFrame.style.left = nLeft + "px";
			}
		}
	}
}

// ------------------------------------------------------------------------ //
// Get all elements with matching class names                        //
// Courtesy of snook - http://www.snook.ca/archives/000370.html //
// ------------------------------------------------------------------------ //
function getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = document.getElementsByTagName(node);
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

// ------------------------------------------------------------------ //
// Create the textfield and move it to desired position               //
// ------------------------------------------------------------------ //

function inittextfield(ctrl) {

	selectWidth = ctrl.offsetWidth; 

    //Create textfield
    textfield = document.createElement("input");
    textfield.type = "text";
    textfield.name = "txt" + ctrl.name;
	textfield.id = "txt" + ctrl.name;
	textfield.className = "comboText";
	textfield.style.zIndex = "998";
	
	//textfield.value = "Type here.";
	//textfield.style.color = "#ccc";
	
	textfield.style.position = "absolute";
    textfield.style.top = nTop + "px";
    textfield.style.left = nLeft + "px";
	textfield.style.border = "none";
	
	//Account for Browser Interface Differences Here
	if((detect.indexOf("safari") + 1)) {
	selectButtonWidth = 18
	textfield.style.marginTop = "0px";
	textfield.style.marginLeft = "0px";
	}
	else if((detect.indexOf("opera") + 1)) {
		selectButtonWidth = 27;
		textfield.style.marginTop = "4px";
		textfield.style.marginLeft = "4px";
	}
	else {
	selectButtonWidth = 27;
	textfield.style.marginTop = "2px";
	textfield.style.marginLeft = "3px";
	}
	
	textfield.style.width = (selectWidth - selectButtonWidth) + "px";
    
    ctrl.parentNode.appendChild(textfield); 
    
	// dirty, aber mir fällt keine bessere Lösung ein
	if ( (document.getElementById("txtwort0") !=null) && (document.profil.wland0.selectedIndex == 0) ) {
		document.getElementById("txtwort0").readOnly = 1; }
	if ( (document.getElementById("txtwort1") !=null) && (document.profil.wland1.selectedIndex == 0) ) {
		document.getElementById("txtwort1").readOnly = 1; }
	if ( (document.getElementById("txtwort2") !=null) && (document.profil.wland2.selectedIndex == 0) ) {
		document.getElementById("txtwort2").readOnly = 1; }
	if ( document.getElementById("umkreis") !=null )
	{
		if ( (document.getElementsByName( "land")[0].value != 1) && (document.getElementsByName( "land")[0].value != 5) && (document.getElementsByName( "land")[0].value != 6) ) {
		    document.getElementById("plz").disabled = true;
			document.getElementById("umkreis").disabled = true; }
		else {
		    document.getElementById("plz").disabled = false;
			document.getElementById("umkreis").disabled = false; }
    }	
	
	if ( document.getElementById("suche_20") !=null )
	{
		if ( (document.getElementsByName( "land")[0].value != 1) && (document.getElementsByName( "land")[0].value != 5) && (document.getElementsByName( "land")[0].value != 6) ) {
		  document.getElementById("suche_20").disabled = true;
		  document.getElementById("suche_50").disabled = true;
		  document.getElementById("suche_100").disabled = true;
	    } else {
		  document.getElementById("suche_20").disabled = false;
		  document.getElementById("suche_50").disabled = false;
		  document.getElementById("suche_100").disabled = false;
	    }
	}
	

//  Textfeld mit dem 1. Element aus der Combobox vorbelegen
//  0. Element ist leerer Eintrag
	if ( ctrl.options.length > 1 ) {
		params=ctrl.options[1].value.split('|');
	    document.getElementById("txt" + ctrl.name).value = params[params.length-1];
	    ctrl.selectedIndex=1;
	}

//  locally overwritten; uncomment this lines if you have no local function
//	ctrl.onchange=function() {
//		optiontext = this.options[this.selectedIndex].value;	
//        document.getElementById("txt" + this.name).value = optiontext;
//	}
	
	ctrl.onfocus=function() {
		//document.getElementById("txt" + this.name).style.color = "#333";
	}

	textfield.onfocus=function() {
		//this.style.color = "#333";
	}
	
// this function doesn't work with IE;
// use onsubmit="comboOnSubmit()" instead
//
//	this.onsubmit=function() {
//		combos=getElementsByClassName('select', "comboBox");
//	 	for(i=0; i<combos.length; i++) {
//			txtvalue = document.getElementById("txt" + combos[i].name).value;
//	        document.getElementById("hidden" + combos[i].name).value = txtvalue;
//	 	}
//	}

}

// function comboOnSubmit() 
// {
// 	combos=getElementsByClassName('select', "comboBox");
//  	for(i=0; i<combos.length; i++) {
// 		txtvalue = document.getElementById("txt" + combos[i].name).value;
//         document.getElementById("hidden" + combos[i].name).value = txtvalue;
//  	}
// }


// ------------------------------------------------------------------ //
// Internet Explorer hack requires an empty iFrame.  We need to add   //
// one right underneath the div -> it will make the zindex work       //
// ------------------------------------------------------------------ //

function initIframe(ctrl) {
	textWidth = textfield.offsetWidth;
	textHeight = textfield.offsetHeight;
    hackFrame = document.createElement("iframe");
    hackFrame.setAttribute("src", "placeHolder.html");
	hackFrame.setAttribute("scrolling", "0");
	hackFrame.setAttribute("tabindex", "-1");
	hackFrame.id = "frame" + ctrl.name;
	hackFrame.style.position = "absolute";
	hackFrame.style.width = textWidth + "px";
	hackFrame.style.height = textHeight + "px";
	hackFrame.style.top = nTop + "px";
	hackFrame.style.left = nLeft + "px";
	hackFrame.style.marginTop = "3px";
	hackFrame.style.marginLeft = "3px";
	ctrl.parentNode.insertBefore(hackFrame, textfield);
}

// ------------------------------------------------------------------------------ //
//  Find the x and y position of the select box and return them                  //
// ----------------------------------------------------------------------------- //

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft-200-document.getElementById('stage').offsetLeft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop-230;
}

function updatePlzOrt(optionvalue)
{
	params = optionvalue.split('|',4);
    document.getElementById("wplz" + params[0]).value = params[2];
    document.getElementById("txtwort" + params[0]).value = params[3];
}

function updatePlzAusschreibungsort(optionvalue)
{
	params = optionvalue.split('|');
    document.getElementById("aplz").value = params[0];
    document.getElementById("txtaort").value = params[1];
}

function updateRegion(optionvalue)
{
    document.getElementById("txtstadt").value = optionvalue;
}

function updatePlz(optionvalue)
{
	params = optionvalue.split(' | ');
    document.getElementById("txtplz").value = params[1];
    document.getElementById("txtstadt").value = params[0];
}

function onChangeLand(combo,position,index)
{
  //  empty combobox list
  combo.options.length = 0;

  // set values to empty string
  document.getElementById("txtwort" + position).value = "";
  document.getElementById("wplz" + position).value = "";
	
  if (index == 0)
  {
    // set textfields to readonly
    document.getElementById("txtwort" + position).readOnly = 1;
    document.getElementById("wplz" + position).readOnly = 1;
    }
  else
  {
  	// Textfelder wieder editierbar schalten
    document.getElementById("txtwort" + position).readOnly = 0;
    document.getElementById("wplz" + position).readOnly = 0;
  	
  	// create a default combo list for the country
    combo.options.length = 0;
	curSelectBox = document.getElementsByName( "wland" + position )[0];
	dataId = "region_" + curSelectBox.value;
    if ( document.getElementById( dataId ).firstChild != null)
    {
		data = document.getElementById( dataId ).firstChild.data;
		options =  data.split( "|" );
	
		combo.options.length=0;
		combo.options[0] = new Option( " ", ""+position+"|"+i+"||", true, true );
		i = 1;
		while( i<options.length )
		{
			newOption = new Option( options[ i-1 ], ""+position+"|"+i+"||"+options[i-1], false, false );
			combo.options[ i ] = newOption;
			i++;
		}
	}
  }
}

function onChangeSearchLand(combo,index)
{  
  combo = document.getElementsByName( "region" )[0];  
  combo.options.length=0;
  curSelectBox = document.getElementsByName( "land")[0];
  dataId = "region_" + curSelectBox.value;
  if ( document.getElementById( dataId ).firstChild != null)
  {
	  data = document.getElementById( dataId ).firstChild.data;
	  options =  data.split( "|" );
		  
	  combo.options[0] = new Option( " ", "", true, true );
	  i = 1;
	  while( i<options.length )
	  {
		newOption = new Option( options[ i-1 ], options[i-1], false, false );
		combo.options[ i ] = newOption;
		i++;
	  }
  }
  
}

function onChangeAusschreibungLand(combo,index)
{
  curSelectBox = document.getElementsByName( "land")[0];
  if ( (curSelectBox.value != 1) && (curSelectBox.value != 5) && (curSelectBox.value != 6) )
  {
	  document.getElementById("suche_20").disabled = true;
	  document.getElementById("suche_50").disabled = true;
	  document.getElementById("suche_100").disabled = true;
  } else {
	  document.getElementById("suche_20").disabled = false;
	  document.getElementById("suche_50").disabled = false;
	  document.getElementById("suche_100").disabled = false;
  }
}
