/* (c) Ideakone Ltd - antti@ideakone.fi */


/**
 * Registration
 */
Registration = {
  checkUrlChars: function(str) {
      var s = str.toLowerCase();
      s = s.replace(/&amp;[^;\s]+;/g, '');
      s = s.replace(/[^\w\s]/g, '');
      s = s.replace(/\s+/g, '');
      s = s.replace(/[_]/g, '');
      return s;
  },
  setUrl: function(urlField, nameFieldValue) {
    if(!urlField)
      return;
    if(typeof nameFieldValue == "undefined" || nameFieldValue == null)
      nameFieldValue = "";
    urlField.value = Registration.checkUrlChars(nameFieldValue).substr(0, 60);
  },

  submit: function(msg) {
    if ($("license").checked) return true;
    else alert(msg);

    return false;
  },

  cleanUrl: function(url) {
      var s = url.toLowerCase();
      s = s.replace(/\s+/g, '');
      return s;
  }
}



/**
 * Selecting themes
 */
function themeImgOn(img) {
  img.style.border = "1px solid #1CE21C";
}
function themeImgOff(img) {
  img.style.border = "1px solid #000";
}


TopBarPosition = { x: 17, y: 14, w: 327, h: 71 };
var currentTheme = '';
var colorTheme = '';
function layoutImgClick(img, themeName, color, topbarShape, topbarStyle) {

  document.getElementById("theme_preview").style.backgroundImage = 'url( ' + img.src + ' )';
  currentTheme = themeName;
  if (topbarStyle != null) {
    TopBarPosition.x = topbarStyle[0];
    TopBarPosition.y = topbarStyle[1];
    TopBarPosition.w = topbarStyle[2];
    TopBarPosition.h = topbarStyle[3];
  }
  if (topbarShape != null)
    currentTopbarShape = topbarShape;

  Element.hide('theme_preview_topbar');
  if (topbarShape != null)
    Topbar.select(currentTopbarElem);


  $("layout_field").value = themeName;
  $("shape_field").value = topbarShape;
}



Themes = {
  getSelections: function() {
    var topbar = "";
    if (currentTopbar != "") {
      //var i = currentTopbar.indexOf('/topbars/');
      //topbar = "/Kotisivukone/clients" + currentTopbar.substring(i, currentTopbar.length);
      topbar = currentTopbarElem.getAttribute("name");
    }
    var path = "theme=" + currentTheme + "&topbar=" + topbar + "&shape=" + currentTopbarShape + "&color=" + colorTheme;
    //alert(path);
    return path
  }
}



var currentCategory = 'top_bars_bisnes';
var currentTopbarShape = 'nelio';
var currentTopbar = '';
var currentTopbarElem = null;
Topbar = {
  hover: function(ison, elem) {
    elem.style.border = ison ? "1px solid #1CE21C" : "1px solid #666";
  },

  select: function(elem) {
    currentTopbarElem = elem;
    $('theme_preview_topbar').src = "/Kotisivukone/www/pics/white.gif";
    if (elem && elem.className && elem.className.indexOf(currentTopbarShape) != -1)
      $('theme_preview_topbar').src = elem.src.replace(/nelio/, currentTopbarShape);
    else if (elem) {
      $('theme_preview_topbar').src = elem.src;
    }

    currentTopbar  =  $('theme_preview_topbar').src;
    $('theme_preview_topbar').style.marginTop = TopBarPosition.y + "px";
    $('theme_preview_topbar').style.marginLeft = TopBarPosition.x + "px";
    $('theme_preview_topbar').style.width = TopBarPosition.w + "px";
    $('theme_preview_topbar').style.height = TopBarPosition.h + "px";
    Element.show('theme_preview_topbar');

    if (elem && elem.name)
      $("topbar_field").value = elem.name;
  },


  selectCategory: function(cat) {
    eval("var a = " + cat + "Images");
    if ($('top_bars').scrollTop)
      $('top_bars').scrollTop = 0;

    $('top_bars').innerHTML = a;
  }
}
