// Event Handler functions by (c)2007 Paul Imisi for The Academy of Royal Arts Ltd.
// used for handling form element events

function setRange(thisChk,chk,theRow,delMin,delMax) {

	//theRow = document.getElementById(theRow);
	//delMin = document.getElementById(delMin);
	//delMax = document.getElementById(delMax);
	
	if(chk.length!=undefined) { 
		l=chk.length;
		for(k=0;k<l;k++) {
			if(chk[k].checked) {
				if(thisChk.value==chk[k].value) {
					if ((delMin.value=='')||(delMin.value==0)||(theRow[k].value<delMin.value)) {
						delMin.value=theRow[k].value
					}
					if (theRow[k].value>delMax.value) {
						delMax.value=theRow[k].value
					}
				}
			}
		}
	} else {
		if(chk.checked) {
			if(thisChk.value==chk.value) {
				if ((delMin.value=='')||(delMin.value==0)||(theRow.value<delMin.value)) {
					delMin.value=theRow.value
				}
				if (theRow.value>delMax.value) {
					delMax.value=theRow.value
				}
			}
		}
	}
	
}

function formSubmit() {
	dform.submit();
}

function formAction(selection) {
	selection=document.getElementById(selection);
	if (selection.value>0) {
		//document.theForm.action=formAction;
		formSubmit();
	} else {
		//document.theForm.action=formActionDefault;
	}
}

function getRangeQuantity(upperRange, lowerRange, targetOutput, additionalValue, isHTML) {
	var rangeQuantity;
	var upperR;
	var lowerR;
	
	if(additionalValue) {
		additionalValue = parseInt(additionalValue) * 1.0;	
	} else {
		additionalValue = 0;	
	}
	
	upperR = (parseInt(upperRange.value) * 1.0);
	lowerR = (parseInt(lowerRange.value) * 1.0)
	
	if((upperR >=0) && (lowerR >=0)) {
		rangeQuantity = (upperR - lowerR) + additionalValue;
	
		if(!isHTML) {
			targetOutput.value = "This" + upperR + lowerR + rangeQuantity;
		} else {
			targetOutput.innerHTML = rangeQuantity;
		}
	}
}

function validateUniquePart(thisField, targetField) {
	if(thisField.checked==true) {
		targetField.checked=false;
		targetField.disabled=true;
			
	} else {
		targetField.disabled=false;	
	}
}

function hideShowLayer(targetLayer, layerState) {
	targetLayer.style.display = layerState;
}

function validateCheckBox(refChk, chk, chkDisabled, chkChecked) {
	//chk.disabled=((!toggleRow(this, tdVB, tdVBL))?false:true);
	chkChecked='';
}

function toggleDiv(clickImage,targetLayer,retainValue,imgMore,imgLess) {
	if(!imgMore) { imgMore = '../images/more.gif'; }
	if(!imgLess) { imgLess = '../images/less.gif'; }
	clickImage = document.getElementById(clickImage);
	targetLayer = document.getElementById(targetLayer);
	if(retainValue) {
		retainValue = document.getElementById(retainValue);
	}
	targetLayer.style.display = ((targetLayer.style.display=='none')?'block':'none');
	clickImage.src = ((targetLayer.style.display=='none')?imgMore:imgLess);
	if(retainValue) {
		retainValue.value = ((targetLayer.style.display=='none')?0:1);
	}
}

function toggleDiv2(clickText,targetLayer,retainValue,textMore,textLess) {
	if(!textMore) { textMore = 'more &raquo;'; }
	if(!textLess) { textLess = '&laquo; less'; }
	clickText = document.getElementById(clickText);
	targetLayer = document.getElementById(targetLayer);
	if(retainValue) {
		retainValue = document.getElementById(retainValue);
	}
	targetLayer.style.display = ((targetLayer.style.display=='none')?'block':'none');
	clickText.innerHTML = ((targetLayer.style.display=='none')?textMore:textLess);
	if(retainValue) {
		retainValue.value = ((targetLayer.style.display=='none')?0:1);
	}
}

function toggleDiv3(layer1,layer2) {
	//layer1 = document.getElementById(layer1);
	//layer2 = document.getElementById(layer2);

	layer1.style.display = 'none';
	layer2.style.display = 'block';
}


function openIn(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function doAddNewListItem(lst,txt,chk,divLayer) {
	
	if(chk.checked==true) { 
		lst.value='';
		lst.disabled=true; 
		txt.disabled=false; 
		divLayer.style.display='block'; 
	} else { 
		lst.disabled=false; 
		txt.disabled=true;
		divLayer.style.display='none'; 
	}

}

function ignoreValue(txtThis,chkIgnore)
{
    //txtThis=document.getElementById(txtThis);
	chkIgnore=document.getElementById(chkIgnore);
	var intext = txtThis.value; 
	
	if(intext!='') { 
		chkIgnore.checked=false; 
	} else { 
		chkIgnore.checked=true; 
	}  
}
/*function toggleRow(thisField, chkValue, firstRow, secondRow)
{
	//if(cType) {
		if(thisField.checked==true) { firstRow.style.display='block'; secondRow.style.display='none'; } else { firstRow.style.display='block'; secondRow.style.display='none'; }
	//} else {
		//if(thisField.value==chkValue) { firstRow.style.display='block'; secondRow.style.display='none'; }
	//}
}*/

function toggleRow(thisField, firstRow, secondRow, inverseVal)
{
	if(!inverseVal) { inverseVal=0; }
	if(thisField.checked==true) { 
		
		if(firstRow) {
			if(firstRow.length!=undefined) { 
				fl=firstRow.length;
				for(i=0;i<fl;i++) {
					firstRow[i].style.display=((inverseVal==1)?'none':'block'); 
					
				}
			} else {
				firstRow.style.display=((inverseVal==1)?'none':'block'); 
				
			}
		}
		if(secondRow) {
			if(secondRow.length!=undefined) { 
				sl=secondRow.length;
				for(k=0;k<sl;k++) {
					secondRow[k].style.display=((inverseVal==1)?'block':'none'); 
						return true;
				}
			} else {
				secondRow.style.display=((inverseVal==1)?'block':'none'); 
				return true;
			}
		}
	} else { 
		
		if(firstRow) {
			if(firstRow.length!=undefined) { 
				fl=firstRow.length;
				for(i=0;i<fl;i++) {
					firstRow[i].style.display=((inverseVal==1)?'block':'none');
					
				}
			} else {
				firstRow.style.display=((inverseVal==1)?'block':'none');
				
			}
		}
		
		if(secondRow) {
			if(secondRow.length!=undefined) { 
				sl=secondRow.length;
				for(k=0;k<sl;k++) {
					secondRow[k].style.display=((inverseVal==1)?'block':'none'); 
					return false;
				}
			} else {
				secondRow.style.display=((inverseVal==1)?'block':'none'); 
				return false;
			}
		}
		//secondRow.style.display='none';
	}
}

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

function toggleRowDisplay(thisField, chkValue, targetRow, displayState, displayElse)
{
	var chkValArr;
	chkValArr = chkValue;
	chkValArr = chkValArr.split(" ")

	if(targetRow.length!=undefined) { 
		rlen=targetRow.length;
		for(i=0;i<rlen;i++) {
			if(thisField.value in oc(chkValArr) ) { 
			//if(thisField.value==chkValue) { 
				targetRow[i].style.display=displayState; 
			} else { 
				if((displayElse)&&(displayElse!='')) { 
					targetRow[i].style.display=displayElse;
				}
			}
		}
	} else {
		if(thisField.value in oc(chkValArr) ) { 
		//if(thisField.value==chkValue) { 
			targetRow.style.display=displayState; 
		} else { 
			if((displayElse)&&(displayElse!='')) {  
				targetRow.style.display=displayElse;
			}
		}
	}
}

function sumFields(thisField, fieldArray, resultField, maxAllowable, outputDisplay) 
{
	var theSum = 0, remValue=0;
	if(fieldArray.length!=undefined) { 
		l=fieldArray.length;
		for(k=0;k<l;k++) {
			theSum = ((theSum * 1) + (fieldArray[k].value * 1));
		}
	} else {
		theSum = (fieldArray.value * 1);
	}
	//resultField.value = theSum;
	if(theSum>maxAllowable) {
		remValue = ((maxAllowable * 1) - ((theSum * 1) - thisField.value));
		alert("The value of your current selection raises the total value above the acceptable maximum of " + maxAllowable + ".\t \nThe maximum accetable remainder is: " + remValue + ". This will be automatically set for you.");
		thisField.value = remValue;
		theSum = 0;
		if(fieldArray.length!=undefined) { 
			l=fieldArray.length;
			for(k=0;k<l;k++) {
				theSum = ((theSum * 1) + (fieldArray[k].value * 1));
			}
		} else {
			theSum = (fieldArray.value * 1);
		}
		resultField.value = theSum;
		outputDisplay.innerHTML = "<font color=\'#FF0000\'><strong>" + theSum + "</strong></font>";
	} else {
		resultField.value = theSum;
		outputDisplay.innerHTML = "<font color=\'#FF0000\'><strong>" + theSum + "</strong></font>";
	}
}

function toggleRowColor(thisRow,presentColorHolder,NewColor, isRestore, clickHolder)
{
	if(isRestore==0) {
		if(clickHolder.value!=1) {
			presentColorHolder.value = thisRow.style.backgroundColor;
			thisRow.style.backgroundColor=NewColor; 
		}
	} else if(isRestore==2) {
		if(clickHolder.value==0) {
			clickHolder.value=1;
			presentColorHolder.value = presentColorHolder.value
			thisRow.style.backgroundColor=NewColor; 
		} else {
			clickHolder.value=0;
			thisRow.style.backgroundColor=presentColorHolder.value;
		}
	} else {
		if(clickHolder.value!=1) {
			thisRow.style.background=presentColorHolder.value;
		}
	}
}

function controlField(thisKey, fieldArray) 
{
	if(fieldArray.length!=undefined) { 
		l=fieldArray.length;
		for(k=0;k<l;k++) {
			if((k * 1)==(thisKey*1)) {
				fieldArray[k].disabled=false;
			} else {
				fieldArray[k].value='';
				fieldArray[k].disabled=true;
			}
		}
	}
}

function checkControlField(thisCheck, theField) 
{
	if(thisCheck.checked==true) { 
		theField.disabled=false;
	} else {
		theField.value='';
		theField.disabled=true;
	}
}

function listValues(fieldArray, listValue, controlType, theRow)
{
	listValue.value = '';
	var kpass = 0;
	//alert("Starting List");
	if(!controlType) {
		controlType = 0;
	}
	//control types 0:listSelect; 1:checkBox; 2:hiddenfield FFBBFF
	if(fieldArray.length!=undefined) { 
		//alert("List is in Array");
		l=fieldArray.length;
		for(k=0;k<l;k++) {
			switch (controlType) {
				case 0:
				if(fieldArray[k].selected==true) {
					//alert(K + " is Checked");
					if(kpass==0) {
						listValue.value = fieldArray[k].value;
						
					} else {
						listValue.value = listValue.value + ', ' + fieldArray[k].value;
					}
					if(theRow!=undefined) {
						theRow[k].style.backgroundColor='#FFBBFF';
					}
					kpass++;
				} else {
					if(theRow!=undefined) {
						theRow[k].style.backgroundColor='#FFFFFF';
					}
				}
				case 1:
				if(fieldArray[k].checked==true) {
					//alert(K + " is Checked");
					if(kpass==0) {
						listValue.value = fieldArray[k].value;
					} else {
						listValue.value = listValue.value + ', ' + fieldArray[k].value;
					}
					if(theRow!=undefined) {
						theRow[k].style.backgroundColor='#FFBBFF';
					}
					kpass++;
				} else {
					if(theRow!=undefined) {
						theRow[k].style.backgroundColor='#FFFFFF';
					}
				}
			}	//switch
		}
	} else {
		switch (controlType) {
			case 0:
			if(fieldArray.selected==true) {
				listValue.value = fieldArray.value;
				theRow.style.backgroundColor='#FFBBFF';
			} else {
				listValue.value = '';
				if(theRow!=undefined) {
					theRow.style.backgroundColor='#FFFFFF';
				}
			}
			case 1:
			if(fieldArray.checked==true) {
				listValue.value = fieldArray.value;
				if(theRow!=undefined) {
					theRow.style.backgroundColor='#FFBBFF';
				}
			} else {
				listValue.value = '';
				if(theRow!=undefined) {
					theRow.style.backgroundColor='#FFFFFF';
				}
			}
		}	//switch
			//listValue.value = fieldArray.value;
	}
}

function sumCreditUnit(thisField, fieldArray, resultField, maxAllowable, courseID, selectedCourses, outputDisplay) 
{
	//if(!initialTotal) { initialTotal = 0; }
	var theSum = 0, remValue=0;
	maxAllowable = parseInt(maxAllowable);
	var courseList;
	courseList = '';
	if(fieldArray.length!=undefined) { 
		l=fieldArray.length;
		//alert(thisField.value);
		for(k=0;k<l;k++) {
			if(fieldArray[k].checked==true) {
			
				theSum = ((theSum * 1) + (fieldArray[k].value * 1));
				if(courseID[k].value!='') {
					courseList = ((courseList=='')?courseID[k].value:courseList + ',' + courseID[k].value);
				}
			}
		}
	} else {
		
		theSum = (fieldArray.value * 1);
		courseList = courseID.value
	}
	selectedCourses.value = courseList;//((requiredCourse.value!='')?requiredCourse.value + ((courseList!='')?',' + courseList:''):courseList);
	theSum = parseInt(theSum);
	
	if(theSum>maxAllowable) {
		remValue = ((maxAllowable * 1) - ((theSum * 1) - thisField.value));
		
		alert("The value of your current selection raises your total credit unit above the acceptable maximum of " + maxAllowable + ".\t \nThe maximum accetable remainder is: " + remValue + ". \t\n\nPlease choose a course or a combination of courses with a total credit unit less or equal to " + remValue + ".");

		thisField.checked = false;
		theSum = 0;
		if(fieldArray.length!=undefined) { 
			l=fieldArray.length;
			for(k=0;k<l;k++) {
				if(fieldArray[k].checked==true) {
					theSum = ((theSum * 1) + (fieldArray[k].value * 1));
					if(courseID[k].value!='') {
						courseList = ((courseList=='')?courseID[k].value:courseList + ',' + courseID[k].value);
					}
				}
			}
		} else {
			if(fieldArray.checked==true) {
				theSum = (fieldArray.value * 1);
				courseList = courseID.value
			}
		}
		theSum = parseInt(theSum);
		resultField.value = theSum;
		outputDisplay.innerHTML = "<font color=\'#FF0000\'><strong>" + theSum + "</strong></font>";
		selectedCourses.value = courseList;//((requiredCourse.value!='')?requiredCourse.value + ((courseList!='')?',' + courseList:''):courseList);
	} else {
		theSum = parseInt(theSum);
		resultField.value = theSum;
		outputDisplay.innerHTML = "<font color=\'#FF0000\'><strong>" + theSum + "</strong></font>";
		selectedCourses.value = courseList;//((requiredCourse.value!='')?requiredCourse.value + ((courseList!='')?',' + courseList:''):courseList);
	}
}





