﻿// override common.js
Checker.prototype.execute = function(msg)
{
	for (var i in this.frm.elements)
	{
		var f = this.frm.elements[i]
		if (f != null && typeof f.type != "undefined" && typeof f.borderColor_ != "undefined")
			if (f.type == "text" || f.type == "password" || f.type == "textarea")
				f.style.borderColor = f.borderColor_;
	}

	if (this.msg != "")
	{
		__alert((msg||_L12N_["PLS_RETRY"]) + this.msg);

		for (var i in this.frm.elements)
		{
			var f = this.frm.elements[i]

			if (f != null && typeof f.type != "undefined")
				if (f.type == "text" || f.type == "password" || f.type == "checkbox" || f.type == "textarea")
					if (contains(this.err, f.name))
					{
						f.borderColor_ = f.style.borderColor
						f.style.borderColor = "red";
					}
		}

		if (typeof message != "undefined")
			message.style.display = "block"
		return false;
	}

	if (typeof message != "undefined")
		message.style.display = "none"
	this.frm.dirty=0
	return true;
}

// system alert
__alert   = function(msg) { ajaxAlert(msg); }
ajaxAlert = function(msg, icon)
{
	if (msg)
	{
		message.style.display="block"
		gE('message-body').style.color = icon ? "#FF6C02" : "#157B32"
		gE('message-body').innerHTML = msg
		window.scrollTo(0,0)
	}
	else
		message.style.display="none"
}

// product search
function global_search_submit(f)
{
	var c = new Checker(f)
	c.checkEmpty(f.tags, '搜索內容');
	c.execute() && f.submit()

	return false
}

// pageNavAjax
function check_page_nav(cp, total)
{
	if (isNaN(cp) || cp <= 0 || cp > total || cp.indexOf('.')>=0)
	{
		__alert(_L12N_["NO_SUCH_PAGE"])
		return false
	}

	return true
}

// pageNavAjax
function ajax_change_page(url, pg, el, total, evt)
{
	if ((keycode(evt)==0 || keycode(evt)==13) && (!total || check_page_nav(pg, total)))
	{
		window.scrollTo(0, 0)
		if (typeof message != "undefined")
			message.style.display = "none"

		ajaxInclude(url + (url.indexOf("?")==-1 ? "?cp=" : "&cp=") + pg, gE(el), true, hidePreview)
	}
}



/* header.inc
*/
function navSelect()
{
	var l = location + ""

	if      (l.indexOf("/firsttimeusers.jsp") > 0) swapImg(mn1,"nav0_.png");
	else if (l.indexOf("/marketplace/")       > 0) swapImg(mn2,"nav0.png");
	else if (l.indexOf("/diy.jsp")            > 0) swapImg(mn3,"nav0.png");
	else if (l.indexOf("/myaccount/shop/")    > 0) swapImg(mn4,"nav0.png");
	else if (l.indexOf("/myaccount/")         > 0) swapImg(mn6,"nav0.png");
}



// <!-- marketplace/index.jsp
var marketplace_vmode="", marketplace_omode="";

function marketplace_filter_def(obj,v1,v2)
{
	if (obj.value == v1)
		obj.value = v2;
}

function marketplace_resetFilter(f)
{
	var all = categoryfilter.getElementsByTagName("INPUT")
	for (var i=0; i<all.length; i++)
		all[i].checked = false

	f.price1.value=f.price2.value=f.tags.value=''
	doFilter(f)
}

function marketplace_doFilter(f, qs)
{
	var c = new Checker(f)

	if (v(f.tags) == _L12N_["DEF_INP_KEYWORD"])
		f.tags.value = ""
	if (v(f.price1) ==_L12N_["DEF_INP_PRICE"])
		f.price1.value = ""
	if (v(f.price2) ==_L12N_["DEF_INP_PRICE"])
		f.price2.value = ""

	c.checkNumber(f.price2, _L12N_["INCORRECT_PRICE"], 8, 2);
	c.checkNumber(f.price1, _L12N_["INCORRECT_PRICE"], 8, 2);

	if (c.execute())
	{
		qs = qs + "&v=" + marketplace_vmode
		qs = qs.replace(/(categoryIDs=[^&]*)/g, ""); // remove categoryIDs=...
		qs = qs.replace(/(tags=[^&]*)/g, ""); // remove tags=...
		qs += "&price1="+valueOf(f.price1) + "&price2="+valueOf(f.price2) + "&categoryIDs="+valueOf(f.categoryIDs) + "&tags="+urlencode(valueOf(f.tags))

		ajaxInclude("index_loader.jsp?"+qs, "mainlist", true, hidePreview);

		if (!v(f.tags))   f.tags.value   = _L12N_["DEF_INP_KEYWORD"]
		if (!v(f.price1)) f.price1.value = _L12N_["DEF_INP_PRICE"]
		if (!v(f.price2)) f.price2.value = _L12N_["DEF_INP_PRICE"]
	}
}

function marketplace_changeView(x, qs, url)
{
	if (x == 1 || x == 2)
		marketplace_vmode=x; // view mode
	else if (x)
	{
		qs = qs.replace(/&(cp=[\d]*)|^(cp=[\d]*)/g, "");
		marketplace_omode=x; // order mode
	}
	else
		return

	qs = qs.replace(/(^|&)(v=[\d]*)/g, "") + "&v=" + marketplace_vmode;
	qs = qs.replace(/(^|&)(o=[%\w\s]*)/g, "") + "&o=" + marketplace_omode

	ajaxInclude((url || "index_loader.jsp?") + qs, "mainlist", true, hidePreview);
}
// marketplace/index.jsp -->



// <!-- storefront/index.jsp
var storefront_categoryID = -1;

function storefront_changeView(qs, c)
{
	articleBox.style.display = 'none';
	productBox.style.display = 'block';

	storefront_categoryID = c || storefront_categoryID;

	qs = qs.replace(/(^|&)(cp=[\d]*)/g, "");
	qs = qs.replace(/(^|&)(categoryID=[\d]*)/g, "") + "&categoryID=" + storefront_categoryID;
	qs = qs.replace(/(^|&)(o=[%\w\s]*)/g, "") + "&o=" + v(gE("product_order_by"))

	ajaxInclude("index_loader.jsp?" + qs, "mainlist", true, hidePreview);
}

function storefront_showArticle(shopID, x)
{
	articleBox.style.display = 'block';
	productBox.style.display = 'none';

	if (isNaN(x))
		ajaxInclude('indexArticleList_loader.jsp?shopID='+shopID+'&purpose='+x, 'articleDiv', false, hidePreview)
	else
		ajaxInclude('indexArticle_loader.jsp?shopID='+shopID+'&articleID='+x, 'articleDiv', false, hidePreview)
}
// storefront/index.jsp -->



//
// Misc functions
//

// add to bookmark / favorites
function addbookmark(name, url)
{
	if ($.browser.msie)
		window.external.AddFavorite(url, name);
	else if ($.browser.mozilla)
		window.sidebar.addPanel(name, url, '');
}

// blink any HTML element
function blink(el)
{
	if (typeof el.BLINK=="undefined")
		el.BLINK=0

	if (typeof el.timer!="undefined")
		clearInterval(el.timer)

	el.timer=setInterval("blink("+el.id+")",500)
	el.style.visibility= ++el.BLINK % 2 ? "visible" : "hidden"
}
function unblink(el)
{
	if (typeof el.timer!="undefined")
		clearInterval(el.timer)

	el.style.visibility= "visible"
}

function changeCurrency(curr)
{
	setCookie('CURRENCY',curr);
	j();
}

// rotate the src of an <img> obj
function rotateImg(o, arr, t)
{
	var obj = gE(o)

	if (typeof obj.arr == "undefined")
	{
		obj.arr = arr
		obj.idx = 0
	}

	obj.src = "" + obj.arr[obj.idx++]
	window.setTimeout("rotateImg('"+o+"',null,"+t+")", t);

	if (obj.idx >= obj.arr.length)
		obj.idx = 0;
}

// 顔色變換特效
function viewVarient(div,url,w,h)
{
	return; // DEBUG

	gE("bgPic").style.display="none";

	var dv = document.createElement("div");
	dv.id = "divPage";
	var ds = dv.style;
	ds.cursor = "pointer";
	gE("PicToPic").appendChild(dv);

	var str = '<div><img src="'+ url +'" width="'+ w +'" height="'+ h +'"></div>';
	str += '<div align="right"><button style="border:0;padding:2px 0 0 0;" onclick="divClose()">返回</button></div>';
	url ? gE("divPage").innerHTML=str : gE("divPage").innerHTML="<a style='color:red;font-size:12px;'>正在加載中...</a>";
	gE("divPage").style.display="block";

	divClose = function()
	{
		if(gE("divPage").style.display=="block")
		{
			gE("divPage").style.display="none";
			gE("bgPic").style.display="block";
		}
		else
		{
			gE("divPage").style.display="block";
			gE("bgPic").style.display="none";
		}
	}
}

// display only one item from the obj array
function display1(arr, sel, className1, className2)
{
	for (var i=0; i<arr.length; i++)
	{
		if (className1 || className2)
			arr[i].className = sel == i ? className1 : className2
		arr[i].style.display = sel == i ? "block" : "none"
	}
}

/*
myaccount/design/view.jsp
myaccount/product/view.jsp
myaccount/shop/view.jsp
*/
function showTab(ID,cnt)
{
	gE("T").className="top"+ID;
	var arr = []
	for (var i=1; i<=cnt; i++)
	{
		gE("mid"+i).style.fontWeight="normal";
		arr.push(gE("cont0"+i))
	}
	gE("mid"+ID).style.fontWeight="bold"
	display1(arr, ID-1)
}

// <!-- Preview
document.write('<div id=previewDiv style="display:none; z-index:110; position:absolute; border:#000 1px solid; background:#fff; overflow:hidden;"></div>');
var previewReady = false

function showPreview(url, title, w, h)
{
	if (previewReady)
		timer = setTimeout("openPreview('"+url+"','"+title+"',"+w+","+h+");",200);
}

function hidePreview()
{
	clearTimeout(timer);
	gE("previewDiv").style.display= "none";
	document.onmousemove = _mouseoverHdlr
}

var offsetfrommouse=[8,8]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var timer=null
var _mouseoverHdlr=null

function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function openPreview(url, title, width, height)
{
	var docwidth  = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>width && docheight>height))
	{
		var dv = gE("previewDiv")
		dv.innerHTML = '<iframe id="previewIfm" name="previewIfm" scrolling="no" frameborder="0" style="width:100%; height:100%;"></iframe>';
		dv.maxW = dv.style.width = width+20;
		dv.maxH = dv.style.height = height+20;

		document.all("previewIfm").src=url;

		_mouseoverHdlr = document.onmousemove
		document.onmousemove=followmouse;
	}
}

function followmouse(e)
{
	var x = offsetfrommouse[0]
	var y = offsetfrommouse[1]
	var dv = gE("previewDiv")
	var maxW = dv.maxW
	var maxH = dv.maxH

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined")
	{
		if (docwidth - e.pageX < maxW + 2*x)
			x = e.pageX - x - maxW; // Move to the left side of the cursor
		else
			x += e.pageX;

		if (docheight - e.pageY < maxH + 2*y)
			y += e.pageY - Math.max(0,(2*y + maxH + e.pageY - docheight - truebody().scrollTop));
		else
			y += e.pageY;
	}
	else if (typeof window.event != "undefined")
	{
		if (docwidth - event.clientX < maxW + 2*x)
			x = event.clientX + truebody().scrollLeft - x - maxW; // Move to the left side of the cursor
		else
			x += truebody().scrollLeft+event.clientX

		if (docheight - event.clientY < (maxH + 2*y))
			y += event.clientY + truebody().scrollTop - Math.max(0,(2*y + maxH + event.clientY - docheight));
		else
			y += truebody().scrollTop + event.clientY;
	}

	dv.style.display = "block";
	dv.style.left=x+"px"
	dv.style.top=y+"px"
}

$(function() {
	hidePreview()
	previewReady = true
});
// Preview -->

