
window.onload = attach;

function attach(){
  $("#nexticon").click(showNext);
  $("#beforeicon").click(showBefore);
  $("#search_stepup").click(search);
  $("#sb_backnumber").click(gotoback);
  $("#btnconfirm").click(confirmq);
  $("#btnsubmit").click(submitq);
  $("#btncancel").click(cancelq);
  $("#keyword").keypress(chgFocus);
  $("#userid").keypress(chgFocus2);
  $("#password").keypress(chgFocus2);
  $("#jognotemobile").click(goMobile);
  $("#foodbanner").click(goFood);
  $("#maintitle").click(goTop);
  $("#loginbutton").click(checkLogin);
  $("#jognote").click(goJognote);
}

function gotoback(){
  location.href = "/marathon/back/";
}

function showNext(){
  $("#part1").hide();
  $("#part2").fadeIn("slow");
  window.scroll(0,0);
}

function showBefore(){
  $("#part2").hide();
  $("#part1").fadeIn("slow");
  window.scroll(0,0);
}

function search(){
  var key = document.getElementById("keyword").value;
  $.post( "/", {
    ajax: "Stepup",
    key: key
    }, function(text, status) {
      $("#column").hide();
      $("#backnumbers").hide();
      $("#searchres").show();
      $("#res").html(text);
  });
}

function confirmq(){
  var question = $("#question").val();
  if(question.length>500){
    alert(str1);
    return;
  }
  question = question.replace(/\r\n|\r|\n/g,"<br />");
  $("#confirmq").html(question);
  $("#q_notice").addClass("hidden");
  $("#max255").addClass("hidden");
  $("#btnconfirm").addClass("hidden");
  $("#btnsubmit").removeClass("hidden");
  $("#btncancel").removeClass("hidden");
  $("#question").addClass("hidden");
  $("#confirmq_wrapper").removeClass("hidden");
  $("#confirmq_wrapper").fadeIn("slow");
}
function cancelq(){
  $("#q_notice").removeClass("hidden");
  $("#max255").removeClass("hidden");
  $("#btnconfirm").removeClass("hidden");
  $("#btnsubmit").addClass("hidden");
  $("#btncancel").addClass("hidden");
  $("#confirmq_wrapper").hide();
  $("#question").removeClass("hidden");
  $("#question").fadeIn();
}

function submitq(form){
  $("#thanks").removeClass("hidden");
  var question = document.getElementById("question").value;
  var pub = document.getElementById("pub").value;

  $.post( "/", {
    ajax:   "Other",
    state:  "stepupq",
    question: question,
    pub: pub
    }, function(text, status) {
      if(text=="1"){
        $("#confirmq_wrapper").hide();
        $("#snake").addClass("hidden");
        $("#thanks").html(str5);
      }
      else{
        $("#snake").addClass("hidden");
        $("#thanks").html(str6);
      }
      $("#btnconfirm").addClass("hidden");
      $("#btnsubmit").addClass("hidden");
      $("#btncancel").addClass("hidden");
      $("#maxstr").addClass("hidden");
  });

}

function chgFocus(event){
  if(event.keyCode == 13){
    document.getElementById("search_stepup").click();
  }
}

function chgFocus2(event){
  if(event.keyCode == 13){
    checkLogin(); 
  }
}

function goMobile(){
  location.href = "/mobile-info/info.html";
}

function goFood(){
  location.href = "/column/food/";
}

function goTop(){
  location.href = "/column/stepup/";
}

function checkLogin(){
  var userid  = document.getElementById("userid").value;
  var pass  = document.getElementById("password").value;
  $.post( "/", {
    ajax: "Login",
    userid: userid,
    pass: pass,
    state: "login" 
    }, function(text, status) {
      if(text==1){
        location.href = "/marathon/";
      }
      else{
        $("#loginnotice").html(str4);
        $("#loginnotice").slideDown("slow");
      }
  });
}

function logout(){
  $.post( "/", {
    ajax: "Login",
    state:  "logout"
    }, function(text, status) {
    //location.href="/";
  });

  dTime = new Date();
  dTime.setYear(dTime.getYear() - 1);
  document.cookie = "userid=;expires=" + dTime.toGMTString();
  $("#yokoso").addClass("hidden");
  $("#logincontainer").fadeIn("slow");
  $("#needlogin").show();
  $("#buttons").hide();
}

function goJognote(){
  location.href = "/";
}
