/**
 *  Major javascript-file for searchResult.jsp, handles init-methods, global parameters and onchangemethods (BUT NOT FOR MAP)
 *
 */

 // ---- constants & globals --------
var  firstline;
var  secondline;
var  thirdline;

// ---- constants & globals --------


// ---- init-methods --------

function loadInstructions(){
  setGlobals();//Call for globals
}



/*function buildInstructions(){
  document.getElementById("instructionsSearch").style.display="none";


  var div = document.getElementById("instructionsSearch");
  var line1 = document.createElement("p");
  var line2 = document.createElement("p");
  var line3 = document.createElement("p");

  line1.appendChild(document.createTextNode(firstline));
  line2.appendChild(document.createTextNode(secondline));
  line3.appendChild(document.createTextNode(thirdline));

  div.appendChild(line1);
  div.appendChild(line2);
  div.appendChild(line3);
}*/

function setGlobals(){
  //fetch rest of globalpropertys from ajaxfacade
  var params= new Array(3);
  params[0] ="searchForm.instructions.firstline";
  params[1] ="searchForm.instructions.secondline";
  params[2] ="searchForm.instructions.thirdline";

  PropertyClass.getPropertyValues(params,{callback:setGlobalsReceiverFunc, errorHandler:majorErrorHandler});
}

function setGlobalsReceiverFunc(data){
  firstline=data[0];
  secondline=data[1];
  thirdline=data[2];
  //buildInstructions();
}

function fillCountys(){
  SearchClass.getCountys({callback:geoAreaCountyReceiverFunc,
   errorHandler:fillListsErrorHandler});
}
// ---- / init-methods --------
// ---- onchange-methods --------

function fillMunicipality(coords, county){

	if(document.getElementById("countyList").selectedIndex!=0){
	  deleteAllCookies();
	  if(document.getElementById("countyList").selectedIndex!=0 && document.getElementById("countyList").selectedIndex!=-1){
	    setCookieWrapper("ms","countyList", document.getElementById("countyList").selectedIndex);
	  }

	  resetMunicipalityList();//Reset municipality droplist
	  resetSettlementList();//Reset settlement droplist

	  var arrayCoords = coords.split(":");
	  setMapByCoords(arrayCoords[1], arrayCoords[0]);//Call to pan the map to new location given by coords
	  SearchClass.getMunicipalitys(county, {callback:geoAreaMunicipalityReceiverFunc, errorHandler:fillListsErrorHandler});
	}else{
	  resetAllLists();
	}

}

function fillSettlement(){
	if(document.getElementById("municipalityList").selectedIndex!=0){
	  deleteAllCookies();
	  if(document.getElementById("municipalityList").selectedIndex!=0&&document.getElementById("municipalityList").selectedIndex!=-1)
	    setCookieWrapper("ms","municipalityList", document.getElementById("municipalityList").selectedIndex);

	  if(document.getElementById("countyList").selectedIndex!=0&&document.getElementById("countyList").selectedIndex!=-1)
	    setCookieWrapper("ms","countyList", document.getElementById("countyList").selectedIndex);

	  var county = document.getElementById('countyList').options[document.getElementById('countyList').selectedIndex].text;
	  var municipality = document.getElementById('municipalityList').options[document.getElementById('municipalityList').selectedIndex].text;
	  var coords =document.getElementById('municipalityList').options[document.getElementById('municipalityList').selectedIndex].value;
	  var arrayCoords = coords.split(":");
	  setMapByCoords(arrayCoords[1], arrayCoords[0]);//Call to pan the map to new location given by coords
	  SearchClass.getSettlements(county, municipality, {callback:geoAreaSettlementReceiverFunc, errorHandler:fillListsErrorHandler});
	}else{
    //delete cookies
    if(getCookieWrapper("ms", "municipalityList")!=null)
      deleteCookieWrapper("ms", "municipalityList");
    if(getCookieWrapper("ms", "settlementList")!=null)
      deleteCookieWrapper("ms", "settlementList");
    //set initial values
    document.getElementById("municipalityList").selectedIndex = 0;
    DWRUtil.removeAllOptions("settlementList");
    DWRUtil.addOptions("settlementList", [propertyChooseCity]);
  }
}

function showSettlement(){
  if(document.getElementById("settlementList").selectedIndex!=0){
    if(document.getElementById("settlementList").selectedIndex!=0&&document.getElementById("settlementList").selectedIndex!=-1)
     setCookieWrapper("ms","settlementList", document.getElementById("settlementList").selectedIndex);
  	var coords =document.getElementById('settlementList').options[document.getElementById('settlementList').selectedIndex].value;
  	var arrayCoords = coords.split(":");
  	setMapByCoords(arrayCoords[1], arrayCoords[0]);//Call to pan the map to new location given by coords
  } else{
    //delete cookie
    if(getCookieWrapper("ms", "settlementList")!=null)
      deleteCookieWrapper("ms", "settlementList");
    //set initial values
    document.getElementById("settlementList").selectedIndex = 0;
  }
}

function setMapByCoords(x,y){//Pan the map to its new position after user has change the values in the dropdownlists
   xVal=parseInt(x);
   yVal=parseInt(y);
  //setimageXYStartvalue(x,y);//New startvalue for the map from coords from droplist
  //resetMap();//Reset the map

  //moveCrossAfterDropDownlist(x,y);//Move the cross in overview images
  dropDownListCoords=true;
  orderMapFromKartaDirekt();

}

// ---- / onchange-methods --------

// ---- AJAX-reciever methods --------

function geoAreaCountyReceiverFunc(data){

  DWRUtil.removeAllOptions("countyList");
  DWRUtil.addOptions("countyList", [propertyChooseCounty]);
  addOptions2("countyList", data);

  if(getCookieWrapper("ms", "countyList")!=null){
    document.getElementById("countyList").selectedIndex=getCookieWrapper("ms", "countyList");
    if(document.getElementById("countyList").options[document.getElementById("countyList").selectedIndex].text!="Select County" && document.getElementById("countyList").options[document.getElementById("countyList").selectedIndex].text!="Välj län"){
      SearchClass.getMunicipalitys(document.getElementById("countyList").options[document.getElementById("countyList").selectedIndex].text, {callback:geoAreaMunicipalityReceiverFunc, errorHandler:fillListsErrorHandler});
    }
  }
}

function geoAreaMunicipalityReceiverFunc(data){
    DWRUtil.removeAllOptions("municipalityList");
    DWRUtil.addOptions("municipalityList", [propertyChooseMunicipality]);
    addOptions2("municipalityList", data);
    DWRUtil.removeAllOptions("settlementList");
	DWRUtil.addOptions("settlementList", [propertyChooseCity]);

  if(getCookieWrapper("ms", "municipalityList")!=null){
    document.getElementById("municipalityList").selectedIndex=getCookieWrapper("ms", "municipalityList");
    var county = document.getElementById('countyList').options[document.getElementById('countyList').selectedIndex].text;
    var municipality = document.getElementById('municipalityList').options[document.getElementById('municipalityList').selectedIndex].text;
    SearchClass.getSettlements(county, municipality, {callback:geoAreaSettlementReceiverFunc, errorHandler:fillListsErrorHandler});
}
}

function geoAreaSettlementReceiverFunc(data){
  DWRUtil.removeAllOptions("settlementList");
  DWRUtil.addOptions("settlementList", [propertyChooseCity]);
  addOptions2("settlementList", data);
  if(getCookieWrapper("ms", "settlementList")!=null){
    document.getElementById("settlementList").selectedIndex=getCookieWrapper("ms", "settlementList");
  }

}


// ---- AJAX-reciever methods --------


// ---- error-methods --------

function fillListsErrorHandler(errorString, exception) {
  clearAllLists();
  setMessageAllLists(exception.msg);
  disableAllLists();
}

function majorErrorHandler(errorString, exception) {
  var ele=document.getElementById('infoContainer');
  ele.style.display = "block";
  while (ele.childNodes.length > 0)
     ele.removeChild(ele.firstChild); //remove original text

  var img = document.createElement("img");
  img.src="../images/error.gif";
  img.alt="";
  ele.appendChild(img); //add warning image
  ele.appendChild(document.createTextNode(exception.msg)); //sets error-message text

  //animera meddelandet
  setColor(0);
  ele.style.border="1px solid #f00";
  ele.style.padding="10px";
}
// ---- / error-methods --------

