/*
	@Author: Anthony Cashaw of http://enknot.estrip.org
	@Version: 1.0 2008-06-27
	@Package: RenalEstimator
	@Dependacies: Surebert Tookit version 2.08.  
	@Note: 	All equations intellectual property of Hyung Kim of
			http://www.roswellpark.org/Site/Patient_Care/Meet_the_Team/Clinicians/Kim_Hyung_MD. This program may not be used 
			or changed without express permission from Hyung Kim.  Please contact him at Hyung.Kim@RoswellPark.org for 
			further infomration.
*/

var RenalEstimator = function(ele){
	
	if((typeof(ele.myType) == 'function') && ele.myType() === 'Renal'){
		return ele;
	}
	
	try{
		ele = sb.s$('#' + ele);		
	}catch(e){
		//sb.consol.dump(e);
	}
	
				
	if(ele.nodeName && ele.nodeName == 'FORM'){						
		tc.importProperties(this, ele);				
		ele.init();						
		return ele;
	}
};

RenalEstimator.prototype = {
	
	bjornsson : function(){ 	
		var ret;
		
		if(this.data.sex == 1){
			ret = ((25-(0.175*this.data.age))*this.data.weight*0.07)/this.data.creatinine;
		}else{
			ret = ((27-(0.173*this.data.age))*this.data.weight*0.07)/this.data.creatinine;
		}
		
		this.write(Math.round(ret), 'bjornsson');
		
		return ret;
	},
	
	collectData : function(extra){ 
		var ele, node, lbl;
		var that = this;
		var bad = false;
		var sBlank = '';
		var ret = true;
		var reError = /error/ig;
		var workList = [];
		
		
		extra = extra || 0;
		this.data = {};
		
		if(extra){
			workList = this.plainList.concat(this.extraList);
		}else{			
			workList = this.plainList.slice(0);
		}
		
		workList.forEach(function(name){ 
			ele = that[name];
			
			
			//clear error status	
			lbl =  $('#'+name+'_label');
			lbl.className = lbl.className.replace(reError, '').trim();
			
			if(ele){	
								
				if(ele.type){
					//text, textarea, password, etc			
					if((name != 'warmischemia') && ele.value.trim() == ''){
						bad = true;					
						sBlank += "<p>" + that.getLabelText(name) + " requires a value</ p>\n"; 						
					}
					
					//extra creatine < 4
					if(extra && name == 'creatinine' && $('#'+name).value > 4){
						bad = true;
						sBlank += "<p>" + that.getLabelText(name) + " must be less than or equal to 4 (four)<p />\n"; 		
					}
					that.data[name] = ele.value.trim();
				}else{
					//checkbox and radio button arrays	
					that.data[name] = '';				
					for(var i=0;i<ele.length;i++){
						node = ele[i];
						if(node.checked){
							that.data[name] = node.value;
						}
					}
					
					if(that.data[name] == ''){ 
						bad = true;
						sBlank += "<p>" + that.getLabelText(name) + " requires a value</p>\n"; 
					}
				}
			}else{
				bad = true;
				this.warn('Missing ' + name + ' calculator cannot function without this element');
			}
		});
		
		if(bad){
			ret = false;
			this.write(sBlank);
		}
		
		return ret;
	},
	
	calculate : function(e){
		
		var ret = 0;
		var targ = sb.events.target(e);				
	//	this.output.innerHTML = '';
		this.results = {};
				
		if(targ.nodeName == 'BUTTON'){	
			
			
			sb.events.stopAndPrevent(e);
			
			switch(targ.id){	
			
				case 'creatine_clearance':
				
				if(this.collectData()){
					var all = 0;
					
					//run all caculation functions
					all += this.bjornsson();
					all += this.gault();
					all += this.hull();
					all += this.martin();
					all += this.mawer();
					
					this.write(Math.round(all/5), 'average');
				}
				
				break;
				
				case 'postoperative_renal':	
				
					if(this.collectData(1)){			
						this.postop();
					}
				break;
			}
		}
	},
	
	data : {},
	
	getLabelText : function(id){	
		var reLbl = /(<([^>]+)>)/ig;
		var label = $('#'+id+'_label');		
		var ret = '';
		
		if(label){
			ret = label.innerHTML.replace(reLbl, '');
			label.className += ' error';
		}
		
		return ret;
	},
	
	gault : function(){
		
		var ret;
		
		if(this.data.sex == 2){
			ret = ((140-this.data.age)*this.data.weight)/(this.data.creatinine*72);
		}else{
			ret = (((140-this.data.age)*this.data.weight)/(this.data.creatinine*72))*.85;
		}
		
		this.write(Math.round(ret), 'gault');		
		return ret;
	},
	
	hull : function(){
		var ret;
		
		if(this.data.sex == 2){
			ret = (((145-this.data.age)/this.data.creatinine)-3)*(this.data.weight/70);
		}else{
			ret =  ((((145-this.data.age)/this.data.creatinine)-3)*(this.data.weight/70))*0.85;
		}
		
		this.write(Math.round(ret), 'hull');
		
		return ret;
	},
	
	init : function(){ 
		 
		this.event('keydown', this.inNums);
		this.event('keyup', this.outNums);		
		this.event('submit', function(e){sb.events.stopAndPrevent(e);});
		this.event('click', this.calculate);
				
		this.output = $('#output');
	},
	
	
	inNums : function(e){
		var targ = sb.events.target(e);			
		var re = /text/i;
		
				
		targ.previous = targ.value;
		//DEBUG -- report the hit key
		//document.title = e.keyCode;
		if(targ.type && (re.test(targ.type))){
			if((e.keyCode > 36 && (e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 106)) && (e.keyCode !== 190) && (e.keyCode !== 110)){
				sb.events.preventDefault(e);
			}
		}
	},
	
	martin : function(){		
		var ret;
		
		if(this.data.sex == 2){
			ret = ((163*this.data.weight)*(1-(0.00496*this.data.age)))/(this.data.creatinine*88);
		}else{				
			ret = ((163*this.data.weight)*(1-(0.00496*this.data.age))*(1-0.252))/(this.data.creatinine*88);
		}	
		
		this.write(Math.round(ret), 'martin');
		
		return ret;
	}, 
	
	mawer : function(){			
		var ret;
		
		if(this.data.sex == 1)		{
			ret =  ((this.data.weight*(25.3-(.175*this.data.age)))*(1-(0.03*this.data.creatinine)))/(14.4*this.data.creatinine);
		}else{				
			ret =  ((this.data.weight*(29.3-(.203*this.data.age)))*(1-(0.03*this.data.creatinine)))/(14.4*this.data.creatinine);
		}
		
		this.write(Math.round(ret), 'mawer');
		
		return ret;
	},
	
	myType : function(){
		return 'Renal';
	},
	
 
			
	outNums : function(e){
		var targ = sb.events.target(e);	
		var re = /text/i;
				
		if( (targ.type && (re.test(targ.type))) && ((targ.value != '.') && isNaN(targ.value) )){
			targ.value = targ.previous;
		}
	},
	
	postop : function(){ 
		var ret;
		
		
	if(this.data.nephrectomy != 1){		
				
				ret = this.gault()-(18.59808+(this.data.weight*.2946716)+(this.data.age*-0.3770909));
				
	}else{
				
				if(this.data.warmischemia != ''){
						
					ret = this.gault()-(15.49394+(this.data.age*-0.2655462)+(this.data.warmischemia*.3536349));
					
				}else{
					
					ret = this.gault()-(18.96348+(this.data.age*-0.2758516)+(this.data.tumorcm*1.962794));
					
	
				}
			}
			
			//print results		
			this.write((ret < 5)?'<5':Math.round(ret), 'postop');
		
	},
	
	results : {},
	
	plainList : ['creatinine','height', 'weight', 'age',  'sex'],
	
	//removed - 'race'
	extraList : ['tumorcm', 'warmischemia', 'nephrectomy'],
	
	warn : function(str){
		alert("Warning: \n" + str);
	},
	
	write : function(str, targ){
		var targele;
		var oRE = /<(.|\n)*?>/gi;

			
		if(targ && (targele = $('#'+targ))){
			targele.innerHTML = str;
		}else{
			if(this.output){
				//this.output.innerHTML += str + '<br />';
				this.warn(str.replace(oRE, ''));				
				
			}else{
				this.warn(str.replace(oRE, ''));
			}
		}
	}
	
};
