
window.CPage=function(Page){if(arguments.length<=0)return;this.Init.apply(this,arguments);}
CPage.prototype.Init=function(Page){this.Page=Page;this.Id='';this.ActionPanelNames=new Array('HelpPanel');this.ImagesPath='';this.MailDomain='';this.ZendProfile=false;this.aActions={};this.bGuiSecureMode=true;this.Events={};}
window.CField=function(Name,Value,EncodeValue){if(arguments.length<=0)return;if(arguments.length<3){this.Init(Name,Value,true);}else{this.Init(Name,Value,EncodeValue);}}
CField.prototype.Init=function(Name,Value,EncodeValue){this.Name=Name;this.Value=Value;if(typeof(EncodeValue)=='undefined')EncodeValue=true;this.EncodeValue=EncodeValue;}
htmlspecialchars=function(Source){Target=new String(Source)
Target=Target.replace(/&/g,'&amp;');Target=Target.replace(/</g,'&lt;');Target=Target.replace(/>/g,'&gt;');Target=Target.replace(/\"/g,'&quot;');Target=Target.replace(/\'/g,'&#039;');return Target;}
function MarkupParagraphs(PlainTextString){if(!PlainTextString)return"";var MarkedUpString=PlainTextString.replace(/\n\n/g,"</p><p>");if(MarkedUpString!=PlainTextString){MarkedUpString="<p>"+MarkedUpString+"</p>";}
MarkedUpString=MarkedUpString.replace(/\n/g,"<br>\n");return MarkedUpString;}
var Utf8={encode:function(string){if(!string)return'';string=string.replace(/\r\n/g,"\n");var utftext="";for(var n=0;n<string.length;n++){var c=string.charCodeAt(n);if(c<128){utftext+=String.fromCharCode(c);}
else if((c>127)&&(c<2048)){utftext+=String.fromCharCode((c>>6)|192);utftext+=String.fromCharCode((c&63)|128);}
else{utftext+=String.fromCharCode((c>>12)|224);utftext+=String.fromCharCode(((c>>6)&63)|128);utftext+=String.fromCharCode((c&63)|128);}}
return utftext;},decode:function(utftext){var string="";var i=0;var c=c1=c2=0;while(i<utftext.length){c=utftext.charCodeAt(i);if(c<128){string+=String.fromCharCode(c);i++;}
else if((c>191)&&(c<224)){c2=utftext.charCodeAt(i+1);string+=String.fromCharCode(((c&31)<<6)|(c2&63));i+=2;}
else{c2=utftext.charCodeAt(i+1);c3=utftext.charCodeAt(i+2);string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3;}}
return string;}}
CField.prototype.ToReadOnlyElement=function(){var e=document.createElement('div');e.className='inputgroup';e.innerHTML=this._ToInnerHtml();return e;}
CField.prototype._ToInnerHtml=function(){var aContent=new Array();aContent.push('<span class="inputlabel">'+htmlspecialchars(this.Name)+':</span>');aContent.push('<span class="inputcontrol">');if(this.EncodeValue){aContent.push(htmlspecialchars(this.Value));}else{aContent.push(this.Value);}
aContent.push('</span>');return aContent.join('');}
CField.prototype.ToHtml=function(Attributes){var aContents=[];aContents.push('<div class="inputgroup" ');if(Attributes){aContents.push(Attributes);}
aContents.push('>');aContents.push(this._ToInnerHtml());aContents.push('</div>');return aContents.join('');}
CField.prototype.ToInputControl=function(Attributes){if(!Attributes)Attributes='';var aContent=new Array();aContent.push('<input name="'+this.Name+'" '+Attributes+' value="');if(this.Value){if(this.EncodeValue){aContent.push(htmlspecialchars(this.Value));}else{aContent.push(this.Value);}}
aContent.push('">');return aContent.join('');}
CField.prototype.ToTextArea=function(Attributes){if(!Attributes)Attributes='';Result=['<textarea name="',this.Name,'" ',Attributes,'>',this.Value?htmlspecialchars(this.Value):'','</textarea>'].join('');return Result;}
CPage.prototype.AddEventHandler=function(EventName,ContextObject,Method){if(!this.EventHandlers)this.EventHandlers={};if(!this.EventHandlers[EventName])this.EventHandlers[EventName]=[];this.EventHandlers[EventName].push({"Context":ContextObject,"Method":Method});}
CPage.prototype.RaiseEvent=function(EventName,EventArgs){if(!this.EventHandlers)return;if(!this.EventHandlers[EventName])return;var aHandlers=this.EventHandlers[EventName];for(var iIndex=0;iIndex<aHandlers.length;iIndex++){var Handler=aHandlers[iIndex];Handler.Method.apply(Handler.Context,EventArgs);}}
CPage.prototype.RemoveEventHandler=function(EventName,ContextObject,Method){if(!this.EventHandlers)return;if(!this.EventHandlers[EventName])return;var EventHandlers=this.EventHandlers[EventName];for(var iIndex=0;iIndex<EventHandlers.length;iIndex++){if(EventHandlers[iIndex].Context!=ContextObject)continue;if(EventHandlers[iIndex].Method.toString()!=Method.toString())continue;EventHandlers.splice(iIndex,1);break;}}
CPage.prototype.ClearForm=function(Element){if(!Element)return;var oForm=(Element.form?Element.form:Element);if(!oForm)return;if(!oForm.elements)return;for(i=0;i<oForm.length;i++){var oInputControl=oForm.elements[i];if(!oInputControl)continue;if(oInputControl.type=='text')oInputControl.value='';if(oInputControl.type=='textarea')oInputControl.value='';}}
CPage.prototype.ShowPanel=function(PanelName){var oPanel=document.getElementById(PanelName);if(!oPanel)return;oPanel.style.display='';}
CPage.prototype.HidePanel=function(PanelName){var oPanel=document.getElementById(PanelName);if(!oPanel)return;oPanel.style.display='none';}
CPage.prototype.TogglePanel=function(PanelName){var oPanel=document.getElementById(PanelName);if(!oPanel)return;if(oPanel.style.display=='none'){oPanel.style.display='';}else{oPanel.style.display='none';}}
CPage.prototype.ShowActionPanel=function(PanelName){for(var iIndex in this.ActionPanelNames){if(PanelName!=this.ActionPanelNames[iIndex]){this.HidePanel(this.ActionPanelNames[iIndex]);}}
if(PanelName)this.TogglePanel(PanelName);}
CPage.prototype.ShowResultMessage=function(Result){var p=document.getElementById('ActionResult');if(!p)return;if(typeof(Result)!=='undefined'&&Result&&Result.Message){p.innerHTML=Result.Message;p.style.display='';}else{p.innerHTML='';p.style.display='none';}}
CPage.prototype.HiddenFormInputsToHtml=function(){return'';}
CPage.prototype.ActionButtonToHtml=function(aAction){var aResult=['<input name="Action" value="',htmlspecialchars(aAction.Name),'" '];if(aAction.Attributes){aResult.push(aAction.Attributes);var s=new String(aAction.Attributes);if(s.indexOf('type=')==-1){aResult.push('type="submit" ');}}else{aResult.push('type="submit" ');}
aResult.push(">");return aResult.join('');}
CPage.prototype.ActionButtonsToHtml=function(Mask){var aResult=[];for(var ActionName in this.aActions){var aValue=this.aActions[ActionName];var bPermitted=this.bGuiSecureMode;switch(aValue.Permissions){case XMLDB_PERMISSION_ENABLE:bPermitted=true;break;case XMLDB_PERMISSION_DISABLE:bPermitted=false;break;case XMLDB_PERMISSION_INHERIT:default:break;}
if(!bPermitted)continue;if(Mask){if(typeof(aValue.Style)!='undefined'){if(!(aValue.Style&Mask))continue;}}
aResult.push(this.ActionButtonToHtml(aValue));}
return aResult.join('');}
CPage.prototype.CheckboxToHtml=function(FieldName,Data,Attributes){if(!Attributes)Attributes='';var aResult=['<input type="checkbox" Name="',FieldName,'" ',Attributes];if(Data[FieldName]){if(Data[FieldName]=='1'){aResult.push(' checked');}}
aResult.push(">");return aResult.join('');}
CPage.prototype.RadioButtonToHtml=function(Id,FieldName,Option,Data,Label,Attributes){if(!Attributes)Attributes='';var aResult=['<span ',Attributes,'>','<input type="radio"',' Name="',FieldName,'"',' Value="',Option,'"',' Id="',Id,'"'];if(Data[FieldName]){if(Data[FieldName]==Option){aResult.push(' checked');}}
aResult.push('>');if(Label){aResult.push(['<label for="',Id,'">',Label,'</label>'].join(''));}
aResult.push('</span>');return aResult.join('');}
CPage.prototype.ForEachCheckbox=function(FormId,Callback){var oForm=document.forms.namedItem(FormId);if(!oForm)return;var oInputList=oForm.elements;if(!oInputList)return;for(i=0;i<oInputList.length;i++){var oInputElement=oInputList.item(i);if(!oInputElement)continue;if(oInputElement.type!='checkbox')continue;var oInputName=new String(oInputElement.name);if(oInputName.substring(0,6)!='Select')continue;Callback(oInputElement);}}
window.AjaxCallback=function(oParent,Callback){this.oParent=oParent;this.Callback=Callback;this.xmlhttp=false;if(navigator.userAgent.indexOf(" Firefox/")<0){try{this.xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e1){try{this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e2){this.xmlhttp=false;}}}
if(!this.xmlhttp&&typeof XMLHttpRequest!='undefined'){try{this.xmlhttp=new XMLHttpRequest();}catch(e){this.xmlhttp=false;}}}
AjaxCallback.prototype.Send=function(Page,Data){if(this.oParent.ZendProfile){Page+='?'+Data+'&start_debug=1&debug_port=10000&start_profile=1';Data='';}
var oCallback=this;this.xmlhttp.onreadystatechange=function(){if(oCallback.xmlhttp.readyState==4){if(oCallback.xmlhttp.status==200){oCallback.Complete();}else{oCallback.Error();}}}
this.xmlhttp.open("POST",Page);this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');this.xmlhttp.send(Data);}
AjaxCallback.prototype.Error=function(){alert("Error while processing callback");}
AjaxCallback.prototype.Complete=function(){eval('this.oParent.'+this.Callback+'(this.xmlhttp);');}
window.CAutoCompletionControl=function(Element){if(arguments.length<=0)return;this.Init.apply(this,arguments);}
CAutoCompletionControl.prototype.Init=function(Element,Options){this.Element=Element;this.Options=Options?Options:[];}
CAutoCompletionControl.prototype.OnKeyPress=function(oEvent){if(this.Element.nodeName!="INPUT"&&this.Element.nodeName!='TEXTAREA')return true;if(!this.Element.ReplaceSelection){this.Element.ReplaceSelection=function(Text){if(navigator.userAgent.indexOf("IE")==-1){var iStart=this.selectionStart;this.value=this.value.substring(0,iStart)+Text+this.value.substring(this.selectionEnd,this.value.length);this.setSelectionRange(iStart+Text.length,iStart+Text.length);}else{var oRange=document.selection.createRange();oRange.text=Text;oRange.collapse(true);oRange.select();}
this.focus();};}
if(!this.Element.GetSelection){this.Element.GetSelection=function(){if(navigator.userAgent.indexOf("IE")==-1){return{Start:this.selectionStart,Value:this.value.substring(this.selectionStart,this.selectionEnd-this.selectionStart),End:this.selectionEnd};}else{var Marker=Math.random();while(this.value.indexOf(Marker)!=-1){Marker+=Math.random();}
var OriginalText=new String(this.value);var oRange=document.selection.createRange();oRange.text=[Marker,oRange.text,Marker].join('');var aArray=this.value.split(Marker);var Result={};Result.Start=aArray[0].length;Result.Value=aArray[1];Result.End=aArray[0].length+aArray[1].length;this.value=OriginalText;this.Select(Result.Start,Result.End);return Result;}}}
if(!this.Element.Select){this.Element.Select=function(iStart,iEnd){switch(arguments.length){case 0:this.select();break;case 1:iEnd=this.value.length;case 2:if(navigator.userAgent.indexOf("IE")==-1){this.setSelectionRange(iStart,iEnd);}else{var oRange=this.createTextRange();var aLines=this.value.split('\n');var iRunner=0;iEnd+=aLines.length-1;while(iRunner<=iEnd){iRunner+=aLines.shift().length;iRunner+=1;if(iRunner<=iStart)iStart--;if(iRunner<=iEnd)iEnd--;}
oRange.moveStart("character",iStart);oRange.moveEnd("character",-(this.value.length-iEnd));oRange.select();}}
this.focus();}}
var CharCode=0;if(window.event){CharCode=window.event.keyCode;}else if(oEvent.which){CharCode=oEvent.which;}
switch(CharCode){case 0:case 38:case 40:case 37:case 39:case 33:case 34:case 36:case 35:case 9:case 27:case 16:case 17:case 18:case 20:case 8:case 46:return true;case 13:if(this.Element.nodeName=='TEXTAREA')return true;var Selection=this.Element.GetSelection();if(Selection.Start==Selection.End){return true;}else{var iLen=this.Element.value.length;this.Element.Select(iLen,iLen);return false;}
default:this.OnAutoComplete(CharCode);}
return false;}
CAutoCompletionControl.prototype.OnAutoComplete=function(CharCode){var Selection=this.Element.GetSelection();var SkipAutocompletion=(Selection.End<this.Element.value.length);this.Element.ReplaceSelection(String.fromCharCode(CharCode));if(SkipAutocompletion)return;var CurrentValue=new String(this.Element.value);var FixedContent='';if(this.Element.nodeName=='TEXTAREA'){var LastEOL=CurrentValue.lastIndexOf('\n');if(LastEOL>0){FixedContent=CurrentValue.substring(0,LastEOL+1);CurrentValue=CurrentValue.substring(LastEOL+1);}}
CurrentValue=CurrentValue.replace(/([\.\\\+\*\?\[\^\]\(\$\)])/g,"\\$1");var SearchRegExp=new RegExp("^"+CurrentValue,"i");for(var OptionIndex in this.Options){var Option=this.Options[OptionIndex];if(Option.search(SearchRegExp)!=-1){var iLen=this.Element.value.length;var aNewValue=[FixedContent,Option];this.Element.value=aNewValue.join('');this.Element.Select(iLen,this.Element.value.length);return;}}}

window.CResultAction=function(Name,DisplayName,Link,Tooltip){this.Name=Name;this.DisplayName=DisplayName;this.Link=Link;this.Tooltip=Tooltip;}
CResultAction.prototype.ToHtml=function(Parent,RecordId,Attributes,Data){var aResults=['<button',' id="',Parent.Id,this.Name,RecordId,'"',' onclick="',htmlspecialchars(eval(this.Link)),'"',' type="button"',];if(this.Tooltip){aResults.push(' title="',this.Tooltip,'"');}
if(Attributes)aResults.push(Attributes);aResults.push('>');aResults.push(htmlspecialchars(this.DisplayName));aResults.push('</button>');return aResults.join('');}
window.CResultInput=function(Name,Type,Attributes){this.Name=Name;this.Type=Type;this.Attributes=Attributes;}
CResultInput.prototype.ToHtml=function(Parent,RecordId){var aResults=['<input',' id="',Parent.Id,this.Name,RecordId,'"',' type="',this.Type,'"',' name="',htmlspecialchars(this.Name),RecordId,'"'];if(this.Attributes)aResults.push(this.Attributes);aResults.push('>');return aResults.join('');}
window.CXDbGuiEntry=function(){if(arguments.length<=0)return;this.Init.apply(this,arguments);}
CXDbGuiEntryStaticData=function(){this.ShowImage=new Image();this.ShowImage.src="/image/misc/arrow-down.gif";this.HideImage=new Image();this.HideImage.src="/image/misc/arrow-right.gif";}
s_CXDbGuiEntryStaticData=new CXDbGuiEntryStaticData();CXDbGuiEntry.prototype.Init=function(RecordId,Data,Parent){this.Parent=Parent;this.Data=Data;this.RecordId=RecordId;this.ExtendedInfo=null;this.InfoPanel=null;this.Form=null;this.LoadingPanel=null;this.prototype={};this.prototype.ToInfoPanel=null;}
CXDbGuiEntry.prototype._GetExtendedInfo=function(){if(this.ExtendedInfo)return this.ExtendedInfo;var Id='ExtendedInfo'+this.RecordId;this.ExtendedInfo=document.getElementById(Id);if(this.ExtendedInfo)return this.ExtendedInfo;this.ExtendedInfo=document.createElement('span');this.ExtendedInfo.id='ExtendedInfo'+this.RecordId;this.ExtendedInfo.className='EntryExtendedInfo';this.ExtendedInfo.style.display='none';return this.ExtendedInfo;}
CXDbGuiEntry.prototype._GetExpansionControlHtml=function(){return['<a class="ExtendedInfoIndicatorRegion EntrySummaryRegion"',' onclick="javascript:',this.Parent.Id,'.ToggleEntryExtendedInfo(',this.RecordId,')"','>','<img',' id="ExtendedInfoIndicator',this.RecordId,'"',' src="',s_CXDbGuiEntryStaticData.HideImage.src,'"',' alt="Show/Hide details"',' style="width:16;border:none">','</a>'].join('');}
CXDbGuiEntry.prototype.ToggleEntryExtendedInfo=function(){if(!this.Cancel())return;var PanelName='ExtendedInfo'+this.RecordId;var ImageName='ExtendedInfoIndicator'+this.RecordId;var oPanel=document.getElementById(PanelName);if(!oPanel)return;var oImage=document.getElementById(ImageName);if(!oImage)return;if(oPanel.style.display=='none'){oPanel.style.display='';oImage.src=s_CXDbGuiEntryStaticData.ShowImage.src;}else{oPanel.style.display='none';oImage.src=s_CXDbGuiEntryStaticData.HideImage.src;}}
CXDbGuiEntry.prototype.InsertInto=function(Table,Index){if(!Table)return;this._InsertSummaryInto(Table,Index);if(this.ToInfoPanel){var RowElementId='ExtendedInfoRow'+this.RecordId;var Row=document.getElementById(RowElementId);if(Row){Index=Row.rowIndex;var t=Row.parentNode;while(t.nodeName!='TABLE')t=t.parentNode;t.deleteRow(Row.rowIndex);}else{if(Index){Index=Index+1;}else{Index=-1;}}
var Row=Table.insertRow(Index);Row.id=RowElementId;var Cell=Row.insertCell(-1);var SummaryRow=document.getElementById('Row'+this.RecordId);Cell.colSpan=SummaryRow.cells.length;Cell.appendChild(this._GetExtendedInfo());}}
CXDbGuiEntry.prototype._InsertSummaryInto=function(Table,Index){if(!Table)return;var RowElementId='Row'+this.RecordId;var Row=document.getElementById(RowElementId);if(Row){Index=Row.rowIndex;var t=Row.parentNode;while(t.nodeName!='TABLE')t=t.parentNode;t.deleteRow(Row.rowIndex);}else{if(!Index)Index=-1}
var Row=Table.insertRow(Index);Row.id='Row'+this.RecordId;Row.className="EntrySummaryInfo";var iRowsPerEntry=(this.ToInfoPanel?2:1);if(Math.floor(Table.rows.length/iRowsPerEntry)%2){Row.style.backgroundColor="#EEEEEE";}
{var e=Row.insertCell(-1);e.className="EntrySummaryActions EntrySummaryRegion";if(this.ToInfoPanel){e.innerHTML=this._GetExpansionControlHtml();}else{if(this.Parent.HeaderRows==-1){this.Parent.HeaderRows=Row.rowIndex-1;}
e.innerHTML=Row.rowIndex-this.Parent.HeaderRows;}
for(var iIndex in this.Parent.aResultInputs){var e=Row.insertCell(-1);e.innerHTML=this.Parent.aResultInputs[iIndex].ToHtml(this.Parent,this.RecordId,'',this.Data);}
for(var iIndex in this.Parent.aResultActions){var e=Row.insertCell(-1);e.innerHTML=this.Parent.aResultActions[iIndex].ToHtml(this.Parent,this.RecordId,'',this.Data);}}
this.InsertSummaryIntoRow(Row);}
CXDbGuiEntry.prototype.InsertSummaryIntoRow=function(Row){for(var iIndex=0;iIndex<this.Parent.aDisplayColumns.length;iIndex++){var Name=this.Parent.aDisplayColumns[iIndex];var e=Row.insertCell(-1);e.innerHTML=htmlspecialchars(this.Data[Name]);}
return;}
CXDbGuiEntry.prototype.InsertIntoElement=function(Container,Index){if(!Container)return;var Id='Entry'+this.RecordId;var r=document.getElementById(Id);if(r){r.innerHTML='';}else{r=document.createElement('span');r.style.display='block';r.style.width='100%';r.style.marginBottom='5px';r.id=Id;Container.appendChild(r);}
if(!Index)Index=Container.childNodes.length;r.appendChild(this._SummaryToElement(Index));r.appendChild(this._GetExtendedInfo());}
CXDbGuiEntry.prototype._SummaryToElement=function(Index){var r=document.createElement('span');r.className="EntrySummaryInfo";if(Index%2){r.style.backgroundColor="#EEEEEE";}
{var e=document.createElement('span');e.className="EntrySummaryActions EntrySummaryRegion";var aContents=[this._GetExpansionControlHtml()];for(var iIndex in this.Parent.aResultActions){aContents.push(this.Parent.aResultActions[iIndex].ToHtml(this.Parent,this.RecordId));}
e.innerHTML=aContents.join('');r.appendChild(e);}
this.InsertSummaryIntoElement(r);return r;}
CXDbGuiEntry.prototype.InsertSummaryIntoElement=function(r){return;}
CXDbGuiEntry.prototype.ShowLoadingPanel=function(){if(this.InfoPanel){this.InfoPanel.style.display='none';}
if(this.Form){this.Form.style.display='none';}
if(this.LoadingPanel){this.LoadingPanel.style.display='';}else{this.LoadingPanel=document.createElement('div');this.LoadingPanel.style.textAlign="center";this.LoadingPanel.innerHTML=['<p>','Loading... <img src="',this.Parent.ImagesPath,'progress.gif" alt="Loading"/>','</p>'].join('');this.ExtendedInfo.appendChild(this.LoadingPanel);}}
CXDbGuiEntry.prototype.Cancel=function(){if(this.Form){if(!confirm('Are you sure you want to abandon your changes?  This action is not reversible!')){return false;}
this.Form.parentNode.removeChild(this.Form);this.Form=null;}
if(this.LoadingPanel){this.LoadingPanel.style.display='none';}
if(this.InfoPanel){this.InfoPanel.style.display='';}else if(this.ToInfoPanel){this.ExtendedInfo.appendChild(this.ToInfoPanel());}
return true;}
CXDbGuiEntry.prototype.Edit=function(){if(this.LoadingPanel){this.LoadingPanel.style.display='none';}
if(this.InfoPanel){this.InfoPanel.style.display='none';}
if(this.Form){this.Form.style.display='';}else{this.ExtendedInfo.appendChild(this.ToEditForm());}}
CXDbGuiEntry.prototype.ToInfoPanel=function(){if(this.InfoPanel)return this.InfoPanel;var e=document.createElement('span');e.id='InfoPanel'+this.RecordId;this.InfoPanel=e;return e;}
CXDbGuiEntry.prototype.ToEditForm=function(){if(this.Form)return this.Form;this.Form=document.createElement('form');this.Form.action=['javascript:',this.Parent.Id,'.Apply(\'',this.RecordId,'\')'].join('');this.Form.id="EditForm"+this.RecordId;{var b=document.createElement('div');b.className='inputgroup';b.innerHTML=['<span class="inputcontrol">','<input type="submit" value="',this.RecordId?'Modify':'Create','">','<button onclick="',this.Parent.Id,'.Cancel(\'',this.RecordId,'\');return false;">','Cancel</button>','</span>',''].join('');this.Form.appendChild(b);}
return this.Form;}
CXDbGuiEntry.prototype.Apply=function(){this.ShowLoadingPanel();var oRequest=new AjaxCallback(this,'_ModifyComplete');oRequest.RecordId=this.RecordId;var PostData=this.Parent.BuildPostData(this.Form,'Js');oRequest.Send(this.Parent.Page,PostData);}
CXDbGuiEntry.prototype._ModifyComplete=function(oRequest){eval(oRequest.responseText);if(typeof(SearchResults)=='undefined'){this.Edit();return;}
if(!SearchResults.Results)return;if(this.RecordId==0){this.Parent.HidePanel('AddPanel');this.ExtendedInfo.innerHTML='';this.Parent.AddFormEntry=null;this.Parent._SearchComplete(oRequest);}else if(SearchResults.Results[this.RecordId]){this.Parent.Data['Results'][this.RecordId]=SearchResults.Results[this.RecordId];this.Init(this.RecordId,SearchResults.Results[this.RecordId],this.Parent);var Table=document.getElementById('SearchResults');this.InsertInto(Table);this.ToggleEntryExtendedInfo();}}
window.CXDbGui=function(Page){if(arguments.length<=0)return;this.Init.apply(this,arguments);}
CXDbGui.prototype=new CPage();CXDbGui.prototype.constructor=CXDbGui;CXDbGui.superclass=CPage.prototype;CXDbGui.prototype.Init=function(Page){CXDbGui.superclass.Init.apply(this,arguments);this.ActionPanelNames.push('AddPanel');this.ActionPanelNames.push('SearchPanel');this.SortByField='';this.SortReversed=false;this.aDisplayNames=[];this.aDisplayColumns=null;this.aResultActions=[];this.aResultInputs=[];this.HeaderRows=-1;this.AddFormEntry=null;this.StaticRequestVars=[];this.Events.onSearchComplete="onSearchComplete";this.AddEventHandler(this.Events.onSearchComplete,this,this.onSearchComplete);this.Data=null;}
CXDbGui.prototype._GetData=function(RecordId){var iIndex=this._GetDataIndex(RecordId);if(iIndex<0)return null;return this.Data['Results'][iIndex];}
CXDbGui.prototype._GetDataIndex=function(RecordId){var aResults=this.Data['Results'];for(var iIndex in aResults){var Result=aResults[iIndex];if(Result.RecordId==RecordId){return iIndex;}}
return-1;}
CXDbGui.prototype._NewDataEntry=function(RecordId,Data){return new CXDbGuiEntry(RecordId,Data,this);}
CXDbGui.prototype.CallbackThisForm=function(Form,ResultFormat){if(!ResultFormat)ResultFormat='Html';if(ResultFormat=='Html'){this.ShowLoadingPanel();}
var oRequest=new AjaxCallback(this,'CallbackThisFormComplete'+ResultFormat);var PostData=this.BuildPostData(Form,ResultFormat);oRequest.Send(this.Page,PostData);}
CXDbGui.prototype.BuildPostData=function(Form,ResultFormat){if(!ResultFormat)ResultFormat='Html';var PostData='ContentType=Ajax'+ResultFormat;for(iIndex=0;iIndex<Form.elements.length;iIndex++){var element=Form.elements[iIndex];if(element.name=='')continue;if(element.disabled)continue;if(element.type=='checkbox'){if(!element.checked)continue;}
if(element.type=='radio'){if(!element.checked)continue;}
if(element.type=='select-one'){if(element.value==''){if(element.selectedIndex==-1)continue;var value=element.options[element.selectedIndex].text;PostData+='&'+element.name+'='+escape(value);continue;}}
PostData+='&'+element.name+'=';if(element.value){PostData+=escape(Utf8.encode(element.value));}}
return PostData;}
CXDbGui.prototype.BuildSingleActionPostData=function(Form,ResultFormat,Action){var aDisabledControls=[];for(iIndex=0;iIndex<Form.elements.length;iIndex++){var element=Form.elements[iIndex];if(element.name!='Action')continue
if(element.disabled)continue;element.disabled=true;aDisabledControls.push(iIndex);}
var PostData=this.BuildPostData(Form,ResultFormat);for(iIndex=0;iIndex<aDisabledControls.length;iIndex++){if(Form.elements[iIndex].name=='Action'){Form.elements[iIndex].disabled=false;}}
return PostData+'&Action='+Action;}
CXDbGui.prototype.ShowLoadingPanel=function(){var oLoadingPanel=document.getElementById('CallbackLoadingPanel');if(oLoadingPanel){var oPanel=document.getElementById('CallbackResponsePanel');if(oPanel){oPanel.style.display='';oPanel.innerHTML=oLoadingPanel.innerHTML;window.location.replace("#CallbackResponsePanel");}}}
CXDbGui.prototype.LoadingItem=function(RecordId){var Row=document.getElementById('Row'+RecordId);if(!Row)return;var ColSpan=Row.cells.length;while(Row.cells.length){if(Row.cells[0].colSpan){ColSpan+=Row.cells[0].colSpan-1;}
Row.deleteCell(0);}
var LoadingCell=Row.insertCell(-1);LoadingCell.colSpan=ColSpan;var oLoadingItemPanel=document.getElementById('LoadingItemPanel');if(oLoadingItemPanel){var e=document.createElement('span');e.innerHTML=oLoadingItemPanel.innerHTML;e.className='loadingitempanel';LoadingCell.appendChild(e);}}
CXDbGui.prototype.HideLoadingPanel=function(){var oPanel=document.getElementById('CallbackResponsePanel');if(oPanel){oPanel.style.display='none';oPanel.innerHTML='';window.location.replace("#ActionsPanel");}}
CXDbGui.prototype.CallbackThisFormCompleteHtml=function(oRequest){var oPanel=document.getElementById('CallbackResponsePanel');if(!oPanel){alert("Can't find the CallbackResponsePanel.  The results are shown here instead:"+oRequest.responseText);}else{oPanel.style.display='';oPanel.innerHTML=oRequest.responseText;window.location.href="#CallbackResponsePanel";}}
CXDbGui.prototype.CallbackThisFormCompleteJs=function(oRequest){alert(oRequest.responseText);}
CXDbGui.prototype.ToggleEntryExtendedInfo=function(RecordId){var Result=this._GetData(RecordId);if(!Result)return;Result.ToggleEntryExtendedInfo();}
CXDbGui.prototype.ShowActionPanel=function(){CXDbGui.superclass.ShowActionPanel.apply(this,arguments);var p=document.getElementById('AddPanel');if(!p)return;if(p.style.display=='none')return;if(!this.Id)return;if(!this.AddFormEntry){this.AddFormEntry=this._NewDataEntry(0,{});}
this.AddFormEntry.ExtendedInfo=p;this.AddFormEntry.Edit();}
CXDbGui.prototype.DisplayName=function(FieldName){var DisplayName=FieldName;for(var iIndex in this.aDisplayNames){if(this.aDisplayNames[iIndex].Name==FieldName){DisplayName=this.aDisplayNames[iIndex].Value;break;}}
return DisplayName;}
CXDbGui.prototype.FieldSortLink=function(FieldName,Attributes){var aResult=new Array();aResult.push('<span style="white-space: nowrap;" ');aResult.push(Attributes);aResult.push('>');{aResult.push('<a href="javascript:');aResult.push(this.Id+'.SortAndRender(\''+FieldName+'\')');aResult.push('" title="Sort by this column">');aResult.push(htmlspecialchars(this.DisplayName(FieldName)));aResult.push('</a>');if(this.SortByField==FieldName)
if(this.SortReversed){aResult.push('&nbsp;<img src="'+this.ImagesPath+'arrow-up.gif">');}else{aResult.push('&nbsp;<img src="'+this.ImagesPath+'arrow-down.gif">');}}
aResult.push('</span>');return aResult.join('');}
CXDbGui.prototype.Sort=function(FieldName){if(FieldName){if(FieldName==this.SortByField){this.SortReversed=!this.SortReversed;}else{this.SortByField=FieldName;for(var i in this.Data['Results']){this.Data['Results'][i].SortKey=null;}}}
if(!this.SortByField)return;var SortByField=this.SortByField;var Reversed=this.SortReversed?-1:1;var f=function(a,b){if(!a.SortKey){a.SortKey=eval('a.'+SortByField);if(!a.SortKey)a.SortKey=a.RecordId;}
if(!b.SortKey){b.SortKey=eval('b.'+SortByField);if(!b.SortKey)b.SortKey=b.RecordId;}
if(a.SortKey>b.SortKey)return 1*Reversed;if(a.SortKey<b.SortKey)return-1*Reversed;return 0;}
this.Data['Results'].sort(f);}
CXDbGui.prototype.RenderHeaders=function(eTable){var eSortControls=eTable.insertRow(-1);eSortControls.className="SortControls listheader";{Element=eSortControls.insertCell(-1);Element.className='EntrySummaryActions';var iColspan=this.aResultActions.length+this.aResultInputs.length;Element.colSpan=iColspan+1;Element.innerHTML='<span class="ExtendedInfoIndicatorRegion EntrySummaryRegion"></span>'
+'<span class="EntrySummaryRegion"></span>';if(this.Data['Results'].length){if(!this.aDisplayColumns){this.aDisplayColumns=[];for(var Name in this.Data['Results'][0].Data){this.aDisplayColumns.push(Name);}}
for(var iIndex=0;iIndex<this.aDisplayColumns.length;iIndex++){var Name=this.aDisplayColumns[iIndex];Element=eSortControls.insertCell(-1);Element.className='EntrySummaryName';Element.innerHTML=this.FieldSortLink(htmlspecialchars(Name),' class="EntrySummaryRegion"');}}}}
CXDbGui.prototype.Render=function(){var Container=document.getElementById('ActionContent');Container.innerHTML='';var Element=document.createElement('span');var aContents=new Array();aContents.push("<b>Results:</b> "+this.Data['Results'].length);Element.innerHTML=aContents.join('');Container.appendChild(Element);var Message=this.Data['Message'];if(Message){Element=document.createElement('p');Element.innerHTML=Message;Container.appendChild(Element);}
var Form=document.createElement('form');Form.id='SearchResultsForm';Container.appendChild(Form);Form.innerHTML=this.HiddenFormInputsToHtml();{var eTable=document.createElement('table');eTable.id='SearchResults';eTable.className='SearchResults';this.RenderHeaders(eTable);Form.appendChild(eTable);{var aResults=this.Data['Results'];for(var iIndex in aResults){var Result=aResults[iIndex];Result.InsertInto(eTable);}}
var SearchResultActions=document.createElement('span');SearchResultActions.id="SearchResultsActions";SearchResultActions.innerHTML=this.ActionButtonsToHtml(XMLDBGUI_ACTION_STYLE_SEARCH_LIST);Form.appendChild(SearchResultActions);}}
CXDbGui.prototype.SortAndRender=function(FieldName){this.ShowResultMessage(null);this.Sort(FieldName);this.Render();}
CXDbGui.prototype.Search=function(XPathSearch){var ActionTitle=document.getElementById('ActionTitle');if(ActionTitle){if(!ActionTitle.firstChild){ActionTitle.appendChild(document.createTextNode(''));}
ActionTitle.firstChild.nodeValue="Searching the database";}
var Element=document.getElementById('ActionContent');Element.innerHTML='';this.ShowLoadingPanel();this.ShowResultMessage(null);var oRequest=new AjaxCallback(this,'_SearchComplete');var PostData=[this.StaticRequestVars.join('&'),'ContentType=AjaxJs','Action=Search','Search='+escape(Utf8.encode(XPathSearch))].join('&');oRequest.Send(this.Page,PostData);}
CXDbGui.prototype._SearchComplete=function(oRequest){this.HideLoadingPanel();var ActionTitle=document.getElementById('ActionTitle');if(ActionTitle){ActionTitle.firstChild.nodeValue="Search results";}
var x=oRequest.responseText;if(typeof(x)=='object'){if(!x.Results)return;this.Data=x;}else{x=eval(oRequest.responseText);if(typeof(SearchResults)!='object'){this.Data=x;}else{this.Data=SearchResults;}}
{var aResults=this.Data['Results'];var aEntries=new Array();for(var iIndex in aResults){var Result=aResults[iIndex];aEntries.push(this._NewDataEntry(iIndex,Result));}
this.Data['Results']=aEntries;}
this.RaiseEvent(this.Events.onSearchComplete,[]);}
CXDbGui.prototype.onSearchComplete=function(){this.Sort();this.Render();}
CXDbGui.prototype.Edit=function(RecordId){var Entry=this._GetData(RecordId);if(!Entry)return;Entry.Edit();}
CXDbGui.prototype.Apply=function(RecordId){var Entry=null;if(RecordId==0){Entry=this.AddFormEntry;}else{Entry=this._GetData(RecordId);}
if(!Entry)return;Entry.Apply();}
CXDbGui.prototype.Cancel=function(RecordId){var Entry=this._GetData(RecordId);if(!Entry)return;Entry.Cancel();}
CXDbGui.prototype.DeleteSelected=function(Form){if(!confirm('Are you sure you want to delete these users?  This action is not reversible!')){return false;}
this.ShowLoadingPanel();var oRequest=new AjaxCallback(this,'_DeleteComplete');var PostData=this.BuildSingleActionPostData(Form,'Js','Delete Record');oRequest.Send(this.Page,PostData);}
CXDbGui.prototype.Delete=function(RecordId){this.LoadingItem(RecordId);var oRequest=new AjaxCallback(this,'_DeleteComplete');var PostData=['ContentType=AjaxJs','Action=Delete Record','Select'+RecordId+'=on'].join('&');oRequest.Send(this.Page,PostData);}
CXDbGui.prototype._DeleteComplete=function(oRequest){this.HideLoadingPanel();this.HidePanel('AddPanel');eval(oRequest.responseText);if(typeof(Result)==='undefined'||!Result.DeletedRecordIds){this.Render();return;}
this.ShowResultMessage(Result);var Table=document.getElementById('SearchResults');for(iIndex=0;iIndex<Result.DeletedRecordIds.length;iIndex++){var RecordId=Result.DeletedRecordIds[iIndex];var iDataIndex=this._GetDataIndex(RecordId);if(iDataIndex<0)continue;this.Data['Results'].splice(iDataIndex,1);}
this.Render();}
