var DEBUG=false;function hasClass(c,a){if(!c){return false}return c.className.match(new RegExp("(\\s|^)"+a+"(\\s|$)"))}function addClass(c,a){if(!c){return false}if(!hasClass(c,a)){c.className+=" "+a}}function trackBack(d,c){var a=1;while(a<100000000000){if((d-(a*c))*raisePercentage==a){alert(a);return}a++}alert("not found...?")}function removeClass(d,a){if(hasClass(d,a)){var c=new RegExp("(\\s|^)"+a+"(\\s|$)");d.className=d.className.replace(c," ")}}function clone(d){if(d===null||typeof(d)!="object"){return d}var a=new d.constructor();for(var c in d){if(d.hasOwnProperty(c)){a[c]=clone(d[c])}}return a}if(!window.getElementsByClassName){var getElementsByClassName=function(l,k,c){if(!k){k=document.getElementsByTagName("body")[0]}var d,h,g,f,e;d=[];h=new RegExp("\\b"+l+"\\b");g=k.getElementsByTagName(c?c:"*");for(f=0,e=g.length;f<e;f++){if(h.test(g[f].className)){d.push(g[f])}}return d}}var lowestDefense=null;var lowestIncome=null;var totalIncome=0;var totalAttack=0;var buildingsDefense=0;var unitsDefense=0;var allInOneValues=null;var allInOneRemain=0;var maxAttack=0;var unitsUpkeep=0;var buildingsUpkeep=0;var loadingTimer=null;var userLevel=0;function makeRequest(a,j,d,c){if(d){d.innerHTML='<center><img src="/images/loading.gif" alt="Loading" />&nbsp;Loading</center>'}var h=false;if(window.XMLHttpRequest){h=new XMLHttpRequest();if(h.overrideMimeType){h.overrideMimeType("text/html")}}else{if(window.ActiveXObject){try{h=new ActiveXObject("Msxml2.XMLHTTP")}catch(g){try{h=new ActiveXObject("Microsoft.XMLHTTP")}catch(f){}}}}if(!h){alert("Failed to save data: Cannot create XMLHTTP instance");return false}h.onreadystatechange=function(){if(h.readyState===4){if(h.status===200){if(d){d.innerHTML=h.responseText}if(c){c()}if(loadingTimer!==null){clearTimeout(loadingTimer)}loadingTimer=setTimeout(function(){document.getElementById("saveResult").innerHTML=""},5000)}else{if(d){d.innerHTML="Failed to load data, error code "+h.status}}}};if(j){h.open("POST",a,true);h.setRequestHeader("Content-type","application/x-www-form-urlencoded");h.setRequestHeader("Content-length",j.length);h.setRequestHeader("Connection","close");h.send(j)}else{a+=(a.indexOf("?")===-1?"?":"&")+"time="+(new Date().getTime());h.open("GET",a,true);h.send(null);try{if(window.pageTracker){pageTracker._trackPageview(a)}}catch(g){}}}function formatCurrency(a,d){a=a.toString().replace(/\$|\,/g,"");if(isNaN(a)){a="0"}sign=(a==(a=Math.abs(a)));a=Math.floor(a*100+0.50000000001);cents=a%100;a=Math.floor(a/100).toString();if(cents<10){cents="0"+cents}for(var c=0;c<Math.floor((a.length-(1+c))/3);c++){a=a.substring(0,a.length-(4*c+3))+","+a.substring(a.length-(4*c+3))}res=(((sign)?"":"-")+"$"+a);if(typeof d!="undefined"&&d){return res}len=res.length;if(res.length>12&&res.substring(len-12,len)==",000,000,000"){res=res.substring(0,len-12)+" bil"}else{if(res.length>8&&res.substring(len-8,len)==",000,000"){res=res.substring(0,len-8)+" mil"}else{if(res.length>4&&res.substring(len-4,len)==",000"){res=res.substring(0,len-4)+" k"}}}return res}function createCookie(d,e,f){var a="";if(f){var c=new Date();c.setTime(c.getTime()+(f*24*60*60*1000));a="; expires="+c.toGMTString()}document.cookie=d+"="+e+a+"; path=/"}function readCookie(d){var f=d+"=";var a=document.cookie.split(";");for(var e=0;e<a.length;e++){var g=a[e];while(g.charAt(0)===" "){g=g.substring(1,g.length)}if(g.indexOf(f)===0){return g.substring(f.length,g.length)}}return null}function eraseCookie(a){createCookie(a,"",-1)}function levelUpIndication(){var d=document.getElementById("rankpercentage");if(d){var a=parseInt(d.value,10);var c=(totalIncome-(unitsUpkeep+buildingsUpkeep))/(totalIncome/100);document.getElementById("net-income-percentage").innerHTML=Math.round(c*100)/100+"%";elem=document.getElementById("level-up-indicator");elem.innerHTML=(c>a?"Yes":"No");elem.style.color=(c>a?"green":"red")}}function save(){var k=document.getElementById("saveResult");var c=[];var d=[];var j=[];var a=[];var h=0;for(i=0;i<properties.length;i++){if(properties[i].num===null){properties[i].num=0}c[c.length]=properties[i].id+"="+properties[i].num;d[d.length]=properties[i].num}if(units){levelUpIndication();for(i=0;i<units.length;i++){if(units[i].num===null){units[i].num=0}a[a.length]=units[i].id+"="+units[i].num;j[j.length]=parseInt(units[i].num,10)}}var g=document.getElementById("friends");if(g){h=parseInt(g.value,10);if(h===0){addClass(g,"warning")}else{removeClass(g,"warning")}}var f=document.getElementById("synchronized");if(typeof cookieName!="undefined"&&cookieName){createCookie(cookieName+"-data",d.join(","),60);if(units){createCookie(cookieName+"-units",j.join(","),60)}createCookie(cookieName+"-level",userLevel,60);if(k){k.innerHTML="OK: Cookie has been saved ";setTimeout(function(){document.getElementById("saveResult").innerHTML=""},5000)}}else{var e=document.getElementById("code");makeRequest("/"+path+"/save/","data="+escape(encodeURI(c.join(",")))+"&level="+escape(encodeURI(userLevel))+"&units="+escape(encodeURI((units?a.join(","):"0")))+"&sync="+escape(encodeURI((f&&f.checked?"1":"0")))+"&code="+escape(encodeURI((e?e.value:"")))+"&friends="+escape(encodeURI(h)),document.getElementById("saveResult"))}}function saveCode(){var a=document.getElementById("saveResult");a.innerHTML="Saving code...";makeRequest("/"+path+"/saveCode/"+escape(encodeURI(document.getElementById("code").value))+"?ajax=1",null,document.getElementById("saveResult"))}function toggleItem(c,a,d){makeRequest(c.href+"?ajax=true",null,null,function(){var e=document.getElementById(a);if(e){if(d=="hide"){addClass(e,"hidden")}else{if(d=="unhide"){removeClass(e,"hidden")}}level()}});return false}function updatePropertyData(g,c,k){if(typeof c=="undefined"||c===null){field=document.getElementById("property-"+g.id);if(field){g.num=parseInt(field.value,10)}}else{g.num=c}g.num=parseInt(g.num,10);if(g.max!==null&&g.max!==""&&g.num>g.max){g.num=parseInt(g.max,10);field=document.getElementById("property-"+g.id);if(field){field.value=g.num}}g.price=g.start+(g.num*(g.start*raisePercentage));var j;if(!k){j=document.getElementById("row-"+g.id);var e=getElementsByClassName("price",j,"span");var f=getElementsByClassName("value",e[0],"span");if(f[0]){f[0].innerHTML=formatCurrency(g.price)}}var d=g.price;var h=g.income;if(g.based!==null){d+=g.basedCount*properties[g.basedIndex].price}if(g.based2!==null){d+=g.based2Count*properties[g.based2Index].price}if(g.based3!==null){d+=g.based3Count*properties[g.based3Index].price}if(g.based4!==null){d+=g.based4Count*properties[g.based4Index].price}if(g.based5!==null){d+=g.based5Count*properties[g.based5Index].price}g.pph=(d/(h?h:(g.defense?g.defense:1)));if(!k){var a=getElementsByClassName("pph",j,"span");if(a[0]){pphValue=getElementsByClassName("value",a[0],"span");if(pphValue[0]){pphValue[0].innerHTML=formatCurrency(g.pph)}}}return g}function allInOne(){var j=document.getElementById("amount");var c=document.getElementById("copy");var h=(document.getElementById("invest_defense")&&document.getElementById("invest_defense").checked);if(j&&c){c.value="Loading....";var d=j.value.replace(/[^0-9]/g,"");var k=d;var a=clone(properties);if(DEBUG){for(i=0;i<properties.length;i++){if(properties[i].visible){console.debug(properties[i].name+" = "+properties[i].num)}}}maxLoops=0;var f=[];var o=0;var m=0;var l=false;while(!l&&maxLoops++<2500){if(DEBUG){console.debug("----")}var e=null;var g=null;for(i=0;i<a.length;i++){if(a[i].visible&&!(a[i].max!==null&&a[i].num>=a[i].max)){if(a[i].defense&&(g===null||a[i].pph<g)){g=a[i].pph}if(a[i].income&&(e===null||a[i].pph<e)){e=a[i].pph}}}if(DEBUG){console.debug(h?"Lowest defense pph: "+g:"Lowest income pph: "+e)}for(i=0;i<a.length;i++){if(!a[i].visible||(a[i].max!==null&&a[i].num>=a[i].max)){continue}if((h&&a[i].defense&&a[i].pph==g)||(!h&&a[i].income&&a[i].pph==e)){if(DEBUG){console.debug("Try buying item "+a[i].name+" (num: "+a[i].num+", pph: "+a[i].pph+")")}if(a[i].price<=d&&d>0){if(a[i].based!==null){baseObj=a[a[i].basedIndex];while(baseObj.num<a[i].basedCount&&baseObj.price<=d&&d>0){if(DEBUG){console.debug("(Buying based1 item "+baseObj.name+" for "+baseObj.price+" (leftover = "+(d-baseObj.price)+")")}d-=baseObj.price;a[a[i].basedIndex]=updatePropertyData(baseObj,baseObj.num+1,true);f.push(a[i].basedIndex)}}if(a[i].based2!==null){baseObj=a[a[i].based2Index];while(baseObj.num<a[i].based2Count&&baseObj.price<=d&&d>0){if(DEBUG){console.debug("(Buying based2 item "+baseObj.name+" for "+baseObj.price+" (leftover = "+(d-baseObj.price)+")")}d-=baseObj.price;a[a[i].based2Index]=updatePropertyData(baseObj,baseObj.num+1,true);f.push(a[i].based2Index)}}if(a[i].based3!==null){baseObj=a[a[i].based3Index];while(baseObj.num<a[i].based3Count&&baseObj.price<=d&&d>0){if(DEBUG){console.debug("(Buying based3 item "+baseObj.name+" for "+baseObj.price+" (leftover = "+(d-baseObj.price)+")")}d-=baseObj.price;a[a[i].based3Index]=updatePropertyData(baseObj,baseObj.num+1,true);f.push(a[i].based3Index)}}if(a[i].based4!==null){baseObj=a[a[i].based4Index];while(baseObj.num<a[i].based4Count&&baseObj.price<=d&&d>0){if(DEBUG){console.debug("(Buying based4 item "+baseObj.name+" for "+baseObj.price+" (leftover = "+(d-baseObj.price)+")")}d-=baseObj.price;a[a[i].based4Index]=updatePropertyData(baseObj,baseObj.num+1,true);f.push(a[i].based4Index)}}if(a[i].based5!==null){baseObj=a[a[i].based5Index];while(baseObj.num<a[i].based5Count&&baseObj.price<=d&&d>0){if(DEBUG){console.debug("(Buying based3 item "+baseObj.name+" for "+baseObj.price+" (leftover = "+(d-baseObj.price)+")")}d-=baseObj.price;a[a[i].based5Index]=updatePropertyData(baseObj,baseObj.num+1,true);f.push(a[i].based5Index)}}if(d>0&&a[i].price<=d){if(DEBUG){console.debug("Buy 1 "+a[i].name+", costs "+formatCurrency(a[i].price)+", money to spend: "+formatCurrency(d))}if(a[i].based!==null){a[a[i].basedIndex]=updatePropertyData(a[a[i].basedIndex],a[a[i].basedIndex].num-a[i].basedCount,true)}if(a[i].based2!==null){a[a[i].based2Index]=updatePropertyData(a[a[i].based2Index],a[a[i].based2Index].num-a[i].based2Count,true)}if(a[i].based3!==null){a[a[i].based3Index]=updatePropertyData(a[a[i].based3Index],a[a[i].based3Index].num-a[i].based3Count,true)}if(a[i].based4!==null){a[a[i].based4Index]=updatePropertyData(a[a[i].based4Index],a[a[i].based4Index].num-a[i].based4Count,true)}if(a[i].based5!==null){a[a[i].based5Index]=updatePropertyData(a[a[i].based5Index],a[a[i].based5Index].num-a[i].based5Count,true)}if(h){m+=a[i].defense}else{o+=a[i].income}d-=a[i].price;f.push(i);a[i]=updatePropertyData(a[i],a[i].num+1,true);if(DEBUG){console.debug("Leftover: "+formatCurrency(d)+", new values or "+a[i].name+": pph ="+a[i].pph+", price = "+formatCurrency(a[i].price)+", owned= "+a[i].num)}}}else{if(DEBUG){console.debug("This item ("+a[i].name+" costs "+formatCurrency(a[i].price)+", and there is only "+formatCurrency(d)+" left, so STOP!")}l=true}break}}}if(maxLoops==2501){alert("NOTE: The calulation has been limited to 2500 properties.")}c.value="";if(f.length===0){c.value="Nothing to buy!";return}allInOneValues=[];allInOneRemain=d;for(i=0;i<properties.length;i++){var n=0;for(x=0;x<f.length;x++){if(f[x]==i){n++}}if(n>0){c.value+=properties[i].name+": "+n+"\n";allInOneValues[allInOneValues.length]={id:properties[i].id,value:(a[i].num)}}}if(h){c.value+="--------\nNew total defense points: "+(buildingsDefense+m);c.value+="\n("+(m)+" extra defense points)"}else{c.value+="--------\nNew total income: "+formatCurrency(totalIncome+o);c.value+="\nAdditional income: "+formatCurrency(o)}c.value+="\nPrice of all above: "+formatCurrency(k-d);return}}function sortByTotalPointsDesc(d,c){if((d.attack+d.defense)>(c.attack+c.defense)){return -1}else{if((d.attack+d.defense)<(c.attack+c.defense)){return 1}else{if(d.attack>c.attack){return -1}else{if(d.attack<c.attack){return 1}else{if(d.defense>c.defense){return -1}else{if(d.defense<c.defense){return 1}else{return 0}}}}}}}function sortByAttackDesc(d,c){if(d.attack>c.attack){return -1}else{if(d.attack<c.attack){return 1}else{if(d.price<c.price){return -1}else{if(d.price>c.price){return 1}else{return 0}}}}}function sortByDefenseDesc(d,c){if(d.defense>c.defense){return -1}else{if(d.defense<c.defense){return 1}else{if(d.price<c.price){return -1}else{if(d.price>c.price){return 1}else{return 0}}}}}function showHideDefenseNote(){if(hasPropertiesDef){var c=unitsDefense+buildingsDefense;var a=document.getElementById("investDefenseNote");if(typeof a!="undefined"&&a!==null){if(c<maxAttack){document.getElementById("max_attack").innerHTML=maxAttack;document.getElementById("current_defense").innerHTML=c;a.style.display=(a.nodeName=="UL"?"list-item":"block")}else{a.style.display="none"}}}}function UnitsObject(e,c,a,d){this.id=e;this.name=c;this.num=a;this.upkeep=d}function calculateUnits(v){if(!units){return}var J=[];var G=[];var C=[];var f=[];unitsDefense=0;unitsUpkeep=0;totalAttack=0;maxAttack=0;maxDefense=0;maxTotal=0;var h=0;var g=[];var M;var s=parseInt(document.getElementById("friends_required").innerHTML,10);var E=document.getElementById("friends")?parseInt(document.getElementById("friends").value,10):s;var o=(E>s?s:E)*unitsPerFriend;var k=false;if(document.getElementById("units")){document.getElementById("units").innerHTML=(E>s?s:E)*(!catTypes?unitsPerFriend:(numTypes*unitsPerFriend))}for(i=units.length-1;i>=0;i--){if(units[i].level==-5){field=document.getElementById("unit-"+units[i].id);if(field){units[i].num=parseInt(field.value,10)}if(units[i].num===null||isNaN(units[i].num)){units[i].num=0}units[i].num=parseInt(units[i].num,10);if(units[i].num>0){k=true}break}}for(i=units.length-1;i>=0;i--){var u=catTypes?units[i].type:0;field=document.getElementById("unit-"+units[i].id);if(field){units[i].num=parseInt(field.value,10)}if(units[i].num===null||isNaN(units[i].num)){units[i].num=0}units[i].num=parseInt(units[i].num,10);if(units[i].visible){if(units[i].upkeep!==null&&units[i].upkeep>0){var n=document.getElementById("unit-row-"+units[i].id);if(n){var w=getElementsByClassName("upkeep",n,"span");if(w){var F=getElementsByClassName("value",w[0],"span");if(F[0]){F[0].innerHTML=formatCurrency((k?(units[i].upkeep*0.95):units[i].upkeep))}}}}h+=units[i].num;if(typeof g[units[i].type]=="undefined"){g[units[i].type]=units[i].num}else{g[units[i].type]+=units[i].num}if(units[i].num>0){index=units[i].part?units[i].id:u;if(typeof f[index]==="undefined"){f[index]=[]}f[index][f[index].length]=units[i];if(units[i].upkeep!==null){unitsUpkeep+=(k?(units[i].upkeep*0.95):units[i].upkeep)*units[i].num}}if(units[i].part&&units[i].price!=-1){if(DEBUG){console.debug("maxAttack: Car part "+units[i].name+": max attack "+s+" x "+units[i].attack+" = "+(s*units[i].attack*unitsPerFriend));console.debug("maxDefense: Car part "+units[i].name+": max defense "+s+" x "+units[i].defense+" = "+(s*units[i].defense*unitsPerFriend))}maxAttack+=(E<s?E:s)*units[i].attack*unitsPerFriend;maxDefense+=(E<s?E:s)*units[i].defense*unitsPerFriend;maxTotal+=(E<s?E:s)*(units[i].attack+units[i].defense)*unitsPerFriend;continue}ba=J[u];if(units[i].price!=-1&&(ba===null||typeof ba=="undefined"||units[i].attack>units[ba].attack||(units[i].attack==units[ba].attack&&units[i].price<units[ba].price))){J[u]=i}bd=G[u];if(units[i].price!=-1&&(bd===null||typeof bd=="undefined"||units[i].defense>units[bd].defense||(units[i].defense==units[bd].defense&&units[i].price<units[bd].price))){G[u]=i}bt=C[u];if(units[i].price!=-1&&(bt===null||typeof bt=="undefined"||(units[i].defense+units[i].attack)>(units[bt].defense+units[bt].attack)||((units[i].defense+units[i].attack)==(units[bt].defense+units[bt].attack)&&units[i].price<units[bt].price))){C[u]=i}}var n=document.getElementById("unit-row-"+units[i].id);removeClass(n,"highlight-green");removeClass(n,"highlight-blue");removeClass(n,"highlight-both")}var I=4;if(types&&types.length>0){I=types.length}for(i=1;i<=I;i++){if(typeof g[i]=="undefined"){g[i]=0}M=document.getElementById("subtab-"+i+"-button");if(M){M.innerHTML=M.title+" ("+g[i]+")"}}M=document.getElementById("tab-2-button");if(M){M.innerHTML=M.title+" ("+h+")"}var r=[];var m=[];if(DEBUG){console.debug("We can use "+o+" items per 'tab'");console.debug("We can use "+o+" items per 'tab'");if(!catTypes){console.debug("(We treat all the tabs as one)")}if(combinePoints){console.debug("---- POINTS COMBINED -----")}else{console.debug("---- ATTACK -----")}}var B;var c;var a=false;for(var A in f){if(f.hasOwnProperty(A)){B=0;f[A].sort(combinePoints?sortByTotalPointsDesc:sortByAttackDesc);for(i=0;i<f[A].length;i++){if(f[A][i].id==930){a=true;continue}if(B>=o){if(true){if(DEBUG){console.debug("tab"+A+": We should SELL "+f[A][i].num+" of "+f[A][i].name)}r[r.length]=new UnitsObject(f[A][i].id,f[A][i].name,f[A][i].num,f[A][i].upkeep)}}else{if((B+f[A][i].num)>o){var L=(o-B);if(DEBUG){console.debug("tab"+A+": We use "+L+" x "+f[A][i].name+"  ("+(L*(combinePoints?(f[A][i].attack+f[A][i].defense):f[A][i].attack))+" points)")}c=f[A][i].num-L;if(c>0&&!f[A][i].losable){if(DEBUG){console.debug("tab"+A+": We should SELL "+c+" of "+f[A][i].name)}r[r.length]=new UnitsObject(f[A][i].id,f[A][i].name,c,f[A][i].upkeep)}m[m.length]=new UnitsObject(f[A][i].id,f[A][i].name,L,f[A][i].upkeep);totalAttack+=L*f[A][i].attack;if(combinePoints){unitsDefense+=L*f[A][i].defense}B=o}else{if(DEBUG){console.debug("tab"+A+": We use "+f[A][i].num+" x "+f[A][i].name+"  ("+(f[A][i].num*(combinePoints?(f[A][i].attack+f[A][i].defense):f[A][i].attack))+" points)")}totalAttack+=f[A][i].num*f[A][i].attack;if(combinePoints){unitsDefense+=f[A][i].num*f[A][i].defense}B+=f[A][i].num;m[m.length]=new UnitsObject(f[A][i].id,f[A][i].name,f[A][i].num,f[A][i].upkeep)}}}}}if(!combinePoints){if(DEBUG){console.debug("Total units attack strength: "+totalAttack);console.debug("---- DEFENSE -----")}for(var d in f){if(f.hasOwnProperty(d)){B=0;f[d].sort(sortByDefenseDesc);for(i=0;i<f[d].length;i++){if(f[d][i].id==930){a=true;continue}if(B>=o){if(true){var D=false;for(x=0;x<r.length;x++){if(r[x]!==null&&r[x].id==f[d][i].id){D=true}}if(!D){for(x=0;x<m.length;x++){if(f[d][i].id==m[x].id){D=true;continue}}}if(!D){if(DEBUG){console.debug("tab"+d+": We should SELL "+f[d][i].num+" of "+f[d][i].name)}r[r.length]=new UnitsObject(f[d][i].id,f[d][i].name,f[d][i].num,f[d][i].upkeep)}}}else{if((B+f[d][i].num)>o){if(DEBUG){console.debug("tab"+d+": We use "+(o-B)+" x "+f[d][i].name+" for defending")}var K=false;for(x=0;x<m.length;x++){if(m[x].id==f[d][i].id){if(m[x].num>=(o-B)){if(DEBUG){console.debug("(We already make use of the "+f[d][i].name+" ("+((o-B)*f[d][i].defense)+" points))")}unitsDefense+=(o-B)*f[d][i].defense;B=o;K=true}else{var H=(o-B)-m[x].num;if(DEBUG){console.debug("(We already made use of "+m[x].num+" of the "+f[d][i].name+" for attacking ("+(m[x].num*f[d][i].defense)+" points))")}unitsDefense+=m[x].num*f[d][i].defense;if(DEBUG){console.debug("(We still need "+H+" extra "+f[d][i].name+" ("+(H*f[d][i].defense)+" points))")}unitsDefense+=H*f[d][i].defense;m[x].num+=H;for(z=0;z<r.length;z++){if(r[z]!==null&&r[z].id==f[d][i].id){if(r[z].num<H){if(DEBUG){console.debug("(Removed "+r[z].num+" items from sell list)")}r[z]=null}else{r[z].num-=H;if(DEBUG){console.debug("(Removed "+H+" items from sell list, still "+r[z].num+" items to sell)")}}}}B=o;K=true}}}if(!K){unitsDefense+=(o-B)*f[d][i].defense;for(x=0;x<r.length;x++){if(r[x]!==null&&r[x].id==f[d][i].id){r[x]=null}}c=f[d][i].num-(o-B);if(c>0&&!f[d][i].losable){if(DEBUG){console.debug("tab"+d+": We should SELL "+c+" of "+f[d][i].name)}r[r.length]=new UnitsObject(f[d][i].id,f[d][i].name,c,f[d][i].upkeep)}B=o}}else{if(DEBUG){console.debug("tab"+d+": We use "+f[d][i].num+" x "+f[d][i].name+" for defending ("+(f[d][i].num*f[d][i].defense)+" points)")}unitsDefense+=f[d][i].num*f[d][i].defense;B+=f[d][i].num;for(x=0;x<r.length;x++){if(r[x]!==null&&r[x].id==f[d][i].id){if(DEBUG){console.debug("(removed item from sell list)")}r[x]=null}}}}}}}}if(a){for(i=units.length-1;i>=0;i--){if(units[i].id==930){if(DEBUG){console.debug("We have "+units[i].num+" PINKY RINGS, so add an extra "+(units[i].defense*units[i].num)+" defense and "+(units[i].attack*units[i].num)+" attack points")}unitsDefense+=(units[i].defense*units[i].num);totalAttack+=(units[i].attack*units[i].num);break}}}if(DEBUG){console.debug("Total UNITS defense points: "+unitsDefense)}var j="";var q=0;for(x=0;x<r.length;x++){if(r[x]!==null){if(r[x].id==778){if(r[x].num==1){continue}else{r[x].num-=1}}else{if(r[x].id==930||r[x].id==2082){continue}}var l=(r[x].num*r[x].upkeep);q+=l;j+="<li>"+r[x].num+" x "+r[x].name+" ("+formatCurrency(l)+")</li>\n"}}var e=document.getElementById("sellNote");if(e){if(j!=""){if(q===0){j="You can make some money by selling these units, because you don't use them when attacking/defending.<br /><ul>"+j+"</ul><br /><i>Don't sell them if you use them for missions!</i>"}else{j="You can save "+formatCurrency(q)+" upkeep by selling these, because you do not use them while attacking/defending.<br/>\nBefore you sell, make sure that you have:<br />1. Have the maximum number of friends<br />2. You do not use these units in missions<br />\n<br /><ul>"+j+"</ul>"}e.style.display=(e.noteName=="UL"?"list-item":"block");document.getElementById("sellNoteContent").innerHTML=j}else{e.style.display="none"}}if(combinePoints){for(i=0;i<C.length;i++){if(typeof C[i]!="undefined"&&C[i]!==null){maxAttack+=(E<s?E:s)*units[C[i]].attack*unitsPerFriend;maxDefense+=(E<s?E:s)*units[C[i]].defense*unitsPerFriend;if(DEBUG){console.debug("maxTotal: friends ("+(E<s?E:s)+") x best total points item ("+units[C[i]].name+", "+(units[C[i]].attack+units[C[i]].defense)+" total points) x unitsPerFriend ("+unitsPerFriend+") = "+((E<s?E:s)*(units[C[i]].attack+units[C[i]].defense)*unitsPerFriend))}addClass(document.getElementById("unit-row-"+units[C[i]].id),"highlight-green")}}}else{for(i=0;i<J.length;i++){if(typeof J[i]!="undefined"&&J[i]!==null){if(typeof G[i]!="undefined"&&G[i]!==null){if(units[J[i]].attack==units[G[i]].attack){J[i]=G[i]}}maxAttack+=(E<s?E:s)*units[J[i]].attack*unitsPerFriend;if(DEBUG){console.debug("maxAttack: friends ("+(E<s?E:s)+") x best attack item ("+units[J[i]].name+", "+units[J[i]].attack+" points) x unitsPerFriend ("+unitsPerFriend+") = "+((E<s?E:s)*units[J[i]].attack*unitsPerFriend))}addClass(document.getElementById("unit-row-"+units[J[i]].id),"highlight-green")}}for(i=0;i<G.length;i++){if(typeof G[i]!="undefined"&&G[i]!==null){if(typeof J[i]!="undefined"&&J[i]!==null){if(units[J[i]].attack==units[G[i]].attack){G[i]=J[i]}}maxDefense+=(E<s?E:s)*units[G[i]].defense*unitsPerFriend;if(DEBUG){console.debug("maxDefense: friends ("+(E<s?E:s)+") x best defense item ("+units[G[i]].name+", "+units[G[i]].attack+" points) x unitsPerFriend ("+unitsPerFriend+") = "+((E<s?E:s)*units[G[i]].attack*unitsPerFriend))}if(hasClass(document.getElementById("unit-row-"+units[G[i]].id),"highlight-green")){removeClass(document.getElementById("unit-row-"+units[G[i]].id),"highlight-green");addClass(document.getElementById("unit-row-"+units[G[i]].id),"highlight-both")}else{addClass(document.getElementById("unit-row-"+units[G[i]].id),"highlight-blue")}}}}M=document.getElementById("max-level-attack");if(M){M.innerHTML=maxAttack}M=document.getElementById("max-level-defense");if(M){M.innerHTML=maxDefense}M=document.getElementById("attack");if(M){M.innerHTML=totalAttack;M.title="Your attack strength against known defense: "+(Math.round((totalAttack/maxDefense)*100))+"%"}M=document.getElementById("defense");if(M){M.innerHTML=unitsDefense+buildingsDefense;M.title="Your defense strength against max attack: "+(Math.round(((unitsDefense+buildingsDefense)/maxAttack)*100))+"%"}M=document.getElementById("units-defense");if(M){M.innerHTML=unitsDefense}var y=(unitsUpkeep+buildingsUpkeep);M=document.getElementById("upkeep");if(M){M.innerHTML=formatCurrency(y,true)}M=document.getElementById("netincome");if(M){M.innerHTML=formatCurrency(totalIncome-y,true)}var p=document.getElementById("buyDefenseUnitsNote");if(p){p.style.display=(unitsDefense<maxDefense&&hasPropertiesDef?(p.nodeName=="UL"?"list-item":"block"):"none")}showHideDefenseNote();if(!v){save()}}function hideMessage(a){theDiv=a.parentNode.parentNode.parentNode;makeRequest(a.href+"?ajax=1",null,theDiv,function(){theDiv.parentNode.removeChild(theDiv)});return false}function calculateProperties(e){buildingsUpkeep=0;var d=0;lowestIncome=null;lowestDefense=null;for(i=0;i<properties.length;i++){if(properties[i].based!==null&&properties[i].basedIndex===null){for(b=0;b<properties.length;b++){if(properties[b].id==properties[i].based){properties[i].basedIndex=b;if(properties[i].basedCount===null){properties[i].basedCount=1}}}}if(properties[i].based2!==null&&properties[i].based2Index===null){for(b=0;b<properties.length;b++){if(properties[b].id==properties[i].based2){properties[i].based2Index=b;if(properties[i].based2Count===null){properties[i].based2Count=1}}}}if(properties[i].based3!==null&&properties[i].based3Index===null){for(b=0;b<properties.length;b++){if(properties[b].id==properties[i].based3){properties[i].based3Index=b;if(properties[i].based3Count===null){properties[i].based3Count=1}}}}if(properties[i].based4!==null&&properties[i].based4Index===null){for(b=0;b<properties.length;b++){if(properties[b].id==properties[i].based4){properties[i].based4Index=b;if(properties[i].based4Count===null){properties[i].based4Count=1}}}}if(properties[i].based5!==null&&properties[i].based5Index===null){for(b=0;b<properties.length;b++){if(properties[b].id==properties[i].based5){properties[i].based5Index=b;if(properties[i].based5Count===null){properties[i].based5Count=1}}}}if(!properties[i].visible){continue}field=document.getElementById("property-"+properties[i].id);if(field){properties[i].num=parseInt(field.value,10);properties[i]=updatePropertyData(properties[i])}properties[i].num=parseInt(properties[i].num,10);if(properties[i].pph===null||properties[i].price===null){properties[i]=updatePropertyData(properties[i])}if(properties[i].max!==null&&properties[i].max!==""&&properties[i].num>=properties[i].max&&properties[i].num>0){if(properties[i].upkeep!==null){buildingsUpkeep+=properties[i].max*properties[i].upkeep}d+=properties[i].max;continue}d+=properties[i].num;if(properties[i].defense&&(lowestDefense===null||properties[i].pph<lowestDefense)){lowestDefense=properties[i].pph}if(properties[i].income&&(lowestIncome===null||properties[i].pph<lowestIncome)){lowestIncome=properties[i].pph}if(properties[i].num>0&&properties[i].upkeep!==null){buildingsUpkeep+=properties[i].num*properties[i].upkeep}}var f=document.getElementById("tab-1-button");if(f){f.innerHTML=f.title+" ("+d+")"}buildingsDefense=0;totalIncome=0;for(i=0;i<properties.length;i++){if(properties[i].defense){if(properties[i].pph==lowestDefense&&!(properties[i].max!==null&&properties[i].max!==""&&properties[i].num>=properties[i].max)){addClass(document.getElementById("row-"+properties[i].id),"highlight-blue")}else{removeClass(document.getElementById("row-"+properties[i].id),"highlight-blue")}if(properties[i].visible){buildingsDefense+=properties[i].num*properties[i].defense}}if(properties[i].income){if(properties[i].pph==lowestIncome&&!(properties[i].max!==null&&properties[i].max!==""&&properties[i].num>=properties[i].max)){if(properties[i].based!==null&&parseInt(document.getElementById("property-"+properties[i].based).value,10)===0&&properties[properties[i].basedIndex].visible){addClass(document.getElementById("row-"+properties[i].based),"highlight-green");removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}else{if(properties[i].based2!==null&&parseInt(document.getElementById("property-"+properties[i].based2).value,10)===0&&properties[properties[i].based2Index].visible){addClass(document.getElementById("row-"+properties[i].based2),"highlight-green");removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}else{if(properties[i].based3!==null&&parseInt(document.getElementById("property-"+properties[i].based3).value,10)===0&&properties[properties[i].based3Index].visible){addClass(document.getElementById("row-"+properties[i].based3),"highlight-green");removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}else{if(properties[i].based4!==null&&parseInt(document.getElementById("property-"+properties[i].based4).value,10)===0&&properties[properties[i].based4Index].visible){addClass(document.getElementById("row-"+properties[i].based4),"highlight-green");removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}else{if(properties[i].based5!==null&&parseInt(document.getElementById("property-"+properties[i].based5).value,10)===0&&properties[properties[i].based5Index].visible){addClass(document.getElementById("row-"+properties[i].based5),"highlight-green");removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}else{addClass(document.getElementById("row-"+properties[i].id),"highlight-green")}}}}}}else{removeClass(document.getElementById("row-"+properties[i].id),"highlight-green")}if(properties[i].visible){totalIncome+=properties[i].num*properties[i].income;if(DEBUG){console.debug("Income: "+properties[i].num+" x "+properties[i].income+" ("+properties[i].name+") = "+formatCurrency(properties[i].income*properties[i].num))}}}}if(DEBUG){console.debug("Total income: "+formatCurrency(totalIncome))}f=document.getElementById("defense");if(f){f.innerHTML=buildingsDefense+unitsDefense}f=document.getElementById("property-defense");if(f){f.innerHTML=buildingsDefense}document.getElementById("income").innerHTML=formatCurrency(totalIncome,true);var c=buildingsUpkeep+unitsUpkeep;f=document.getElementById("upkeep");if(f){f.innerHTML=formatCurrency(c,true)}var a=document.getElementById("netincome");if(a){a.innerHTML=formatCurrency(totalIncome-c,true)}showHideDefenseNote();if(!e){save()}}function assignAllInOne(){if(allInOneValues&&allInOneValues.length>0){for(i=0;i<allInOneValues.length;i++){document.getElementById("property-"+allInOneValues[i].id).value=allInOneValues[i].value}document.getElementById("copy").value="";document.getElementById("amount").value=allInOneRemain;allInOneValues=[];calculateProperties()}else{alert("Nothing to assign!")}}function level(e){var a=parseInt(document.getElementById("level").value,10);if(e!==null&&a+e>=1){userLevel=parseInt(a+e,10);document.getElementById("level").value=userLevel;if(cookieName){createCookie(cookieName+"-level",userLevel,60)}else{makeRequest("/"+path+"/save/","level="+escape(encodeURI(userLevel)))}}else{if(userLevel!=a){userLevel=a;if(cookieName){createCookie(cookieName+"-level",userLevel,60)}else{makeRequest("/"+path+"/save/","level="+escape(encodeURI(userLevel)))}}}if(path=="raceordie"){if(userLevel<=50){document.getElementById("friends_required").innerHTML=userLevel*10}else{document.getElementById("friends_required").innerHTML=500+((userLevel-50)*2)}}else{document.getElementById("friends_required").innerHTML=userLevel*friendsPerLevel}if(units&&document.getElementById("units")){var g=(userLevel*friendsPerLevel);var c=document.getElementById("friends")?parseInt(document.getElementById("friends").value,10):g;document.getElementById("units").innerHTML=(c>g?g:c)*(!catTypes?unitsPerFriend:(numTypes*unitsPerFriend))}var d=true;var f;if(properties){for(i=0;i<properties.length;i++){if(properties[i].level!==null){d=(properties[i].level<=userLevel)}f=document.getElementById("row-"+properties[i].id);if(f){f.style.display=(d?(f.nodeName=="LI"?"list-item":"block"):"none")}if(hasClass(f,"hidden")){properties[i].visible=false}else{properties[i].visible=d}}}if(units){d=true;for(i=0;i<units.length;i++){if(units[i].level!==null){d=(units[i].level<=userLevel)}f=document.getElementById("unit-row-"+units[i].id);if(f){f.style.display=(d?(f.nodeName=="LI"?"list-item":"block"):"none")}if(hasClass(f,"hidden")){units[i].visible=false}else{units[i].visible=d}}}calculateProperties(true);if(units){calculateUnits(true)}return false}function increase(a,e){var k=document.getElementById((e?"unit-":"property-")+a);var h=document.getElementById("synchronized");if(!e){for(i=0;i<properties.length;i++){if(properties[i].id==a){if(properties[i].max!==null&&properties[i].max!==""&&properties[i].num>=properties[i].max){alert("You can only have "+properties[i].max+" "+properties[i].name);k.value=parseInt(properties[i].max,10);calculateProperties();return false}}}}var d,g;if(!e&&h&&h.checked){for(i=0;i<properties.length;i++){if(properties[i].id==a&&(properties[i].based||properties[i].based2||properties[i].based3||properties[i].based4||properties[i].based5)){var c="";var j=[];for(x=0;x<properties.length;x++){if(properties[x].id==properties[i].based){j.push(x);d=properties[i].basedCount!==null?properties[i].basedCount:1;if(properties[x].num<d){c+=(d-properties[x].num)+" x "+properties[x].name+"\n"}}else{if(properties[i].based2!==null&&properties[x].id==properties[i].based2){j.push(x);d=properties[i].based2Count!==null?properties[i].based2Count:1;if(properties[x].num<d){c+=(d-properties[x].num)+" x "+properties[x].name+"\n"}}else{if(properties[i].based3!==null&&properties[x].id==properties[i].based3){j.push(x);d=properties[i].based3Count!==null?properties[i].based3Count:1;if(properties[x].num<d){c+=(d-properties[x].num)+" x "+properties[x].name+"\n"}}else{if(properties[i].based4!==null&&properties[x].id==properties[i].based4){j.push(x);d=properties[i].based4Count!==null?properties[i].based4Count:1;if(properties[x].num<d){c+=(d-properties[x].num)+" x "+properties[x].name+"\n"}}else{if(properties[i].based5!==null&&properties[x].id==properties[i].based5){j.push(x);d=properties[i].based5Count!==null?properties[i].based5Count:1;if(properties[x].num<d){c+=(d-properties[x].num)+" x "+properties[x].name+"\n"}}}}}}}if(!c||c==""){for(x=0;x<j.length;x++){theId=properties[j[x]].id;var f=document.getElementById("property-"+theId);if(theId==properties[i].based){g=properties[i].basedCount!==null?properties[i].basedCount:1}else{if(theId==properties[i].based2){g=properties[i].based2Count!==null?properties[i].based2Count:1}else{if(theId==properties[i].based3){g=properties[i].based3Count!==null?properties[i].based3Count:1}else{if(theId==properties[i].based4){g=properties[i].based4Count!==null?properties[i].based4Count:1}else{if(theId==properties[i].based5){g=properties[i].based5Count!==null?properties[i].based5Count:1}else{g=0}}}}}f.value=parseInt(f.value,10)-g;properties[j[x]]=updatePropertyData(properties[j[x]])}}else{alert("You have to buy these items first:\n\n"+c);return false}}}}k.value=parseInt(k.value,10)+1;if(e){calculateUnits()}else{calculateProperties()}return false}function decrease(e,c){var d=document.getElementById((c?"unit-":"property-")+e);var a=parseInt(d.value,10)-1;if(a>=0){d.value=a}if(c){calculateUnits()}else{calculateProperties()}return false}var tabLinkClick=function(d){if(!d){d=window.event}var a=this.parentNode;var c=a.childNodes;var f=this.getAttribute("href").replace("#","").replace(document.location,"");for(i=0;i<c.length;i++){if(c[i].nodeType==1&&c[i].nodeName=="DIV"){if(c[i].id==f){c[i].style.display="block";if(c[i].innerHTML.replace(/^\s+|\s+$/g,"")==""){makeRequest("/"+path+"/index?ajax="+f,null,c[i],level)}}else{c[i].style.display="none"}}if(c[i].nodeType==1&&c[i].nodeName=="A"&&hasClass(c[i],"tabbutton")){removeClass(c[i],"activetab")}}addClass(this,"activetab");if(d&&d.preventDefault){d.preventDefault()}return false};function loadTabData(a){var c=a.replace(document.location.href,"").substring(1);var f=document.getElementById(c);if(f){var d=f.parentNode;if(d){var e=d.childNodes;for(i=0;i<e.length;i++){if(e[i].nodeType==1&&e[i].nodeName=="DIV"){if(e[i].id==c){e[i].style.display="inline";if(e[i].innerHTML.replace(/^\s+|\s+$/g,"")==""){makeRequest("/"+path+"/index?ajax="+c,null,e[i],level)}}else{e[i].style.display="none"}}}}}}function tabs(e){var a=document.getElementById(e);if(!a){return}var c=a.childNodes;var d=[];var f=[];for(i=0;i<c.length;i++){if(c[i].nodeType==1&&c[i].nodeName=="DIV"){d.push(c[i]);if(d.length==1){c[i].style.display="inline";if(c[i].innerHTML.replace(/^\s+|\s+$/g,"")==""){makeRequest("/"+path+"/index?ajax="+c[i].id,null,c[i],level)}}else{c[i].style.display="none"}}if(c[i].nodeType==1&&c[i].nodeName=="A"&&hasClass(c[i],"tabbutton")){f.push(c[i]);if(f.length==1){addClass(c[i],"activetab")}}}for(i=0;i<f.length;i++){f[i].onclick=tabLinkClick}}function cookieLoad(){if(typeof properties=="undefined"||typeof cookieName=="undefined"){return}var e=readCookie(cookieName+"-data");if(!e){e=""}e=e.split(",");var a;for(var d=0;d<properties.length;d++){a=document.getElementById("property-"+properties[d].id);if(a){if(a.value==0){value=e[d];if(value!==null&&value!=""&&!isNaN(value)){a.value=parseInt(value,10)}}else{value=a.value}}}e=readCookie(cookieName+"-units");if(e&&units){e=e.split(",");for(d=0;d<units.length;d++){a=document.getElementById("unit-"+units[d].id);if(a){if(a.value===0){value=e[d];if(value!==null&&value!=""&&!isNaN(value)){a.value=parseInt(value,10)}}else{value=a.value}}}}var c=document.getElementById("level");if(c){userLevel=readCookie(cookieName+"-level");if(userLevel&&c.value=="1"){c.value=parseInt(userLevel,10)}else{userLevel=parseInt(c.value,10)}}}function init(){cookieLoad();level();levelUpIndication();tabs("subtabs");tabs("maintabs");var d=document.getElementById("synchronized");if(typeof d!="undefined"&&d!==null){d.onchange=save}if(document.getElementById("rankpercentage")){var a=null;var e=document.getElementById("rankpercentage").value;var c=new Slider(document.getElementById("slider-1"),document.getElementById("slider-input-1"));c.setValue(e);c.onchange=function(){document.getElementById("rankpercentage").value=c.getValue();levelUpIndication();if(a!==null){clearTimeout(a)}a=setTimeout(function(){makeRequest("/"+path+"/save/","rankpercentage="+escape(encodeURI(document.getElementById("rankpercentage").value)))},500)};c.setMinimum(0);c.setMaximum(100)}}function addLoadEvent(a){var c=window.onload;if(typeof window.onload!="function"){window.onload=a}else{window.onload=function(){if(c){c()}a()}}}addLoadEvent(init);