
var engine_application_process = "webcpms/priv_application.php";

var q_basic = new Array("Browser", "WordProcessor", "Laptop", "Car", "CellPhone", "Sales");

var ajaxSaveApplication,
    ajaxAttachResumeFile;

function validateApplicationForm() {

   var err = false;

   var basic = "";

   var i;

   toggle("ApplicationErrorMsgHost", true, "none");
   toggle("AuthenticationErrorMsgHost", true, "none");

   toggle('ApplicationErrorPersonal', false);
   for (i = 0; i < q_basic.length; i++) {
      toggle('ApplicationError' + q_basic[i], true, "none");
      toggle('ApplicationErrorSelect' + q_basic[i], true, "none");
   }
   toggle('ApplicationErrorDifficult', true, "none");
   toggle('ApplicationErrorEasiest', true, "none");
   toggle('ApplicationErrorWhyWant', true, "none");

//   if (document.getElementById('SessionID').value.length == 0) {
      var pers = "";

      var personal = validateInputPersonal("Application");
      var address = document.getElementById('SessionID').value.length == 0 ? validateInputAddress("Application") : true;

      if (personal && address) {
         pers += "&PersonalID=" + personal[6];
         pers += "&PersonalFirstName=" + personal[0];
         pers += "&PersonalLastName=" + personal[1];
         pers += "&PersonalPhoneArea=" + personal[2] + "&PersonalPhoneFirst=" + personal[3] + "&PersonalPhoneSecond=" + personal[4];
         pers += "&PersonalEMail=" + personal[5];
         if (document.getElementById('SessionID').value.length == 0) {
            pers += "&PersonalAddress1=" + address[0];
            pers += "&PersonalAddress2=" + address[1];
            pers += "&PersonalCity=" + address[2];
            pers += "&PersonalState=" + address[3];
            pers += "&PersonalZip=" + address[4];
         }
      } else {
         toggle('ApplicationErrorPersonal', true);
         err = true;
      }
//   }
/*
   for (i = 0; i < q_basic.length; i++) {
      if (!document.getElementById('ApplicationChkYes' + q_basic[i]).checked && !document.getElementById('ApplicationChkNo' + q_basic[i]).checked) {
         toggle('ApplicationErrorSelect' + q_basic[i], true, "inline");
         err = true;
      } else if (document.getElementById('ApplicationChkYes' + q_basic[i]).checked && document.getElementById('Application' + q_basic[i]).value.length == 0) {
         toggle('ApplicationError' + q_basic[i], true, "inline");
         err = true;
      } else if (document.getElementById('ApplicationChkYes' + q_basic[i]).checked) {
         basic += "&Application" + q_basic[i] + "=" + encodeURIComponent(document.getElementById('Application' + q_basic[i]).value);
      }
   }

   var application = "";

   if (!document.getElementById('ApplicationChkDifficultConnecting').checked &&
       !document.getElementById('ApplicationChkDifficultConvincing').checked &&
       !document.getElementById('ApplicationChkDifficultRejection').checked &&
       !document.getElementById('ApplicationChkDifficultNothing').checked) {
      toggle('ApplicationErrorDifficult', true, "inline");
      err = true;
   } else {
      application += "&ApplicationDifficult=";
      if (document.getElementById('ApplicationChkDifficultConnecting').checked) application += "Connecting";
         else if (document.getElementById('ApplicationChkDifficultConvincing').checked) application += "Convincing";
         else if (document.getElementById('ApplicationChkDifficultRejection').checked) application += "Rejection";
         else if (document.getElementById('ApplicationChkDifficultNothing').checked) application += "Nothing";
   }
   if (!document.getElementById('ApplicationChkEasiestConnecting').checked &&
       !document.getElementById('ApplicationChkEasiestConvincing').checked &&
       !document.getElementById('ApplicationChkEasiestRejection').checked &&
       !document.getElementById('ApplicationChkEasiestNothing').checked) {
      toggle('ApplicationErrorEasiest', true, "inline");
      err = true;
   } else {
      application += "&ApplicationEasiest=";
      if (document.getElementById('ApplicationChkEasiestConnecting').checked) application += "Connecting";
         else if (document.getElementById('ApplicationChkEasiestConvincing').checked) application += "Convincing";
         else if (document.getElementById('ApplicationChkEasiestRejection').checked) application += "Rejection";
         else if (document.getElementById('ApplicationChkEasiestNothing').checked) application += "Nothing";
   }
   if (!document.getElementById('ApplicationChkWhyWantMoney').checked &&
       !document.getElementById('ApplicationChkWhyWantSuccess').checked &&
       !document.getElementById('ApplicationChkWhyWantBoth').checked &&
       !document.getElementById('ApplicationChkWhyWantNo').checked) {
      toggle('ApplicationErrorWhyWant', true, "inline");
      err = true;
   } else {
      application += "&ApplicationWhyWant=";
      if (document.getElementById('ApplicationChkWhyWantMoney').checked) application += "Money";
         else if (document.getElementById('ApplicationChkWhyWantSuccess').checked) application += "Success";
         else if (document.getElementById('ApplicationChkWhyWantBoth').checked) application += "Both";
         else if (document.getElementById('ApplicationChkWhyWantNo').checked) application += encodeURIComponent("I do not want to be in sales");
   }
*/
   if (!err) {
      startIndicator("ApplicationIndicator", "Processing");
//      var cmd = "cmd=cmdSaveApplication&sid=" + document.getElementById('SessionID').value + basic + application + pers + "&ResumeUploader=" + resume_uploader + "&ResumeFile=" + top.frames[resume_uploader].document.getElementById('UploadInput').value + "&captcha=" + document.getElementById('ApplicationCaptchaCode').value;
      var cmd = "cmd=cmdSaveApplication&sid=" + document.getElementById('SessionID').value + pers + "&ResumeUploader=" + resume_uploader + "&ResumeFile=" + encodeURIComponent(top.frames[resume_uploader].document.getElementById('UploadInput').value) + "&CoverLetterUploader=" + coverletter_uploader + "&CoverLetterFile=" + encodeURIComponent(top.frames[coverletter_uploader].document.getElementById('UploadInput').value) + (document.getElementById('SessionID').value.length == 0 ? "&captcha=" + document.getElementById('ApplicationCaptchaCode').value : "") + "&PositionID=" + document.getElementById('ApplicationPositionID').value;
      load(ajaxSaveApplication = GetXmlHttpObject(), engine_application_process, cmd, savedApplication);
   }

}




function savedApplication() {
   if (ajaxSaveApplication.readyState == 4 || ajaxSaveApplication.readyState == "complete") {
      var err = getProperty("Err", ajaxSaveApplication.responseText);
      if (err.length > 0) {
         if (err == "exists") {
            toggle("ApplicationErrorMsgHost", true, "block");
         } else if (err == "captcha") {
            toggle("AuthenticationErrorMsgHost", true, "block");
         }
         stopIndicator("ApplicationIndicator");
         reloadCaptcha("Application");
      } else {
         var ru = getProperty("ResumeUploader", ajaxSaveApplication.responseText);
         var cu = getProperty("CoverLetterUploader", ajaxSaveApplication.responseText);
         var user = getProperty("User", ajaxSaveApplication.responseText);
         var ready = true;
         if (top.frames[ru].document.getElementById('UploadInput').value.length > 0) {
////            top.frames[ru].document.getElementById('ReadyPage').value = encodeURIComponent(getWebAddress() + "/" + getPageBase() + "?page=careers&step=ready&new_user_id=" + user);
//            top.frames[ru].document.getElementById('ReadyScript').value = 'finishedSavingApplication';
            top.frames[ru].document.getElementById('UserID').value = user;
            top.frames[ru].document.getElementById('root').value = "users/" + user;
            top.frames[ru].document.getElementById('UploaderForm').submit();
            ready = false;
         }
         if (top.frames[cu].document.getElementById('UploadInput').value.length > 0) {
////            top.frames[cu].document.getElementById('ReadyPage').value = encodeURIComponent(getWebAddress() + "/" + getPageBase() + "?page=careers&step=ready&new_user_id=" + user);
//            top.frames[cu].document.getElementById('ReadyScript').value = 'finishedSavingApplication';
            top.frames[cu].document.getElementById('UserID').value = user;
            top.frames[cu].document.getElementById('root').value = "users/" + user;
            top.frames[cu].document.getElementById('UploaderForm').submit();
            ready = false;
         }
         if (ready) {
            loadPage(getWebAddress() + "/" + getPageBase() + "?page=careers&step=ready&new_user_id=" + user);
//            stopIndicator("ApplicationIndicator");
         }
      }
   }
}


function finishedSavingApplication(framename, filename, user_id, files_to_upload) {
   if (files_to_upload == 0) {
      loadPage(getWebAddress() + "/" + getPageBase() + "?page=careers&step=ready&new_user_id=" + user_id);
   }
}




function attachResume(uploadframe, filename, user_id, files_to_upload) {
   var file = top.frames[uploadframe].document.getElementById('UploadedFile').value;

   startIndicator("ApplicationIndicator", "Updating");
   var cmd = "cmd=cmdAttachResume&sid=" + document.getElementById('SessionID').value + "&ResumeFile=" + encodeURIComponent(filename);
   load(ajaxAttachResumeFile = GetXmlHttpObject(), engine_application_process, cmd, attachedResume);
}


function attachedResume() {
   if (ajaxAttachResumeFile.readyState == 4 || ajaxAttachResumeFile.readyState == "complete") {
      stopIndicator("ApplicationIndicator", "Updating");
      var res = ajaxAttachResumeFile.responseText.split("|");
   }
}






function resetApplicationForm() {
   var i;

   toggle("ApplicationErrorMsgHost", true, "none");

   toggle('ApplicationErrorPersonal', false);
   for (i = 0; i < q_basic.length; i++) {
      toggle('ApplicationError' + q_basic[i], true, "none");
      toggle('ApplicationErrorSelect' + q_basic[i], true, "none");
   }
   toggle('ApplicationErrorDifficult', true, "none");
   toggle('ApplicationErrorEasiest', true, "none");
   toggle('ApplicationErrorWhyWant', true, "none");

   document.getElementById('ApplicationLoginOrCreateUserExisting').checked = true;

   document.getElementById('ApplicationUserName').value = '';
   document.getElementById('ApplicationPassword').value = '';

   toggle('ApplicationLoginOrCreateUserExistingHost', true);
   toggle('ApplicationLoginOrCreateUserNewHost', false);

   setInputPersonal("Application");
   setInputAddress("Application");
/*
   for (i = 0; i < q_basic.length; i++) {
      document.getElementById('ApplicationChkYes' + q_basic[i]).checked = false;
      document.getElementById('ApplicationChkNo' + q_basic[i]).checked = false;
      document.getElementById('Application' + q_basic[i]).value = "";
   }

   document.getElementById('ApplicationChkDifficultConnecting').checked = false;
   document.getElementById('ApplicationChkDifficultConvincing').checked = false;
   document.getElementById('ApplicationChkDifficultRejection').checked = false;
   document.getElementById('ApplicationChkDifficultNothing').checked = false;

   document.getElementById('ApplicationChkEasiestConnecting').checked = false;
   document.getElementById('ApplicationChkEasiestConvincing').checked = false;
   document.getElementById('ApplicationChkEasiestRejection').checked = false;
   document.getElementById('ApplicationChkEasiestNothing').checked = false;

   document.getElementById('ApplicationChkWhyWantMoney').checked = false;
   document.getElementById('ApplicationChkWhyWantSuccess').checked = false;
   document.getElementById('ApplicationChkWhyWantBoth').checked = false;
   document.getElementById('ApplicationChkWhyWantNo').checked = false;
*/
}







function loadApplications() {
   var starting_date = document.getElementById('AppYear').value + "-" + makeTwoDigits(document.getElementById('AppMonth_sel').value) + "-" + makeTwoDigits(document.getElementById('AppDay_sel').value);
   var loading_interval = document.getElementById('AppInterval').value;
   var loading_unit = document.getElementById('AppSelector').value;
   if (loading_interval.length == 0 || isNaN(loading_interval)) {
      alert("Please specify interval");
      return;
   }
   loadPage("index.php?page=careers&starting_date=" + starting_date + "&loading_interval=" + loading_interval + "&loading_unit=" + loading_unit);
}


var app_autoresponse_categories;


function loadedApplicantsAutoResponseEMailTemplates() {
   if (ajaxLoadEMailTemplates.readyState == 4 || ajaxLoadEMailTemplates.readyState == "complete") {
      stopIndicator("ApplicantsAutoResponseTemplateIndicator");

      var sel_category = 0, sel_template = document.getElementById('EMailNewApplicant').value;

      app_autoresponse_categories = new Array();

      parseEMailTemplates(ajaxLoadEMailTemplates.responseText, app_autoresponse_categories);

      printEMailTemplateSelector("ApplicantsAutoResponse", app_autoresponse_categories, "", sel_category, sel_template, true);
   }
}



var application_email_categories;




function saveApplicantsSettings() {
   var settings = new Array();

   settings[0] = "email_NewApplicant=" + document.getElementById('ApplicantsAutoResponseTemplateSelector').value + "|New applicant Welcome e-mail";

   saveSystemSettings(settings, false, "ApplicantsAutoResponseSettingsIndicator");
}



var applicant_id = 0;

function printApplicationMessagePanel(id) {
   printMessagePanel('Applicant_' + id, "", new Person(id, document.getElementById('Applicants_' + id + 'RegUserName').value, document.getElementById('Applicants_' + id + 'FirstName').value, document.getElementById('Applicants_' + id + 'LastName').value, "", document.getElementById('Applicants_' + id + 'EMail').value));

   applicant_id = id;

   if (!loadEMailTemplates('Applicant_' + id, loadedApplicationEMailTemplates)) {
      setApplicationMsgTemplate();
   }
}





function loadedApplicationEMailTemplates() {
   if (ajaxLoadEMailTemplates.readyState == 4 || ajaxLoadEMailTemplates.readyState == "complete") {
      stopIndicator("Applicant_" + applicant_id + "TemplateIndicator");

      var sel_category = 0, sel_template = 0;

      application_email_categories = new Array();

      parseEMailTemplates(ajaxLoadEMailTemplates.responseText, application_email_categories);

      printEMailTemplateSelector("Applicant_" + applicant_id, application_email_categories, "setApplicationMsgTemplate();", sel_category, sel_template);
      setApplicationMsgTemplate();
   }
}


function setApplicationMsgTemplate() {
   resetEMailMessageInfo('Applicant_' + applicant_id);

   var tc = document.getElementById('Applicant_' + applicant_id + 'TemplateSelector').value.split(".");

   var category_id = tc[0];
   var template_id = tc[1];

   var template = getEMailTemplate(category_id, template_id, application_email_categories);
   var lang = document.getElementById('lang').value;

   var errors = new Array();

   var body = "";
   if (template) {

      document.getElementById('Applicant_' + applicant_id + 'Subject').value = template.subject();

      body = evaluateVariables(template.body(), 'Applicant_' + applicant_id, null, errors);
   } else {

      document.getElementById('Applicant_' + applicant_id + 'Subject').value = document.getElementById('Applicant_' + applicant_id + 'SubjectOrig').value;

      body = evaluateVariables("Dear %FirstName%,<br /><br /><br />The Staff", 'Applicant_' + applicant_id, null, errors);
   }

   var e = eval("wreApplicant_" + applicant_id + "Text");

   e.setDocument(lang, body);
   e.setText(body);

   setEMailMessageInfo('Applicant_' + applicant_id, errors, false);
}










var applicants_email_categories;


function setApplicantSendMessageList(id) {
   var chk = document.getElementById("Applicants_" + id + "SendMessage");
   var asm = document.getElementById('ApplicantSendMessage');

   var list = asm.value.split(",");

   var i = 0;
   while (i < list.length && list[i] != id) i++;
   var found = i < list.length ? true : false;

   if (chk.checked) {
      if (!found) {
         if (list.length == 1 && list[0] == 0) {
            list[0] = id;
         } else {
            list[list.length] = id;
         }
      }
   } else {
      if (found) {
         list.splice(i, 1);
      }
   }

   asm.value = list.join(",");

   toggle("ApplicantSendMessages", list.length > 0 ? true : false);

   if (list.length > 0) printApplicantsMessagePanel();

   var p = wreApplicantsText.getPlugIn("ApplicantsMessageScroller");
   if (p) p.invalidate(applicants_email_categories, "Dear %FirstName%,<br /><br /><br />The Staff");
}




function printApplicantsMessagePanel() {
   var list = document.getElementById('ApplicantSendMessage').value.split(",");

   var i;
   var tos = new Array();
   for (i = 0; i < list.length; i++) {
      tos[tos.length] = new Person(list[i], document.getElementById('Applicants_' + list[i] + 'RegUserName').value, document.getElementById('Applicants_' + list[i] + 'FirstName').value, document.getElementById('Applicants_' + list[i] + 'LastName').value, "", document.getElementById('Applicants_' + list[i] + 'EMail').value);
   }

   printMessagePanel('Applicants', "", tos);

   if (!loadEMailTemplates('Applicants', loadedApplicantsEMailTemplates)) {
      setApplicantsMsgTemplate();
   }
}





function loadedApplicantsEMailTemplates() {
   if (ajaxLoadEMailTemplates.readyState == 4 || ajaxLoadEMailTemplates.readyState == "complete") {
      stopIndicator("ApplicantsTemplateIndicator");

      var sel_category = 0, sel_template = 0;

      applicants_email_categories = new Array();

      parseEMailTemplates(ajaxLoadEMailTemplates.responseText, applicants_email_categories);

      printEMailTemplateSelector("Applicants", applicants_email_categories, "setApplicantsMsgTemplate();", sel_category, sel_template);
      setApplicantsMsgTemplate();
   }
}


function setApplicantsMsgTemplate() {
   resetEMailMessageInfo('Applicants');

   var tc = document.getElementById('ApplicantsTemplateSelector').value.split(".");

   var category_id = tc[0];
   var template_id = tc[1];

   var template = getEMailTemplate(category_id, template_id, applicants_email_categories);
   var lang = document.getElementById('lang').value;

   var errors = new Array();

   var body = "";
   if (template) {

      document.getElementById('ApplicantsSubject').value = template.subject();

      body = evaluateVariables(template.body(), 'Applicants', null, errors);
   } else {

      document.getElementById('ApplicantsSubject').value = document.getElementById('ApplicantsSubjectOrig').value;

      body = evaluateVariables("Dear %FirstName%,<br /><br /><br />The Staff", 'Applicants', null, errors);
   }

   var e = eval("wreApplicantsText");

   e.setDocument(lang, body);
   e.setText(body);

   setEMailMessageInfo('Applicants', errors, false);

   var p = wreApplicantsText.getPlugIn("ApplicantsMessageScroller");
   if (p) p.invalidate(applicants_email_categories, "Dear %FirstName%,<br /><br /><br />The Staff");
}




function updateApplicantMessages(applicant_id_list) {
   var applicant_ids = applicant_id_list.split(",");

   var i;
   for (i = 0; i < applicant_ids.length; i++) {
      document.getElementById('Applicants_' + applicant_ids[i] + 'SendMessage').checked = false;
      if (document.getElementById('Applicants_' + applicant_ids[i] + 'ListMessagesHost').style.display == "none") {
         document.getElementById('Applicants_' + applicant_ids[i] + 'MessagesPostedUpdated').value = 0;
      } else {
         loadMessagesPosted('Applicants_' + applicant_ids[i], 'Applicants_' + applicant_ids[i] + 'ListMessagesHost', true, 'to=' + applicant_ids[i]);
      }
   }
   document.getElementById('ApplicantSendMessage').value = "";

}


//var ajaxJobInterviewEventOccasion;
//var jobinterview_events_occasions = null;
//
//
//
//
//function saveJobInterviewEventSettings(round) {
//   var settings = new Array();
//
//   var evts = getEventsOccasionsToDisplay("JobRound" + round + "Event");
//
//   var loc_id = document.getElementById("JobRound" + round + "Event_LocationSelector").value;
//   var evt_id = evts[0];
//
//   document.getElementById("JobRound" + round + "EventLocation").value = loc_id;
//   document.getElementById("JobRound" + round + "EventID").value = evt_id;
//
//   settings[0] = "seminarevent_location=" + loc_id;
//   settings[1] = "seminarevent_id=" + evt_id;
//
//   saveSystemSettings(settings, false, "JobRound" + round + "EventIndicator");
//}
//
//
//
//
//function locationChangedJobInterviewEvent() {
//   var loc_id = document.getElementById("JobRound" + round + "Event_LocationSelector").value;
//   var cmd = "cmd=cmdLoadEventsOccasions" +
//            "&sid=" + document.getElementById('SessionID').value +
//            "&loc_id=" + loc_id + "&filter=accept_res";
//   startIndicator("SeminarEventIndicator");
//   load(ajaxSeminarEventOccasion = GetXmlHttpObject(), engine_event_process, cmd, updateSeminarEvent);
//}
//
//
//function updateSeminarEvent() {
//   if (ajaxSeminarEventOccasion.readyState == 4 || ajaxSeminarEventOccasion.readyState == "complete") {
//      stopIndicator("SeminarEventIndicator");
//
//      var seminarevent_id = document.getElementById('SeminarEventID').value;
//
//      seminar_events_occasions = new Array();
//      parseEvents(ajaxSeminarEventOccasion.responseText, seminar_events_occasions);
//      printOccasionFilter('SeminarEvent', seminar_events_occasions, true, new Array(seminarevent_id), null, null);
//   }
//}
//
