var popups = new Array("loginpop", "registerpop", "forgotpop", "sharepop", "tippop");

function initMaster() {

/*  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); }
  }*/

  if ($("quotes"))
    changeQuote();

  $('share').firstChild.firstChild.style.display = 'inline';

  try { initPage(); }
  catch (e) { nfndebug(e.message); };
}


function openPop(id, show) {
  for (var i=0; i < popups.length; i++)
    $(popups[i]).style.display = "none";
  if (show)
    $(id).style.display = "block";;
}
function openLogin() {
  openPop('loginpop', true);
  $("email1").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 email = $("email").value;
  var pw = $("password").value;
  var rem = $('remember').checked;
  var response = Master.LoginCookie(email, pw, rem);
  if (response.value) document.location.href = "/" + _pageId + ".aspx";
  else alert("Login not accepted.");
}

function logout() {
  Master.LogoutCookie();
  document.location.href = "/home.aspx?lo=y";
}

var lastregerror = "Please enter valid values in all mandatory fields";

function setValidInp(inp, valid, err) {
  if (valid) inp.previousSibling.style.color = "#000000";
  else {
    lastregerror = err;
    inp.previousSibling.style.color = "#ee1c25";
    inp.focus();
    $S("regerror").display = "none";
  }
}

function validEmail(inp) {
  var emailregexp = "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$";
  var re = new RegExp(emailregexp);
  var valid = re.test(inp.value);
  setValidInp(inp, valid, "Please enter a valid e-mail address");
  if (!valid)  return false;

  var inp2 = (inp.id.indexOf("email1") >= 0 ? $(inp.id.substring(0, inp.id.length-1) + "2") : $(inp.id.substring(0, inp.id.length-1) + "1"));
  valid = inp2.value.length == 0 || inp2.value == inp.value
  setValidInp(inp, valid, "The supplied e-mail addresses do not match");
  if (!valid) return false;

  var response = Master.ValidUsername(inp.value);
  valid = response.value && inp.value.length > 0;
  setValidInp(inp, valid, "The supplied e-mail is already registered at www.qlucore.com. Please use the main menu to log in.");

  return valid;
}

function validPassword(inp) {
  var inp2 = (inp.id.indexOf("password1") >= 0 ? $(inp.id.substring(0, inp.id.length-1) + "2") : $(inp.id.substring(0, inp.id.length-1) + "1"));
  var valid = inp.value.length > 0 && (inp2.value.length == 0 || inp2.value == inp.value);
  setValidInp(inp, valid, "The supplied passwords do not match");

  return valid;
}

function validMandatory(inp) {
  var valid = inp.value.length > 0;
  setValidInp(inp, valid, "Please enter valid values in all mandatory fields");
  return valid;
}

function validate() {
  if (!validEmail($("email1"))) return false;
  if (!validEmail($("email2"))) return false;
  if (!validPassword($("newpassword1"))) return false;
  if (!validPassword($("newpassword2"))) return false;
  if (!validMandatory($("firstname"))) return false;
  if (!validMandatory($("lastname"))) return false;
  if (!validMandatory($("address"))) return false;
  if (!validMandatory($("city"))) return false;
  if (!validMandatory($("zip"))) return false;
  if (!validMandatory($("country"))) return false;
  if (!validMandatory($("phone"))) return false;
  return true;
}

function register() {
  if (validate()) {
    var vals = new Array();
    vals[0] = $("email1").value;
    vals[1] = $("newpassword1").value;
    vals[2] = $("firstname").value;
    vals[3] = $("lastname").value;
    vals[4] = $("company").value;
    vals[5] = $("address").value;
    vals[6] = $("city").value;
    vals[7] = $("zip").value;
    vals[8] = $("state").value;
    vals[9] = $("country").value;
    vals[10] = $("phone").value;
    vals[11] = $("mobile").value;
    vals[12] = $("email1").value;
    vals[13] = ($("prodinfo").checked ? "Y" : "N");

    var response = Master.RegisterUser(vals, "");

    var html;
    if (response.value.length == 0) {
      html = "<h2>A new account has been created at qlucore.com</h2>";
      html += "<p>An email with a confirming link has been sent to " + vals[12] + ".</p>";
      html += "<p><a href=javascript:openPop('registerpop',false) onfocus='this.blur()'>Close</a></p>";
    }
    else {
      html = "<h2>The registration failed.</h2><p>Error: " + response.value + "</p>";
    }
    var pop = $("registerpop");
    pop.style.width = "200px";
    pop.innerHTML = html;
  }
  else {
    $("regerror").innerHTML = lastregerror;
    $S("regerror").display = "block";
  }
}

function recoverPass() {
  var email = $("forgotemail").value;
  if (email.length == 0)
    alert("Supply your e-mail address.");
  else
    Master.RequestQlucorePassword(email, function(r){recoverPass2(r, email);});
}

function recoverPass2(response, email) {
  if (response.value.length == 0) {
    var html = "<h2>Forgot your password?</h2><p>An e-mail with login credentials has been sent to " + email + ".";
    html += "<p><a href=javascript:openPop('forgotpop',false) onfocus='this.blur()'>Close</a></p>";
    var pop = $("forgotpop");
    pop.style.width = "150px";
    pop.innerHTML = html;
  }
  else {
    var err = $("forgoterror");
    err.style.display = "block";
    err.innerHTML = response.value;
  }
}

var nofQuotes = 5;
var currQuote = 1;

function changeQuote() {
  var newQuote = Math.floor(Math.random()*nofQuotes) + 1;
  while (newQuote == currQuote)
    newQuote = Math.floor(Math.random()*nofQuotes) + 1;
  currQuote = newQuote;
  $("quotes").firstChild.src = "gfx/quotes/quote" + currQuote + ".gif";
  setTimeout("changeQuote()", 10000);
}

function sendtip() {
  var err='';
  if (!validateEmail($('tipemail1').value)) err += "Please enter your friend's email address<br />";
  if (!validateEmail($('tipemail2').value)) err += "Please enter your email address<br />";
  if ($('tipname2').value.length == 0) err += "Please enter your name";
  if (err.length > 0) {
    $('tiperr').innerHTML = err;
    $S('tiperr').display = "block";
    setTimeout("$S('tiperr').display = 'none'", 2000);
  }
  else {
    Master.SendTip($('tipemail1').value, $('tipemail2').value, $('tipname2').value, $('tipmess').value, _pageId, sendtip2);
  }

}

function sendtip2(response) {
  if (response.error) {
    $('tiperr').innerHTML = "An error occured when the tip was sent.";
    //$('tiperr').innerHTML = response.error.Message;
    $S('tiperr').display = "block";
    setTimeout("$S('tiperr').display = 'none'", 2000);
  }
  else {
    $S('tipform').display = "none";
    $S('tipthanks').display = "block";
    setTimeout(hideThanks, 2000);
  }
}

function hideThanks() {
  openPop("tippop", false);
  $S('tipform').display = "block";
  $S('tipthanks').display = "none";
}

if (window.attachEvent) window.attachEvent("onload", initMaster);
else window.addEventListener("load", initMaster, false);
