// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	

//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
//	alert(to);
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
	if (!hasOptions(from)) { return; }
	//alert(from.options.length);
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected || o.value == arguments[3]) {
		//alert('value: '+o.value);
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}


// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}


// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		var o=from.options[i]; 
		if (o.selected) { 
			from.options[i] = null; 
			} 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 



// from html

function Selectvalgte(obj, str1)
{
	for (i=0; i<obj.options.length; i++) {
	var s = str1.indexOf(obj.options[i].value)
	obj.options[i].selected = (s>=0);
	}
}

function SkiftTilNyKommune(kortfunc, mapfunc) {
//	alert("tjek");
   document.fynkort.src='fileadmin/templates/gfx/'+kortfunc;
   document.fynkort.useMap=mapfunc;
//  alert(document.all.copy5.style.display);
   document.all.copy1.style.display='none';
   document.all.copy2.style.display='none';
   document.all.copy3.style.display='none';
   document.all.copy4.style.display='none';
   document.all.copy5.style.display='block';
   document.all.copy6.style.display='block';
//   alert(document.all.copy5.style.display);

//   document.all.copySelec3.style.display='block';
 //  document.all.RemoveSelec3.style.display='block';
   document.all.copyAll3.style.display='block';
//   alert(document.all.copyAll3.style.display);
   document.all.removeAll3.style.display='block';
//	alert(document.all.removeAll3.style.display);
   
   document.all.copySelec2.style.display='none';
   document.all.RemoveSelec2.style.display='none';
   document.all.copyAll2.style.display='none';
   document.all.removeAll2.style.display='none';

//   document.all.copySelec1.style.display='none';
//   document.all.RemoveSelec1.style.display='none';
   document.all.copyAll1.style.display='none';
//   alert(document.all.removeAll1.style.display);
   document.all.removeAll1.style.display='none';
//   alert(document.all.removeAll1.style.display);
   
}

function SkiftTilKommune(kortfunc, mapfunc) {
	alert("gamle kommuner");
   document.fynkort.src='fileadmin/templates/gfx/'+kortfunc;
   document.fynkort.useMap=mapfunc;
   document.all.copy1.style.display='none';
   document.all.copy2.style.display='none';
   document.all.copy3.style.display='block';
   document.all.copy4.style.display='block';
   document.all.copy5.style.display='none';
   document.all.copy6.style.display='none';

   document.all.copySelec3.style.display='none';
   document.all.RemoveSelec3.style.display='none';
   document.all.copyAll3.style.display='none';
   document.all.removeAll3.style.display='none';

   document.all.copySelec2.style.display='block';
   document.all.RemoveSelec2.style.display='block';
   document.all.copyAll2.style.display='block';
   document.all.removeAll2.style.display='block';

   document.all.copySelec1.style.display='none';
   document.all.RemoveSelec1.style.display='none';
   document.all.copyAll1.style.display='none';
   document.all.removeAll1.style.display='none';
   
}

function SkiftTilPostnr(kortfunc, mapfunc) {
   document.fynkort.src='fileadmin/templates/gfx/'+kortfunc;
   document.fynkort.useMap=mapfunc;
   document.all.copy1.style.display='block';
   document.all.copy2.style.display='block';
   document.all.copy3.style.display='none';
   document.all.copy4.style.display='none';
   document.all.copy5.style.display='none';
   document.all.copy6.style.display='none';

//   document.all.copySelec3.style.display='none';
 //  document.all.RemoveSelec3.style.display='none';
   document.all.copyAll3.style.display='none';
   document.all.removeAll3.style.display='none';

   document.all.copySelec2.style.display='none';
   document.all.RemoveSelec2.style.display='none';
   document.all.copyAll2.style.display='none';
   document.all.removeAll2.style.display='none';

 //  document.all.copySelec1.style.display='block';
 //  document.all.RemoveSelec1.style.display='block';
   document.all.copyAll1.style.display='block';
   document.all.removeAll1.style.display='block';
}

function Copy1FraKort(id) { 
    if(!document.forms[2].copy1.options[id].selected)
      document.forms[2].copy1.options[id].selected= true;
    else
    {
      document.forms[2].copy1.options[id].selected= false;
      document.forms[2].copy1.options[id].selected= true;
  	}
	copySelectedOptions(document.forms[2]['copy1'],document.forms[2]['copy2'],true);
//	removeSelectedOptions(document.forms[2]['copy1']);
	document.forms[2]['copy1'].selectedIndex = -1;
}

function Copy2FraKort(id) { 
    if(!document.forms[2].copy3.options[id].selected)
      document.forms[2].copy3.options[id].selected= true;
    else
    {
      document.forms[2].copy3.options[id].selected= false;
      document.forms[2].copy3.options[id].selected= true;
  	}
	copySelectedOptions(document.forms[2]['copy3'],document.forms[2]['copy4'],true);
//	removeSelectedOptions(document.forms[2]['copy3']); 
	document.forms[2]['copy3'].selectedIndex = -1;
}

function Copy3FraKort(id) { 
    if(!document.forms[2].copy5.options[id].selected)
      document.forms[2].copy5.options[id].selected= true;
    else
    {
      document.forms[2].copy5.options[id].selected= false;
      document.forms[2].copy5.options[id].selected= true;
  	}
	copySelectedOptions(document.forms[2]['copy5'],document.forms[2]['copy6'],true);
//	removeSelectedOptions(document.forms[2]['copy5']);
	document.forms[2]['copy5'].selectedIndex = -1;
}

