﻿// this file is just for Edt page functions that should not be exposed on Public forms

// =============================================	Duplicate Field Check	====================================================
var xmlHttpFldDupChk;
var fldDupChk_DupNm = '';
var fldDupChk_DupType = '';
var fldDupChk_DupLnk = '';
var fldDupChk_Control;

// possibly change this to use just the fldID later
function FldDupChk(control, tblID, tblNm, tblIDCol, fldID, fld, val, recID, fltr, dupNm, dupLnk, fldTpID)
{
	val = Trim(val);
	//alert('FldDupChk val = ' + val);
	// check that it did not have dup alert before being cleared out 
	if(val == '') 
	{
		var tr = $('trDup' + control.id);
		if(tr != null)
		{
			var tbl = tr.parentNode.parentNode;
			tbl.deleteRow(tr.rowIndex);
			tbl.setAttribute('dupAlert' + control.id, '0');
			
			RemoveDupItem(dupNm);			
			ShowDupMsg();
		}
		return;
	}			
	
	/// The entity being checked could be an 'extra' entity on the form
	var entHidEntID = $('hidEntID_' + tblID);
	var entID;
	if(entHidEntID && entHidEntID.value && entHidEntID.value != '') 
		entID = entHidEntID.value;
	else
    {
	    entID = GetControlValue('hidPrntEntID');
		if(entID == '') entID = GetControlValue('hidEntID');
	}
	
	if(entID != '') recID = entID;
			
	var dupType = '';	
	if(fldTpID == '7') dupType = 'Phone Number';
	else if(fldTpID == '8') dupType = 'Email';
	
	if(typeof(fldTpID) == 'undefined') fldTpID = '';
		
	var entHidDupLnkUrl = $('hidDupLnkUrl_' + tblID);				
	if(entHidDupLnkUrl && entHidDupLnkUrl.value && entHidDupLnkUrl.value != '') dupLnk = entHidDupLnkUrl.value;				
				
	xmlHttpFldDupChk = AJAXGetXmlHttpObject();
	fldDupChk_DupType = dupType;
	if(dupNm != '')
		fldDupChk_DupNm = dupNm; // sometimes we wont have a field name
	else
		fldDupChk_DupNm = dupType;  
	fldDupChk_DupLnk = dupLnk;
	fldDupChk_Control = control;
	xmlHttpFldDupChk.onreadystatechange = AJAXFldDupChkStateChanged;	
	
	/// for custom forms
	var hidFldDupChkFltr = $('hidFldDupChkFltr'); 
	//alert('hidFldDupChkFltr =  ' + hidFldDupChkFltr.value);
	if(hidFldDupChkFltr && hidFldDupChkFltr.value) fltr = hidFldDupChkFltr.value;
	
	var fldDupChkUrl = '/SAM/Cmn/AJAX/FldDupChk.ashx?tblid=' + tblID + '&tblnm=' + tblNm + '&tblidcol=' + tblIDCol + '&fldid=' + fldID + '&fld=' + fld + '&val=' + UrlEncode(val) + '&recid=' + recID + '&fltr=' + fltr + '&fldtpid=' + fldTpID;
	//document.write(fldDupChkUrl);
	xmlHttpFldDupChk.open("GET", fldDupChkUrl, true);
	xmlHttpFldDupChk.send(null);		
}

function AJAXFldDupChkStateChanged() 
{ 	
	if(xmlHttpFldDupChk && xmlHttpFldDupChk != null && (xmlHttpFldDupChk.readyState == 4 || xmlHttpFldDupChk.readyState == "complete"))
	{	
		var response = xmlHttpFldDupChk.responseText; // will be recID of duplicate if found, otherwise 0 if not found		
		//alert('response = ' + response);
		if(response != null && !isNaN(response) && Number(response) > 0)
		{			
			if(ArrayIndexOf(arrDupNm, fldDupChk_DupNm) == -1) 
			{
				ArrayAddItem(arrDupNm, fldDupChk_DupNm);
				
				if(fldDupChk_DupLnk != '') 
				{
					fldDupChk_DupLnk += response;					
					arrDupLnk[arrDupLnk.length] = fldDupChk_DupLnk;		
				}
				else
					arrDupLnk[arrDupLnk.length] = '';	 // add just to act as placeholder
				
				//document.write(fldDupChk_Control);
					
				if(fldDupChk_Control != null)
				{
					var td = fldDupChk_Control.parentNode;
					var tr = td.parentNode;
					var tbl = tr.parentNode.parentNode;
					
					// use attribute on table to identify if this alert has already been set so that it does not show twice
					if(tbl.getAttribute('dupAlert' + fldDupChk_Control.id) != '1')
					{										
						tbl.setAttribute('dupAlert' + fldDupChk_Control.id, '1'); // set this immediately afterwards so that it does not fire again
						
						var newTr = tbl.insertRow(tr.rowIndex + 1);
						newTr.id = 'trDup' + fldDupChk_Control.id;
						var newTd = newTr.insertCell(0);
						newTd.colSpan = tr.cells.length;					
											
						newTd.className = 'tdDupAlert';
						var html = 'Duplicate ' + (fldDupChk_DupType != null && fldDupChk_DupType != '' ? fldDupChk_DupType : fldDupChk_DupNm) + ' Alert!';
						if(fldDupChk_DupLnk != '') html += '<span class="padDupLnk"><a href="' + fldDupChk_DupLnk + '" target="_blank" class="ctrlLnk">View Duplicate Record</a>';
						newTd.innerHTML = html;
						
					}
				
					fldDupChk_Control = null; // reset it
				}
			}
		} 
		else		
		{
			RemoveDupItem(fldDupChk_DupNm);
			 
			if(fldDupChk_Control != null)
		    {
			    var tr = $('trDup' + fldDupChk_Control.id);
			    if(tr != null)
			    {
				    var tbl = tr.parentNode.parentNode;
				    tbl.deleteRow(tr.rowIndex);
				    tbl.setAttribute('dupAlert' + fldDupChk_Control.id, '0');
			    }					
			}
		}				
		
		ShowDupMsg();
	}			
}

function RemoveDupItem(dupNm)
{
	var index = ArrayIndexOf(arrDupNm, dupNm);
	if(index != -1) // get index so that can remove the dup lnk also
	{ 
		arrDupNm.splice(index, 1);        
		arrDupLnk.splice(index, 1);        
	}		
}

///  TO DO = (MAYBE) SETUP DUP CHECK ATTRIBUTE ON CONTROL TO KNOW WETHER THAT VALUE HAD ALREADY BEEN CHECKED SO THAT IT DOES NOT HAVE TO BE RECHECK
/// Have an object which contains all the dup checked values so that it does not have to check them twice



// ===================================================		Fld Dup Name Check		=========================================
var fldDupNmChk_LNm;
var fldDupNmChk_FNm;
var fldDupNmChk_MNm;

/// TO DO - TEST OR SETUP THAT ONE DUP CHECK WILL NOT OVERRIDE AND CANCEL OUT AN IN-PROGRESS ONE

function FldDupNmChk(lNmID, fNmID, mNmID, entNm, fldID, tblID)
{
	var lNm = GetControlValue(lNmID);
	var fNm = GetControlValue(fNmID);	
	
	if(lNm != '' && fNm != '')
	{
		var dupLnk;
		var entHidDupLnkUrl = $('hidDupLnkUrl_' + tblID);		
		
		if(entHidDupLnkUrl && entHidDupLnkUrl.value && entHidDupLnkUrl.value != '') 
			dupLnk = entHidDupLnkUrl.value;
		else
			dupLnk = GetControlValue('hidDupLnkUrl');		
		var mNm = GetControlValue(mNmID);	
		
		/// The entity being checked could be an 'extra' entity on the form
		var entHidEntID = $('hidEntID_' + tblID);
		var entID;
		if(entHidEntID && entHidEntID.value && entHidEntID.value != '') 
			entID = entHidEntID.value;
		else
		{
		    entID = GetControlValue('hidPrntEntID'); /// Have to use the hidPrntEntTpID because the hidEntID for the Case, but the hidPrntEntTpID not be there if there is just a main Ent Tp without a subTbl
			if(entID == '') entID = GetControlValue('hidEntID');
		}
		
		// get just the middle initial
		if(mNm != '') mNm = mNm.substr(0, 1);
		
		fldDupChk_DupNm = (entNm != '' ? entNm + '\'s' : '') + ' Name';		
		fldDupChk_DupLnk = dupLnk;
		fldDupChk_Control = $(mNmID);
		fldDupChk_DupType = null;
		
		xmlHttpFldDupChk = AJAXGetXmlHttpObject();						
		xmlHttpFldDupChk.onreadystatechange = AJAXFldDupChkStateChanged;
		
		var fldDupChkUrl = "/SAM/Cmn/AJAX/FldDupNmChk.ashx?fldid=" + fldID + "&tblid=" + tblID + "&recid=" + entID + "&lnm=" + lNm + "&fnm=" + fNm + "&mnm=" + mNm;
		//document.write(fldDupChkUrl);
		xmlHttpFldDupChk.open("GET", fldDupChkUrl, true);
		xmlHttpFldDupChk.send(null);		
	}			
	else
	{
		// check if the dup alert is triggered for these name fields
		var tr = $('trDup' + mNmID);		
		if(tr != null)
		{
			var tbl = tr.parentNode.parentNode;
			tbl.deleteRow(tr.rowIndex);
			tbl.setAttribute('dupAlert' + mNmID, '0');		
			
			RemoveDupItem(entNm + '\'s Name');			
			ShowDupMsg();
		}
	}
}


// toggles the Mod_History
function ViewModHistory()
{
    var lnk = GetControlByMyID('a', 'lnkModHistory')
	var ctrlModHistory = $('ctrlModHistory');
	//var lnkModHistory = $('lnkModHistory');
	
	if(ctrlModHistory.style.display == 'none')
	{		
		var fldID = GetQueryStringValue('fldid');
		var recID = GetQueryStringValue('entid');
		AJAXShowModHistory('/SAM/Ctrl/AJAX/Mod_History.ashx?fldid=' + fldID + '&recid=' + recID);								
		
		lnk.innerHTML = 'Hide Modification History';
	}
	else
	{
		ctrlModHistory.style.display = 'none';
		lnk.innerHTML = 'View Modification History';
	}
}

var xmlHttpShowModHistory;
var modHistoryContainer;

function AJAXShowModHistory(pageUrl, containerID)
{			
	xmlHttpShowModHistory = AJAXGetXmlHttpObject();
	if(xmlHttpShowModHistory == null)
	{
		AJAXShowError(); return;
	} 		
	modHistoryContainer = $('ctrlModHistory');
	if(modHistoryContainer.style.display == 'none') modHistoryContainer.style.display = '';			
	modHistoryContainer.innerHTML = '<img src="/SAM/Img/Loading.gif" alt="" />';
	xmlHttpShowModHistory.onreadystatechange = AJAXShowModHistoryStateChanged;
	xmlHttpShowModHistory.open("GET", pageUrl, true);
	xmlHttpShowModHistory.send(null);		
}
function AJAXShowModHistoryStateChanged() 
{ 	
	if(xmlHttpShowModHistory.readyState == 4 || xmlHttpShowModHistory.readyState == "complete")	
	{		
		// hide the area if it empty
		if(xmlHttpShowModHistory.responseText != '' && xmlHttpShowModHistory.responseText != ' ') // ajax pages can return blank space 
		{
			modHistoryContainer.innerHTML = xmlHttpShowModHistory.responseText; 						
			ResizeEdtWindow();
		}
		else
			modHistoryContainer.style.display = 'none'; 
	}
}
function ResizeEdtWindow()
{
	var edtFrame = window.parent.$('edtFrame');
	if((document.body.offsetHeight - edtFrame.offsetHeight) < 40)
	{
		edtFrame.style.height = '80%';				
		if(edtFrame.offsetHeight > window.parent.document.body.offsetHeight)
		{
			var ifrmEdt = window.parent.$('ifrmEdt');
			//ifrmEdt.scrolling = true;
		}
	}
	//alert('edtFrame.offsetHeight = ' + edtFrame.offsetHeight + '\ndocument.body.offsetHeight = ' + document.body.offsetHeight + '\nwindow.parent.document.body.offsetHeight = ' + window.parent.document.body.offsetHeight);
	//alert(document.body.offsetHeight);
}
function SetDdlVal_AddIfNotExists(id, val, text)
{
	var ctrl = $(id);
	if(ctrl == null) return;	
	var isExists = false;
	for(var i = 0; i < ctrl.options.length; i++)
	{
		if(ctrl.options[i].value == val)
		{
			isExists = true;
			ctrl.value = val;
			break;
		}
	}
	if(!isExists)DDLAddOption(ctrl, val, text, true);			
}
function SetDdlVal(id, val)
{
	var ctrl = $(id);		
	for(var i = 0; i < ctrl.options.length; i++)
	{
		if(ctrl.options[i].value == val)
		{
			ctrl.value = val;
			return true;
		}
	}
	return false;
}
function SetPhnVal(id, val)
{	
	var phnInt = $(id + '_PhnInt');	
	
	if(phnInt && phnInt != null) // check to make sure that
	{
		// show the international phone number if it does not exists so that we can just set the number as int, even if it domestic (cause its much easier this way)	
		if(phnInt.style.display == 'none') ChngPhnNum(id);
		phnInt.value = val;
	}
}

/// The val is the text (not the RgnID)
function SetRgnVal(id, val)
{
	if(val && val != '')
	{		
		var ctrlSt = $(id + '_st');		
		var isSelected = SetDdlVal(id + '_st', val);		
		//alert('isSelected = ' + isSelected + ' - ctrlSt.style.display = ' + ctrlSt.style.display);
		if(!isSelected)
		{
		    var ctrl = $(id);
		    //alert('SetRgnVal - id = ' + id);
			if(ctrl.style.display == 'none') ToggleStRgn(id + '_a'); // we have to have this on the end because of the normal way that it is passed in						
			ctrl.value = val;
		}
		else if(ctrlSt.style.display == 'none') // make sure that if state is selcted then it is visible
		{
		    ToggleStRgn(id + '_a'); // we have to have this on the end because of the normal way that it is passed in
		}		
	}
	else
	{
		$(id + '_st').value = '';		
		$(id).value = '';		
	}
}
function SetCntVal(id, val)
{
	if(val != '')
	{			
		var ctrl = $(id);		
		var isSelected = false;
		var isSelected = SetDdlVal(id, val);						
		if(!isSelected)
		{					
			ShowAllCountries(id,val);
		}				
	}
}


function ParentGoUsr(usrID) // for mod_history ctrl
{
	window.parent.location = '/SAM/Usr/Usr_Vw.aspx?usrid=' + usrID;
}

/// This can be setup much better by returning a JS object from the AJAX Page but it works for now.
/// Don't use EntTpFld, use EntTpFldID so that don't have to add the EntTpFld
var FldFill = {	
	EntTpID:'', EntID:'',  EntTpFldID:'', /*EntTpFld: '',*/ LNmFld:'', LNmFld2:'', FNmFld:'', FNmFld2:'',  AdrStrFld:'', AdrStr2Fld:'', AdrCtFld:'', AdrRgnFld:'', AdrZipFld:'', AdrCntFld:'', HomePhnFld:'',
	
	Go: function ()
	{
		if(this.EntID != '')
		{
			var url = '/SAM/Ctrl/AJAX/GetEntCmnFldVals.ashx?enttpid=' + this.EntTpID + '&entid=' + this.EntID + '&flds='
			if(this.EntTpFldID != '') url += 'EntTpFldID,'
			//if(this.EntTpFld != '') url += 'EntTpFld,'		
			if(this.LNmFld != '') url += 'LNmFld,'
			if(this.LNmFld2 != '') url += 'LNmFld2,'
			if(this.FNmFld != '') url += 'FNmFld,'
			if(this.FNmFld2 != '') url += 'FNmFld2,'
			if(this.AdrStrFld != '') url += 'AdrStrFld,'
			if(this.AdrStr2Fld != '') url += 'AdrStr2Fld,'
			if(this.AdrCtFld != '') url += 'AdrCtFld,'
			if(this.AdrRgnFld != '') url += 'AdrRgnFld,'
			if(this.AdrZipFld != '') url += 'AdrZipFld,'
			if(this.AdrCntFld != '') url += 'AdrCntFld,'		
			if(this.HomePhnFld != '') url += 'HomePhnFld,'
			url = url.substr(0, url.length-1); // to remove the last ','
			AJAXGetStr(url, FldFill.Response);		
		}
		else
		{
			SetControlValue(FldFill.EntTpFldID, '');
			SetControlValue(FldFill.LNmFld, '');
			SetControlValue(FldFill.LNmFld2, '');
			SetControlValue(FldFill.FNmFld, '');
			SetControlValue(FldFill.FNmFld2, '');
			SetControlValue(FldFill.AdrStrFld, '');
			SetControlValue(FldFill.AdrStr2Fld, '');
			SetControlValue(FldFill.AdrCtFld, '');
			SetControlValue(FldFill.AdrZipFld, '');
			SetControlValue(FldFill.AdrCntFld, '');
						
			SetRgnVal(FldFill.AdrRgnFld, '');
			SetPhnVal(FldFill.HomePhnFld, '');
		}
	},
	
	Response: function ()
	{
		if (xmlHttpGeStr != null && (xmlHttpGeStr.readyState==4 || xmlHttpGeStr.readyState=="complete"))
		{ 
			var xmlDoc = xmlHttpGeStr.responseXML;
			var root = xmlDoc.getElementsByTagName('flds')[0];
			
			for(var i = 0; i < root.childNodes.length; i++)
			{
				var nm = root.childNodes[i].nodeName;

				if(nm == 'EntTpFldID'){ var val = XmlGetVal(root, i); SetDdlVal_AddIfNotExists(FldFill.EntTpFldID, val, val); }
				//else if(nm == 'EntTpFld'){ var val = XmlGetVal(root, i); SetDdlVal_AddIfNotExists(FldFill.EntTpFld, val, val); }				
				else if(nm == 'AdrRgnFld') SetRgnVal(FldFill.AdrRgnFld, XmlGetVal(root, i));
				else if(nm == 'AdrCntFld_ID') 
				{
					var cnt = XmlGetVal(root, i-1); // the cntFld will always be one up from the CntID						
					SetDdlVal_AddIfNotExists(FldFill.AdrCntFld, XmlGetVal(root, i), cnt);				
				}
				else if(nm == 'LNmFld') SetControlValue(FldFill.LNmFld, XmlGetVal(root, i));
				else if(nm == 'FNmFld') SetControlValue(FldFill.FNmFld, XmlGetVal(root, i));
				else if(nm == 'LNmFld2') SetControlValue(FldFill.LNmFld2, XmlGetVal(root, i));
				else if(nm == 'FNmFld2') SetControlValue(FldFill.FNmFld2, XmlGetVal(root, i));														
				else if(nm == 'AdrStrFld') SetControlValue(FldFill.AdrStrFld, XmlGetVal(root, i));
				else if(nm == 'AdrStr2Fld') SetControlValue(FldFill.AdrStr2Fld, XmlGetVal(root, i));
				else if(nm == 'AdrCtFld') SetControlValue(FldFill.AdrCtFld, XmlGetVal(root, i));
				else if(nm == 'AdrZipFld') SetControlValue(FldFill.AdrZipFld, XmlGetVal(root, i));
				else if(nm == 'HomePhnFld'){ SetPhnVal(FldFill.HomePhnFld, XmlGetVal(root, i)); }				
			}			
		} 
	}
}

/// This is for when a table is selected in the Tbl_Select_Popup.aspx
/// Is in the common file because multiple different Js files have the TableChanged func
function TblSelectPopupCtrl_TableChanged(idx, tblID, tblTtl)
{
    var hid = $('Tbl_' + idx);
	hid.value = tblID;
	
	$('lbl_' + idx).innerHTML = tblTtl;

    /// This must come after the first part
    TableChanged(null, idx, tblID);
    $('lnk_' + idx).className='ctrlLnk';
    $('lnk_' + idx).innerHTML='edit';        
    
    //if(hid.name == null) hid.name = hid.id;
    //alert('TblSelectPopupCtrl_TableChanged - hid.value = ' + hid.value + ' - hid.name = ' + hid.name + ' - idx = ' + idx);
}

function AdrPaste(entTpID, isShip)
{
    var iframe = GetMediumIFrame();
    iframe.src = '/SAM/Ctrl/AdrPaste.aspx?enttpid=' + entTpID + '&isship=' + isShip;
}

function AdrPasteClose(){ CloseMediumIframe(); }