//If the SDK namespace object is not defined, create it.
if (typeof (N52) == "undefined")
{ N52 = {}; }
// Create Namespace container for functions in this library;
N52.DialogHelpers = {

GetServerUrl: function()
{
    var context, crmServerUrl;  
    if (typeof GetGlobalContext != "undefined") {  
        context = GetGlobalContext();  
    }  
    else if (typeof Xrm != "undefined") {  
        context = Xrm.Page.context;  
    }  
    else {  
        throw new Error("CRM context is not available.");  
    }  
  
 
      if (typeof context.getClientUrl != "undefined") 
      {
         crmServerUrl = context.getClientUrl();
      }
      else
      {
          crmServerUrl = context.getServerUrl();  
          crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);  
          crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any  
      }

   
    return crmServerUrl; 

},


IsUR12Plus: function()
{
   var result = false;
   var context;  
    if (typeof GetGlobalContext != "undefined") {  
        context = GetGlobalContext();  
    }  
    else if (typeof Xrm != "undefined") {  
        context = Xrm.Page.context;  
    }  
    else {  
        throw new Error("CRM context is not available.");  
    }

    if (typeof context.getClientUrl != "undefined") 
    {
         result = true;
     }

   return result ;
},



GetConfigurationId: function()

{
      ODataPath = N52.DialogHelpers.GetServerUrl() + "/XRMServices/2011/OrganizationData.svc";

     var retrieveRecordsReq = new XMLHttpRequest();
		
     retrieveRecordsReq.open('GET', ODataPath+"/north52_configurationSet",false);
     retrieveRecordsReq.setRequestHeader("Accept", "application/json");
     retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveRecordsReq.send();

     var n52config =  JSON.parse(retrieveRecordsReq.responseText).d.results;
     
     if (n52config[0] == null) {
             alert("North52 Configuration Missing");
      }
      else {
                    return n52config[0].north52_configurationId;
                  
                }
},


GetParameterByName: function (name) {

  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
  var regexS = "[\\?&]" + name + "=([^&#]*)"; 
  var regex = new RegExp(regexS); 
  var results = regex.exec(window.location.search); 
  if(results == null) 
    return ""; 
  else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
}

}