//
// General.js
// ========================
var JSS_MsgMarkCompNoneSel = "You have to select at least one task in order to mark it as completed.";
var JSS_MsgConfirmDelEntries = "Are you sure you want to delete the selected entry(-ies)?";
var JSS_MsgConfirmDelEntry = "You have to select an entry in order to delete it.";
var JSS_MsgSelEntryToAddContact = "To add a contact you must select an entry first.";
// Copied from AmAddressBook.aspx

// ******************************************************************************
								  // IF YOU MOVED THE PROJECT UNDER ANOTHER NAME
//								   alert(top.location.href);
// ******************************************************************************

var portalStart = "";

DefinePortalStart(top);
			
var LastSelectedRow;	// to know, what was selected last time.
var SelectedItem;	
var grid_background_color = 'white';
var currentRow;
var lastCurrentRow;
//var currentKey;
var selectionColor = '#8fa8c1';	// color of the selected row of the grid

if (document.layers) {
	visible = 'show';
	hidden = 'hide';
} else if (document.all) {
	visible = 'visible';
	hidden = 'hidden';
}

var lastMenuStyle = null;
var lastMenu=null;
var mouseoverset = true;


// Keyhandler - assign the event to the function
// ******************************************************************************
//document.onkeypress = keyhandler;

// ******************************************************************************
function keyhandler(e) {
	if (document.layers)
		Key = e.which;
	else
		Key = window.event.keyCode;
	if (Key == 27) 	// On ESC - close the popup
	{
		try
		{
			parent.refreshParent();
		}
		catch(e)
		{
		}
		if(window.dialogArguments != null)
		{
			window.close();
		}
	}
}

// ******************************************************************************
function DefinePortalStart(win)
{
	var h = new String(win.top.location.href);
	s = h.lastIndexOf("/");
	if(s>0)
		portalStart = h.substring(0,s);
}


// changes selected item object
// ******************************************************************************
function setProps (e) {

	if (typeof(SelectedItem) != "undefined")
	{
		SelectedItem.style.backgroundColor = grid_background_color;
	}
	SelectedItem = e.parentElement.parentElement;				
}


// ******************************************************************************
function switchToABAndSearch()
{
	//alert('1');
	top.location.href = "AddressBook.aspx?forceadvsearch=1";
	adv_ss = window.showModalDialog(portalStart  + "/Dialogs/AdvSearchDlg.aspx?ownertype=ABENTRY", window, "edge: Raised;help:no;resizable:no; status:yes; scroll:no; dialogWidth:620px; dialogHeight:380px; ");		
	if(adv_ss != '' && adv_ss != 'undefined' && adv_ss)
	{

		document.getElementById("AdvNewSearch").value = adv_ss;
		AmTemplate.submit();
	}

	return false;
	
}


// Calls Quick Search dialog box form
// ******************************************************************************
function showSearchPrompt(c, s)	// c - field codename , s - displayed field name
{
	AmTemplate.SearchByCode.value = c;
	AmTemplate.SearchBy.value = s;
	
	adv_ss = window.showModalDialog(portalStart + "/Dialogs/QuickSearchDlg.aspx?c=" + c + "&s=" + escape(s), window, "edge: Raised;help:0;resizable:0; status:1; scroll:0; dialogWidth:370px; dialogHeight:235px; ");		
	if(adv_ss != '' && adv_ss != 'undefined' && adv_ss)
	{
		document.getElementById("AdvNewSearch").value = adv_ss;
		AmTemplate.submit();
	}
}


// ******************************************************************************
function showSearchPromptDate(c, s, blDoRange)	// c - field codename , s - displayed field name, blDoRange - show range
{
	AmTemplate.SearchByCode.value = c;
	AmTemplate.SearchBy.value = s;
	
	adv_ss = window.showModalDialog(portalStart + "/Dialogs/QuickSearchDlgDate.aspx?c=" + c + "&s=" + escape(s) + "&doRange=" + blDoRange, window, "edge: Raised;help:0;resizable:0; status:1; scroll:0; dialogWidth:400px; dialogHeight:285px; ");		
	if(adv_ss != '' && adv_ss != 'undefined' && adv_ss)
	{
		document.getElementById("AdvNewSearch").value = adv_ss;
		AmTemplate.submit();
	}
}

////////////////
//	Copied from IFAbGrid.aspx
/////////

// change selection	- set on		
// ******************************************************************************
function makeCurrent(r_id, key)
{
	removeCurrent();
	
	document.forms[0].currentKey.value = key;
	
	document.forms[0].SelectedRowIndex.value = r_id;

	currentRow = document.all.item("PrevRow" + r_id);

	if (currentRow == 'undefined' || !currentRow)
	{
		return;
	}
	
	lastCurrentRow = currentRow;
		
	//currentRow.style.backgroundColor = selectionColor ;
	currentRow.style.backgroundImage = 'url(../../images/ABGrid/selection_bg.jpg)';
	prevColor = selectionColor ;
}

// change selection - set off
// ******************************************************************************
function removeCurrent()
{
	if (lastCurrentRow != null && lastCurrentRow != 'undefined')
	{
		//lastCurrentRow.style.backgroundColor = grid_background_color;
		lastCurrentRow.style.backgroundImage = 'none';
	}
}

// This function expands/collapses the brief info of the selected entry
// ******************************************************************************
function show_brief_details(r_id)
{
	var ItemToShow = document.all.item("dsGrid_theRow" + r_id);
	var PrevRow = document.all.item("PrevRow" + (r_id));

	if (ItemToShow.style.display=="none")
	{
		// show item
		ItemToShow.style.display = "";
		if (typeof(LastSelectedRow) != "undefined")
		{
			LastSelectedRow.style.backgroundColor = grid_background_color;
		}
		LastSelectedRow = PrevRow;
		setTimeout("document.forms[0].exp_im_" + r_id + ".src = '../../images/ABGrid/collapse.gif'", 10);
	}
	else
	{
		// hide item
		ItemToShow.style.display = "none";
		PrevRow.style.backgroundColor = grid_background_color;
		prevColor =  grid_background_color;
		setTimeout("document.forms[0].exp_im_" + r_id + ".src = '../../images/ABGrid/expand.gif'", 10);
	}
}

// submit grid form
// ******************************************************************************
function submit_me()
{
	document.forms[0].submit();
}

// ******************************************************************************
function customPopupCall ( popupASPX, key, W, H )
{

	if(document.forms[0].currentKey != null && document.forms[0].currentKey != 'undefined')
	{
		
		masterKey  =	document.forms[0].currentKey.value;
	}
	else
	{
		masterKey  =	"";
	}
	
	window.showModalDialog(
	    portalStart + "/" + popupASPX + "?key=" + 
	    key +  "&masterkey=" + masterKey, 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:no; caption:'WooHoo'; dialogWidth:" + W + "px; dialogHeight:" + H + "px; " );
}

// ******************************************************************************
function customPopupCallEx ( popupASPX, isLead, type, W, H )
{

	

	document.body.style.cursor="wait";
	parent.closeMenu();
	
	var doAddContact = "";
	
	key = "";
	
	if(type == "Contact")
	{
		key = document.forms[0].currentKey.value;
		if(key == "")
		{
			document.body.style.cursor='default';
			alert(JSS_MsgSelEntryToAddContact);
			return;
		}
		doAddContact= "&addContact=1";
	}

	window.showModalDialog(
	    portalStart + "/" + popupASPX + "?key=" + 
	    key + "&entryType="+type + "&masterKey=" + document.forms[0].currentKey.value +
	    "&isLead=" + isLead + doAddContact, 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:no; caption:'WooHoo'; dialogWidth:" + W + "px; dialogHeight:" + H + "px; " );
	   
	document.body.style.cursor='default';
}


// open popup dialog when a user clicks on a row
// ******************************************************************************
function openPopup(key, type)
{

	document.body.style.cursor="wait";
	
	try{
	parent.closeMenu();
	}
	catch(e){}
	
	if(document.forms[0].currentKey != null && document.forms[0].currentKey != 'undefined')
	{
		
		masterKey  =	document.forms[0].currentKey.value;
	}
	else
	{
		masterKey  =	"";
	}
	
	if(true)
	{
	window.showModalDialog(
	    portalStart + "/AmEntryPopup.aspx?key=" + 
	    key + "&type="+type + "&masterkey=" + masterKey, 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:no; caption:'WooHoo'; dialogWidth:750px; dialogHeight:560px; " );
	}
	else
	{
	window.open(
	    portalStart + "/AmEntryPopup.aspx?key=" + 
	    key + "&type="+type + "&masterkey=" + masterKey);
	}
	    
	document.body.style.cursor='default';

}


// open popup dialog when a user clicks on a row
// ******************************************************************************
function openPopupEx(key, type, isLead)
{	
	document.body.style.cursor="wait";
	parent.closeMenu();
	
	var doAddContact = "";
	
	if(key == "AddContact")
	{
		key = document.forms[0].currentKey.value;
		if(key == "")
		{
			document.body.style.cursor='default';
			alert( JSS_MsgSelEntryToAddContact );
			return;
		}
		doAddContact= "&addcontact=1";
	}

	window.showModalDialog(
	    portalStart + "/AmEntryPopup.aspx?key=" + 
	    key + "&entryType="+type + "&masterkey=" + document.forms[0].currentKey.value +
	    "&isLead=" + isLead + doAddContact, 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:no; caption:'WooHoo'; dialogWidth:750px; dialogHeight:560px; " );
	   
	document.body.style.cursor='default';
	    
	
}

// ******************************************************************************
function openABEntryPopup(key)
{
	
	openPopup(key, 0);
}

// ******************************************************************************
function openOpportunityEntryPopup(key)
{
	openPopup(key, 1);
}

// ******************************************************************************
function openCSCasesEntryPopup(key)
{
	openPopup(key, 2);
}

// ******************************************************************************
function openCampaignEntryPopup(key)
{
	openPopup(key, 3);
}

// ******************************************************************************
function openArticlePopup(key,iType,sMode)
{
	//key-article key
	//iType: 0-Standard article; 1-DocumentType article
	//sMode (open mode): 'edit'- view/edit article; 'addNew'-add standard type article
	var height;
	if (iType==0) 
		{
			height=540;	
		}
	else
		{
			height=350;
		}
		
		
	var res = window.showModalDialog(
	
	    portalStart +"/PopupContent/KBase/contentArticleInfo.aspx?_key="+key+"&type="+iType+"&mode="+sMode, 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:auto; caption:'WooHoo'; dialogWidth:" + 700 + "px; dialogHeight:" +height + "px; " );
	    
	if(res != null)
		document.forms[0].submit();
}

// Search Any Field (KBase) ********************************
// ******************************************************************************
function openKBSearchAnyField()
{
	var res = window.showModalDialog(
	
	    portalStart +"/Dialogs/KBase/SearchAnyField.aspx", 
	    window, 
	    "edge: Raised;help:no;resizable:no; status:yes; scroll:auto; caption:'WooHoo'; dialogWidth:" + 700 + "px; dialogHeight:" +340 + "px; " );
	    
	  if(res == "1")
	  {
		document.forms[0].submit();
		return true;
	  }
	  return false;
}

// Edit commands **********************************
// ******************************************************************************
function selectAll()
{

	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			
			if ( cbox.id != "CarryForwardCheck" )
			{
			cbox.checked = true;
			}
		}
	}
}


// ******************************************************************************
function deselectAll()
{ 
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			
			if ( cbox.id != "CarryForwardCheck" )
			{
			cbox.checked = false;
			}
		}
	}
}


// ******************************************************************************
function inverseSelection()
{ 
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			if ( cbox.id != "CarryForwardCheck" )
			{
				if(cbox.id.indexOf('SelectAllCheckbox')<0)
				{
					//alert(cbox.id.indexOf('SelectAllCheckbox'));
					cbox.checked = !cbox.checked;
				}
				else
				{
					cbox.checked = false;
				}
			}
		}
	}
}			


// ******************************************************************************
function markAsCompleted()
{
	var nothing_to_mark = true;
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			
			if (cbox.checked == true)
			{
				if (cbox.checked && cbox.id != "CarryForwardCheck")
				{
					nothing_to_mark = false;
					
					break;
				}
			}
		}
	}
	if (!nothing_to_mark)
	{
			document.forms[0].ActionMode.value = "MarkAsCompleted";
			submit_me();
	}
	else
	{
		alert( JSS_MsgMarkCompNoneSel );
	}
}


// ******************************************************************************
function deleteSelected()
{
	var nothing_to_delete = true;
	
	
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			
			if (cbox.checked && cbox.id != "CarryForwardCheck")
			{
				nothing_to_delete = false;
				
				break;
			}
		}
	}
	if (!nothing_to_delete)
	{
			if (confirm( JSS_MsgConfirmDelEntries ))
			{
				document.forms[0].ActionMode.value = "Delete";
				submit_me();
			}
	}
	else
	{
		alert( JSS_MsgConfirmDelEntry );
	}
}

// ******************************************************************************
function addAutoCampaign()
{
    return window.showModalDialog(
        portalStart +"/Dialogs/Campaigns/AutomatedCampaign.aspx", 
        window, 
        "edge: Raised;help:0;resizable:0; status:1; scroll:0; dialogWidth:600px; dialogHeight:450px; ");		
}

// popup Opp summary dialog
// ******************************************************************************
function ShowOppSummaryDialog(StrategyID, NotSupportedMessage, WrongStrategyMessage)
{
	if(StrategyID == 0)
	{
		alert(NotSupportedMessage);
		return false;
	
	}
	else if(StrategyID != -1 )
	{
		alert(WrongStrategyMessage);
		return false;
	}
	
    return window.showModalDialog(
        portalStart +"/Dialogs/Opportunity/OppProgressDates.aspx?key=" + 
        document.forms[0].currentKey.value, 
        window, 
        "edge: Raised;help:0;resizable:0; status:1; scroll:0; dialogWidth:330px; dialogHeight:290px; ");		
}

// popup Opp Amalysis dialog
// ******************************************************************************
function ShowOppAnalysisDialog()
{
    return window.showModalDialog(
        portalStart +"/Dialogs/Opportunity/OppAnalysis.aspx?key=" + 
        document.forms[0].currentKey.value, 
        window, 
        "edge: Raised;help:0;resizable:0; status:1; scroll:0; dialogWidth:330px; dialogHeight:150px; ");		
}

// ******************************************************************************
function hideContacts()
{
	AmEntryPopup.canSwitchLeftMenu.value = "false";
	if(AmEntryPopup.btnAddContact != null && AmEntryPopup.btnAddContact != 'undefined')
	{	AmEntryPopup.btnAddContact.style.display='none';	}
	AmEntryPopup.arrowUp.style.display='none';
	AmEntryPopup.arrowDn.style.display='none';
	AmEntryPopup.curMode.value	= '';
}

// ******************************************************************************
function showContacts()
{
	AmEntryPopup.canSwitchLeftMenu.value = "true";
	if(AmEntryPopup.btnAddContact != null && AmEntryPopup.btnAddContact != 'undefined')
	{	AmEntryPopup.btnAddContact.style.display='';	}
	AmEntryPopup.arrowUp.style.display='';
	AmEntryPopup.arrowDn.style.display='';
	AmEntryPopup.curMode.value	= '';
}





//	*********



// ******************************************************************************
function selectAllFromChBox(chID)
{
	selection = document.getElementById(chID).checked;
	
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' )
		{
			var cbox = document.all(c);
			
			cbox.checked = selection;
			
		}
	}
}


// ******************************************************************************
function selectAllFromChBoxButOne( chID, excludeID )
{
	selection = document.getElementById(chID).checked;
	
	for(c=0;c<document.all.length;c++)
	{
		if ( document.all(c).type == 'checkbox' && document.all(c).id != excludeID )
		{
			var cbox = document.all(c);
			
			cbox.checked = selection;
			
		}
	}
}


// ******************************************************************************
function FeatureHasNotImplemented()
{
	alert("Not yet implemented for this Beta release.");
}


var animTextLen=0, animTextCur=234224;
var animText,animCnt=1;
var animStart = true;

// ******************************************************************************
function animateText(spanId)
{

	spanTxt = document.all(spanId).innerText;
	

	if(animTextCur % 2) 
	{
		document.all(spanId).innerHTML = "<font face=helvetica size=1 color=green><b>" + spanTxt + "";
	}
	else
	{
		document.all(spanId).innerHTML = "<font face=helvetica size=1 color=black><b>" + spanTxt + "";
	}

	animTextCur ++;
	
	setTimeout("animateText('" + spanId + "')", 150);
	
}

// Hides waiting message on top of AddressBook page.
// ******************************************************************************
function hideWm()
{
	try
	{
		if(parent.document.all('waitmessage'))
		{
			parent.document.all('waitmessage').innerText = '';
		}
		else
		{
			document.all('waitmessage').innerText = '';
		}
	}
	catch(e)
	{
	
	}
}



// ******************************************************************************
function openModalWindow_Gen(url,w,h)
{

	return window.showModalDialog(
	    url, 
	    window, 
	    "edge:raised;help:no;resizable:no;status:yes;scroll:no;dialogWidth:"+w
	    +"px;dialogHeight:"+h+"px;" );
}


// ******************************************************************************
function restrictInputForDouble ( elem, numSeparator ) // onkeypress event handler
{
	var Key = window.event.keyCode;
	var result = false;
	var numSeparatorCode = asc(numSeparator.charAt(0));
	
	if ( elem.value.indexOf ( numSeparator ) >-1 && Key == numSeparatorCode )	// no more than one dot in the text
	{
		result = false;
	}
	else if ( elem.value.indexOf ( "-" ) >-1 && Key == 45 )	// no more than one minus sign in the text
	{
		result = false;
	}
	else
	{
		result = ((Key > 47 && Key < 58) || Key == 45 || Key == numSeparatorCode || Key == 9 || Key == 27 || Key == 13) 	// Digits(0..9), dot (.), Enter, Esc and Tab
	}

	return result;
}

// ******************************************************************************
function asc(each_char)
{
	var n = 0;
	var char_str = charSetStr();
	for (i = 0; i < char_str.length; i++)
	{
		if (each_char == char_str.substring(i, i+1))
		{
			break;
		}
	}
	return i + 32;
}

// ******************************************************************************
function charSetStr()
{
	var str;
	str = ' !"#$%&' + "'" + '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
	return str;
}


// ******************************************************************************
function allowDeleteOnly ( elem )
{
	Key = window.event.keyCode;
	
	if ( Key == 27 )
		return true;
	if ( Key == 46 || Key == 8 )
	{
		elem.value = "";
		return true;
	}
	return false;
}


// ******************************************************************************
if ( typeof GBI == 'undefined' )
{
	GBI = document.getElementById;
}

// ******************************************************************************
window.top.scroll(0,0);	//??
// ******************************************************************************

