var hasMouse = false;
var notifBoxOn = false;

function resizeToolbar()
	{
	document.getElementById("toolbar").style.width = "100%";
	}

function switchOffDivs()
	{
	if (!hasMouse)
		{
		document.getElementById("sbList").style.display = "none";
		}
	}

function checkSearchBox(action)
	{
	if (action==0)
		{
		// the focus is NOT on the searchbox, check if it is empty and put the "Search..." text back
		if (document.getElementById("kw").value=="") {document.getElementById("kw").value = "Search...";}
		}
	else
		{
		// the focus IS on the searchbox, check if it is equal to "Search..." and empty if so
		if (document.getElementById("kw").value=="Search...") {document.getElementById("kw").value = "";}
		}
	}

function searchListHover(action)
	{
	if (action==0) {hasMouse=false;}
	else {hasMouse=true;}
	}

function toggleSearchList()
	{
	var sbList = document.getElementById("sbList");
	if (sbList.style.display=="block")
		{
		sbList.style.display = "none";
		}
	else
		{
		hideNotification();
		sbList.style.display = "block";
		}
	}

function chooseSearchItem(chosenItem)
	{
	var sbSelector = document.getElementById("sbSelector");
	sbSelector.innerHTML = chosenItem;
	toggleSearchList();
	}

function goPage(getURL, action)
	{
	if (action==0) {location.href=getURL;}
	else {window.open(getURL);}
	}

function execSearch()
	{
	var searchType = document.getElementById("sbSelector").innerHTML;
	var searchTerms = document.getElementById("kw").value;
	var searchURL = "search.asp";
	if(searchType=="Parts") {searchURL = "/search.asp";}
	else if (searchType=="Broadcasts") {searchURL = "/broadcasts.asp";}
	else if(searchType=="Companies") {searchURL = "/companies.asp";}
	if (searchTerms=="Search...") {searchTerms="";}
	if (searchTerms=="")
		{
		showNotification(35, 215, "notificationImageAlert", "Please enter at least one keyword.");
		}
	else
		{
		location.href = searchURL + "?kw=" + searchTerms + "&a=1";
		}
	}

function execBCSearch()
	{
	var searchTerms = document.getElementById("bcKW").value;
	var searchList = document.getElementById("bcselect").value;
	var searchType = document.getElementById("bcType").value;
	/*
	if (searchTerms=="")
		{
		showNotification(326, 0, "notificationImageAlert", "Please enter at least one keyword.");
		}
	else
		{
	*/
		location.href = "/broadcasts.asp?kw=" + searchTerms + "&sl=" + searchList + "&st=" + searchType;
	//	}
	}

function execNewsSearch()
	{
	var searchTerms = document.getElementById("newsKW").value;
	location.href = "/news.asp?kw=" + searchTerms;
	}

function checkIfEnter(e, whichBox)
	{
	hideNotification();
	// check if "enter" is pressed
	if(window.event) {var keyPressed = e.keyCode;} // IE
	else if(e.which) {var keyPressed = e.which;} // Firefox/Chrome/Opera
	if (keyPressed==13)
		{
		if (whichBox=="Topbar Search") {execSearch();}
		else if (whichBox=="Login") {hideSubmit();}
		else if (whichBox=="Parts Search") {execPartsSearch();}
		else if (whichBox=="Company Search") {execCompSearch();}
		else if (whichBox=="Broadcast Search") {execBCSearch();}
		else if (whichBox=="News Search") {execNewsSearch();}
		}
	}

function showNotification(divPosTop, divPosLeft, divNotType, divNotText)
	{
	var notifBox = document.getElementById("notificationBox");
	notifBox.style.top = divPosTop + "px";
	notifBox.style.left = divPosLeft + "px";
	document.getElementById("warningImg").className = divNotType;
	document.getElementById("warningText").innerHTML = divNotText;
	notifBoxOn = true;
	$("#notificationBox").fadeIn(200, "linear");
	}

function hideNotification()
	{
	notifBoxOn = false;
	$("#notificationBox").fadeOut(200, "linear");
	}

function showLogin()
	{
	hideNotification();
	document.getElementById("loginTextDiv").style.display = "none";
	$("#loginDiv").fadeIn(200, "linear");
	}
function hideLogin()
	{
	hideNotification();
	if (document.getElementById("loginDiv"))
		{
		document.getElementById("loginDiv").style.display = "none";
		}
	$("#loginTextDiv").fadeIn(200, "linear");
	}
	
function hideSubmit()
	{
	var inputEmail = document.getElementById("txtEmail");
	var correctEmail = verifyEmail(inputEmail.value)
	var inputPassword = document.getElementById("txtPassword");
	if (jQuery.trim(inputEmail.value)=="")
		{
		showNotification(96, 0, "notificationImageError", "You have to enter your email address.");
		}
	else if (!correctEmail)
		{
		showNotification(96, 0, "notificationImageError", "The email address you entered is invalid.");
		}
	else if (jQuery.trim(inputPassword.value)=="")
		{
		showNotification(96, 170, "notificationImageError", "You have to enter your password.");
		}
	else
		{
		inputEmail.setAttribute('readonly', 'readonly');
		inputPassword.setAttribute('readonly', 'readonly');
		document.getElementById("loginButtDiv").style.display = "none";
		$("#loginProcessDiv").fadeIn(200, "linear");
		hideNotification();
		document.getElementById("frmLogin").submit();
		return true;
		}
	}

function execPartsSearch()
	{
	var inputKW = document.getElementById("textKW");
	if (jQuery.trim(inputKW.value)=="")
		{
		showNotification(167, 0, "notificationImageError", "Enter Part Number, Description or Keywords.");
		}
	else
		{
		location.href = "/search.asp?kw=" + inputKW.value;
		}
	}

function execCompSearch()
	{
	var inputKW = document.getElementById("compKW");
	var selCountry = document.getElementById("companyCountries");
	/*
	if (jQuery.trim(inputKW.value)=="")
		{
		showNotification(167, 0, "notificationImageError", "Enter Company, Contact Person or Specialty.");
		}
	else
		{
	*/
		location.href = "/companies.asp?a=1&kw=" + encodeURIComponent(inputKW.value) + "&c=" + encodeURIComponent(selCountry.value);
	//	}
	}

function sendMemberEmail()
	{
	var elemSubject = document.getElementById("subject");
	var elemMessage = document.getElementById("message");
	elemSubject.style.border = "solid 1px #c4c4c4";
	elemMessage.style.border = "solid 1px #c4c4c4";
	if ((elemSubject.value=="") || (elemSubject.value=="Please enter a Subject"))
		{
		elemSubject.style.border = "solid 1px #c00";
		elemSubject.value = "Please enter a Subject";
		}
	else if ((elemMessage.value=="") || (elemMessage.value=="Please enter a Message"))
		{
		elemMessage.style.border = "solid 1px #c00";
		elemMessage.value = "Please enter a Message";
		}
	else
		{
		document.sendMail.submit();
		return true;
		}
	}

function sendRFQ()
	{
	var elemCompany = document.getElementById("fromCompany");
	var elemContact = document.getElementById("fromContact");
	var elemEmail = document.getElementById("fromEmail");
	var elemQuantity = document.getElementById("reqQty");
	var elemSecCode = document.getElementById("securitycode");
	elemCompany.style.border = "solid 1px #c4c4c4";
	elemContact.style.border = "solid 1px #c4c4c4";
	elemEmail.style.border = "solid 1px #c4c4c4";
	elemQuantity.style.border = "solid 1px #c4c4c4";
	elemSecCode.style.border = "solid 1px #c4c4c4";
	if ((elemCompany.value=="") || (elemCompany.value=="Please enter your company name"))
		{
		elemCompany.style.border = "solid 1px #c00";
		elemCompany.value = "Please enter your company name";
		}
	else if ((elemContact.value=="") || (elemContact.value=="Please enter the contact person"))
		{
		elemContact.style.border = "solid 1px #c00";
		elemContact.value = "Please enter the contact person";
		}
	else if ((elemEmail.value=="") || (elemEmail.value=="Please enter your email address"))
		{
		elemEmail.style.border = "solid 1px #c00";
		elemEmail.value = "Please enter your email address";
		}
	else if ((elemQuantity.value=="") || (elemQuantity.value=="Enter quantity"))
		{
		elemQuantity.style.border = "solid 1px #c00";
		elemQuantity.value = "Enter quantity";
		}
	else if ((elemSecCode.value=="") || (elemSecCode.value=="Enter Code"))
		{
		elemSecCode.style.border = "solid 1px #c00";
		elemSecCode.value = "Enter Code";
		}
	else
		{
		document.getElementById("sec_code").value = document.getElementById("securitycode").value;
		document.getElementById("securitycode").value = "";
		document.frmRFQ.submit();
		return true;
		}
	}

function sendContactEmail()
	{
	var elemFromName = document.getElementById("sender_contact");
	var elemFromComp = document.getElementById("sender_company");
	var elemFromEmail = document.getElementById("sender_email");
	var elemFromTel = document.getElementById("sender_telephone");
	var elemSubject = document.getElementById("subject");
	var elemMessage = document.getElementById("message");
	elemFromName.style.border = "solid 1px #c4c4c4";
	elemFromComp.style.border = "solid 1px #c4c4c4";
	elemFromEmail.style.border = "solid 1px #c4c4c4";
	elemFromTel.style.border = "solid 1px #c4c4c4";
	elemSubject.style.border = "solid 1px #c4c4c4";
	elemMessage.style.border = "solid 1px #c4c4c4";
	if ((elemFromName.value=="") || (elemFromName.value=="Please enter your name"))
		{
		elemFromName.style.border = "solid 1px #c00";
		elemFromName.value = "Please enter your name";
		}
	else if ((elemFromComp.value=="") || (elemFromComp.value=="Please enter your company name"))
		{
		elemFromComp.style.border = "solid 1px #c00";
		elemFromComp.value = "Please enter your company name";
		}
	else if ((elemFromEmail.value=="") || (elemFromEmail.value=="Please enter your email address"))
		{
		elemFromEmail.style.border = "solid 1px #c00";
		elemFromEmail.value = "Please enter your email address";
		}
	else if ((elemSubject.value=="") || (elemSubject.value=="Please enter a Subject"))
		{
		elemSubject.style.border = "solid 1px #c00";
		elemSubject.value = "Please enter a Subject";
		}
	else if ((elemMessage.value=="") || (elemMessage.value=="Please enter a Message"))
		{
		elemMessage.style.border = "solid 1px #c00";
		elemMessage.value = "Please enter a Message";
		}
	else
		{
		if (elemFromTel.value=="") {elemFromTel.value = "None entered";}
		document.sendMail.submit();
		return true;
		}
	}

function verifyEmail(enteredEmail)
	{
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (enteredEmail.search(emailRegEx) == -1) {return false;}
	else {return true;}
	}

function showRecommendForm(action)
	{
	hideNotification();
	if (action==1)
		{
		document.getElementById("recommendButton").style.display = "none";
		document.getElementById("reqRecommendButton").style.display = "none";
		$("#recommendForm").fadeIn(200, "linear");
		}
	else
		{
		document.getElementById("recommendForm").style.display = "none";
		$("#recommendButton").fadeIn(200, "linear");
		$("#reqRecommendButton").fadeIn(200, "linear");
		}
	}

function showReqRecommendForm(action)
	{
	hideNotification();
	if (action==1)
		{
		document.getElementById("recommendButton").style.display = "none";
		document.getElementById("reqRecommendButton").style.display = "none";
		$("#reqRecommendForm").fadeIn(200, "linear");
		}
	else
		{
		document.getElementById("reqRecommendForm").style.display = "none";
		$("#recommendButton").fadeIn(200, "linear");
		$("#reqRecommendButton").fadeIn(200, "linear");
		}
	}

function recommendSubmit()
	{
	hideNotification();
	if (document.getElementById("recommendContent").value!="")
		{
		document.getElementById("frmRecommend").submit();
		return true;
		}
	else
		{
		var leftPos = document.getElementById("recommendContent").offsetLeft - 462;
		var topPos = document.getElementById("recommendContent").offsetTop + document.getElementById("recommendContent").offsetHeight;
		showNotification(topPos, leftPos, "notificationImageError", "Please enter your recommendation here.");
		}
	}

function requestSubmit()
	{
	hideNotification();
	if (document.getElementById("requestContent").value!="")
		{
		document.getElementById("frmReqRecommend").submit();
		return true;
		}
	else
		{
		var leftPos = document.getElementById("requestContent").offsetLeft - 462;
		var topPos = document.getElementById("requestContent").offsetTop + document.getElementById("requestContent").offsetHeight;
		showNotification(topPos, leftPos, "notificationImageError", "Please enter your request here.");
		}
	}

function switchOffBorder(elem)
	{
	elem.style.border = "solid 1px #c4c4c4";
	}

