﻿enc = encodeURIComponent;
function $gt(id) { return document.getElementById(id); }
function addListener(obj, eventName, handler) { if (obj.attachEvent) obj.attachEvent("on" + eventName, handler); else obj.addEventListener(eventName, handler, false); }
function newElem(tagName, attrs, listeners) {
  var e = document.createElement(tagName);
  if (attrs) for (var attrName in attrs) e[attrName] = attrs[attrName];
  if (listeners) for (var eventName in listeners) addListener(e, eventName, listeners[eventName]);
  return e; 
}

function loadScripts(e) {
  var scripts = e.getElementsByTagName("script"),
      hd = document.getElementsByTagName("head")[0],
      src;
  loadScripts.loadingCount = 0;
  for (var i = 0; i < scripts.length; i++) {
    if (src = scripts[i].getAttribute("src")) {
      loadScripts.loadingCount++;
      hd.appendChild(
        newElem("script",
          {
            src: src,
            type: 'text/javascript'
          },
          {
            load:
              function () {
                loadScripts.loadingCount--;
                execScripts(e);
              }
          }
        )
      );
    }
  }
  execScripts(e);
}
function execScripts(e) {
  if (loadScripts.loadingCount > 0) return;

  var scripts = e.getElementsByTagName("script"),      
      html;
  for (var i = 0; i < scripts.length; i++) {
    if (!scripts[i].getAttribute("src")) {
      html = scripts[i].innerHTML;
      if (window.execScript) window.execScript(html);
      else window.eval(html);
    }
  }
}

Xhr = function () { }
Xhr.Get = function (config) { config.method = "get"; Xhr.Send(config); }
Xhr.Post = function (config) { config.method = "post"; Xhr.Send(config); }
Xhr.Send = function (config) {
  var c = config;
  c.method = c.method || "get";
  c.async = c.async || false;
  c.controls = c.controls || null;
  c.callback = c.callback || null;

  var r = new XMLHttpRequest();
  var postData = Xhr.GetPostData(c.controls);
  r.open(c.method, c.url, c.async);
  if (c.method == "post") r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  if (c.async && c.callback) r.onreadystatechange = function () { if (r.readyState == 4) c.callback(r); }
  r.send(postData);
}
Xhr.GetPostData = function (C) {
  var s = [];
  if (C) {
    if (C.tagName == "FORM") {
      s.push(Xhr.GetPostData(C.getElementsByTagName("input")));
      s.push(Xhr.GetPostData(C.getElementsByTagName("select")));
      s.push(Xhr.GetPostData(C.getElementsByTagName("textarea")));
    }
    else if (C.tagName == "TABLE") {
      var row, inp;
      for (var i = 0; i < C.rows.length; i++) {
        row = C.rows[i];
        if (row.cells.length >= 2) {
          inp = row.cells[1].getElementsByTagName("input");
          if (inp && inp.length > 0) {
            s.push([enc(row.cells[0].innerText), '=', enc(inp[0].value)].join(''));
          }
        }
      }
    }
    else {
      var c;
      for (var i = 0; i < C.length; i++) {
        c = C[i];
        s.push([c.name || c.id, '=', enc(c.value)].join(''));
      }
    }
  }
  return s.join('&');
}

function mailForm(form, callback) {
  Xhr.Post({
    url: "Callbacks.aspx?c=mailForm",
    async: true,
    controls: typeof form == 'string' ? $gt(form) : form,
    callback: callback || function (r) {
      var f = typeof form == 'string' ? $gt(form) : form;
      if (f) {
        var fp = f.parentNode;
        if (fp) {
          fp.innerHTML = "<h2 style='margin:0; padding-top:50px;'>" + r.responseText + "</h2>";
        }
      }
    }
  });
}

function appendHidden(form, name, value) {
  if (form[name]) {
    form[name].value = value;
  }
  else {
    var h = document.createElement("input");
    h.type = "hidden";
    h.name = h.id = name;
    h.value = value;
    form.appendChild(h);
  }
}

function formReturned(r) {
  var pc = $gt("mcpage");
  var d = newElem("div", { innerHTML: r.responseText });
  pc.innerHTML = $(d).children().first().html();
  loadScripts(pc);
}


var Scroller = {
  id: '', sign: 0, timer: null,
  start: function (id, sign) {
    Scroller.id = id;
    Scroller.sign = sign;
    Scroller.setTimer();
  },

  setTimer: function () {
    setTimeout(Scroller.onTimer, 20);
  },

  stop: function () {
    Scroller.sign = 0;
  },

  onTimer: function () {
    if (Scroller.sign != 0) {
      var el = $gt(Scroller.id);
      if (el) {
        el.scrollLeft += Scroller.sign * 5;
        Scroller.setTimer();
      }
    }
  }
}

