﻿//This file contains all of the calls made to the 'ws_Applications' web service and handles the return calls from it
var intGApplicationID
var strCompanyFormLink;

    function a_SaveOnlineApplication(intUserID, intJobID, strFormLink){
        strCompanyFormLink = strFormLink
        //record the application
        ret = ws_Applications.Save_Site_OnlineForm(intUserID, intJobID, a_OnCompleteOnlineApplication, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteOnlineApplication(result){
        //open the companies own application form
        window.open(strCompanyFormLink);
    }


    function a_NewApplication(intCompanyID, strCompanyName){
        ret = ws_Applications.AddNewApplication(intCompanyID, strCompanyName, a_OnCompleteDetail, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_ShowApplicationDetail(intApplicationID){
        document.getElementById("ApplicationDetail").innerHTML = "Loading....";
        ret = ws_Applications.ApplicationDetail(intApplicationID, a_OnCompleteDetail, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteDetail(result){
        document.getElementById("ApplicationDetail").innerHTML = result;
    }

    function a_ResendApplication(intApplicationID){
        ret = ws_Applications.ResendApplication(intApplicationID, a_OnCompleteResendApplication, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteResendApplication(result){
        if (result.length>0){
            window.alert("The application has been resent to " + result);
        }
    }

    function a_Search(){
	    var intID=0;
	    var intUserID=0;
	    var intJobID=0;
	    var julStartDate='';
	    var julEndDate='';
	    var strCoveringNote='';
	    var strCVPath='';
	    var strStatus='';
        
        intID = document.getElementById("txtsrchID").value;
        intID= CheckNumeric(intID);
        intUserID = document.getElementById("cmbsrchJobApplicants").value;
        intUserID= CheckNumeric(intUserID);
        intJobID = document.getElementById("cmbsrchJobs").value;
        intJobID= CheckNumeric(intJobID);
        strStatus = document.getElementById("cmbsrchJobApplicationStatus").value;
        
        ret = ws_Applications.Search(intID, intUserID, intJobID, julStartDate, julEndDate, strCoveringNote, strCVPath, strStatus, false, false, a_OnCompleteSearch, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteSearch(result){
        document.getElementById("SearchResults").innerHTML = result;    
    }
   
    function a_SetStatus(intApplicationID, strStatus){
        ret = ws_Applications.SetStatus(intApplicationID, strStatus, a_OnCompleteSetStatus, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteSetStatus(result){
        //dont need to do anything here as the statuses are set in real time on the form by the option boxes
    }
    

    function a_Search_Site(intPageNumber, intCompanyID, strOrderBy){
        var intID=0;
	    var intJobID=0;
        var strFreeText;
        var strLocationIDs;
        var strCategoryIDs;
        var strAvailability;
        var strStatus;
        var strContractTypes;
        var strJobTitleSought;
        var strUniversityIDs;
        var strGrades;
        var len;
        var i;

        if (intCompanyID.length<1){
            intCompanyID = 0;
        }
                
        intJobID = document.getElementById("cmbJobs").value;
        if (intJobID.length<1){
            intJobID = 0;
        }

        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 + "~"
            } 
        }
        strAvailability = document.getElementById("cmbAvailability").value;
        strStatus = document.getElementById("cmbStatus").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 + "~"
            } 
        }
        strJobTitleSought = document.getElementById("txtJobTitleSought").value;
        len = document.getElementById("lstUniversities").length
        i = 0
        strUniversityIDs = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstUniversities")[i].selected) {
                strUniversityIDs = strUniversityIDs + document.getElementById("lstUniversities")[i].value + "~"
            } 
        }
        len = document.getElementById("lstGrades").length
        i = 0
        strGrades = '';
        for (i = 0; i < len; i++) {
            if (document.getElementById("lstGrades")[i].selected) {
                strGrades= strGrades + document.getElementById("lstGrades")[i].value + "~"
            } 
        }

        document.getElementById("divSearchResults").style.visibility = '';
        document.getElementById("divResults").innerHTML = "Loading....";
                
        ret = ws_Applications.Search_Site(intPageNumber, 10, intID, intJobID, strFreeText, strLocationIDs, strCategoryIDs, strAvailability, strStatus, strContractTypes, strJobTitleSought, strUniversityIDs, strGrades, strOrderBy, intCompanyID, a_OnCompleteSearch_Site, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteSearch_Site(result){
        var varArray = result.split("||");
        document.getElementById("divResults").innerHTML = varArray[0];
        document.getElementById("divResultsHead").innerHTML = varArray[1];
    }


    function a_ShowAll(){
        ret = ws_Applications.ShowAll(a_OnCompleteShowAll, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_OnCompleteShowAll(result){
        document.getElementById("SearchResults").innerHTML = result;
    }


    function a_Save(intID, intUserID, intJobID){
	    var strCoveringNote='';
	    var strCVPath='';
	    var strStatus='';
        
        intID= CheckNumeric(intID);
        intUserID = CheckNumeric(intUserID);
        intJobID = CheckNumeric(intJobID);
        strCoveringNote = document.getElementById("txtCoveringNote").value;
        strCVPath = document.getElementById("txtCVPath").value;
        strStatus = document.getElementById("cmbJobApplicationStatuses").value;

        ret = ws_Applications.Save(intID, intUserID, intJobID, strCoveringNote, strCVPath, strStatus, a_OnCompleteSave, a_OnErrorGeneric, a_OnTimeoutGeneric);
    }
    function a_Delete(intID){
        var resp= confirm("Do you really want to delete this Application?");
        if (resp==true){
            ret = ws_Applications.Delete(intID, a_OnCompleteSave, a_OnErrorGeneric, a_OnTimeoutGeneric);
        }
    }
    function a_OnCompleteSave(result){
        if (result.length>0){
            window.alert(result);
        }else{
            window.alert("Save complete");
            document.getElementById("ApplicationDetail").innerHTML = '';
        }
    }


    function a_Save_Site(intID, intUserID, intJobID){
	    var strCoveringNote='';
	    var strCVPath='';
	    var strEmailAddress='';
        
        intUserID = CheckNumeric(intUserID);
        intJobID = CheckNumeric(intJobID);
        strCoveringNote = document.getElementById("ctl00_ContentPlaceHolder1_txtGradCoveringNote").value;
        strCVPath = document.getElementById("ctl00_ContentPlaceHolder1_lblGradCVLink").innerHTML;
        strEmailAddress = document.getElementById("ctl00_ContentPlaceHolder1_lblGradEmail").innerHTML;
        if (strCoveringNote.length<1){
            window.alert("Please enter your covering message.");
        }else{    
            ret = ws_Applications.Save_Site(intID, intUserID, intJobID, strCoveringNote, strCVPath, strEmailAddress, a_OnCompleteSave_Site, a_OnErrorGeneric, a_OnTimeoutGeneric);
        }
    }
    function a_OnCompleteSave_Site(result){
        if (result.length>0){
            window.alert(result);
        }else{
            ShowMessageApplied();  //this is on the job_apply.aspx page and shows the confirmation message
        }
    }



    function a_OnErrorGeneric(error){
        var stackTrace = error.gea_stackTrace();
        var message = error.gea_message();
        var statusCode = error.gea_statusCode();
        var exceptionType = error.gea_exceptionType();
        var timedout = error.gea_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 a_OnTimeoutGeneric(error){
        var stackTrace = error.gea_stackTrace();
        var message = error.gea_message();
        var statusCode = error.gea_statusCode();
        var exceptionType = error.gea_exceptionType();
        var timedout = error.gea_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;
    }
