// JavaScript Document

var searchresults

window.onload = init;

function init()
{
	checkSearch();
	
	var searchinput = document.getElementById('smart');
	searchresults = document.getElementById('ssresults');
	
	searchinput.onkeyup =  function()
	{
		sendsmartSearch(searchinput.value);
	}
	
	searchresults.onblur = searchinput.onblur = function()
	{
		hideThatMenu = setTimeout("hideMenu()",500)
	}
	
	//MENU
	
	var a = document.getElementsByTagName("DIV")
	
	for( i=0; i<a.length;i++ )
	{
		if( a[i].className )
		{
			if( a[i].className == 'nav2' )
			{
				a[i].onmouseover = function()
				{
					//alert("yes")
					if(document.getElementById('sub_'+this.id.split('_')[0]))
						document.getElementById('sub_'+this.id.split('_')[0]).style.display = 'block'
						//var thesub = document.getElementById('sub_'+this.id.split('_')[0])
						//opacity('thesub', 0, 100, 500)
				}
				
				a[i].onmouseout = function()
				{
						if( document.getElementById('sub_'+this.id.split('_')[0]))
							document.getElementById('sub_'+this.id.split('_')[0]).style.display = 'none'
							//var thesub2 = document.getElementById('sub_'+this.id.split('_')[0])
							//opacity('thesub2', 100, 0, 500)
				}
			}
			
			if(a[i].className == 'subtier' )
			{
				a[i].onmouseover = a[i].onfocus = function()
				{
					newid = this.id.split('_')[1]
					if( document.getElementById('tier_'+newid))
					thesub = document.getElementById('tier_'+newid)
					thesub.style.display = 'block'
				}
				
				a[i].onmouseout = a[i].onblur = function()
				{
					newid = this.id.split('_')[1]
					if( document.getElementById('tier_'+newid))
					thesub = document.getElementById('tier_'+newid)
					thesub.style.display = 'none'
				}
			}
		}
	}

}

function pullsmartSearch()
{
	if( xmlHttp.readyState == 4 )
	{
		searchresults = document.getElementById('ssresults');
		var seeall = document.getElementById('smart')
		var results = ''
		var matches = ''
		var sMatch = ''
		 matches = xmlHttp.responseText.split('*|*');
		 
		 results += '<div>'
		for( i=0;i<(matches.length-1);i++ )
			{	
				sMatch = matches[i].split("||");
				results += '<a href="pageview.asp?id='+sMatch[0]+'">'+sMatch[1]+'</a>'
			}
		results += '<a class="all" href="smartSearch.asp?ssphrase='+seeall.value+'">See all results</a>'
		results += '</div>'
		searchresults.innerHTML = results
		
		if( results.value != '' )
		{
			delaymenu = setTimeout("showMenu()",2000);
		}
		//else
//		{
//			searchresults.style.display = 'none'
//		}
	}
}

function sendsmartSearch(searchphrase)
{
	checkAJAX();
	//alert(searchphrase)
	var url = "/Scripts/ajax/smartsearch.ajax.asp";
	url += "?smart="+searchphrase;
	xmlHttp.onreadystatechange = pullsmartSearch;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showMenu()
{
	searchresults = document.getElementById('ssresults');
	//Effect.SlideDown(searchresults); return false;
	searchresults.style.display = 'block'
}

function hideMenu()
{
	var hide = document.getElementById('smart')
	searchresults = document.getElementById('ssresults');
	
	searchresults.style.display = 'none'
	hide.value = ''
			
}

// find Y position
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

//function openNews(id)
//{
//	news = document.getElementById(id)
//	news.style.display = ( news.style.display == "none" ) ? "block" : "none";
//}
//
//function openSubNav(id)
//{
//	subnav = document.getElementById(id)
//	subnav.style.display = ( subnav.style.display == "none" ) ? "block" : "none";
//}
//
//function jobSearch()
//	{
//		var jobinput = document.getElementById('jobinput').value
//		//jobinput.onclick = function()
////		{
//			document.location.href='/Scripts/genericSearch.asp?action=jobsearch&value='+jobinput	
//	}


// browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//function getElementByClassName(findclass,form)
//{
//	var tag=document.body.getElementsByTagName('*');
//	var errormsg = document.getElementById('errormsg')
//	errormsg.style.display = 'block'
//	for(var i=0; i<tag.length; i++) 
//	{
//		if(tag[i].className == findclass)
//			{
//				tag[i].classname == 'ERROR'
//				//errormsg.style.display = 'block'
//				return false;
//			}
//		return false;
//	}
//	return false;
//}
//				

function validate(form,findclass)
{
	var tag=document.body.getElementsByTagName('*');
	var errormsg = document.getElementById('errormsg')
	var txt = ''
	for(var i=0; i<tag.length; i++) 
	{
		if(tag[i].className == findclass)
			{
				//tag[i].style.className = 'ERROR'
					//confirm(tag[i].name)
				if(tag[i].value == '')
				{
					errormsg.style.display = "block"
					tag[i].style.border = '#C00 solid 3px'
					tag[i].focus();
					return false;
				}
			}
	}
	//sendForm()
}

//AJAX function for the physician search
var xmlHttp

function searchPhysicians(oForm){ 
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="searchPhysician.asp?1=1";
	url=url+"&s=" + oForm.speciality.value;
	url=url+"&l=" + oForm.location.value;
	url=url+"&h=" + oForm.hospital.value;

	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() { 
	if(xmlHttp.readyState==4){ 
		document.getElementById("physResults").innerHTML=xmlHttp.responseText;
		//document.getElementById("memberbutton").style.bottom = "-25px"
//		document.getElementById("memberbutton").style.left = "285px"
		//document.getElementById("memberbutton").innerHTML = ""
		
	}
}

//Calls data base to query trials
function searchTrials()
{
	//alert(form.agegroup.value)
	checkAJAX();
	//alert(searchphrase)
	var form = document.getElementById("trialForm");
	var url = "/scripts2/ajax/searchtrials_ajax.asp";
	url += "?cat="+form.category.value;
	url += "&age="+form.agegroup.value;
	url += "&loc="+form.diseasesite.value;
	url += "&fac="+form.facility.value;
	xmlHttp.onreadystatechange = searchTrials_update;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

//Send the new updated data to page
function searchTrials_update()
{
	newdata = xmlHttp.responseText
	display = document.getElementById('trialresults')
	display.innerHTML = newdata
	document.getElementById("memberbutton").innerHTML = ""
	

}

//If a user views a physician profile after a physician search, if they then return to the search page via the back button the search results are no longer shown....
//because the browser does retain the select box value this function checks to see if there is a value selected and if so, resends the search parameters when the form loads
function checkSearch(){
	var theForm = document.getElementById('phySearchForm');
	if(theForm){
		if(theForm.speciality.value!=""||theForm.location.value!=""||theForm.hospital.value!=""){
			searchPhysicians(theForm);	
		}		
	}
	
}


function updateSubType(value){

	checkAJAX2();
	
	var url = "/scripts2/ajax/searchTrialsUpdateDrop.ajax.asp";
	url = url + "?mainType=" + value;
	
	xmlHttp2.onreadystatechange = subType_update;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
	
	
	
}

function subType_update(){
	
	var display = document.getElementById("subType");
	
	display.innerHTML = xmlHttp2.responseText;
	
	//searchTrials();
}
