//JS mechanisms for Account Entity
function openDialogProcess(dialogId, entityName, objectId)
{
 var url = Xrm.Page.context.getClientUrl() +
  "/cs/dialog/rundialog.aspx?DialogId=" +
  dialogId + "&EntityName=" +
  entityName + "&ObjectId=" +
  objectId;

  var x = screen.width/2 - 700/2;
    var y = screen.height/2 - 450/2;
 window.open(url,"_blank",'height=485,width=700,left='+x+',top='+y);
}

function openTerminateAccountDialog()
{
	var accId = Xrm.Page.data.entity.getId();
	var entName = Xrm.Page.data.entity.getEntityName();
	var dialogId = "2A677DF4-C2CA-4783-83B4-DF2F33342F3D";

	openDialogProcess(dialogId, entName, accId);
}

function hideSection()
{
  Xrm.Page.ui.tabs.get("SUMMARY_TAB").sections.get("Custom_Fields").setVisible(Xrm.Page.getAttribute("ocl_showmore").getValue());
}

function customFieldsVisibility()
{
  //If show more info = false, return.
  if (!Xrm.Page.getAttribute("ocl_showmore").getValue())
    return;

  var accType = Xrm.Page.getAttribute("ocl_accnttype").getValue();
  //get all controls from Custom Fields section
  var controls = getControlsFromSection("SUMMARY_TAB","Custom_Fields");
  //if no controls found, return
  if(controls.length == 0)
    return;

  //get all custom fields records for the entity
  var customFields = getCustomFieldsByEntity("account");  
  //if no Custom Fields, return
  if (!customFields)
    return;

  //cycle through the controls
  for (var i = 0, len = controls.length; i < len ; i++) 
  {
    //Ignore controls of type webresource
    if (controls[i].getControlType() != "webresource") 
    {
      var flag = false; //flag to see if control matches any of the custom fields record
      //Cycle through the Custom Fields records
      for (var j = 0, length = customFields.length; j < length; j++) 
      {
        //If any record is found for the control with the same account type, set flag to true
        if (customFields[j].attributes["ocl_accounttype"].value == accType && controls[i].getName() == "ocl_"+customFields[j].attributes["ocl_schemaname"].value.toLowerCase())
          flag = true;
      }
      //set control visibility according to flag value.
      controls[i].setVisible(flag);

    }
  }

}

function getControlsFromSection(tab,section)
{
  return Xrm.Page.ui.tabs.get(tab).sections.get(section).controls.getAll();
}

function getCustomFieldsByEntity(schemaname)
{
  debugger;
  var fetch ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' >"
+"    <entity name='ocl_customfield' >"
+"        <attribute name='ocl_customfieldid' />"
+"        <attribute name='ocl_name' />"
+"        <attribute name='ocl_schemaname' />"
+"        <attribute name='ocl_entity' />"
+"        <attribute name='ocl_accounttype' />"
+"        <filter type='and' >"
+"            <condition attribute='statecode' operator='eq' value='0' />"
+"            <condition attribute='ocl_entityname' operator='like' value='%"+schemaname+"%' />"
+"        </filter>"
+"    </entity>"
+"</fetch>";

    var retrieve = XrmServiceToolkit.Soap.Fetch(fetch);

    if(retrieve.length > 0)
    {
      return retrieve;
    }
    else 
      return null;
}