function getParent(src,tagName) {
	while (src != null){
		if (src.tagName == tagName){
			return src;}
		src = src.parentNode;
	}
	return null;
}
 
 
 
function checkFields(theFields,theLoc,theFormName){
	var arrFields = theFields.split(",")
	var j = -1;
	for (i=0; i < arrFields.length; i++){
		var theEle = document.getElementById(arrFields[i]);
		var parentTR = getParent(theEle,"TR");
		if (theEle.type=='checkbox' || theEle.type=='radio'){
			theEleColl = eval("document."+ theFormName +"."+ theEle.name);
			var k = 0;
			for(n=0;n < theEleColl.length;n++){
				if (theEleColl[n].checked) 
					k++;
			}
			if (k == 0){
				addClass(parentTR,"ofMissing");
				j++
				if (j==0)
					theEle.focus();
			}
			else{
				removeClass(parentTR,"ofMissing");
			}
		}
		else{
			if (theEle.value == ""){
				addClass(parentTR,"ofMissing");
				j++;
				if (j==0)
					theEle.focus();
			}
			else{
				removeClass(parentTR,"ofMissing");
			}
		}
	}
	if (j == -1) window.location = theLoc;
}


 
function selectField(theEle,theFocus){
	var parentTR = getParent(theEle,"TR");

	if (parentTR.nodeType == 1 && parentTR.tagName == "TR"){
		if (!hasClass(parentTR,"ofSelected")){
			addClass(parentTR,"ofSelected");
		}
		else{
			removeClass(parentTR,"ofSelected");
		}
	}
}

function numTyped(current,next,maxlen){
	var len = current.value.length
	if (len==maxlen) {
		next.focus()
	}
}


function parentElementsByTagName(doc, tagname, classname) { 
   var findings = new Array(); 
   var digg_in = doc; 
   while (digg_in.parentNode) { 
      digg_in = digg_in.parentNode; 
      if (digg_in.nodeName.toLowerCase() == tagname.toLowerCase()) { 
         if (arguments.length==3) { 
            if (digg_in.getAttribute('class') && 
                digg_in.getAttribute('class').toLowerCase().indexOf(classname.toLowerCase())>-1) { 
               findings.push(digg_in); 
            } 
         } else { 
            // no doubt 
            findings.push(digg_in); 
         } 
      } 
   } 
   return findings; 
} 



function updateGroupTotals(theEle){
	var theClassToSearch = theEle.className.replace("ofxInput ","")
	var allInputs = document.getElementsByClassName(theClassToSearch);
	var totalId = "";

	switch(theClassToSearch){
		case "ofxDomEq" : 
			totalId = "dom_eq";
			break    
		case "ofxGlobal" :
			totalId = "glob_int";
			break
		case "ofxFixed" :
			totalId = "fixed";
			break
		case "ofxShortTerm" :
			totalId = "short";
			break
	} 


	if (!isNaN(parseInt(theEle.value))){
		theEleVal = Math.round(theEle.value);
		theEle.value = theEleVal;
	}
	else{
		theEle.value = "";
	}

	var j = 0;
	for (i=0; i < allInputs.length;i++){
		if (allInputs[i].value != "" && !isNaN(parseInt(allInputs[i].value))){
			j = j + parseInt(allInputs[i].value);
		}
	}

	document.getElementById(totalId + "1").innerHTML = j;
	document.getElementById(totalId + "2").innerHTML = j;
}


function updateTotal(theEle){
	if (theEle.className.indexOf(" ") > 0){
		updateGroupTotals(theEle);
	}

	var allInputs = document.getElementsByClassName("ofxInput");
	var theEleVal = 0;

	if (!isNaN(parseInt(theEle.value))){
		theEleVal = Math.round(theEle.value);
		theEle.value = theEleVal;
	}
	else{
		theEle.value = "";
	}

	var j = 0;
		for (i=0; i < allInputs.length;i++){
			if (allInputs[i].value != "" && !isNaN(parseInt(allInputs[i].value))){
				j = j + parseInt(allInputs[i].value);
			}
		}

	if (j > 100){
		var testVal = (100 - (j - theEleVal));
		alert("The value you entered causes the total to exceed 100%. Please enter a value of "+ testVal +"% or less.")
		theEle.value = "";
		theEle.focus();
	}
	else{
		document.getElementById("ofxTotal").innerHTML = j;
	}
}



function updateTotal2(theEle){
	var allInputs = document.getElementsByClassName("ofxInput2");
	var theEleVal = 0;
	
	if (!isNaN(parseInt(theEle.value))){
		theEleVal = Math.round(theEle.value);
		theEle.value = theEleVal;
	}
	else{
		theEle.value = "";
	}
	
	var j = 0;
	for (i=0; i < allInputs.length;i++){
		if (allInputs[i].value != "" && !isNaN(parseInt(allInputs[i].value))){
			j = j + parseInt(allInputs[i].value);
		}
	}
	
	if (j > 100){	
		var testVal = (100 - (j - theEleVal));	
		alert("The value you entered causes the total to exceed 100%. Please enter a value of "+ testVal +"% or less.")
		theEle.value = "";
		theEle.focus();
	}
	else{
		document.getElementById("ofxTotal2").innerHTML = j;
	}
}

