// JavaScript Document

function globalSearchFunction(){
	var search = document.getElementById('search').value;
	location.href = 'global_search.php?search=' + search + '&section=Downloads';
}

function opacity(id, opacStart, opacEnd, millisec, slide) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			if(slide == 'yes'){
				setTimeout("slideScreen(" + i + ",'" + id + "')",(timer * speed)); 
			}
            timer++; 
        } 
    }
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function slideScreen(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.width = opacity+'%'; 
} 


function ajaxFunction(action, id, rowstart, file_size){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	if(action == 'search'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('file_scroll');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				opacity('file_scroll', 0, 100, 500);
			}
		}
		var search = document.getElementById('search').value;
		var x = 'search';
		var category_list = get_search_categories();
		var queryString = "?search=" + search  + "&x=" + x  + "&rowstart=" + rowstart + "&categories=" + category_list;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
	
	} else if(action == 'getpagenav'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('download_bottom_nav');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				opacity('download_bottom_nav', 0, 100, 500);
			}
		}
		var search = document.getElementById('search').value;
		var x = 'pagerownav';
		var category_list = get_search_categories();
		
		var queryString = "?search=" + search  + "&x=" + x + "&rowstart=" + rowstart + "&categories="+category_list;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
		
	} else if(action == 'drilldown'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('file_display2');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				opacity('file_display2', 0, 100, 500);
			}
		}
		var x = 'drilldown';
		var queryString = "?id=" + id  + "&x=" + x;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
	
	} else if(action == 'fldwnldr'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('dwnflwdw');
				j = document.getElementById('blockoutpage');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				if (rowstart == '1') {
					ajaxDisplay.style.zIndex = '4';
					j.style.zIndex = '3';
					ajaxDisplay.style.visibility = 'visible';
					j.style.visibility = 'visible';
					opacity('dwnflwdw', 0, 100, 500);
					opacity('blockoutpage', 0, 75, 500);
				}
			}
		}
		var x = 'fldwnldr';
		var queryString = "?id=" + id  + "&x=" + x + "&y=" + rowstart + "&z=" + file_size;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
		
	} else if(action == 'example_display'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('dwnflwdw');
				j = document.getElementById('blockoutpage');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				if (rowstart == '1') {
					ajaxDisplay.style.zIndex = '4';
					j.style.zIndex = '3';
					ajaxDisplay.style.visibility = 'visible';
					j.style.visibility = 'visible';
					opacity('dwnflwdw', 0, 100, 500);
					opacity('blockoutpage', 0, 75, 500);
				}
			}
		}
		var x = 'example_display';
		var queryString = "?id=" + id  + "&x=" + x + "&y=" + rowstart;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
		
	}  else if(action == 'new_display'){ 
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById('new_marketing_display');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				opacity('new_marketing_display', 0, 100, 500);
			}
		}
		var x = 'new_display';
		var queryString = "?id=" + id  + "&x=" + x;
		ajaxRequest.open("GET", "search_actions.php" + queryString, true);
		ajaxRequest.send(null); 
	
	} else if(action.substring(0,6) == 'regdoc'){  // for the regulatory docs
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				if(action == 'regdoc1'){
					var targetContainer = 'regDocMainCategory';
				} else {
					var targetContainer = 'regDocOutput';
				}
				var ajaxDisplay = document.getElementById(targetContainer);
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
				if(action != 'regdoc1'){
					opacity(targetContainer, 0, 100, 500, 'yes');
				}
			}
		}
		
		
		var rand_no = Math.ceil(1000*Math.random());
		
		var queryString = "?action=" + action + "&mainID=" + escape(id) + "&parent=" + escape(rowstart) + "&rnd=" + rand_no;
		ajaxRequest.open("GET", "regulatoryDocsAction.php" + queryString, true);
		ajaxRequest.send(null); 
		
	}
}

function pagego(n) {
	// FInd the current value of the dopdown selet box
	var temp = document.myPage.rowstart;
	var beginrow = temp.options[temp.selectedIndex].value;
	// load the query list
	changeOpac(0, 'file_scroll'); 
	ajaxFunction('search', 'nothing', beginrow );
	// replace the current page number in the text based on the drop down info
	var i;
	for(i= temp.options.length-1;i>=0;i--)
	{
		if(temp.options[i].selected) {
			document.getElementById('currentpage').innerHTML = i + 1;
		}
	}
	// turn on and off the arrows	
	if(beginrow == 0){
		document.getElementById('btn_back').className='image_off';
		document.getElementById('btn_forward').className='image_on';
	} else if((beginrow/12)+1 == temp.options.length) {
		document.getElementById('btn_back').className='image_on';
		document.getElementById('btn_forward').className='image_off';
	} else {
		document.getElementById('btn_back').className='image_on';
		document.getElementById('btn_forward').className='image_on';
	}
}

function closedownload() {
	j = document.getElementById('dwnflwdw');
	k = document.getElementById('blockoutpage');
	opacity('dwnflwdw', 100, 0, 500);
	opacity('blockoutpage', 25, 0, 500);
	setTimeout("j.style.zIndex = '1'; k.style.zIndex = '0'; j.style.visibility = 'hidden'; k.style.visibility = 'hidden'; j.innerHTML = ''; k.innerHTML = '';", 500);
}

function arrowgo(direction) {
	//get the current page location from the selectbox
	var temp = document.myPage.rowstart;
	var beginrow = temp.options[temp.selectedIndex].value;
	var currindex = temp.selectedIndex; // current index value
	var page = (beginrow/12)+1; //this is the current page 
	var number_of_pages = temp.options.length; //this is the number of pages
		if(direction == 'back'){
			if(currindex != 0){ // at the first page, can't go back
				// load the query list
				changeOpac(0, 'file_scroll'); 
				whatever = parseInt(beginrow) - 12;
				ajaxFunction('search', 'nothing', whatever );
				// change drop down value
				temp.selectedIndex = currindex-1;
				// change the text number
				document.getElementById('currentpage').innerHTML = page-1;
			}
		} else if(direction == 'forward'){
			if(currindex != number_of_pages-1){ // at the last page, can't go back
				// load the query list
				changeOpac(0, 'file_scroll'); 
				whatever = parseInt(beginrow) + 12;
				ajaxFunction('search', 'nothing', whatever );
				// change drop down value
				temp.selectedIndex = currindex+1;
				// change the text number
				document.getElementById('currentpage').innerHTML = page+1;
				// change the button opacity
			}	
		}
		// turn on and off the arrows	
		if(temp.selectedIndex == 0){
			document.getElementById('btn_back').className='image_off';
			document.getElementById('btn_forward').className='image_on';
		} else if(temp.selectedIndex == temp.options.length-1) {
			document.getElementById('btn_back').className='image_on';
			document.getElementById('btn_forward').className='image_off';
		} else {
			document.getElementById('btn_back').className='image_on';
			document.getElementById('btn_forward').className='image_on';
		}
}

// Open/close The advanced search dropdown menu 
function change_advanced(action) {
	j = document.getElementById('search_drop_down');
	k = document.getElementById('advanced_search');
	if(action == 'open') {
		j.style.zIndex = '3';
		j.style.visibility = 'visible';
		k.value = '1';
		opacity('search_drop_down', 0, 100, 500);
	} else if (action == 'close') {
		opacity('search_drop_down', 100, 0, 500);
		setTimeout("j.style.zIndex = '1';j.style.visibility = 'hidden';k.value = '0';",600);
	}
} 

// creates a list of categories for the search
function get_search_categories() {
	j = document.drop_form.search_checks;
	list = '';
	for (i = 0; i < j.length; i++){
		if(j[i].checked == true){
			list = j[i].value+','+list;
		}
	}
	var list = list.replace(/ /g,"t7t5"); 
	var list = list.replace(/,/g,"t7t6");
	var list = list.replace(/&/g,"t7t3");
	var list = list.replace("/","t7t4");
	return list;
}

// shows the download link after the disclaimer button has been pressed
function showDownloadLink(id){
	document.getElementById('disclaimerBox').style.display = 'none';
	document.getElementById(id+'_large').style.visibility = 'visible';
	document.getElementById(id+'_large2').style.visibility = 'visible';
	document.getElementById(id+'_large3').style.visibility = 'visible';
}
