//Main Javascript file, Used for Core and General use scripts, Please don't add module specific scripts in here:

//the following 3 lines define 'trim' functions for the String object
String.prototype.ltrim = new Function("return this.replace(/^\\s*/gi, '');"); 
String.prototype.rtrim = new Function("return this.replace(/\\s+$/gi, '');"); 
String.prototype.trim = new Function("return this.replace(/\\s+$|^\\s*/gi, '');"); 
//Prototype a Rounding feature to numbers, Number.round(nDigits).
Number.prototype.round = new Function("nDigits", "var nMult = Math.pow(10, nDigits); return Math.round(this * nMult) / nMult");


//Search Bar Variables and Functions
var sCurrent_Letter = "A"; // Global

//Plugin Directions
var MOVEUP = 1;
var MOVEDOWN = 2;
var MOVELEFT = 3;
var MOVERIGHT = 4;

var SEARCHTYPE_ORGANIZATION = '1';
var SEARCHTYPE_PEOPLE = '2';
var SEARCHTYPE_DISTRICT = '3';
var SEARCHTYPE_SCHOOL = '4';
//var SEARCHTYPE_ALL = '5';
var SEARCHTYPE_ALL = '7';

var DISTRICT_DB_ID = '3';
var SCHOOL_DB_ID = '6';

var SEARCHPATH_ORGANIZATION = "Organizations.aspx";
var SEARCHPATH_PEOPLE = "People.aspx";

var bShowArrows = false;

//Used by the ABC bar to change basetype search.
function ShowTypeMenu( nPos ){
	var oSearchMenu = document.all.DropDownSearchMenu;
	var nOffSetRatio = (nPos - 65)/26 ;
	sCurrent_Letter = String.fromCharCode(nPos);// Set Global
	oSearchMenu.all.CurrentLetter.innerHTML = sCurrent_Letter;
	oSearchMenu.style.top = 5;
	oSearchMenu.style.display = "block";
	oSearchMenu.style.left = event.x - oSearchMenu.scrollWidth * nOffSetRatio;
}
//Used by the old ABC bar to change basetype search.
function ShowNumberMenu(){
	var oSearchMenu = document.all.DropDownSearchMenu;
	var nOffSetRatio = 1/26 ;
	sCurrent_Letter = "0-9";// Set Global
	oSearchMenu.all.CurrentLetter.innerHTML = sCurrent_Letter;
	oSearchMenu.style.top = 5;
	oSearchMenu.style.display = "block";
	oSearchMenu.style.left = event.x - oSearchMenu.scrollWidth * nOffSetRatio;
}

//Used by the new ABC bar to change the basetype search.
function ShowSearchTypeDropDownMenu()
{
	var oSearchTypeDropDownMenu = document.all.SearchTypeSelect;
	var varDetectCollapse = document.getElementById('txtDetectCollapse').value;
	
	oSearchTypeDropDownMenu.style.display = "block";
	
	if(document.getElementById('alertsbarpresent').value == 'true')
	{
		oSearchTypeDropDownMenu.style.top = 55;
	}
	else
	{
		oSearchTypeDropDownMenu.style.top = 33;
	}
	
	if(varDetectCollapse == "0")
	{
		oSearchTypeDropDownMenu.style.left = 168;
	}
	else
	{
		oSearchTypeDropDownMenu.style.left = 34;
	}
}

/*
function ToggleSearchIcon(sSearchType)
{
	if(sSearchType == 'districts')
	{
		document.getElementById('search_icon').src = 'Images\\icons\\org_17x17.gif';
	}
	else if(sSearchType == 'schools')
	{
		document.getElementById('search_icon').src = 'Images\\icons\\bell_13x15.gif';
	}
	else if(sSearchType == 'people')
	{
		document.getElementById('search_icon').src = 'Images\\icons\\person_16x16.gif';
	}
	else if(sSearchType == 'all')
	{
		document.getElementById('search_icon').src = 'Images\\icons\\peach_14x15.gif';
	}
	else
	{
		//
	}
}
*/

//Used by the ABC bar to search for basetypes
function DoSearch(sInputName, sKey, sPageID )
{

	// the current type of the search is embedded in the hidden input field
	var sType;
	sType = document.getElementById(sInputName).value;
	
	var sPath;			// the path for the redirect (each page will do a different search process
	var sSubType;			// an argument defining the type of the entity for which to search

	switch(sType)
	{
		case SEARCHTYPE_ORGANIZATION:
		{
			sPath = SEARCHPATH_ORGANIZATION;
			sSubType = 0;
			break;
		}
		case SEARCHTYPE_PEOPLE:
		{
			sPath = SEARCHPATH_PEOPLE;
			sSubType = 0; 
			break;
		}
		case '':// the empty case occurs at the beginning when user hasn't explicitly selected a type
		{
			sPath = SEARCHPATH_ORGANIZATION;
			sSubType = DISTRICT_DB_ID;
			sType = SEARCHTYPE_DISTRICT;

			break;
		}
		
		case SEARCHTYPE_DISTRICT:
		{
			sPath = SEARCHPATH_ORGANIZATION;
			sSubType = DISTRICT_DB_ID;

			break;
		}
		case SEARCHTYPE_SCHOOL:
		{
			sPath = SEARCHPATH_ORGANIZATION;
			sSubType = SCHOOL_DB_ID;
			break;
		}
		case SEARCHTYPE_ALL:
		{
			sPath = SEARCHPATH_ORGANIZATION;
			sSubType = 0;
			break;
		}
		default:// default to all
		{
					alert("The system cannot search with filter " + sType + ". All entities will be searched.");
					sPath = SEARCHPATH_ORGANIZATION;
		
		}
	}


	var sLocation = sPath + "?key=" + sKey;
	if(sPageID == PT_POPUP || sPageID == PT_IFRAME || sPageID == PT_DIRECTORY)
		sLocation += "&p=" + sPageID;
	if(sPageID == PT_IFRAME || sPageID == PT_DIRECTORY)
		sLocation += "&oid=" + ORGID;
		
	// add the type
	sLocation += "&SubType=" + sSubType;
	sLocation += "&SearchType=" + sType;
		
	window.location = sLocation;
	
	return false;
}

//Used by Clients detail to show the Add User Frame.
function ToggleIFrame(oButton, oIFrame, sButtonText, sSource){
	if(oIFrame.style.display != "block"){
		oIFrame.style.display = "block";
		oIFrame.src = sSource; //+ "&x=y";
		//alert(oIFrame.tagName);
		oButton.innerText = "Hide";	
	}
	else{
		oIFrame.style.display = "none";
		oButton.value = sButtonText;	
	} 
}
function AddOrgToUser(nOrgID, sOrgName){
	var oTheForm = document.EditBaseType;
	var oAddOrg = GetObjectArray(oTheForm, "AddOrg");
	var bAddIt = true;
	for(x=0; x < oAddOrg.length; x++){ //Check to see if user already added
		if(oAddOrg[x].value == nOrgID){
			alert("Organization already contains this Contact");
			bAddIt = false;
			break;
		}
	}
	if(bAddIt){
		var oOrgTable = document.all.Organizations;
		oOrgTable.insertRow();
		var oRow = oOrgTable.rows(oOrgTable.rows.length - 1);
		oRow.insertCell();
		var sNewOrg = "<a href='Organizations.aspx?oid=" + nOrgID + "'> " + sOrgName + "</a><input type='hidden' name='AddOrg' id='AddUser" + nOrgID + "' value='" + nOrgID + "'>";
			sNewOrg += "<button class='" + CLASS_ICONBUTTON + "' title='click to remove " + sOrgName + "' onclick='this.parentElement.parentElement.parentElement.deleteRow(this.parentElement.parentElement.rowIndex);'>";
			sNewOrg += "<img src='" + ICON_BREAKLINK + "' /></button>";
		oRow.cells(0).innerHTML = sNewOrg;
	}	
}

function AddUserToOrg(nUserID, sUserName){
	var oTheForm = document.EditBaseType;
	var oAddUsers = GetObjectArray(oTheForm, "AddUser");
	var bAddIt = true;
	for(x=0; x < oAddUsers.length; x++){ //Check to see if user already added
		if(oAddUsers[x].value == nUserID){
			alert("Contact already in Organization");
			bAddIt = false;
			break;
		}
	}
	if(bAddIt){
		var oUserTable = document.all.Contacts;
		oUserTable.insertRow();
		var oRow = oUserTable.rows(oUserTable.rows.length - 1);
		oRow.insertCell();
		var sNewUser = sUserName + "<input type='hidden' name='AddPerson'  value='" + nUserID + "'>";
			sNewUser += "<input type='image' src='" + ICON_BREAKLINK + "' class='" + CLASS_ICONBUTTON + "' onclick='this.parentElement.parentElement.parentElement.deleteRow(this.parentElement.parentElement.rowIndex);'/>";
		oRow.cells(0).innerHTML = sNewUser;
	}
}
function RemoveChild(oFormEle){
	oFormEle.value = (-1 * Math.abs(oFormEle.value));
	oFormEle.parentElement.style.textDecoration = "line-through";
	//--OR--
	//oFormEle.parentElement.parentElement.style.display = "none"
}

function RemoveFormItem(oFormEle, oFormDiv){
	oFormDiv.style.display = "none";
	oFormEle.value = (Math.abs(oFormEle.value) * -1);
}

function GetObjectArray( oFormObject, sEleName){
	var oArray = new Array();
	var oObject;
	try{
		oObject = eval("oFormObject." + sEleName);
		var x = oObject.index;
	}
	catch(e){
		//alert("None");
		return oArray;
	}
	
	if(!oObject.length){
		//alert("ONE");
		oArray[0] = oObject;
	}
	else{
		//alert("Many");
		oArray = oObject;
	}
	return oArray;
}
function ToggleTableSection(sSectionID, sflipFlop){
	var oSectionID = document.getElementById(sSectionID);
	var oflipFlop = document.getElementById(sflipFlop);
	if(oSectionID.style.display == "none"){
		oSectionID.style.display = "block";	
		oflipFlop.src = "images/arrow_D01w.gif";		
	}
	else{
		oSectionID.style.display = "none";	
		oflipFlop.src = "images/arrow_R01w.gif";
	}	
}

function ToggleSection(oSectionID,oButtonImg){
	if(oSectionID.style.display == "none"){
		oSectionID.style.display = "block";
		oButtonImg.src = oButtonImg.src.replace(/_dn/, "_up");
	}
	else{
		oSectionID.style.display = "none";
		oButtonImg.src = oButtonImg.src.replace(/_up/, "_dn");
	}	
}

function SwitchTabs(oTabArray, oSectionArray, nID){
	for(x=0; x < oSectionArray.length; x++){
		oSectionArray[x].style.display = "none";
		oTabArray[x].className = "tdNavDW";
		oTabArray[x].GBnormalClass = "tdNavDW";
		if(x == nID){
			oSectionArray[x].style.display = "block";
			oTabArray[x].className = "tdNavDWSel";
			oTabArray[x].GBnormalClass = "tdNavDWSel";
		}
	}	
	

}
function SwitchTaskTabs(nID){
	var oTabArray= document.all.TabArray;
	var oSectionArray = document.all.TabbedTask;
	var OpriorityArray = document.all.TabbedTask1;
	if( oSectionArray != null)
	{
		for(x=0; x < oSectionArray.length; x++){
			oSectionArray[x].style.display = "none";
			OpriorityArray[x].style.display = "none";
			oTabArray[x].className = "tdNavDW";
			oTabArray[x].GBnormalClass = "tdNavDW";
			if(x == nID){
				oSectionArray[x].style.display = "block";
				OpriorityArray[x].style.display = "block";
				oTabArray[x].className = "tdNavDWSel";
				oTabArray[x].GBnormalClass = "tdNavDWSel";
			}
		}	
	}

}

function ToggleSectionNoImage(oSectionID){
	if(oSectionID.style.display == "none"){
		oSectionID.style.display = "block";
	}
	else{
		oSectionID.style.display = "none";
	}	
}

function MovePlugin(oDiv, nDirection){
	//alert(nDirection);
	var oColumns = document.all.Columns;
	var oPluginCollection = oDiv.parentElement.all.Plugin;
	var sHolding = ""
	var nCurRow = oDiv.all.Row.value;
	var nCurCol = oDiv.all.Col.value;
	switch(nDirection){
		case MOVEUP:
			var oNewDiv = oPluginCollection[nCurRow-2]
			sHolding = oNewDiv.innerHTML;
			oNewDiv.innerHTML = oDiv.innerHTML;
			oDiv.innerHTML = sHolding;
			oNewDiv.all.Row.value = nCurRow;
			break;
		case MOVEDOWN:
			var oNewDiv = oPluginCollection[nCurRow]
			sHolding = oNewDiv.innerHTML;
			oNewDiv.innerHTML = oDiv.innerHTML;
			oDiv.innerHTML = sHolding;
			oNewDiv.all.Row.value = nCurRow;
			break;
		case MOVELEFT:
			var oNewDiv = document.createElement("div");
			oNewDiv.id = "Plugin";
			oColumns[nCurCol-2].appendChild(oNewDiv);	
			sHolding = oNewDiv.innerHTML;
			oNewDiv.innerHTML = oDiv.innerHTML;
			oDiv.innerHTML = sHolding;
			oNewDiv.all.Row.value = nCurRow;
			oColumns[nCurCol-1].removeChild(oDiv);
			break;
		case MOVERIGHT:
			var oNewDiv = document.createElement("div");
			oNewDiv.id = "Plugin";
			oColumns[nCurCol].appendChild(oNewDiv);	
			sHolding = oNewDiv.innerHTML;
			oNewDiv.innerHTML = oDiv.innerHTML;
			oDiv.innerHTML = sHolding;
			oNewDiv.all.Row.value = nCurRow;
			oColumns[nCurCol-1].removeChild(oDiv);
			break;
	}
	UpdateButtons()
}
function UpdateButtons(){
	var oColumns = document.all.Columns
	for(c=0; c < oColumns.length; c++){
		var oButtons = oColumns[c].all.EditButtons;
		
		//alert(oButtons);
		
		var oButtonLen = 0;
		try{
			oButtonLen = oButtons.length;
		}
		catch(e){
			oButtonLen = 0;
		}
		
		if(oButtonLen > 0){
			for(r=0; r < oButtons.length; r++){
				HideShowButtons(oButtons[r], oButtons.length, oColumns.length-1 )
			}
		}
		else if(oButtons){
			//alert("Zero Length");
			HideShowButtons(oButtons, 0, oColumns.length-1 );
		}
		else{
			//alert("no Object")
		}
	}
}
function HideShowButtons(oTheButtons, nButtonsLen, ColumnLength){
	var sButtonHTML;
	//alert(oTheButtons)
	var oDiv = oTheButtons.parentElement;
	//var oPluginCollection = oDiv.parentElement.Plugin;
	//var oPluginCollection = oDiv.parentElement.all.Plugin;
	//Up Button
	oTheButtons.all.UpButton.style.display = "none";
	if(r > 0) 
		oTheButtons.all.UpButton.style.display = "inline";
	//Down Button
	oTheButtons.all.DownButton.style.display = "none";
	if(r < nButtonsLen - 1) 
		oTheButtons.all.DownButton.style.display = "inline";
	//Left Button
	oTheButtons.all.LeftButton.style.display = "none";
	if(c > 0) 
		oTheButtons.all.LeftButton.style.display = "inline";
	//Right Button
	oTheButtons.all.RightButton.style.display = "none";
	if(c < ColumnLength) 
		oTheButtons.all.RightButton.style.display = "inline";
	oDiv.all.Col.value = c+1;
	oDiv.all.Row.value = r+1;
}

//Delete Item
//Simple Confirm before redir to delete page
function ConfirmDelete(sURL){
	if(confirm('This will permanently delete this object.')){
		window.location=sURL;
	}
}

//    oFormEle is the Formelement triggering the change, oTheTable is the table containg all the  rows, oSecRow is the Row to be added
function AddRepeatingRow(oFormEle, oTheTable, oSecRow ){
	oTheTable.tBodies[0].appendChild(oSecRow); // Add the new row
	oSecRow = oSecRow.cloneNode(true); // get a new copy of the row
	oFormEle.onchange = null; // remove the trigger
	return oSecRow; // return the new copy to be assigned to the next new row.
}// end function AddRepeatingRow

//FEIN Validation
function SetFEIN(type,frmEle){
	var val = new String(frmEle.value);
	val = val.replace("-","");
	if(type == 0){
		frmEle.value = val;
		frmEle.maxLength = 9;
	}
	else{
		if(val.length > 8){
			var newval = "";
			newval = val.substr(0,2) + "-" + val.substr(2,7);
			frmEle.maxLength = 10;
			frmEle.value = newval;	
		}	
	}
}// end function SetFEIN


function ToggleTabs(intTotalTabs, intTabNumber, sPluginName, oTab)
{
	document.getElementById(sPluginName + 'Tab' + intTabNumber + 'Content').style.display = 'block';
	oTab.style.display = 'block';
	oTab.className = 'tdPluginTabSelected';
	
	for(i = 0; i < intTotalTabs; i++)
	{
		if(intTabNumber != i + 1)
		{
			//document.getElementById(sPluginName + 'Tab' + (i + 1)).style.display = 'none';
			document.getElementById(sPluginName + 'Tab' + (i + 1)).className = 'tdPluginTab';
			document.getElementById(sPluginName + 'Tab' + (i + 1) + 'Content').style.display = 'none';
		}
	}
}

function CollapseNav()
{
	document.getElementById('NavColumn').style.display = 'none';
	document.getElementById('CollapseNavButton').style.display = 'none';
	document.getElementById('ExpandNavButton').style.display = 'block';
	//txtDetectCollapse helps drop down menu to position itself to coordinate with collapse/expand navigation block.
	document.getElementById('txtDetectCollapse').value = "1";
	document.all.SearchTypeSelect.style.display = 'none';
}

function ExpandNav()
{
	document.getElementById('NavColumn').style.display = 'block';
	document.getElementById('CollapseNavButton').style.display = 'block';
	document.getElementById('ExpandNavButton').style.display = 'none';
	//txtDetectCollapse helps drop down menu to position itself to coordinate with collapse/expand navigation block.
	document.getElementById('txtDetectCollapse').value = "0";
	document.all.SearchTypeSelect.style.display = 'none';
}

// redirects the page to the favorites page
function AddPageToFavorites(nFavoriteMode)
{
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
	window.location = "Favorites.aspx?obj="+0+"&p="+nFavoriteMode+"&NewFavorite=" + sPage
}

function ResetSearchGPSFrm()
					{	
						if(document.forms["frmSearchGPS"])					
							document.forms["frmSearchGPS"].reset();
					}