// JavaScript Document
navTL = new Image()
navTL.src = "images/nav_tl.gif"

navTR = new Image()
navTR.src = "images/nav_tr.gif"

badgeSearch = new Image()
badgeSearch.src = "images/images/badge_newsearch2.gif"

var homeSubMenuInnerHtml ="&nbsp;";
var navtimerID;

// used for onloads
//function init() {
	//homeSubMenuInnerHtml = document.getElementById("quicksearch").innerHTML;
	//alert(homeSubMenuInnerHtml);
	//setSelectedIndexByValue(document.getElementById("scat"), 9){
//}


function setSelectedIndexByValue(element, value){
		
	
	
}
function isSelectedIndex(element, errorAlert, indexValue) {     
	if (element.selectedIndex == indexValue) {  
		alert(errorAlert);
		element.focus();
		return false;        
	}  
	return true;
}  
function showSubCat(mainCategory, newColour) {
	//alert(menuArray[mainCategory][1]);
	//alert(menuArray.eval(mainCategory));
	barDiv = document.getElementById("bar");
	if (mainCategory == "c0" ){
		barDiv.innerHTML = homeSubMenuInnerHtml;
	} else {
		//alert(menuArray[mainCategory]);		
		
		//alert(barDiv.innerHTML);
		barDiv.innerHTML = menuArray[mainCategory];		
	}
	barDiv.style.backgroundColor = newColour;
	clearTimer();
}

//navigation timer functions
function startTimer() {
	clearTimer();
	navtimerID = setTimeout("setCurrentCat()", 2500)
}
function clearTimer() {
	clearTimeout(navtimerID)
}

function setCurrentCat() {
	clearTimeout(navtimerID)
	barDiv = document.getElementById("bar");
	barDiv.innerHTML = curSubHtml;// "menu reset";
	barDiv.style.backgroundColor = curSubColour; //"#000000";
}


// clear inputs
function clearInput(defaultText, elementName) {	
	element = document.getElementById(elementName);			
	//alert(element.value)
	if (element.value == defaultText) {
		element.value= "";
	}	
}
function submitSearch(defaultText, formName) {
	form = document.getElementById(formName);			
	if (form.stext.value == defaultText) {
		form.stext.value= "";
	}	
}

function submitSearch2(defaultText1,defaultText2,defaultText3, formName) {
	//form = document.getElementById(formName);			
	loading(1);
	
	if (document.getElementById("stext1").value == defaultText1) {
		document.getElementById("stext1").value= "";
	}
	if (document.getElementById("stext2").value == defaultText2) {
		document.getElementById("stext2").value= "";
	}
	if (document.getElementById("stext3").value == defaultText3) {
		document.getElementById("stext3").value= "";
	}	
}
function submitMailingList(defaultText) {		
	element = document.getElementById("mlEmail"); 			
	if ( validateEmail(element.value, defaultText) == false ) {	
		alert("Please enter a valid email address.");
		element.focus();
		return false;		
	}					
	return true;
}
function submitQuickProductSearch(defaultText) {		
	element = document.getElementById("partNumber"); 			
	if ( element.value == "" ) {	
		alert("Please enter a part number to search for.");
		element.focus();
		return false;		
	}					
	return true;
}


function submitClientLogin(defaultUser, defaultPassword) {
	user = document.getElementById("user"); 
	password = document.getElementById("pass"); 

	if ( (user.value == defaultUser) && (password.value == defaultPassword ) ) {		
		alert("Please enter your username and password.");
		user.focus();
		return false;
	}
	if ( (user.value == defaultUser) || (user.value == '' ) ) {		
		alert("Please enter your username.");
		user.focus();
		return false;
	}
	if ( (password.value == defaultPassword) || (password.value == '' ) ) {		
		alert("Please enter your password.");
		password.focus();
		return false;
	}		
	return;	
}


function validateEmail(email, defaultText) {	
	if ((email == "") || (email == defaultText)) {
		return false;
	} else {
		eLength = email.length;
		eAt = email.indexOf("@"); 
		eDot = email.lastIndexOf(".");	
		
		// if no @ sign, @ is at start or end of email
		if ( (eAt == -1) || (eAt == 0) || (eAt == eLength-1) ) {
			return false;
		} else {
			// if no dot, dot is at start end or next to or less than @
			if ( (eDot == -1) || (eDot == 0) || (eDot == eLength-1) || (eDot <= eAt+1)  )  {
				return false;
			}		
		}
	}
	return true;
}

// set input to text to default text
function setInput(defaultText) {
	alert(defaultText);
}



// form validation
function valForm() {	
	// must enter a name
	if (document.contact.surname.value == "") {
		alert("Please enter your name.")
		document.contact.surname.focus()
		return false
	}
	// must enter a method of contact
	if ( (document.contact.telephone.value == "") && (document.contact.email.value == "") ) {
		alert("Please enter a contact telephone number or email address.")
		document.contact.telephone.focus()
		return false
	} else {
		// if an email address entered check its valid format
		if ( document.contact.email.value != "") {
			if ( validateEmail(document.contact.email.value) == false ) {
				alert("Please enter a valid email address.")
				document.contact.email.focus()
				return false		
			}				
		}
	}	
	return true
}

// output back link
function backLink() {
	if (history.length != 0 ) {
		document.write("<a href='javascript:history.back()' >&lt;&lt; Back</a>");
	}
}
function closeButton() {
	document.write("<input type='button' class='button' name='close' value='CLOSE' onClick='javascript:self.close()' />");
}

function openPage(pageToOpen) {
	window.location = pageToOpen;
}
function populate() {
	detailsForm = document.getElementById("customerDetails");

	if (detailsForm.sameasdetails.checked) {
		//alert(true);
						
		detailsForm.sname.value = detailsForm.name.value;
		detailsForm.saddress.value =detailsForm.address.value;
		detailsForm.scity.value =detailsForm.city.value;
		detailsForm.sstate.selectedIndex = detailsForm.state.selectedIndex;
		detailsForm.szip.value = detailsForm.zip.value;	
		detailsForm.scity.value = detailsForm.city.value;
		//detailsForm.scountry.value ="";
		
	}
	else {
		//alert(false);
		detailsForm.sname.value ="";
		detailsForm.saddress.value ="";
		detailsForm.scity.value ="";
		detailsForm.sstate.selectedIndex =0;
		detailsForm.szip.value ="";	
		detailsForm.scity.value ="";
		//detailsForm.scountry.value ="";
			
	}
}

function openEmailWindow(id) {
	popupWin = window.open('/emailfriend.asp?id='+id,'email_friend','menubar=no, scrollbars=yes, width=400, height=380, directories=no,location=no,resizable=yes,status=no,toolbar=no')
	popupWin.focus();	
	return false;
}
function openHelpWindow(id) {
	popupWin = window.open('/yshelp.asp?id='+id,'yshelp','menubar=no, scrollbars=yes, width=300, height=280, directories=no,location=no,resizable=yes,status=no,toolbar=no')
	popupWin.focus();
	return false;
}
function openProductImage(id) {
	popupWin = window.open('/ysProductImage.asp?pid='+id,'yshelp','menubar=no, scrollbars=yes, width=450, height=500, directories=no,location=no,resizable=yes,status=no,toolbar=no')
	popupWin.focus();
	return false;
}
function loading(intType) {
	document.getElementById("bottomFrame").style.display = "none";
	document.getElementById("loading").style.display = "block";
	
	document.getElementById("loadingFrame").src = "/images/loading.gif";	
}
function resizeImage(imageId, maxSize, dimension) {
	if (document.getElementById(imageId)) {
		imgWidth = document.getElementById(imageId).width;
		imgHeight = document.getElementById(imageId).height;
		
		resize_image = false;
		
		if (dimension=="h") {					
			if (imgHeight > maxSize ) {
				new_width  = (imgWidth  * maxSize) / imgHeight;
				new_height = maxSize ;	
				
				resize_image = true;
			} 
		} else {		
			if (imgWidth > maxSize ) { 
				new_width = maxSize ;
				new_height = (imgHeight  * maxSize) / imgWidth;
				
				resize_image = true;
			}
		}
		//alert("orig dimensions: " + imgWidth + " x " + imgHeight);
				
		if (resize_image ) {
			//alert("resizing!: " + new_width + " x " + new_height);
			document.getElementById(imageId).width = parseInt(new_width);
			document.getElementById(imageId).height = parseInt(new_height);
		}
			
		
	}	
}

function hideOrShow(id) {
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "block";
	} else {
		document.getElementById(id).style.display = "none";
	}
	return false;
}
