/* Search Function for Shopica */
function PreloadImg() {
	var d = document;
	if (d.images) {
		if (!d.Pli) d.Pli = new Array();
		var i, j=d.Pli.length, a=PreloadImg.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.Pli[j] = new Image;
				d.Pli[j++].src = a[i];
			}
		}
	}
}
function Init() {
	Event.observe (window, 'load', function () {
		new Effect.Appear('resultbox', {
			duration: 1.5,
			from: 0.0,
			to: 0.9,
			afterFinishInternal: function () {
         		$('loadingbox').update('<img src="/images/loading1.gif" class="waiting" align="absbottom" />');
        	}
		});
//		new Effect.toggle('resultbox', 'appear');
//		$('loadingbox').update('<img src="/images/loading1.gif" class="waiting" align="absbottom" />');
	});
}
function fadeOut () {
	if ($F('seKeyword').strip().length>0) {
		var showLength = 20;
		var strKey = $F('seKeyword').stripTags().strip();
		if (strKey.length>showLength) strKey = strKey.substr(0, showLength)+'...';
		$('seTitle').update("Search '"+strKey+"'");

		var seKey = $F('seKeyword').strip();
		seKey = seKey.replace(/\+/g, "_-_");
		seKey = escape(escape(seKey.replace(/\s+/g, "+")));
		seKey = seKey.replace(/\//g, "%252F");
		seKey = seKey.replace(/\+/g, "%2B");
		seKey = seKey.replace(/_-_/g, "%252B");
		document.location='/'+seKey+'/';
	} else {
		return false;
	}
	if ($('loadingbox')) new Effect.Loading('loadingbox');
	if ($('adsbox')) new Effect.toggle('adsbox', 'appear');
	if ($('resultbox')) new Effect.toggle('resultbox', 'appear');
}
function filterShowAll(what, whatBox, more) {
	if($(what)) {
		var liAll = $(what).getElementsByTagName('li');
		if (liAll.length>7) {
			$(whatBox).style.overflowY="auto";
			for(var i=0; i<liAll.length; i++) {
				liAll[i].style.display="";
			}
			$(more).remove();
		}
	}
}
function doFilterCatSelect(obj) {
	var what = 'filterCat';
	if($(what)) {
		var checkboxAll = $(what).getElementsByTagName('input');
		var checkedSum = 0;
		for(var i=0; i<checkboxAll.length; i++) {
			if (checkboxAll[i].checked) checkedSum++;
		}
		if (checkedSum>=6) {
			obj.checked = false;
			if ($('selectCatNotify')) $('selectCatNotify').style.color='red';
		} else {
			if ($('selectCatNotify')) $('selectCatNotify').style.color='';
		}
	}
}
function doFilterCatSubmit(what) {
	if($(what)) {
		var checkboxAll = $(what).getElementsByTagName('input');
		for(var i=0; i<checkboxAll.length; i++) {
			if (checkboxAll[i].checked) {
				showStatusBox();
				$('filterForm').submit();
				break;
			}
		}
	}
}
function doFilterBrandPriceSubmit(what, hasBrand) {
	var re = /^\d+(\.\d+)?$/
	var pass = 0;

	$('vMin').value = $F('vMin').replace(/,/g, '');
	$('vMax').value = $F('vMax').replace(/,/g, '');
	if (!re.test($F('vMin'))) {
		if ($F('vMin').strip().length>0 && $F('vMin').strip()!='min') {
			$('vMin').focus();
			$('checkMsg').show();
			$('checkMsg').update('We did not recognize your input. Please enter numbers and decimals only.');
			return false;
		}
		$('vMin').value = '';
		pass+= 1;
	}
	if (!re.test($F('vMax'))) {
		if ($F('vMax').strip().length>0 && $F('vMax').strip()!='max') {
			$('vMax').focus();
			$('checkMsg').show();
			$('checkMsg').update('We did not recognize your input. Please enter numbers and decimals only.');
			return false;
		}
		$('vMax').value = '';
		pass+= 2;
	}
	if (re.test($F('vMin')) && re.test($F('vMax'))) {
		var tmp = $F('vMin');
		if ($F('vMin')-$F('vMax')>0) {
			$('vMin').value = $F('vMax');
			$('vMax').value = tmp;
		}
		if ($F('vMin')-$F('vMax')==0) {
			$('checkMsg').show();
			$('vMin').value = '';
			$('vMax').value = '';
			$('checkMsg').update('We can not recognize your input. Please enter min or max price again.');
			return false;
		}
	}
	if (re.test($F('vMin')) || re.test($F('vMax'))) {
		var floatReg = /^\d+(\.\d{3,})?$/
		if (floatReg.test($F('vMin'))) {
			$('vMin').value = Math.floor($F('vMin')*100)/100;
		}
		if (floatReg.test($F('vMax'))) {
			$('vMax').value = Math.floor($F('vMax')*100)/100;
		}
		if (re.test($F('vMax')) && $F('vMax')==0) {
			$('checkMsg').show();
			$('checkMsg').update('We can not recognize your input. Please enter min or max price again.');
			return false;
		}
	}

	if($(what)) {
		pass+= 4;
		var checkboxAll = $(what).getElementsByTagName('input');
		for(var i=0; i<checkboxAll.length; i++) {
			if (checkboxAll[i].checked) {
				pass-= 4;
				break;
			}
		}
	}

	if (pass==3 && !hasBrand) {
		$('checkMsg').show();
		$('checkMsg').update("You forgot to refine your search! Please enter a price range before clicking 'Submit'.");
	}
	else if (pass==7) {
		$('checkMsg').show();
		$('checkMsg').update("You forgot to refine your search! Please enter a price range, brand or both before clicking 'Submit'.");
		return false;
	} else {
		$('checkMsg').hide();
		showStatusBox();
		$('filterForm').submit();
	}
}
function removePrice(obj) {
	obj.hide();
	$('priceStrBox').hide();
	$('vMin').value='';
	$('vMax').value='';
	$('priceBox').show();
	$('butView').show();
}
function removeBrand(obj) {
	var checkboxAll = $('filterBrand').getElementsByTagName('input');

	for(var i=0; i<checkboxAll.length; i++) {
		checkboxAll[i].checked = false;
	}
	obj.style.display = 'none';
	if ($('brandListHasProductBox')) $('brandListHasProductBox').hide();
	if ($('brandListNoProductBox')) $('brandListNoProductBox').hide();
	if ($('brandListNoProductTitle')) $('brandListNoProductTitle').hide();
	if ($('filterBrandBox')) $('filterBrandBox').show();
	if ($('butView')) $('butView').show();
	if ($('brandMore')) $('brandMore').show();

}
function getPageStr4CatFilter(action) {
	$('filterForm').action = action;
	$('filterForm').submit();
}
function showStatusBox() {
	if (!document.getElementsByTagName){ return; }
	if (!$('stautsBox')) {
		bod = document.body;
		statusBox = document.createElement('div');
		statusBox.setAttribute('id','statusBox');
		bod.appendChild(statusBox);
		statusBox.innerHTML = '<img id="statusIco" src="/images/ico_pageloading.gif" style="display:none">';
	}
	$('statusBox').style.height = getPageSize().pHeight;
	$('statusIco').style.marginLeft = getPageScroll().xScroll+((getPageSize().wWidth-100)/2);
	$('statusIco').style.marginTop = getPageScroll().yScroll+((getPageSize().wHeight-100)/2);
	$('statusIco').show();
}
function fade(id, opacity) {
	document.getElementById(id).style.display = '';
	if(document.all){
		document.getElementById(id).style.filter='Alpha(Opacity='+opacity+')';
	}else{
		if(opacity<10)
			document.getElementById(id).style.opacity='0.0'+opacity;
		else if(opacity<100)
			document.getElementById(id).style.opacity='0.'+opacity;
		else
			document.getElementById(id).style.opacity='1';
	}
	opacity = parseInt(opacity)+10;
	if(opacity>100)	return '';
	setTimeout("fade('"+id+"','"+opacity+"')",5);
}
function filterOver(obj) {
	obj.style.cursor='pointer';
	obj.style.textDecoration='underline';
}
function filterClick(op) {
	if ($(op)) {
		$(op).checked = $(op).checked ? false : true;
		if (op.indexOf('fc_')!=-1) doFilterCatSelect($(op));
	}
}
function filterOut(obj) {
	obj.style.textDecoration='';
}