function initMaster() {

  if (typeof AjaxPro != "undefined" && AjaxPro !== null) {
    AjaxPro.timeoutPeriod = 1000 * 10;
    AjaxPro.onTimeout = function(b, res) { alert(res.Message); }
    AjaxPro.onLoading = function(b) { ajaxWait(b); }
    AjaxPro.onError = function(res) { alert(res.Message); }
  }

  sizeBackground();

  try { initPage(); }
  catch (e) { nfndebug(e.message); };
}

function ajaxWait(b) {
  N$S('ajaxwait').display = (b ? "block" : "none");
}

function getWindowSize() {
  var w = 0;
  var h = 0;
  if(!window.innerWidth) {
    if(!(document.documentElement.clientWidth == 0)) {
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    }
    else {
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
  }
  else {
    w = window.innerWidth;
    h = window.innerHeight;
  }
  return {width:w, height:h};
}

function sizeBackground() {
  var isize = {width:1426, height:839};
  var wsize = getWindowSize();
  var imgfrac = isize.width/isize.height;
  var wndfrac = wsize.width/wsize.height;
  if (imgfrac < wndfrac) {
    isize.width = wsize.width;
    isize.height = Math.round(isize.width/imgfrac);
  }
  else {
    isize.height = wsize.height;
    isize.width = Math.round(isize.height*imgfrac);
  }
  if (bgflashurl.length == 0) {
    N$('bgimg').firstChild.firstChild.width = isize.width;
    N$('bgimg').firstChild.firstChild.height = isize.height;
  }
  N$S('bgimg').marginTop = String(-isize.height/2) + "px";
  N$S('bgimg').marginLeft = String(-isize.width/2) + "px";

  if (wsize.height < 602) {
    N$S('whiteframe').marginTop = String(-wsize.height/2+10) + "px";
    N$S('contentarea').marginTop = String(-wsize.height/2+10) + "px";
    document.getElementsByTagName('html')[0].style.overflow = "auto"
  }
  else {
    N$S('whiteframe').marginTop = "-301px";
    N$S('contentarea').marginTop = "-301px";
    document.getElementsByTagName('html')[0].style.overflow = "hidden";
  }

  if (bgflashurl.length > 0) {
    var params = {wmode:"transparent"};
    swfobject.embedSWF(bgflashurl, "bginner", String(isize.width), String(isize.height), "8.0.0", "", null, params);
  }

  N$S('bgimg').display = "block";
}

function windowResized() {
  sizeBackground();
}

function changecss(theClass,element,value) {
  var cssRules = (document.all ? 'rules' : 'cssRules');
  for (var S = 0; S < document.styleSheets.length; S++){
    for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
      if (document.styleSheets[S][cssRules][R].selectorText == theClass)
        document.styleSheets[S][cssRules][R].style[element] = value;
    }
  }
}

function showLogin() {
  N$S("loginerr").display = "none";
  var login = N$("logindiv");
  login.style.display = (login.style.display == "block" ? "none" : "block");
  N$("username").focus();
}

function checkEnter(e) {
  var keynum = 0;
  if(window.event) keynum = e.keyCode;
  else if(e.which) keynum = e.which;
  if (keynum == 13) login();
}

function login() {
  var uname = N$("username").value;
  var pwd = N$("password").value;
  Master.Login(uname, pwd, login2);
}

function login2(response) {
  if (response.value.length == 0)  document.location.href = "retailers_start.aspx";
  else {
    N$S("loginerr").display = "block";
    setTimeout(function(){N$S("loginerr").display = "none";}, 2000);
  }
}

function logout() {
  Master.Logout();
  document.location.href = "/";
}

var currNewsId = 0;
var currMove = 0;

function changeNews() {
  currNewsId = (currNewsId + 1) % 3;
  N$('nextNews').innerHTML = N$('newsholder' + currNewsId).innerHTML;
  N$S('nextNews').width = "0";
  moveNewsDivs();
}

function moveNewsDivs() {
  currMove += 10;
  if (currMove >= 780) {
    N$('currNews').innerHTML = N$('nextNews').innerHTML;
    N$S('currNews').marginLeft = "0";
    currMove = 0;
  }
  else {
    N$S('currNews').marginLeft = String(-currMove) + "px";
    N$S('nextNews').width = currMove + "px";
    setTimeout("moveNewsDivs()", 10);
  }
}


if (window.attachEvent) window.attachEvent("onload", initMaster);
else window.addEventListener("load", initMaster, false);

if (window.attachEvent) window.attachEvent("onresize", windowResized);
else window.addEventListener("resize", windowResized, false);
