//serverID - - needed for appending the https link
//szUser - - userId
//szPassword - - user password
//ClBkPostUserLogin - - callback function to process the response from the login process. It must accept
//  one string argument, which is the response from the login process and will be
//  'successful' or 'refreshpage' if it's successful.  Otherwise, it's the error from logging in.
//
//Example:
//      postUserLogin(
//        '<%=Request.ServerVariables["SERVER_NAME"]%>',
//        txtUserId.value,
//        txtPassWord.value,
//        chkSaveMe.value,
//        respondToLoginUser
//        );
//
//

//function PostRequest(RequestURL) {
//  var frm = document.forms[0];
//  if (frm.__VIEWSTATE) {
//    frm.__VIEWSTATE.name = "NOVIEWSTATE";
//  }
//  frm.action = RequestURL;
//  frm.submit();
//}

function postUserLogin(serverID, szUser, szPassword, szSaveMe, callBackFucntion) {
  var params = 'Command=login&UserId=' + formatFieldForAjaxPost(szUser) + '&Password=' + formatFieldForAjaxPost(szPassword) + '&saveme=' + szSaveMe;
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postUserRegister(serverID, szUserParms, callBackFucntion) {
  var params = 'Command=register' + szUserParms;
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postEditPassword(serverID, szCurPassword, szPassword, szPassword1, callBackFucntion) {
  var params = 'Command=edit_Password&txtCurPasswordEP=' + formatFieldForAjaxPost(szCurPassword) + '&txtPasswordEP=' + formatFieldForAjaxPost(szPassword) + '&txtPasswordEP1=' + formatFieldForAjaxPost(szPassword1);
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postEditEmail(serverID, szCurPassword, szEmail, szEmail1, callBackFucntion) {
  var params = 'Command=edit_Email&txtPasswordEE=' + formatFieldForAjaxPost(szCurPassword) + '&txtEmailEE=' + formatFieldForAjaxPost(szEmail) + '&txtEmailEE1=' + formatFieldForAjaxPost(szEmail1);
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postSawAssessmentQuiz(serverID, callBackFunction) {
  var params = 'Command=update_SawAssessmentQuiz';
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFunction);
}

function postForgotPassword(serverID, szEmail, callBackFucntion) {
  var params = 'Command=forgot_Password&txtEmailFP=' + formatFieldForAjaxPost(szEmail);
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postATI(serverID, szUser, szEmail, szMessage, callBackFucntion) {
  var params = 'txtYourName=' + formatFieldForAjaxPost(szUser) + '&txtYourEmail=' + formatFieldForAjaxPost(szEmail) + '&txtMessage=' + formatFieldForAjaxPost(szMessage);
  postToServer(getServerURL(serverID, '/Common/AskTheInstitutePostTo.aspx'), params, callBackFucntion);
}
function postEmailSendPage(serverID, szUser, szUserEmail, szRecipEmail, szMessage, callBackFucntion) {
  var params = 'Command=send_Page_Email&txtYourName=' + formatFieldForAjaxPost(szUser) + '&txtYourEmail=' + formatFieldForAjaxPost(szUserEmail) + '&txtRecipEmail=' + formatFieldForAjaxPost(szRecipEmail) + '&txtareaMessage=' + formatFieldForAjaxPost(szMessage) + '&PAGE=' + formatFieldForAjaxPost(szEmailPageLink);
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFucntion);
}

function postUpdateKnowledgeLevel(serverID, szKnowledgeLevel, callBackFunction) {
  var params = 'Command=update_Knowledge_Level&txtKnowledgeLevel=' + formatFieldForAjaxPost(szKnowledgeLevel);
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFunction);
}

function postDoNotShowAQ(serverID, callBackFunction) {
  var params = 'Command=do_not_show_aq';
  postToServer(getServerURL(serverID, '/UserAdmin/UserPostTo.aspx'), params, callBackFunction);
}

/* Helper functions */
function postToServer(serverURL, params, callBackFucntion) {
  $.ajax({
    type: "POST",
    url: serverURL,
    data: params,
    dataType: "application/x-www-form-urlencoded",
    cache: false,
    success: callBackFucntion
  });
}

function getHTTPSServerURL(serverID, url) {
  var serverURL = '';
  if (isIntranet != true && serverID != '198.160.148.37') {
    serverURL = 'https://' + serverID;
  }
  return serverURL + url;
}

function getServerURL(serverID, url) {
  var serverURL = '';
  return url;
}

function CheckInputModal(e, butt) {
  var key;
  if (window.event) { key = e.keyCode; }
  else if (e.which) { key = e.which; }
  if (key == 13) {
    btn = document.getElementById(butt);
    btn.click();
    //stop event capturing phase
    e.returnValue = false;
    e.cancel = true;
    //stop event bubbling phase
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
  }
  return false;
}

function getRedirectURL(returnURL) {
  if ((typeof returnURL == 'undefined') || (returnURL == '')) {
    return window.location.href.substring(window.location.href.indexOf(window.location.pathname));
  }
  else { return returnURL; }
}
/* End Helper Methods */


