/**
 *  File for general element-handling in searchForm.jsp, like adding items to lists, changing
 *  styles etc.
 */

//---- general element-handlers --------

function resetAllLists(){
  document.getElementById("countyList").selectedIndex = 0;
  DWRUtil.removeAllOptions("municipalityList");
  DWRUtil.addOptions("municipalityList", [propertyChooseMunicipality]);
  DWRUtil.removeAllOptions("settlementList");
  DWRUtil.addOptions("settlementList", [propertyChooseCity]);
  deleteAllCookies();
}

function resetSettlementList(){
  DWRUtil.removeAllOptions("settlementList");
}

function resetMunicipalityList(){
  DWRUtil.removeAllOptions("municipalityList");
}

function deleteAllCookies(){
  if(getCookieWrapper("ms", "countyList")!=null)
    deleteCookieWrapper("ms", "countyList");
  if(getCookieWrapper("ms", "municipalityList")!=null)
    deleteCookieWrapper("ms", "municipalityList");
  if(getCookieWrapper("ms", "settlementList")!=null)
    deleteCookieWrapper("ms", "settlementList");
}

function disableAllLists(){
  document.getElementById('countyList').disabled=true;
  document.getElementById('municipalityList').disabled=true;
  document.getElementById('settlementList').disabled=true;
}
function enableAllLists(){
  document.getElementById('countyList').disabled=false;
  document.getElementById('municipalityList').disabled=false;
  document.getElementById('settlementList').disabled=false;
}
function setMessageAllLists(msg){
  addOptionsDefault("countyList", [msg]);
  addOptionsDefault("municipalityList", [msg]);
  addOptionsDefault("settlementList", [msg]);
}

function goSweLang(){
  var lang="locale=sv_SE";
  var url=window.location+"";

  if(url.charAt(url.length-1)=="#"){
    url=url.substring(0,url.length-1);
  }

  if(url.indexOf("locale=en_US")!=-1){
    url = url.replace("locale=en_US","locale=sv_SE");
    document.location.href=url;
  }
  else{
    if(url.substring(url.length-4,url.length)=="html"){
       url=url+"?"+lang;
       document.location.href=url;
    }
    else{
      if(url.indexOf("locale=sv_SE")==-1){
        url=url+"&"+lang;
        document.location.href=url;
      }
    }
  }

}

function goEngLang(){
  var lang="locale=en_US";
  var url=window.location+"";

  if(url.charAt(url.length-1)=="#"){
    url=url.substring(0,url.length-1);
  }

  if(url.indexOf("locale=sv_SE")!=-1){
    url = url.replace("locale=sv_SE","locale=en_US");
    document.location.href=url;
  }
  else{
    if(url.substring(url.length-4,url.length)=="html"){
       url=url+"?"+lang;
       document.location.href=url;
    }
    else{
      if(url.indexOf("locale=en_US")==-1){
        url=url+"&"+lang;
        document.location.href=url;
      }
    }
  }
}


function addOptions2(ele, data) {

  var text, value;
  var orig = ele;
  ele = $(ele);

  if (ele == null) {
    DWRUtil.debug("addOptions() can't find an element with id: " + orig + ".");
    return;
  }
  var useOptions = DWRUtil._isHTMLElement(ele, "select");
  if (!useOptions) {
    DWRUtil.debug("addOptions() can only be used with selectelement s. Attempt to use: " + DWRUtil._detailedTypeOf(ele));
    return;
  }
  if (data == null){
	  DWRUtil.debug("no data");
	  return;
  }
  if (!DWRUtil._isArray(data)) {
  	  DWRUtil.debug("illegal data");
	  return;
  }

  for (var i = 0; i < data.length; i++) {
	    text = data[i].name;
            value = data[i].xcoords+":"+data[i].ycoords;
	 if (text || value) {
            opt = new Option(text, value);
            ele.options[ele.options.length] = opt;
	  }
  }
}

function showHide(id){
           var target=document.getElementById(id);
                 if ((target.style.display == "") || (target.style.display == "none"))
    		{
                  target.style.display = "block";
                }
                 else
                {
                  target.style.display = "none";
    		}
  }


function alternateSetColor(i){
  if(i<9){
  var color="#"+i+""+i+"f";
  document.getElementById('infoContainer').style.backgroundColor=color;
  i++;
  setTimeout('alternateSetColor('+i+')', 40);
  }
  else{
    document.getElementById('infoContainer').style.backgroundColor="#f0f0f0";
    return;
  }
}

function alternateSetColor2(i){
  if(i<9){
  var color="#"+i+""+i+"f";
  document.getElementById('infoContainer2').style.backgroundColor=color;
  i++;
  setTimeout('alternateSetColor2('+i+')', 40);
  }
  else{
    document.getElementById('infoContainer2').style.backgroundColor="#f0f0f0";
    return;
  }
}

function setColor(i){
  if(i<9){
  var color="#f"+i+""+i;
  document.getElementById('infoContainer').style.backgroundColor=color;
  i++;
  setTimeout('setColor('+i+')', 40);
  }
  else{
    document.getElementById('infoContainer').style.backgroundColor="#f0f0f0";
    return;
  }
}

// ---- / general element-handlers --------
