﻿function callFunctionServWebService(serviceMethod, parameters, typeReturn, funcOK, funcNOK, params) {

    $.ajax({
        async: true,
        type: "GET",
        url: "/App_WebServices/ServWebService.asmx/" + serviceMethod + "?" + parameters,
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded",
        success: function(xml) {
            if (funcOK) funcOK(callBackFunctionServWebService(xml, typeReturn), params);
            else return callBackFunctionServWebService(xml, typeReturn);
        }
         ,
        error: function(ex) {
            if (funcNOK) funcNOK(ex, params)
        }
    });
}

function callBackFunctionServWebService(xml, typeReturn) {
    return $(xml).find(typeReturn).text();
}

function GetUrlBackOK(res, varMessages) {
    $("#ctl00_HeaderArg1_SiteMapPath1").html(res);
}

function GetUrlBackKO(res, varMessages) {
    $("#ctl00_HeaderArg1_SiteMapPath1").html("");
}

var ServWebService = {

    Alert24x7: function(p_IdAdvert, p_Email, p_Frequency, p_Origin, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("Alert24x7", "p_IdAdvert=" + p_IdAdvert + "&p_Email=" + p_Email + "&p_Frequency=" + p_Frequency + "&p_Origin=" + p_Origin, "int", funcOK, funcNOK, params);
        });
    }
    ,
    AlertSearch24x7: function(p_SearchOFind, p_Email, p_Frequency, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("AlertSearch24x7", "p_SearchOFind=" + p_SearchOFind + "&p_Email=" + p_Email + "&p_Frequency=" + p_Frequency, "int", funcOK, funcNOK, params);
        });
    }
    ,
    SendContact: function(p_Email, p_Name, p_Description, p_Message, p_IdContact, p_IdAdvert, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("SendContact", "p_Email=" + p_Email + "&p_Name=" + p_Name + "&p_Description=" + p_Description + "&p_Message=" + p_Message + "&p_IdContact=" + p_IdContact + "&p_IdAdvert=" + p_IdAdvert, "int", funcOK, funcNOK, params);
        });
    }
    ,
    SendFriend: function(p_FriendEmail, p_FriendName, p_Name, p_Title, p_Price, p_IdAdvert, p_sUsUnico, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("SendFriend", "p_FriendEmail=" + p_FriendEmail + "&p_FriendName=" + p_FriendName + "&p_Name=" + p_Name + "&p_Title=" + p_Title + "&p_Price=" + p_Price + "&p_IdAdvert=" + p_IdAdvert + "&p_sUsUnico=" + p_sUsUnico, "int", funcOK, funcNOK, params);
        });
    }
    ,
    AdvertContact: function(p_Email, p_Name, p_Title, p_IdAdvert, p_sUserContact, p_sPhoneContact, p_sEmailContact, p_sText, p_sUsUnico, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("AdvertContact", "p_Email=" + p_Email + "&p_Name=" + p_Name + "&p_Title=" + p_Title + "&p_IdAdvert=" + p_IdAdvert + "&p_sUserContact=" + p_sUserContact + "&p_sPhoneContact=" + p_sPhoneContact + "&p_sEmailContact=" + p_sEmailContact + "&p_sText=" + p_sText + "&p_sUsUnico=" + p_sUsUnico, "int", funcOK, funcNOK, params);
        });
    }
    ,
    VendorContact: function(p_EmailVendor, p_EmailUser, p_NameVendor, p_Title, p_IdAdvert, p_sUsUnico, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("VendorContact", "p_EmailVendor=" + p_EmailVendor + "&p_EmailUser=" + p_EmailUser + "&p_NameVendor=" + p_NameVendor + "&p_Title=" + p_Title + "&p_IdAdvert=" + p_IdAdvert + "&p_sUsUnico=" + p_sUsUnico, "int", funcOK, funcNOK, params);
        });
    }
    ,
    ViewPhone: function(p_IdAdvert, p_sUsUnico, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("ViewPhone", "&p_IdAdvert=" + p_IdAdvert + "&p_sUsUnico=" + p_sUsUnico, "bool", funcOK, funcNOK, params);
        });
    }
    ,
    SendConversation: function(p_AdvertId, p_lUserId, p_sEmail, p_sDescripction, p_sName, p_sPhone, p_sUsUnico, funcOK, funcNOK, params) {
        $(function() {
            callFunctionServWebService("AddConversation", "p_IdAdvert=" + p_AdvertId + "&p_lUserId=" + p_lUserId + "&p_sDescription=" + p_sDescripction + "&p_sEmail=" + p_sEmail + "&p_sName=" + p_sName + "&p_sPhone=" + p_sPhone + "&p_sUsUnico=" + p_sUsUnico, "long", funcOK, funcNOK, params);
        });
    }
    ,
    GetUrlBack: function(p_AdvertId, funcOK, funcNOK, params) {
        var p_UrlReferer = document.referrer;
        $(function() {
            callFunctionServWebService("GetUrlBack", "p_IdAdvert=" + p_AdvertId + "&p_UrlReferer=" + p_UrlReferer, "string", funcOK, funcNOK, params);
        });
    }
};

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Metodos de comunicacion estadísiticas OLAP
//////////////////////////////////////////////////////////////////////////////////////////////////////

function callBack(xml, typeReturn) {
    var retval
    if ($(xml).find(typeReturn).children().length == 0) { retval = $(xml).find(typeReturn).text(); }
    else { retval = xml; }
    return retval;

}
function callWebService(serviceUrl, serviceMethod, parameters, typeReturn, funcOK, funcNOK, params) {
    $.ajax({
        async: true,
        type: "GET",
        url: serviceUrl + serviceMethod + "?" + parameters,
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded",
        success: function(xml) {
            if (funcOK) funcOK(callBack(xml, typeReturn), params);
            else return callBack(xml, typeReturn);
        }
        ,
        error: function(ex) {
            if (funcNOK) funcNOK(ex, params);
        }
    });
}

//OLAP Advert Visits
function OLAPDetailVisitsOK(res, varMessages) {
    $("#numAdvertVisits").html(res);
}

function OLAPDetailVisitsKO(res, varMessages) {
    $("#numAdvertVisits").html("0");
}

var WSStatisticsUrl = "/App_WebServices/OLAPStatistics.asmx/";
var WSStatistics = {
    Detail: function(idAdvert, strCookie, idSession) {
        callWebService(WSStatisticsUrl, "DetailAdvert", "p_IdAdvert=" + idAdvert + "&p_susUnico=" + strCookie + "&idSession=" + idSession, "boolean", null, null, "");
    },
    GetDetail: function(idAdvert, funcOK, funcKO) {
        callWebService(WSStatisticsUrl, "GetAdvertVisits", "p_IdAdvert=" + idAdvert, "int", funcOK, funcKO, "");
    }
};

