// DOM READY EVENT
function bindReady(b){var c=false;function a(){if(c){return}c=true;b()}if(document.addEventListener){document.addEventListener("DOMContentLoaded",a,false)}else{if(document.attachEvent){if(document.documentElement.doScroll&&window==window.top){function d(){if(c){return}if(!document.body){return}try{document.documentElement.doScroll("left");a()}catch(f){setTimeout(d,0)}}d()}document.attachEvent("onreadystatechange",function(){if(document.readyState==="complete"){a()}})}}if(window.addEventListener){window.addEventListener("load",a,false)}else{if(window.attachEvent){window.attachEvent("onload",a)}}};
bindReady(scaleBg);

function scaleBg(){
  var img = document.getElementById('bg');
  imageStretcher.stretch(img);
}
//RegisterFormValidation
function validateregisterform() {
 name = $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_txtName").val();
 email = $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_txtEmail").val();
 bjerg = $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_ddlMountains").val();
 parkcode = $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_txtParkingCode").val();
 topcode = $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_txtTopCode").val();
 namevalid = false;
 emailvalid = false;
 bjergvalid = false;
 parkcodevalid = false;
 topcodevalid = false;
 if (name != "") {
  $(".navn_btn_valid").addClass("valid");
  namevalid = true;
 } else {
  $(".navn_btn_valid").removeClass("valid");
}
 var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
 if (email !== "" && filter.test(email)) {
  $(".email_btn_valid").addClass("valid");
  emailvalid = true;
 }
 else {
  $(".email_btn_valid").removeClass("valid");
 }
 if (bjerg !=='0') {
  $(".bjerg_btn_valid").addClass("valid");
  bjergvalid = true;
 } else {
  $(".bjerg_btn_valid").removeClass("valid");
 }
 if (parkcode != "") {
  $(".park_btn_valid").addClass("valid");
  parkcodevalid = true;
 } else {
  $(".park_btn_valid").removeClass("valid");
}
 if (topcode != "") {
  $(".top_btn_valid").addClass("valid");
  topcodevalid = true;
 } else {
  $(".top_btn_valid").removeClass("valid");
}
//activate button if valid
if(namevalid&&emailvalid&&bjergvalid&&parkcodevalid&&topcodevalid){
  $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_btnCommitRegistration").removeAttr("disabled");
} else {
    $("#ContentPlaceHolderDefault_SideBarPlaceHolder_Register_6_btnCommitRegistration").attr("disabled", "disabled");
}
}
//Set up validation on fields;

// image stretcher module
var imageStretcher = (function(){
  var windowWidth, windowHeight, images = [];
  function addImage(img) {
    recalcSize();
    images.push(img);
    if(img.complete) {
      getRatio(img, resizeImage);
    } else {
      img.onload = function(){
        getRatio(img, resizeImage);
        img.onload = null;
      }
      img.src = img.src; // IE Fix
    }
  }
  function removeImage(img) {
    for(var i=0; i<images.length; i++) {
      if(images[i] === img) {
        images.splice(i,1);
        return;
      }
    }
  }
  function getRatio(img, cb) {
    img.style['msInterpolationMode'] = 'bicubic'; // IE7 fix
    var newImg = new Image();
    newImg.onload = function() {
      img.ratio = newImg.width / newImg.height;
      if(typeof cb === 'function') cb.apply(img)
      newImg.onload = null;
    }
    newImg.src = img.src;
  }
  function resizeImage(img) {
    var _ratio = this.ratio;
    var _slideWidth = windowWidth;
    var _slideHeight = _slideWidth/_ratio;
    if(_slideHeight < windowHeight) {
      _slideHeight = windowHeight;
      _slideWidth = _slideHeight * _ratio;
    }
    this.style.width = _slideWidth+'px';
    this.style.height = _slideHeight+'px';
    this.style.top = (windowHeight-_slideHeight)/2+'px';
    this.style.left = (windowWidth-_slideWidth)/2+'px';
  }
  function recalcSize() {
    windowWidth = getClientWidth();
    windowHeight = getClientHeight();
  }
  function handleResize() {
    recalcSize();
    for(var i=0; i<images.length; i++) resizeImage.apply(images[i]);
  }
  addHandler(window, 'resize', handleResize);

  // util functions
  function addHandler(object, event, handler) {
    if (typeof object.addEventListener != 'undefined') object.addEventListener(event, handler, false);
    else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, handler);
  }
  function getClientWidth(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientWidth'] || window.document.body['clientWidth']}
  function getClientHeight(){return window.document.compatMode === 'CSS1Compat' && window.document.documentElement['clientHeight'] || window.document.body['clientHeight']}

  // methods
  return {
    stretch: addImage,
    unstretch: removeImage
  }
})()

function initPage()
{
  clearFormFields({
    clearInputs: true,
    clearTextareas: true,
    passwordFieldText: false,
    addClassFocus: "focus",
    filterClass: "default"
  });
}

function clearFormFields(o)
{
  if (o.clearInputs == null) o.clearInputs = true;
  if (o.clearTextareas == null) o.clearTextareas = true;
  if (o.passwordFieldText == null) o.passwordFieldText = false;
  if (o.addClassFocus == null) o.addClassFocus = false;
  if (!o.filterClass) o.filterClass = "default";
  if(o.clearInputs) {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++ ) {
      if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
        inputs[i].valueHtml = inputs[i].value;
        inputs[i].onfocus = function ()  {
          if(this.valueHtml == this.value) this.value = "";
          if(this.fake) {
            inputsSwap(this, this.previousSibling);
            this.previousSibling.focus();
          }
          if(o.addClassFocus && !this.fake) {
            this.className += " " + o.addClassFocus;
            this.parentNode.className += " parent-" + o.addClassFocus;
          }
        }
        inputs[i].onblur = function () {
          if(this.value == "") {
            this.value = this.valueHtml;
            if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
          }
          if(o.addClassFocus) {
            this.className = this.className.replace(o.addClassFocus, "");
            this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
          }
        }
        if(o.passwordFieldText && inputs[i].type == "password") {
          var fakeInput = document.createElement("input");
          fakeInput.type = "text";
          fakeInput.value = inputs[i].value;
          fakeInput.className = inputs[i].className;
          fakeInput.fake = true;
          inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
          inputsSwap(inputs[i], null);
        }
      }
    }
  }
  if(o.clearTextareas) {
    var textareas = document.getElementsByTagName("textarea");
    for(var i=0; i<textareas.length; i++) {
      if(textareas[i].className.indexOf(o.filterClass) == -1) {
        textareas[i].valueHtml = textareas[i].value;
        textareas[i].onfocus = function() {
          if(this.value == this.valueHtml) this.value = "";
          if(o.addClassFocus) {
            this.className += " " + o.addClassFocus;
            this.parentNode.className += " parent-" + o.addClassFocus;
          }
        }
        textareas[i].onblur = function() {
          if(this.value == "") this.value = this.valueHtml;
          if(o.addClassFocus) {
            this.className = this.className.replace(o.addClassFocus, "");
            this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
          }
        }
      }
    }
  }
  function inputsSwap(el, el2) {
    if(el) el.style.display = "none";
    if(el2) el2.style.display = "inline";
  }
}
if (window.addEventListener)
  window.addEventListener("load", initPage, false);
else if (window.attachEvent)
  window.attachEvent("onload", initPage);

