function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function SwapImage(strObj, strSrc) {
	if ((x=MM_findObj(strObj))!=null) {
		x.src = strSrc;
	}
}

function EnlargedImage(strURL, intWidth, intHeight) {
	Param = 'SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=no,MENUBAR=no,WIDTH=' + intWidth + ',HEIGHT=' +  intHeight;
	window.open('common/view_image.asp?Image=' + strURL, null, Param);
}



function SelectManufacturer(blnNewBuild) {
	f = document.frmManufacturer;
	if (f.txtManufacturer.selectedIndex > 0) {
		if (blnNewBuild == true) {
			window.location = 'search.asp?mid=' + f.txtManufacturer.value;
		}
		else {
			window.location = 'showcategory.asp?ManufacturerID=' + f.txtManufacturer.value;
		}
	}
}

function ValidateSearch() {
	return ValidateSearchOther('frmSearch');
}

function ValidateSearchOther(strForm) {
	f = eval('document.' + strForm);
	if (f.Search.value == '' || f.Search.value.toLowerCase() == 'quick search' || f.Search.value.toLowerCase() == 'search') {
		alert('Please enter a search query');
		f.Search.focus();
		f.Search.select();
		return false;
	}
	return true;
}

function ValidateSearchSubmit() {
	f = document.frmSearch;
	if (f.Search.value == '' || f.Search.value.toLowerCase() == 'quick search' || f.Search.value.toLowerCase() == 'search') {
		alert('Please enter a search query');
		f.Search.focus();
		f.Search.select();
	}
	else {
		f.submit();
	}
}

function ValidateAdvancedSearch() {
	f = document.frmAdvancedSearch;
	blnReturn = false;
	
	if (f.search_name) {
		if (f.search_name.value != '') { blnReturn = true; }
	}
	if (f.search_subtitle) {
		if (f.search_subtitle.value != '') { blnReturn = true; }
	}
	if (f.search_name_subtitle) {
		if (f.search_name_subtitle.value != '') { blnReturn = true; }
	}
	if (f.manufacturer_name) {
		if (f.manufacturer_name.value != '') { blnReturn = true; }
	}
	if (f.manufacturer_number) {
		if (f.manufacturer_number.value != '') { blnReturn = true; }
	}
	if (f.part_number) {
		if (f.part_number.value != '') { blnReturn = true; }
	}
	if (f.manufacturer) {
		if (f.manufacturer.value != '') { blnReturn = true; }
	}
	if (f.range) {
		if (f.range.value != '') { blnReturn = true; }
	}
	if (f.keywords) {
		if (f.keywords.value != '') { blnReturn = true; }
	}
	if (f.category) {
		if (f.category.options) {
			if (f.category.value != '') { blnReturn = true; }
		}
		else if (f.category.length) {
			for (i=0; i<f.category.length; i++) {
				if (f.category[i].value != '') { blnReturn = true; }
			}
		}
		else {
			if (f.category.value != '') { blnReturn = true; }
		}
	}
	for (i=1; i<=f.length; i++) {
		fld = eval('f.specification_select' + i);
		if (fld) {
			if (fld.value != '') { blnReturn = true; }
		}
		fld = eval('f.specification_search' + i);
		if (fld) {
			if (fld.value != '') { blnReturn = true; }
		}
	}
	
	
	if (!blnReturn) {
		alert('Please enter a search query');
	}
	return blnReturn;
}

function RemoveFromSearch(strField) {
	f = document.frmProducts;
	fld = eval('f.' + strField);
	if (fld) {
		fld.value = '';
		f.reset_page.value = '1';
		f.submit();
	}
}

function RemoveCategoryFromSearch(intCatID) {
	f = document.frmProducts;
	
	if (f.category) {
		f.category.value = f.category.value.replace(intCatID,'');
		f.reset_page.value = '1';
		f.submit();
	}
}

function ShowFullCategoryList() {
	document.getElementById('category_list_short').style.display = 'none';
	document.getElementById('category_list_full').style.display = '';
}

function ShowFullCategoryMatchList() {
	document.getElementById('category_match_short').style.display = 'none';
	document.getElementById('category_match_full').style.display = '';
}

function ProcessSearchCategory() {
	f = document.frmProducts;
	
	if (f.search_category) {
		if (f.search_category.value != '') {
			f.category.value = f.cid.value;
			f.keywords.value = f.search_category.value;
			f.reset_page.value = '1';
		}
	}
}

function ClearSearch() {
	ClearSearchOther('frmSearch');
}

function ClearSearchOther(strForm) {
	f = eval('document.' + strForm);
	if (f.Search.value.toLowerCase() == 'quick search' || f.Search.value.toLowerCase() == 'search') {
		f.Search.value = '';
	}
}

function ClearLoginEmail() {
	f = document.frmLogin;
	if (f.txtEmail.value.toLowerCase() == 'email' || f.txtEmail.value.toLowerCase() == 'email address') {
		f.txtEmail.value = '';
	}
}

function ClearLoginPassword() {
	f = document.frmLogin;
	if (f.txtPassword.value.toLowerCase() == 'password') {
		f.txtPassword.value = '';
	}
}

function ClearTextField(objField, strTextColour) {
	if (objField.value == objField.defaultValue) {
		objField.value = '';
	}
	
	if (strTextColour) {
		if (strTextColour != '') {
			objField.style.color = strTextColour;
		}
	}
}

function ValidateLogin(frm,isReturn) {
	result = true;
	if (result) { result = ValidateTextField(frm.txtEmail,'Please enter your Email Address'); }
	if (result) { result = ValidateEmail(frm.txtEmail); }
	if (result) { result = ValidateTextField(frm.txtPassword,'Please enter your Password'); }

	if (isReturn) {
		return result;
	}
	else {
		if (result) { frm.submit(); }
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function AddToCart(page) {
	if (document.frmPartNo.selOption) {
		page += '&option=';
		if (document.frmPartNo.selOption.options) {
			if (!ValidateMultipleSelect(document.frmPartNo.selOption,'Please select an option from the dropdown list')) {
					return;
				}
				else {
					page += document.frmPartNo.selOption.value;
				}
		}
		else if (!document.frmPartNo.selOption.length) { // 1 hidden field
			page += document.frmPartNo.selOption.value;
		}
		else {
			for(i=0;i<document.frmPartNo.selOption.length;i++) {
				if (!ValidateMultipleSelect(document.frmPartNo.selOption[i],'Please select an option from each dropdown list')) {
					return;
				}
				else {
					if (i>0) {
						page += ',';
					}
					page += document.frmPartNo.selOption[i].value;
				}
			}
		}
	}
	
	if (document.frmPartNo.txtQuantity) {
		qty = document.frmPartNo.txtQuantity;
		if (qty.value.length > 0) {
			q = parseInt(qty.value);
			if (!isNaN(q)) {
				if (page.indexOf('count=') == -1) {
					page += '&count=' + q;
				}
			}
		}
	}
	
	if (page.indexOf('count=') == -1) {
		page += '&count=1';
	}
	
	window.location = page;
	//alert(page);
}

function AddQuantityToCart(intPartNumberID,strURL) {
	f = document.frmProducts;
	fldQty = eval('f.q' + intPartNumberID);
	
	if (!fldQty) {
		alert('Error: An unspecified error has occurred');
		return;
	}
	if (strURL != '') {
		if (ValidateIntegerRange(fldQty, 1, 999, 'Please enter a quantity for this item')) {
			strURL += '&count=' + fldQty.value;
		}
		else { return; }
	}
	else {
		alert('Error: Cart URL not specified');
		return;
	}
	
	window.location = strURL;
}

function AddWeightToCart(intPartNumberID,strURL) {
	f = document.frmProducts;
	fldGrams = eval('f.g' + intPartNumberID);
	fldLbs = eval('f.lb' + intPartNumberID);
	fldOz = eval('f.oz' + intPartNumberID);
	
	if (strURL != '') {
		if (fldGrams) {
			if (ValidateIntegerRange(fldGrams, 1, 9999, 'Please enter a weight for this item')) {
				strURL += '&count=' + fldGrams.value;
			}
			else { return; }
		}
		else if (fldLbs && fldOz) {
			if (fldLbs.value == '') { fldLbs.value = 0; }
			if (fldOz.value == '') { fldOz.value = 0; }
			
			if (ValidateIntegerRange(fldLbs, 0, 99, 'Please enter a Lbs weight for this item') && ValidateIntegerRange(fldOz, 0, 15, 'Please enter a Oz weight for this item')) {
				if ((parseInt(fldLbs.value) > 0 && parseInt(fldOz.value) > 0) || (parseInt(fldLbs.value) == 0 && parseInt(fldOz.value) > 0) || (parseInt(fldLbs.value) > 0 && parseInt(fldOz.value) == 0)) {
					intGrams = Math.round(((parseInt(fldLbs.value) * 16) + parseInt(fldOz.value)) * 28.3495231);
					strURL += '&count=' + intGrams;
				}
				else {
					alert('Please enter a valid weight for this item');
					return;
				}
			}
			else { return; }
		}
		else {
			alert('Error: An unspecified error has occurred');
			return;
		}
	}
	else {
		alert('Error: Cart URL not specified');
		return;
	}
	
	window.location = strURL;
}

function RemoveCartQueryStringFromLocation() {
	s = window.location.search;
	
	if (s.length > 0) {
		s = s.substr(1);
		a = s.split('&');
		
		for (i=0;i<a.length;i++) {
			b = a[i].split('=');
			if (b[0] == 'count' || b[0] == 'action' || b[0] == 'item') { a.splice(i,1); i = i - 1; }
		}
		
		s = '?' + a.join('&');
	}
	
	return s;
}

function SelectAll(frm) {
	if (frm.chkSelectAll && frm.chkSelected) {
		if (frm.chkSelected.length) {
			for (i=0;i<frm.chkSelected.length;i++) {
				frm.chkSelected[i].checked = frm.chkSelectAll.checked;
			}
		}
		else {
			frm.chkSelected.checked = frm.chkSelectAll.checked;
		}
	}
}

function AnySelected(frm) {
	if (frm.chkSelected) {
		if (frm.chkSelected.length) {
			for (i=0;i<frm.chkSelected.length;i++) {
				if (frm.chkSelected[i].checked) {
					return true;
				}
			}
		}
		else if (frm.chkSelected.checked) {
			return true;
		}
	}
	return false;
}

