var CalcField=function(){var compTags=/(SELECT|RADIO|CHECKBOX)/,errorConst='NA',op=['+','-','*','/','(',')'],retObj={};op.inArray=function(param){for(var i=0;i<this.length;i++){if(this[i]==param){return true;}}return false;};function _getFormElement(formElem,elemName){var elem=[];for(var i=0;i<formElem.elements.length;i++){if(formElem.elements[i].name==elemName){elem.push(formElem.elements[i]);}}return elem;};function _getValue(formElem,operand){var elem=[];if(!isNaN(parseFloat(operand))){return parseFloat(operand);}else{elem=_getFormElement(formElem,operand);if(elem.length!=0){for(var i=0;i<elem.length;i++){if(compTags.test(elem[i].tagName)){if(elem[i].multiple){throw errorConst;}return elem[i][elem[i].selectedIndex].value;}else if(elem[i].tagName=="INPUT"&&compTags.test(elem[i].type.toUpperCase())){if(elem[i].checked){return elem[i].value;}}else{return elem[i].value;}}}else{throw errorConst;}}};function _calculate(formElem,equation){var equationParts,leftExp,operand='',rightExp,rightExpMod='',rightValue,value='';try{equation=equation.replace(/\s/g,'');equationParts=equation.split('=');leftExp=_getFormElement(formElem,equationParts[0]);if(leftExp.length==0){return;}rightExp=equationParts[equationParts.length-1];rightExpMod=rightExp;for(var i=0;i<rightExp.length;i++){if(op.inArray(rightExp.charAt(i))){if(operand!=''){value=Number(_getValue(formElem,operand));rightExpMod=rightExpMod.replace(operand,value);operand='';}}else{operand+=rightExp.charAt(i);}}if(operand!=''){value=_getValue(formElem,operand);rightExpMod=rightExpMod.replace(operand,value);}if(rightExpMod!=''){eval('rightValue='+rightExpMod);if(!isNaN(parseFloat(rightValue))){leftExp[0].value=rightValue;return;}throw errorConst;}}catch(e){leftExp[0].value=errorConst;}};retObj.Calculate=function(evt){var elem,formElem;evt=(evt)?evt:(window.event)?window.event:null;if(evt){elem=(evt.target)?evt.target:evt.srcElement;formElem=elem.form;if(formElem.equations){for(var i=0;i<formElem.equations.length;i++){if(formElem.equations[i].indexOf(elem.name)){_calculate(formElem,formElem.equations[i]);}}}}};retObj.addEquation=function(formName,equation){var formElem,me=this;formElem=(document.getElementById(formName))?document.getElementById(formName):(document.getElementsByName(formName).length>0)?document.getElementsByName(formName)[0]:null;if(formElem){if(formElem.equations==null||formElem.equations==undefined){formElem.equations=new Array();}formElem.equations.push(equation);for(var i=0;i<formElem.elements.length;i++){if(compTags.test(formElem.elements[i].tagName)){formElem.elements[i].onchange=function(evt){me.Calculate(evt);};}else if(formElem.elements[i].tagName=="INPUT"){if(compTags.test(formElem.elements[i].type.toUpperCase())){formElem.elements[i].onclick=function(evt){me.Calculate(evt);};}else{formElem.elements[i].onchange=function(evt){me.Calculate(evt);};}}}}};return retObj;}();