/**
 * The Opiniator WebClient Webservice.  Mirrors all methods in 
 * the WebClient WebService.  Includes functions for: 
 *  
 * Managing Surveys 
 * Collecting Analytics
 */
isc.defineClass("OPINIATOR_WebClientService", OPINIATOR_SoapService);
//isc.defineClass("OPINIATOR_WebClientService", OPINIATOR_JSONService);
OPINIATOR_WebClientService.addProperties({
    //endpoint: "/services/json/WebClientService",
    endpoint: "/services/soap/WebClientService",
    schema: "1",
    namespace: "ns:webclient.opiniator.com",
    ID: "ns:webclient.opiniator.com",

    /**
     *  Initializes the service.  
     */
    init: function () 
        { this.Super("init", arguments); },

    /**
     * Verfiy's the user and creates a session in the webclient 
     * @param loginame  The login name of the user 
     * @param password  Sha1 encrypted password 
     */
    login: function ( loginname, password, responseHandler, errorHandler )
        { this.callOperation("login", {loginname:loginname, password:password}, 
                             responseHandler, errorHandler, {sendNoQueue:true}); },


    /**
     * Gets a blank survey.  The survey is not recorded in store
     * until saveSurvey is called on the object. 
     *  
     * @param orgid     The orgid in which this survey will be 
     *                  added. 
     */
    newSurvey: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("newSurvey", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     * Gets a Survey from store
     * @param surveyid  The id of the survey
     */
    getSurvey: function ( surveyid, responseHandler, errorHandler )
        { this.callOperation("getSurvey", {surveyid:surveyid}, responseHandler, errorHandler); },

    /**
     * Saves a survey to store
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    saveSurvey: function ( survey, responseHandler, errorHandler )
        { this.callOperation("saveSurvey", {survey:survey}, responseHandler, errorHandler); },

	/**
     * Delete responses of survey
     * @param survey   The survey to delete response
     * 
     */
    deleteResponseOfSurvey: function ( survey, responseHandler, errorHandler )
        { this.callOperation("deleteResponseOfSurvey", {survey:survey}, responseHandler, errorHandler); },


    /**
     * Deletes a survey in store.
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    deleteSurvey: function ( surveyid, responseHandler, errorHandler )
        { this.callOperation("deleteSurvey", {surveyid:surveyid}, responseHandler, errorHandler); },


    /**
     * Returns a Tree of the Surveys accessible to the user in the
     * ParentModel form.
     */
    getSurveyTree: function ( responseHandler, errorHandler )
        { this.callOperation("getSurveyTree", null, responseHandler, errorHandler); },


    /**
     * Gets a blank orgunit.  The orgunit is not recorded in store 
     * until saveOrgUnit is called on the object. 
     *  
     * @param orgid     The orgid in which this orgunit will be 
     *                  added.
     */
    newOrgUnit: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("newOrgUnit", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     * Saves a orgunit to store
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    getOrgUnit: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("getOrgUnit", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     * Saves a orgunit to store
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    saveOrgUnit: function ( org, responseHandler, errorHandler )
        { this.callOperation("saveOrgUnit", {org:org}, responseHandler, errorHandler); },


    /**
     * Saves an orgunit to store
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    deleteOrgUnit: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("deleteOrgUnit", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     * Get's Category References and KPI's for an org
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    getOrgKPIList: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("getOrgKPIList", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     * Get's Category References and KPI's for an org
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    updateOrgKPIList: function ( orgid, kpilist, responseHandler, errorHandler )
        { 
        // Don't send back the category references
        var nkpilist = { kpis:kpilist.kpis };
        this.callOperation("updateOrgKPIList", {orgid:orgid, kpilist:nkpilist}, responseHandler, errorHandler); 
        },


    /**
     * Checks the availability of a keyword.  This check will be 
     * performed at the server on survey save as well. 
     * @param survey   The survey to save
     * @param password  Array of id's for Questions that should be 
     *                  deleted
     */
    checkKeywordAvailability: function ( surveyid, keyword, startdate, enddate, responseHandler, errorHandler )
        { 
        if ( !isc.isA.Number(surveyid) )
            { surveyid = -1; }
        this.callOperation("checkKeywordAvailability", 
                           {surveyid:surveyid, keyword:keyword, range:{begin:startdate, end:enddate}}, 
                           responseHandler, errorHandler); 
        },

    /**
     * Returns a list of categories that the user has access to
     */
    getUserCategoryRefList: function ( responseHandler, errorHandler )
        { this.callOperation("getUserCategoryRefList", null, responseHandler, errorHandler); },


    /**
     * Saves or updates a category within an org.  The category must 
     * have a valid org unit in which to associate the category.
     */
    newCategory: function ( title, type, responseHandler, errorHandler )
        { this.callOperation("newCategory", {title:title, type:type}, responseHandler, errorHandler); },

    /**
     * Saves or updates a category within an org.  The category must 
     * have a valid org unit in which to associate the category.
     */
    saveCategory: function ( category, responseHandler, errorHandler )
        { this.callOperation("updateCategory", {category:category}, responseHandler, errorHandler); },
		
	/**
	 * 
	**/
    saveTargetLine: function (categoryId, surveyId, enableTargetLine, responseHandler, errorHandler )
    { this.callOperation("saveTargetLine", {categoryId:categoryId, surveyId:surveyId, enableTargetLine:enableTargetLine}, responseHandler, errorHandler); },

	/**
	 * 
	**/
    getSequencedCategory: function (categoryId, surveyId, responseHandler, errorHandler )
    { this.callOperation("getSequencedCategory", {categoryId:categoryId, surveyId:surveyId}, responseHandler, errorHandler); },

    /**
     * gets a category record.
     */
    getCategory: function ( catid, responseHandler, errorHandler )
        { this.callOperation("getCategory", {catid:catid}, responseHandler, errorHandler); },

    /**
     * Deletes the category from the orgunit
     */
    deleteCategory: function ( catid, responseHandler, errorHandler )
        { this.callOperation("deleteCategory", {catid:catid}, responseHandler, errorHandler); },

    /**
     *  Gets a blank user record within the org specified
     */
    newUser: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("newUser", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     *  Saves a user record.  The user being saved must be with in
     *  the same org unit or below
     */
    saveUser: function ( user, responseHandler, errorHandler )
        { this.callOperation("saveUser", {user:user}, responseHandler, errorHandler); },

    /**
     *  Gets a user record.  The user being retrieved must be in the
     *  same org unit or below
     */
    getUser: function ( userid, responseHandler, errorHandler )
        { this.callOperation("getUser", {userid:userid}, responseHandler, errorHandler); },

    /**
     *  Deletes a user record.  The user being retrieved must be in
     *  the same org unit or below
     */
    deleteUser: function ( userid, responseHandler, errorHandler )
        { this.callOperation("deleteUser", {userid:userid}, responseHandler, errorHandler); },

    /**
     * Returns all users from the active users' org and suborgs
     */
    getUserTree: function ( responseHandler, errorHandler )
        { this.callOperation("getUserTree", null, responseHandler, errorHandler); },

    /**
     *  Gets the Analtyic Records for the current month for a given
     *  survey.
     */
    getSurveyAnalytics: function ( surveyid, responseHandler, errorHandler )
        { this.callOperation("getSurveyAnalytics", {surveyid:surveyid}, responseHandler, errorHandler); },
    /**
     *  Gets the Analtyic Records for the current month for a given
     *  survey.
     */
    getOrgUnitAnalytics: function ( orgid, responseHandler, errorHandler )
        { this.callOperation("getOrgUnitAnalytics", {orgid:orgid}, responseHandler, errorHandler); },

    /**
     *  Gets the Analtyic Record for the frequency of values of the
     *  category
     */
    getFrequencySets: function (etype, eid, catid, sets, responseHandler, errorHandler )
        { this.callOperation("getFrequencySets", {etype:etype,eid:eid,catid:catid,sets:sets}, responseHandler, errorHandler); },
 
    /**
     *  Gets the Analtyic Record for the frequency of values of the
     *  category
     */
    eraseOrg: function (orgid, responseHandler, errorHandler )
        { this.callOperation("eraseOrg", {orgid:orgid}, responseHandler, errorHandler); },

	/**
	 * Get existing Alert following survey and category
	 * @param {Object} surveyId
	 * @param {Object} catId
	 */
	findAlert: function(etype, surveyId, catId, responseHandler, errorHandler) {
		this.callOperation("findAlert", {etype:etype, surveyId:surveyId, catId:catId}, responseHandler, errorHandler); 
	},
	

	/**
	 * Create or update an alert
	 * @param {Object} etype
	 * @param {Object} eId
	 */
	updateAlert: function(etype, alertObj, responseHandler, errorHandler) {
		this.callOperation("updateAlert", {etype:etype, alertObj:alertObj}, responseHandler, errorHandler);
	},

	/**
	 * Delete Alert.
	 * @param {Object} alertId
	 * @param {Object} responseHandler
	 * @param {Object} errorHandler
	 */
	deleteAlert: function(alertId, responseHandler, errorHandler) {
		this.callOperation("deleteAlert", {alertId:alertId}, responseHandler, errorHandler);
	},

	/**
	 * Find Recipient by Alert
	 * @param {Object} alertId
	 * @param {Object} responseHandler
	 * @param {Object} errorHandler
	 */
	findRecipient: function(alertId, type, orgId, responseHandler, errorHandler) {
		this.callOperation("findRecipient", {alertId:alertId, type:type, orgId:orgId}, responseHandler, errorHandler);
	},
	
	/**
	 * Add or Update Recipient
	 * @param {Object} recipient
	 * @param {Object} responseHandler
	 * @param {Object} errorHandler
	 */
	updateRecipient:function(recipient, responseHandler, errorHandler) {
		this.callOperation("updateRecipient", {recipient:recipient}, responseHandler, errorHandler);
	},
	
	/**
	 * Delete a recipient.
	 * @param {Object} recipientId
	 * @param {Object} responseHandler
	 * @param {Object} errorHandler
	 */
	deleteRecipient:function(recipientId, responseHandler, errorHandler) {
		this.callOperation("deleteRecipient", {recipientId:recipientId}, responseHandler, errorHandler);
	},
	
	getSurveyAnalyticsByPeriod:function(questionid, surveyid, orgid, period, startDate, endDate, responseHandler, errorHandler) {
		this.callOperation("getSurveyAnalyticsByPeriod", {questionid:questionid, surveyid:surveyid, orgid:orgid, period:period, startDate:startDate, endDate:endDate}, responseHandler, errorHandler);
	},

	getKPIAnalytics:function(surveyid, orgid, period, responseHandler, errorHandler) {
		this.callOperation("getKPIAnalytics", {surveyid:surveyid, orgid:orgid, period:period}, responseHandler, errorHandler);
	},

	findResponses:function(eId, type) {
		this.callOperation("findResponses", {eId:eId, type:type, startDate:startDate, endDate:endDate}, responseHandler, errorHandler);
	},

    /**
     *   Poll the system for changes in a survey
     */
    pollForAnalyticsChange: function ( type, id, lastUpdated, responseHandler, errorHandler )
        { this.callOperation("pollForAnalyticsChange", {type:type, id:id, lastUpdated:lastUpdated}, responseHandler, errorHandler); },

    getFetchResponsesDS: function ( fields, etype, eid, catid )
        {
        return this.getFetchDS("fetchResponses", {etype:etype, eid:eid, catid:catid}, "responseDTO", fields);
        }
    });

