/* TopStrap_auto_x.cpp - sample extra source file for project TopStrap_auto */

#include "stdafx.h"
#include "TopStrap_auto.h"

/* TODO:	place references to your global functions and global variables in the TopStrap_auto.h file
			write additional functions below as required
			place private variables and function declarations here
*/

bncs_string getObjectSetting(const bncs_string& strObject, const bncs_string& strSetting){
	bncs_config cfg(bncs_string("object_settings.%1.%2").arg(strObject).arg(strSetting));
	bncs_string strValue = "";
	if(cfg.isValid()){
		strValue = cfg.attr("value");
	}
	return strValue;
}

int getInstanceDevice(const bncs_string & instance) {
	bncs_config cfg(bncs_string("instances.%1").arg(instance));
	bncs_stringlist sltRef = cfg.attr("ref");
	return sltRef.getNamedParam("device").toInt();
}

int getRouterSize(int intDevice, int intSwitch) {
	char szFileName[32];
	char szKey[32];
	sprintf(szFileName, "dev_%03d.ini", intDevice);
	sprintf(szKey, "DatabaseSize_%d", intSwitch);
	int intSize = atoi(r_p(szFileName, "Database", szKey,"0",TRUE));
	return intSize;
}

void getAppSettings(){
	//Load settings for this system
	bncs_string strAppName = "topstrap";


	//1 get the device IDs
	intDeviceSDI = getInstanceDevice("sdi");
//	intDeviceHD = getInstanceDevice("hd_router");
//	intDeviceAES = getInstanceDevice("aes_router");
//	intDeviceStuder = getInstanceDevice("studer_router");
//	intDeviceTieline = getInstanceDevice("tieline_manage");
	intDeviceMGNOutputs = getInstanceDevice("mgn_gpi_outputs");

	//2 get the source size of the AES, Studer and "tieline manager" databases
//	intAESRouterSize = getRouterSize(intDeviceAES, 0);
//	intTielineRouterSize = getRouterSize(intDeviceTieline, 0);
//	intStuderRouterSize = getRouterSize(intDeviceStuder, 0);

	//3 get the router indexes
//	intAES_SRCE_TONE1 = getObjectSetting(strAppName, "aes_srce_tone1");
//	intAES_SRCE_TONE2 = getObjectSetting(strAppName, "aes_srce_tone2");
//	intAES_SRCE_DESK1 = getObjectSetting(strAppName, "aes_srce_desk1");
//	intTielineSourceDesk1 = intAES_SRCE_DESK1 + intStuderRouterSize;
//	intAES_SRCE_DESK2 = getObjectSetting(strAppName, "aes_srce_desk2");
//	intTielineSourceDesk2 = intAES_SRCE_DESK2 + intStuderRouterSize;

//	intAES_DEST_CCA_M1 = getObjectSetting(strAppName, "aes_dest_cca_m1");
//	intAES_DEST_CCA_M2 = getObjectSetting(strAppName, "aes_dest_cca_m2");	
//	intAES_DEST_CCA_RES1 = getObjectSetting(strAppName, "aes_dest_cca_res1");	
//	intAES_DEST_CCA_RES2 = getObjectSetting(strAppName, "aes_dest_cca_res2");	
//	intAES_DEST_BYPASS1 = getObjectSetting(strAppName, "aes_dest_bypass1");		
//	intAES_DEST_BYPASS2 = getObjectSetting(strAppName, "aes_dest_bypass2");		

//	intSTUDER_SRCE_SILENCE = getObjectSetting(strAppName, "studer_srce_silence");
//	intSTUDER_SRCE_PROGRAM = getObjectSetting(strAppName, "studer_srce_main_prog");
//	intSTUDER_DEST_INT_PROG_CUT = getObjectSetting(strAppName, "studer_dest_int_prog_cut");		
//	intSTUDER_DEST_EXT_PROG_CUT = getObjectSetting(strAppName, "studer_dest_ext_prog_cut");		
//	intSTUDER_DEST_EXT_PROG_CUT = getObjectSetting(strAppName, "studer_dest_ext_prog_cut");		
	int_MAX_TX_FLASH = getObjectSetting(strAppName, "tx_lights_flash_time");		

	intSDI_SRCE_BARS = getObjectSetting(strAppName, "sdi_srce_bars");	
	intSDI_SRCE_MIXER_OUT = getObjectSetting(strAppName, "sdi_srce_mixer_out");	
	intSDI_SRCE_MIXER_RES = getObjectSetting(strAppName, "sdi_srce_mixer_res");	
	intSDI_SRCE_BYPASS = getObjectSetting(strAppName, "sdi_srce_bypass");	
	intSDI_SRCE_BYPASS_B = getObjectSetting(strAppName, "sdi_srce_bypass_B");
	intSDI_SRCE_BYPASS_RES = getObjectSetting(strAppName, "sdi_srce_bypass_Res");

	intSDI_DEST_CCA_LINE = getObjectSetting(strAppName, "sdi_dest_cca_line");
	intSDI_DEST_CCA_LINE_B = getObjectSetting(strAppName, "sdi_dest_cca_line_B");
	intSDI_DEST_CCA_RES1 = getObjectSetting(strAppName, "sdi_dest_cca_res1");	
	intSDI_DEST_BYPASS = getObjectSetting(strAppName, "sdi_dest_bypass");
	intSDI_DEST_BYPASS_RES = getObjectSetting(strAppName, "sdi_dest_bypass_res");

//	intHD_SRCE_BARS = getObjectSetting(strAppName, "hd_srce_bars");	
//	intHD_SRCE_MIXER_OUT = getObjectSetting(strAppName, "hd_srce_mixer_out");	
//	intHD_SRCE_MIXER_RES = getObjectSetting(strAppName, "hd_srce_mixer_res");	
//	intHD_SRCE_BYPASS = getObjectSetting(strAppName, "hd_srce_bypass");	

//	intHD_DEST_CCA_LINE = getObjectSetting(strAppName, "hd_dest_cca_line");	
//	intHD_DEST_CCA_RES1 = getObjectSetting(strAppName, "hd_dest_cca_res1");	
//	intHD_DEST_BYPASS = getObjectSetting(strAppName, "hd_dest_bypass");		

	
	intMGN_OUTPUT_REH = getObjectSetting(strAppName, "mgn_output_reh");				
	intMGN_OUTPUT_TX = getObjectSetting(strAppName, "mgn_output_tx");				

	sltMGN_OUTPUT_REH_LIST = bncs_stringlist("").fromString(getObjectSetting(strAppName, "mgn_output_reh_list"));	
	sltMGN_OUTPUT_TX_LIST = bncs_stringlist("").fromString(getObjectSetting(strAppName, "mgn_output_tx_list"));	

	//Lists below are switched on except when "Flr Off" is enabled
	sltMGN_OUTPUT_REH_FLOOR = bncs_stringlist("").fromString(getObjectSetting(strAppName, "mgn_output_reh_floor"));	
	sltMGN_OUTPUT_TX_FLOOR = bncs_stringlist("").fromString(getObjectSetting(strAppName, "mgn_output_tx_floor"));	

	//The list of MGN outputs to be set when bypassAV is on
	sltMGN_OUTPUT_BYPASS_LIST = bncs_stringlist("").fromString(getObjectSetting(strAppName, "mgn_output_bypass"));	
	intMGN_OUTPUT_BYPASS = -1;  // unknown as of version 1.22

	//Added by Simon
	sltMGN_OUTPUT_VENT_OFF = getObjectSetting(strAppName,"mgn_vent_off");
	sltMGN_OUTPUT_VTTONE16 = getObjectSetting(strAppName,"mgn_tone_vt_1_6");
	sltMGN_OUTPUT_VTONE712 = getObjectSetting(strAppName,"mgn_tone_vt_7_12");
	sltMGN_OUTPUT_TONEM1 = getObjectSetting(strAppName,"mgn_tone_line");
	sltMGN_OUTPUT_TONERES1 = getObjectSetting(strAppName,"mgn_tone_line_res");
	sltMGN_OUTPUT_INT_CUT = getObjectSetting(strAppName, "mgn_int_cut");
	sltMGN_OUTPUT_EXT_CUT = getObjectSetting(strAppName, "mgn_ext_cut");


	//Check they are OK
	Debug("Video Resources");
	Debug("   sdi_router=%d", intDeviceSDI);
	Debug("   sdi_srce_mixer_out=%d", intSDI_SRCE_MIXER_OUT);
	Debug("   sdi_dest_cca_line=%d", intSDI_DEST_CCA_LINE);
	Debug("   sdi_dest_cca_res1=%d", intSDI_DEST_CCA_RES1);
	Debug("   sdi_dest_bypass=%d", intSDI_DEST_BYPASS);

//	Debug("   hd_router=%d", intDeviceHD);
//	Debug("   hd_srce_mixer_out=%d", intHD_SRCE_MIXER_OUT);
//	Debug("   hd_dest_cca_line=%d", intHD_DEST_CCA_LINE);
//	Debug("   hd_dest_cca_res1=%d", intHD_DEST_CCA_RES1);
//	Debug("   hd_dest_bypass=%d", intHD_DEST_BYPASS);

	
//	Debug("Audio Resources");
//	Debug("   aes_router=%d - source count=%d", intDeviceAES, intAESRouterSize);
//	Debug("   studer_router=%d - source count=%d", intDeviceStuder, intStuderRouterSize);
//	Debug("   tieline_router=%d - source count=%d", intDeviceTieline, intTielineRouterSize);

//	Debug("   aes_src_tone1=%d", intAES_SRCE_TONE1);
//	Debug("   aes_src_tone2=%d", intAES_SRCE_TONE2);
//	Debug("   aes_dest_cca_m1=%d", intAES_DEST_CCA_M1);
//	Debug("   aes_dest_cca_m2=%d", intAES_DEST_CCA_M2);
//	Debug("   aes_dest_cca_res1=%d", intAES_DEST_CCA_RES1);
//	Debug("   aes_dest_cca_res2=%d", intAES_DEST_CCA_RES2);
//	Debug("   aes_dest_bypass1=%d", intAES_DEST_BYPASS1);
//	Debug("   aes_dest_bypass2=%d", intAES_DEST_BYPASS2);
//	Debug("   studer_srce_silence=%d", intSTUDER_SRCE_SILENCE);
//	Debug("   studer_srce_program=%d", intSTUDER_SRCE_PROGRAM);
//	Debug("   studer_dest_int_prog_cut=%d", intSTUDER_DEST_INT_PROG_CUT);
//	Debug("   studer_dest_ext_prog_cut=%d", intSTUDER_DEST_EXT_PROG_CUT);


	#define	HWY_NEAR_SRCE	0
	#define	HWY_NEAR_DEST	1
	#define	HWY_NEAR_ROUTER	2
	#define	HWY_FAR_DEST	3

//	bncs_string strHighway = "";
//	bncs_string strDebug;
//	bncs_stringlist sltHighway;
//	int intHighway = 0;
//	char szDebug[256];
//	bool blnMore = true;
//	while (blnMore){
//		intHighway++;
//		strHighway = getObjectSetting(bncs_string("allocated_highways_aes_router"), intHighway);
//		if(strHighway.length() > 0){
//			sltHighway = sltHighway.fromString(strHighway);
//			strDebug = bncs_string("allocated highway(%1) StuderDest=%2 to AESSrce=%3 for AESDest=%4")
//				.arg(intHighway).arg(sltHighway[HWY_FAR_DEST]).arg(sltHighway[HWY_NEAR_SRCE]).arg(sltHighway[HWY_NEAR_DEST]);
//			strcpy(szDebug, strDebug);
//
//			if(sltHighway[HWY_NEAR_DEST].toInt() == intAES_DEST_BYPASS1){
//				intAES_SRCE_HWY_BYPASS1	= sltHighway[HWY_NEAR_SRCE].toInt();
//				intSTUDER_DEST_HWY_BYPASS1 = sltHighway[HWY_FAR_DEST].toInt();
//				Debug("BypassAudition1 uses %s", szDebug);
//			}
//			if(sltHighway[HWY_NEAR_DEST].toInt() == intAES_DEST_BYPASS2){
//				intAES_SRCE_HWY_BYPASS2	= sltHighway[HWY_NEAR_SRCE].toInt();
//				intSTUDER_DEST_HWY_BYPASS2 = sltHighway[HWY_FAR_DEST].toInt();
//				Debug("BypassAudition2 uses %s", szDebug);
//			}
//		}
//		else{
//			//no more highways
//			blnMore = false;
//		}
//	}

	//Show MGN details
	Debug("MGN Cue Director Resources");
	Debug("   mgn_gpi_outputs=%d", intDeviceMGNOutputs);
	Debug("   mgn_output_reh=%d", intMGN_OUTPUT_REH);
	Debug("   mgn_output_tx=%d", intMGN_OUTPUT_TX);
	Debug(bncs_string("   mgn_output_reh_list=%1").arg(sltMGN_OUTPUT_REH_LIST.toString()));
	Debug(bncs_string("   mgn_output_tx_list=%1").arg(sltMGN_OUTPUT_TX_LIST.toString()));
	Debug(bncs_string("   mgn_output_reh_floor=%1").arg(sltMGN_OUTPUT_REH_FLOOR.toString()));
	Debug(bncs_string("   mgn_output_tx_floor=%1").arg(sltMGN_OUTPUT_TX_FLOOR.toString()));

	Debug("------------------------------------------------------------------------------------------------------");


}

void initStatusDialog(){
	//NOTE - see Win32 API docs on Listview
	//http://www.codeproject.com/listctrl/listview.asp
	
	//init listview
	hWndOutputsReh = GetDlgItem(hWndDlg, IDC_OUTPUTS_REH);
	hWndOutputsTX = GetDlgItem(hWndDlg, IDC_OUTPUTS_TX);

	char szBuf[256];
	
	if(hWndOutputsReh && hWndOutputsReh){
		LVITEM lvi;
		LVCOLUMN lvc;

		// Here we put the info on the Column headers
		// this is not data, only name of each header we like
		memset(&lvc,0,sizeof(lvc));                  // Zero Members
		lvc.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;    // Type of mask

		lvc.cx=0x32;                                   // width of column
		lvc.pszText="Output";                            // First Header Text
		SendMessage(hWndOutputsReh, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc); // Insert/Show the column
		SendMessage(hWndOutputsTX, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc); // Insert/Show the column

		lvc.cx=0x40;                                   // width of column
		lvc.pszText="On Floor";                            // First Header Text
		SendMessage(hWndOutputsReh, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc); // Insert/Show the column
		SendMessage(hWndOutputsTX, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc); // Insert/Show the column

		lvc.cx=0x50;                                   // width of column
		lvc.pszText="State";                            // First Header Text
		SendMessage(hWndOutputsReh, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc); // Insert/Show the column
		SendMessage(hWndOutputsTX, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc); // Insert/Show the column


		//Create the items
		memset(&lvi,0,sizeof(lvi)); // Zero struct's Members

		//  Setting properties Of members:
		lvi.mask = LVIF_TEXT;   // Text Style
		lvi.cchTextMax = 256; // Max size of test


		int intItem;

		//Add the rehearsal outputs
		sprintf( szBuf, " %d", intMGN_OUTPUT_REH);
		SetDlgItemText(hWndDlg, IDC_REH_OUTPUT, szBuf);
		for(intItem = 0; intItem < sltMGN_OUTPUT_REH_LIST.count(); intItem++){
			lvi.iItem = intItem;          // choose item  
			lvi.iSubItem = COL_OUTPUT;       // Put in first column
			strcpy(szBuf, sltMGN_OUTPUT_REH_LIST[intItem]);
			lvi.pszText = szBuf; // Text to display (can be from a char variable) (Items)
			SendMessage(hWndOutputsReh, LVM_INSERTITEM, 0, (LPARAM)&lvi); // Send info to the Listview

			//is this output on the floor?
			if(sltMGN_OUTPUT_REH_FLOOR.find(sltMGN_OUTPUT_REH_LIST[intItem]) > -1){
			   lvi.iSubItem = COL_ONFLOOR;
			   lvi.pszText = "yes";
			   SendMessage(hWndOutputsReh, LVM_SETITEM, intItem, (LPARAM)&lvi); // Enter text to SubItems
			}
		   lvi.iSubItem = COL_STATE;
		   //sprintf(szTemp, "SubItem %d", i);
		   lvi.pszText = "UNKNOWN";
		   SendMessage(hWndOutputsReh, LVM_SETITEM, intItem, (LPARAM)&lvi); // Enter text to SubItems
		}
	
		//Add the TX outputs
		sprintf( szBuf, " %d", intMGN_OUTPUT_TX);
		SetDlgItemText(hWndDlg, IDC_TX_OUTPUT, szBuf);
		for(intItem = 0; intItem < sltMGN_OUTPUT_TX_LIST.count(); intItem++){
			lvi.iItem = intItem;          // choose item  
			lvi.iSubItem = COL_OUTPUT;       // Put in first column
			strcpy(szBuf, sltMGN_OUTPUT_TX_LIST[intItem]);
			lvi.pszText = szBuf; // Text to display (can be from a char variable) (Items)
			SendMessage(hWndOutputsTX, LVM_INSERTITEM, 0, (LPARAM)&lvi); // Send info to the Listview

			//is this output on the floor?
			if(sltMGN_OUTPUT_TX_FLOOR.find(sltMGN_OUTPUT_TX_LIST[intItem]) > -1){
			   lvi.iSubItem = COL_ONFLOOR;
			   lvi.pszText = "yes";
			   SendMessage(hWndOutputsTX, LVM_SETITEM, intItem, (LPARAM)&lvi); // Enter text to SubItems
			}
		   lvi.iSubItem = COL_STATE;
		   //sprintf(szTemp, "SubItem %d", i);
		   lvi.pszText = "UNKNOWN";
		   SendMessage(hWndOutputsTX, LVM_SETITEM, intItem, (LPARAM)&lvi); // Enter text to SubItems
		}
	}
}

void setOutputItemState(int intOutput, int intState){
	hWndOutputsReh = GetDlgItem(hWndDlg, IDC_OUTPUTS_REH);
	hWndOutputsTX = GetDlgItem(hWndDlg, IDC_OUTPUTS_TX);
	char szBuf[256];
	bncs_stringlist sltStates = bncs_stringlist("OFF,ON,FORCE_OFF,FORCE_ON");

	if(hWndOutputsReh && hWndOutputsReh){
		//find the item
		if(sltMGN_OUTPUT_REH_LIST.find(intOutput) > -1){
		   int intItem = sltMGN_OUTPUT_REH_LIST.find(intOutput);
		   
		   strcpy(szBuf, sltStates[intState]);
		   ListView_SetItemText(hWndOutputsReh, intItem,COL_STATE, szBuf);
		}
		if(sltMGN_OUTPUT_TX_LIST.find(intOutput) > -1){
		   int intItem = sltMGN_OUTPUT_TX_LIST.find(intOutput);
		   
		   strcpy(szBuf, sltStates[intState]);
		   ListView_SetItemText(hWndOutputsTX, intItem,COL_STATE, szBuf);
		}
	}
}


void revSDICCALine(int intSource){
	intSDILineSource = intSource;
}

void revSDICCARes1(int intSource){
	intSDIRes1Source = intSource;
}

void revSDIBypass(int intSource){
	intSDIBypassSource = intSource;
}
//
//void revHDCCALine(int intSource){
//	intHDLineSource = intSource;
//}

//void revHDCCARes1(int intSource){
//	intHDRes1Source = intSource;
//}

//void revHDBypass(int intSource){
//	intHDBypassSource = intSource;
//}

//void revAESCCAM1(int intSource){
//	//char szSlot[12];
//	Debug("revAESCCAM1 source = %d", intSource);
//	intAESM1Source = intSource;
//	// if the src is the tone then flag it
//	if (intSource==intAES_SRCE_TONE1) {
//		// check to do this or not....
//		// set internal flag to on and colour top strap panel
//		//intAESToneM1State = GPI_ON;
//		//UpdateUI( SLOT_TONE_M1 );
//		//sprintf(szSlot, "%d", intState);
//		//ei.setslot(SLOT_TONE_M1 ,szSlot);
//	}
//	// else decolor if pgm or other src
//}
//void revAESCCAM2(int intSource){
//	intAESM2Source = intSource;
//}
//void revAESCCARES1(int intSource){
//	intAESRes1Source = intSource;
//}
//void revAESCCARES2(int intSource){
//	intAESRes2Source = intSource;
//}


//void revAESBYPASS1(int intSource){
//	intAESSourceBypass1 = intSource;
//	updateAudioBypassTally(1);
//}
//
//void revAESBYPASS2(int intSource){
//	intAESSourceBypass2 = intSource;
//	updateAudioBypassTally(2);
//}
//
//void revSTUDERBYPASS1(int intSource){
//	intStuderSourceBypass1 = intSource;
//	updateAudioBypassTally(1);
//}
//
//void revSTUDERBYPASS2(int intSource){
//	intStuderSourceBypass2 = intSource;
//	updateAudioBypassTally(2);
//}
//
// new ver 1.2
//void revINTPROGCUT(int intSource){
//	intINTProgCutSrc = intSource;
//	updateIntProgCut();
//}
//
//void revEXTPROGCUT(int intSource){
//	intEXTProgCutSrc = intSource;
//	updateExtProgCut();
//}

void revMGNOutputReh(int intValue){
	char szBuf[32];
	bncs_stringlist sltStates = bncs_stringlist(" OFF, ON, FORCE OFF, FORCE ON");
	strcpy(szBuf, sltStates[intValue]);
	SetDlgItemText(hWndDlg, IDC_REH_STATUS, szBuf);

	switch(intValue){
		//The rehearse value is either routed on or is forced on
		case GPI_ON:
		case GPI_FORCE_ON:	//note this will not be used in the final requirement
			setMGNOutputList(sltMGN_OUTPUT_REH_LIST, sltMGN_OUTPUT_REH_FLOOR, GPI_FORCE_ON);
			intRehState = intValue;
			break;
		case GPI_OFF:
		case GPI_FORCE_OFF:
			setMGNOutputList(sltMGN_OUTPUT_REH_LIST, sltMGN_OUTPUT_REH_FLOOR, GPI_FORCE_OFF);
			intRehState = intValue;
			break;
	}
}

void revMGNOutputTX(int intValue){
	char szBuf[32];
	bncs_stringlist sltStates = bncs_stringlist(" OFF, ON, FORCE OFF, FORCE ON");
	strcpy(szBuf, sltStates[intValue]);
	SetDlgItemText(hWndDlg, IDC_TX_STATUS, szBuf);

	switch(intValue){
		//The TX value is either routed on or is forced on
		case GPI_ON:
		case GPI_FORCE_ON:

			//Store new state for TX output
			intTXState = intValue;
			bTXLightsOn = TRUE;

			setMGNOutputList(sltMGN_OUTPUT_TX_LIST, sltMGN_OUTPUT_TX_FLOOR, GPI_FORCE_ON); // do this everytime 

			// need to execute this sequence on first flash or going into tx permanently from panel press
			//if (bStartTXMode==TRUE) {
			if (bTXFullMode==TRUE) {   // revised version only lock interlocks and reset values on entering FULL TX

				bStartTXMode=FALSE; // stays until end of TX mode ( flashing or otherwise )

				//setMGNOutputList(sltMGN_OUTPUT_TX_LIST, sltMGN_OUTPUT_TX_FLOOR, GPI_FORCE_ON); // do this everytime or just once ??

				//must remember what state the REH is in at this time
				if(intRehState == GPI_FORCE_ON){
					blnRehWasForced = true;
				}
				//must ensure that rehearse lights are now off
				infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_OFF, intMGN_OUTPUT_REH);

				// new v1.2
//				if(intINTProgCutState == GPI_ON ) {
//					routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_PROGRAM, intSTUDER_DEST_INT_PROG_CUT);
//				}
				//if bars to line is on then switch it off
				if(intSDIBarsLineState == GPI_ON){
					setBarsLine(GPI_OFF);
				}
				if(intSDIBarsRes1State == GPI_ON){
					setBarsRes1(GPI_OFF);
				}
				if(intVTTone16State == GPI_FORCE_ON){
					setTone(SLOT_TONE_VT1_6, GPI_FORCE_OFF);
				}
				if(intVTTone712State == GPI_FORCE_ON){
					setTone(SLOT_TONE_VT7_12, GPI_FORCE_OFF);
				}
				if(intToneM1State == GPI_FORCE_ON){
					setTone(SLOT_TONE_M1, GPI_FORCE_OFF);
				}
				if(intToneRes1State == GPI_FORCE_ON){
					setTone(SLOT_TONE_RES1, GPI_FORCE_OFF);
				}
				if(intINTProgCutState == GPI_FORCE_ON)
				{
					progCut(SLOT_INT_PROG_CUT,GPI_FORCE_OFF);
				}

			} // if bstarttxmode

		break;

		case GPI_OFF:
		case GPI_FORCE_OFF:
			intTXState = intValue;
			bTXLightsOn = FALSE;

			setMGNOutputList(sltMGN_OUTPUT_TX_LIST, sltMGN_OUTPUT_TX_FLOOR, GPI_FORCE_OFF);

			if (bTXFlashingMode== FALSE ) { // ie not toggling to flash lights, so a genuine turn off !!

				//setMGNOutputList(sltMGN_OUTPUT_TX_LIST, sltMGN_OUTPUT_TX_FLOOR, GPI_FORCE_OFF);
				bTXFullMode = FALSE;
				if(blnRehWasForced){
					//TX has now cleared but REH was previously set to ON so this should be restored
					infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_ON, intMGN_OUTPUT_REH);			
					//now reset the flag as this should only happen once
					blnRehWasForced = false;
				}
				// update dialog window
				SetDlgItemText(hWndDlg, IDC_TX_MODE, " TX off " );

			}// flashingmode
		break;

	}
	
}

void setBarsLine(int intState){
	Debug("setBarsLine intState=%d",intState);
	char szSlot[32] = "";
	if(intSDIBarsLineState != intState){
		if(intState == GPI_ON){
			//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
			if (InTXMode()==FALSE) {  // only react  if red lights not on
				//route bars to line

				// == hd
				//remember the source before going into bars
//				intHDLineSourcePreBars = intHDLineSource;
//				routerCrosspoint(intDeviceHD, intHD_SRCE_BARS, intHD_DEST_CCA_LINE);
				// == end hd


				intSDILineSourcePreBars = intSDILineSource;
				routerCrosspoint(intDeviceSDI, intSDI_SRCE_BARS, intSDI_DEST_CCA_LINE);
				intSDIBarsLineState = intState;
				sprintf(szSlot, "%d", intState);
				ei.setslot(SLOT_BARS_LINE ,szSlot);

				//disable video bypass
				setVideoBypass(GPI_UNKNOWN);
			}
		}
		else if(intState == GPI_OFF)
		{
			int intSource = 0;
			
			if(intSDILineSourcePreBars){
				intSource = intSDILineSourcePreBars;
			}
			else{
				//Default return to mixer out
				intSource = intSDI_SRCE_MIXER_OUT;
			}
			intSource = intSDI_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs
			//TODO this has changed - the source is not always Mixer out
			routerCrosspoint(intDeviceSDI, intSource, intSDI_DEST_CCA_LINE);

			//TODO restore previous source to line
			intSDILineSourcePreBars = 0;

			// == do HD dest too
//			intSource = 0;
//			if(intHDLineSourcePreBars){
//				intSource = intHDLineSourcePreBars;
//			}
//			else{
//				//Default return to mixer out
//				intSource = intHD_SRCE_MIXER_OUT;
//			}
//			intSource = intHD_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs
//			//TODO this has changed - the source is not always Mixer out
//			routerCrosspoint(intDeviceHD, intSource, intHD_DEST_CCA_LINE);
//
//			//TODO restore previous source to line
//			intHDLineSourcePreBars = 0;
//			// == end hd

			
			intSDIBarsLineState = intState;
			sprintf(szSlot, "%d", intState);
			ei.setslot(SLOT_BARS_LINE ,szSlot);

			//Is it ok to enable video bypass
			//Is bars to Res1 also off?
			if(intSDIBarsRes1State == GPI_OFF){
				setVideoBypass(GPI_OFF);
			}
		}
	}
	updateUI(SLOT_BARS_LINE);
}

void setBarsRes1(int intState){
	char szSlot[32] = "";
	if(intSDIBarsRes1State != intState){
		if(intState == GPI_ON){
			//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
			if (InTXMode()==FALSE) {  // only react  if red lights not on
				//route bars to Res1

				// == hd
//				intHDRes1SourcePreBars = intHDRes1Source;
//				routerCrosspoint(intDeviceHD, intHD_SRCE_BARS, intHD_DEST_CCA_RES1);
				// == end hd

				
				//remember the source before going into bars
				intSDIRes1SourcePreBars = intSDIRes1Source;
				routerCrosspoint(intDeviceSDI, intSDI_SRCE_BARS, intSDI_DEST_CCA_RES1);
				intSDIBarsRes1State = intState;
				sprintf(szSlot, "%d", intState);
				ei.setslot(SLOT_BARS_RES1 ,szSlot);

				//disable video bypass
				setVideoBypass(GPI_UNKNOWN);
			}
		}
		else if(intState == GPI_OFF){
			int intSource = 0;
			
			if(intSDIRes1SourcePreBars){
				intSource = intSDIRes1SourcePreBars;
			}
			else{
				//Default return to mixer out
				intSource = intSDI_SRCE_MIXER_OUT;
			}
			intSource = intSDI_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs

			//TODO this has changed - the source is not always Mixer out
			routerCrosspoint(intDeviceSDI, intSource, intSDI_DEST_CCA_RES1);

			//TODO restore previous source to line
			intSDIRes1SourcePreBars = 0;

			//== hd
//			intSource = 0;
//			
//			if(intHDRes1SourcePreBars){
//				intSource = intHDRes1SourcePreBars;
//			}
//			else{
//				//Default return to mixer out
//				intSource = intHD_SRCE_MIXER_OUT;
//			}
//			intSource = intHD_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs
//
//			//TODO this has changed - the source is not always Mixer out
//			routerCrosspoint(intDeviceHD, intSource, intHD_DEST_CCA_RES1);
//
//			//TODO restore previous source to line
//			intHDRes1SourcePreBars = 0;
//			// == end hd
			
			intSDIBarsRes1State = intState;
			sprintf(szSlot, "%d", intState);
			ei.setslot(SLOT_BARS_RES1 ,szSlot);

			//Is it ok to enable video bypass
			//Is bars to line also off?
			if(intSDIBarsLineState == GPI_OFF){
				setVideoBypass(GPI_OFF);
			}
		}
	}
	updateUI(SLOT_BARS_RES1);
}

//void updateAudioBypassTally(int intBypass){
//	char szSlot[32] = "";
//	switch (intBypass){
//		case 1:
//			if(intAudioBypassTally1 == intAES_SRCE_HWY_BYPASS1){
//				//The source to bypass is from the studer router
//				intAudioBypassTally1 = intStuderSourceBypass1;
//			}
//			else {
//				//the source is from the AES router - store with added offset (size of studer router)
//				intAudioBypassTally1 = intAESSourceBypass1 + intStuderRouterSize;
//			}
//			Debug ("  updating Audio Bypass 1 Tally to %d (Studer=%d, AES=%d)",
//				intAudioBypassTally1, intStuderSourceBypass1, intAESSourceBypass1);
//			break;
//		case 2:
//			if(intAudioBypassTally2 == intAES_SRCE_HWY_BYPASS2){
//				//The source to bypass is from the studer router
//				intAudioBypassTally2 = intStuderSourceBypass2;
//			}
//			else {
//				//the source is from the AES router - store with added offset (size of studer router)
//				intAudioBypassTally2 = intAESSourceBypass2 + intStuderRouterSize;
//			}
//			Debug ("  updating Audio Bypass 2 Tally to %d (Studer=%d, AES=%d)",
//				intAudioBypassTally2, intStuderSourceBypass2, intAESSourceBypass2);
//			break;
//	}
//}

/* older version 
void updateBypassAV(void){
	char szSlot[32] = "";

	int intState = GPI_OFF;
	if((intSDIBypassState == GPI_ON) || (intAESBypass1State == GPI_ON) || (intAESBypass2State == GPI_ON)){
		intState = GPI_ON;
	}

	if(intState != intBypassAVState){
		intBypassAVState = intState;
		sprintf(szSlot, "%d", intBypassAVState);
		ei.setslot(SLOT_BYPASS_AV ,szSlot);

		//TODO set MGN outputs in sltMGN_OUTPUT_BYPASS_LIST
		setMGNBypassList( GPI_FORCE_ON);

		setMGNBypassList( GPI_FORCE_OFF);

	}
}
*/

void setVentOff(int intState)
{
	if(intVentOffState != intState)
	{
		char szSlot[32] = "";
		intVentOffState = intState;
		if(intState == GPI_FORCE_ON)
		{
			intVentOffState = GPI_FORCE_ON;
		}
		else
		{
			intVentOffState = GPI_FORCE_OFF;
		}
		sprintf(szSlot, "%d", intVentOffState);
		ei.setslot(SLOT_VENT_OFF ,szSlot);
		setMGNOutputList(sltMGN_OUTPUT_VENT_OFF,bncs_stringlist(),intVentOffState);

	}
}

/*
// new 1.2
void updateBypassAV(void){
	char szSlot[32] = "";

	if(intSDIBypassState==GPI_ON && intSDIBypassState_RES==GPI_ON)
	{
		//Set light to constant
		sprintf(szSlot, "%d", 2);
	}
	else if(intSDIBypassState==GPI_ON || intSDIBypassState_RES==GPI_ON)
	{
		//Set light to flashing
		sprintf(szSlot, "%d", 1);
	}
	else
	{
		//Set light to off
		sprintf(szSlot, "%d", 0);
	}

	ei.setslot(SLOT_BYPASS_VIDEO_FLASHING ,szSlot);

}
*/
/*	
	int intAState = GPI_OFF;
	int intVState = GPI_OFF;
*/
	//if((intAESBypass1State == GPI_ON) || (intAESBypass2State == GPI_ON)){ pre 1.22
//	if(intAESBypass1State == GPI_ON){ // only audio bypass 1 for bypass ind
//		intAState = GPI_ON;
//	}
//	if(intAState != intBypassAudioState){
//		intBypassAudioState = intAState;
//		sprintf(szSlot, "%d", intBypassAudioState);
//		ei.setslot(SLOT_BYPASS_AUDIO ,szSlot);
//	}

/*	if((intBypassState == GPI_ON)){
		intVState = GPI_ON;
	}
	if(intVState != intBypassVisionState){
		intBypassVisionState = intVState;
		sprintf(szSlot, "%d", intBypassVisionState);
		ei.setslot(SLOT_BYPASS_VISION ,szSlot);
	}
*/
/*
	// determine if to set flashing bypass, perm bypass or no lights
	if ((intBypassAudioState == GPI_OFF)&&(intBypassVisionState == GPI_OFF)) {
		// turn off any bypass lights - flashing or otherwise
		bBypassFlashingMode = FALSE ;
		bBypassLightsOn = FALSE;
		// turn lights off
		setMGNBypassList( GPI_FORCE_OFF );
		// stop timer if no longer needed
		if (bTXFlashingMode==FALSE) 
			KillTimer( hWndMain, TX_FLASHING_TIMER_ID ); 
		return;
	}
	if ((intBypassAudioState == GPI_ON)&&(intBypassVisionState == GPI_ON)) {
		// turn on any bypass lights steady 
		bBypassFlashingMode = FALSE ;
		bBypassLightsOn = TRUE;
		// turn lights on
		setMGNBypassList( GPI_FORCE_ON);
		// stop timer if no longer needed
		if (bTXFlashingMode==FALSE) 
			KillTimer( hWndMain, TX_FLASHING_TIMER_ID ); 
		return;
	}
	if ((intBypassAudioState == GPI_ON)||(intBypassVisionState == GPI_ON)) {
		// turn on any bypass lights flashing
		bBypassFlashingMode = TRUE ;
		bBypassLightsOn = TRUE;
		// turn lights on
		setMGNBypassList( GPI_FORCE_ON );
		// start timer if not already running
		if (bTXFlashingMode==FALSE) 
			SetTimer( hWndMain, TX_FLASHING_TIMER_ID, TX_FLASHING_INTERVAL, NULL );
		return;
	}
*/



// new 1.2
/*
void  updateIntProgCut(void) 
{
	char szSlot[32] = "";

	if (intINTProgCutSrc==intSTUDER_SRCE_SILENCE) {
		intINTProgCutState = GPI_ON;
	}
	else {
		intINTProgCutState = GPI_OFF;  set state off 
		if (intINTProgCutSrc!=intSTUDER_SRCE_PROGRAM)//  not silence but log error too as it is not program
			Debug( "updateIntProgCut - src is neither silence nor program but %d ",  intINTProgCutSrc );
	}
	sprintf(szSlot, "%d", intINTProgCutState);
	ei.setslot(SLOT_INT_PROG_CUT ,szSlot);
	updateUI( SLOT_INT_PROG_CUT );
}

void  	updateExtProgCut(void) 
{
	char szSlot[32] = "";
	int intState = GPI_OFF;

	if (intEXTProgCutSrc==intSTUDER_SRCE_SILENCE) {
		intEXTProgCutState = GPI_ON;
	}
	else {
		intEXTProgCutState = GPI_OFF;  set state off
		if (intEXTProgCutSrc!=intSTUDER_SRCE_PROGRAM) // - ie not silence but log error too as it is not program
			Debug( "updateExtProgCut - src is neither silence nor program but %d ",  intEXTProgCutSrc );
	}
	sprintf(szSlot, "%d", intEXTProgCutState);
	ei.setslot(SLOT_EXT_PROG_CUT ,szSlot);
	updateUI( SLOT_EXT_PROG_CUT );
}
*/

void updateUI(int intSlot){
	char szBuf[32];
	switch(intSlot){
		case SLOT_BARS_LINE:
			wsprintf(szBuf, "%d", intSDIBarsLineState);
			SetDlgItemText(hWndDlg, IDC_BARS_LINE_STATUS, szBuf);
			break;
		case SLOT_BARS_RES1:
			wsprintf(szBuf, "%d", intSDIBarsRes1State);
			SetDlgItemText(hWndDlg, IDC_BARS_RES1_STATUS, szBuf);
			break;
//		case SLOT_TONE_M1:
//			wsprintf(szBuf, "%d", intAESToneM1State);
//			SetDlgItemText(hWndDlg, IDC_TONE_M1_STATUS, szBuf);
//			break;
//		case SLOT_TONE_M2:
//			wsprintf(szBuf, "%d", intAESToneM2State);
//			SetDlgItemText(hWndDlg, IDC_TONE_M2_STATUS, szBuf);
//			break;
//		case SLOT_TONE_RES1:
//			wsprintf(szBuf, "%d", intAESToneRes1State);
//			SetDlgItemText(hWndDlg, IDC_TONE_RES1_STATUS, szBuf);
//			break;
//		case SLOT_TONE_RES2:
//			wsprintf(szBuf, "%d", intAESToneRes2State);
//			SetDlgItemText(hWndDlg, IDC_TONE_RES2_STATUS, szBuf);
//			break;
		case SLOT_VIDEO_BYPASS_A:
			wsprintf(szBuf, "%d", intSDIBypassState);
			SetDlgItemText(hWndDlg, IDC_VIDEO_BYPASS_STATUS, szBuf);
			break;

//		case SLOT_AUDIO_BYPASS_ACTION1:
//			wsprintf(szBuf, "%d", intAESBypass1State);
//			SetDlgItemText(hWndDlg, IDC_AUDIO_BYPASS1_STATUS, szBuf);
//			break;
//		case SLOT_AUDIO_BYPASS_ACTION2:
//			wsprintf(szBuf, "%d", intAESBypass2State);
//			SetDlgItemText(hWndDlg, IDC_AUDIO_BYPASS2_STATUS, szBuf);
//			break;
//		case SLOT_INT_PROG_CUT:
//			wsprintf(szBuf, "%d", intINTProgCutState);
//			SetDlgItemText(hWndDlg, IDC_INT_PROG_CUT, szBuf);
//			break;
//		case SLOT_EXT_PROG_CUT:
//			wsprintf(szBuf, "%d", intEXTProgCutState);
//			SetDlgItemText(hWndDlg, IDC_EXT_PROG_CUT, szBuf);
			break;
	}
}

void setVideoBypass(int intState,int intSlot){

	switch (intSlot)
	{
	case SLOT_VIDEO_BYPASS_A:
		setSingleVideoBypass(intSDI_SRCE_BYPASS, intSDI_DEST_CCA_LINE, &intSDIBypassState,&intSDILineSourcePreBypass,intState, intSlot);
		break;
	case SLOT_VIDEO_BYPASS_B:
		setSingleVideoBypass(intSDI_SRCE_BYPASS_B,intSDI_DEST_CCA_LINE_B,&intSDIBypassState_B,&intSDILineSourcePreBypass_B,intState,intSlot);
		break;
	case SLOT_VIDEO_BYPASS_Res:
		setSingleVideoBypass(intSDI_SRCE_BYPASS_RES,intSDI_DEST_CCA_RES1,&intSDIBypassState_RES,&intSDILineSourcePreBypass_RES,intState,intSlot);
		break;
	case 0:
		setSingleVideoBypass(intSDI_SRCE_BYPASS, intSDI_DEST_CCA_LINE, &intSDIBypassState,&intSDILineSourcePreBypass,intState, SLOT_VIDEO_BYPASS_A);
		setSingleVideoBypass(intSDI_SRCE_BYPASS_B,intSDI_DEST_CCA_LINE_B,&intSDIBypassState_B,&intSDILineSourcePreBypass_B,intState,SLOT_VIDEO_BYPASS_B);
		setSingleVideoBypass(intSDI_SRCE_BYPASS_RES,intSDI_DEST_CCA_RES1,&intSDIBypassState_RES,&intSDILineSourcePreBypass_RES,intState,SLOT_VIDEO_BYPASS_Res);
		break;
	}

}

void setSingleVideoBypass(int iBypassSource, int iDestSource, int* iBypassStatus, int* iPreBypass, int intState, int intSlot)
{
	char szSlot[32] = "";
	int intSource;
	if(*iBypassStatus != intState){
		switch(intState){
			case GPI_UNKNOWN:
				*iBypassStatus = intState;
				sprintf(szSlot, "%d", intState);
				ei.setslot(intSlot ,szSlot);
				break;
			case GPI_ON:
				//check if either bars to line or bars to res are on
				if(intSDIBarsLineState != GPI_ON && intSDIBarsRes1State != GPI_ON){

					//remember the source before going into bypass
					*iPreBypass = intSDILineSource;

					//route the bypass loopback source to the destinations
					routerCrosspoint(intDeviceSDI, iBypassSource, iDestSource);

					// == hd
//					routerCrosspoint(intDeviceHD, intHD_SRCE_BYPASS, intHD_DEST_CCA_LINE);
//					routerCrosspoint(intDeviceHD, intHD_SRCE_BYPASS, intHD_DEST_CCA_RES1);
					// == end hd

					*iBypassStatus = intState;
					sprintf(szSlot, "%d", intState);
					ei.setslot(intSlot ,szSlot);
				}
				break;
			case GPI_OFF:
				//restore main line source
				if(intSDILineSourcePreBypass){
					intSource = intSDILineSourcePreBypass;
				}
				else{
					//Default return to mixer out
					intSource = intSDI_SRCE_MIXER_OUT;
				}
				routerCrosspoint(intDeviceSDI, intSource, iDestSource);
				*iPreBypass = 0;
				
				// == hd
				//restore main line source
//				if(intHDLineSourcePreBypass){
//					intSource = intHDLineSourcePreBypass;
//				}
//				else{
					//Default return to mixer out
//					intSource = intHD_SRCE_MIXER_OUT;
//				}
//				routerCrosspoint(intDeviceHD, intSource, intHD_DEST_CCA_LINE);
//				intHDLineSourcePreBypass = 0;

				//restore reserve line source
//				if(intHDRes1SourcePreBypass){
//					intSource = intHDRes1SourcePreBypass;
//				}
//				else{
					//Default return to mixer reserve
//					intSource = intHD_SRCE_MIXER_RES;
//				}
//				routerCrosspoint(intDeviceHD, intSource, intHD_DEST_CCA_RES1);
//				intHDRes1SourcePreBypass = 0;
				// == end hd

				*iBypassStatus = intState;
				sprintf(szSlot, "%d", intState);
				ei.setslot(intSlot ,szSlot);
				break;
		}
	}
//	updateBypassAV();
	updateUI(intSlot);
}

//void setAudioBypassPreview(int intBypass, int intState){
	//TODO - toggles routes to 
	//			audition preview dest 59(60)
	//		between 
	//			srce desk output 56(57)
	//		and
	//			srce currently selected bypass source intAESSourceBypass1(intAESSourceBypass2)
//	char szSlot[32] = "";
//	switch (intBypass){
//		case 1:
//			Debug("");
//			if(intState == BYPASS_PREVIEW_PVW){
//				Debug ("RX Command - set Audio Bypass 1 Preview to PREVIEW");
//				//route the current bypass1 source to the bypass1 preview audition destination
//				tielineCrosspoint(intDeviceTieline, intTielineSourceBypass1, intAES_DEST_BYPASS1);
//			}
//			else if (intState == BYPASS_PREVIEW_DESK){
//				Debug ("RX Command - set Audio Bypass 1 Preview to DESK");
//				//route the desk output1 source to the bypass1 preview audition destination
//				tielineCrosspoint(intDeviceTieline, intTielineSourceDesk1, intAES_DEST_BYPASS1);
//			}
//
//			//update the bypass1 preview state slot 
//			intAESBypass1PreviewState = intState;
//			sprintf(szSlot, "%d", intAESBypass1PreviewState);
//			ei.setslot(SLOT_AUDIO_BYPASS_PVW1, szSlot);
//			break;
//		case 2:
//			Debug("");
//			if(intState == BYPASS_PREVIEW_PVW){
//				Debug ("RX Command - set Audio Bypass 2 Preview to PREVIEW");
//				//route the current bypass2 source to the bypass2 preview audition destination
//				tielineCrosspoint(intDeviceTieline, intTielineSourceBypass2, intAES_DEST_BYPASS2);
//			}
//			else if (intState == BYPASS_PREVIEW_DESK){
//				Debug ("RX Command - set Audio Bypass 2 Preview to DESK");
//				//route the desk output2 source to the bypass2 preview audition destination
//				tielineCrosspoint(intDeviceTieline, intTielineSourceDesk2, intAES_DEST_BYPASS2);
//			}
//			
//			//update the bypass2 preview state slot 
//			intAESBypass2PreviewState = intState;
//			sprintf(szSlot, "%d", intAESBypass2PreviewState);
//			ei.setslot(SLOT_AUDIO_BYPASS_PVW2, szSlot);
//			break;
//	}
//}

//void setAudioBypassSource(int intBypass, int intSource){
//	char szSlot[32] = "";
//	//range check this source to ensure it is within the range 1 - TielineRouterSize
//	if((intSource >= 1) && (intSource <= intTielineRouterSize)){
//		
//		switch (intBypass){
//			case 1:
//				//has it changed
//				if(intSource != intTielineSourceBypass1){
//					//remember the source
//					intTielineSourceBypass1 = intSource;
//					//if preview is on then make a route to the bypass audition dest
//					Debug("");
//					Debug("RX Command - set Audio Bypass1 source to %d", intTielineSourceBypass1);
//					if(intAESBypass1PreviewState == BYPASS_PREVIEW_PVW){
//						tielineCrosspoint(intDeviceTieline, intTielineSourceBypass1, intAES_DEST_BYPASS1);
//					}
//
//					//update the bypass 1 source slot 
//					sprintf(szSlot, "%d", intTielineSourceBypass1);
//					ei.setslot(SLOT_AUDIO_BYPASS_SRCE1 ,szSlot);
//
//					//if(intSource > intStuderRouterSize) 
//					//	sprintf(szSlot, "%d", (intSource - intStuderRouterSize) );
//					//else
//					//	sprintf(szSlot, "%d", intSource );
//					sprintf(szSlot, "%d", intSource );
//					ei.setslot(SLOT_SRC_AUDIO_BYPASS1 ,szSlot); // this updates the topstap panel
//
//				}
//				break;
//			case 2:
//				if(intSource != intTielineSourceBypass2){
//					//remember the source
//					intTielineSourceBypass2 = intSource;
//					//if preview is on then make a route to the bypass audition dest
//					Debug("");
//					Debug("RX Command - set Audio Bypass2 source to %d", intTielineSourceBypass2);
//					if(intAESBypass2PreviewState == BYPASS_PREVIEW_PVW){
//						tielineCrosspoint(intDeviceTieline, intTielineSourceBypass2, intAES_DEST_BYPASS2);
//					}
//
//					//update the bypass 2 source slot 
//					sprintf(szSlot, "%d", intTielineSourceBypass2);
//					ei.setslot(SLOT_AUDIO_BYPASS_SRCE2 ,szSlot);
//
//					//if(intSource > intStuderRouterSize) 
//					//	sprintf(szSlot, "%d", (intSource - intStuderRouterSize) );
//					//else
//					//	sprintf(szSlot, "%d", intSource );
//					sprintf(szSlot, "%d", intSource );
//					ei.setslot(SLOT_SRC_AUDIO_BYPASS2 ,szSlot); // this updates the topstap panel
//
//				}
//				break;
//		}
//	}
//}

//void setAudioBypassState(int intBypass, int intState){
//	//TODO make this work with classes please
//	
//	char szSlot[32] = "";
//	//Get the details of the line
//	switch(intBypass){
//		case 1:
//			//check if either tone to m1 or tone to res1 are on
//			if(intAESToneM1State != GPI_ON && intAESToneRes1State != GPI_ON){
//				if(intState == GPI_ON){
//					Debug ("");
//					Debug ("RX Command - set Audio Bypass 1 to ON");
//
//					//remember the source on M1 before entering bypass
//					intAESM1SourcePreBypass = intAESM1Source + intStuderRouterSize;
//					//remember the source on Res1 before entering bypass
//					intAESRes1SourcePreBypass = intAESRes1Source + intStuderRouterSize;
//
//					//route the desk1 source to bypass1 audition on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceDesk1, intAES_DEST_BYPASS1);
//
//					//route the current bypass1 source to M1 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass1, intAES_DEST_CCA_M1);
//
//					//route the current bypass1 source to Res1 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass1, intAES_DEST_CCA_RES1);
//
//					//Force the bypass audition preview output to the desk source
//					setAudioBypassPreview(1, BYPASS_PREVIEW_DESK);
//
//				}
//				else {
//					Debug ("");
//					Debug ("RX Command - set Audio Bypass 1 to OFF");
//
//					//route the bypass1 source to bypass1 audition on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass1, intAES_DEST_BYPASS1);
//
//					//route the main1PreBypassSource source to main1 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intAESM1SourcePreBypass, intAES_DEST_CCA_M1);
//					intAESM1SourcePreBypass = 0;
//
//					//route the res1PreBypassSource to res1 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intAESRes1SourcePreBypass, intAES_DEST_CCA_RES1);
//					intAESRes1SourcePreBypass = 0;
//					
//					//Force the bypass audition preview output to the desk source
//					setAudioBypassPreview(1, BYPASS_PREVIEW_PVW);
//				}
//				//update the bypass1 state slot 
//				intAESBypass1State = intState;
//				sprintf(szSlot, "%d", intAESBypass1State);
//				ei.setslot(SLOT_AUDIO_BYPASS_ACTION1, szSlot);
//				updateUI(SLOT_AUDIO_BYPASS_ACTION1);
//			}
//			break;
//		case 2:
//			//check if either tone to m2 or tone to res2 are on
//			if(intAESToneM2State != GPI_ON && intAESToneRes2State != GPI_ON){
//				if(intState == GPI_ON){
//					Debug ("");
//					Debug ("RX Command - set Audio Bypass 1 to OFF");
//					//remember the source on M2 before entering bypass
//					intAESM2SourcePreBypass = intAESM2Source + intStuderRouterSize;
//					//remember the source on Res2 before entering bypass
//					intAESRes2SourcePreBypass = intAESRes2Source + intStuderRouterSize;
//
//					//route the desk2 source to bypass2 audition on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceDesk2, intAES_DEST_BYPASS2);
//
//					//route the current bypass2 source to M2 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass2, intAES_DEST_CCA_M2);
//
//					//route the current bypass2 source to Res2 on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass2, intAES_DEST_CCA_RES2);
//
//					//Force the bypass audition preview output to the desk source
//					setAudioBypassPreview(2, BYPASS_PREVIEW_DESK);
//
//				}
//				else {
//					Debug ("");
//					Debug ("RX Command - set Audio Bypass 2 to OFF");
//
//					//route the bypass2 source to bypass2 audition on the Tieline router
//					tielineCrosspoint(intDeviceTieline, intTielineSourceBypass2, intAES_DEST_BYPASS2);
//
//					//route the main2PreBypassSource source to main2 on the AES router
//					tielineCrosspoint(intDeviceTieline, intAESM2SourcePreBypass, intAES_DEST_CCA_M2);
//					intAESM2SourcePreBypass = 0;
//
//					//route the res1PreBypassSource to res2 on the AES router
//					tielineCrosspoint(intDeviceTieline, intAESRes2SourcePreBypass, intAES_DEST_CCA_RES2);
//					intAESRes2SourcePreBypass = 0;
//
//					//Force the bypass audition preview output to the desk source
//					setAudioBypassPreview(2, BYPASS_PREVIEW_PVW);
//				}
//				//update the bypass2 state slot 
//				intAESBypass2State = intState;
//				sprintf(szSlot, "%d", intAESBypass2State);
//				ei.setslot(SLOT_AUDIO_BYPASS_ACTION2, szSlot);
//				updateUI(SLOT_AUDIO_BYPASS_ACTION2);
//			}
//	}
//
//	updateBypassAV();
//}

void setTone(int intSlot, int intState){
	//TODO make this work with classes please

	bncs_stringlist sltMGN_OUTPUT;
	int* currentState;
	switch (intSlot){
	case SLOT_TONE_M1:
		currentState = &intToneM1State;
		sltMGN_OUTPUT = sltMGN_OUTPUT_TONEM1;
		break;
	case SLOT_TONE_RES1:
		currentState = &intToneRes1State;
		sltMGN_OUTPUT = sltMGN_OUTPUT_TONERES1;
		break;
	case SLOT_TONE_VT1_6:
		 currentState = &intVTTone16State;
		sltMGN_OUTPUT = sltMGN_OUTPUT_VTTONE16;
		break;
	case SLOT_TONE_VT7_12:
		currentState = &intVTTone712State;
		sltMGN_OUTPUT = sltMGN_OUTPUT_VTONE712;
		break;
	}
	char szSlot[32] = "";
	sprintf(szSlot, "%d", intState);
	if(*currentState != intState)
	{
		if(intState == GPI_FORCE_ON)
		{
			//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
			if (InTXMode()==FALSE) 
			{  // only react  if red lights not on
				setMGNOutputList(sltMGN_OUTPUT,bncs_stringlist(),intState);
				*currentState = intState;
				ei.setslot(intSlot ,szSlot);
			}
		}
		else if(intState == GPI_FORCE_OFF)
		{
			setMGNOutputList(sltMGN_OUTPUT,bncs_stringlist(),intState);
			*currentState = intState;
			ei.setslot(intSlot ,szSlot);
		}

	}
	
}

void progCut(int intSlot,int intState)
{
	bncs_stringlist sltMGN_OUTPUT;
	int* currentState;
	bool bChangeEvenIfTXModeIsTrue=true;
	switch(intSlot)
	{
	case SLOT_INT_PROG_CUT:
		bChangeEvenIfTXModeIsTrue = false;
		currentState = &intINTProgCutState;
		sltMGN_OUTPUT = sltMGN_OUTPUT_INT_CUT;
		break;
	case SLOT_EXT_PROG_CUT:
		currentState = &intEXTProgCutState;
		sltMGN_OUTPUT = sltMGN_OUTPUT_EXT_CUT;
		break;
	}
	char szSlot[32] = "";
	sprintf(szSlot, "%d", intState);
	if(*currentState != intState)
	{
		if(intState == GPI_FORCE_ON)
		{
			//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
			if (InTXMode()==FALSE || bChangeEvenIfTXModeIsTrue==true) 
			{  // only react  if red lights not on
				setMGNOutputList(sltMGN_OUTPUT,bncs_stringlist(),intState);
				*currentState = intState;
				ei.setslot(intSlot ,szSlot);
			}
		}
		else if(intState == GPI_FORCE_OFF)
		{
			setMGNOutputList(sltMGN_OUTPUT,bncs_stringlist(),intState);
			*currentState = intState;
			ei.setslot(intSlot ,szSlot);
		}
		
	}
}
/*
Debug("setBarsLine intState=%d",intState);
char szSlot[32] = "";
if(intSDIBarsLineState != intState){
	if(intState == GPI_ON){
		//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
		if (InTXMode()==FALSE) {  // only react  if red lights not on
			//route bars to line
			
			// == hd
			//remember the source before going into bars
			//				intHDLineSourcePreBars = intHDLineSource;
			//				routerCrosspoint(intDeviceHD, intHD_SRCE_BARS, intHD_DEST_CCA_LINE);
			// == end hd
			
			
			intSDILineSourcePreBars = intSDILineSource;
			routerCrosspoint(intDeviceSDI, intSDI_SRCE_BARS, intSDI_DEST_CCA_LINE);
			intSDIBarsLineState = intState;
			sprintf(szSlot, "%d", intState);
			ei.setslot(SLOT_BARS_LINE ,szSlot);
			
			//disable video bypass
			setVideoBypass(GPI_UNKNOWN);
		}
	}
	else if(intState == GPI_OFF)
	{
		int intSource = 0;
		
		if(intSDILineSourcePreBars){
			intSource = intSDILineSourcePreBars;
		}
		else{
			//Default return to mixer out
			intSource = intSDI_SRCE_MIXER_OUT;
		}
		intSource = intSDI_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs
		//TODO this has changed - the source is not always Mixer out
		routerCrosspoint(intDeviceSDI, intSource, intSDI_DEST_CCA_LINE);
		
		//TODO restore previous source to line
		intSDILineSourcePreBars = 0;
		
		// == do HD dest too
		//			intSource = 0;
		//			if(intHDLineSourcePreBars){
		//				intSource = intHDLineSourcePreBars;
		//			}
		//			else{
		//				//Default return to mixer out
		//				intSource = intHD_SRCE_MIXER_OUT;
		//			}
		//			intSource = intHD_SRCE_MIXER_OUT; // override  --  ver 1.2 always mixer out according to docs
		//			//TODO this has changed - the source is not always Mixer out
		//			routerCrosspoint(intDeviceHD, intSource, intHD_DEST_CCA_LINE);
		//
		//			//TODO restore previous source to line
		//			intHDLineSourcePreBars = 0;
		//			// == end hd
		
		
		intSDIBarsLineState = intState;
		sprintf(szSlot, "%d", intState);
		ei.setslot(SLOT_BARS_LINE ,szSlot);
		
		//Is it ok to enable video bypass
		//Is bars to Res1 also off?
		if(intSDIBarsRes1State == GPI_OFF){
			setVideoBypass(GPI_OFF);
		}
	}
	}
*/
	//Get the details of the line
//	switch(intSlot){
//		case SLOT_TONE_M1:
//			if(intAESBypass1State == GPI_ON){
//				blnAudioBypass = true;
//			}
//			intCurrentState = intAESToneM1State;
//			intDest = intAES_DEST_CCA_M1;
//			intCurrentSource = intAESM1Source;
//			intSourcePreTone = intAESM1SourcePreTone;
//			break;
//		case SLOT_TONE_M2:
//			if(intAESBypass2State == GPI_ON){
//				blnAudioBypass = true;
//			}
//			intCurrentState = intAESToneM2State;
//			intDest = intAES_DEST_CCA_M2;
//			intCurrentSource = intAESM2Source;
//			intSourcePreTone = intAESM2SourcePreTone;
//			break;
//		case SLOT_TONE_RES1:
//			if(intAESBypass1State == GPI_ON){
//				blnAudioBypass = true;
//			}
//			intCurrentState = intAESToneRes1State;
//			intDest = intAES_DEST_CCA_RES1;
//			intCurrentSource = intAESRes1Source;
//			intSourcePreTone = intAESRes1SourcePreTone;
//			break;
//		case SLOT_TONE_RES2:
//			if(intAESBypass2State == GPI_ON){
//				blnAudioBypass = true;
//			}
//			intCurrentState = intAESToneRes2State;
//			intDest = intAES_DEST_CCA_RES2;
//			intCurrentSource = intAESRes2Source;
//			intSourcePreTone = intAESRes2SourcePreTone;
//			break;
//	}
//
//	if(intCurrentState != intState){
//		if(intState == GPI_ON){
//			//if(intTXState == GPI_OFF || intTXState == GPI_FORCE_OFF){
//			if (InTXMode()==FALSE) {  // only react  if red lights not on
//				//route tone to line
//				intSourcePreTone = intCurrentSource;
//				if ( (intSlot==SLOT_TONE_M1)||(intSlot==SLOT_TONE_RES1))
//					intSource = intAES_SRCE_TONE1;
//				else if ( (intSlot==SLOT_TONE_M2)||(intSlot==SLOT_TONE_RES2))
//					intSource = intAES_SRCE_TONE2;
 //
//				blnValid = true;
//			}
//		}
//		else if(intState == GPI_OFF){
//			//intSource = intSourcePreTone;
//			if ( (intSlot==SLOT_TONE_M1)||(intSlot==SLOT_TONE_RES1))
//				intSource = intAES_SRCE_DESK1;
//			else if ( (intSlot==SLOT_TONE_M2)||(intSlot==SLOT_TONE_RES2))
//				intSource = intAES_SRCE_DESK2;
//			blnValid = true;
//		}
//	}
//
//	if(blnValid && !blnAudioBypass){
//		//make the route and update the slot
//		routerCrosspoint(intDeviceAES, intSource, intDest);
//		sprintf(szSlot, "%d", intState);
//		ei.setslot(intSlot ,szSlot);
//
//		
//		//update the state of the line
//		switch(intSlot){
//			case SLOT_TONE_M1:
//				intAESToneM1State = intState;
//				intAESM1Source = intSource;
//				if(intSourcePreTone != 0){
//					Debug("M1SourcePreTone=%d", intSourcePreTone);
//					intAESM1SourcePreTone = intSourcePreTone;
//				}
//				break;
//			case SLOT_TONE_M2:
//				intAESToneM2State = intState;
//				intAESM2Source = intSource;
//				if(intSourcePreTone != 0){
//					intAESM2SourcePreTone = intSourcePreTone;
//				}
//				break;
//			case SLOT_TONE_RES1:
//				intAESToneRes1State = intState;
//				intAESRes1Source = intSource;
//				if(intSourcePreTone != 0){
//					intAESRes1SourcePreTone = intSourcePreTone;
//				}
//				break;
//			case SLOT_TONE_RES2:
//				intAESToneRes2State = intState;
//				intAESRes2Source = intSource;
//				if(intSourcePreTone != 0){
//					intAESRes2SourcePreTone = intSourcePreTone;
//				}
//				break;
//		}
//
//		//Now that the tone state has changed check if this enables/disables audio bypass
//		switch(intSlot){
//			case SLOT_TONE_M1:
//			case SLOT_TONE_RES1:
//				if(intState == GPI_ON){
//					//disable bypass audio 1
//					intAESBypass1State = GPI_UNKNOWN;
//					sprintf(szSlot, "%d", intAESBypass1State);
//					ei.setslot(SLOT_AUDIO_BYPASS_ACTION1 ,szSlot);
//					updateUI(SLOT_AUDIO_BYPASS_ACTION1);
//				}
//				else{
//					//enable bypass audio 1
//					if(intAESToneM1State == GPI_OFF && intAESToneRes1State == GPI_OFF ){
//						intAESBypass1State = GPI_OFF;
//						sprintf(szSlot, "%d", intAESBypass1State);
//						ei.setslot(SLOT_AUDIO_BYPASS_ACTION1 ,szSlot);
//						updateUI(SLOT_AUDIO_BYPASS_ACTION1);
//					}
//				}
//				break;
//			case SLOT_TONE_M2:
//			case SLOT_TONE_RES2:
//				if(intState == GPI_ON){
//					//disable bypass audio 2
//					intAESBypass2State = GPI_UNKNOWN;
//					sprintf(szSlot, "%d", intAESBypass2State);
//					ei.setslot(SLOT_AUDIO_BYPASS_ACTION2 ,szSlot);
//					updateUI(SLOT_AUDIO_BYPASS_ACTION2);
//				}
//				else{
//					//enable bypass audio 2
//					intAESBypass2State = GPI_OFF;
//					if(intAESToneM2State == GPI_OFF && intAESToneRes2State == GPI_OFF ){
//						intAESBypass2State = GPI_OFF;
//						sprintf(szSlot, "%d", intAESBypass2State);
//						ei.setslot(SLOT_AUDIO_BYPASS_ACTION2 ,szSlot);
//						updateUI(SLOT_AUDIO_BYPASS_ACTION2);
//					}
//				}
//				break;
//		}
//	}
//	updateUI(intSlot);
//}

void setFloorOff(int intValue){
	
	char szBuf[32];
	if(intFloorOffState != intValue){
		//store the new state FIRST
		intFloorOffState = intValue;
		if(intFloorOffState == GPI_ON){
			strcpy(szBuf, " ON");
			SetDlgItemText(hWndDlg, IDC_FLOOR_OFF_STATUS, szBuf);
			switch(intRehState){
				//The rehearse value is either routed on or is forced on
				case GPI_ON:
				case GPI_FORCE_ON:
					setMGNOutputList(sltMGN_OUTPUT_REH_FLOOR, bncs_stringlist(), GPI_FORCE_OFF);
					break;
			}
			if (InTXMode()==TRUE) { 
				//we need to set the floor lights off
				setMGNOutputList(sltMGN_OUTPUT_TX_FLOOR, bncs_stringlist(), GPI_FORCE_OFF);
			}
		}
		else if(intFloorOffState == GPI_OFF){
			strcpy(szBuf, " OFF");
			SetDlgItemText(hWndDlg, IDC_FLOOR_OFF_STATUS, szBuf);
			switch(intRehState){
				//The rehearse value is either routed on or is forced on
				case GPI_ON:
				case GPI_FORCE_ON:
					//we need to set the floor lights also on
					setMGNOutputList(sltMGN_OUTPUT_REH_FLOOR, bncs_stringlist(), GPI_FORCE_ON);
				break;
			} // switch
			if (InTXMode()==TRUE) { 
				setMGNOutputList(sltMGN_OUTPUT_TX_FLOOR, bncs_stringlist(), GPI_FORCE_ON);
			}
		} // else
	}  
}

void setReh(int intState){
	if (InTXMode()==FALSE) { // only react  if red lights not on
		switch(intRehState){
			case GPI_OFF:
			case GPI_FORCE_OFF:
				if(intState == GPI_ON){
					//Now set the main REH output to FORCE_ON
					infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_ON, intMGN_OUTPUT_REH);
				}
			break;
			case GPI_ON:
			case GPI_FORCE_ON:
				if(intState == GPI_OFF){
					//Now set the main REH output to OFF to allow a routed REH to pass through
					infoWriteInt(intDeviceMGNOutputs, GPI_OFF, intMGN_OUTPUT_REH);
				}
			break;
		} // switch reh
	}
}

void setTX(int intState){
	if (bTXFlashingMode==TRUE) { // irrespective of actual gpi state ( as it toggles in tx flashing mode ) now turn it on permamently
		// end flashing timer, reset vars
		if (bBypassFlashingMode==FALSE )
			KillTimer( hWndMain, TX_FLASHING_TIMER_ID ); // ONLY turn timer off if not flashing bypass lights too
		intFlashTXCounter = int_MAX_TX_FLASH;
		bTXFlashingMode = FALSE;
		bTXLightsOn=TRUE;
		bStartTXMode=FALSE;
		// now issue cmd
		infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_ON, intMGN_OUTPUT_TX);
		// force blue lights off
				//must remember what state the REH is in at this time
				if(intRehState == GPI_FORCE_ON){
					blnRehWasForced = true;
				}
				//must ensure that rehearse lights are now off
				infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_OFF, intMGN_OUTPUT_REH);

				// new v1.2
//				if(intINTProgCutState == GPI_ON ) {
//					routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_PROGRAM, intSTUDER_DEST_INT_PROG_CUT);
//				}
				//if bars to line is on then switch it off
				if(intSDIBarsLineState == GPI_ON){
					setBarsLine(GPI_OFF);
				}
				if(intSDIBarsRes1State == GPI_ON){
					setBarsRes1(GPI_OFF);
				}
				if(intVTTone16State == GPI_FORCE_ON){
					setTone(SLOT_TONE_VT1_6, GPI_FORCE_OFF);
				}
				if(intVTTone712State == GPI_FORCE_ON){
					setTone(SLOT_TONE_VT7_12, GPI_FORCE_OFF);
				}
				if(intToneM1State == GPI_FORCE_ON){
					setTone(SLOT_TONE_M1, GPI_FORCE_OFF);
				}
				if(intToneRes1State == GPI_FORCE_ON){
					setTone(SLOT_TONE_RES1, GPI_FORCE_OFF);
				}
				if(intINTProgCutState == GPI_FORCE_ON){
					progCut(SLOT_INT_PROG_CUT,GPI_FORCE_OFF);
				}
				

		// update dialog window
		SetDlgItemText(hWndDlg, IDC_TX_MODE, "TX Red Lghts ON" );
		SetDlgItemText(hWndDlg, IDC_TX_FLASHCOUNTER, "");
		bTXFullMode = TRUE;
	}
	else {
		switch(intTXState){
			case GPI_OFF:
			case GPI_FORCE_OFF:
				if(intState == GPI_ON){
					// update dialog window
					bTXFullMode = TRUE;
					SetDlgItemText(hWndDlg, IDC_TX_MODE, " TX ON" );
					//Now set the main TX output to FORCE_ON
					infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_ON, intMGN_OUTPUT_TX);
				}
			break;

			case GPI_ON:
			case GPI_FORCE_ON:
				if(intState == GPI_OFF){
					// update dialog window
					bTXFullMode = TRUE;
					SetDlgItemText(hWndDlg, IDC_TX_MODE, " - " );
					//Now set the main TX output to OFF to allow a routed TX to pass through
					infoWriteInt(intDeviceMGNOutputs, GPI_OFF, intMGN_OUTPUT_TX);
				}
			break;
		} // switch tx state
	} // else
}

// new 1.2 below

// ver 1.2 function to return true if in TX state - either 
BOOL InTXMode( void ) 
{
	//if ( (bTXFlashingMode==TRUE)||(intTXState==GPI_ON)||(intTXState==GPI_FORCE_ON)) 
	if ( (bTXFullMode==TRUE) ) // so that interlocks on reh button, bars to line etc only in force when in full tx mode - interlocks OFF in flashing TX mode
		return TRUE;
	else
		return FALSE;
}

void	setFlashingTX(int intState) 
{
	if (bTXFlashingMode==FALSE ) { // only action this if not flashing - only way out is to turn tx on at panel then off
		bStartTXMode=TRUE; //  set when entering a TX mode 
		infoWriteInt(intDeviceMGNOutputs, GPI_FORCE_ON, intMGN_OUTPUT_TX);
		// initiate flashing lights - toggle gpi, set flags for flashing mode
		SetTimer( hWndMain, TX_FLASHING_TIMER_ID, TX_FLASHING_INTERVAL, NULL );
		bTXFlashingMode = TRUE;
		bTXFullMode=FALSE;
		intFlashTXCounter = int_MAX_TX_FLASH;
		SetDlgItemText(hWndDlg, IDC_TX_MODE, "TX Flashing" );
	}
}

//void	setIntProgCut(int intState)
//{	
//	if (InTXMode()==FALSE) {  // only act if not in TX mode or in flashing mode
//		if (intState==GPI_ON) { // route  silence to dest
//			routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_SILENCE, intSTUDER_DEST_INT_PROG_CUT);
//		}
//		else if (intState==GPI_OFF) { // route program to dest
//			routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_PROGRAM, intSTUDER_DEST_INT_PROG_CUT);
//		}
//	}
//}

//void	setExtProgCut(int intState)
//{
//	if (intState==GPI_ON) { // route silence to dest
//		routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_SILENCE, intSTUDER_DEST_EXT_PROG_CUT);
//	}
//	else if (intState==GPI_OFF) { // route program to d 
//		routerCrosspoint(intDeviceStuder, intSTUDER_SRCE_PROGRAM, intSTUDER_DEST_EXT_PROG_CUT);
//	}
//}
//
//
void setMGNBypassList( int intValue)
{
	char szDebug[256];
	bncs_stringlist sltStates = bncs_stringlist("OFF,ON,FORCE_OFF,FORCE_ON");

	strcpy(szDebug, bncs_string("SetMGNBypassList BypassList='%1' Value=%2")
		.arg(sltMGN_OUTPUT_BYPASS_LIST.toString()).arg(sltStates[intValue]));
	Debug(szDebug);

	for(int intOutput=0; intOutput < sltMGN_OUTPUT_BYPASS_LIST.count(); intOutput++){
		int intSlot = sltMGN_OUTPUT_BYPASS_LIST[intOutput];
		infoWriteInt(intDeviceMGNOutputs, intValue, intSlot);
		setOutputItemState(intSlot, intValue);
	}
}


void setMGNOutputList(bncs_stringlist sltOutputs, bncs_stringlist sltFloor, int intValue){
	bool blnSendOK;

	char szDebug[256];
	bncs_stringlist sltStates = bncs_stringlist("OFF,ON,FORCE_OFF,FORCE_ON");
	if(sltFloor.count()){
		strcpy(szDebug, bncs_string("SetMGNOutputList OutputList='%1' FloorList='%2' Value=%3")
			.arg(sltOutputs.toString()).arg(sltFloor.toString()).arg(sltStates[intValue]));
	}
	else {
		strcpy(szDebug, bncs_string("SetMGNOutputList OutputList='%1' Value=%2")
			.arg(sltOutputs.toString()).arg(sltStates[intValue]));
	}
	Debug(szDebug);

	for(int intOutput=0; intOutput < sltOutputs.count(); intOutput++){
		int intSlot = sltOutputs[intOutput];
		blnSendOK = true;
		if(intFloorOffState){
			if(sltFloor.find(intSlot) > -1){
				blnSendOK = false;
			}
		}
		if(blnSendOK){
			infoWriteInt(intDeviceMGNOutputs, intValue, intSlot);
			setOutputItemState(intSlot, intValue);
		}
	}
}

void infoWriteString(int intDevice, const bncs_string& strValue, int intIndex){
	bncs_string strCmd = bncs_string("IW %1 '%2' %3").arg(intDevice).arg(strValue).arg(intIndex);
	ec.txinfocmd(strCmd);
}

void infoWriteInt(int intDevice, int intValue, int intIndex){
	bncs_string strCmd = bncs_string("IW %1 '%2' %3").arg(intDevice).arg(intValue).arg(intIndex);
	ec.txinfocmd(strCmd);
}

void infoPoll(int intDevice, int intMin, int intMax){
	bncs_string strCmd = bncs_string("IP %1 %2 %3").arg(intDevice).arg(intMin).arg(intMax);
	ec.txinfocmd(strCmd);
}

void routerCrosspoint(int intDevice, int intSource, int intDest, const bncs_string& strMask){
	bncs_string strCmd = bncs_string("RC %1 %2 %3").arg(intDevice).arg(intSource).arg(intDest).arg(strMask);
	ec.txinfocmd(strCmd);
}

void tielineCrosspoint(int intDevice, int intSource, int intDest, const bncs_string& strMask){
	Debug("Making route from Tieline source %d to dest %d", intSource, intDest);
}

	//is the source on the studer
//	if((intSource > intStuderRouterSize) && (intSource <= intTielineRouterSize)){
//		Debug("  route from AES source %d to dest %d", intSource - intStuderRouterSize, intDest);
		//the source is on the AES router
		//translate the source index to a near router source and		
		//make the route to the final destination
//		routerCrosspoint(intDeviceAES, intSource - intStuderRouterSize, intDest);
//	}
//	else {
		//Is the destination served by Bypass highway1?
//		if((intDest == intAES_DEST_BYPASS1) || (intDest == intAES_DEST_CCA_M1) || (intDest == intAES_DEST_CCA_RES1)) {
//			Debug("  route from Studer source %d to dest %d", intSource, intSTUDER_DEST_HWY_BYPASS1);
//			Debug("    and route from AES source %d to dest %d", intAES_SRCE_HWY_BYPASS1, intDest);
			//make route from the far source to the tieline start destination
//			routerCrosspoint(intDeviceStuder, intSource, intSTUDER_DEST_HWY_BYPASS1);

			//make route from the tieline end source to the final destination
//			routerCrosspoint(intDeviceAES, intAES_SRCE_HWY_BYPASS1, intDest);
//		}
		//Is the destination served by Bypass highway2?
//		else if((intDest == intAES_DEST_BYPASS2) || (intDest == intAES_DEST_CCA_M2) || (intDest == intAES_DEST_CCA_RES2)) {
//			Debug("  route from Studer source %d to dest %d", intSource, intSTUDER_DEST_HWY_BYPASS2);
//			Debug("    and route from AES source %d to dest %d", intAES_SRCE_HWY_BYPASS2, intDest);
			//make route from the far source to the tieline start destination
//			routerCrosspoint(intDeviceStuder, intSource, intSTUDER_DEST_HWY_BYPASS2);

			//make route from the tieline end source to the final destination
//			routerCrosspoint(intDeviceAES, intAES_SRCE_HWY_BYPASS2, intDest);
//		}
//	}
//}

void routerPoll(int intDevice, int intMin, int intMax){
	bncs_string strCmd = bncs_string("RP %1 %2 %3").arg(intDevice).arg(intMin).arg(intMax);
	ec.txinfocmd(strCmd);

	
}