// JavaScript Document

var glb_FormHasBeenUpdated = false;
var unsavedChangesMessageText = "You have modified data on this page which has not yet been saved.  Press OK if you want to continue and discard your unsaved changes.";

function formUpdatedCallback(){
	glb_FormHasBeenUpdated = true;	
}

function addLink(link, f){
    if(link.commandObject == null){
                new LinkCommand(link);
    }
    link.commandObject.addFunction(f);
}

 

function addFieldCommand(field, f){
    if(field.commandObject == null){
                new FormFieldCommand(field);
    }
    field.commandObject.addFunction(f);

}

 

function LinkCommand(link){
	link.commandObject = this;
	this.baseElement = null;
	this.commands = new Array();
	if(link.onclick) this.addFunction(link.onclick);
	link.onclick=this.executeFunctions;
}

 

LinkCommand.prototype = {
	addFunction : function(f){
			this.commands[this.commands.length] = f;
		},
	setBaseElement : function(e){
			this.baseElement = e;
		},
	executeFunctions : function(event){
				
				for(var i=0; i<this.commandObject.commands.length; i++){
					if(this.commandObject.commands[i] != null){
						if(this.commandObject.commands[i](event)==false) return false;
					}
				
				}
			return true;                    
		}
}

function linkNavigate(e, url){
	e = (e) ? e : ((window.event) ? window.event : null);	
	var el = (e.target) ? e.target : e.srcElement;
	window.location.href = url;

}

 

function FormFieldCommand(field){
	field.commandObject = this;
	this.commands = new Array();
	if(field.onchange) this.addFunction(field.onchange);
	field.onchange=field.commandObject.executeFunctions;
	this.setBaseElement(field);
}

FormFieldCommand.prototype = LinkCommand.prototype;


function confirmNavAwayUnsavedChanges(event){
	if(glb_FormHasBeenUpdated){;
		return confirm(unsavedChangesMessageText);
	}
	 return true;
}


function addOnChange(field, fn){
    if(field == null) alert("field is null!");
    switch(field.type){
        case "hidden":
        case "password":
                break;
        default:
            field.onchange=fn;
            break;
    }
}

 

function hookup(){
    var links = document.links;
    for(var i=0; i < links.length; i++){
		addLink(links[i], confirmNavAwayUnsavedChanges);
    }
    for(var i=0; i < document.forms.length; i++){
    	var isChangeManaged = checkClassNames(document.forms[i].className,"changeManaged");
            for(var j=0; j< document.forms[i].elements.length; j++){
				if(isChangeManaged){
					addFieldCommand(document.forms[i].elements[j], formUpdatedCallback);
					//alert(isChangeManaged);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"dateTransform")){
					addFieldCommand(document.forms[i].elements[j], formatDateElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"lineFeedTransform")){
					addFieldCommand(document.forms[i].elements[j], formatMultiLineElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"urlTransform")){
					addFieldCommand(document.forms[i].elements[j], formatUrlElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"entityListTransform")){
					addFieldCommand(document.forms[i].elements[j], formatEntityListElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"custidListTransform")){
					addFieldCommand(document.forms[i].elements[j], formatCustidListElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"upperCaseTransform")){
					addFieldCommand(document.forms[i].elements[j], formatUpperCase);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"userPermissionManage")){
					addFieldCommand(document.forms[i].elements[j], maintainUserPermissionElement);
				}
				if(checkClassNames(document.forms[i].elements[j].className,"transExtractFormatManaged")){
					addFieldCommand(document.forms[i].elements[j], manageExtractTypeFormat);
				}
				if(document.forms[i].elements[j].type == 'textarea' && status < 3 && (!checkClassNames(document.forms[i].elements[j].className,"skipSpellCheck"))){
						addSpellCheck(document.forms[i].elements[j]);
				}
        }
    }
}


function checkClassNames(names, cn){
	if(names=="") return false;
	var arr = names.split(" ");
	for(var i=0; i<arr.length; i++){
		if(arr[i]==cn) return true;
	
	}
	return false;
}

function checkSpelling(e){
	e = (e) ? e : ((window.event) ? window.event : null);
	var el = (e.target) ? e.target : e.srcElement;
	var f=el.form;
	doSpell( 'en', el, document.location.protocol + '//' + document.location.host + '/cgi-bin/sproxy/sproxy.pl', true);

}

function addSpellCheck(fld){
	var e = document.createElement('input');
	e.setAttribute('type', 'button');
	e.onclick = checkSpelling;
	e.value = "Check Spelling";
	fld.parentNode.appendChild(e);
}


hookup();

 
