var darkcolor = '01b3dd';

function initMaster() {
  darkcolor = (_loggedin ? '718387' : '01b3dd');

  if($.browser.mozilla) $("form").attr("autocomplete", "off");

  if (typeof AjaxPro != "undefined" && AjaxPro !== null) {
    AjaxPro.timeoutPeriod = 1000 * 10;
    AjaxPro.onTimeout = function(b, res) { ajaxTimeout(b, res); }
    AjaxPro.onLoading = function(b) { showBusy(b); }
    AjaxPro.onError = function(res) { ajaxError(res); }
  }

  $("#bottomplatecontent div.ajaxControl").ajaxcontrol({textmode:"multiline"});
  $("#footer").ajaxcontrol();
  $("#PopContent").ajaxcontrol({ width:600, manualCycling:true,thumbWidth:144 });
  $("#PopContent1").ajaxcontrol({width:720});
  $("#PopContent2").ajaxcontrol({width:350});
  $("#PopContent3").ajaxcontrol({width:350});
  $("#PopContent4").ajaxcontrol({width:720});
  $("#RegPopText").ajaxcontrol();
  $("#NLContent, #NLThanks, #NLContentLI, #NLThanksLI").ajaxcontrol({width:260});
  $(window).resize(function(){$("#blocker").css("height",$(document).height());});

  try { initMaster2();}
  catch (e) {};
  try { initAdmin();}
  catch (e) {};
  try { initPage();}
  catch (e) {};

  $(".infoplate").each(function(){
    var from = ($(this).hasClass('revgrad') || $(this).hasClass('nograd') ? 'ffffff' : darkcolor);
    var to = ($(this).hasClass('revgrad') ? darkcolor : 'ffffff');
    var mid = $(this).children("div:nth-child(2)");
    var h = mid.height() + parseInt(mid.css("padding-top")) + parseInt(mid.css("padding-bottom"));
    $(this).children("div:nth-child(1)").height(h).gradient({from:from,to:to,direction:'horizontal'});
    $(this).children("div:nth-child(3)").height(h).gradient({from:from,to:to,direction:'horizontal'});
  });
}

function ajaxTimeout(b, res) {
  showBusy(false);
  nfndebug("Timeout i funktionen " + res.method);
  setTimeout("$('#ajaxerr').hide()", 3000);
}
function ajaxError(res) {
  showBusy(false);
  nfndebug(res.Message);
  setTimeout("$('#ajaxerr').hide()", 3000);
}
function showBusy(busy) {
  $("#ajaxbusy").css("display", (busy ? "block" : "none"));
}

function checkEnter(e) {
  var keynum = 0;
  if(window.event) keynum = e.keyCode;
  else if(e.which) keynum = e.which;
  if (keynum == 13) login();
}

function openlogin() {
  $("#lidiv").animate({width:'toggle'});
  $("#forgotdiv").hide();
}
function openforgot() {
  $("#forgotdiv").animate({width:'toggle'});
  $("#lidiv").hide();
}

function openRegister(type) {
  $("#regtype").val(type);
  $("#webinardiv").css("display",type == "W" ? "block" : "none");
  showPop('regpop');
}

function login() {
  Master.LoginCookie($("#un").val(), $("#pw").val(), $("#remember").attr("checked"), login2);
}
function login2(response) {
  var err = (response.error ? response.error.Message : (response.value ? "" : "Credentials not accepted"));
  if (err.length == 0) document.location.reload();
  else {
    $("#lierr").html(err).show("normal");
    setTimeout("$('#lierr').hide('normal')", 3000);
  }
}

function logout() {
  Master.LogoutCookie(logout2);
}
function logout2(response) {
  document.location.reload();
}

var popPageCount = 0;
var popPageCountTot = 0;
function popPage(pageId) {
  Master.GetPopPageInfo(pageId, popPage2);
}
function popPage2(response) {
  popPageCount = 0;
  var filename = response.value[0];
  if (filename == 'standard_pop.aspx') {
    popPageCountTot = 4;
    $("#PopContent1").ajaxcontrol_setPropertyId(response.value[1], function(){popPage3(filename);});
    $("#PopContent2").ajaxcontrol_setPropertyId(response.value[2], function(){popPage3(filename);});
    $("#PopContent3").ajaxcontrol_setPropertyId(response.value[3], function(){popPage3(filename);});
    $("#PopContent4").ajaxcontrol_setPropertyId(response.value[4], function(){popPage3(filename);});
  }
  else {
    $("#PopContent").ajaxcontrol_setPropertyId(response.value[1], function(){popPage3(filename);});
    popPageCountTot = 1;
  }
}
function popPage3(filename) {
  popPageCount++;
  if (popPageCount == popPageCountTot) {
    var popdiv = (filename == 'standard_pop.aspx' ? 'popPage2' : 'popPage')
    showPop(popdiv);
  }
}

function showPop(id, offset) {
  if (!offset) offset = {left:0, top:0};
  var pop = $("#" + id);
  pop.children("div:nth-child(3)").width(pop.width()).gradient({from:darkcolor,to:'ffffff',direction:'vertical'});
  pop.children("div:nth-child(5)").width(pop.width()).gradient({from:'ffffff',to:darkcolor,direction:'vertical'});

  var left = $(window).width()/2 - pop.width()/2 + $(window).scrollLeft() + offset.left;
  pop.css({"left":left}).show("normal");
  $("#blocker").css("height",$(document).height()).show();
}

function closePop(a) {
  $(a).parent().parent().hide("normal");
  $("#PopContent").html("");
  $("#blocker").hide();
}

function register() {
  var err = "";
  if (!$.validateEmail($("#email1").val())) err += "The email address is invalid<br />";
  if ($("#email1").val() != $("#email2").val()) err += "The email addrsses do not match";
  if ($("#newpassword1").val().length == 0) err += "Password cannot be empty<br />";
  if ($("#newpassword1").val() != $("#newpassword2").val()) err += "The passwords do not match";
  if ($("#firstname").val().length == 0) err += "First name is mandatory<br />";
  if ($("#lastname").val().length == 0) err += "Family name is mandatory<br />";
  if ($("#address").val().length == 0) err += "Address is mandatory<br />";
  if ($("#city").val().length == 0) err += "City is mandatory<br />";
  if ($("#zip").val().length == 0) err += "Zip code is mandatory<br />";
  if ($("#country").val().length == 0) err += "Country is mandatory<br />";
  if ($("#phone").val().length == 0) err += "Phone number is mandatory<br />";
  if (err.length > 0) {
    $("#regerrdiv").html(err).show("normal");
    setTimeout("$('#regerrdiv').hide('normal')", 3000);
  }
  else
    Master.RegisterUser($("#regtype").val(), $("#webinardd").val(), $("#email1").val(), $("#newpassword1").val(), $("#firstname").val(), $("#lastname").val(), $("#company").val(), $("#address").val(), $("#city").val(), $("#zip").val(), $("#state").val(), $("#country").val(), $("#phone").val(), $("#prodinfo").attr("checked"), register2);
}

function register2(response) {
  if (response.error) {
    $("#regerrdiv").html(response.error.Message).show("normal");
    setTimeout("$('#regerrdiv').hide('normal')", 3000);
  }
  else {
    _gaq.push(['_trackPageview', '/tracker/register']);
    $("#regthanksemail").html($("#email1").val());
    $("#regformdiv").hide();
    $("#regthanksdiv").show();
  }
}

function submitnl(loggedin) {
  if (loggedin)
    Master.SubmitNewsletter("", "", "", submitnl2);
  else {
    var err = "";
    if ($("#nlfname").val().length == 0) err += "Please supply your first name<br />";
    if ($("#nllname").val().length == 0) err += "Please supply your family name<br />";
    if (!$.validateEmail($("#nlemail1").val())) err += "Please supply a valid e-mail address<br />";
    if ($("#nlemail1").val() != $("#nlemail2").val()) err += "The e-mail addresses do not match";
    if (err.length > 0) {
      $("#nlerr").html(err).show("normal");
      setTimeout("$('#nlerr').hide('normal')", 3000);
    }
    else
      Master.SubmitNewsletter($("#nlfname").val(), $("#nllname").val(), $("#nlemail1").val(), submitnl2);
  }
}

function submitnl2(response) {
  if (response.error) {
    $("#nlerr").html(response.error.Message).show("normal");
    setTimeout("$('#nlerr').hide('normal')", 3000);
  }
  else {
    _gaq.push(['_trackPageview', '/tracker/newsletter']);
    $("#nlform").hide();
    $("#nlthanks").show();
    setTimeout(closeNL, 3000);
  }
}

function closeNL() {
  $("#newsletter").hide("normal");
  $("#blocker").hide();
}

function forgot() {
  Master.ForgotLogin($("#forgotemail").val(), forgot2);
}
function forgot2(response) {
  if (response.error) {
    $("#forgoterr").html(response.error.Message).show("normal");
    setTimeout("$('#forgoterr').hide('normal')", 3000);
  }
  else {
    $("#forgotdiv").html("Login information has been sent to " + $("#forgotemail").val());
    setTimeout(openforgot, 5000);
  }
}

$(document).ready(function(){
   initMaster();
});

