function CoreWS(srv) 
{
  this.clnKey = "xxx";
  this.srv = srv ? srv : "";

//user
  this.CreateUser = function(captcha, cid, ud, scs, err) { 
    var p = {clientKey: this.clnKey, captcha: captcha, captchaId: cid, userData: ud};
    Invoke("CreateUser", toJsonS(p), scs, err)
  }
  this.RetrievePwd = function(unm, eml, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm, email: eml};
    Invoke("RetrievePwd", toJsonS(p), scs, err)
  }
  this.Login = function(unm, pwd, scs, err) {
    var p = {clientKey: this.clnKey, unm: unm, pwd: pwd};
    Invoke("Login", toJsonS(p), scs, err)
  }
  this.Logoff = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("Logoff", toJsonS(p), scs, err)
  }
  this.GetUserData = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("GetUserData", toJsonS(p), scs, err)
  }
  this.SetUserData = function(sid, ud, upd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, userData: ud, updAttrs: upd};
    Invoke("SetUserData", toJsonS(p), scs, err)
  }
  this.GetProfileData = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetProfileData", toJsonS(p), scs, err)
  }
  this.SetProfileData = function(sid, pd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, profileData: pd};
    Invoke("SetProfileData", toJsonS(p), scs, err)
  }
  this.IsUsernameAvailable = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("IsUsernameAvailable", toJsonS(p), scs, err)
  }
  this.DeleteProfileImage = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("DeleteProfileImage", toJsonS(p), scs, err)
  }
  this.ChangePassword = function(sid, oldPwd, newPwd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, oldPwd: oldPwd, newPwd: newPwd};
    Invoke("Logoff", toJsonS(p), scs, err)
  }
//webcast  
  this.GetWebcastCounts = function(sid, unm, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, unm: unm};
    Invoke("GetWebcastCounts", toJsonS(p), scs, err)
  }
  this.GetWebcasts = function(sid, unm, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, unm: unm, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetWebcasts", toJsonS(p), scs, err)
  }
  this.GetUserPerPageWebcastCount = function(scs, err) { 
    var p = {clientKey: this.clnKey};
    Invoke("GetUserPerPageWebcastCount", toJsonS(p), scs, err)
  }
  this.GetUserPerPageWebcasts = function(se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetUserPerPageWebcasts", toJsonS(p), scs, err)
  }
  this.GetUserArchivesCount = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("GetUserArchivesCount", toJsonS(p), scs, err)
  }
  this.GetUserArchives = function(sid, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetUserArchives", toJsonS(p), scs, err)
  }
  this.GetPublicArchivesCount = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetPublicArchivesCount", toJsonS(p), scs, err)
  }
  this.GetPublicArchives = function(unm, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetPublicArchives", toJsonS(p), scs, err)
  }
  this.GetMapPublicArchivesCount = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetMapPublicArchivesCount", toJsonS(p), scs, err)
  }
  this.GetMapPublicArchives = function(unm, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetMapPublicArchives", toJsonS(p), scs, err)
  }
  this.GetPublicLiveWebcastsAll = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetPublicLiveWebcastsAll", toJsonS(p), scs, err)
  }
  this.GetPublicLiveWebcastsCount = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetPublicLiveWebcastsCount", toJsonS(p), scs, err)
  }
  this.GetPublicLiveWebcasts = function(unm, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetPublicLiveWebcasts", toJsonS(p), scs, err)
  }
  this.GetPublicWebcastsCount = function(unm, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm};
    Invoke("GetPublicWebcastsCount", toJsonS(p), scs, err)
  }
  this.GetPublicWebcasts = function(unm, se, dsc, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, unm: unm, sortExpr: se, desc: dsc, from: frm, count: cnt};
    Invoke("GetPublicWebcasts", toJsonS(p), scs, err)
  }
  this.GetPublicWebcastsMap = function(fltr, lt0, ln0, lt1, ln1, frm, cnt, scs, err) { 
    var p = {clientKey: this.clnKey, filter: fltr, lt0: lt0, ln0: ln0, lt1: lt1, ln1: ln1, from: frm, count: cnt};
    Invoke("GetPublicWebcastsMap", toJsonS(p), scs, err)
  }
  this.GetWebcast = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, id: id};
    Invoke("GetWebcast", toJsonS(p), scs, err)
  }
  this.DeleteWebcast = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, id: id};
    Invoke("DeleteWebcast", toJsonS(p), scs, err)
  }
  this.DeleteWebcasts = function(sid, csvIDs, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, csvIDs: csvIDs};
    Invoke("DeleteWebcasts", toJsonS(p), scs, err)
  }
  this.UpdateWebcastData = function(sid, wd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, webcastData: wd};
    Invoke("UpdateWebcastData", toJsonS(p), scs, err)
  }
  this.UpdateWebcastDataWithoutWaypoint = function(sid, wd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, webcastData: wd};
    Invoke("UpdateWebcastDataWithoutWaypoint", toJsonS(p), scs, err)
  }
  this.SetWebcastPublicAccess = function(sid, id, grant, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, id: id, grant: grant};
    Invoke("SetWebcastPublicAccess", toJsonS(p), scs, err)
  }
  this.SetWebcastsPublicAccess = function(sid, csvIDs, grant, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, csvIDs: csvIDs, grant: grant};
    Invoke("SetWebcastsPublicAccess", toJsonS(p), scs, err)
  }
  this.Search = function(term, attrs, frm, cnt, scs, err)
  {
    var p = {term: term, attrs: attrs, from: frm, count: cnt};
    Invoke("Search", toJsonS(p), scs, err)
  }
//contacts  
  this.GetContacts = function(sid, cgid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactGroupId: cgid};
    Invoke("GetContacts", toJsonS(p), scs, err)
  }
  this.AddEditContact = function(sid, cd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactData: cd};
    Invoke("AddEditContact", toJsonS(p), scs, err)
  }
  this.DeleteContact = function(sid, cid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactId: cid};
    Invoke("DeleteContact", toJsonS(p), scs, err)
  }
  this.GetContactGroups = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("GetContactGroups", toJsonS(p), scs, err)
  }
  this.AddEditContactGroup = function(sid, cgd, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactGroupData: cgd};
    Invoke("AddEditContactGroup", toJsonS(p), scs, err)
  }
  this.DeleteContactGroup = function(sid, cgid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactGroupId: cgid };
    Invoke("DeleteContactGroup", toJsonS(p), scs, err)
  }
  this.AddContactToGroup = function(sid, cgid, cid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactGroupId: cgid, contactId: cid};
    Invoke("AddContactToGroup", toJsonS(p), scs, err)
  }
  this.RemoveContactFromGroup = function(sid, cgid, cid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, contactGroupId: cgid, contactId: cid};
    Invoke("RemoveContactFromGroup", toJsonS(p), scs, err)
  }
//comments&ratings  
  this.AddComment = function(sid, wid, val, name, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, webcastId:wid, val: val, name: name};
    Invoke("AddComment", toJsonS(p), scs, err)
  }
  this.GetComments = function(sid, wid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, webcastId:wid};
    Invoke("GetComments", toJsonS(p), scs, err)
  }
  this.Rate = function(wid, val, scs, err) { 
    var p = {clientKey: this.clnKey, webcastId:wid, val: val};
    Invoke("Rate", toJsonS(p), scs, err)
  }
//waypoints  
  this.GetWaypoints = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("GetWaypoints", toJsonS(p), scs, err)
  }
  this.CreateWaypoint = function(sid, nm, lon, lat, dln, dlt, zm, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, name: nm, lon: lon, lat: lat, dLon: dln, dLat: dlt, zoom: zm};
    Invoke("CreateWaypoint", toJsonS(p), scs, err)
  }
  this.DeleteWaypoint = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, id: id};
    Invoke("DeleteWaypoint", toJsonS(p), scs, err)
  }
  this.SetDefaultWaypoint = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, id: id};
    Invoke("SetDefaultWaypoint", toJsonS(p), scs, err)
  }  
  this.SetWebcastsWaypoint = function(sid, lon, lat, zoom, name, csvIDs, scs, err) {
    var p = {clientKey: this.clnKey, sid: sid, lon: lon, lat: lat, zoom: zoom, name: name, csvIDs: csvIDs};
    Invoke("SetWebcastsWaypoint", toJsonS(p), scs, err)  
  }
//blogs, youtube etc  
  this.CreateBlogAccount = function(sid, prv, url, unm, pwd, auto, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, provider: prv, url: url, unm: unm, pwd: pwd, auto: auto};
    Invoke("CreateBlogAccount", toJsonS(p), scs, err)
  }
  this.DeleteBlogAccount = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, blogAccountId: id};
    Invoke("DeleteBlogAccount", toJsonS(p), scs, err)
  }
  this.GetBlogAccounts = function(sid, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid};
    Invoke("GetBlogAccounts", toJsonS(p), scs, err)
  }
  this.GetBlogs = function(sid, id, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, blogAccountId: id};
    Invoke("GetBlogs", toJsonS(p), scs, err)
  }
  this.SetAutoPosting = function(sid, prv, auto, scs, err) { 
    var p = {clientKey: this.clnKey, sid: sid, provider: prv, auto: auto};
    Invoke("SetAutoPosting", toJsonS(p), scs, err)
  }
  this.SendEmail = function(eFrm, eTo, sbj, msg, scs, err) { 
    var p = {clientKey: this.clnKey, emailFrom: eFrm, emailTo: eTo, subject: sbj, message: msg};
    Invoke("SendEmail", toJsonS(p), scs, err)
  }
  this.ShareArchivesSocialBlog = function(sid, csvWebcastIDs, csvSocialNetworksAndBlogs, scs, err) {
    var p = {clientKey: this.clnKey, sid: sid, csvWebcastIDs: csvWebcastIDs, csvSocialNetworksAndBlogs: csvSocialNetworksAndBlogs};
    Invoke("ShareArchivesSocialBlog", toJsonS(p), scs, err)  
  }
  this.ShareArchives= function(p, scs, err) {
    p.clientKey = this.clnKey
    //, sid: sid, messageEmail: messageEmail, emailSubject: emailSubject, messageSMS: messageSMS, emailAddy: emailAddy, csvRecipientIDs: csvRecipientIDs};
    Invoke("ShareArchives", toJsonS(p), scs, err)  
  }

  Invoke = function(methodName, p, onSuccess, onError)
  {
    $.ajax({
//      "url":          this.srv + "/corews/svc.asmx/" + methodName,
      "url":          "/corews/svc.asmx/" + methodName,
      "data":         p,
      "success":      onSuccess,
      "error":        onError,
      "type":         "POST",
      "contentType":  "application/json; charset=utf-8",
      "dataType":     "json",
      "timeout":      60000
    });
  }

  toJsonS = function(arg) {
    return toJsonStringArray(arg).join('');
  }

  toJsonStringArray = function(arg, out) {
    out = out || new Array();
    var u; // undefined

    switch (typeof arg) {
    case 'object':
      if (arg) {
        if (arg.constructor == Array) {
          out.push('[');
          for (var i = 0; i < arg.length; ++i) {
            if (i > 0) out.push(',\n');
            toJsonStringArray(arg[i], out);
          }
          out.push(']');
          return out;
        } else if (typeof arg.toString != 'undefined') {
          out.push('{');
          var first = true;
          for (var i in arg) {
            var curr = out.length; // Record position to allow undo when arg[i] is undefined.
            if (!first) out.push(',\n');
            toJsonStringArray(i, out);
            out.push(':');                    
            toJsonStringArray(arg[i], out);
            if (out[out.length - 1] == u) out.splice(curr, out.length - curr);
            else first = false;
          }
          out.push('}');
          return out;
        }
        return out;
      }
      out.push('null');
      return out;
    case 'unknown':
    case 'undefined':
    case 'function':
      out.push(u);
      return out;
    case 'string':
      out.push('"')
      out.push(arg.replace(/(["\\])/g, '\\$1').replace(/\r/g, '').replace(/\n/g, '\\n'));
      out.push('"');
      return out;
    default:
      out.push(String(arg));
      return out;
    }
  }
}
