function toggleItem(fset)
{
	if(!(x = document.getElementById(fset))) return false;
	if (x.style.display == 'block') x.style.display = 'none';
	else x.style.display = 'block';
}


function toggleToolbar(isOpen) {
	if(isOpen) {
		setToolbar('long_toolbar_wrapper', 'block');				
	} else {		
		setToolbar('long_toolbar_wrapper', 'none');
	}	
}

function setToolbar(sl, v) {
	if(d == document.getElementById(sl)) {
		d.style.display = v;
	}
}


function formatDateInput(v) {
	r = new Array();

	v = killChars(v, 'date');
	s = v.split('/');	

	// month, day
	for(i=0; i<2; i++) {
		if(!s[i] || s[i].length == 0) r[i] = '01';
		else if(s[i].length == 1) r[i] = '0' + s[i].substr(0,1);
		else r[i] = s[i].substr(0,2);		
	}
	
	// year
	d = new Date();
	y = d.getFullYear();
	if(!s[2]) r[2] = y;
	else r[2] = y.toString().substr(0, 4-s[2].length) + s[2];	
	
	// return	
	return r.join('/');
}

function killChars(string, totype) {
	switch(totype) {
		case 'date':
				return string.replace(/[^\d\/]/g, '');
		break;
		
		case 'money': // kill anything that's not a number or decimal
				return string.replace(/[^\d.]/g, '');
		break;

		case 'zip':
				return string.replace(/[^\d-]/g, '');
		break;

		case 'numberonly':
		case 'phone':
				return string.replace(/[^\d]/g, '');
		break;

		default:
				return string;
		break;
	}
}

function handleSignup() {
		document.getElementById('signupbutton').innerHTML = "<span style=\"color:#990000; font-weight: bold; font-style: italic\">Working...</span>";
		
				var theformintro = "					<form id='mailingListForm'  action=\"\" method=\"get\" onSubmit=\"javascript:handleSignup(); return false;\">";
		theformintro = theformintro + "						<p>";
		theformintro = theformintro + "							Choose one or all of our Hype and Hope information channels to receive email alerts and up to the minute news and information.";
		theformintro = theformintro + "						</p>";
	
			  var theform = "						<p>";
		theform = theform + "							<input type='checkbox' name='mailing_list[]' value='704472' id='mailing_list_sc' />";
		theform = theform + "							<label for='mailing_list_sc'>Stem Cells</label>";
		theform = theform + "						<br />";
		theform = theform + "							<input type='checkbox' class='checkbox' name='mailing_list[]' value='1481073' id='mailing_list_pm' />";
		theform = theform + "							<label for='mailing_list_pm'>Personal Medicine</label>";
		theform = theform + "						</p>";
		theform = theform + "						<p>";
		theform = theform + "							<input type='text' class='text' name='email' value=\"" + document.getElementById('mailingListForm').elements['email'].value + "\" />\n";
		theform = theform + "							<input type='hidden' name='function' value='signupForm' />";
		theform = theform + "							<span id=\"signupbutton\"><input type=\"image\" name='submit' src=\"/img/btn_go_bkg_white.gif\" /></span>\n";
		theform = theform + "						</p>	";
		theform = theform + "					</form>\n";
	
	var responseSuccess = function (resp) {
	//	alert (resp.responseText);
		var retmsg;
		retmsg = resp.responseText.split("|");	
	
		if (retmsg[0] == "noerror") {
			document.getElementById('signup_contents').innerHTML = retmsg[1];
		} else {
			var errortxt="";
			for(x=1;x<retmsg.length;x=x+2) {
				errortxt =  errortxt + "<p style=\"color:#990000;\">" + retmsg[x] + "</p>";
			}
			document.getElementById('signup_contents').innerHTML = theformintro + errortxt + theform;
		}
	}
	
	var responseFailure = function (resp) { 
		var errortxt = "There was a problem processing your request to be added to the Mailing Lists at this time.</span><br />\nPlease contact <a href=\"mailto:hypeandhope@zoomedia.com\">hypeandhope@zoomedia.com</a> to be manually added.";
		document.getElementById('signup_contents').innerHTML = theformintro + errortxt + theform;
	}
	
	var callback = {
		success:responseSuccess,
		failure:responseFailure
	};
	
	var formObject = document.getElementById('mailingListForm');
	YAHOO.util.Connect.setForm(formObject); 

	sUrl = "/AJAXResponse";

	var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);	
}
function pop_new_list(city,search_value,search_type,textid)
{
	//url = "/clinical_trials_locations
	postdata = "function=ct_citylookup&search_value="+search_value+"&city="+city+"&ct_search_type="+search_type;
	//pop_new_window(url,'locwindow',' width=400,height=300');
	//
	
	var responseSuccess = function (resp) {
			r = document.getElementById(textid);
			r.innerHTML = resp.responseText;		
		
		}	
	
	var responseFailure = function (resp) { 

	}	
	var callbackg = {
		success:responseSuccess,
		failure:responseFailure
	};	
	
	//alert(postdata);
	var outp = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callbackg, postdata);	
	return false;
}
var savefailed = function(o) { 	
	alert(o.statusText);
};

var trialSaved = function(o) {
	if (o.responseText.substring(0,3) == 'NCT') {
	r = document.getElementById('saver'+o.responseText);
	r.innerHTML = "<b>trial saved</b>";
	} else {
		alert(o.responseText);
	}
};		
		
function saveTrial(trialid) {
	var callback = { success:trialSaved, failure:savefailed	}; 
	postData = 'function=saveTrial&trialid=' + trialid;
	var request = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callback, postData);
	return false;
}
var trialRemoved = function(o) {
	if (o.responseText.substring(0,3) == 'NCT') {

		b = document.getElementById('trialrow_'+o.responseText+'_2');
		b.parentNode.removeChild(b);

		r = document.getElementById('trialrow_'+o.responseText);
		r.parentNode.removeChild(r);
	
	} else {
		alert(o.responseText);
	}
};
function removeTrial(trialid) {
	answer = confirm("Remove Trial?");
	if (answer)
	{
		var callback = { success:trialRemoved, failure:savefailed	}; 
		postData = 'function=removeTrial&trialid=' + trialid;
		var request = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callback, postData);
	}
	return false;
}

function toggleTrialDisplay(nct_id,anchor) {
	var pan = document.getElementById('more_'+nct_id);
	if (pan.style.display == 'none') pan.style.display = 'block';
	else pan.style.display = 'none';
	
	if (anchor.innerHTML == '+') {
		anchor.innerHTML = '-';
	} else {
		anchor.innerHTML = '+';
	}
	var successhandle = function(o) {
		pan.innerHTML = o.responseText;
	}
	var failhandle = function(o) {
		alert(o.responseText);
	}
	var callbackg = {
		success:successhandle,
		failure:failhandle
	};
	
	if (pan.innerHTML.indexOf('>Loading..') != -1) {
		postdata = 'function=trialdetails&trialid=' + nct_id;
		var outp = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callbackg, postdata);
	}
	return false;
}

function sendvote(vote,nct_id) {
	var pan = document.getElementById('vote_'+nct_id);
	
	var successhandle = function(o) {
		if (o.responseText == 'SUCCESS') {
			//pan.innerHTML = '<b>VOTED</b>';
			updateChart(nct_id);
		}
	}
	var failhandle = function(o) {
		alert(o.responseText);
	}
	var callbackg = {
		success:successhandle,
		failure:failhandle
	};	
	postdata = 'function=addvote&trialid=' + nct_id+'&vote='+vote;
	var outp = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callbackg, postdata);
	return false;
}

function updateChart(nct_id)
{
	var pan = document.getElementById('vote_container_'+nct_id);
	var successhandle = function(o) {
		pan.innerHTML = o.responseText;
	}
	var failhandle = function(o) {
		alert(o.responseText);
	}
	var callbackg = {
		success:successhandle,
		failure:failhandle
	};	
	postdata = 'function=updatechart&trialid=' + nct_id;
	var outp = YAHOO.util.Connect.asyncRequest('POST', '/AJAXResponse', callbackg, postdata);

}