﻿







/// =============== Pic Frame =============
var isPicIframe = false;
var picIframeID = "ifrmPic";
var picIframeCntr;
//var picCtrl; // the container for pic (updated to new src if deleting, adding or setting a default pic)

function OpenPic(entTpID, entID)
{
	//picCtrl = document.getElementById(picCtrl);
	
	if(!isPicIframe) CreatePicFrame();			
	else 
	{ 
		picIframeCntr.style.display = '';				
	}	
	
	var picIframe = document.getElementById(picIframeID);
//	var entID = GetControlValue('hidEntID');
//	var entTpID = GetControlValue('hidEntTpID');
	//var entNm = GetControlValue('hidEntNm');
	picIframe.src = '/SAM/Ctrl/Pic_Vw.aspx?entid=' + entID + '&enttpid=' + entTpID;// + '&entnm=' + escape(entNm);		
}

function CreatePicFrame()
{
	picIframeCntr = document.createElement("div");
	document.body.appendChild(picIframeCntr);
		
	picIframeCntr.className = "divIframeCntr";	
	picIframeCntr.id = "picFrame";
	isPicIframe = true;

	CreateMyIFrame(picIframeCntr, picIframeID, '');
}

// =============================================	Edit Popup	====================================================

var isEdtIframe = false; 
var edtIframeID = "ifrmEdt";
var edtIframeCntr;
var edtIframe;

// the val is only passed in and used if the control is encrypted
function Edt(fldID, entID, ctrlID, isGetVal, lnk, chkRecID, chkEntTp)
{			
	if(!isEdtIframe) CreateEdtFrame();			
	else 
	{ 
//		edtIframe.src = '/SAM/Loading.html';	// so that it wont show the previous iframes content
		edtIframeCntr.style.display = '';						
	}	
	
	if(edtIframe == null) edtIframe = document.getElementById(edtIframeID);	
	var entTpID = GetControlValue('hidEntTpID');
	var entNm = GetControlValue('hidEntNm');

	var control = null;
	var isEnt = false;
	if (IsDefinedNNotNull(ctrlID)) control = $(ctrlID);		
	if(control != null) isEnt = control.getAttribute('isent'); // need to pass of the isEnt to know whether to get the valueToShow like all other controls
	//edtIframe.src = '/SAM/Loading.html';
	
	if(typeof(isGetVal) == 'undefined' || isGetVal == null) isGetVal = false;
	var val = (isGetVal ? lnk.innerHTML : '');
	
	var src = '/SAM/Ctrl/EdtFldVal.aspx?fldid=' + fldID + '&entid=' + entID + '&enttpid=' + entTpID + '&entnm=' + escape(entNm) + '&ctrlid=' + ctrlID + '&isent=' + isEnt + '&val=' + val;
	if(chkRecID && chkRecID != '') src += '&chkrecid=' + chkRecID;
	if(chkEntTp && chkEntTp != '') src += '&chkenttp=' + chkEntTp;
	src += '&bs=' + new Date().getTime();
	edtIframe.src = src;	
}
function EdtWChk(fldID, entID, ctrlID, chkRecID, chkEntTp) { Edt(fldID, entID, ctrlID, false, null, chkRecID, chkEntTp); }

function CreateEdtFrame()
{
	edtIframeCntr = document.createElement("div");
	document.body.appendChild(edtIframeCntr);
		
	edtIframeCntr.className = "divEdtIframe";	
	edtIframeCntr.id = "edtFrame";	
	isEdtIframe = true;

	CreateMyIFrame(edtIframeCntr, edtIframeID, '');
}
/// =============== Medium Frame =============
var isMediumIframe = false;
var mediumIframeID = "ifrmMedium";
var mediumIframeCntr;

function GetMediumIFrame(win)
{
	if(!IsDefinedNNotNull(win)) win = window;
	
	if(!win.isMediumIframe) 
		win.CreateMediumFrame();			
	else 	 
		win.mediumIframeCntr.style.display = '';				
			
	return win.$(win.mediumIframeID);
}
function CreateMediumFrame()
{
	mediumIframeCntr = document.createElement("div");
	document.body.appendChild(mediumIframeCntr);
		
	mediumIframeCntr.className = "divMediumIframe";	
	mediumIframeCntr.id = "mediumFrame";
	isMediumIframe = true;
	
	CreateMyIFrame(mediumIframeCntr, mediumIframeID, '');	
}
function CloseMediumIframe(){ mediumIframeCntr.style.display = 'none'; }
/// =============== Add Ent/Big Frame =============
var isAddEntIframe = false;
var addEntIframeID = "ifrmAddEnt";
var addEntIframeCntr;
var addEntControlID;
var addEntTargetWin; // the window object which called this function, which is necessary when there are multiple windows
var addEntOpenerWin;

function AddEntForList(controlID, entityUrl)
{		
	addEntControlID = controlID;
	if(!isAddEntIframe) CreateAddEntFrame();			
	else 
	{ 
		addEntIframeCntr.style.display = '';				
	}			
	
	var addEntIframe = document.getElementById(addEntIframeID);
	addEntIframe.src = entityUrl + '?isaddentforlist=1';
}
function CreateAddEntFrame()
{
	addEntIframeCntr = document.createElement("div");
	document.body.appendChild(addEntIframeCntr);
		
	addEntIframeCntr.className = "divAddEntIframe";	
	addEntIframeCntr.id = "entEntFrame";
	isAddEntIframe = true;

	CreateMyIFrame(addEntIframeCntr, addEntIframeID, '');		
}
function GoAddEntIframe(url, win) {  
    if(!IsDefinedNNotNull(win)) win = window; 
    GetAddEntIframe(win).src = url; 
}
function CloseAddEntIframe()
{ 
    HideInPage(addEntIframeCntr); /// Use this instead because it sets the "Loading
//addEntIframeCntr.style.display = 'none'; 
}
function GetAddEntIframe(win)
{
    //alert(win);
	//alert('window.parent && window.parent.window 
	//var win = (window.parent && window.parent.window ? window.parent.window : window);
	//var win = window;
	//alert(!IsDefinedNNotNull(win));
	if(!IsDefinedNNotNull(win)) win = window;
		
	win.addEntTargetWin = window;
	addEntOpenerWin = win;
	//alert('addEntOpenerWin.location = ' + addEntOpenerWin.location);
	if(window.parent && window.parent.window) window.parent.window.addEntOpenerWin = addEntOpenerWin;
	//alert(addEntTargetWin.location);
	
	//alert(" win.$(win.addEntIframeID) = " + win.$(win.addEntIframeID) + " - $(addEntIframeID) = " + $(addEntIframeID));
	
	if(!win.isAddEntIframe || win.$(win.addEntIframeID) == null) 			
	{
	    //alert('CreateAddEntFrame');
		win.CreateAddEntFrame();				
	}
	else 
	{
	    //alert('AddEntFrame Exists so just set its display to show');	    
	    
		win.addEntIframeCntr.style.display = '';					
	}
		
	
	if(win.$(win.addEntIframeID) != null)	    			
	    return win.$(win.addEntIframeID);		
	else
	    return $(addEntIframeID);		
}

function ReturnAddedEntForList(entID, entNm)
{
	var addEntControl = $(addEntControlID);
	if(addEntControlID == bigLstFld) // means that they went to add the entity 
	{
		ReturnBigList(entID, entNm);
	}
	else
	{
		//alert( ' entID = ' + entID + ' entNm = ' + entNm);
		DDLAddOption(addEntControl, entID, entNm, true);
		addEntIframeCntr.style.display = 'none';
	}
}

var bigLstFld; // used to hold the fld for the bigLst ctrl, set when popups, this way we don't have to worry about keeping up with it
var isBigListWin = false; // to know if should ref the child window
function OpenBigList(fld, fldID, isSearch, win) // win is for the lists which are opened dynamically
{	
	if(IsDefinedNNotNull(win))
	{
		win.isBigListWin = true;
	}	
	var lbl = $(fld + '_nm');	
	if(lbl == null) lbl = GetControlByMyID('lbl', fld + '_nm'); // for repeaters				
	bigLstFld = fld;	
	addEntControlID = fld; // so that if the user clicks on the add entity link		
	GetAddEntIframe(win).src = '/SAM/Ctrl/BigList.aspx?fldid=' + fldID + '&issearch=' + isSearch + '&valnm=' + lbl.innerHTML;
}
function ReturnBigList(entID, entNm, entTpID)
{
	if(isBigListWin) { edtIframe.contentWindow.ReturnBigList(entID, entNm); return; };

	var hid = $(bigLstFld);
	if(hid == null) hid = GetControlByMyID('input', bigLstFld); // for repeaters
	var lbl = $(bigLstFld + '_nm');
	if(lbl == null) lbl = GetControlByMyID('lbl', bigLstFld + '_nm'); // for repeaters			
	
	
	hid.value = entID;
	lbl.innerHTML = entNm;
	
	if(entTpID && entTpID != null)
	{
		var hidEntTpID = $(bigLstFld + '_enttpid');
		if(hidEntTpID == null) hidEntTpID = GetControlByMyID('input', bigLstFld + '_enttpid'); // for repeaters	or pages that use a master 
		if(hidEntTpID != null) hidEntTpID.value = entTpID;
	}

	TrigOnchange(hid.id);
	
	if(addEntIframeCntr)
		addEntIframeCntr.style.display = 'none';
	else // when more complex cause opened in main window by child
	{
		if(window.parent && window.parent.window.addEntIframeCntr) window.parent.window.addEntIframeCntr.style.display = 'none';
	}
}

function TrigOnchange(id) // for hidden inputs with onchange
{
	var hid = $(id);
	var onchange = hid.getAttribute('onchange');
	if(onchange != null && onchange != '') hid.onchange();
}

function OpenHelpItem(helpItemID)
{
//	if(!isAddEntIframe) CreateAddEntFrame();			
//	else addEntIframeCntr.style.display = '';				
//		
//	var addEntIframe = document.getElementById(addEntIframeID);
//	addEntIframe.src = '/SAM/Help/Help_Item.aspx?helpitemid=' + helpItemID;
	GetAddEntIframe().src = '/SAM/Help/Help_Item.aspx?helpitemid=' + helpItemID;
}

//function OpenHelpItemByKey(helpItemKey) { GetAddEntIframe().src = '/SAM/Help/Help_Item.aspx?helpitemkey=' + helpItemKey;}

function SetFldVal(controlID, val, valID)
{	
	//alert('SetFldVal - \n controlID = ' + controlID + '\n val = ' + val);
	// when modifying fields for the checklist, then refresh it because it probably has changed other fields or needs to show the response time
	// (it would be good to check if the edited field is a date so to not refresh when not a date because only dates are triggers)
	if(window.location.href.indexOf('Chk.aspx') != -1)
	{ 
		Refresh();		
		return;
	}
	
	var control = $(controlID);
	
	control.innerHTML = val;
	CloseEdtIframe();
	
	// the entity form links need to update the ID in the href
	var isEntLnk = control.getAttribute('isent');
	
	//alert('val = ' + val + '\nisEntLnk = ' + isEntLnk)
	
	if(isEntLnk != null && isEntLnk == '1')
	{
		var href = control.href;
		href = href.substr(0, (href.indexOf('=')+1));
		href += valID;
		control.href = href;
		//alert(href);
	}	
	
	if(GetControlValue('hidIsDupCheck') == '1')	DupAlertDivCheck();
}
function SetCryptVal(ctrlID, ctrlHtml)
{
	document.getElementById(ctrlID + '_cntr').innerHTML = ctrlHtml;
	CloseEdtIframe();
	document.getElementById(ctrlID + '_lnk').style.display = 'none';
}

function CloseEdtIframe()
{
	edtIframeCntr.style.display = 'none';
	if(edtIframe == null) edtIframe = document.getElementById(edtIframeID);
	edtIframe.src = '/SAM/Loading.html';
}

function AlertDivCheck(entTpID, entID, prntEntTpID, prntEntID)
{
	if(!IsDefined(entTpID)) entTpID = GetControlValue('hidEntTpID');
	if(!IsDefined(entID)) entID = GetControlValue('hidEntID');
	
	if(!IsDefined(prntEntTpID)) prntEntTpID = GetControlValue('hidPrntEntTpID');
	if(!IsDefined(prntEntID)) prntEntID = GetControlValue('hidPrntEntID');
	
	var pageUrl = '/SAM/Cmn/AJAX/Alert_Div.ashx?enttpid=' + entTpID + '&entid=' + entID + '&prntenttpid=' + prntEntTpID + '&prntentid=' + prntEntID;
	AJAXShowArea(pageUrl, 'divAlert');
}

function DupAlertDivCheck()
{
	var entTpID = GetControlValue('hidEntTpID');
	var entID = GetControlValue('hidEntID');
	var pageUrl = '/SAM/Cmn/AJAX/Dup_Alert_Div.ashx?enttpid=' + entTpID + '&entid=' + entID;
	AJAXShowArea(pageUrl, 'divDupAlert');
}

function DupAlertDsbl(dupNmAlertID, dupAlertID)
{
	if(window.confirm('Are you sure that you want to Disable this Duplicate Alert?\n\nYou should only disable a duplicate alert after you have confirmed that this record is not duplicate.\n\nPlease be aware that this action cannot be undone!'))
	{
		var pageUrl = '/SAM/Cmn/AJAX/Dup_Alert_Dsbl.ashx?dupnmalertid=' + dupNmAlertID + '&dupalertid=' + dupAlertID;
		AJAXCallPage(pageUrl);
		
		var div = $('divDupAlert_' + dupNmAlertID + '_' + dupAlertID);
		//var div = lnk.parentNode;
		div.style.display = 'none';
		
		var divDupAlert = $('divDupAlert');
		var dupDivs = divDupAlert.getElementsByTagName('div');
		var isAllHidden = true;
		
		for(var i = 1; i < dupDivs.length; i++) // have to start at one because the first is the hdr 
		{
			if(dupDivs[i].style.display != 'none') 
			{ 
				isAllHidden = false;
				break;
			}
		}
		if(isAllHidden == true) divDupAlert.style.display = 'none';
	}
}

function ObjDelNRefresh(tblID, recID) { ObjDelNRedirect(tblID, recID, window.location); }


/// We can pass in the type to make it less generic
function ObjDelNRedirect(tblID, recID, redirectPageUrl, tp)
{
	
	if(window.confirm(ObjDelMsg(tp)))
	{
		var ajaxPageUrl = '/SAM/Cmn/AJAX/Obj_Del.ashx?tblid=' + tblID + '&recid=' + recID;
		AJAXCallPageNRedirect(ajaxPageUrl, redirectPageUrl);
	}
}

// Setup here to make things more standerdized
function ObjDelMsg(tp)
{
	if(!IsDefinedNNotNull(tp)) tp = 'record';
	return 'Are you sure that you want to delete this ' + tp + '?'
}

var objDelReturn; // for places that need to know if it was deleted okay
// the lnk is left for legacy reason but it is no longer used and cannot be used by passing 'this' when using the href property for the onlcick event
function ObjDel(lnk, tblID, recID, controlID, ctrlRowNumID, isRefresh, tp)
{	
	if(window.confirm(ObjDelMsg(tp)))
	{		
		var ajaxPageUrl = '/SAM/Cmn/AJAX/Obj_Del.ashx?tblid=' + tblID + '&recid=' + recID;
		//document.write(pageUrl); return false;		
											
		if(isRefresh && isRefresh == true)
		{
			var url = location.href;
			if(url.indexOf('&isdel=1') == -1)
			{ 
			    if(url.indexOf('?') == -1) url += '?';
			    url += '&isdel=1';
		    }
			//document.write(ajaxPageUrl);
			AJAXCallPageNRedirect(ajaxPageUrl, url);
		}									
		else
		{
			AJAXCallPage(ajaxPageUrl);
			
			if(controlID && controlID != null)
			{
				HideControl(controlID);		
				
				/// When IsSnglRowWMemo then there could possible be another row to delete
				HideIsSnglRowWMemo(controlID);
				
				/// have to check for and hide any multiPart rows that were children of this row
				HideMultiPartItemRows(controlID);
			}
		}
		objDelReturn = true;
		/// Can't have the return statement in here
		//return true;
	}
	else
		objDelReturn = false; //return false; /// we need to return them for places that do other things aftewards
}

function ConfirmNGo(msg, redirectUrl)
{
	if(window.confirm(msg)) window.location = redirectUrl;
}

function HideMultiPartItemRows(id)
{
	// need to replace the rptrItem with rptrMultiPart because of how it is setup
	id = id.replace('_rptrItem', '_rptrMultiPart')
	
	var control = $(id + '_ctl00_rptrMultiPartItem')	
	var i = 0;
	while(control != null)
	{
		HideControl(control.id);
		
		i++;
		var controlID = id + '_ctl';
		if(i < 10) controlID += '0';
		controlID += i;
		controlID += '_rptrMultiPartItem'
		control = $(controlID)		
	}
}

function HideIsSnglRowWMemo(id)
{
	/// When IsSnglRowWMemo then there could possible be another row to delete
	/// First find out if we are on the first or second row
	if(id.substr(id.length, 1) == '2')
	{
	    var control = $(id.substr(0, id.length-1))	
	    if(control && control.style) control.style.display = 'none';
	}
	else
	{
	    var control = $(id + '2')	
	    if(control && control.style) control.style.display = 'none';
	}
	id = id.replace('_rptrItem', '_rptrMultiPart')
	
	var control = $(id + '_ctl00_rptrMultiPartItem')	
	var i = 0;
	while(control != null)
	{
		HideControl(control.id);
		
		i++;
		var controlID = id + '_ctl';
		if(i < 10) controlID += '0';
		controlID += i;
		controlID += '_rptrMultiPartItem'
		control = $(controlID)		
	}
}

function ShowDelLnks(entTpID)
{		
	if(!IsDefined(entTpID)) entTpID = '';
	
	arr = GetElementsByTagN2Attr('span', 'class', 'spanDel', 'entTpID', entTpID);			
	
	for(var i = 0; i < arr.length; i++)
	{		
		arr[i].style.display = 'inline';
		// setup so that if there are multiple repeaters on the page, then only the one that they clicked on will show the delete links			
	}		
}

/// The "actTp" is for when we are doing something that is not actually considered deleting the record, such as "removing from volunteers"
function DelEnt(entTpID, entID, subTblID, subID, actTp)
{
    var msg = 'Are you sure that you want to ';
    if(IsDefinedNNotNull(actTp)) msg += actTp + '?';
    else msg += 'delete this record?'
	if(window.confirm(msg))
	{
		if(!isEdtIframe) CreateEdtFrame();			
		else 
		{ 
			edtIframeCntr.style.display = '';						
		}	
		
		edtIframeCntr.className = "divDelIframe";	
		
		if(edtIframe == null) edtIframe = document.getElementById(edtIframeID);						
		var href = '/SAM/Cmn/Ent_Del.aspx?enttpid=' + entTpID + '&entid=' + entID;
		if(IsDefinedNNotNull(subTblID)) href += '&subtblid=' + subTblID;
		if(IsDefinedNNotNull(subID)) href += '&subID=' + subID;
		edtIframe.src = href;
	}
}

function GetEntFormByEntTpID(entTpID)
{
	if(entTpID == '1') return "/SAM/Fm/FmVw_Vw.aspx?fmid=";
	else if(entTpID == '200') return "/SAM/FmFos/FmFos_Vw.aspx?fmid=";
	else if(entTpID == '2') return "/SAM/Ch/Ch_Vw_Vw.aspx?chid=";
	else if(entTpID == '9') return "/SAM/Ch/Ch_Lct.aspx?chlctid=";
	else if(entTpID == '3') return "/SAM/Ag/Ag_Vw.aspx?agid=";
	else if(entTpID == '4') return "/SAM/Usr/Usr_Vw.aspx?usrid=";
	else if(entTpID == '28') return "/SAM/Ch/Ch_M_Vw.aspx?chmid=";
	else if(entTpID == '29') return "/SAM/Ch/Ch_F_Vw.aspx?chfid=";
	else if(entTpID == '300') return "/SAM/Don/Don_Cntr_Vw.aspx?dnrid=";
	else if(entTpID == '400') return "/SAM/Vol/Vol_Vw.aspx?volid=";
	else if(entTpID == '600') return "/SAM/Ther/Ther_Vw.aspx?therid=";
	else if(entTpID == '602') return "/SAM/Ther/Ther_Prnt_Vw.aspx?therprntid=";
}

function GoEntForm(entTpID, entID){
	window.location = GetEntFormByEntTpID(entTpID) + entID;
}

function GoEntFormNewWin(entTpID, entID) {
    window.open(GetEntFormByEntTpID(entTpID) + entID);
}

function InitChkForTasks(days, respUsrID)
{
	var pageUrl = '/SAM/Chk/AJAX/Chk_for_Tasks.ashx?days=' + days + '&usrid=' + respUsrID;
	AJAXShowArea(pageUrl, 'ctrlChkTasks');	
	
	/// To save the selected value
	AJAXCallPage('/SAM/Chk/AJAX/UsrChkTaskDays_Wrt.ashx?days=' + days);
}
function ToggleChkForTasksArea()
{
	var tblChk = $('tblChk');
	var lnk = $('lnkChkToggle');
	if(tblChk.style.display == 'none')
	{
		ShowControl('tblChk');
		lnk.innerHTML = 'Hide Tasks';
	}
	else
	{
		HideControl('tblChk');
		lnk.innerHTML = 'Show Tasks';
	}
}

function ToggleAreaByVal(control, areaID, val)
{			
	var area = $(areaID);
	if(area == null) area = GetControlByMyID('div', areaID);
	
	if(area != null)
	{
		if(control.value == val)				
			area.style.display = 'block';		
		else
			area.style.display = 'none';		
	}
}
function SecTgl(id) // for places that use the expand and collapse links
{
	var sec = $('sec_' + id);
	//var lnk = $('lnk_' + id);
	var img = $('img_' + id);
	
	if(sec.style.display == 'none')
	{
		sec.style.display = '';
		img.src = '/SAM/Img/Collapse.gif'
	}
	else
	{
		sec.style.display = 'none';
		img.src = '/SAM/Img/Expand.gif'
	}
}

function InitMenu(qs)
{
	if(typeof(qs) == 'undefined' || qs == null) qs = '';
	
	var pageUrl = '/SAM/Menu/AJAX/Menu.ashx';
	if(qs != '') pageUrl += '?' + qs;	 
	 //pageUrl += '?&bs=' + new Date().getTime()
	AJAXShowArea(pageUrl, 'menu');
	
	// means viewing regular report menu so need to hide that op
	if(qs == '')
	{
		HideControl('lnkMenuReg');
		ShowControl('lnkMenuAllAbc');
		ShowControl('lnkMenuAllEntTp');
	}
	else if(qs == '&ismenuall=1') // viewing all report alphabetically
	{
		HideControl('lnkMenuAllAbc');
		ShowControl('lnkMenuReg');		
		ShowControl('lnkMenuAllEntTp');
	}
	else if(qs == '&ismenuall=1&isbyenttp=1') // viewing all report by entity type
	{
		HideControl('lnkMenuAllEntTp');
		ShowControl('lnkMenuReg');
		ShowControl('lnkMenuAllAbc');		
	}
	else if(qs != '') // one of the single entity type report menus
	{
		ShowControl('lnkMenuAllEntTp');
		ShowControl('lnkMenuReg');
		ShowControl('lnkMenuAllAbc');
	}
}
// this calls the GetCryptVal page which gets the encrypted value will and either put it back into the page or puts in the control and puts the controls in the page or passes it off to the EdtFldVal
function GetCryptVal(lnk, actTp, fldID, tabIdx, entID, entTpID)
{
//	var entID = GetControlValue('hidEntID');
//	var entTpID = GetControlValue('hidEntTpID');
	var ctrlID = lnk.id.replace('_lnk', ''); // the ctrlID will the same as the lnk id except without the '_lnk' postfix
	
	//alert('tabIdx = ' + tabIdx);
	
	if(!isEdtIframe) CreateEdtFrame();			
	else 
	{ 
		edtIframeCntr.style.display = '';						
	}	
	
	if(edtIframe == null) edtIframe = document.getElementById(edtIframeID);
			
	var entNm = GetControlValue('hidEntNm');	
	var control = $(ctrlID);
	var isEnt = false; // for now wont have any entity links being encrypted //control.getAttribute('isent'); // need to pass of the isEnt to know whether to get the valueToShow like all other controls
			
	edtIframe.src = '/SAM/Ctrl/CryptGetVal.aspx?fldid=' + fldID + '&entid=' + entID + '&enttpid=' + entTpID + '&entnm=' + escape(entNm) + '&ctrlid=' + ctrlID + '&isent=' + isEnt + '&acttp=' + actTp + '&tabidx=' + tabIdx;			
}

function AJAXFldValWrtNRefresh(fldID, entID, entTpID, val) {
    AJAXFldValWrt(fldID, entID, entTpID, val);
    Refresh();
}

function AJAXFldValWrt(fldID, entID, entTpID, val)
{	
	//document.write('/SAM/Cmn/AJAX/Fld_Val_Wrt.ashx?fldid=' + fldID + '&entid=' + entID + '&enttpid=' + entTpID + '&val=' + escape(val));
	AJAXCallPage(GetAJAXFldValWrtUrl(fldID, entID, entTpID, val));
}
function GetAJAXFldValWrtUrl(fldID, entID, entTpID, val){ return '/SAM/Cmn/AJAX/Fld_Val_Wrt.ashx?fldid=' + fldID + '&entid=' + entID + '&enttpid=' + entTpID + '&val=' + escape(val); }

function Help()
{
    GetAddEntIframe().src = '/SAM/Help/Help.aspx?url=' + UrlEncode(window.location.toString());

//	if(!isLgIframe) CreateLgFrame();			
//	else 
//	{ 
//		lgIframeCntr.style.display = '';				
//	}	
//	
//	var url = '/SAM/Help/Help.aspx?url=' + UrlEncode(window.location.toString());
//	var lgIframe = document.getElementById(lgIframeID);
//	lgIframe.src = url;
	//window.open('/SAM/Help/Help.aspx?url=' + UrlEncode(window.location.toString()));
}



/// =============== Large Frame =============
// can be used for add ent or help, or any other popup 
var isLgIframe = false;
var lgIframeID = "ifrmLg";
var lgIframeCntr;

function CreateLgFrame()
{
	lgIframeCntr = document.createElement("div");
	document.body.appendChild(lgIframeCntr);
		
	lgIframeCntr.className = "divLgIframe";	
	lgIframeCntr.id = "entEntFrame";
	isLgIframe = true;

	CreateMyIFrame(lgIframeCntr, lgIframeID, '');		
}


function ChkRespWrtForTask(recID, entTp)
{
	//alert('recID = ' + recID);
	AJAXCallPage('/SAM/Chk/AJAX/Chk_Resp_Wrt.ashx?recid=' + recID + '&enttp=' + entTp);
	HideControl('trChk_' + recID);
}

// when is from the edtFrame, then it will open a new popup from the main opener and refresh the edit popup when changes made
function EdtLst(fld, fldID, tblID, fldVal, isEdtFrame, tabIndex)
{	
	//this.CreateAddEntFrame()
	//return;
	//alert(' isEdtFrame = ' + isEdtFrame);
	var winObj = (isEdtFrame == true ? window.parent : this);
//	with((isEdtFrame == true ? window.parent : window))
//	{
		if(!winObj.isAddEntIframe) winObj.CreateAddEntFrame();			
		else 
		{ 
			winObj.addEntIframeCntr.style.display = '';				
		}
						
		//alert('winObj.addEntIframeID = ' + winObj.addEntIframeID);
		var addEntIframe = winObj.$(winObj.addEntIframeID);
		
		var ctrlFld = $(fld);
		if(ctrlFld == null) ctrlFld = GetControlByMyID('select', fld);
		var ctrlFldParent;
		if(ctrlFld != null) ctrlFldParent = ctrlFld.parentNode;
		// when one-to-many then have to get the vals of each of the flds
		if(GetControlAttribute(ctrlFldParent, 'isOneToMany') == '1')
		{
			ctrlFldParent = ctrlFldParent.parentNode
			fldVal = '';
			var ctrls = ctrlFldParent.getElementsByTagName('select');
			//alert('ctrlFldParent.id = ' + ctrlFldParent.id + ' - ctrlFldParent.tagName = ' + ctrlFldParent.tagName + ' ctrls.length = ' + ctrls.length + '\nctrlFldParent.innerHtml = \n' +ctrlFldParent.innerHTML);
			for(var i = 0; i < ctrls.length; i++)			
				fldVal += ctrls[i].value + ',;,';
		}
		
		addEntIframe.src = '/SAM/Lst/LstEdtPopup.aspx?tblid=' + tblID + (fldID != null ? '&fldid=' + fldID : '') + '&fld=' + fld + '&fldVal=' + fldVal + '&tabidx=' + tabIndex;
	//}				
}

// check if from the edtFrame, when is from the edtFrame then it will have the edtFrameVis
// uses similar functionality in Lst/LstEdt.js so will have to update it also if something in here changes.
/// Gets the whole html instead of just using DOM to add, remove, re-order flds, because its much easier, especially when re-ordering or setting them to default order
function EdtLstReturn(fld, controlHtml)
{
	if(edtIframeCntr && edtIframeCntr != null && edtIframeCntr.style.display != 'none')
	{		
		// just reload the iframe to get the lists newest values, Have to put the bs at the end for IE not to cache	
		if(edtIframe) edtIframe.src = edtIframe.src + '&bs=' + new Date().getTime();
	}
	else
	{
		var ctrlFld = $(fld);
		if(ctrlFld == null) ctrlFld = GetControlByMyID('select', fld);
		var ctrlFldParent;
		if(ctrlFld != null) ctrlFldParent = ctrlFld.parentNode;
		// when one-to-many then have to go up one more to get to the div
//		alert('GetControlAttribute(ctrlFldParent, \'isOneToMany\') = ' + GetControlAttribute(ctrlFldParent, 'isOneToMany'));
//		alert('controlHtml = ' + controlHtml);
		
		if(GetControlAttribute(ctrlFldParent, 'isOneToMany') == '1') ctrlFldParent = ctrlFldParent.parentNode;
		if(ctrlFldParent != null) ctrlFldParent.innerHTML = controlHtml;
	}
}



function AdpCancel(chID)
{
	if(window.confirm('Are you sure that you want to Cancel/Undo this Adoption?\n\nThis should only be done if a commitment was placed on the wrong family or child and should NOT be done for Commitment Cancellations, Disruptions or Dissolutions.\n\nThis will untie the family from the child and remove any Post-Adoption Reports.'))
	{
		AJAXCallPageNRedirect('/SAM/Ch/AJAX/Adp_Cancel.ashx?chid=' + chID, location);
	}

}

function ShowPopupMsg(msg)
{				
	
	if(!isEdtIframe) CreateEdtFrame();			
	else 
	{ 
		edtIframeCntr.style.display = '';						
	}	
	
	if(edtIframe == null) edtIframe = document.getElementById(edtIframeID);			
		
	edtIframe.src = '/SAM/Ctrl/PopupMsg.aspx?msg=' + msg;
}

// the actual dirty work is done in the proc which will check the value in the hid against the value in the db
function IsMultiPartEnable(entTp) 
{	
	SetControlValue('Is' + entTp + 'MultiPart', '1');			
	SetControlValue('Is' + entTp + 'MultiPartChange', '1');			
	MyPostBack();			
}
function IsMultiPartDisable(entTp, multiPartNm)
{
	if(window.confirm('Are you sure that you want to remove all the ' + multiPartNm + 's and change the form back to the regular way?\n\nThis will sum up the data in the ' + multiPartNm + ' records and update the main record with it. Then it will permanently delete all the ' + multiPartNm + ' records shown here.'))
	{
		SetControlValue('Is' + entTp + 'MultiPart', '0');			
		SetControlValue('Is' + entTp + 'MultiPartChange', '1');			
		MyPostBack();		
	}
}

function OpenWinInEdtFrame(url)
{
	if(!isEdtIframe) CreateEdtFrame();			
	else edtIframeCntr.style.display = '';						
		
	if(edtIframe == null) edtIframe = $(edtIframeID);		
	
	edtIframe.src = url;
	//return edtIframe; /// this breaks it, because this func is called from anchor's href
}



// =============================================        Logout              ====================================================
     
function LogOut(isTimeout)
{                	
	var redirectUrl = '/SAM/SAMLogin.aspx'
	if(isTimeout) redirectUrl += '?isautologout=1&oldurl=' + UrlEncode(window.location);
	AJAXCallPageNRedirect('/SAM/Cmn/AJAX/Logout.ashx', redirectUrl);
}

//var sessTimeout = 300
var sessTimeout = 300000; // each page will check for session expiration every 5 minutes (to ensure that if the user logged out then the other windows will be logged out shortly
function LogOutWhenSessionExpired()
{
	AJAXGetStr('/SAM/Cmn/AJAX/IsSesExp.ashx', StateChangedForIsLogOutWhenSessionExpired);
}

//setTimeout("LogOutWhenSessionExpired()", sessTimeout); // now put on page by the MyPage class, only for internal pages
//window.defaultStatus = 'Test';
function StateChangedForIsLogOutWhenSessionExpired() 
{ 
	if (xmlHttpGeStr != null && (xmlHttpGeStr.readyState==4 || xmlHttpGeStr.readyState=="complete"))
	{ 
		if(xmlHttpGeStr.responseText == "1")
		{
			LogOut(true);
		}
		else 
		{
			setTimeout("LogOutWhenSessionExpired()", sessTimeout);
		}
	} 
}

function CheckForTskEmlToConfirm()
{
	AJAXGetStr('/SAM/Tsk/AJAX/Tsk_Eml_to_Confirm.ashx', StateChangedFor_CheckForTskEmlToConfirm);
}

//setTimeout("LogOutWhenSessionExpired()", sessTimeout); // now put on page by the MyPage class, only for internal pages
//window.defaultStatus = 'Test';
function StateChangedFor_CheckForTskEmlToConfirm() 
{ 
	if (xmlHttpGeStr != null && (xmlHttpGeStr.readyState==4 || xmlHttpGeStr.readyState=="complete"))
	{ 
		if(xmlHttpGeStr.responseText && Trim(xmlHttpGeStr.responseText) != "")
		{			
			var tskEmlID = Number(xmlHttpGeStr.responseText);
			if(!isNaN(tskEmlID))
			{
				GoAddEntIframe('/SAM/Tsk/Tsk_Eml_Confirm_N_Send.aspx?tskemlid=' + tskEmlID);
			}
		}		
	} 
}


function SecHide(frmSecID, entID)
{
	HideControl('sec_' + frmSecID);
	ShowControl('lnkShow_' + frmSecID);
	FrmSecHidWrt(1, frmSecID, entID, null, null);	
}
function SecShow(frmSecID, entID)
{	
	HideControl('lnkShow_' + frmSecID);
	ShowControl('sec_' + frmSecID);
	FrmSecHidWrt(0, frmSecID, entID, null, null);	
}
function ChkSecHide(chkSecID, entID, fldEntID)
{
	HideControl('sec_' + chkSecID + '_' + fldEntID);
	ShowControl('lnkShow_' + chkSecID + '_' + fldEntID);
	FrmSecHidWrt(1, null, entID, chkSecID, fldEntID);	
}
function ChkSecShow(chkSecID, entID, fldEntID)
{
	HideControl('lnkShow_' + chkSecID + '_' + fldEntID);
	ShowControl('sec_' + chkSecID + '_' + fldEntID);	
	FrmSecHidWrt(0, null, entID, chkSecID, fldEntID);	
}
function FrmSecHidWrt(isHid, frmSecID, entID, chkSecID, fldEntID)
{	
	var url = '/SAM/Cmn/AJAX/Frm_Sec_Hid_Wrt.ashx?ishid=' + isHid;
	if(IsDefinedNNotNull(frmSecID)) url += '&frmsecid=' + frmSecID;
	if(IsDefinedNNotNull(entID)) url += '&entid=' + entID; 
	if(IsDefinedNNotNull(chkSecID)) url += '&chksecid=' + chkSecID;
	if(IsDefinedNNotNull(fldEntID)) url += '&fldEntID=' + fldEntID;
	AJAXCallPage(url);
}

function PrintAdrLblForEnt(entTpID, entID, isShip)
{
	AJAXGetStr('/SAM/Cmn/AJAX/Ent_Adr_for_Lbl.ashx?enttpid=' + entTpID + '&entid=' + entID + '&isship=' + isShip, StateChangedForPrintAdrLblForEnt);
}
function StateChangedForPrintAdrLblForEnt() 
{ 
	if(xmlHttpGeStr != null && (xmlHttpGeStr.readyState==4 || xmlHttpGeStr.readyState=="complete"))
	{ 
		var s = xmlHttpGeStr.responseText;//.replace('\n', '\\n');		
		if(isIE)		
			DYMOPrintIE(s);
		else
			DYMOPrint(s);		
	} 
}

function DYMOPrintList()
{	
	var tbl = $('tblLbls');
	for(var iRow = 0; iRow < tbl.rows.length; iRow++)
	{
		var row = tbl.rows[iRow];
		for(var iCell = 0; iCell < row.cells.length; iCell++)
		{
			var cell = row.cells[iCell];				
			var s = cell.innerHTML;
			if(s != '' && s != '&nbsp;' && s != '&nbsp' && s != '&NBSP;' && s != '&NBSP')
			{								
				/// The browsers render the innerHTML differently so we have to make sure to get all 
				s = Replace(s, '<br/>', '\n');
				s = Replace(s, '<br>', '\n');
				s = Replace(s, '<BR>', '\n');
				s = Replace(s, '&amp;', '&');
				s = Replace(s, '&quot;', '"');
				
				/// Have to strip out all the HTML, because we have some divs around it
				var matchTag = /<(?:.|\s)*?>/g;                
                s = s.replace(matchTag, "");
				//alert(s);
				
				/// After replacing the HTML we use this
				s = Replace(s, "&lt;", "<");
				s = Replace(s, "&gt;", ">");				
								
				if(isIE)		
					DYMOPrintIE(s);
				else
					DYMOPrint(s);
			}
		}			
	}
}

function DYMOPrint(s)
{
	try
	{     			
		var DymoAddIn, DymoLabel;	  				
		
		try
		{
			DymoAddIn = new nsDymoAddIn();
		}
		catch(ex)
		{
			GetMediumIFrame().src = '/SAM/Util/InstalDymoAddIn.aspx';
		}
		DymoLabels = new nsDymoLabels();		
		
		//if(!IsDefined(DymoAddIn)) alert('oh no');
		
        if (DymoAddIn.Open('C:\\Documents and Settings\\All Users\\Documents\\DYMO Label\\Label Files\\Address (30252, 30320, 30572).LWL'))
    	{
        	DymoLabels.SetAddress(1, s);
	        DymoAddIn.Print(1, true);
        }
        else if (DymoAddIn.Open('C:\\Documents and Settings\\All Users\\Documents\\DYMO Label\\Label Files\\Address (30252, 30320).LWL'))
    	{
        	DymoLabels.SetAddress(1, s);
	        DymoAddIn.Print(1, true);
        }
    	else if (DymoAddIn.Open('C:\\Program Files\\Dymo Label\\Label Files\\Address  (30252, 30320).LWT'))
        {
    		DymoLabels.SetAddress(1, s);
	        DymoAddIn.Print(1, true);
        }
    	else    	
	      alert('Oops! An Error Occurred.\n\nPlease make sure that you have the DYMO Label Maker software installed and the device is on.\n\nIf you have verified that the the DYMO Software is installed and the device is turned on then please contact support and provide them with the following error message:\nLabel file Not Found.');		  
	}
	catch (err)
	{
		if(err.indexOf("nsDymoAddIn is not defined") != -1)
		{
			GetMediumIFrame().src = '/SAM/Util/InstalDymoAddIn.aspx';
		}
		else
		{
			alert('Oops! An Error Occurred.\n\nPlease make sure that you have the DYMO Label Maker software installed and the device is on.\n\nIf you have verified that the the DYMO Software is installed and turned on then please contact support and provide them with the following error message:\n' + err);		  			
			return;
		}
	}
}

function DYMOPrintIE(s) /// this has to be a separate function because it would not work when the ActiveXObject is not declare in the same block as the calls
{
	try
	{
		var DymoAddIn, DymoLabel;
		DymoAddIn = new ActiveXObject('DYMO.DymoAddIn');
		DymoLabel = new ActiveXObject('DYMO.DymoLabels');

		if (DymoAddIn.Open('C:\\Documents and Settings\\All Users\\Documents\\DYMO Label\\Label Files\\Address (30252, 30320, 30572).LWL'))
		{
		  DymoLabel.SetAddress(1, s);
		  DymoAddIn.Print(1, true);	
		}
		else if (DymoAddIn.Open('C:\\Program Files\\DYMO Label\\Label Files\\Address (30252, 30320, 30572).LWL'))
		{
		  DymoLabel.SetAddress(1, s);
		  DymoAddIn.Print(1, true);	
		}
		else if (DymoAddIn.Open('C:\\Program Files\\Dymo Label\\Label Files\\Address  (30252, 30320).LWT'))
		{
		  DymoLabel.SetAddress(1, s);
		  DymoAddIn.Print(1, true);	
		}
		else
			alert('Oops! An Error Occurred.\n\nPlease make sure that you have the DYMO Label Maker software installed and the device is on.\n\nIf you have verified that the the DYMO Software is installed and the device is turned on then please contact support and provide them with the following error message:\nLabel file Not Found.');		  
	  
	}
	catch (err)
	{
		alert('Oops! An Error Occurred. \n\nPlease contact support and provide them with the following error message:\n' + err);
		return;
	}
}

function DelRow(rowID, hidIsDelID)
{
    if(IsDefinedNNotNull(hidIsDelID)) 
    {
        var ctrlHidIsDelID = $(hidIsDelID);
        ctrlHidIsDelID.value = '1';
    }
    HideControl(rowID);
}