// forms follow these functions:
function seaFieldClear(aField,aValue) {
	if(aField.value == aValue) {
		aField.value = "";
	}
	return aField.value;
}

function seaFieldFill(aField,aValue) {
	if(aField.value == "") {
		aField.value = aValue;
	}
	return aField.value;
}

function seaFormCheck(aForm) {
    var seaWord = seaFieldClear(document.getElementById('filter[find]'),seaLabelWord);
    var seaPLZ = seaFieldClear(document.getElementById('filter[pc]'),seaLabelPLZ);
    var seaCountry = seaFieldClear(document.getElementById('filter[ctry]'),seaLabelCountry);

    var error = false;
	if (aForm) {
		if (seaPLZ.match(/\d/)) {
		  // alert("Zahl in PLZ");
		} else {
		  // no valid PLZ, so we 'shorten' it a bit.
		  seaPLZ = '';
		}
        if (seaPLZ.length || seaCountry.length) {
            // if there's anything in one of those two fields
        } else {
            // in both fields are empty search-string has to be valid.
            if (seaWord.length == 0) {
		        alert(seaNoChars);
		        error = true;
		    } else if (seaWord.length < 3) {
		        alert(seaFewChars);
		        error = true;
		    }
        }
	} else {
		alert("Es ist ein Problem beim Überprüfen des Formulars aufgetreten.\nBitte informieren Sie ar@aspekt1.net.\n\nAn error occured while checking the form.\nPlease inform ar@aspekt1.net.");
		error = true;
	}
	if (error) { // reset fields and return early on error.
        seaFieldFill(document.getElementById('filter[find]'),seaLabelWord);
        seaFieldFill(document.getElementById('filter[pc]'),seaLabelPLZ);
        seaFieldFill(document.getElementById('filter[ctry]'),seaLabelCountry);
	} else { // everything is fine so the form is submitted.
    	aForm.submit();
	}
	return false; // so the server won't be polled twice (once by JS, oncy by browser's own submit)
}

/** Image functions **/
function hideImg() {
  if ($('galerie1').style.display != 'none') {
    // hide all images
    $$("#imageFader img").each(function(value, index) {
      new Effect.Fade(value.id,{duration:0.5});
    });
    // blind up
    new Effect.BlindUp('galerie1',{duration:0.8, queue:{position:'end',scope:'showreal'}});
    // reset Captions
    Element.removeClassName($('h2Profil'), 'inactive');
    if($('h2Profil')) $('h2Galerie').addClassName('inactive');
    // reset fade-in sentinel Variable
    lastklick = -1;
    // remove the Thumbnail selection
    $$("#thumbstrip a").each(function(value, index) {
      Element.removeClassName($(value.id), 'selected');
    });
  }
}
function showImg(idWithoutPrefix) {
  // prevent re-fade-in of an already visible image.
  if (lastklick == idWithoutPrefix) return false;
  
  // make sure, the container is visible
  if ($('galerie1').style.display == 'none') {
    new Effect.BlindDown('galerie1',{duration:0.8, queue:{position:'end',scope:'showreal'}});
    // while at it make the right caption stand out
    Element.removeClassName($('h2Galerie'), 'inactive');
    if($('h2Profil')) $('h2Profil').addClassName('inactive');
  }

  // fade in the right image
  var imgId = "img"+idWithoutPrefix;
  if ($(imgId)) {
    if ($(imgId).getAttribute('rel') != 'ok') {
      // if image isnt loaded yet try again later
      setTimeout(function() { showImg(idWithoutPrefix); }, 100) ;
    } else {
      // change the text-message
//      if (imgCaps[idWithoutPrefix]) {
      if ($$('#galerie1 p').length == 1) $$('#galerie1 p')[0].innerHTML=imgCaps[idWithoutPrefix];
//      }
      // prevent re-FadeIn
      lastklick = idWithoutPrefix


      // hide all images
      $$("#imageFader img").each(function(value, index) {
        if(value.getAttribute('rel') == 'ok')
          new Effect.Fade(value.id,{duration:0.5, queue:{scope:'showreal'}});
      });
      // show selected image
      new Effect.Appear((imgId), {duration:0.8, queue:{position:'end',scope:'showreal'}});

      // Anyway we need to update the navigation
      // loop throug all anchors and remove class 'selected'
      $$("#thumbstrip a").each(function(value, index) {
          Element.removeClassName($(value.id), 'selected');
      });
      // set class selected on anchor
      if($("nav"+idWithoutPrefix)) $("nav"+idWithoutPrefix).addClassName('selected');
    }
  }
}

// makes all images invisible at first
function preloadAndFadeImages(tagId) {
  var first = true;
  $$('#'+tagId+" img").each(function(value,index) {
    preloadImage(value.src,value.id);
  });

//  list = document.getElementById(tagId).getElementsByTagName("img");
//  // show first image (strip prefix 'img' with substr)
//  if (list.length > 0) showImg(list[0].getAttribute('id').substr(3));
}
// handles the preload.
var imgPreload = new Array();
function preloadImage(fn,id) {
  imgPreload[id] = new Image();
  imgPreload[id].onload = function() {
     $(id).setAttribute('rel','ok');
  }
// the following line is unusable in this project
//  $(id).setAttribute("style","display:none;");
  $(id).setAttribute('rel','loading');
  imgPreload[id].src = fn;
}

// Function for JS-Replacement
  function changeForm() {
    // selecting the box and the fieldset
    var checkbox = $('searchInArea');
    var fieldset = checkbox.parentNode;
    
    // creating a suitable replacement node-structure
    var imagebox = document.createElement('div');
    imagebox.setAttribute('id','searchInArea');
    var toggleLink = document.createElement('a');
    toggleLink.setAttribute('href','#');
    toggleLink.setAttribute('onclick','toggleSearchInArea(); return false;');

    var image = document.createElement('img');
    image.setAttribute('id','hiddenCheckboxImg');
    image.src = "img/checkbox_false.gif";
    toggleLink.appendChild(image);
    imagebox.appendChild(toggleLink);
    var input = document.createElement('input');
    input.setAttribute('id','hiddenCheckbox');
    input.setAttribute('type','hidden');
    input.setAttribute('name','karl');
    input.setAttribute('value','false');
    imagebox.appendChild(input);
    
    // replacing the input.
    fieldset.replaceChild(imagebox,checkbox);
    
    // replacing the "red-label"
    var label = $('areaSearch').childNodes[0];
    label.innerHTML= '<a href="#" onclick="toggleSearchInArea(); return false;" style="color: white;">im ausgew&auml;hlten Bereich suchen</a>';
  }
  function toggleSearchInArea() {
    var checkbox = $('hiddenCheckbox');
    var image = $('hiddenCheckboxImg');
    if (checkbox.value != 'true') {
      checkbox.value = 'true';
      image.src = "img/checkbox_true.gif"
    } else {
      checkbox.value = 'false';
      image.src = "img/checkbox_false.gif"
    }
    
  }

