if (!window.HTMLElement) {
	window['innerHeight'] = {
		valueOf: function() {
			return document.documentElement.clientHeight;
		},
		toString: function() {
			return document.documentElement.clientHeight;
		}
	};
	window['innerWidth'] = {
		valueOf: function() {
			return document.documentElement.clientWidth;
		},
		toString: function() {
			return document.documentElement.clientWidth;
		}
	};
};
if (typeof(Function.prototype.call) != "function") {
	Function.prototype.call = function(obj) {
		obj._554fcae493e564ee0dc75bdf2ebf94ca = this;
		var args = [];
		for (var i = 0; i < arguments.length - 1; i++) {
			args[i] = "arguments[" + (i + 1) + "]";
		}
		var result = eval("obj._554fcae493e564ee0dc75bdf2ebf94ca(" + args.join(",") + ");");
		delete obj._554fcae493e564ee0dc75bdf2ebf94ca;
		return result;
	}
}
Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		__method.apply(object, arguments);
	}
};
function Y(id, win) {
	var elem;
	if (typeof win === 'undefined') win = window;
	elem = (typeof(id) == "string") ? win.document.getElementById(id) : elem = id;
	return elem;
}
function $ce(tagName, doc) {
	if (typeof doc === 'undefined') doc = document;
	var newElem = doc.createElement(tagName);
	return newElem;
}
function $class(className, parentElement, tagName) {
	var elements = new Array();
	var children = (Y(parentElement) || document.body).getElementsByTagName(tagName || '*');
	for (var i = 0; i < children.length; i++) {
		if (children[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) elements.push(children[i]);
	}
	elements.item = function(idx) {
		return this[idx];
	};
	return elements;
}
function fixEvent(e, win) {
	if (typeof win === 'undefined') win = window;
	var evt = (typeof e == "undefined") ? win.event: e;
	return evt;
}
function confirm_redirect(msg, url) {
	if (confirm(msg)) location.href = url;
}
function YunAjax()
{
	/*访问模式*/
	this.method="GET";
	/*返回数据类型*/
	this.returnType="JSON";
	/*是否异步执行*/
	this.asyn=true;
	/*完成后执行*/
	this._onComplete=function(){};
	/*运行前执行*/
	this._onRunning=function(){};
	/*初始化对象*/
	if (window.XMLHttpRequest)
	{
		this.xhr = new XMLHttpRequest();
	} else {
		var MSXML = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
		for (var n = 0; n < MSXML.length; n++) {
			try {
				this.xhr = new ActiveXObject(MSXML[n]);
				break;
			} catch(e) {}
		}
	}
}
YunAjax.prototype.setReturnType=function(type)
{
	if (typeof(type) == 'string' && (type.toUpperCase() == 'JSON' || type.toUpperCase() == 'XML' || type.toUpperCase() == 'TEXT'))
	{
		this.returnType = type.toUpperCase();
	}
}
YunAjax.prototype.addVal=function(key,val)
{
	if (!this.data)
	{
		this.data = new Object;
	}
	this.data[key] = val;
}
YunAjax.prototype.call=function(url, callback, method, asyn)
{
	if (typeof(method) == 'string' && (method.toUpperCase() == 'GET' || method.toUpperCase() == 'POST'))
	{
		this.method = method.toUpperCase();
	}else{
		if (this.data && this.data.length > 0)
		{
			this.method = 'POST';
		}else{
			this.method = 'GET';
		}
	}
	var data = '';
	if (this.data)
	{
		data += this.joinData(this.data);
		delete(this.data);
	}
	var returnType = '';
	if (this.returnType)
	{
		returnType = this.returnType;
	}else{
		returnType = 'JSON';
	}
	if (asyn != undefined)
	{
		this.asyn = asyn ? true: false;
	}else{
		this.asyn = true;
	}
	if (this.method == "GET")
	{
		if (data && data.length > 0)
		{
			url += url.indexOf("?") >= 0 ? "&": "?";
			url += data;
			data = ''
		}
	}
	try {
		if (typeof(this._onRunning) == 'function')
		{
			this._onRunning();
		}
		/*检查一下链接是否未关闭*/
		this.abort();
		this.xhr.open(this.method, url, this.asyn);
		this.xhr.setRequestHeader('YunAjax-Request', "1");
		if (this.method == 'POST')
		{
			this.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
		}
		if (this.asyn) {
			this.xhr.onreadystatechange = (function()
			{
				if (this.xhr.readyState == 4)
				{
					if (typeof(this._onComplete) == 'function') this._onComplete();
					if (this.xhr.status == 200)
					{
						if (typeof(callback) == 'function')
						{
							var result = this.parseResult(this.xhr,returnType);
							callback(result, this.xhr.responseText);
						}
					} else {
						throw new Error("An HTTP error " + this.xhr.status + "occurred. \n" + url);
					}
				}
			}).bind(this);
			if (this.xhr != null) this.xhr.send(data);
		}else {
			this.xhr.send(data);
			if (typeof(this._onComplete) == 'function') this._onComplete();
			if (this.xhr.status == 200)
			{
				var result = this.parseResult(this.xhr, returnType);
				if (typeof(callback) == 'function') callback(result, this.xhr.responseText);
				return result;
			} else {
				throw new Error("An HTTP error " + this.xhr.status + "occurred. \n" + url);
			}
		}
	} catch(e) {
		alert(e);
	}
}
YunAjax.prototype.abort=function()
{
	this.xhr.onreadystatechange=function(){};
	this.xhr.abort();
}
YunAjax.prototype.joinData=function(param, pre)
{
	var returnVal = '';
	if (typeof(param) == 'string')
	{
		var pos = param.indexOf('=');
		if (pos > 0)
		{
			returnVal += this.encode(param.substr(0, pos)) + '=' + this.encode(param.substr(pos + 1)) + '&';
		}else{
			returnVal += 'noindex[]=' + this.encode(param) + '&';
		}
	}else if(typeof(param) == 'object'){
		for (n in param) {
			switch (typeof(param[n])) {
				case 'string':
				if (pre == undefined) {
					returnVal += n + '=' + this.encode(param[n]) + '&';
				} else {
					returnVal += pre + '[' + n + ']=' + this.encode(param[n]) + '&';
				}
				break;
				case 'number':
				if (pre == undefined) {
					returnVal += n + '=' + param[n] + '&';
				} else {
					returnVal += pre + '[' + n + ']=' + param[n] + '&';
				}
				break;
				case 'boolean':
				var val = param[i] ? 1 : 0;
				if (pre == undefined) {
					returnVal += n + '=' + val + '&';
				} else {
					returnVal += pre + '[' + n + ']=' + val + '&';
				}
				break;
				case 'object':
				if (param[n].length == 0) {
					if (pre == undefined) {
						returnVal += n + '=&';
					} else {
						returnVal += pre + '[' + n + ']=&';
					}
				} else {
					if (pre == undefined) {
						returnVal += this.joinData(param[n], n);
					} else {
						returnVal += this.joinData(param[n], pre + '[' + n + ']');
					}
				}
				break;
				default:
			}
		}
	}
	if (pre == undefined) returnVal = returnVal.substr(0, returnVal.length - 1);
	return returnVal;
}
YunAjax.prototype.encode=function(str)
{
	return encodeURIComponent(str);
}
YunAjax.prototype.parseResult=function(xhr, returnType)
{
	var result;
	if (returnType == 'JSON')
	{
		result = this.parseJSON(xhr.responseText);
	} else if (returnType == 'TEXT') {
		result = xhr.responseText;
	} else if (returnType == 'XML') {
		result = xhr.responseXML;
	}
	return result;
}
YunAjax.prototype.parseJSON=function(filter)
{
	try {
		if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(filter)) {
			var j = eval('(' + filter + ')');
			if (typeof filter === 'function') {
				function walk(k, v) {
					if (v && typeof v === 'object') {
						for (var i in v) {
							if (v.hasOwnProperty(i)) {
								v[i] = walk(i, v[i]);
							}
						}
					}
					return filter(k, v);
				}
				j = walk('', j);
			}
			return j;
		}
	} catch(e) {};
	return {"msg":filter};
}
/*设置货币价格*/
function setAmount(obj,Price)
{
	var Amount=(Rate*Price).toFixed(2);
	obj.setAttribute("US",Price);
	obj.innerHTML=obj.innerHTML.replace(/[\d\.]+$/,Amount);
}
/*变更货币*/
function change_currency(id)
{
	var Ajax=new YunAjax();
	Ajax.call('/currency/'+id,function(info,msg)
	{
		if(info.done)
		{
			Rate=info.Rate;
			var obj=document.getElementsByTagName("em");
			var NewAmount;
			for(var i=0;i<obj.length;i++)
			{
				if(obj[i].className=='currency')
				{
					NewAmount=(obj[i].getAttribute("US")*Rate).toFixed(2);
					switch(obj[i].getAttribute("Type"))
					{
						case "0":
							NewAmount=info.Short+info.Symbol+" "+NewAmount;
							break;
						case "1":
							NewAmount=info.Symbol+NewAmount;
							break;
					}
					obj[i].innerHTML=NewAmount;
				}
			}
			change_currency_select(info.Code,info.SelectList);
		}
	});
	return false;
}

/*变更货币下拉框*/
function change_currency_select(Code,SelectList)
{
	var Out=['<li class="first-child"><a rel="nofollow" class="currency_'+Code+'">'+Code+' <img style="vertical-align: middle; margin-left: 4px;" alt="" src="/images/arrow_5x5.gif"></a></li>'];
	var NewList=SelectList.split(",");
	var temp;
	for(var i=0;i<NewList.length;i++)
	{
		temp=NewList[i].split("|");
		Out[Out.length]='<li><a onclick="return change_currency('+temp[0]+');" rel="nofollow" class="currency_'+temp[2]+'" href="#">'+temp[2]+'</a></li>';
	}
	if(Y("currencies_list"))
	{
		Y("currencies_list").innerHTML=Out.join("")+"</ul>";
	}
}


function addToWishlist(id) {
	var Ajax=new YunAjax();
	Ajax.call('/member.php?act=add_wishlist&id=' + id + '&ret_url=' + encodeURIComponent(location.href)+'&t='+new Date().getTime(), addToWishlistRespond);
	return false;
}
function addToWishlistRespond(result, result_text)
{
	if (!result.done) {
		if (result.msg == 'NO_LOGIN') {
			document.location.href = result.retval;
			return;
		}
	}
	if (result.msg.length > 0) {
		alert(result.msg);
	}
}

function addToSubscribe(email,name) {
	var Ajax=new YunAjax();
	Ajax.addVal("Email",email);
	Ajax.addVal("Name",name);
	Ajax.call('/subscribe.html',function()
	{
		alert("Thank you for your subscription!");
		Y("form_subscribe").elements["email"].value="";
	},"post");
	return false;
}


function addToCart(id,spec_id, goods_num) {
	if (spec_id=="" && Y("SpecTable") && Y("SpecTable").rows.length>0) {
		alert("Please select specification!");
		return;
	}
	goods_num = Number(goods_num);
	if (!goods_num || goods_num == 'NaN') {
		alert("Sorry! Goods number shall be non-zero positive integer.");
		return;
	}
	var Accessories=[];
	if(Y("Accessories"))
	{
		list=Y("Accessories").getElementsByTagName("input");
		for(var i=0;i<list.length;i++)
		{
			if(list[i].checked)
			{
				Accessories.push('Accessories[]='+list[i].value);
			}
		}
	}
	var Attribute="";
	if(spec_id!=="")
	{
		Attribute="&Att[]="+spec_id.split(",").join("&Att[]=");
	}
	var Ajax=new YunAjax();
	Ajax.call('/cart.html?act=addCart&id='+id+'&goods_number=' + goods_num +Attribute+ '&'+Accessories.join("&")+'&t='+new Date().getTime(),function(info,msg)
	{
		if(info.done)
		{
			location.href='/cart.html';
		}else{
			alert(info.msg);
		}
	});
}

function onCategoryClick(event,obj)
{
	if(event.srcElement.tagName!=="A")
	{
		obj.nextSibling.style.display=obj.className=='show'?"block":"none";
		obj.className=obj.className=="show"?'hide':'show';
	}
}
function CategorySubOver()
{
	clearTimeout(Timer);
}
function CategoryOut(event,out)
{
	Timer=setTimeout("clearCategory();",50);
}
function clearCategory()
{
	var List=Y("goods_category").getElementsByTagName("li");
	for(var i=0;i<List.length;i++)
	{
		List[i].className='';
		if(List[i].childNodes.length>1 && List[i].childNodes[1].nodeType==1)
		{
			List[i].childNodes[1].style.display="none";
		}
	}
}

function OnFocus(obj)
{
	if(obj.getAttribute("def")==obj.value)
	{
		obj.value="";
	}
	obj.style.color="#333333";
}
function OnBlur(obj)
{
	if(obj.getAttribute("def")==obj.value || obj.value=='')
	{
		obj.style.color="#999999";
		obj.value=obj.getAttribute('def');
	}
}
function inputFloat(obj)
{
	obj.value=obj.value.replace(/ /g,"");
	if(isNaN(obj.value))
	{
		if(obj.value.match(/([\d]+)/)!=null)
		{
			obj.value=RegExp.$1;
		}else{
			obj.value=0;
		}
	}
}
function searchSubmit()
{
	var Min=Y("MinPrice").value;
	var Max=Y("MaxPrice").value;
	var Query=Y("Query").value;
	if(Query!=="")
	{
		Query+="&";
	}
	Query+="price="+Min+"-"+Max;
	location.href='?'+Query;
}
