// Copyright 2009, BMT Defence Services Ltd, All rights reserved.
var ResultStatus={UnexpectedError:-1,Success:1,RecordNotFound:2,ValidationFailed:3,DeleteChildRecords:4,FileSuccess:5,GeneralFail:6,Archived:7};var PagingType={None:1,Paging:2};var omEntitySaveFunctions=new Object();var bmEntityKeySave=false;
function Entity(lEntityType,lEntityID,zEntityDisplayName,oSaveFunction,zOuterElementID,zClientValidationRowID,zClientValidationLabelID,zUpdateRowID){this.ControlArray=new Array();this.ControlIndex=new Object();this.EntityType=lEntityType;this.EntityID=lEntityID;this.EntityDisplayName=zEntityDisplayName;this.FileArray=new Array();this.EntityFileSaveResult;this.EntityFileSaveResponse;this.EntityFileIndex;this.ChildEntityArray=new Array();this.Validation=true;if(oSaveFunction){omEntitySaveFunctions[zEntityDisplayName]=oSaveFunction;if(bmEntityKeySave==false){$addHandler(document.body,"keyup",function(){entityKeySave(event)});bmEntityKeySave=true;}}if(zOuterElementID){this.MessageScreenShow=messageScreenShow;this.MessageScreenHide=messageScreenHide;this.OuterElement=document.getElementById(zOuterElementID);}else{try{this.MessageScreenShow=window.parent.messageScreenShow;this.MessageScreenHide=window.parent.messageScreenHide;this.OuterElement=window.parent.defaultContentDiv();}catch(opCatch){}}if(zUpdateRowID)this.UpdateRow=document.getElementById(zUpdateRowID);else this.UpdateRow=document.getElementById("trUpdated");if(this.UpdateRow){if(this.EntityID==-1)this.UpdateRow.style.display="none";else this.UpdateRow.style.display="block";}if(zClientValidationRowID)this.ClientValidationRow=document.getElementById(zClientValidationRowID);else this.ClientValidationRow=document.getElementById("trClientValidation");if(zClientValidationLabelID)this.ClientValidationLabel=document.getElementById(zClientValidationLabelID);else this.ClientValidationLabel=document.getElementById("lblClientValidation");controlAddChangeEntity(zEntityDisplayName);}Entity.prototype={AddChildEntity:function(zEntityName,oEntity,zValidationPrefix){this.ChildEntityArray[this.ChildEntityArray.length]=zEntityName;this[zEntityName]=oEntity;if(zValidationPrefix){this[zEntityName].Prefix=zValidationPrefix;this[zEntityName].Child=true;}},Bind:function(zControlID,zPropertyName,zLabelID){var lpControlType;lpControlType=controlGetType(zControlID);var opControl={PropertyName:zPropertyName,ControlID:zControlID,ControlType:lpControlType};if(zLabelID)opControl.LabelID=zLabelID;switch(lpControlType){case ControlType.Select:case ControlType.SelectLabel:case ControlType.FindControl:case ControlType.FindControlLabel:case ControlType.Checkbox:case ControlType.CheckboxLabel:opControl.TextPropertyName=controlGetAttribute(zControlID,"TextPropertyName",lpControlType);this[opControl.TextPropertyName]=controlGetText(zControlID,lpControlType);break;}this.ControlIndex[zPropertyName]=this.ControlArray.length;this.ControlArray[this.ControlArray.length]=opControl;controlLoad(opControl.ControlID,this.EntityDisplayName,opControl.ControlType);this[zPropertyName]=controlGetValue(zControlID);if(opControl.ControlType==ControlType.File)this.FileArray[this.FileArray.length]=opControl;},Changed:function(){var bpLoad=false;if(controlHasChanged(this.EntityDisplayName)){bpLoad=confirm("Are you sure you want to navigate away from this page?\n\n"+"Changes to the "+this.EntityDisplayName+" screen will be lost.\n\n"+"Press OK to continue, or Cancel to stay on the current page.");if(bpLoad==true)controlChangedReset(this.EntityDisplayName);}else bpLoad=true;return!bpLoad;},Delete:function(){this.MessageScreenShow.call(null,MessageScreenType.Progress,"Deleting Data...",this.OuterElement);var opClass=new Object();var opControl;for(var lpLoop=0;lpLoop<this.ControlArray.length;lpLoop++){opControl=this.ControlArray[lpLoop];switch(opControl.PropertyName){case"EntityID":case"DateUpdated":opClass[opControl.PropertyName]=controlGetValue(opControl.ControlID);break;}}return opClass;},DeleteResponse:function(oResult){this.ClientValidationRow.style.display="none";this.ClientValidationLabel.innerText="";var bpReturn=false;switch(oResult.ResultStatus){case ResultStatus.Success:controlChangedReset(this.EntityDisplayName);this.MessageScreenShow.call(null,MessageScreenType.Message,"Record deleted successfully.",this.OuterElement);bpReturn=true;break;case ResultStatus.UnexpectedError:alert(oResult.Description);break;case ResultStatus.RecordNotFound:this.MessageScreenShow.call(null,MessageScreenType.Message,"Record not found, it may have been deleted by another user.",this.OuterElement);break;case ResultStatus.ValidationFailed:for(var lpLoop=0;lpLoop<oResult.Failures.length;lpLoop++){controlAddValidation(oResult.Failures[lpLoop],this.ClientValidationLabel,"The record could not be deleted due to the following validation failures:");}this.ClientValidationRow.style.display="block";this.MessageScreenHide.call();break;case ResultStatus.DeleteChildRecords:this.MessageScreenShow.call(null,MessageScreenType.Message,"The record could not be deleted as it contains child records that must be deleted first.",this.OuterElement);break;case ResultStatus.GeneralFail:controlAddValidation(oResult.Description,this.ClientValidationLabel,"The record could not be deleted due to the following validation failures:");this.ClientValidationRow.style.display="block";this.MessageScreenHide.call();break;case ResultStatus.Archived:controlChangedReset(this.EntityDisplayName);this.MessageScreenShow.call(null,MessageScreenType.Message,"Record archived successfully.",this.OuterElement);bpReturn=true;break;}return bpReturn;},DisplayImage:function(zImageID,zPropertyName,zImagePath){var opImage=document.getElementById(zImageID+"_imgImage");var zpPath;if(zImagePath!=""&&zImagePath!=null){zpPath=this.GetFilePath(zPropertyName,zImagePath);opImage.style.display="block";}else{var zpDefaultImagePath=bmtWebResolveURL(opImage.defaultImagePath);if(zpDefaultImagePath==""){zpPath="";opImage.style.display="none";}else{opImage.style.display="block";zpPath=zpDefaultImagePath;}}return zpPath;},GetFileName:function(zFullPath){var lpLastIndex;var zpFileName;if(zFullPath!=null){lpLastIndex=zFullPath.lastIndexOf("\\");if(lpLastIndex==-1)zpFileName=zFullPath;else zpFileName=zFullPath.slice(lpLastIndex+1,zFullPath.Length);}else{zpFileName="";}return zpFileName;},GetFilePath:function(zPropertyName,zFilePath){var zpFilePath;var zpFileName=this.GetFileName(zFilePath);zpFilePath="~/EntityFiles/"+this.EntityType+"/"+this.EntityID+"/"+zPropertyName+"/"+zpFileName;return bmtWebResolveURL(zpFilePath);},Reload:function(oEntity){var bpReturn=false;controlPageReload();if(oEntity.ResultStatus){this.MessageScreenHide.call();alert(oEntity.Description);}else{switch(oEntity.Result.ResultStatus){case ResultStatus.Success:var opControl;var zpValue;var opPageControl;for(var lpLoop=0;lpLoop<this.ControlArray.length;lpLoop++){opControl=this.ControlArray[lpLoop];if(opControl.PropertyName=="EntityID")this.EntityID=oEntity[opControl.PropertyName];if(opControl.ControlType==ControlType.Image)zpValue=this.DisplayImage(opControl.ControlID,opControl.PropertyName,oEntity[opControl.PropertyName]);else zpValue=controlCheckDate(oEntity[opControl.PropertyName],opControl.ControlType);if(opControl.TextPropertyName){controlSetText(opControl.ControlID,oEntity[opControl.TextPropertyName]);this[opControl.TextPropertyName]=oEntity[opControl.TextPropertyName];}if(opControl.ControlType==ControlType.File||opControl.ControlType==ControlType.FileLink){opPageControl=omControlForm[opControl.ControlID+"$txtFile"];var opNewControl=opPageControl.cloneNode();var opParent=opPageControl.parentNode;opParent.removeChild(opPageControl);opParent.appendChild(opNewControl);if(opControl.ControlType==ControlType.FileLink){controlSetValue(opControl.ControlID,zpValue);var zpEntityDisplayName=this.EntityDisplayName;var zpControlID=opControl.ControlID;opNewControl.onchange=function(){fileLinkOnChange(zpControlID,"Browse")};omFileLinkClient[opControl.ControlID].ControlBox=omControlForm[opControl.ControlID+"$txtFile"];$addHandler(opNewControl,"propertychange",function(){controlTextChange(event,zpEntityDisplayName);});}}else controlSetValue(opControl.ControlID,zpValue);this[opControl.PropertyName]=zpValue;}for(lpLoop=0;lpLoop<this.ChildEntityArray.length;lpLoop++){this[this.ChildEntityArray[lpLoop]].Reload(oEntity[this.ChildEntityArray[lpLoop]]);}bpReturn=true;if(this.EntityID==-1)this.UpdateRow.style.display="none";else this.UpdateRow.style.display="block";this.ClientValidationRow.style.display="none";this.ClientValidationLabel.nodeValue="";controlChangedReset(this.EntityDisplayName);this.MessageScreenHide.call();break;case ResultStatus.UnexpectedError:this.MessageScreenHide.call();alert(oEntity.Result.Description);break;case ResultStatus.RecordNotFound:this.MessageScreenHide.call();this.MessageScreenShow.call(null,MessageScreenType.Message,"Record not found, it may have been deleted by another user.",this.OuterElement);break;case ResultStatus.ValidationFailed:for(var lpLoop=0;lpLoop<oEntity.Result.Failures.length;lpLoop++){controlAddValidation(oEntity.Result.Failures[lpLoop],this.ClientValidationLabel);}this.ClientValidationRow.style.display="block";break;}}return bpReturn;},Save:function(){this.MessageScreenShow.call(null,MessageScreenType.Progress,"Saving Data...",this.OuterElement);var opClass=new Object();var opControl;var zpTextPropertyName;var opPageControl;for(var lpLoop=0;lpLoop<this.ControlArray.length;lpLoop++){opControl=this.ControlArray[lpLoop];if(opControl.ControlType!=ControlType.Image){opClass[opControl.PropertyName]=controlGetValue(opControl.ControlID);if(opControl.ControlType==ControlType.Date){if(opClass[opControl.PropertyName]!="")bmtWebTimeZoneAdjust(opClass[opControl.PropertyName]);}if(opControl.ControlType==ControlType.Select){opClass[opControl.TextPropertyName]=controlGetText(opControl.ControlID);}else if(opControl.ControlType==ControlType.FindControl){opClass[opControl.TextPropertyName]=controlGetText(opControl.ControlID);}}}for(lpLoop=0;lpLoop<this.ChildEntityArray.length;lpLoop++){opClass[this.ChildEntityArray[lpLoop]]=this[this.ChildEntityArray[lpLoop]].Save();}return opClass;},SaveFile:function(lResultStatus){if(this.EntityFileIndex==this.FileArray.length||(this.EntityFileIndex>0&&lResultStatus!=ResultStatus.Success))this.EntityFileSaveResponse.call(null,lResultStatus,this.EntityFileIndex);else{var zpControlID=this.FileArray[this.EntityFileIndex].ControlID;var opControl=document.getElementById(zpControlID+"_txtFile");if(opControl.value!=""){var zpPropertyName=this.FileArray[this.EntityFileIndex].PropertyName;var opControl=document.getElementById(zpControlID+"_txtFile");var lpFileType;var lpMaxSize;var lpMaxWidth;var lpMaxHeight;lpFileType=opControl.fileType;if(opControl.maxSize)lpMaxSize=opControl.maxSize;else lpMaxSize=0;if(opControl.maxHeight)lpMaxHeight=opControl.maxHeight;else lpMaxHeight=0;if(opControl.maxWidth)lpMaxWidth=opControl.maxWidth;else lpMaxWidth=0;var zpFileName=fileUploadGetFileName(zpControlID);if(fileUploadSave(zpControlID,this.EntityType,this.EntityID,zpPropertyName,lpFileType,lpMaxSize,lpMaxHeight,lpMaxWidth,this,opControl.uploadPath,zpFileName)==false){var opLabel=document.getElementById(this.FileArray[this.EntityFileIndex].LabelID);controlAddValidation(opLabel.innerText+" file not found.  Please use the 'Browse' button to select a valid file.",this.ClientValidationLabel);this.ClientValidationRow.style.display="block";this.EntityFileSaveResult=ResultStatus.ValidationFailed;this.EntityFileIndex++;this.SaveFile();}else this.EntityFileIndex++;}else{this.EntityFileIndex++;this.SaveFile();}}},SaveFileResponse:function(){var lpResultStatus=parseInt(document.getElementById("iframeFileUpload").contentWindow.document.frmMain.hidResultStatus.value);if(lpResultStatus==ResultStatus.FileSuccess){this.SaveFile(lpResultStatus);}else{this.EntityFileSaveResult=ResultStatus.ValidationFailed;controlAddValidation(document.getElementById("iframeFileUpload").contentWindow.document.frmMain.hidResultDescription.value,this.ClientValidationLabel);this.SaveFile(lpResultStatus);}},SaveFiles:function(oResponse){this.EntityFileSaveResult={ResultStatus:ResultStatus.FileSuccess,Description:null,arrFailures:new Array(),arrWarnings:new Array(),ResultDateUpdated:null,ResultUserUpdated:null};this.EntityFileIndex=0;this.EntityFileSaveResponse=oResponse;this.SaveFile();},SaveResponse:function(oResult){var bpReturn=false;switch(oResult.ResultStatus){case ResultStatus.Success:var bpNewRecord=false;if(this.EntityID==-1){bpNewRecord=true;this.UpdateRow.style.display="block";}for(var lpLoop=0;lpLoop<this.ControlArray.length;lpLoop++){opControl=this.ControlArray[lpLoop];switch(opControl.PropertyName){case"EntityID":controlSetValue(opControl.ControlID,oResult.EntityID);break;case"DateUpdated":controlSetValue(opControl.ControlID,oResult.ResultDateUpdated);break;case"UserUpdatedText":controlSetValue(opControl.ControlID,oResult.ResultUserUpdated);break;case"DateCreated":if(bpNewRecord==true)controlSetValue(opControl.ControlID,oResult.ResultDateUpdated);break;case"UserCreatedText":if(bpNewRecord==true)controlSetValue(opControl.ControlID,oResult.ResultUserUpdated);break;}this[opControl.PropertyName]=controlGetValue(opControl.ControlID);if(opControl.TextPropertyName)this[opControl.TextPropertyName]=controlGetText(opControl.ControlID);}controlChangedReset(this.EntityDisplayName);bpReturn=true;break;case ResultStatus.UnexpectedError:this.MessageScreenHide.call();alert(oResult.Description);break;case ResultStatus.RecordNotFound:this.MessageScreenHide.call();this.MessageScreenShow.call(null,MessageScreenType.Message,"Record not found, it may have been deleted by another user.",this.OuterElement);break;case ResultStatus.ValidationFailed:for(var lpLoop=0;lpLoop<oResult.Failures.length;lpLoop++){controlAddValidation(oResult.Failures[lpLoop],this.ClientValidationLabel);}this.ClientValidationRow.style.display="block";this.MessageScreenHide.call();break;case ResultStatus.GeneralFail:controlAddValidation(oResult.Description,this.ClientValidationLabel);this.ClientValidationRow.style.display="block";this.MessageScreenHide.call();break;case ResultStatus.FileSuccess:bpReturn=true;break;case ResultStatus.Archived:controlChangedReset(this.EntityDisplayName);this.MessageScreenShow.call(null,MessageScreenType.Message,"Record archived successfully.",this.OuterElement);bpReturn=true;break;}for(lpLoop=0;lpLoop<this.ChildEntityArray.length;lpLoop++){this[this.ChildEntityArray[lpLoop]].SaveResponse(oResult.Children[this.ChildEntityArray[lpLoop]]);}return bpReturn;},Validate:function(oDiv){if(this.Validation==true){var oElement;var opLabel;var bpValid=true;var lpLength;var opControl;var zpPrefix="";if(!this.Child){this.ClientValidationRow.style.display="none";this.ClientValidationLabel.innerText="";}if(this.Prefix){zpPrefix=this.Prefix;zpPrefix=zpPrefix.replace(/\s+$/,"");zpPrefix=zpPrefix+' ';}for(var lpLoop=0;lpLoop<this.ControlArray.length;lpLoop++){opControl=this.ControlArray[lpLoop];switch(this.ControlArray[lpLoop].ControlType){case ControlType.Input:case ControlType.TextArea:opElement=document.getElementById(opControl.ControlID+"_txtTextbox");if(opElement.required){if(opElement.required==1){if(opElement.value==""){opLabel=document.getElementById(opControl.LabelID);controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,opLabel.firstChild.nodeValue.length-2)+" is a required field.",this.ClientValidationLabel);bpValid=false;}}}if(opControl.ControlType==ControlType.TextArea){if(opElement.value.length>parseInt(opElement.maxLength)){opLabel=document.getElementById(opControl.LabelID);lpLength=opLabel.firstChild.nodeValue.length;if(opElement.required){if(opElement.required==1){lpLength-=2;}}controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,lpLength)+" has a maximum length of "+opElement.maxLength+" characters.",this.ClientValidationLabel);bpValid=false;}}break;case ControlType.Select:if(document.getElementById(opControl.ControlID+"_tblSelect").Required=="True"){if(selectGetValue(opControl.ControlID)=="-1"){opLabel=document.getElementById(opControl.LabelID);controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,opLabel.firstChild.nodeValue.length-2)+" is a required field.",this.ClientValidationLabel);bpValid=false;}}break;case ControlType.FindControl:opElement=document.getElementById(opControl.ControlID+"_tblFind");if(opElement.required==1){if(findGetText(opControl.ControlID)==""){opLabel=document.getElementById(opControl.LabelID);controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,opLabel.firstChild.nodeValue.length-2)+" is a required field.",this.ClientValidationLabel);bpValid=false;}}if(findGetValue(opControl.ControlID)=="-1"&&findGetText(opControl.ControlID)!=""){opLabel=document.getElementById(opControl.LabelID);lpLength=opLabel.firstChild.nodeValue.length;if(opElement.required){if(opElement.required==1){lpLength-=2;}}opLabel=document.getElementById(opControl.LabelID);controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,lpLength)+" does not contain valid data.",this.ClientValidationLabel);bpValid=false;}break;case ControlType.Date:opElement=document.getElementById(opControl.ControlID+"_txtDate");opLabel=document.getElementById(opControl.LabelID);lpLength=opLabel.firstChild.nodeValue.length;if(opElement.required&&parseInt(opElement.required)==1){lpLength-=2;}if(opElement.required){if(opElement.value==""){controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,lpLength)+" is a required field.",this.ClientValidationLabel);bpValid=false;}else if(bmtWebDateValid(opElement.value)==false){controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,lpLength)+" is not a valid date.",this.ClientValidationLabel);bpValid=false;}}else{if(opElement.value!=""&&bmtWebDateValid(opElement.value)==false){controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,lpLength)+" is not a valid date.",this.ClientValidationLabel);bpValid=false;}}break;case ControlType.FileLink:opElement=document.getElementById(opControl.ControlID+"_txtFileEdit");if(opElement.required){if(opElement.required==1){if(opElement.value==""){opLabel=document.getElementById(opControl.LabelID);controlAddValidation(zpPrefix+opLabel.firstChild.nodeValue.substring(0,opLabel.firstChild.nodeValue.length-2)+" is a required field.",this.ClientValidationLabel);bpValid=false;}}}break;}}for(lpLoop=0;lpLoop<this.ChildEntityArray.length;lpLoop++){if(this[this.ChildEntityArray[lpLoop]].Validate()==false)bpValid=false;}if(bpValid==false&&!this.Child){this.ClientValidationRow.style.display="block";oDiv.scrollTop=0;}return bpValid;}else{return true;}}}
function entityFileUploadResult(lResultStatus,zResultDescription,lRecordsAdded){if(parseInt(lResultStatus)==ResultStatus.FileSuccess){if(fileUploadEntity()){if(lRecordsAdded){if(lRecordsAdded>0)fileUploadEntityUpdate(lRecordsAdded);}fileUploadEntity().SaveFileResponse();}else{alert(zResultDescription);fileUploadEntity().SaveFileResponse();}}else{alert(zResultDescription);fileUploadEntity().SaveFileResponse();}}
function entityKeySave(oEvent){if(oEvent.keyCode==83&&oEvent.ctrlKey){if(event.srcElement.EntityDisplayName){if(omEntitySaveFunctions[event.srcElement.EntityDisplayName])omEntitySaveFunctions[event.srcElement.EntityDisplayName].call();}}}
function entityKeySaveUnboundCountrol(oControl,zEntityDisplayName){oControl.EntityDisplayName=zEntityDisplayName;}
