var skipOver;
var whichPulldownBody;
var searchable = false;

// THIS FUNCTION RUNS WHEN YOU SELECT AN ITEM FROM THE PULLDOWN MENU.
// IT GETS THE CHILD HTML CODE, PARSES IT INTO VALUES, ASSIGNES THOSE
// VALUES TO THE HIDDEN FORM ELEMENTS (SELECTEDPRODUCT AND
// ConditionName) AND UPDATES THE FORM INTERFACE ACCORDINGLY.
function chooseFromPulldown(htmlStr,product) {

	// we get the full html string in the a tags and store it as a string
	var fullHtmlString = htmlStr;

	// we determine whether or not the string is a product or a condition, based upon it's contents
	if (fullHtmlString.indexOf('dhtmlDropdownMenuItemGenericName') != -1) {
	// if it is a product, we determine product name, generic name, and legal chars.
		var isProduct = true;
		var isCondition = false;
		//split the code provided by the line break...put first value into brandProductName
		//var brandProductName = fullHtmlString.split('<BR>')[0];
		if(navigator.userAgent.indexOf('compatible; MSIE') != -1 && navigator.platform == 'Win32' && navigator.appName == 'Microsoft Internet Explorer') {
			var brandProductName = fullHtmlString.split('<BR>')[0];
			var genericProductName = fullHtmlString.split('dhtmlDropdownMenuItemGenericName')[1].substring(1, fullHtmlString.split('dhtmlDropdownMenuItemGenericName')[1].length - 7);
		} else {
			var brandProductName = fullHtmlString.split('<br>')[0];
			var genericProductName = fullHtmlString.split('dhtmlDropdownMenuItemGenericName')[1].substring(2, fullHtmlString.split('dhtmlDropdownMenuItemGenericName')[1].length - 7);
		}

		if ((fullHtmlString.indexOf('<BR>') != -1 || fullHtmlString.indexOf('<br>') != -1) && fullHtmlString.indexOf('&') != -1) {
			var legalMarque = '&' + fullHtmlString.split('<BR>')[0].split('&')[0];
		} else {
			var legalMarque = '';
		}
	} else {
	// if it is a condition, we do not need to parse the string, so our job is easy here.
		var isProduct = false;
		var isCondition = true;
	}
	// now that we have determined product || condition and carried out any necessary parsing, we can set values and update the display
	// maximum characters permitted in field
	//if superscript exists... allow the string to be 32 chars long (?) -ep
	if (fullHtmlString.indexOf('<SUP>') != -1 || fullHtmlString.indexOf('<sup>') != -1) {
		var maxLength=32;
	} else {
		var maxLength = 22;
	}

	//if the selected menu is a product
	if (isProduct) {
		document.getElementById('ProductName').value = product;

		document.getElementById('ConditionName').value = 'false';

		//get actual length of the string
		var stringLength = (brandProductName.length + genericProductName.length + 1);
		//alert(genericProductName);
		// if the string exceeds the maximum characters permitted in the field, we truncate it and add an ellipsis...
		if (stringLength > maxLength) {
			var prodPlusGen = brandProductName + genericProductName;
			//alert(brandProductName + ": " + brandProductName.length + "\n" + genericProductName + ": " + genericProductName.length);
			//truncate the product and generic name to maximum allowed length
			var prodPlusGen = prodPlusGen.substring(0, maxLength);
			//if the product name is less than the total product + generic name,
			if (prodPlusGen.length > brandProductName.length) {
				//
				brandProductName = prodPlusGen.substring(0 , brandProductName.length);
				genericProductName = prodPlusGen.substring(brandProductName.length , maxLength) + '...';
			} else {
				brandProductName = prodPlusGen.substring(0, prodPlusGen.length) + '...';
				genericProductName = '';
			}

		} else {
			genericProductName = genericProductName;
		}


		document.getElementById('pulldownHeader').innerHTML = '<div id="dropDownSelected">' + brandProductName + '<span class="dropDownGeneric">' + genericProductName + '</span></div>';
	} else {
		document.getElementById('ProductName').value = 'false';
		document.getElementById('ConditionName').value = product;
		//document.getElementById('ConditionName').value = fullHtmlString;
		// if the string exceeds the maximum characters permitted in the field, we truncate it and add an elipsis...
		if (fullHtmlString.length > maxLength) {
			fullHtmlString = fullHtmlString.substring(0, maxLength) + '...';
		}

		document.getElementById('pulldownHeader').innerHTML = '<span id="selectedCondition">' + fullHtmlString + '</span>';
	}

	document.getElementById(whichPulldownBody).style.display = 'none';

	// this value determines state of the search button
	searchable = true;

	// We need to call this function if the browser is mozilla
if(navigator.userAgent.indexOf('compatible; MSIE') != -1 && navigator.platform == 'Win32' && navigator.appName == 'Microsoft Internet Explorer') {

}
else
{
	setSearchBtn();
}
}


// THIS FUNCTION RUNS WHENEVER YOU CLICK ONE OF THE RADIO BUTTONS.
function showLevelTwo(showWhich) {
	document.getElementById('formStageTwo').style.display = 'block';
	document.getElementById('pulldownHeader').innerHTML = 'Please Select a ' + showWhich.charAt(0).toUpperCase() + showWhich.substring(1,showWhich.length);
	whichPulldownBody = 'pulldownBody_' + showWhich.toLowerCase();
	document.getElementById('ProductName').value = 'false';
	document.getElementById('ConditionName').value = 'false';
	searchable = false;


	setSearchBtn();
}

function showHide() {

	var checkPullDownBody = document.getElementById(whichPulldownBody).style.display;

	if (checkPullDownBody && checkPullDownBody == 'block') {
		document.getElementById(whichPulldownBody).style.display = 'none';
	} else if (checkPullDownBody && checkPullDownBody == 'none') {
		document.getElementById(whichPulldownBody).style.display = 'block';
	} else {
		document.getElementById(whichPulldownBody).style.display = 'block';
	}
}

// THIS KILLS THE MENU TIMER THAT HIDES THE ARTIFICIAL FORM SELECT ELEMENTS, IT GETS CALLED ONMOUSEOVER IN A BUNCH OF PLACES.
function killMenuTimer() {
	skipOver = setTimeout("document.getElementById('" + whichPulldownBody + "').style.display = 'none'", 500);
}

// MULTI-PURPOSE DIV-SWITCHING FUNCTION
function contentDivFamilySwitch(divFamily, divToActivate) {
	//// SHOW/HIDE DIVS
	for (var loop = 0; loop < document.getElementsByTagName('div').length; loop++) {
		var divIdName = eval("document.getElementsByTagName('div')[" + loop + "].id");
		if (divIdName.indexOf(divFamily) == 0) {
			if (divIdName ==  divFamily + divToActivate) {
				document.getElementById(divIdName).style.display = 'block';
			} else {
				document.getElementById(divIdName).style.display = 'none';
			}
		}
	} //end for loop
}

// RUNS IN A LOOP, POLLING FOR WHETHER OR NOT THE
// USER HAS COMPLETED ENOUGH OF THE FORM TO SEARCH   dhtmlDropdownSearchButton_live
function setSearchBtn() {
	var loop = true;

	if(searchable) {
		contentDivFamilySwitch('dhtmlDropdownSearchButton_', 'dead')
		document.getElementById('fixSafariTextField').disabled = false;

	} else {
		contentDivFamilySwitch('dhtmlDropdownSearchButton_', 'live')
		document.getElementById('fixSafariTextField').disabled = true;

	}
	CenterAllSitePartsTimer = setTimeout("setSearchBtn()", 500);
}

function twistMe(divId, linkElement) {
	var divId = divId + 'div';
	if (linkElement.className == 'off') {
		document.getElementById(divId).className = 'dshow';
		linkElement.className = 'on';
	} else {
		document.getElementById(divId).className = 'dhide';
		linkElement.className = 'off';
	}
}

function doNothing() {
	var bootsy = 'collins';
}

function validateSearchForm() {
	var conditionName = document.getElementById('ConditionName').value;
	var productName = document.getElementById('ProductName').value;

	if((conditionName == "false") && (productName == "false")) {
		return false;
	}
	if(document.getElementById('fixSafariTextField').value == "Keyword(s) - optional")
	{
		document.getElementById('fixSafariTextField').value="";
	}

	if(productName == "false" && conditionName != "false") {
		document.lillyConnect.action = "/search/condition/"+conditionName.toUpperCase();
		document.lillyConnect.submit();
	} else if(productName != "false" && conditionName == "false") {
		document.lillyConnect.action = "/search/product/"+productName.toUpperCase();
		document.lillyConnect.submit();
	}
}