//==================================================================================
//==================================================================================
//
// GisForm
// OO-PROGRAMMIERUNG
// AUTOR: DF
// © rdts AG
// ERSTELLT: 31.01.2007
//
//==================================================================================
//==================================================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

function GisForm(id){
  this.id = undefined;
  this._mode = undefined;
  this._status = undefined;
  this._statusControlElementID = undefined;
  this._savedAdressStr = '';
  this._formID = undefined;
  this._fieldMap = {};
  this._fieldMapLength = [];
  this._fieldMapHidden = {};
  this._fieldMapHiddenLength = [];
  this._countryMap = {};
  this._countryMapLength = [];
  
  this.setID(id);
  this.setFormID(this.id);
}

// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// ------------------------------------------------------------
// Öffentliche Instanzmethoden
// ------------------------------------------------------------

// Set-Methoden


GisForm.prototype.setID = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setID: Argument ist nicht vom Typ String!");
  }
  this.id = str;
}

GisForm.prototype.setMode = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setMode: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setMode: Argument ist nicht vom Typ String!");
  }
  this._mode = str;
}

GisForm.prototype.setStatus = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setStatus: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setStatus: Argument ist nicht vom Typ String!");
  }
  this._status = str;
}

GisForm.prototype.setFormID = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setFormID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setFormID: Argument ist nicht vom Typ String!");
  }
  this._formID = str;
}

GisForm.prototype.setStatusControlElementID = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setStatusControlElementID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setStatusControlElementID: Argument ist nicht vom Typ String!");
  }
  this._statusControlElementID = str;
}

GisForm.prototype.getStatusControlElement = function() {
  if(! document.getElementById(this._statusControlElementID)){
    focus();
    throw new Error("GisForm->gisSetCoordinations: Es existiert kein Status-Steuerelement mit ID "+ this._statusControlElementID +"!");
  }
  return document.getElementById(this._statusControlElementID);
}

GisForm.prototype.setSavedAdressStr = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->setSavedAdressStr: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->setSavedAdressStr: Argument ist nicht vom Typ String!");
  }
  this._savedAdressStr = str;
}

GisForm.prototype.addFieldMap = function(strKey, strValue) {
  if (arguments.length != 2) {
    focus();
    throw new Error("GisForm->addFieldMap: Falsche Anzahl von Argumenten!");
  }
  if (typeof strKey != "string") {
    focus();
    throw new Error("GisForm->addFieldMap: Argument strKey ist nicht vom Typ String!");
  }
  if (typeof strValue != "string") {
    focus();
    throw new Error("GisForm->addFieldMap: Argument strValue ist nicht vom Typ String!");
  }
  this._fieldMap[strKey] = strValue;
  this._fieldMapLength.push(strKey);
}

GisForm.prototype.addFieldMapHidden = function(strKey, strValue) {
  if (arguments.length != 2) {
    focus();
    throw new Error("GisForm->addFieldMapHidden: Falsche Anzahl von Argumenten!");
  }
  if (typeof strKey != "string") {
    focus();
    throw new Error("GisForm->addFieldMapHidden: Argument strKey ist nicht vom Typ String!");
  }
  if (typeof strValue != "string") {
    focus();
    throw new Error("GisForm->addFieldMapHidden: Argument strValue ist nicht vom Typ String!");
  }
  this._fieldMapHidden[strKey] = strValue;
  this._fieldMapHiddenLength.push(strKey);
}

GisForm.prototype.addCountryMap = function(strKey, strValue) {
  if (arguments.length != 2) {
    focus();
    throw new Error("GisForm->addCountryMap: Falsche Anzahl von Argumenten!");
  }
  if (typeof strKey != "string") {
    focus();
    throw new Error("GisForm->addCountryMap: Argument strKey ist nicht vom Typ String!");
  }
  if (typeof strValue != "string") {
    focus();
    throw new Error("GisForm->addCountryMap: Argument strValue ist nicht vom Typ String!");
  }
  this._countryMap[strKey] = strValue;
  this._countryMapLength.push(strKey);
}

GisForm.prototype.getGisAdressHash = function() {
  var adress = '';
  var _lang = this.getValueFromField(this._fieldMap['lang']);
  if (this._countryMap[_lang]){
    _lang = this._countryMap[_lang];
  }
  if (! _lang.length){
    _lang = 'DE';
  }
  var _country = this.getValueFromField(this._fieldMap['country']);
  adress += 'land=' + _lang;
  adress += ',ort=' + _country;
  if (this._fieldMap['zip']){
    var _zip = this.getValueFromField(this._fieldMap['zip']);
    adress += ',plz=' + _zip;
  }
  if (this._fieldMap['street']){
    var _street = this.getValueFromField(this._fieldMap['street']);
    if(_street.length){
      adress += ',str=' + _street;
      if (this._fieldMap['street']){
      var _nr = this.getValueFromField(this._fieldMap['nr']);
        if(_nr.length){
          adress += ',nr=' + _nr;
        }
      }
    }
  }
  return adress;
}

GisForm.prototype.gisSetCoordinations = function() {
  var gisX = this.getFieldElement(this._fieldMap['x']);
  var gisY = this.getFieldElement(this._fieldMap['y']); 
  if (this.getValueFromField(this._statusControlElementID)){
    GisForm._setReadonly(gisX);
    GisForm._setReadonly(gisY);
  } else {
    GisForm._unsetReadonly(gisX);
    GisForm._unsetReadonly(gisY);
  }
}

GisForm.prototype.getValueFromField = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->getValueFromField: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->getValueFromField: Argument ist nicht vom Typ String!");
  }
  if (document[this._formID] == undefined){
    focus();
    throw new Error("GisForm->getValueFromField: Es existiert kein Formular mit dem Namen "+ this._formID +"!");
  }
  var elem = this.getFieldElement(str);
  var value = '';
  
  if (elem.type == 'select-one'){
    value = GisForm._stripSpace(elem.options[elem.selectedIndex].value);
  } else if (elem.type == 'select-multiple'){
    for (var ii=0; ii<elem.options.length; ii++){
      if (elem.options[ii].selected){
        value += GisForm._stripSpace(elem.options[ii].value);
      }
    }
  } else if (elem.type == 'radio'){
    if (elem.length){
      for (var ii=0; ii<elem.length; ii++){
        if (elem[ii].checked){
          value += GisForm._stripSpace(elem[ii].value);
        }
      }
    } else {
      if (elem.checked){
        value = GisForm._stripSpace(elem.value);
      }
    }
  } else if (elem.type == 'checkbox'){
    if (elem.length){
      for (var ii=0; ii<elem.length; ii++){
        if (elem[ii].checked){
          value += GisForm._stripSpace(elem[ii].value);
        }
      }
    } else {
      if (elem.checked){
        value = GisForm._stripSpace(elem.value);
      }
    }
  } else {
    value = GisForm._stripSpace(elem.value);
  }
  return value;
}

GisForm.prototype.resetValue = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->resetValue: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->resetValue: Argument ist nicht vom Typ String!");
  }
  if (document[this._formID] == undefined){
    focus();
    throw new Error("GisForm->resetValue: Es existiert kein Formular mit dem Namen "+ this._formID +"!");
  }
  var elem = this.getFieldElement(str);
  var value = '';
  
  if (elem.type == 'select-one'){
    elem.selectedIndex = 0;
  } else if (elem.type == 'select-multiple'){
    for (var ii=0; ii<elem.options.length; ii++){
      elem.options[ii].selected = false;
    }
  } else if (elem.type == 'radio'){
    if (elem.length){
      for (var ii=0; ii<elem.length; ii++){
        elem[ii].checked = false;
      }
    } else {
      elem.checked = false;
    }
  } else if (elem.type == 'checkbox'){
    if (elem.length){
      for (var ii=0; ii<elem.length; ii++){
       elem[ii].checked = false;
      }
    } else {
      if (elem.checked){
        elem.checked = false;
      }
    }
  } else {
    elem.value = '';
  }
}

GisForm.prototype.getFieldElement = function(str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm->getFieldElement: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GisForm->getFieldElement: Argument ist nicht vom Typ String!");
  }
  if (document[this._formID] == undefined){
    focus();
    throw new Error("GisForm->getFieldElement: Es existiert kein Formular mit dem Namen "+ this._formID +"!");
  }
  var elem = document[this.id][str];
  if (elem == undefined){
    focus();
    throw new Error("GisForm->getFieldElement: Es existiert kein Formular-Element mit dem Namen "+ str +"!");
  }
  return elem;
}

GisForm.prototype.submit = function() {
  if (! this.getValueFromField(this._statusControlElementID)){ 
    if (this._fieldMapHiddenLength.length){
      this.getFieldElement(this._fieldMapHidden['lang']).value = this.getValueFromField(this._fieldMap['lang']);
      this.getFieldElement(this._fieldMapHidden['country']).value = this.getValueFromField(this._fieldMap['country']);
      if (this._fieldMapHidden['zip'] && this._fieldMap['zip']){
        this.getFieldElement(this._fieldMapHidden['zip']).value = this.getValueFromField(this._fieldMap['zip']);
      }
    }
  } else {
    if (! this.getValueFromField(this._fieldMap['lang'])){
      alert('Bitte geben Sie die Länderkennung an!');
      this.getFieldElement(this._fieldMap['lang']).focus();
      return false;
    }
    if (! this.getValueFromField(this._fieldMap['country'])){
      alert('Bitte geben Sie den Ort an!');
      this.getFieldElement(this._fieldMap['country']).focus();
      return false;
    }
    /*
    if (! this.getValueFromField(this._fieldMap['zip'])){
      alert('Bitte geben Sie die PLZ an!');
      this.getFieldElement(this._fieldMap['zip']).focus();
      return false;
    }
    this.getFieldElement(this._fieldMap['x']).value = '';
    this.getFieldElement(this._fieldMap['y']).value = '';
    */
    this.getFieldElement(this._fieldMap['gis-hash-adress']).value = this.getGisAdressHash();
    // Hier müssen alle Felder gelöscht werden, damit die Umkreissuche nicht mit der normalen Datenbanksuche kollidiert.
    if (! this._fieldMapHiddenLength.length){
      this.resetValue(this._fieldMap['lang']);
      this.resetValue(this._fieldMap['country']);
      if (this._fieldMap['zip']){
        this.resetValue(this._fieldMap['zip']);
      }
    }
  }
  //document.getElementById('gis_adresse').value = this.getGisAdressHash();
  //alert(this.getFieldElement(this._fieldMap['gis-hash-adress']).value);
  return true;
}

GisForm.prototype.load = function() {
  if(this._mode != 'NEU'){
    this.setSavedAdressStr(this.getGisAdressHash());
  }
  if (document[this._formID] == undefined){
    focus();
    throw new Error("GisForm->init: Es existiert kein Formular mit dem Namen "+ this._formID +"!");
  }
  var id = new String(this.id);
  document[this._formID].onsubmit = function() { return GisForm.getInstance(id).submit() };
  //var func = function() { return GisForm.getInstance(id).submit() };
  //GisForm._addEvent('submit', document[this._formID], func);
}

GisForm.prototype.init = function() {
  var id = new String(this.id);
  var func = function() {GisForm.getInstance(id).load()};
  GisForm._addEvent('load', window, func);
}

// ------------------------------------------------------------
// Private Klasseneigenschaften
// ------------------------------------------------------------

GisForm._defaultID = [];
GisForm._registerInstance = {};
GisForm._registerInstanceLength = [];

// ------------------------------------------------------------
// Öffentliche Klasseneigenschaften
// ------------------------------------------------------------

// ------------------------------------------------------------
// Private Klassenmethoden
// ------------------------------------------------------------

GisForm._addEvent = function(oneEvent, obj, handler) {
  if(window.attachEvent) {
    obj.attachEvent("on" + oneEvent, handler);
  } else if(window.addEventListener){
    obj.addEventListener(oneEvent, handler, false);
  }  
}

GisForm._stripSpace = function(str){
  var result = '';
  if (str){
    str = new String(str);
    result = str.replace(/^\s+/,'');
    result = result.replace(/\s+$/,'');
  }
  return result;
}

GisForm._setReadonly = function(obj) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm._setReadonly: Falsche Anzahl von Argumenten!");
  }
  if (typeof obj != "object") {
    focus();
    throw new Error("GisForm._setReadonly: Argument ist nicht vom Typ HTML-Object!");
  }
  var readonly = document.createAttribute("readonly");
  readonly.nodeValue = "readonly";
  obj.setAttributeNode(readonly);
  obj.style.backgroundColor = '#f0f0f0';
}

GisForm._unsetReadonly = function(obj) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GisForm._setReadonly: Falsche Anzahl von Argumenten!");
  }
  if (typeof obj != "object") {
    focus();
    throw new Error("GisForm._setReadonly: Argument ist nicht vom Typ HTML-Object!");
  }
  obj.readOnly = undefined;
  obj.style.backgroundColor = '#ffffff';
}

// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

GisForm.getInstance = function(id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (GisForm._registerInstance[id])){
    focus();
    throw new Error("Es ist keine GisForm.Instance mit id=" + id + " registriert!");
  } 
  return GisForm._registerInstance[id];
}

GisForm.createInstance = function(id) {
  if (!arguments.length) {
    id = 'GisForm' + GisForm._defaultID.length;
    GisForm._defaultID.push(1);
  }
  if (! (GisForm._registerInstance[id])){
    GisForm._registerInstance[id] = new GisForm(id);
    GisForm._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("GisForm.createInstance: ID schon vorhanden!");
  }
  return GisForm.getInstance(id);
}

GisForm.initAll = function() {
  for (var i in GisForm._registerInstance){
    GisForm.getInstance(i).init();
  }
}

GisForm.gisSetCoordinations = function() {
  
  GisForm.getInstance('myform').gisSetCoordinations();
}
