/* ///////////////////////////////////

	Locator Details Display Controls

*/ ///////////////////////////////////


var detailToggleState = new Boolean();


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	This function drives the display of the state-wide contact
//  list display.
// 	It is called from the links in the results short list 
//	and from from state and base icon interactions on the map.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function toggleDetails (fromWhere, listLength, baseName)
{
	var detailsBlock = document.getElementById('results_details');
	
	
	// Check the toggle state. 
	// Since the variable is declared as a Boolean, it will default to 0
	if (detailToggleState == 0)
	{
		detailToggleState = 1;
		detailsBlock.style.display = "block";
		//clearDetailsList();
		
		// If a base-level details link is clicked
		if (fromWhere == "base")
		{
			makeBaseDetailsList(listLength, baseName);
		}
		else if (fromWhere == "map")
		{
			detailToggleState = 0;
			detailsBlock.style.display = "none";
			clearDetailsList();
		}
		// Display state-wide list when the state link is clicked
		else if (fromWhere == "state")
		{
			makeStateDetailsList(listLength);
			document.getElementById("state_details").innerHTML = "Hide All";
		}
	}
	else
	{
		// If the details list is already open and a display link is clicked
		// in the results short list, this will leave it open and refill the results
		if (fromWhere == "base")
		{
			makeBaseDetailsList(listLength, baseName);
			document.location.href = "#details";
		}
		
		// Clear the list and hide the full state-wide list
		else if (fromWhere == "state")
		{
			detailToggleState = 0;
			detailsBlock.style.display = "none";
			clearDetailsList();
			document.getElementById("state_details").innerHTML = "Show All";
		}
		else if (fromWhere == "list")
		{
			detailToggleState = 0;
			detailsBlock.style.display = "none";
			clearDetailsList();
			document.getElementById("state_details").innerHTML = "Show All";
		}
		
		else if (fromWhere == "map")
		{
			detailToggleState = 0;
			detailsBlock.style.display = "none";
			clearDetailsList();
		}
		// Otherwise, hide the display list
		else
		{
			detailToggleState = 0;
			detailsBlock.style.display = "none";
			clearDetailsList();
		}
				
	}
	
}







/* /////////////////////////

	RESULTS FUNCTIONS

*/ /////////////////////////

var selectedState;
var resultsListLength;


function setListLength(listLength)
{
	resultsListLength = listLength;	
	//alert(listLength);
}

// This function is called by the Flash object when first clicking on a state.
// If is responsible identifying the selected state and base
function getStateAS(whichState, whichBase)
{
	
	// Declare a variable to simplify calls to the placeholder element
	var resultsShortList = document.getElementById("shortlist");
	
	selectedState = whichState;
	selectedBase = "Hello There Base";
	
	resultsShortList.innerHTML = '<li><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/loading-bar.gif" valign="top" alt="Loading" /> Loading ...</li>';
	
	// Set the SELECT element's value to the incoming whichState
	document.getElementById("states_list").value = whichState;
	
	// Write results flags into side short list of results
	if (whichState)
	{
		document.getElementById("statechoice").style.display = "block";
		// Check to see if the state choice is Japan, so "State" can be replaced with "Country"
		if (whichState == "jp")
		{
			//document.getElementById("statechoice").innerHTML = "<strong>Country:</strong>&nbsp;<a href=\"#details\" id=\"state_details\" class=\"locator_state_link\" onclick=\"toggleDetails('state', " + resultsListLength + ")\" title=\"Show All SB contacts for Japan\">Japan</a>";
			document.getElementById("statechoice").innerHTML = "<strong>Country:</strong>";
		}
		else
		{
			//document.getElementById("statechoice").innerHTML = "<strong>Country:</strong>&nbsp;US&nbsp;&nbsp;<strong>State:</strong>&nbsp;" + selectedState.toUpperCase() + "&nbsp;&nbsp;<a href=\"#details\" id=\"state_details\" class=\"locator_state_link\" onclick=\"toggleDetails('state', " + resultsListLength + ")\" title=\"Show All SB contacts for " + selectedState.toUpperCase() + "\">Show&nbsp;All</a>";
			document.getElementById("statechoice").innerHTML = "<strong>Country:</strong>&nbsp;US&nbsp;&nbsp;<strong>State:</strong>&nbsp;" + selectedState.toUpperCase();
		}
		
	}
	else if (whichState && whichBase)
	{
		
		document.getElementById("statechoice").style.display = "block";
		// Check to see if the state choice is Japan, so "State" can be replaced with "Country"
		if (whichState == "jp")
		{
			document.getElementById("statechoice").innerHTML = "<strong>Country:</strong> Japan";
		}
		else
		{
			document.getElementById("statechoice").innerHTML = "<strong>Country:</strong>US <strong>&gt; State:</strong>&nbsp;"+selectedState.toUpperCase();
		}
		
		document.getElementById("basechoice").style.display = "block";
		document.getElementById("basechoice").innerHTML = "<strong>Base:</strong> "+ selectedBase;
	}
	
	
	
	
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	 
//	CONTROLS for BASE CONTACT LIST DISPLAY IN LEFT PANEL
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var baseListLength;
var currentBaseList;
var activeBase;
var shortListData = null;


// Controls the visibility of the contacts list, allowing only one to be viewed at a time.
function toggleContacts (thisID)
{
	
	var contactBlock = document.getElementById("contacts_container" + thisID);
	var contactSwitch = document.getElementById('base_contacts' + thisID);
		
	if (activeBase == null)//When list of bases is built, activeBase is set to null
	{
		contactBlock.style.display = "block";
		activeBase = thisID;
		
	}
	else if (activeBase == thisID)
	{
		
		contactBlock.style.display = "none";
		activeBase = null;
	}
	else if (activeBase != thisID)
	{
		// Hide other/previous contacts
		document.getElementById("contacts_container" + activeBase).style.display = "none";
		
		// Reveal selected contacts
		contactBlock.style.display = "block";
		//contactSwitch.style.backgroundPosition = "100% -30px";
		activeBase = thisID;
		
	}
	
}



// This function is run when user clicks a state on the map or selects a state from the select menu and clicks go.
// It uses an AJAX call to gather the list of bases for the selected state and return a block of HTML code.
// It then walks through the new HTML to determine how many of the elements are valid base information (by "id") and sets a number for the total number of valid bases so the toggle function can operate
function makeShortList(listLength, baseList)
{
var xmlHttp;

document.getElementById("shortlist").innerHTML = '<li><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/loading-bar.gif" valign="top" alt="Loading" /> Loading ...</li>';

try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
catch (e)
	{
	// Internet Explorer
		try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			  {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			catch (e)
			  {
			  
			  document.getElementById("shortlist").innerHTML = '<li><br /><br />There was a problem loading the data. Please refresh the page or try again. Contact the <a href="mailto:webmaster@airforcesmallbiz.org?subject=SAFSB -- Issue: Locator data not loading" title="Contact webmaster about Locator problem">webmaster</a> if the problem persists.</li>';
			  
			  alert("This application will not work in your browser. Please consider upgrading to a newer version, and take full advantage of the latest Web technology.");
			  return false;
			  }
		}
	}
  
	xmlHttp.onreadystatechange=function()
	{
		
		document.getElementById("shortlist").style.display = "block";
		document.getElementById("shortlist").innerHTML = '<li><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/loading-bar.gif" valign="top" alt="Loading" /> Loading ...</li>';
		
		if(xmlHttp.readyState == 3)
		{
			document.getElementById("shortlist").innerHTML = '<li><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp; Data Successfully Loaded ...</li>';
		}
		
		
		else if(xmlHttp.readyState==4)
		{
			shortListData = xmlHttp.responseText;
			currentBaseList = baseList;
			document.getElementById("shortlist").innerHTML = shortListData;
			document.getElementById("shortlist").style.display = "block";
			baseListLength = 0;
			var dynamicListElements = document.getElementById('shortlist').childNodes.length;
			activeBase = null;
			
			for (i=0; i<dynamicListElements; i++)
			{
				// Check to see if the element has attributes	
				if (document.getElementById('shortlist').childNodes[i].attributes)
				{
					// If it has attributes, check to see if it has an ID attribute
					if (document.getElementById('shortlist').childNodes[i].attributes["id"])
					{
						// If it has an id attribute, increase baseListLength for the toggle function to work
						baseListLength++;
						
					}
					else
					{
						//Do nothing;
					}
				}
				else
				{
					// Do nothing
				}
				
			}
			
			var base_id = document.getElementById("base_selected").value;
			if (base_id != "false")
			{
				try
				{
					toggleContacts(base_id);
				}
				catch (e) {}
			}	
			
		}
	}
	
	xmlHttp.open("GET","locator_ajax.php?state="+selectedState,true);
  	xmlHttp.send(null);
	
	  
}



// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	Builds the details list with all details 
//	for all bases in the entire state
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function makeStateDetailsList (listLength)
{
	// Convert lower-case state abbreviation to upper-case
	var myState = selectedState.toUpperCase();
	
	var detailsTable = document.getElementById("details").tBodies[0];
	var cellCount = document.getElementById("details").rows[0].cells.length;
	
	// Function to be used in conditional below. 
	// The conditional is to prevent duplicating the state details list every time the side state link is clicked
	
	// Clear the placeholder row before building the final table.
	detailsTable.deleteRow(0);
	
	// Insert a row in the details table for each short list item
	// Uses/exposes the DOM 1 HTMLTableElement to build the table.
	// IE will not allow table elements to be dynamically created using innerHTML,
	// so we create tables using the HTMLTableElement spec
	
	for (i=0; i < listLength; i++)
	{
		
		// Create a new row on each iteration
		detailsTable.insertRow(i);
		
		
		// In each new row, create the designated amount of cells (determined by the number of cells in the thead row).
		for (x=0; x < cellCount; x++)
		{
			detailsTable.rows[i].insertCell(x);
		}
		
		// Add attributes to each row
		detailsTable.rows[i].id = "details" + (i+1); 
		
		// Add attributes and content to each cell
		detailsTable.rows[i].cells[0].id = "org" + (i+1);
		detailsTable.rows[i].cells[0].className = "orgs";
		detailsTable.rows[i].cells[0].innerHTML = "Test Address"
		detailsTable.rows[i].cells[0].innerHTML = "<address>"+
														"<strong>" + currentBaseList[i+1] + " Information</strong><br />"+
														"123 Streetname St.<br />"+
														"Anytown, " + myState + " 22401<br />"+
														"<a href=\"#\" title=\"Location Mini-site\">Location Mini-site</a>"+
													"</address>";
		
		detailsTable.rows[i].cells[1].id = "opportunities" + (i+1);
		detailsTable.rows[i].cells[1].className = "opportunities";
		detailsTable.rows[i].cells[1].innerHTML = "<a href=\"/opportunities/index.php\" class=\"opps_link\" title=\"Click to jump to opportunties at this location\"># Contracts Available</a>";
		
		detailsTable.rows[i].cells[2].id = "contacts" + (i+1);
		detailsTable.rows[i].cells[2].className = "contacts";
		detailsTable.rows[i].cells[2].innerHTML = "<address>"+
														"<strong>Firstname Lastname</strong><br />"+
														"Title Here<br />"+
														"Phone: 555-555-5555<br />"+
														"DSN: 666-6666<br />"+
														"Fax: 555-555-7777<br />"+
														"<a href=\"mailto:firstname.lastname@thisbase.af.mil\" title=\"Contact Firstname Lastname\">firstname.lastname@thisbase.af.mil</a>"+
													"</address>";
	}
		
	// Print the base name into the placeholder
	document.getElementById("details_title_tag").innerHTML = unabbreviateState(selectedState);
	
}



// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	Builds the details list with all details 
//	for a base selected from the map or shortlist
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


function makeBaseDetailsList (listLength, baseName)
{
	
	var myState = selectedState.toUpperCase();
	var detailsTable = document.getElementById("details").tBodies[0];
	var cellCount = document.getElementById("details").rows[0].cells.length;
	var detailsRowCount = detailsTable.rows.length;
	
	// Clear the state details list before building the base details list.
	for (d=0; d<detailsRowCount; d++)
	{
		detailsTable.deleteRow(0);
	}
	
	// Insert a row in the details table for each short list item
	// Uses/exposes the DOM 1 HTMLTableElement to build the table.
	// IE will not allow table elements to be dynamically created using innerHTML,
	// so we create tables using the HTMLTableElement spec
	for (i=0; i < listLength; i++)
	{
		
		// Create a new row on each iteration
		detailsTable.insertRow(i);
		
		
		// In each new row, create the designated amount of cells (determined by the number of cells in the thead row).
		for (x=0; x < cellCount; x++)
		{
			detailsTable.rows[i].insertCell(x);
		}
		
		// Add attributes to each row
		detailsTable.rows[i].id = "details" + (i+1);
		
		// Add attributes and content to each cell
		detailsTable.rows[i].cells[0].id = "org" + (i+1);
		detailsTable.rows[i].cells[0].className = "orgs";
		detailsTable.rows[i].cells[0].innerHTML = "Test Address"
		detailsTable.rows[i].cells[0].innerHTML = "<address>"+
														"<strong>" + baseName + " Office Information</strong><br />"+
														"123 Streetname St.<br />"+
														"Anytown, " + myState + " 22401<br />"+
														"<a href=\"#\" title=\"Location Mini-site\">Location Mini-site</a>"+
													"</address>";
		
		detailsTable.rows[i].cells[1].id = "opportunities" + (i+1);
		detailsTable.rows[i].cells[1].className = "opportunities";
		detailsTable.rows[i].cells[1].innerHTML = "<a href=\"/opportunities/index.html\" class=\"opps_link\" title=\"Click to jump to opportunties at this location\"># Contracts Available</a>";
		
		detailsTable.rows[i].cells[2].id = "contacts" + (i+1);
		detailsTable.rows[i].cells[2].className = "contacts";
		detailsTable.rows[i].cells[2].innerHTML = "<address>"+
														"<strong>Firstname Lastname</strong><br />"+
														"Title Here<br />"+
														"Phone: 555-555-5555<br />"+
														"DSN: 666-6666<br />"+
														"Fax: 555-555-7777<br />"+
														"<a href=\"mailto:firstname.lastname@thisbase.af.mil\" title=\"Contact Firstname Lastname\">firstname.lastname@thisbase.af.mil</a>"+
													"</address>";
		
	}
	
	// Print the base name into the placeholder
	document.getElementById("details_title_tag").innerHTML = baseName;
	

	
}






function makeContactList(thisBase)
{
	var shortList = document.getElementById('shortlist');
	var shortListResults = shortList.innerHTML;
}


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	Clears all base details from the details placeholder 
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function clearDetailsList()
{
	// FOR SHORT LIST ON THE SIDE
	var detailsShortList = document.getElementById("shortlist");
	//var stateChoice = document.getElementById("statechoice");
	
	detailsShortList.style.display = "none";
	//stateChoice.style.display = "none";
		
	// FOR LARGE DETAILS LIST BELOW MAP
	var detailsTable = document.getElementById("details").tBodies[0];
	var cellCount = document.getElementById("details").rows[0].cells.length;
	var detailsRowCount = detailsTable.rows.length;
	
	// Clear existing rows and cells
	for (d=0; d<detailsRowCount; d++)
	{
		detailsTable.deleteRow(0);
	}
	
	// Create placeholder row and cells
	detailsTable.insertRow(0);
	for (x=0; x < cellCount; x++)
	{
		detailsTable.rows[0].insertCell(x);
	}
}





// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	Clears all hightlights and highlights the selected item 
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function highlightShortList(whichItem)
{
	
	var shortList = document.getElementById("shortList");	
	var listLength = shortList.childNodes.length;
	
	for (i=0; i<listLength; i++)
	{
		shortList.childNodes[i].className = "sb"
	}
	
	shortList.childNodes[whichItem].className = "sb active";
}



// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	Converts lower-case state abbreviations to state name 
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function unabbreviateState(thisState)
{
	var myFullState;
	
	switch (thisState)
	{
		case "ak":
			myFullState = "Alaska";
			break;
		case "al":
			myFullState = "Alabama";
			break;
		case "ar":
			myFullState = "Arkansas";
			break;
		case "az":
			myFullState = "Arizona";
			break;
		case "ca":
			myFullState = "California";
			break;
		case "co":
			myFullState = "Colorado";
			break;
		case "ct":
			myFullState = "Connecticut";
			break;
		case "de":
			myFullState = "Delaware";
			break;
		case "dc":
			myFullState = "DC";
			break;
		case "fl":
			myFullState = "Florida";
			break;
		case "ga":
			myFullState = "Georgia";
			break;
		case "hi":
			myFullState = "Hawaii";
			break
		case "ia":
			myFullState = "Iowa";
			break;
		case "id":
			myFullState = "Idaho";
			break;
		case "il":
			myFullState = "Illinois";
			break;
		case "in":
			myFullState = "Indiana";
			break;
		case "ks":
			myFullState = "Kansas";
			break;
		case "ky":
			myFullState = "Kentucky";
			break;
		case "la":
			myFullState = "Louisiana";
			break;
		case "ma":
			myFullState = "Massachusetts";
			break;
		case "md":
			myFullState = "Maryland";
			break;
		case "me":
			myFullState = "Maine";
			break;
		case "mi":
			myFullState = "Michigan";
			break;
		case "mn":
			myFullState = "Minnesota";
			break;
		case "mi":
			myFullState = "Michigan";
			break;
		case "ms":
			myFullState = "Mississippi";
			break;
		case "mo":
			myFullState = "Missouri";
			break;
		case "mt":
			myFullState = "Montana";
			break;
		case "nc":
			myFullState = "North Carolina";
			break;
		case "nd":
			myFullState = "North Dakota";
			break;
		case "ne":
			myFullState = "Nebraska";
			break;
		case "nh":
			myFullState = "New Hampshire";
			break;
		case "nj":
			myFullState = "New Jersey";
			break;
		case "nm":
			myFullState = "New Mexico";
			break;
		case "nv":
			myFullState = "Nevada";
			break;
		case "ny":
			myFullState = "NewYork";
			break;
		case "oh":
			myFullState = "Ohio";
			break;
		case "ok":
			myFullState = "Oklahoma";
			break;
		case "or":
			myFullState = "Oregon";
			break;
		case "pa":
			myFullState = "Pennsylvania";
			break;
		case "ri":
			myFullState = "RhodeIsland";
			break;
		case "sc":
			myFullState = "South Carolina";
			break;
		case "sd":
			myFullState = "South Dakota";
			break;
		case "tn":
			myFullState = "Tennessee";
			break;
		case "tx":
			myFullState = "Texas";
			break;
		case "ut":
			myFullState = "Utah";
			break;
		case "va":
			myFullState = "Virginia";
			break;
		case "vt":
			myFullState = "Vermont";
			break;
		case "wa":
			myFullState = "Washington";
			break;
		case "wi":
			myFullState = "Wisconsin";
			break;
		case "wv":
			myFullState = "West Virginia";
			break;
		case "wy":
			myFullState = "Wyoming";
			break;
		case "pr":
			myFullState = "Puerto Rico";
			break;
		case "gu":
			myFullState = "Guam";
			break;
		case "jp":
			myFullState = "Japan";
			break;
	}
	return myFullState;
}

function displayMajcom(str)
{
	// Declare a variable to simplify calls to the placeholder element
	var resultsList = document.getElementById("majcomListing");
	var majcom = document.getElementById("majcomid");
	
	resultsList.innerHTML = '<div style="margin:0 auto; width: 250px; margin-bottom: 50px;"><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/loading-bar.gif" valign="top" alt="Loading" /> Loading ...</div>';
	
if (str=="")
  {
  document.getElementById("majcomListing").innerHTML="";
  majcom.value="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
		//alert(xmlhttp.responseText);
    document.getElementById("majcomListing").innerHTML=xmlhttp.responseText;
	toggleShowHide(true);
	majcom.value=str;
    }
  }
xmlhttp.open("GET","/ajax/majcoms.php?q="+str,true);
xmlhttp.send();
}




// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	JS to AS scripting
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


function getTargetState(state) {
	//alert(state);
	getMovieName("SAFSBLocatorModule").setStateFromHTML(state); 
}

function getTargetBase(str)
{
	var resultsList = document.getElementById("shortlist");

	
	resultsList.innerHTML = '<div style="margin:0 auto; width: 250px; margin-bottom: 50px;"><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/loading-bar.gif" valign="top" alt="Loading" /> Loading ...</div>';
	if (str=="")
	{
	  document.getElementById("shortlist").innerHTML="Error!";
	  majcom.value="";
	  return;
	}
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//Get the resposne and split the string so that
			//we can get the base id and state name
			var response = xmlhttp.responseText;
			
			var info = response.split("-");
			
			document.getElementById("base_selected").value = info[1];
			getTargetState(info[0]);
			
		}
	  }
	xmlhttp.open("GET","locator_ajax.php?base="+str,true);
	xmlhttp.send();
	
}

function getMovieName(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
   }
   else {
       return document[movieName];
   }
}

function strstr (haystack, needle, bool) {

    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function toggleShowHide(show)
{
	var btn = document.getElementById("showHide_btn");
	var resultsList = document.getElementById("majcomListing");
	
	if (strstr(btn.src,"show",true) || show == true)
	{
		btn.src = "/images/hide_btn.gif";
		resultsList.style.display = "block";
	}
	else
	{
		btn.src = "/images/show_btn.gif";
		resultsList.style.display = "none";
	}
}

function downloadMajcom()
{
	var id = document.getElementById("majcomid").value;
	var resultsList = document.getElementById("majcomListing");
	
	if (id != null && id != "")
		window.location = "download_majcom.php?q="+id;	
	else
		resultsList.innerHTML = '<div style="margin:0 auto; width: 250px; margin-bottom: 50px;color:#FF0000"><br /><br /><br />Error!  No data to download.</div>';	
}

/*Scrolling functionality*/

scrollStep=1

timerLeft=""
timerRight=""

	
function toLeft(id){
  document.getElementById(id).scrollTop=0;
}

function scrollDivUp(id){
  clearTimeout(timerRight) 
  document.getElementById(id).scrollTop-=scrollStep
  timerRight=setTimeout("scrollDivUp('"+id+"')",10)
}

function scrollDivDown(id){
  clearTimeout(timerLeft)
  document.getElementById(id).scrollTop+=scrollStep
  timerLeft=setTimeout("scrollDivDown('"+id+"')",10)
}

function toRight(id){
  document.getElementById(id).scrollTop=document.getElementById(id).scrollWidth
}

function stopMe(){
  clearTimeout(timerRight) 
  clearTimeout(timerLeft)
}