/*
	Written by Patrick Mulligan
	Written on 8.24.2004
	File: common.js
	Description: This template contains various commonly used JavaScript functions.
*/

//function to swap the left menu image
var the_publication = "dark";
var the_dripenrollment = "dark";
var the_portmanage = "dark";
var the_mutualfund = "dark";

function the_rollover( the_image )
{
	switch( the_image )
	{
		case "publication":
			if( the_publication == "dark" )
			{
				the_publication = "light";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/Publications2.jpg";
			}
			else
			{
				the_publication = "dark";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/Publications.jpg";
			}
			break;	
			
		case "dripenrollment":
			if( the_dripenrollment == "dark" )
			{
				the_dripenrollment = "light";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/DRIPEnrollment2.jpg";
			}
			else
			{
				the_dripenrollment = "dark";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/DRIPEnrollment.jpg";
			}
			break;	
			
		case "portmanage":
			if( the_portmanage == "dark" )
			{
				the_portmanage = "light";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/Manage2.jpg";
			}
			else
			{
				the_portmanage = "dark";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/PortManage.jpg";
			}
			break;	

		case "mutualfund":
			if( the_mutualfund == "dark" )
			{
				the_mutualfund = "light";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/MutualFund2.jpg";
			}
			else
			{
				the_mutualfund = "dark";
				document.getElementById( the_image ).src = "https://www.directinvesting.com/images/MutualFund.jpg";
			}
			break;	

	}
}


// function to find the top position.
function findY( obj )
{
	// variable to hold the top position.
	var top = 0;
	// if the browser supports offsetParent.
	if( obj.offsetParent )
	{
		// loop while object has an offsetParent.
		while( obj.offsetParent )
		{
			// add to the top.
			top += obj.offsetTop;
			// reset the value of obj.
			obj = obj.offsetParent;
		}
	}
	// else other browser support.
	else if( obj.y )
	{
		// add to the top.
		top += obj.y;
	}
	// return the position.
	return top;
}
// function to find the left position.
function findX( obj )
{
	// variable to hold the left position.
	var left = 0;
	// if the browser supports offsetParent.
	if( obj.offsetParent )
	{
		// loop while object has an offsetParent.
		while( obj.offsetParent )
		{
			// add to the left.
			left += obj.offsetLeft;
			// reset the value of obj.
			obj = obj.offsetParent;
		}
	}
	// else other browser support.
	else if( obj.x )
	{
		// add to the left.
		left += obj.x;
	}
	// return the position.
	return left;
}
// function to open a popup window.
function open_popup( file )
{
	// variable to hold the new popup window.
	var popup_window = window.open( file, "search", "height=800,width=1000,location=1,scrollbars=1,menubar=1,toolbar=1,resizable=1" );
	// set the focus to the new popup window.
	popup_window.focus();
}
// function to trim the leading right-hand white space.
function RTrim( str )
{
	var whitespace = new String( " \t\n\r" );
	var s = new String( str );

	if ( whitespace.indexOf( s.charAt( s.length-1 ) ) != -1 )
	{
		var i = s.length - 1;

		while ( i >= 0 && whitespace.indexOf( s.charAt( i ) ) != -1 )
			i--;

		s = s.substring( 0, i + 1 );
	}
	return s;			
}
// function to trim the leading left-hand white space.
function LTrim( str )
{
	var whitespace = new String( " \t\n\r" );
	var s = new String( str );

	if( whitespace.indexOf( s.charAt( 0 ) ) != -1 )
	{
    	var j = 0, i = s.length;

   		while( j < i && whitespace.indexOf( s.charAt( j ) ) != -1 )
    		j++;

	    s = s.substring( j, i );
	}
	return s;
}
// function to trim all white space from a given string.
function Trim( str )
{
	return RTrim( LTrim( str ) );
}
// function to recalculate the total cost when service fee is modified.
function recalc_total( type )
{
	// verify that valid values were passed into this function.
	if( document.getElementById( "cart_total" ) != null )
	{
		// variable to hold the shopping cart.
		var ct = document.getElementById( "cart_table" );
		// variable to hold the running total and current total.
		var run = 0;
		var cur;
		// loop through the max number of rows.
		for( var k = 0; k < 25; k++ )
		{
			// if the current row exists.
			if( ct.rows(k) != null )
			{
				// if the current cell exists and is the cost cell.
				if( ct.rows(k).cells(6) != null && ct.rows(k).cells(6).id.indexOf( "cost_" ) != -1 )
				{
					// set the current value.
					cur = parseFloat( ct.rows(k).cells(6).innerHTML.replace( "$", "" ) );
					// if this is a numeric value.
					if( !isNaN( cur ) )
					{
						// if log in.
						if( type == "i" )
						{
							// if this is a popular company.
							if( Trim( ct.rows(k).cells(4).innerHTML ).toString() == "$15.00" )
							{
								// recalculate the current total.
								cur += (-25);
							}
							// else.
							else
							{
								// recalculate the current total.
								cur += (-25);
							}
						}
						// else.
						else
						{
							// if this is a popular company.
							if( Trim( ct.rows(k).cells(4).innerHTML ).toString() == "$30.00" )
							{
								// recalculate the current total.
								cur += 50; 
							}
							// else.
							else
							{
								// recalculate the current total.
								cur += 50;
							}
						}
						// insert the new current total.
						ct.rows(k).cells(6).innerHTML = "$" + cur.toFixed(2);
						// increment the running total.
						run += cur;
					}
				}
			}
		}
		// insert the new total.
		document.getElementById( "cart_total" ).innerHTML = "$" + run.toFixed(2);
		// if this form uses a dynamic form and the running_t variable is defined.
		if( typeof( running_t ) != "undefined" )
		{
			// reset the value of running_t.
			running_t = "$" + run.toFixed(2);
		}
		// if the total button is defined.
		if( document.getElementById( "con_order" ) != null )
		{
			// rewrite the button value.
			document.getElementById( "con_order" ).value="Continue with Order for $" + run.toFixed(2) + "**";
		}
		// if the process order button is defined.
		else if( document.getElementById( "process_order" ) != null )
		{
			// rewrite the button value.
			document.getElementById( "process_order" ).value = "Process Order for $" + run.toFixed(2) + "**";
		}
	}
}
// function to provide the state drop-down options.
function state_options()
{
	// variable to hold the state drop-down menu.
	var states = "<option value='AL'>Alabama</option><option value='AK'>Alaska</option><option value='AZ'>Arizona</option>";
	states += "<option value='AR'>Arkansas</option><option value='CA'>California</option><option value='CO'>Colorado</option>";
	states += "<option value='CT'>Connecticut</option><option value='DE'>Delaware</option><option value='DC'>District of Columbia</option>";
	states += "<option value='FL'>Florida</option><option value='GA'>Georgia</option><option value='HI'>Hawaii</option>";
	states += "<option value='ID'>Idaho</option><option value='IL'>Illinois</option><option value='IN'>Indiana</option>";
	states += "<option value='IA'>Iowa</option><option value='KS'>Kansas</option><option value='KY'>Kentucky</option>";
	states += "<option value='LA'>Louisiana</option><option value='ME'>Maine</option><option value='MD'>Maryland</option>";
	states += "<option value='MA'>Massachusetts</option><option value='MI'>Michigan</option><option value='MN'>Minnesota</option>";
	states += "<option value='MS'>Mississippi</option><option value='MO'>Missouri</option><option value='MT'>Montana</option>";
	states += "<option value='NE'>Nebraska</option><option value='NV'>Nevada</option><option value='NH'>New Hampshire</option>";
	states += "<option value='NJ'>New Jersey</option><option value='NM'>New Mexico</option><option value='NY'>New York</option>";
	states += "<option value='NC'>North Carolina</option><option value='ND'>North Dakota</option><option value='OH'>Ohio</option>";
	states += "<option value='OK'>Oklahoma</option><option value='OR'>Oregon</option><option value='PA'>Pennsylvania</option>";
	states += "<option value='RI'>Rhode Island</option><option value='SC'>South Carolina</option><option value='SD'>South Dakota</option>";
	states += "<option value='TN'>Tennessee</option><option value='TX'>Texas</option><option value='UT'>Utah</option>";
	states += "<option value='VT'>Vermont</option><option value='VA'>Virginia</option><option value='WA'>Washington</option>";
	states += "<option value='WV'>West Virginia</option><option value='WI'>Wisconsin</option><option value='WY'>Wyoming</option>";
	states += "<option value='AB'>Alberta</option><option value='BC'>British Columbia</option><option value='MB'>Manitoba</option>";
	states += "<option value='NB'>New Brunswick</option><option value='NF'>Newfoundland</option><option value='NS'>Nova Scotia</option>";
	states += "<option value='NT'>N.W.T.</option><option value='ON'>Ontario</option><option value='PQ'>Quebec</option>";
	states += "<option value='PE'>Prince Edward Island</option><option value='SK'>Saskatchewan</option><option value='YT'>Yukon</option>";
	states += "<option value='PR'>Puerto Rico</option><option value='VI'>Virgin Island</option><option value='MP'>Northern Mariana Is</option>";
	states += "<option value='GU'>Guam</option><option value='AS'>American Samoa</option><option value='PW'>Palau</option>";
	// return the state option list.
	return states;
}
// function to return the string format of a state acronym.
function state_string( s )
{
	// variable to hold the state name.
	var state;
	// switch the acronym.
	switch( Trim( s ).toString().toUpperCase() )
	{
		// alabama.
		case "AL":
			state = "Alabama";
			break;
		// alaska.
		case "AK":
			state = "Alaska";
			break;
		// arizona.
		case "AZ":
			state = "Arizona";
			break;
		// arkansas.
		case "AR":
			state = "Arkansas";
			break;
		// california.
		case "CA":
			state = "California";
			break;
		// colorado.
		case "CO":
			state = "Colorado";
			break;
		// connecticut.
		case "CT":
			state = "Connecticut";
			break;
		// delaware.
		case "DE":
			state = "Delaware";
			break;
		// district of columbia.
		case "DC":
			state = "District of Columbia";
			break;
		// florida.
		case "FL":
			state = "Florida";
			break;
		// georgia.
		case "GA":
			state = "Georgia";
			break;
		// hawaii.
		case "HI":
			state = "Hawaii";
			break;
		// idaho.
		case "ID":
			state = "Idaho";
			break;
		// illinois.
		case "IL":
			state = "Illinois";
			break;
		// indiana.
		case "IN":
			state = "Indiana";
			break;
		// iowa.
		case "IA":
			state = "Iowa";
			break;
		// kansas.
		case "KS":
			state = "Kansas";
			break;
		// kentucky.
		case "KY":
			state = "Kentucky";
			break;
		// louisiana.
		case "LA":
			state = "Louisiana";
			break;
		// maine.
		case "ME":
			state = "Maine";
			break;
		// maryland.
		case "MD":
			state = "Maryland";
			break;
		// massachusettes.
		case "MA":
			state = "Massachusettes";
			break;
		// michigan.
		case "MI":
			state = "Michigan";
			break;
		// minnesota.
		case "MN":
			state = "Minnesota";
			break;
		// mississippi.
		case "MS":
			state = "Mississippi";
			break;
		// missouri.
		case "MO":
			state = "Missouri";
			break;
		// montana.
		case "MT":
			state = "Montana";
			break;
		// nebraska.
		case "NE":
			state = "Nebraska";
			break;
		// nevada.
		case "NV":
			state = "Nevada";
			break;
		// new hampshire.
		case "NH":
			state = "New Hampshire";
			break;
		// new jersey.
		case "NJ":
			state = "New Jersey";
			break;
		// new mexico.
		case "NM":
			state = "New Mexico";
			break;
		// new york.
		case "NY":
			state = "New York";
			break;
		// north carolina.
		case "NC":
			state = "North Carolina";
			break;
		// north dakota.
		case "ND":
			state = "North Dakota";
			break;
		// ohio.
		case "OH":
			state = "Ohio";
			break;
		// oklahoma.
		case "OK":
			state = "Oklahoma";
			break;
		// oregon.
		case "OR":
			state = "Oregon";
			break;
		// pennsylvania.
		case "PA":
			state = "Pennsylvania";
			break;
		// rhode island.
		case "RI":
			state = "Rhode Island";
			break;
		// south carolina.
		case "SC":
			state = "South Carolina";
			break;
		// south dakota.
		case "SD":
			state = "South Dakota";
			break;
		// tennessee.
		case "TN":
			state = "Tennessee";
			break;
		// texas.
		case "TX":
			state = "Texas";
			break;
		// utah.
		case "UT":
			state = "Utah";
			break;
		// vermont.
		case "VT":
			state = "Vermont";
			break;
		// virginia.
		case "VA":
			state = "Virginia";
			break;
		// washington.
		case "WA":
			state = "Washington";
			break;
		// west virginia.
		case "WV":
			state = "West Virginia";
			break;
		// wisconsin.
		case "WI":
			state = "Wisconsin";
			break;
		// wyoming.
		case "WY":
			state = "Wyoming";
			break;
		// alberta.
		case "AB":
			state = "Alberta";
			break;
		// british columbia.
		case "BC":
			state = "British Columbia";
			break;
		// manitoba.
		case "MB":
			state = "Manitoba";
			break;
		// new brunswick.
		case "NB":
			state = "New Brunswick";
			break;
		// newfoundland.
		case "NF":
			state = "Newfoundland";
			break;
		// nova scotia.
		case "NS":
			state = "Nova Scotia";
			break;
		// n.w.t.
		case "NT":
			state = "N.W.T";
			break;
		// ontario.
		case "ON":
			state = "Ontario";
			break;
		// quebec.
		case "PQ":
			state = "Quebec";
			break;
		// prince edward island.
		case "PE":
			state = "Prince Edward Island";
			break;
		// saskatchewan.
		case "SK":
			state = "Saskatchewan";
			break;
		// yukon.
		case "YT":
			state = "Yukon";
			break;
		// puerto rico.
		case "PR":
			state = "Puerto Rico";
			break;
		// virgin island.
		case "VI":
			state = "Virgin Islands";
			break;
		// northern mariana island.
		case "MP":
			state = "Northern Mariana Island";
			break;
		// guam.
		case "GU":
			state = "Guam";
			break;
		// american samoa.
		case "AS":
			state = "American Samoa";
			break;
		// palau.
		case "PW":
			state = "Palau";
			break;
	}
	// return the state.
	return state;
}


function poBox(yesno){
 	var address = document.getElementById('addressBlock');
	var po = document.getElementById('poBlock');
}


function set_focus( element_from, element_to )
{
	// local variables to hold the two elements in question.
	var element1 = document.getElementById( element_from );
	var element2 = document.getElementById( element_to );
	// if the max length has been entered.
	if( parseInt( element1.value.toString().length ) == element1.maxLength )
	{
		element2.focus();
	}
}


// toggle main navigation
function togMainNav(section){
	var smenu = document.getElementById('smenu_'+section);
	if (smenu.className == 'smenu hide'){
		smenu.className = 'smenu show';
	}
    else {
		smenu.className = 'smenu hide';
	}
}