var searchto = null;
var window_width = 780;
var searching = null;
var bgColor;
var color;
var selectedProduct = null;
var oldLabel;
var scrollPosition = 0;

function showImage(obj){
	if(!document.getElementById('dwOverlay')){
		temp = document.createElement('div');
		temp.setAttribute('id','dwOverlay');
		temp.className = 'popHide';
		document.body.appendChild(temp);
	}
	if(!document.getElementById('dwContain')){
		temp = document.createElement('div');
		temp.setAttribute('id','dwContain');
		temp.className = 'popHide';
		document.body.appendChild(temp);
	}

	document.body.appendChild(obj);
	document.getElementById('dwContain').innerHTML = "";
	document.getElementById('dwContain').appendChild(obj);
	
	if(document.getElementById("dwOverlay").className == 'popHide'){
		document.getElementById("dwContain").className = 'popShowContain';
		document.getElementById("dwOverlay").className = 'popShowOverlay';
    }
    
	obj.style.display = 'block';
	obj.style.top = 100;
	obj.style.left = 5;
	obj.style.right = 5;
}
function closeImage(obj){
	obj.style.display = 'none';
	document.getElementById("dwContain").className = 'popHide';
	document.getElementById("dwOverlay").className = 'popHide';
}
function reqprod(ptype,prod,comp){
	if(ptype)
		window.open('/request_product.cfm?r='+ptype+'&p='+prod+'&c='+comp);
	else
		window.open('/request_product.cfm');
}
function searchAgrian(){
	var left = Math.abs((window_width - screen.width) / 2);
	var top = Math.abs((600 - screen.height) / 2);
	window.open('/labelcenter/results.cfm', '_blank', 'height=600,width=950,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,left='+left+',top='+top);
}
function clickdoc(w){
	doc = w.split('/')[w.split('/').length-1];
	resp = simpleajax('http://www.agrian.com/labelcenter/results.cfm?log='+su(doc));
	//window.open(w,'_blank','height=100%,width=100%,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
}
function reset_adv(){
	document.getElementById('type').options[0].selected = true;
	document.getElementById('company').options[0].selected = true;
	document.getElementById('state').options[0].selected = true;
	document.getElementById('ai').value = '';
	//document.getElementById('crop').value = '';
	//document.getElementById('pest').value = '';
	crop_search_focus();
	pest_search_focus();
	document.getElementById('chkOrganic').checked = false;
	return true;
}
function search_enter(){
	if(document.getElementById('labelsearch').value == ' Search Product Labels...'){
		document.getElementById('labelsearch').value = '';
	}
}
function search_leave(){
	if(document.getElementById('labelsearch').value == ''){
		document.getElementById('labelsearch').value = ' Search Product Labels...';
	}
}
function search_change(){
	if(searchto) clearTimeout(searchto);
	if(document.getElementById('labelsearch').value.length > 2){
		searchto = setTimeout("dosearch();",500); // Wait 1 second before doing search (in case still typing)
	}
}
function dosearch(){
	searchstring = document.getElementById('labelsearch').value;
	
	var left = Math.abs((window_width - screen.width) / 2);
	var top = Math.abs((450 - screen.height) / 2);
	window.open('/labelcenter/results.cfm?s='+su(searchstring), '_blank', 'height=450,width='+window_width.toString()+',status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,left='+left+',top='+top);
}
function su(thetext){
	if(thetext.toString() != ''){
		var tempfinal = '';
		for(tempz=0;tempz<=thetext.length-1;tempz++){
			tempfinal += thetext.charCodeAt(tempz) + 'A';
		}
		tempfinal.match(/^(.*)A$/);
		tempfinal = RegExp.$1;
		return tempfinal;
	}else{
		return '';
	}
}
function adv(){
	var left = Math.abs((window_width - screen.width) / 2);
	var top = Math.abs((450 - screen.height) / 2);
	window.open('/labelcenter/results.cfm?advanced=1', '_blank', 'height=450,width='+window_width.toString()+',status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,left='+left+',top='+top);
}

function change(){
	if (searching)
		clearTimeout(searching);
	if (document.getElementById('labelsearch').value.length > 2)
		searching = setTimeout("performSearch();",1000);
}
function enter(){
	if (document.getElementById('labelsearch').value == ' Search Product Labels...')
		document.getElementById('labelsearch').value = '';
}
function leave(){
	if (document.getElementById('labelsearch').value.trim() == '')
	{
		document.getElementById('labelsearch').value = ' Search Product Labels...';
		document.getElementById('label').innerHTML = "";
	}
}
function performSearch(){
	searchstring = document.getElementById('labelsearch').value;
	showSearching();
	ajax('results.cfm?s1=1&s='+su(searchstring), search_response, 1);
}
function search_response(w){
	var det = document.getElementById('details_out');
	//var doc = document.getElementById('docs');
	var res = document.getElementById('theresults');
	var adv = document.getElementById('advanced_out');
	res.style.display = '';
	det.style.display = 'none';
	//doc.style.display = 'none';
	adv.style.display = 'none';

	var obj = w.substring(w.indexOf("|")+1).trim();

	res.innerHTML = obj.substring(obj.indexOf("|")+1);
	selectedProduct = null;
	showResults();

	var cnt = w.substring(0,w.indexOf("|")).trim();
	changeDetailsState(false);

	var plural = cnt == 1 ? 'Result' : 'Results'
	var lbl = document.getElementById('label');
	lbl.innerHTML = "&nbsp;" + cnt + " " + plural + " Found For: <b>" + obj.substring(0,obj.indexOf("|")).trim() + "</b>";
}
function gover(w,n){
	w.className = 'hovered'+n;
	w.style.color = '';
	tds = w.getElementsByTagName('td');
	for(var i=0;i<tds.length;i++){
		tds[i].style.color = '';
	}
}
function gout(w,cl,c){
	w.className = cl;
	w.style.color = c;
	tds = w.getElementsByTagName('td');
	for(var i=0;i<tds.length;i++){
		tds[i].style.color = c;
	}
}
function view(id, default_tab){
	var temp = selectedProduct;
	scrollPosition = (document.all)?document.body.scrollTop:window.pageYOffset;

	if (id != -1)
		selectedProduct = id;

	if (selectedProduct == null){
		showResults();
	}
	else
	{
		var det = document.getElementById('details_out');

		changeDetailsState(true);
		changeClass('details', 'normal');

		if (temp != id && id !=-1)
		{
			det.innerHTML = "";
			if(default_tab)
				ajax("results.cfm?dt=" + default_tab + "&d="+selectedProduct, view_response, 1);
			else
				ajax("results.cfm?d="+selectedProduct, view_response, 1);
		}
		viewDetails();
	}
}
function view_response(w){
	var res = w.substring(w.indexOf("|")+1);

	var det = document.getElementById('details_out');
	det.innerHTML = w.trim();

}
function viewDetails(){
	if (selectedProduct != null){
		var det = document.getElementById('details_out');
		var res = document.getElementById('theresults');
		var adv = document.getElementById('advanced_out');
		res.style.display = 'none';
		det.style.display = '';
		adv.style.display = 'none';
		scroll(0,0);
		changeSelected('2');
	}

}
function viewDoc(){
	if (selectedProduct == null)
	{
		showResults();
	}
	else
	{
		var det = document.getElementById('details_out');
		var res = document.getElementById('theresults');
		var adv = document.getElementById('advanced_out');
		res.style.display = 'none';
		det.style.display = 'none';
		adv.style.display = 'none';

		changeSelected('3');

	}
}
function showResults(){
	var det = document.getElementById('details_out');
	var res = document.getElementById('theresults');
	var adv = document.getElementById('advanced_out');
	res.style.display = '';
	det.style.display = 'none';
	adv.style.display = 'none';


	changeSelected('1');

	window.scrollTo(0, scrollPosition);
}
function changeClass(tab, imgstate){

	var td = document.getElementById(tab+'_tab');
	var tdl = document.getElementById(tab+'_link');
	var tdi = document.getElementById(tab+'_img');
	if(tdi != null)
	{
		tdi.src = 'images/'+tab+'_'+imgstate+'.jpg';
	}
	
}
function changeDetailsState(enabled){
	var view_class = "disabled";
	if (enabled)
		view_class = "normal";
	changeClass('details', view_class);

}
function changeSelected(index){
	switch (index)
	{
		case '1': //results
		changeClass('results', 'selected');
		if (selectedProduct == null)
		{
			changeClass('details', 'disabled');
		}
		else
		{
			changeClass('details', 'normal');
		}
		changeClass('advanced', 'normal');
			break;
		case '2': //details
		changeClass('results', 'normal');
		changeClass('details', 'selected');
		changeClass('advanced', 'normal');
			break;
		case '3': //documents
		changeClass('results', 'normal');
		changeClass('details', 'normal');
		changeClass('advanced', 'normal');
			break;
		case '4': //advanced
		changeClass('results', 'normal');
		if (selectedProduct == null)
		{
			changeClass('details', 'disabled');
		}
		else
		{
			changeClass('details', 'normal');
		}
		changeClass('advanced', 'selected');
			break;
	}
}
function adv(){
	var det = document.getElementById('details_out');
	//var doc = document.getElementById('docs');
	var res = document.getElementById('theresults');
	var adv = document.getElementById('advanced_out');
	res.style.display = 'none';
	det.style.display = 'none';
	//doc.style.display = 'none';
	adv.style.display = '';

	changeSelected('4');

}
function advancedSearch(){
	var temp = 'type='+document.getElementById('type').value+'&';
		temp += 'company='+document.getElementById('company').value+'&';
		temp += 'state='+document.getElementById('state').value+'&';
		temp += 'ai='+document.getElementById('ai').value+'&';
		temp += 'chkShowAI='+document.getElementById('chkShowAI').checked+'&';
		temp += 'crop='+su(document.getElementById('adv_crop').value)+'&';
		temp += 'pest='+su(document.getElementById('pest').value)+'&';
		temp += 'organic='+document.getElementById('chkOrganic').checked+'&as=1';
	showSearching();
	ajax('results.cfm?'+temp, advanced_response, 1);
}
function advanced_response(w){
	var det = document.getElementById('details_out');
	//var doc = document.getElementById('docs');
	var res = document.getElementById('theresults');
	var adv = document.getElementById('advanced_out');
	res.style.display = '';
	det.style.display = 'none';
	//doc.style.display = 'none';
	adv.style.display = 'none';

	res.innerHTML = w.substring(w.indexOf("|")+1).trim();
	selectedProduct = null;
	showResults();

	var cnt = w.substring(0,w.indexOf("|")).trim();
	changeDetailsState(false);

	var lbl = document.getElementById('label');
	var plural = '';
	if(cnt != 1){plural = 's'}
	lbl.innerHTML = "&nbsp;" + cnt + " Advanced Search Result"+plural+'.';
}
function showSearching(){
	var res = document.getElementById('theresults');
	if(res)
		res.innerHTML = "";
	var lbl = document.getElementById('label');
	lbl.innerHTML = "&nbsp;Searching...";
	selectedProduct = null;
	showResults();
}
function ai_click(ai){
	showSearching();
	ajax('results.cfm?as=-1&ai='+ai,ai_response,1);
}
function ai_response(w){
	var det = document.getElementById('details_out');
	//var doc = document.getElementById('docs');
	var res = document.getElementById('theresults');
	var adv = document.getElementById('advanced_out');
	res.style.display = '';
	det.style.display = 'none';
	//doc.style.display = 'none';
	adv.style.display = 'none';

	var pName = w.substring(0,w.indexOf("|")).trim();
	var response = w.substring(w.indexOf("|")+1).trim();
	res.innerHTML = response.substring(response.indexOf("|")+1).trim();
	selectedProduct = null;
	showResults();

	changeDetailsState(false);

	var lbl = document.getElementById('label');
	var plural = '';
	if(pName != 1){plural = 's'}
	lbl.innerHTML = "&nbsp;" + pName + " Result"+plural+" Found For Active Ingredient: <b>" + response.substring(0,response.indexOf("|")).trim() + "</b>";
}
function pcs(whocalled){
	document.getElementById('pcspests').options[0].selected = true;
	try{
		container = document.getElementById('pstImageLink');
		container.innerHTML = "";
		toPrintContainer = document.getElementById('printImage');
		toPrintContainer.innerHTML = "";
	}catch(err){}
	var divsp = new Array('rateoutput','adoutput','gdoutput');
	selectPhrase = "Select State/Commodity/Pest First."
	for(var i=0;i<divsp.length;i++)
		{
				document.getElementById(divsp[i]).innerHTML = selectPhrase;
		}
	
	var divs = new Array('reioutput','phioutput','maxappsoutput','maxseasonoutput','methodsoutput','genericcondition');
	if(whocalled == 'pcsstates')
	{
		document.getElementById('pcscoms').options[0].selected = true;
		selectPhrase = "Select State/Commodity First.";
		for(var i=0;i<divs.length;i++)
		{
				document.getElementById(divs[i]).innerHTML = selectPhrase;
		}
	}
	/*Returns: REI | PHI | Max Apps | Max per season | Restricted Methods | Generic Condition | Decision Rate | MRL Data*/
	
	if(document.getElementById('pcscoms').options[document.getElementById('pcscoms').selectedIndex].value != ''){
		var id = document.getElementById('pid').value;
		var st = document.getElementById('pcsstates').options[document.getElementById('pcsstates').selectedIndex].value;
		var com = document.getElementById('pcscoms').options[document.getElementById('pcscoms').selectedIndex].value;
		var gen = document.getElementById('pcsgenc').options[document.getElementById('pcscoms').selectedIndex].value;
		gen = gen.replace(/%/g,'%25');
		var co = document.getElementById('pcscountries').options[document.getElementById('pcscountries').selectedIndex].value;
		var result = simpleajax('results.cfm?p='+id+'&st='+st+'&com='+com+'&gen='+gen+'&co='+co).trim();
		if(gen != '' && co == 'US') { rates(); }
		result = result.trim();
		result = result.substr(0,result.length-1);
		allRows = result.split("`")
		sRows = allRows[0].split("^");
		if(sRows.length>1) // has decision rates
		{
			items = sRows[0].split("||");
			// starting from maxapps ... will add REI, PHI and decision rates later
			for(var i=2;i<divs.length;i++){
				document.getElementById(divs[i]).innerHTML = items[i];
			}
			rei="";
			phi="";
			isREIdec = false;
			isPHIdec = false;
			for(var j=0; j<sRows.length; j++)
			{
				items = sRows[j].split("||");
				dec = items[items.length-1];

				// REI
				if(sRows[j+1]&&items[0].trim()!=sRows[j+1].split('||')[0].trim() && items[items.length-1]!="-")
					isREIdec = true;
				rei += (rei!=""?("<br>"):"") + items[0]+" " + ((isREIdec)?dec:"");

				// PHI
				if(sRows[j+1]&&items[1].trim()!=sRows[j+1].split('||')[1].trim() && items[items.length-1]!="-")
					isPHIdec = true;
				phi += (phi!=""?("<br>"):"") + items[1]+" " + ((isPHIdec)?dec:"");
			}
			if(isREIdec)
				document.getElementById('reioutput').innerHTML = rei;
			else
				document.getElementById('reioutput').innerHTML = sRows[0].split("||")[0];
			if(isPHIdec)
				document.getElementById('phioutput').innerHTML = phi;
			else
				document.getElementById('phioutput').innerHTML = sRows[0].split("||")[1];
		}
		else
		{
			items = result.split("||");
			for(var i=0;i<divs.length;i++){
				document.getElementById(divs[i]).innerHTML = items[i];
			}
		}
		if(allRows.length == 2) {
			mrlData = allRows[1].split("||");
			var mrlString = "";
			if(mrlData.length > 0) {
				for(i=0;i<mrlData.length;i++) {
					mrlString += mrlData[i].split('|')[0]+': '+mrlData[i].split('|')[1]+'<br />';
				}
			} else {
				mrlString = 'None';
			}
		} else {
			mrlString = 'None';
		}
		//$('mrl').innerHTML = mrlString;
	}
}
function getpests(){
	try{
		container = document.getElementById('pstImageLink');
		container.innerHTML = "";
		toPrintContainer = document.getElementById('printImage');
		toPrintContainer.innerHTML = "";
	}catch(err){}
	var st = document.getElementById('pcsstates').options[document.getElementById('pcsstates').selectedIndex].value;
	var cp = document.getElementById('pcscoms').options[document.getElementById('pcscoms').selectedIndex].value;
	var co = document.getElementById('pcscountries').options[document.getElementById('pcscountries').selectedIndex].value;
	var pid = document.getElementById('pid').value;
	if(cp != '' && co == "US"){
		document.getElementById('pcspests').length = 0;
		document.getElementById('pcspests').options[0] = new Option('Getting Pests...','');

		var results = simpleajax('results.cfm?t=pe&st='+st+'&com='+cp+'&pid='+pid).trim().split('~');
		document.getElementById('pcspests').length = 0;
		document.getElementById('pcspests').options[0] = new Option('Select Pest','');
		for(var i=0;i<results.length;i++){
			var items = results[i].split('|');
			document.getElementById('pcspests').options[document.getElementById('pcspests').length] = new Option(items[0],items[1]);
		}
	} else if(co != 'US') {
		document.getElementById('pcspests').length = 0;
		document.getElementById('pcspests').options[0] = new Option('Select Pest','');
		document.getElementById('pcspests').options[1] = new Option('United States only.','');
	} else {
		document.getElementById('pcspests').length = 0;
		document.getElementById('pcspests').options[0] = new Option('Select Pest','');
		document.getElementById('pcspests').options[1] = new Option('Select Commodity First.','');
		rates();
	}
}

function countries () {
	if($('pcscountries').value != "US") {
		$('pcsstates').selectedIndex = $('pcsstates').options.length - 1
		$('pcsstates').disabled = true;		
	} else {
		$('pcsstates').disabled = null;
	}
	pcs($('pcscountries').name);
	getpests();
}
function rates(){
	try{
		container = document.getElementById('pstImageLink');
		container.innerHTML = "";
		toPrintContainer = document.getElementById('printImage');
		toPrintContainer.innerHTML = "";
	}catch(err){}
	
	var divs = new Array('rateoutput','adoutput','gdoutput','cdoutput');
	var s = document.getElementById('pcsstates').options[document.getElementById('pcsstates').selectedIndex].value;
	var pr = document.getElementById('pid').value;
	var pe = document.getElementById('pcspests').options[document.getElementById('pcspests').selectedIndex].value;
	var c = document.getElementById('pcscoms').options[document.getElementById('pcscoms').selectedIndex].value;
	var gen = document.getElementById('pcsgenc').options[document.getElementById('pcscoms').selectedIndex].value;
	gen = gen.replace(/%/g,'%25');
	var results = simpleajax('results.cfm?t=ra&pr='+pr+'&st='+s+'&pe='+pe+'&com='+c+'&gen='+gen).trim().split('|');
	for(var i=0;i<divs.length;i++){
		document.getElementById(divs[i]).innerHTML = results[i];
	}
	// get pest image if available
	try{
		container = document.getElementById('pstImageLink');
		container.innerHTML = simpleajax('results.cfm?t=getImage&pe='+pe).trim();
		toPrintContainer = document.getElementById('printImage');
		toPrintContainer.innerHTML = simpleajax('results.cfm?t=getImagePrint&pe='+pe).trim();
	}catch(err){}
}
var currentDetailsTab = 0;
function changedetailstab(newtab){
	currentDetailsTab = newtab;
	var tabs = new Array('general','crop_specific','documents','safety','map','mfg');
	if(tabs[newtab]!="undefined")
	{
		for(var i=0;i<tabs.length;i++){
			if(document.getElementById('details_'+tabs[i]+'_tab')){
				document.getElementById('details_'+tabs[i]+'_tab').style.fontWeight = 'normal';
				document.getElementById('details_'+tabs[i]+'_triangle').style.display = 'none';
			}
			if(document.getElementById('details_'+tabs[i])){
				document.getElementById('details_'+tabs[i]).style.display = 'none';
			}
		}
		if(document.getElementById('details_'+tabs[newtab]+'_tab')){
			document.getElementById('detailsbar').style.backgroundImage = 'url(images/'+tabs[newtab]+'_fade.gif)';
			document.getElementById('details_'+tabs[newtab]+'_tab').style.fontWeight = 'bold';
			document.getElementById('details_'+tabs[newtab]+'_triangle').style.display = '';
		}
		if(document.getElementById('details_'+tabs[newtab]))
		{
			document.getElementById('details_'+tabs[newtab]).style.display = '';
		}
	}
}
function doprint(pl){
	if(pl){
		var nw = window.open('printerfriendly.cfm?pl=1','_blank');
	}else{
		var nw = window.open('printerfriendly.cfm?','_blank');
	}
}
function pfpass(){
	return document.getElementById('details_out').innerHTML;
}
function pfpass2(){
	if(currentDetailsTab==2)// documents tab
		return document.getElementById('label').innerHTML + "<br />" + document.getElementById('theresults').innerHTML.replace(/label|msds|\|/gi,'');
	else
		return document.getElementById('label').innerHTML + "<br />" + document.getElementById('theresults').innerHTML;
}
function close_win(){
	close_dark_window(document.getElementById('labelcenter_media'));
	document.getElementById('labelcenter_media').innerHTML = '&nbsp;';
}
function send_mediaform(id){
	var fieldsA = listoffields.split(',');
	var urlA = new Array(fieldsA.length);
	var theurl = '';
	for(var i=0;i<fieldsA.length;i++){
		urlA[i] = fieldsA[i]+'='+su(document.getElementById('labelcenter_media_form_'+fieldsA[i]).value);
	}
	theurl = urlA.join('&');
	resp = simpleajax('results.cfm?submitting=1&media='+id+'&'+theurl);
	respA = resp.trim().split('|');
	if(respA[0] == 'error'){
		alert(respA[1]);
		return false;
	} else {
		viewmedia(id);
	}
}
function move_box(){
	once = true;
	box = document.getElementById('disclaimer');
	msie = document.getElementById('msie');
	if(box){
		winheight = get_window_height();
		box.style.top = winheight + document.body.scrollTop - box.offsetHeight + 'px';
	}
}
function get_window_height(){
	var wheight=0;
	if (typeof(window.innerHeight)=='number') {
		wheight=window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			wheight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				wheight = document.body.clientHeight;
			}
		}
	}
	return wheight;
}
function safety_select(which){
	var id = selectedProduct;
	var _state = $("safety_states");
	var _comm = $("safety_coms");
	var _user = $("safety_user");
	var _task = $("safety_task");
	var _lang = $("safety_language");
	
	var state = escape(_state.value);
	var comm = escape(_comm.value);
	var user = escape(_user.value);
	var task = escape(_task.value);
	
	$('safety_printonly').innerHTML = "Safety : <br>" +
		"State : " + _state.options[_state.selectedIndex].text + "<br>" +
		"Commodity : " + _comm.options[_comm.selectedIndex].text + "<br>" +
		"User : " + _user.options[_user.selectedIndex].text + "<br>" +
		"Task : " + _task.options[_task.selectedIndex].text + "<br>" +
		"Language : " + _lang.options[_lang.selectedIndex].text + "<br>";

	if(which == "lang"){
		$("ppe_message_english").style.display = (_lang.value == 'spanish')?"none":"";
		$("ppe_message_spanish").style.display = (_lang.value == 'spanish')?"":"none";
	}

	$("safety_coms").disabled = false;
	$("safety_user").disabled = false;
	$("safety_task").disabled = false;
	
	if(which == "lang" && _state.value != '' && _comm.value != '' && _user.value != '' && _task.value != ''){
		safety_select('task');
		return;
	}else if(_state.value == "other"){
		$("safety_coms").disabled = true;
		$("safety_user").disabled = true;
		$("safety_task").disabled = true;
		$("safety_ppe").innerHTML = unescape($("default_ppe").value);
		$("safety_reppe").innerHTML = unescape($("default_reppe").value);
		return;
	}else if(which == 'state' && _state.value != ''){
		//clear user/task
		clear_dropdown("safety_user");
		clear_dropdown("safety_task");
		$("safety_user").options[0] = new Option("Select User", "");
		$("safety_task").options[0] = new Option("Select Task", "");
		//get commodities
		selectwait($('safety_coms'));
		ajax('ajax/safety_get_commodities.cfm?id='+id+'&st='+state,safety_get_commodities_resp);
	}else if(which == 'state' && _state.value == ''){
		//clear comm/user/task
		clear_dropdown("safety_coms");
		clear_dropdown("safety_user");
		clear_dropdown("safety_task");
		$("safety_coms").options[0] = new Option("Select Commodity", "");
		$("safety_user").options[0] = new Option("Select User", "");
		$("safety_task").options[0] = new Option("Select Task", "");
	}else if(which == 'comm' && _comm.value != ''){
		//clear task
		clear_dropdown("safety_task");
		$("safety_task").options[0] = new Option("Select Task", "");
		//get users
		selectwait($('safety_user'));
		ajax('ajax/safety_get_users.cfm?id='+id+'&st='+state+'&cm='+comm,safety_get_users_resp);
	}else if(which == 'comm' && _comm.value == ''){
		//clear user/task
		clear_dropdown("safety_user");
		clear_dropdown("safety_task");
		$("safety_user").options[0] = new Option("Select User", "");
		$("safety_task").options[0] = new Option("Select Task", "");
	}else if(which == 'user' && _user.value != ''){
		//get tasks
		selectwait($('safety_task'));
		ajax('ajax/safety_get_tasks.cfm?id='+id+'&st='+state+'&cm='+comm+'&usr='+user,safety_get_tasks_resp);
	}else if(which == 'user'){
		//clear tasks
		clear_dropdown("safety_task");
		$("safety_task").options[0] = new Option("Select Task", "");
	}else if(which == 'task' && _task.value != ''){
		// get data
		ajax('ajax/safety_get_data.cfm?id='+id+'&st='+state+'&cm='+comm+'&usr='+user+'&tsk='+task+'&lang='+_lang.value,safety_get_data_resp);
		return;
	}
	$("safety_ppe").innerHTML = "Select State/Commodity/User/Task First";
	$("safety_reppe").innerHTML = "Select State/Commodity/User/Task First";
	task_toggle("scouts_english",0);
	task_toggle("scouts_spanish",0);	
	task_toggle("nocontact_english",0);
	task_toggle("nocontact_spanish",0);
	task_toggle("limited_english",0);
	task_toggle("limited_spanish",0);
	task_toggle("other_english",0);
	task_toggle("other_spanish",0);
	task_toggle("hand_english",0);
	task_toggle("hand_spanish",0);
	task_toggle("contact_english",0);
	task_toggle("contact_spanish",0);
}
function safety_get_commodities_resp(response){
	results_to_select(response,$('safety_coms'));
}
function safety_get_users_resp(response){
	results_to_select(response,$('safety_user'));
}
function safety_get_tasks_resp(response){
	results_to_select(response,$('safety_task'));
	if($('safety_task').length == 1){
		safety_select('task');
	}
}
function safety_get_data_resp(response){
	if(response.match(/\<\!\-\-safety\_rei\-\-\>/)){
		$("safety_ppe").innerHTML = '';
		$("safety_reppe").innerHTML = response;
	}else if(response.match(/\<\!\-\-safety\_ppe\-\-\>/)){
		$("safety_ppe").innerHTML = response;
		$("safety_reppe").innerHTML = '';
	}
	
	var lang = $("safety_language").value;
	var task = $("safety_task").value;
	var user = $("safety_user").value;

	task_toggle("scouts_english",(user == 'Scouts' && lang == 'english')?1:0);
	task_toggle("scouts_spanish",(user == 'Scouts' && lang == 'spanish')?1:0);		
	task_toggle("nocontact_english",(task == 'No Contact Tasks' && lang == 'english')?1:0);
	task_toggle("nocontact_spanish",(task == 'No Contact Tasks' && lang == 'spanish')?1:0);
	task_toggle("limited_english",(task.match(/Limited Contact Tasks/) && lang == 'english')?1:0);
	task_toggle("limited_spanish",(task.match(/Limited Contact Tasks/) && lang == 'spanish')?1:0);
	task_toggle("other_english",(task.match(/All Other Tasks/) && lang == 'english')?1:0);
	task_toggle("other_spanish",(task.match(/All Other Tasks/) && lang == 'spanish')?1:0);
	task_toggle("hand_english",(task == 'Hand Labor Tasks' && lang == 'english')?1:0);
	task_toggle("hand_spanish",(task == 'Hand Labor Tasks' && lang == 'spanish')?1:0);
	task_toggle("contact_english",(task == 'Contact Tasks' && lang == 'english')?1:0);
	task_toggle("contact_spanish",(task == 'Contact Tasks' && lang == 'spanish')?1:0);
}
function task_toggle(which,show){
	if($('task_def_'+which)){
		$('task_def_'+which).className = (show == 1)?"":"noprint";
		$('task_def_'+which).style.display = (show == 1)?"":"none";
	}
}
function clear_dropdown(which){
	if(document.getElementById(which)){
		var el = document.getElementById(which);
		if(el.hasChildNodes()){
			while(el.childNodes.length > 0){
				el.removeChild(el.firstChild);       
			}
		}
	}
}
function docViewChange(toindex_id){
	document.getElementById("supplemental_container").innerHTML = "<tr class='group1'><td>Loading...</td></tr>";
		
	document.getElementById('cmbDocCommodity').length = 0;
	document.getElementById('cmbDocCommodity').options[0] = new Option('Getting Commodities...','');

	var st = document.getElementById("cmbDocState").value;
	var results = simpleajax('results.cfm?t=gcs&sst='+st+'&pid='+toindex_id).trim().split('~');
	document.getElementById('cmbDocCommodity').length = 0;
	document.getElementById('cmbDocCommodity').options[0] = new Option('All Commodities','');
	for(var i=0;i<results.length;i++){
		var items = results[i].split('|');
		document.getElementById('cmbDocCommodity').options[document.getElementById('cmbDocCommodity').length] = new Option(items[0],items[1]);
	}
	
	var cm = document.getElementById("cmbDocCommodity").value;
	w = simpleajax('/labelcenter/results.cfm?filter_docs=filter&filter_p='+toindex_id+'&filter_st='+st+'&filter_cm='+cm);
	
	document.getElementById("supplemental_container").innerHTML = w.trim();
}

function docViewChangeCommodity(toindex_id){
	document.getElementById("supplemental_container").innerHTML = "<tr class='group1'><td>Loading...</td></tr>";
	
	var st = document.getElementById("cmbDocState").value;
	var cm = document.getElementById("cmbDocCommodity").value;
	w = simpleajax('/labelcenter/results.cfm?filter_docs=filter&filter_p='+toindex_id+'&filter_st='+st+'&filter_cm='+cm);
	
	document.getElementById("supplemental_container").innerHTML = w.trim();
}

function send_comment(cid)
{
	if(document.emailform.comment.value!="")
	{
		postajax('/labelcenter/results.cfm?sendcomment='+cid,'comment='+document.emailform.comment.value,send_comment_resp);
	}
}

function send_comment_resp(w)
{
	w=w.trim();
	if(w=="ok")
	{
		document.getElementById("lbl_commentinfo").innerHTML = "Thank You! Your message has been sent.";
		document.emailform.comment.value = ""
	}
	else
	{
		document.getElementById("lbl_commentinfo").innerHTML = w;
	}
}
function plantback_state_change(){
	var p = $('pid').value;
	var s = $('plantback_state').value;
	results_to_select('|Select Commodity First^',$('plantback_condition'));
	$('plantback_result').innerHTML = 'Select State/Commodity/Condition First';
	if(s.match(/^\d+$/) && p.match(/^\d+$/)){
		ajax('ajax/plantback_stuff.cfm?a=getcoms&p='+p+'&s='+s,plantback_state_change_resp);
	} else {
		results_to_select('|Select State First^',$('plantback_commodity'));
	}
}
function plantback_state_change_resp(w){
	results_to_select(w,$('plantback_commodity'));
	plantback_commodity_change();
}
function plantback_commodity_change(){
	var p = $('pid').value;
	var s = $('plantback_state').value;
	var cm = $('plantback_commodity').value;
	$('plantback_result').innerHTML = 'Select State/Commodity/Condition First';
	if(s.match(/^\d+$/) && p.match(/^\d+$/) && cm.match(/^\d+$/)){
		ajax('ajax/plantback_stuff.cfm?a=getconds&p='+p+'&s='+s+'&cm='+cm,plantback_commodity_change_resp);
	} else {
		results_to_select('|Select Commodity First^',$('plantback_condition'));
	}
}
function plantback_commodity_change_resp(w){
	results_to_select(w,$('plantback_condition'));
	plantback_condition_change();
}
function plantback_condition_change(){
	var p = $('pid').value;
	var s = $('plantback_state').value;
	var cm = $('plantback_commodity').value;
	var cn = $('plantback_condition').value;
	if(s.match(/^\d+$/) && p.match(/^\d+$/) && cm.match(/^\d+$/) && (cn != '' || $('plantback_condition').options[$('plantback_condition').selectedIndex].text == 'No Special Conditions')){
		ajax('ajax/plantback_stuff.cfm?a=getplantback&p='+p+'&s='+s+'&cm='+cm+'&cn='+cn,plantback_condition_change_resp);
	} else {
		$('plantback_result').innerHTML = 'Select State/Commodity/Condition First';
	}
}
function plantback_condition_change_resp(w){
	if(success(w)){
		$('plantback_result').innerHTML = w;
	} else {
		$('plantback_result').innerHTML = 'An error has occurred while retrieving data.';
	}
}