//<script Language="JavaScript">
//****************************************************************************************
//GLOBAL VARIABLES
//****************************************************************************************
var	EventArray	= new Array();
var	SourceArray	 = new Array();
var	SecondSourceArray =	new	Array();

var	EventReady = false;
var	SourceReady	= false;
var	SecondSourceReady =	false;

var	e_windowHandler	= null;
var	s_windowHandler	= null;
var	ss_windowHandler = null;

var allevent
var allsource
var all2ndsource
var codesselected

// Object constructor 
function codeitem(code,	active)	{ 
   this.code = code;
   this.active = active;
}

//****************************************************************************************
//EVENT	FUNCTIONS 
//****************************************************************************************
function AddEvent(theCode){
	EventArray[EventArray.length] =	new	codeitem(theCode, true);
}
//**********************************************************************
function DeleteEvent(theCode){
	var	x =	0;
	var	CodeDeleted	= false;
	while(x	< EventArray.length	&& CodeDeleted == false){
		if (EventArray[x].code == theCode && EventArray[x].active == true){
			EventArray[x].active = false;
			CodeDeleted	= true;
		}
		x++;
	}
}
//**********************************************************************
function UpdateEvents(){
	document.forms[0].elements["event"].value =	EventString();
	if(document.forms[0].elements["event"].value == ""){
		document["eventstatus"].src = allevent.src;
	}else{
		document["eventstatus"].src = codesselected.src;
	}
}
//***********************************************************************
function EventString(){
	var	tmpCodes = new Array();
	
	var	x;
	for(x =	0; x < EventArray.length; x++){
		if (EventArray[x].active ==	true){
			tmpCodes[tmpCodes.length] =	EventArray[x].code;
		}
	}
	return tmpCodes.toString();
}
//***********************************************************************
function SortEvents(){
	EventArray.sort(compareCodes)
}
//***********************************************************************
function compareCodes(a, b)	{return	a.code - b.code}
//***********************************************************************
function eventCodeListed(theCode){
	var	x=0;
	CodeExists = false;
	while(x<EventArray.length && CodeExists	== false){
		if (EventArray[x].code == theCode && EventArray[x].active == true){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}
//***********************************************************************
function showEventCodes(view_mode) {
	var	popupheight	= 480;
	var	popupwidth = 680;
	
	var	top_dist=((window.screen.availHeight/2)	- popupheight/2);
	var	left_dist =	((window.screen.availWidth/2) -	popupwidth/2);
	
	var	webpage_location = ((view_mode == 'tree')? 'treeofevents.asp':'eventselections1.asp');
	
	if (e_windowHandler	== null	|| EventReady == false){
		e_windowHandler	= window.open(webpage_location,'EventInfo','resizable=yes,'
									  + 'toolbar=no,scrollbars=yes,menubar=no,status=no,location=no,width='
								      +popupwidth+',height='+popupheight+',left='+left_dist+
								      ',top='+top_dist);
	}else{
		e_windowHandler.location = webpage_location;
	}
	
	e_windowHandler.focus();
	if (!e_windowHandler.opener) e_windowHandler.opener	= self;
}

//***********************************************************************
function notifyEvent(notify_value){
	EventReady=notify_value;
}
//****************************************************************************************
//SOURCE FUNCTIONS 
//****************************************************************************************
function AddSource(theCode){
	SourceArray[SourceArray.length]	= new codeitem(theCode,	true);
}
//**********************************************************************
function DeleteSource(theCode){
	var	x=0;
	var	CodeDeleted	= false;
	while(x<SourceArray.length && CodeDeleted == false){
		if (SourceArray[x].code	== theCode && SourceArray[x].active	== true){
			SourceArray[x].active =	false;
			CodeDeleted	= true;
		}
		x++;
	}
}
//**********************************************************************
function UpdateSources(){
	document.forms[0].elements["firstsource"].value	= SourceString();
	if(document.forms[0].elements["firstsource"].value == ""){
		document["sourcestatus"].src = allsource.src;
	}else{
		document["sourcestatus"].src = codesselected.src;
	}
}
//**********************************************************************
function SourceString(){
	var	tmpCodes = new Array();
	var	x;
	for(x=0; x<SourceArray.length; x++){
		if (SourceArray[x].active == true){
			tmpCodes[tmpCodes.length] =	SourceArray[x].code;
		}
	}
	return tmpCodes.toString();
}
//***********************************************************************
function SortSources(){
	SourceArray.sort(compareCodes)
}
//***********************************************************************
function sourceCodeListed(theCode){
	var	x = 0;
	var	CodeExists = false;;
	while(x < SourceArray.length && CodeExists ==	false){
		if (SourceArray[x].code	== theCode && SourceArray[x].active	== true){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}
//***********************************************************************
function showSourceCodes(view_mode) {
	var	popupheight	= 480;
	var	popupwidth = 680;
	
	var	top_dist=((window.screen.availHeight/2)	- popupheight/2);
	var	left_dist =	((window.screen.availWidth/2) -	popupwidth/2);
	
	var	webpage_location = ((view_mode == 'tree')? 'treeofsources.asp':'sourceselections1.asp');

	if (s_windowHandler	== null	|| SourceReady == false){
		s_windowHandler	= window.open(webpage_location,'SourceTree','resizable=yes,'
									  + 'toolbar=no,scrollbars=yes,menubar=no,status=no,location=no,width='
								      +popupwidth+',height='+popupheight+',left='+left_dist+
								      ',top='+top_dist);
	}else{
		s_windowHandler.location = webpage_location;
	}
	
	s_windowHandler.focus();
	if (!s_windowHandler.opener) s_windowHandler.opener	= self;
}
//***********************************************************************
function notifySource(notify_value){
	SourceReady=notify_value;
}
//****************************************************************************************
//SECONDARY	SOURCE FUNCTIONS 
//****************************************************************************************
function AddSecondSource(theCode){
	SecondSourceArray[SecondSourceArray.length]	= new codeitem(theCode,	true);
}
//**********************************************************************
function DeleteSecondSource(theCode){
	var	x=0;
	var	CodeDeleted	= false;
	while(x<SecondSourceArray.length &&	CodeDeleted	== false){
		if (SecondSourceArray[x].code == theCode &&	SecondSourceArray[x].active	== true){
			SecondSourceArray[x].active	= false;
			CodeDeleted	= true;
		}
		x++;
	}
}
//**********************************************************************
function UpdateSecondSources(){
	document.forms[0].elements["secondsource"].value = SecondSourceString();
	if(document.forms[0].elements["secondsource"].value == ""){
		document["secondsourcestatus"].src = all2ndsource.src;
	}else{
		document["secondsourcestatus"].src = codesselected.src;
	}
}
//***********************************************************************
function SecondSourceString(){	
	var	tmpCodes = new Array();
	var	x;
	for(x=0; x<SecondSourceArray.length; x++){
		if (SecondSourceArray[x].active	== true){
			tmpCodes[tmpCodes.length] =	SecondSourceArray[x].code;
		}
	}
	return tmpCodes.toString();
}
//***********************************************************************
function SortSecondSources(){
	SecondSourceArray.sort(compareCodes)
}
//***********************************************************************
function secondSourceCodeListed(theCode){
	var	x=0;
	var	CodeExists;
	CodeExists = false;
	while(x<SecondSourceArray.length &&	CodeExists == false){
		if (SecondSourceArray[x].code == theCode &&	SecondSourceArray[x].active	== true){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}
//***********************************************************************
function showSecondSourceCodes(view_mode)	{
	var	popupheight	= 480;
	var	popupwidth = 690;
	
	var	top_dist=((window.screen.availHeight/2)	- popupheight/2);
	var	left_dist =	((window.screen.availWidth/2) -	popupwidth/2);
	
	var	webpage_location = ((view_mode == 'tree')? 'treeofsecondsources.asp':'secondsourceselections1.asp');

	if (ss_windowHandler == null || SecondSourceReady == false){
		ss_windowHandler = window.open(webpage_location,'SecondSourceTree','resizable=yes,'
									  + 'toolbar=no,scrollbars=yes,menubar=no,status=no,location=no,width='
								      +popupwidth+',height='+popupheight+',left='+left_dist+
								      ',top='+top_dist);
	}else{
		ss_windowHandler.location = webpage_location;
	}
	
	ss_windowHandler.focus();				
	if (!ss_windowHandler.opener) ss_windowHandler.opener =	self;
}
//***********************************************************************
function notifySecondSource(notify_value){
	SecondSourceReady=notify_value;
}

//**********************************************************************************
//SHARED FUNCTIONS
//**********************************************************************************
function reloadAll(){
	EventArray = ReloadArray("event");
	SourceArray	= ReloadArray("firstsource");
	SecondSourceArray =	ReloadArray("secondsource");
	
	allevent= new Image();
	allsource= new Image();
	all2ndsource= new Image();
	codesselected= new Image();
	
	allevent.src = "./images/allevents.gif";
	allsource.src  = "./images/allsources.gif";
	all2ndsource.src  = "./images/all2ndsources.gif";
	codesselected.src  = "./images/selectedcodes.gif";
	
	if(document.forms[0].elements["event"].value == ""){
		document["eventstatus"].src = allevent.src;
	}else{
		document["eventstatus"].src = codesselected.src;
	}
		
	if(document.forms[0].elements["firstsource"].value == ""){
		document["sourcestatus"].src = allsource.src;
	}else{
		document["sourcestatus"].src = codesselected.src;
	}
	
	if(document.forms[0].elements["secondsource"].value == ""){
		document["secondsourcestatus"].src = all2ndsource.src;
	}else{
		document["secondsourcestatus"].src = codesselected.src;
	}
}
//************************************************************************
function ReloadArray(SourceField){
	var	returnArray	= new Array();
	if(document.forms[0].elements[SourceField].value !=	''){
		var	tmpArray  =	document.forms[0].elements[SourceField].value.split(',');

		var	tmpIndex = 0;
		for(tmpIndex = 0; tmpIndex < tmpArray.length; tmpIndex ++){
			   returnArray[returnArray.length] = new codeitem(tmpArray[tmpIndex], true);
		}	
	}
	return returnArray;
}
//************************************************************************
function CompactArray(theArray){
	var	tmpArray  =	new	Array();
	var	tmpIndex = 0;
	for(tmpIndex = 0; tmpIndex < theArray.length; tmpIndex ++){
		if (theArray[tmpIndex].active == true){
		   tmpArray[tmpArray.length] = new codeitem(theArray[tmpIndex].code, theArray[tmpIndex].active);
		}
	}	
	return tmpArray;
}
//************************************************************************
function Level1HasChildren(theCode,	theArray){
	var	x=0;
	var	CodeExists;
	CodeExists = false;
	
	//window.alert(theCode);
	//window.alert(theCode*1000);
	//window.alert(((theCode+1)*1000));
	
	while(x	< theArray.length && CodeExists	== false){
		if (theArray[x].code >=	(theCode*1000) && 
			theArray[x].code < ((theCode+1)*1000) &&
			theArray[x].code !=	(10999)	&& 
			theArray[x].active == true ){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}
//***********************************************************************
function Level2HasChildren(theCode,	theArray){
	var	x=0;
	var	CodeExists;
	CodeExists = false;
	
	//window.alert(theCode*100);
	//window.alert((theCode+1)*100);
	
	while(x<theArray.length	&& CodeExists == false){
		if (theArray[x].code >=	(theCode*100) && 
			theArray[x].code < ((theCode+1)*100) &&	
			theArray[x].active == true ){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}
//***********************************************************************
function Level3HasChildren(theCode,	theArray){
	var	x=0;
	var	CodeExists;
	CodeExists = false;
	
	//window.alert(theCode);
	//window.alert(theCode*10);
	//window.alert((theCode+1)*10);
	
	while(x<theArray.length	&& CodeExists == false){
		if (theArray[x].code >=	(theCode*10) &&	
			theArray[x].code < ((theCode+1)*10)	&& 
			theArray[x].active == true ){
			CodeExists = true;
		}
		x++;
	}
	return CodeExists;
}

//**********************************************************************************
//OTHER	FUNCTIONS
//**********************************************************************************
function closeAll(){
	if (SecondSourceReady){
		ss_windowHandler.turnNotifyOff();
		ss_windowHandler.close();
	}
	if(SourceReady){
		s_windowHandler.turnNotifyOff();
		s_windowHandler.close();
	}
	if(EventReady){
		e_windowHandler.turnNotifyOff();
		e_windowHandler.close();
	}
	//window.alert("closing")
}
//</script>