/*
 * 
 * Copyright © 2012 All Rights Reserved, http://simpletrees.co.uk
 * 
 * NOTICE:  All information contained herein is, and remains the property 
 * of Simple Trees Ltd and its suppliers, if any. The intellectual and 
 * technical concepts contained herein are proprietary to Simple Trees Ltd 
 * and its suppliers and may be covered by UK, EU and Foreign Patents, 
 * patents in process, and are protected by copyright law. Dissemination of 
 * this information or reproduction of this material is strictly forbidden
 * unless prior written permission is obtained from Simple Trees Ltd. 
 *
 * This version of the product and all associated intellectual/technical
 * concepts are intended for demo purposes only and not for resale.
 *
 * Contact info@simpletrees.co.uk for license agreements or more details.
 *
 */
function getAccounts(a){tm=getQueryString("testmode","false");if(tm=="true"){testData()}else{var b=PadServerUrl(Xrm.Page.context.getServerUrl())+"XRMServices/2011/OrganizationData.svc/"+"AccountSet?$select=AccountId,Name,ParentAccountId,CustomerTypeCode,opportunity_customer_accounts/StateCode&$orderby=Name asc&$expand=opportunity_customer_accounts&$filter=StateCode/Value eq 0";if(a){b=b+" and substringof('"+a+"',Name)"}ODataCall(b,recievedAccountData)}}function recievedAccountData(a){if(!existingData){existingData=[]}var b=processAccounts(existingData,a.d.results,1);existingData=b;if(a.d.__next){ODataCall(a.d.__next,recievedAccountData)}else{createKendoTree(b)}}function processAccounts(a,b,c){if(c>10)return;var d=[];for(var e=0;e<b.length;e++){if(b[e].ParentAccountId.Id){var f=getAccount(a,b[e].ParentAccountId.Id);if(f){if(!f.items){f.items=[]}var g=createAccountItem(b[e]);f.items.push(g)}else{d.push(b[e])}}else{var h=createAccountItem(b[e]);a.push(h)}}if(d.length>0){processAccounts(a,d,c+1)}return a}function createAccountItem(a){var b={text:a.Name,accountId:a.AccountId};if(a.CustomerTypeCode.Value==8){b.noaccount=true}if(a.opportunity_customer_accounts&&a.opportunity_customer_accounts.results.length>0&&a.opportunity_customer_accounts.results[0].StateCode.Value){b.opportunity=true}return b}function getAccount(a,b){for(var c=0;c<a.length;c++){if(a[c].accountId==b){return a[c]}if(a[c].items){var d=getAccount(a[c].items,b);if(d)return d}}return null}function testData(){var a=[];a.push({text:"Item 1",opportunity:true,items:[{text:"Item 1.1",noaccount:true},{text:"Item 1.2"}]});a.push({text:"Item 2",watchlist:true});a.push({text:"Item 3",watchlist:true,noaccount:true,opportunity:true});a.push({text:"Item 4",noaccount:true,opportunity:true,watchlist:true});for(var b=1;b<100;b++){a.push({text:"Other "+b})}createKendoTree(a)}function onSelect(a){accountTree.expand(a.node)}function createKendoTree(a){accountTree=$("#treeview").kendoTreeView({theme:"blueopal",template:kendo.template($("#treeview-template").html()),dataSource:a,select:onSelect}).data("kendoTreeView");$(".k-in").dblclick(itemDblClick)}var accountTree;var tm;var existingData=null