﻿var jQueryScriptOutputted = false;
function initJQuery() {  
  if (typeof (jQuery) == 'undefined') {
    if (!jQueryScriptOutputted) {      
      jQueryScriptOutputted = true;
      document.write("<script type=\"text/javascript\" src=\"/Global/javascript/JQuery/js/jquery-1.3.2.min.js\"></script>");
    }
    setTimeout("initJQuery()", 50);
  } else {
    $(function () {
      $(document).ready(Startup);
    });
  }
}

initJQuery();

function DoFeedBack() { if ($("#divFeedBack").length > 0) { return false; } else { return true; } }

function Startup() {
  if (!DoFeedBack()) { return };

  $.ajax({ type: "POST",
    url: "/Global/WebServices/Login.asmx/CurrentUserID",
    data: " { }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
      if (msg.d != "0") {
        RenderFeedBack();
      }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {      
    }
  });
}

function RenderFeedBack() {
  if (!DoFeedBack()) { return };

  $(document.body).append("<div id=\"divFeedBack\" style=\"position: absolute; z-index: 1000; right: 0px; top: 0px;\" ></div>");
  $(document.body).append("<div id=\"divFeedBack\" style=\"position: absolute; right: 0px; top: 0px;\" ></div>");
  
/*
  $(window).scroll(function () {
    MoveFeedBack();
  });

  $(window).resize(function () {
    MoveFeedBack();
  });*/

  $("#divFeedBack").append("<table id=\"feedback_main\" style=\"border-collapse:collapse;\"></table>");
  $("#feedback_main").append("<tr valign=\"bottom\" align=\"right\"><td valign=\"bottom\" align=\"right\"><img alt=\"Feedback\" style=\"cursor:help;\"src=\"/Global/Images/feedbackH.png\" onclick=\"showfeedback();\">");
  $("#feedback_main").append("<tr><td id=\"fbtd\">");  
  $("#fbtd").append("<table id=\"feedback_table\"></table></td>");
  $("#feedback_table").append("<tr><td class=\"PPHeaderRow\" style=\"text-align: center;\" id=\"feedback_FeedbackHeader\"><b>Feedback</b>");
  $("#feedback_table").append("<tr><td class=\"Print\">As part of our ongoing improvement program we have provided this feedback service so you can let us know your thoughts on various aspects of the website.");
  $("#feedback_table").append("<tr><td class=\"Print\"><select id=\"ddlCat\"><option value=\"\">Select a Category</option><option value=\"1\">Compliment</option><option value=\"2\">Complaint</option><option value=\"3\">Suggestion</option><option value=\"4\">Tech Support</option><option value=\"5\">Content Enquiry</option><option value=\"6\">General Enquiry</option></select>");
  $("#feedback_table").append("<tr><td><textarea id=\"feedback_txtComments\" cols=\"20\" rows=\"4\"></textarea>");
  $("#feedback_table").append("<tr><td class=\"Print\">Your questions and comments are welcomed and appreciated.</td></tr>");
  $("#feedback_table").append("<tr><td id=\"feedback_busy\" style=\"align: center;\">");
  $("#feedback_table").append("<tr><td style=\"text-align: center;\"><input type=\"button\" id=\"feedback_btnSend\" value=\"Submit\" onclick=\"javascript:FeedbackSumbit();\"></td></tr>");
  $("#feedback_table").attr("style", "text-align:center;background-color:#DEF1D1;Border:1px solid #A1E292;border-collapse:collapse;");
  $("#feedback_table").addClass("DT");
  $("#feedback_table").hide();
  Resize("80", "20");
}

function Resize(AWidth, AHeight) {
  $("#divFeedBack").attr("width", AWidth); $("#divFeedBack").attr("height", AHeight);
  $("#feedback_main").attr("width", AWidth);
  
  $("#divFeedBack").css("top", "0");
  $("#divFeedBack").css("right", "0" );
  //MoveFeedBack();
}

function MoveFeedBack() {
  $("#divFeedBack").hide();
  $("#divFeedBack").css("top", ($(window).scrollTop()));
  $("#divFeedBack").css("right", "0");
  $("#divFeedBack").fadeIn(100);    
}

function showfeedback() {
  var isHidden = $("#feedback_table").is(":hidden");
  if (isHidden) {
    $("#feedback_table").show();
    Resize("206", "290");                
    $("#feedback_txtComments").focus();
  } else {
    Resize("80", "20");
    $("#feedback_table").hide();
  };
  //MoveFeedBack();
}

function FeedbackSumbit() {
  $("#feedback_busy").html("<img src=\"/Global/Images/ajaxloader.gif\" width=\"180px\">");
  var sDesc = $("#feedback_txtComments").val().replace("\'", "");
  if ((sDesc == null) || (sDesc.replace(/\s/g, "") == "")) {
    alert("Please enter a message.");
    $("#feedback_busy").html("");
    return;
  }

  $("#feedback_btnSend").hide();

  var IssueType = $("#ddlCat").val();
  if ((IssueType == null) || (IssueType == "")) {
    IssueType = "0";
  }

  $.ajax({ type: "POST",
    url: "/Global/WebServices/Feedback.asmx/FeedBack",
    data: " {'IssueType': '" + IssueType + "', 'Description': '" + $("#feedback_txtComments").val().replace("\'", "") + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
      if (msg.d == "Success") {
        alert("Thank you for your feedback");
        $("#feedback_busy").html("");
        $("#feedback_btnSend").show();
        $("#feedback_txtComments").val("");
        $("#ddlCat").val("0");
        showfeedback();
      } else {
        Feedbackfail(msg.d);
      }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      Feedbackfail(textStatus);
    }
  });
}

function Feedbackfail(msg) {
  $("#feedback_busy").html("");
  $("#feedback_btnSend").show();
  alert("There was a problem in sumbitting your response:\r\n" + msg);
}
