﻿//This file contains all of the calls made to the 'ws_Jobs' web service and handles the return calls from it
var intGJobID
var boolNewJob
var intEnableDisableJobID;

    function j_NewJob(intCompanyID, strCompanyName){
        ret = ws_Jobs.AddNewJob(intCompanyID, strCompanyName, j_OnCompleteDetail, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_ShowJobDetail(intJobID){
        document.getElementById("JobDetail").innerHTML = "Loading....";
        ret = ws_Jobs.JobDetail(intJobID, j_OnCompleteDetail, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteDetail(result){
        document.getElementById("JobDetail").innerHTML = result;
    }

    function j_NewJobSite(intCompanyID){
        boolAppPathVisible=''; //this is the local toggle setting for showing / hiding the app form box.  it needs to be reset whenever a job is displayed
        ret = ws_Jobs.AddNewJobSite(intCompanyID, j_OnCompleteDetailSite, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_ShowJobDetailSite(intJobID, strHeader){
        boolAppPathVisible=''; //this is the local toggle setting for showing / hiding the app form box.  it needs to be reset whenever a job is displayed
        document.getElementById("JobDetail").innerHTML = "Loading....";
        document.getElementById("JobDetailHead").innerHTML = strHeader;
        ret = ws_Jobs.JobDetailSite(intJobID, j_OnCompleteDetailSite, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteDetailSite(result){
        document.getElementById("JobDetail").innerHTML = result;
    }

    function j_EnableDisable(intJobID, boolEnable){
        intEnableDisableJobID = intJobID
        ret = ws_Jobs.EnableDisable(intJobID, boolEnable, j_OnCompleteEnableDisable, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteEnableDisable(result){
        document.getElementById("JobEnableDisable" + intEnableDisableJobID).innerHTML = result;
    }



    function j_Search(){
	    var intID=0;
	    var strTitle='';
	    var strContractType='';
	    var strYourRef='';
	    var strDescription='';
	    var strHours='';
	    var strLocation='';
	    var strSalary='';
	    var boolEmailWhenApplicationIsMade=false;
	    var strEmailForApplicationAlert='';
        var strStartDate='';
        var strEndDate='';
        var boolUseOnlineAppForm=false;
        var boolUseOwnAppForm=false;
        var strAppFormPath='';
        var boolLive=false;
        var intEnteredBy=0;
        var intCategoryID=0;
    
        intID = document.getElementById("txtsrchID").value;
        intID= CheckNumeric(intID);
        strTitle = document.getElementById("txtsrchTitle").value;
        strContractType = document.getElementById("cmbsrchContractTypes").value;
        strYourRef = document.getElementById("txtsrchYourRef").value;
        strDescription = document.getElementById("txtsrchDescription").value;
        strLocation = document.getElementById("txtsrchLocation").value;
        boolLive = document.getElementById("chksrchLive").checked;
        intCategoryID = document.getElementById("cmbsrchJobCategories").value;
        intCategoryID = CheckNumeric(intCategoryID);
        
        ret = ws_Jobs.Search(intID, 0, strTitle, strContractType, strYourRef, strDescription, strHours, strLocation, strSalary, boolEmailWhenApplicationIsMade, strEmailForApplicationAlert, strStartDate, strEndDate, boolUseOnlineAppForm, boolUseOwnAppForm, strAppFormPath, boolLive, intEnteredBy, intCategoryID, false, false, j_OnCompleteSearch, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteSearch(result){
        document.getElementById("SearchResults").innerHTML = result;    
    }
   

    function j_ShowAll(){
        ret = ws_Jobs.ShowAll(j_OnCompleteShowAll, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteShowAll(result){
        document.getElementById("SearchResults").innerHTML = result;
    }

    function j_Job_Search_Refresh(intUserID){
        ret = ws_Jobs.LoadMostRecentUserSearch(intUserID, j_OnCompleteSearchSite, j_OnErrorGeneric, j_OnTimeoutGeneric);        
    }

    function j_Search_Site(intPageNumber, strOrderBy, boolInitialSearch){
        var intID=0;
        var strFreeText;
        var strLocationIDs;
        var strCategoryIDs;
        var strSalary;
        var strContractTypes;
        var strCompanyIDs;
        var len;
        var i;

        if (strOrderBy.length<1){
            strOrderBy = 'intID DESC';
        }
                
        strFreeText = document.getElementById("txtKeyword").value;
        len = document.getElementById("lstLocations").length
        i = 0
        strLocationIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstLocations")[i].selected) {
                strLocationIDs = strLocationIDs + document.getElementById("lstLocations")[i].value + "~"
            } 
        }
        len = document.getElementById("lstCategories").length
        i = 0
        strCategoryIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstCategories")[i].selected) {
                strCategoryIDs = strCategoryIDs + document.getElementById("lstCategories")[i].value + "~"
            } 
        }
        strSalary = document.getElementById("txtSalary").value;
        len = document.getElementById("lstContractTypes").length
        i = 0
        strContractTypes = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstContractTypes")[i].selected) {
                strContractTypes = strContractTypes + document.getElementById("lstContractTypes")[i].value + "~"
            } 
        }
        len = document.getElementById("lstCompanies").length
        i = 0
        strCompanyIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstCompanies")[i].selected) {
                strCompanyIDs = strCompanyIDs + document.getElementById("lstCompanies")[i].value + "~"
            } 
        }

        if (strCategoryIDs.length>0){
            boolInitialSearch=false;
        }
        if (strLocationIDs.length>0){
            boolInitialSearch=false;
        }
        if (strContractTypes.length>0){
            boolInitialSearch=false;
        }
        if (strCompanyIDs.length>0){
            boolInitialSearch=false;
        }
        if (strFreeText.length>0){
            boolInitialSearch=false;
        }
        if (strSalary.length>0){
            boolInitialSearch=false;
        }
        document.getElementById("divSearchResults").style.visibility = '';
        document.getElementById("divResults").innerHTML = "Loading....";
        ret = ws_Jobs.SearchSite(intPageNumber, 20, intID, strFreeText, strLocationIDs, strSalary, strCategoryIDs, strContractTypes, strCompanyIDs, strOrderBy, boolInitialSearch, j_OnCompleteSearchSite, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteSearchSite(result){
        var varArray = result.split("||");
        document.getElementById("divResults").innerHTML = varArray[0];
        document.getElementById("divResultsHead").innerHTML = varArray[1];
    }

    function j_Save(intID){
        var intCompanyID;
        var intMonthsJobPostLive;
	    var strTitle;
	    var strContractType;
	    var strYourRef;
	    var strDescription;
	    var strHours='';
	    var strLocation;
	    var strSalary='';
	    var boolEmailWhenApplicationIsMade=false;
	    var strEmailForApplicationAlert=false;
        var strStartDate='';
        var strEndDate='';
        var boolUseOnlineAppForm=false;
        var boolUseOwnAppForm=false;
        var strAppFormPath='';
        var boolLive;
        var strCategoryIDs;
    
        intID= CheckNumeric(intID);
        intCompanyID = querySt("cid");  //querySt is a function contained on the jobs.aspx page
        intCompanyID = CheckNumeric(intCompanyID);
        strTitle = document.getElementById("txtTitle").value;
        strContractType = document.getElementById("cmbContractTypes").value;
        strYourRef = document.getElementById("txtYourRef").value;
        strDescription = document.getElementById("txtDescription").value;
        strHours = document.getElementById("txtHours").value;
        strLocation = document.getElementById("txtLocation").value;
        strSalary = document.getElementById("txtSalary").value;
        boolEmailWhenApplicationIsMade = document.getElementById("chkEmailWhenApplicationIsMade").checked;
        strEmailForApplicationAlert = document.getElementById("txtEmailForApplicationAlert").value;
        strStartDate = document.getElementById("txtStartDate").value;
        strEndDate = document.getElementById("txtEndDate").value;
        boolUseOnlineAppForm = document.getElementById("chkUseOnlineAppForm").checked;
        boolUseOwnAppForm = document.getElementById("chkUseOwnAppForm").checked;
        strAppFormPath = document.getElementById("txtAppFormPath").value;
        boolLive = document.getElementById("chkLive").checked;
        intMonthsJobPostLive = document.getElementById("cmbMonthsJobPostLive").value;
        intMonthsJobPostLive= CheckNumeric(intMonthsJobPostLive);

        len = document.getElementById("lstJobCategories").length
        i = 0
        strCategoryIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstJobCategories")[i].selected) {
                strCategoryIDs = strCategoryIDs + document.getElementById("lstJobCategories")[i].value + "~"
            } 
        }
        
        /*
        window.alert(intID);
        window.alert(intCompanyID);
        window.alert(boolEmailWhenApplicationIsMade);
        window.alert(boolUseOnlineAppForm);
        window.alert(boolUseOwnAppForm);
        window.alert(boolLive);
        window.alert(strCategoryIDs);
        */
        ret = ws_Jobs.Save(intID, intMonthsJobPostLive, strTitle, strContractType, strYourRef, strDescription, strHours, strLocation, strSalary, boolEmailWhenApplicationIsMade, strEmailForApplicationAlert, strStartDate, strEndDate, boolUseOnlineAppForm, boolUseOwnAppForm, strAppFormPath, boolLive, strCategoryIDs, intCompanyID, j_OnCompleteSave, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_Delete(intID){
        var resp= confirm("Do you really want to delete this Job?");
        if (resp==true){
            ret = ws_Jobs.Delete(intID, j_OnCompleteSave, j_OnErrorGeneric, j_OnTimeoutGeneric);
        }
    }
    function j_OnCompleteSave(result){
        if (result.length>0){
            if (IsNumeric(result)==true){
                window.alert("Save complete");
                if (result>0){
                    window.location='companies.aspx?guid=show&id=' + result;
                }else{
                    document.getElementById("JobDetail").innerHTML = result;
                }
            }else{
                window.alert(result);
            }
        }
    }



    function j_Save_Site(intID, intCompanyID, strFormAppPath){
	    var strTitle;
	    var strContractType;
	    var strYourRef;
	    var strDescription;
	    var strHours='';
	    var strLocation;
	    var strSalary='';
	    var boolEmailWhenApplicationIsMade=false;
	    var strEmailForApplicationAlert=false;
        var strStartDate='';
        var strEndDate='';
        var boolUseOnlineAppForm=false;
        var boolUseOwnAppForm=false;
        var boolLive;
        var strCategoryIDs;
        var strAppFormPath='';
    
        if (intID>0){
            boolNewJob = false;
        }else{
            boolNewJob = true;
        }
                
        strTitle = document.getElementById("txtTitle").value;
        strContractType = document.getElementById("cmbContractTypes").value;
        strYourRef = document.getElementById("txtYourRef").value;
        strDescription = document.getElementById("txtDescription").value;
        strHours = document.getElementById("txtHours").value;
        strLocation = document.getElementById("txtLocation").value;
        //strSalary = document.getElementById("txtSalary").value;
        strSalary = document.getElementById("cmbSalary").value;
        boolEmailWhenApplicationIsMade = document.getElementById("chkEmailWhenAppIsMade").checked;
        strEmailForApplicationAlert = document.getElementById("txtSubmissionEmail").value;
        //strStartDate = document.getElementById("txtPostStartDate").value;
        //strEndDate = document.getElementById("txtPostEndDate").value;
        boolUseOnlineAppForm = document.getElementById("chkUseOnlineForm").checked;
        boolUseOwnAppForm = document.getElementById("chkUseOwnForm").checked;
        strAppFormPath = document.getElementById("txtAppFormPath").value;
        if (strAppFormPath.length<1){
            strAppFormPath = strFormAppPath;
        }
        boolLive = true;

        len = document.getElementById("lstJobCategories").length
        i = 0
        strCategoryIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstJobCategories")[i].selected) {
                strCategoryIDs = strCategoryIDs + document.getElementById("lstJobCategories")[i].value + "~"
            } 
        }
        document.getElementById("DivJobSave").innerHTML = 'working...';
        ret = ws_Jobs.Save_Site(intID, strTitle, strContractType, strYourRef, strDescription, strHours, strLocation, strSalary, boolEmailWhenApplicationIsMade, strEmailForApplicationAlert, strStartDate, strEndDate, boolUseOnlineAppForm, boolUseOwnAppForm, strAppFormPath, boolLive, strCategoryIDs, intCompanyID, j_OnCompleteSaveSite, j_OnErrorGeneric, j_OnTimeoutGeneric);
    }
    function j_OnCompleteSaveSite(result){
        if (result.length>0){
            if (IsNumeric(result)==true){
                if (boolNewJob == true){
                    ShowMessage(); //this function is held on the job_post.aspx page
                }else{
                    window.location='job_post.aspx';
                }
            }else{
                window.alert(result);
            }
        }
    }


    function j_Preview_Site(intID, intCompanyID, strFormAppPath){
        //save the job to the database (but dont make it live), redirect to the preview page and as soon as the preview is created and shown, delete the job row
	    var strTitle;
	    var strContractType;
	    var strYourRef;
	    var strDescription;
	    var strHours='';
	    var strLocation;
	    var strSalary='';
	    var boolEmailWhenApplicationIsMade=false;
	    var strEmailForApplicationAlert=false;
        var strStartDate='';
        var strEndDate='';
        var boolUseOnlineAppForm=false;
        var boolUseOwnAppForm=false;
        var boolLive;
        var strCategoryIDs;
    
        if (intID>0){
            boolNewJob = false;
        }else{
            boolNewJob = true;
        }
        
        if (boolNewJob==true){
            strTitle = document.getElementById("txtTitle").value;
            strContractType = document.getElementById("cmbContractTypes").value;
            strYourRef = document.getElementById("txtYourRef").value;
            strDescription = document.getElementById("txtDescription").value;
            strHours = document.getElementById("txtHours").value;
            strLocation = document.getElementById("txtLocation").value;
            strSalary = document.getElementById("cmbSalary").value;
            boolEmailWhenApplicationIsMade = document.getElementById("chkEmailWhenAppIsMade").checked;
            strEmailForApplicationAlert = document.getElementById("txtSubmissionEmail").value;
            //strStartDate = document.getElementById("txtPostStartDate").value;
            //strEndDate = document.getElementById("txtPostEndDate").value;
            boolUseOnlineAppForm = document.getElementById("chkUseOnlineForm").checked;
            boolUseOwnAppForm = document.getElementById("chkUseOwnForm").checked;
            boolLive = false;

            len = document.getElementById("lstJobCategories").length
            i = 0
            strCategoryIDs = '';
            for (i = 0; i < len; i++) {
                if (document.getElementById("lstJobCategories")[i].selected) {
                    strCategoryIDs = strCategoryIDs + document.getElementById("lstJobCategories")[i].value + "~"
                } 
            }
            ret = ws_Jobs.Save_Preview(intID, strTitle, strContractType, strYourRef, strDescription, strHours, strLocation, strSalary, boolEmailWhenApplicationIsMade, strEmailForApplicationAlert, strStartDate, strEndDate, boolUseOnlineAppForm, boolUseOwnAppForm, strFormAppPath, boolLive, strCategoryIDs, intCompanyID, j_OnCompleteSavePreview, j_OnErrorGeneric, j_OnTimeoutGeneric);
        }else{
            j_OnCompleteSavePreview(intID);
        }
    }
    function j_OnCompleteSavePreview(result){
        if (IsNumeric(result)==true){
            if (boolNewJob == true){
                window.open('job.aspx?jid=' + result + '&prev=tr&cl=tr', 'Job_Preview');
            }else{
                window.open('job.aspx?jid=' + result + '&prev=tr', 'Job_Preview');
            }
        }else{
            window.alert(result);
        }
    }


    function j_OnErrorGeneric(error){
        var stackTrace = error.gej_stackTrace();
        var message = error.gej_message();
        var statusCode = error.gej_statusCode();
        var exceptionType = error.gej_exceptionType();
        var timedout = error.gej_timedOut();
        var strMessage;
       
        // Display the error.    
        //strMessage = "Stack Trace: " +  stackTrace + "    ||    Service Error: " + message + "    ||    Status Code: " + statusCode + "    ||    Exception Type: " + exceptionType + "    ||    Timedout: " + timedout;
        strMessage = "Message: " +  message;
        window.alert("Error:" + strMessage);
    }
    
    function j_OnTimeoutGeneric(error){
        var stackTrace = error.gej_stackTrace();
        var message = error.gej_message();
        var statusCode = error.gej_statusCode();
        var exceptionType = error.gej_exceptionType();
        var timedout = error.gej_timedOut();
        var strMessage;
       
        // Display the error.    
        //strMessage = "Stack Trace: " +  stackTrace + "    ||    Service Error: " + message + "    ||    Status Code: " + statusCode + "    ||    Exception Type: " + exceptionType + "    ||    Timedout: " + timedout;
        strMessage = "Message: " +  message;
        window.alert("Error:" + strMessage);
    }    

    function CheckNumeric(input){
        var intOutput;
        intOutput= '';
        intOutput = new String(input);
        if (intOutput.length<1){
            intOutput='j';
        }
        if(isNaN(intOutput)==true){
            intOutput = 0;
        }
        return intOutput;
    }
    
    function IsNumeric(sText){
        var ValidChars = "0123456789.";
        var IsNumber=true;
        var Char; 
        for (i = 0; i < sText.length && IsNumber == true; i++) 
          { 
          Char = sText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
             {
             IsNumber = false;
             }
          }
        return IsNumber;
    }

