var xmlHttp101, xmlHttp102, xmlHttp103, xmlHttp104, xmlHttp105, xmlHttp106, xmlHttp107, xmlHttp108
var investor_edu_content = "investor_edu_content"
var xmlHttpUrl = "/include/investor_edu.asp"
var search_txt_default_value =  "Enter search words here"
var recordsPerPage = -1
var lastSearchString
var xmlHttpRequestHistory = new Array( )
var selectedMenuItemElementID = null

function classroom( id ) {
    resetMenus( )
    switch( id ) {
        case 1:
            loadResources( )
            break
        case 2:
            loadArticles( )
            break
        case 3:
        	loadGlossary( )
     }
 }
 
 function agentClassroom( id, aid ) {
 	resetMenus( )
    switch( id ) {
        case 1:
            loadResources( )
            break
        case 2:
            agentLoadArticles( aid )
            break
        case 3:
        	loadGlossary( )
     }
 }
 
 function goBack( ) {
 	try {
 		if ( xmlHttpRequestHistory.length > 0 ) {
 			var strings = xmlHttpRequestHistory.pop( )
 			var string = strings.split( " " )
 			//alert( strings )
 			if ( string[ 0 ] == "104" ) {
 				_buildTopArticles( string[ 1 ] )
 			}
 			else if ( string[ 0 ] == "103" ) {
 				_searchArticles( string[ 1 ] )
 			}
 			else if ( string[ 0 ] == "106" ) {
 				_articlesFor( string[ 1 ] )
 			}
 			else if ( string[ 0 ] == "105" ) {
 				_viewArticle( string[ 1 ] )
 			}
 			else if ( string[ 0 ] == "107" ) {
 				_page( string[ 1 ] )
 			}
 			else {
 				alert( "Can't go back" )
 			}
 		}
 	}
 	catch( err ) {
 		alert( err )
 	}
 }
 
 function putXmlHttpRequestHistory( xmlHttpObject ) {
 	try {
 		if ( xmlHttpRequestHistory.length == 2 ) {
 			xmlHttpRequestHistory.pop( )
 		}
 		xmlHttpRequestHistory.unshift( xmlHttpObject.getResponseHeader( "action-strings" ) )
 	}
 	catch( err ) {
 		alert( err )
 	}
 }
 
 function loadResources( ) {
    var html = ""
    html += '<div id="resources">'
	html += '<div id="page_title">Resources</div>'
	html += '<a href="http://www.ci.seattle.wa.us/dclu" target="_blank">Seattle Department of Planning and Development</a><br>'
	html += '<a href="http://www.dsaa.com/" target="_blank">Dupre &amp; Scott Apartment Advisors</a><br>'
	html += '<a href="http://www.djc.com/" target="_blank">Seattle Daily Journal of Commerce</a><br>'
	html += '<a href="http://seattle.bizjournals.com/seattle/" target="_blank">Puget Sounds Business Journal</a><br>'
	html += '<a href="http://access.wa.gov/" target="_blank">Washington State Government Information and Services</a><br>'
	html += '<a href="http://www.rha-ps.com/" target="_blank">Rental Housing Association of Puget Sound</a>'
	html += '</div>'	
	writeInnerHtml( investor_edu_content, html )
 }
 
 function loadArticles( ) {
    loadArticleBlock( )
	ShowLoadMessage( 'articles_container',  'Loading Articles' , null )
	//buildAgentArticleList( )
	//buildArticleSearchForm( )
	//buildTopArticles( )
	allArticles( )
 }
 
 function agentLoadArticles( aid ) {
 	loadArticleBlock( )
	ShowLoadMessage( 'articles_container',  'Loading Articles', null )
	//buildAgentArticleList( )
	//buildArticleSearchForm( )
	//buildTopArticles( )
	agentArticlesFor( -1, aid )
 }
 
 function loadArticleBlock( ) {
 	var html = ""
    html += '<div id="articles">'
	html += '<div id="articles_container"></div>'
	html += '</div>'
	writeInnerHtml( investor_edu_content, html )
 }
 
 function loadGlossary( ) {
 	ShowLoadMessage( investor_edu_content,  'Loading Glossary', null )
 	xmlHttp108 = GetXMLHttpRequest( xmlHttpUrl + "?a=108", function( ) {
		if ( ! xmlHttp108 ) {
			return
		}
		if ( xmlHttp108.readyState == 4 ) { 
			if ( xmlHttp108.status == 200 ) {
				StopLoadMessage( )
				writeInnerHtml( investor_edu_content, xmlHttp108.responseText  )
				xmlHttp108 = null
			}
			else {
				alert( xmlHttp108.responseText )
			}
			xmlHttp108 = null
		} 
	} )
 }
 
 function scrollToObject( elem_id ) {
 	// Not supported at this time.
 }
 
 function latestArticles( ) {
 	buildTopArticles( )
 }
 
 function allArticles( ) {
 	articlesFor( -1 )
 }
 
 function resetMenus( ) {
    //writeInnerHtml( "articles_sub_menu", "" )
 }
 
 function articlesMenuItemSelected( elem_id ) {
 	if ( selectedMenuItemElementID != null ) {
 		getElement( selectedMenuItemElementID ).className = "article_sub_menu_lbl"
 	}
 	selectedMenuItemElementID = elem_id
 	if ( selectedMenuItemElementID != null ) {
 		getElement( selectedMenuItemElementID ).className = "selected_article_sub_menu_lbl"
 	}
 }
 
 function buildAgentArticleList( ) {
    xmlHttp101 = GetXMLHttpRequest( xmlHttpUrl + "?a=101", function( ) {
		if ( ! xmlHttp101 ) {
			return
		}
		if ( xmlHttp101.readyState == 4 ) { 
			if ( xmlHttp101.status == 200 ) {
				writeInnerHtml( "articles_sub_menu", xmlHttp101.responseText  )
				xmlHttp101 = null
			}
			else {
				alert( xmlHttp101.responseText )
			}
			xmlHttp101 = null
		} 
	} )
}

function buildArticleSearchForm( ) {
    xmlHttp102 = GetXMLHttpRequest( xmlHttpUrl + "?a=102", function( ) {
		if ( ! xmlHttp102 ) {
			return
		}
		if ( xmlHttp102.readyState == 4 ) { 
			if ( xmlHttp102.status == 200 ) {
				writeInnerHtml( "articles_search_container", xmlHttp102.responseText  )
				xmlHttp102 = null
			}
			else {
				alert( xmlHttp102.responseText )
			}
			xmlHttp102 = null
		} 
	} )
}

function buildTopArticles( ) {
	_buildTopArticles( xmlHttpUrl + "?a=104&agent_id=-1&page=1" + "&page_length=" + recordsPerPage + "&title=Today's latest articles" )
}

function _buildTopArticles( url ) {
     xmlHttp104 = GetXMLHttpRequest( url, function( ) {
		if ( ! xmlHttp104 ) {
			return
		}
		if ( xmlHttp104.readyState == 4 ) { 
			if ( xmlHttp104.status == 200 ) {
				writeInnerHtml( "articles_container", xmlHttp104.responseText  )
				putXmlHttpRequestHistory( xmlHttp104 )
				articlesMenuItemSelected( "latest_articles" )
				xmlHttp104 = null
			}
			else {
				alert( xmlHttp104.responseText )
			}
			xmlHttp104 = null
		} 
	} )
}

function searchArticles( theForm ) {
	if ( theForm.Search_Text.value ==  search_txt_default_value ||  trim( theForm.Search_Text.value ).length == 0 ) {
        alert( "Please enter words to search" )
        return false
    }
    lastSearchString = theForm.Search_Text.value
    var values = createQueryStringOfFormElementsValues( theForm )
    values += "&page=1&page_length=" + recordsPerPage + "&paging=true" + "&title=Search Results" 
    _searchArticles( xmlHttpUrl + "?a=103&" + values )
    return false
}

function _searchArticles( url ) {
	articlesMenuItemSelected( null )
    ShowLoadMessage( 'articles_container',  'Searching', null )
    xmlHttp103 = GetXMLHttpRequest( url, function( ) {
		if ( ! xmlHttp103 ) {
			return
		}
		if ( xmlHttp103.readyState == 4 ) { 
			StopLoadMessage( )
			if ( xmlHttp103.status == 200 ) {
				writeInnerHtml( "articles_container", xmlHttp103.responseText  )
				putXmlHttpRequestHistory( xmlHttp103 )
				xmlHttp103 = null
			}
			else {
				alert( xmlHttp103.responseText )
			}
			xmlHttp103 = null
		} 
	} )
    return false
}

function searchFocusEvent( obj ) {
    if ( obj.value.length == 0 ) {
        obj.value = "Enter search words here"
        obj.style.color = "#a6a6a6"
    }
    else if ( obj.value == "Enter search words here" ) {
        obj.value = ""
        obj.style.color = "#000"
    }
    else {
        obj.style.color = "#000"
    }
}

function articlesFor( agent_id ) {
	var title = ""
	if ( agent_id > 0 ) {
		title = "Agent's Articles" 
	}
	else {
		title = "Articles" 
	}
	_articlesFor( xmlHttpUrl + "?a=106&agent_id=" + agent_id + "&page=1" + "&page_length=" + recordsPerPage + "&paging=true" + "&title=" + title  )
}

function agentArticlesFor( agent_id, aid) {
	var title = ""
	if ( agent_id > 0 ) {
		title = "Agent's Articles" 
	}
	else {
		title = "Articles" 
	}
	_articlesFor( xmlHttpUrl + "?a=106&agent_id=" + agent_id + "&page=1" + "&page_length=" + recordsPerPage + "&paging=true" + "&title=" + title + "&aid=" + aid )
}

function _articlesFor( url ) {
    xmlHttp106 = GetXMLHttpRequest( url, function( ) {
		if ( ! xmlHttp106 ) {
			return
		}
		if ( xmlHttp106.readyState == 4 ) { 
			if ( xmlHttp106.status == 200 ) {
				StopLoadMessage( )
				writeInnerHtml( "articles_container", xmlHttp106.responseText  )
				putXmlHttpRequestHistory( xmlHttp106 )
				xmlHttp106 = null
			}
			else {
				alert( xmlHttp106.responseText )
			}
			xmlHttp106 = null
		} 
	} )
}

function moreSearchResults( page ) {
     if ( lastSearchString == null && lastSearchString.length( ) == 0 ) {
        alert( "Please enter words to search" )
        return false
    }
    ShowLoadMessage( 'articles_container', 'Searching', null )
    var values = "Search_Text=" + lastSearchString + "&page=" + page + "&page_length=" + recordsPerPage + "&paging=true" + "&title=Search Results" 
    xmlHttp103 = GetXMLHttpRequest( xmlHttpUrl + "?a=103&" + values, function( ) {
		if ( ! xmlHttp103 ) {
			return
		}
		if ( xmlHttp103.readyState == 4 ) { 
			StopLoadMessage( )
			if ( xmlHttp103.status == 200 ) {
				writeInnerHtml( "articles_container", xmlHttp103.responseText  )
				putXmlHttpRequestHistory( xmlHttp103 )
				xmlHttp103 = null
			}
			else {
				alert( xmlHttp103.responseText )
			}
			xmlHttp103 = null
		} 
	} )
}

function page( page, agent_id ) {
	var title
	if ( agent_id > 0 ) {
		title = "Agent's Articles" 
	}
	else {
		title = "Articles" 
	}
	_page( xmlHttpUrl + "?a=107&agent_id=" + agent_id + "&page=" + page + "&page_length=" + recordsPerPage + "&paging=true"  + "&title=" + title  )
}

function _page( url ) {
    xmlHttp107 = GetXMLHttpRequest( url, function( ) {
		if ( ! xmlHttp107 ) {
			return
		}
		if ( xmlHttp107.readyState == 4 ) { 
			if ( xmlHttp107.status == 200 ) {
				writeInnerHtml( "articles_container", xmlHttp107.responseText  )
				putXmlHttpRequestHistory( xmlHttp107 )
				xmlHttp107 = null
			}
			else {
				alert( xmlHttp107.responseText )
			}
			xmlHttp107 = null
		} 
	} )
}

function viewArticle( article_id, agent_id ) {
	_viewArticle( xmlHttpUrl + "?a=105&agent_id=" + agent_id + "&article_id=" + article_id )
}

function agentViewArticle( article_id, agent_id, aid ) {
	_viewArticle( xmlHttpUrl + "?a=105&agent_id=" + agent_id + "&article_id=" + article_id + "&aid=" + aid )
}

function _viewArticle( url ) {
	ShowLoadMessage( 'articles_container', 'Loading Article', null )
    xmlHttp105 = GetXMLHttpRequest( url, function( ) {
		if ( ! xmlHttp105 ) {
			return
		}
		if ( xmlHttp105.readyState == 4 ) { 
			if ( xmlHttp105.status == 200 ) {
				StopLoadMessage( )
				writeInnerHtml( "articles_container", xmlHttp105.responseText  )
				putXmlHttpRequestHistory( xmlHttp105 )
				xmlHttp105 = null
			}
			else {
				alert( xmlHttp105.responseText )
			}
			xmlHttp105 = null
		} 
	} )
}

function agentLabelMouseOver( id ) {
    //getElement( id ).style.background = "#999966"
    //getElement( id ).style.color = "#fff"
}

function agentLabelMouseOut( id ) {
    //getElement( id ).style.background = "rgb(233,234,214)";
    //getElement( id ).style.color = "#003366"
}

var selectedMenuItem = null
function menu_item_selected( elem_id ) {
	if ( selectedMenuItem != null ) {
		//getElement( selectedMenuItem ).className = "menu_item"
	}
	selectedMenuItem = elem_id
	//getElement( selectedMenuItem ).className = "selected_menu_item"
}

 