
window.addEvent("domready", function(){
	//var links = $$("a");
	new SmoothScroll();

	new Accordion($$("#faqlist li span"), $$("#faqlist li p"), { "alwaysHide":true, "show":-1 });

	$$(".volatile").each(function(el){
		$(el).addEvent("focus", function(){
			if(this.getValue() == this.getProperty("title")) {
				this.value = "";
			}
		});
	});

	$$("a[rel*='external']").each(function(a){ 
		$(a).setProperty("target", "_blank");
	});

	$$(".helpdiv").each(function(el) {
		$(el).setStyle("display","none");
	});

  	$$(".helpicon").each(function(el) {
		$(el).addEvent("click", function(e) {
			new Event(e).stop();
			var elem = $(this.getProperty("id").replace("helpicon","helpdiv"));
			if(elem.getStyle("display") == "none") {
				elem.setStyle("display","block");
			} else {
				elem.setStyle("display","none");
			}
		});  
	});

	/*
	if($("quantity")){
		$("quantity").addEvent("change", function(e){
			$("totalprice").setHTML("&pound;" + ($("productprice").getValue() * this.getValue()).numberFormat(2));
		});
	}
	
	$$("input[value=selectall]").each(function(el){
		var formID = el.id.substr(10, el.id.length);
		if($(formID)){
			el.addEvent("click", function(){
				$ES("input[type=checkbox]",$(formID)).each(function(checkBox){
					if($(checkBox).getProperty("value") != "selectall") {
						$(checkBox).checked = $(el).checked;
						//$(checkBox).fireEvent("click");
					}
				});
			});
		}
	});
	
    $$('#SuspectReaction').each(function(el){
        var checklistID = '';
        new MysticMeg("autocomplete_" + checklistID, { topOffset: 20, leftOffset: -8, onSelect: checklistAutoCompleteSelect, onSuccess: checklistAutoCompleteSuccess });
 
    });	 
    */
	
    /* ShowHide control for Other Source field */
    var OtherSourceFieldControl = {
    
		init: function(){
			$$('select[id^=Source_]').each(function(el){
				OtherSourceFieldControl.checkOtherField(el);
				el.addEvent('change', function(){
					OtherSourceFieldControl.checkOtherField(el);
				});
			});
		},
		
		checkOtherField: function(el){
			var listID = OtherSourceFieldControl.getListIDNumber(el.id);
			
			if (el.getValue() == 'Other')
			{				
				OtherSourceFieldControl.show(listID);
			}
			else
			{
				OtherSourceFieldControl.hide(listID);
			}
		},
		
		show: function(listID){
			OtherSourceFieldControl.otherField(listID).setStyle('display','');
			OtherSourceFieldControl.otherFieldTitle(listID).setStyle('display','');
		},
		
		hide: function(listID){
			OtherSourceFieldControl.otherField(listID).setStyle('display','none');
			OtherSourceFieldControl.otherFieldTitle(listID).setStyle('display','none');
		},
		
		otherField: function(listID) {
			return $('SourceOther_' + listID).getParent();
		},
		
		otherFieldTitle: function(listID){
			return $('SourceOtherTitle_' + listID);
		},
		
		getListIDNumber: function(id){
			var idArray = id.split('_');
			if (idArray.length >= 2)
			{
				return idArray[1];
			}
		}
		
    }
    
    OtherSourceFieldControl.init();
	
});

window.addEvent("load", function(){
	if($("SuspectReaction")){
		new MysticMeg($("SuspectReaction"), {
			ajaxUrl:"/_colony/MedDRALists/look-up.asp", 
			topOffset:20,
			listClass:"predictive",
			multipleSelect: false,
			onSelect: function(v, f, o){
				f.value = v.value;
				o.flush();
				o.hideResults();
			}
		});
	}
	
	$$(".SuspectReaction").each(function(elem){
		new MysticMeg(elem, {
			ajaxUrl:"/_colony/MedDRALists/look-up.asp", 
			topOffset:20,
			listClass:"predictive",
			multipleSelect: false,
			onSelect: function(v, f, o){
				f.value = v.value;
				o.flush();
				o.hideResults();
			}
		});
	});

	if($("SuspectDrug")){
		new MysticMeg($("SuspectDrug"), {
			ajaxUrl:"/_colony/MHRALists/look-up.asp", 
			topOffset:20,
			listClass:"predictive",
			multipleSelect: false,
			onSelect: function(v, f, o){
				f.value = v.value;
				o.flush();
				o.hideResults();
			}
		});
	}
	
	if($("OtherDrug")){
		new MysticMeg($("OtherDrug"), {
			ajaxUrl:"/_colony/MHRALists/look-up.asp", 
			topOffset:20,
			listClass:"predictive",
			multipleSelect: false,
			onSelect: function(v, f, o){
				f.value = v.value;
				o.flush();
				o.hideResults();
			}
		});
	}
  
  if($("ReactionOutcome_Caused_Death") ) {
  
     $("ReactionOutcome_Caused_Death").addEvent("click", function(e) {
  
    
    });
  
  
  }	

	$$(".hascondition").each(function(el){
		el.setStyle("display", "none");
	});

	$$(".hasotherfield").each(function(el){
		var otherFields = el.getProperty("name");
		var selectedValue = el.value;
		
		if(el.getProperty("type") == "radio"){
			elements = $$("input[name=" + otherFields + "]");
		} else {
			elements = el;
		}
		
		elements.addEvent("click", function(e){
			if(this.getProperty("id").toLowerCase() == "reactionoutcome_caused_death"){
				if($("ReactionSeverity_7")) $("ReactionSeverity_7").checked = true;
				if($("ReactionSeriousReasons_1")) $("ReactionSeriousReasons_1").checked = true;
			}
			
			//hide related other fields
			$$(".tid_" + otherFields).each(function(others){
				others.setStyle("display", "none");
			});
			
			//then show if has other field
			$$(".tid_" + this.getProperty("id")).each(function(others){
				others.setStyle("display", "inline");
			});
	
			
		});

			if(el.getValue() == selectedValue){
				$$(".tid_" + el.getProperty("id")).each(function(others){
					others.setStyle("display", "inline");
				});
			}
	});
	
	$$(".weighti").each(function(el) {
	    el.addEvent("keyup", function(e) {
	        var strRootName = this.id;
            strRootName = strRootName.substr(0, strRootName.indexOf('_'));
            var iStones, iPounds, iValue
            iStones = $(strRootName + '_stones').value;
            iPounds = $(strRootName + '_pounds').value;
            iValue = 0;
            if (!(isNaN(iStones))) {
                iValue = iStones * 6.35;
            }
            
            if (!(isNaN(iPounds))) {
                iValue = iValue + (iPounds * 0.453);
            }
            
            $(strRootName).value = iValue.toFixed(2);
            
	        
	    });
	});
	
	$$(".heighti").each(function(el) {
	    el.addEvent("keyup", function(e) {
	        var strRootName = this.id;
            strRootName = strRootName.substr(0, strRootName.indexOf('_'));
            var iFeet, iInches, iValue
            iFeet = $(strRootName + '_feet').value;
            iInches = $(strRootName + '_inches').value;
            iValue = 0;
            if (!(isNaN(iFeet))) {
                iValue = iFeet * 0.304;
            }
            
            if (!(isNaN(iInches))) {
                iValue = iValue + (iInches * 0.025);
            }
            
            $(strRootName).value = iValue.toFixed(2);
            
	        
	    });
	});	
	
	if ($("Referrer")) {
	
		checkReferrerValue();
		
		$("Referrer").addEvent("change", function(el) {
			 
           checkReferrerValue();   
	        
	    });

	};	
	
});


function showHideElement(element, bShow) {
	
	if (bShow) 
	{
		element.removeClass('hiddenfield');
	}
	else 
	{
		element.addClass('hiddenfield')	;
	}

}

function checkReferrerValue(){
	    
	if ( $("Referrer").value == 'Other (please give details)') 
    {
		showHideElement($("fieldWrapper_ReferrerOther"), true);
    }
    else 
    {
		showHideElement($("fieldWrapper_ReferrerOther"), false);
    } 
     
}


/* Check List Functions */
function addToCheckList( addValue, addText, fieldID ) {
	
}

function checklistAutoCompleteSuccess( oResults, inputElement ){
	oResults.each(function(oResult){
 
    });	
}

function checklistAutoCompleteSelect(oSelected, inputElement){
  
}

function initializeCheckListRemove(el){
    el.addEvent("click", function(e){
        
    });
}


