//ie or firefox ....
if(!document.all){
window.constructor.prototype.__defineGetter__("event",function(){
		var func = arguments.callee.caller;
		while(func != null){
			var arg0 = func.arguments[0];
			if(arg0 && (arg0.constructor==Event || arg0.constructor ==MouseEvent)){
				return arg0;
			}
			func = func.caller;
		}
    	return null;
	});
}
function $(id) {
	return document.getElementById(id);
}

function show2hide(obj) {
	$(obj).style.display = $(obj).style.display == "none" ? "block" : "none";
}
function chk_search(f_obj){
	var f_obj = f_obj ? f_obj : 'f';

	var f = $(f_obj);
	if(!f || f.value == ''){
		alert("请输入搜素关键字");
		return false;
	}
	var categorybox = document.getElementsByName("category");
	var category = "";
	if(categorybox){
		for(i=0;i<categorybox.length;i++)   
		{
			if(categorybox[i].checked	== true)   
			{
				category = category ? category + "," + categorybox[i].value : categorybox[i].value;
			}
		}
	}
	var startp_obj = $("startp");
	var startp = "";
	if(startp_obj && startp_obj.value != null){
		startp = startp_obj.value;
	}
	var endp_obj = $("endp");
	var endp = "";
	if(endp_obj && endp_obj.value != null){
		endp = endp_obj.value;
	}
	var startdate_obj = $("startdate");
	var startdate = "";
	if(startdate_obj && startdate_obj.value != null){
		startdate = startdate_obj.value;
	}
	var enddate_obj = $("enddate");
	var enddate = "";
	if(enddate_obj && enddate_obj.value != null){
		enddate = enddate_obj.value;
	}

	var wd = encodeURIComponent(f.value);
	var linkpage = "search.php?f=" + wd + "&category=" + category + "&startp=" + startp + "&endp=" + endp + "&startdate=" + startdate + "&enddate=" + enddate;

	window.location.href = linkpage;
	return true;
}
function PopWin(path,width,height){
var win=window.open (path, 'pop', 'width='+width+', height='+height+', top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no'); 
}
function copy(content){
	if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
		clipboardData.setData('Text',content);
		alert("ok");
	} else {
		prompt("no:",content); 
	}
}
function cleanAll(idname) {
	if($(idname)) {
		$(idname).innerHTML = '';
	}
}
function addcar(){
	var d = $("aj_msg");
	var code = $("txtcode");
	var num = $("txtnum");
	if(!code){
		d.innerHTML = "没有选择任何商品";
		setTimeout("cleanAll('" + "aj_msg" + "')", 3000);
	}
	code = code.value;
	if(!num) {
		num = 1;
	}
	num = parseInt(num.value);
	if(!num || num == 0){
		num = 1;
	}
	ajax.sendReq('get','ajax.php?action=addcar&code='+code+'&num='+num,'', savecar);
}
function savecar(obj){
	var t = obj.responseText;
	var d = $("aj_msg");
	if(t == '1'){
		d.innerHTML = '放入购物车成功!';
	}
	else if(t == '401'){
		d.innerHTML = '没有选择任何物品!';		
	}
	else if(t == '402'){
		d.innerHTML = "请先<a target='_blank' href='login.php'>登录</a>或<a target='_blank' href='register.php'>注册</a>";
	}
	else if(t == '403'){
		d.innerHTML = '商品不存在或不支持购买';		
	}
	else if(t == '406'){
		d.innerHTML = '您已注销或者被禁用';
	}
	else if(t == '407'){
		d.innerHTML = '该商品是VIP专卖商品';
	}
	else {
		d.innerHTML = '操作出错或者服务器繁忙';
	}
	setTimeout("cleanAll('" + "aj_msg" + "')", 3000);
}
function show2hidden(divid,width,event){
	var d = $(divid);
	if(d.style.display == '' || d.style.display == 'block'){
		d.style.display = 'none';
	}
	else {
		var E = new Evt(event);
		var w = E.get_absX();
		var h = E.get_absY();
		d.style.width = width + "px";
		d.style.left = (w - (width/2)) + "px";
		d.style.top = h + "px";
		d.style.display = '';
	}
}
function Evt(event) {
	var o = this;
	//ie or firfox .....
	o.evt = event ? event : (window.event ? window.event : null);
	
	o.getX = function() {
		return o.evt.clientX;
	};
	o.getY = function() {
		return o.evt.clientY;	
	};
	o.get_absX = function() {
		return o.evt.clientX +  document.documentElement.scrollLeft;
	};
	o.get_absY = function() {
		return o.evt.clientY + document.documentElement.scrollTop;
	};
}

var ajax = {
    _objPool: [],
    
    _getInstance: function ()
    {
        for (var i = 0; i < this._objPool.length; i ++)
        {
            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
            {
                return this._objPool[i];
            }
        }
        
        // IE5中不支持push方法
        this._objPool[this._objPool.length] = this._createObj();

        return this._objPool[this._objPool.length - 1];
    },

    _createObj: function ()
    {
        if (window.XMLHttpRequest)
        {
            var objXMLHttp = new XMLHttpRequest();

        }
        else
        {
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++)
            {
                try
                {
                    var objXMLHttp = new ActiveXObject(MSXML[n]);        
                    break;
                }
                catch(e)
                {
                }
            }
         }          
        
        // mozilla某些版本没有readyState属性
        if (objXMLHttp.readyState == null)
        {
            objXMLHttp.readyState = 0;

            objXMLHttp.addEventListener("load", function ()
                {
                    objXMLHttp.readyState = 4;
                    
                    if (typeof objXMLHttp.onreadystatechange == "function")
                    {
                        objXMLHttp.onreadystatechange();
                    }
                },  false);
        }

        return objXMLHttp;
    },
    
    // 发送请求(方法[post,get], 地址, 数据, 回调函数)
    sendReq: function (method, url, data, callback)
    {
        var objXMLHttp = this._getInstance();

        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function ()
                {                   
                    if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                    {
                        callback(objXMLHttp);
                    }
                }
            }
            catch(e)
            {
                alert(e);
            }
        }
    }
};
