function createsettingrecord() {

    retrievesetting();
    //createsetting();

}
function displaySetting() {
    var whitespace, infromemail, intoemail, outfromemail, outtoemail,
        dateformat, caseemailfeeds, collapselinebreak, emaildelimiter, emailmarker, header, footer, excludeemailaddressesoutgoing,
        excludeemailsubjectoutgoing,fullnamefrom;

    var name = "iotapemailfeed"
    var oDataURI = GetODataServerUrl()
        + "/XRMServices/2011/OrganizationData.svc/"
        + "iotap_emailfeedsdisplaysettingSet";

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

    var retrieved = this.parent.JSON.parse(req.responseText).d;
    var length = retrieved.results.length;
    if (length == 1) {
        whitespace = retrieved.results[0].iotap_collapsewhitespacecheck;
        infromemail = retrieved.results[0].iotap_inboundemailfromcheck;
        intoemail = retrieved.results[0].iotap_inboundemailtocheck;
        outfromemail = retrieved.results[0].iotap_outboundemailfromcheck;
        outtoemail = retrieved.results[0].iotap_outboundemailtocheck;
        excludeemailaddressesoutgoing = retrieved.results[0].iotap_excludeemailaddressesoutgoing;
        excludeemailsubjectoutgoing = retrieved.results[0].iotap_excludeemailsubjectoutgoing;

        dateformat = retrieved.results[0].iotap_dateformat;

        caseemailfeeds = retrieved.results[0].iotap_caseemailfeeds;
        collapselinebreak = retrieved.results[0].iotap_collapselinebreaks;

        emaildelimiter = retrieved.results[0].iotap_emaildelimiter;
        emailmarker = retrieved.results[0].iotap_emailbottommarker;
        header = retrieved.results[0].iotap_header;
        footer = retrieved.results[0].iotap_footer;
        fullnamefrom = retrieved.results[0].iotap_fullnamefrom;

        document.getElementById("whitespace").checked = whitespace;
        document.getElementById("infromemail").checked = infromemail;
        document.getElementById("intoemail").checked = intoemail;
        document.getElementById("outfromemail").checked = outfromemail;
        document.getElementById("outtoemail").checked = outtoemail;
        if (dateformat != null) {
            document.getElementById("dateformat").value = dateformat;
        }
        document.getElementById("caseemailfeeds").checked = caseemailfeeds;
        document.getElementById("collapselinebreak").checked = collapselinebreak;
        if (emaildelimiter != null) {
            document.getElementById("emaildelimiter").value = emaildelimiter;
        }
        if (emailmarker != null) {
            document.getElementById("emailmarker").value = emailmarker;
        }
        if (header != null) {
            document.getElementById("headersetting").value = header;
        }
        if (footer != null) {
            document.getElementById("footersetting").value = footer;
        }
        if (excludeemailaddressesoutgoing != null) {
            document.getElementById("excludeemailaddressesoutgoing").value = excludeemailaddressesoutgoing;
        }
        if (excludeemailsubjectoutgoing != null) {
            document.getElementById("excludeemailsubjectoutgoing").value = excludeemailsubjectoutgoing;
        }
        document.getElementById("fullnamefrom").checked = fullnamefrom;
    }
}
function unescapeHTML(escapedHTML) {
    return escapedHTML.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "'");
}

function createsetting() {
    displaySetting();
    var header = "";
    var footer = "";
    var whitespace = document.getElementById("whitespace").checked;
    var infromemail = document.getElementById("infromemail").checked;
    var intoemail = document.getElementById("intoemail").checked;
    var outfromemail = document.getElementById("outfromemail").checked;
    var outtoemail = document.getElementById("outtoemail").checked;
    var dateformat = document.getElementById("dateformat").value;

    var caseemailfeeds = document.getElementById("caseemailfeeds").checked;
    var collapselinebreak = document.getElementById("collapselinebreak").checked;
    var emaildelimiter = document.getElementById("emaildelimiter").value;
    var emailmarker = document.getElementById("emailmarker").value;
    header = document.getElementById("headersetting").value;
    var excludeemailaddressesoutgoing = document.getElementById("excludeemailaddressesoutgoing").value;
    var excludeemailsubjectoutgoing = document.getElementById("excludeemailsubjectoutgoing").value;
    var fullnamefrom = document.getElementById("fullnamefrom").checked;

    if (header.indexOf("<") > -1 && header.indexOf(">") > -1) {
        header = unescapeHTML(header);
    }
    footer = document.getElementById("footersetting").value;
    if (footer.indexOf("<") > -1 && footer.indexOf(">") > -1) {
        footer = unescapeHTML(footer);
    }
    var ServerUrl = GetODataServerUrl();

    var Soap = " ";
    Soap += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
    Soap += '  <s:Body>';
    Soap += '    <Create xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">';
    Soap += '      <entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">';
    Soap += '        <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_name</b:key>';
    Soap += '            <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">iotapemailfeed</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '           <b:key>iotap_dateformat</b:key>';
    Soap += '            <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + dateformat + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    //Soap += '          <a:KeyValuePairOfstringanyType>';
    //Soap += '            <b:key>iotap_collapsewhitespacecheck</b:key>';
    //Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + whitespace + '</b:value>';
    //Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_inboundemailfromcheck</b:key>';
    Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + infromemail + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_inboundemailtocheck</b:key>';
    Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + intoemail + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_outboundemailfromcheck</b:key>';
    Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + outfromemail + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_outboundemailtocheck</b:key>';
    Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + outtoemail + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '            <b:key>iotap_caseemailfeeds</b:key>';
    Soap += '            <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + caseemailfeeds + '</b:value>';
    Soap += '          </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '           <b:key>iotap_collapselinebreaks</b:key>';
    Soap += '           <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + collapselinebreak + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_emaildelimiter</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + emaildelimiter + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_emailbottommarker</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + emailmarker + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_header</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + header + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_excludeemailaddressesoutgoing</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + excludeemailaddressesoutgoing + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_excludeemailsubjectoutgoing</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + excludeemailsubjectoutgoing + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '           <a:KeyValuePairOfstringanyType>';
    Soap += '               <b:key>iotap_footer</b:key>';
    Soap += '               <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + footer + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '          <a:KeyValuePairOfstringanyType>';
    Soap += '           <b:key>iotap_fullnamefrom</b:key>';
    Soap += '           <b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + fullnamefrom + '</b:value>';
    Soap += '           </a:KeyValuePairOfstringanyType>';
    Soap += '        </a:Attributes>';
    Soap += '        <a:EntityState i:nil="true" />';
    Soap += '        <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />';
    Soap += '        <a:Id>00000000-0000-0000-0000-000000000000</a:Id>';
    Soap += '        <a:LogicalName>iotap_emailfeedsdisplaysetting</a:LogicalName>';
    Soap += '        <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />';
    Soap += '      </entity>';
    Soap += '    </Create>';
    Soap += '  </s:Body>';
    Soap += '</s:Envelope>';



    var req = new XMLHttpRequest();
    req.open("POST", ServerUrl + "/XRMServices/2011/Organization.svc/web", false)

    // Responses will return XML. It isn't possible to return JSON.
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Create");
    req.send(Soap);
}

function GetODataServerUrl() {
    var sUrl = null;
    if (typeof Xrm.Page.context.getClientUrl !== 'function')
        sUrl = Xrm.Page.context.getServerUrl();
    else
        sUrl = Xrm.Page.context.getClientUrl();

    var lastChar = sUrl.charAt(sUrl.length - 1);
    if (lastChar != "/")
        sUrl += "/";

    return sUrl;

}

function retrievesetting() {

    var name = "iotapemailfeed"
    var oDataURI = GetODataServerUrl()
        + "/XRMServices/2011/OrganizationData.svc/"
        + "iotap_emailfeedsdisplaysettingSet";

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

    var retrieved = this.parent.JSON.parse(req.responseText).d;
    var length = retrieved.results.length;
    if (length > 0) {
        var name = "";
        var guid = "";

        for (var i = 0; i < length; i++) {

            guid = retrieved.results[i].iotap_emailfeedsdisplaysettingId;
            name = retrieved.results[i].iotap_name;

            var Soap1 = " ";
            Soap1 += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
            Soap1 += '  <s:Body>';
            Soap1 += '    <Delete xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">';
            Soap1 += '      <entityName>iotap_emailfeedsdisplaysetting</entityName>';
            Soap1 += '      <id>' + guid + '</id>';
            Soap1 += '    </Delete>';
            Soap1 += '  </s:Body>';
            Soap1 += '</s:Envelope>';

            var ServerUrl = GetODataServerUrl();
            var req = new XMLHttpRequest();
            req.open("POST", ServerUrl + "/XRMServices/2011/Organization.svc/web", false)

            // Responses will return XML. It isn't possible to return JSON.
            req.setRequestHeader("Accept", "application/xml, text/xml, */*");
            req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Delete");
            req.send(Soap1);
        }
        createsetting();
        alert("Setting Updated");
    } //if end
    else {
        createsetting();
        alert("Setting Created");
    }
}

