var _CM_ = new ChangeManager();

function ChangeManager() {
this.fieldChanged = CM_fieldChanged;
this.fieldBlur = CM_fieldBlur;
this.keyPress = CM_keyPress;
this.suspend = CM_suspend;
this.resume = CM_resume;
this.registerForm = CM_registerForm;
this.record = CM_record;
this.allowLeave = CM_allowLeave;
this.captureUnloadEvents = CM_captureUnloadEvents;

this.fieldValueChanged = false;
this.allowLeaveForm = false;
this.isOn = true;
this.suspended = new Array();
this.eventsCapured = false;
}

function CM_fieldChanged(e) {
if (this.isOn && e.changeManagement != false && e.formObj.changeManagement == true && this.suspended[e.name] != true) {
this.fieldValueChanged = true;
}
}
function CM_fieldBlur(e) {
if (this.isOn && e.changeManagement != false && e.formObj.changeManagement == true && this.suspended[e.name] != true) {
if (this.fieldValueChanged == null) this.fieldValueChanged = e.hasChanged(false);
}
}
function CM_keyPress() {
this.fieldValueChanged = this.fieldValueChanged ? true : null;
}
function CM_suspend(e) {
if (e == null) {
this.isOn = false;
} else {
this.suspended[e.name] = true;
}
}
function CM_resume(e) {
if (e == null) {
this.isOn = true;
} else {
this.suspended[e.name] = false;
}
}
function CM_registerForm(formObj) {
if (!this.eventsCaptured) {
this.eventsCaptured = true;
setTimeout("_CM_.captureUnloadEvents()",100);
}
var e = formObj.getElement("_fieldChanged");
if (e != null) this.fieldValueChanged = e.get() == "true";
}
function CM_record(formObj) {
var e = formObj.getElement("_fieldChanged");
if (e != null) e.set(this.fieldValueChanged);
}
function CM_allowLeave() {
this.allowLeaveForm = true;
}
function CM_captureUnloadEvents() {
var i, link, href;
for (i in document.links) {
link = document.links[i];
href = link.href;
if (href != null) {
href = href.toLowerCase();
if (href.indexOf("mailto:") != 0) {
if (link.onclick != null) {
link.oldOnClick = link.onclick;
}
if (href.indexOf("javascript:") == -1) {
link.onclick = fieldValueChangedCheck;
} else if (href.indexOf("javascript:%20history.") == 0) {
link.onclick = acceptLink;
} else {
link.onclick = fixJSLink;
}
}
}
}
document.body.onbeforeunload=onUnloadCheck;
}


function onUnloadCheck() {
if (_CM_.fieldValueChanged != false && !_CM_.allowLeaveForm) {
return "You have changed data.  If you continue, your changes will be lost!";
}
}
function acceptLink() {
if (this.oldOnClick != null && this.oldOnClick() == false) {
return false;
}
_CM_.allowLeave();
}
function fixJSLink() {
if (this.oldOnClick != null && this.oldOnClick() == false) {
return false;
}
var href=unescape(this.href);
var pos = href.toLowerCase().indexOf("javascript:");
if (pos == -1) return true;
href = href.substring(pos + 11);
eval(href);
return false;
}
function fieldValueChangedCheck() {
if (this.oldOnClick != null && this.oldOnClick() == false) {
return false;
}
if (_CM_.fieldValueChanged != false && !_CM_.allowLeaveForm) {
var result;
if (document.all && typeof uberPrompt == "function") {
result = uberPrompt('<p align="center" style="color:red;font-size:14pt">You have made changes.</p><p>Click "Save" to save and continue.<br>Click "Don\'t Save" to continue without saving.<br>Click "Cancel" to remain on the form.',new Array("Save","Don't Save","Cancel"),300,200,"Yes","Cancel");
} else {
result = confirm("You have changed data.  Click 'Ok' to save and continue or 'Cancel' to continue without saving.") ? 0 : 1;
}
if (result == 0) {
submitForm(null, null, this.href);
}
if (result != 1) {
return false;
}
_CM_.allowLeave();
}
}

var _SR_;
if(_SR_ != null) _SR_.notify("changemanagement.js");