﻿/*
    事件注册
*/
function addEvent(obj, evType, fn){  
	var bool = false;
	if (obj.addEventListener){  
		obj.addEventListener(evType, fn, false);  
		bool = true;  
	}
	else if(obj.attachEvent)
		bool = obj.attachEvent("on" + evType, fn);  
	return bool;  
}

var getElementsByName = function(tag, name){
    var returns = document.getElementsByName(name);
    if(returns.length > 0) return returns;
    returns = new Array();
    var e = document.getElementsByTagName(tag);
    for(var i = 0; i < e.length; i++){
        if(e[i].getAttribute("name") == name){
            returns[returns.length] = e[i];
        };
    };
    return returns;
};

function getServiceValue(strXML)
{
    if(typeof strXML == 'string')
    {
        var re=new RegExp("(<\\?xml version=\"(.*)\"\\s+encoding=\"(.*)\"\\s*\\?>\\s+<(\\w+)\\s+xmlns=\"http\\://(.*)\">(.*)</(\\w+)>)");
        return(strXML.replace(re,"$6"));
    }
    else
    {
        return '';
    };
};
function checkStorage(productid,storage,oFunction)
{
    var ajax = new Ajax();
    ajax.AddParams("productid",productid);
    ajax.AddParams("buynum",storage);
    ajax.Post("/webserv/service.asmx/sk_GetProductStorage",function(){oFunction(getServiceValue(arguments[0]));});
    ajax.RemoveAllParams();
    ajax.Dispose();    
};
var pricenodes;
var hasshowprice=false;
addEvent(window,'load',new Function("isLogin(dealprice)"));
function isLogin(oFunction)
{
    inittip();
    var ajax = new Ajax();
    ajax.AddParams("id",2);
    ajax.AddParams("price",5);
    ajax.Post("/webserv/service.asmx/sk_IsLogin",function(){oFunction(getServiceValue(arguments[0]),getServiceValue(arguments[1]));});
    ajax.RemoveAllParams();
    ajax.Dispose();    
};
function dealprice(t,x)
{
    if(t!='0#no')
    {
        pricenodes=getElementsByName("cite","pricecon");
        if(document.getElementById('load')==null)
        {
        var node = document.createElement("div");
        node.id = "load";
        node.innerHTML = '价格更新中...';
        document.body.appendChild(node);
        }else{
        document.getElementById('load').innerHTML='价格更新中...';
        document.getElementById('load').style.display='';
        }
        var productids = "";
        var prices="";
        var split=',';
        for(var i=0;i<pricenodes.length;i++)
		{
			productids+=pricenodes[i].id.toString()+split;
			prices+=pricenodes[i].innerHTML.toString()+split;
		};
		
		var ajax = new Ajax();
        ajax.AddParams("productids",productids);
        ajax.AddParams("prices",prices);
        ajax.Post("/webserv/service.asmx/sk_GetMyPrice",function(){
        showprice(getServiceValue(arguments[0]),getServiceValue(arguments[1]));});
        ajax.RemoveAllParams();
        ajax.Dispose();
        if(document.getElementById('load')!=null){
        document.getElementById('load').style.display='none';
        }
    }
    else
    {
        hasshowprice=true;
    };
};
function showprice(t,x)
{
    var newpriceary = t.split(',');
    for(var i=0;i<pricenodes.length;i++)
    {
        if(typeof newpriceary[i]!='undefined')
        {
    	    pricenodes[i].innerHTML=newpriceary[i];
    	};
    };
    hasshowprice=true;
};
function soonkacollect(id)
{
    var ajax = new Ajax();
    ajax.AddParams("id",id);
    ajax.Post("/webserv/service.asmx/sk_ProductCollect",function(){ window.alert(getServiceValue(arguments[0]),getServiceValue(arguments[1]));});
    ajax.RemoveAllParams();
    ajax.Dispose(); 	
};
function fetchOrderCode(objid,oFunction)
{
    var ajax = new Ajax();
    ajax.Post("/webserv/service.asmx/sk_FetchOrderCode",function(){ document.getElementById(objid).value=getServiceValue(arguments[0]);oFunction();});
    ajax.Dispose(); 	
};
function getAreaNameByCode(objid, areacode)
{
     var ajax = new Ajax();
    ajax.AddParams("code",areacode);
    ajax.Post("/webserv/service.asmx/sk_GetAreaNameByCode",function(){ document.getElementById(objid).innerHTML=getServiceValue(arguments[0]);});
    ajax.RemoveAllParams();
    ajax.Dispose(); 
};
function userlogin(name,pwd,code)
{
    var ajax = new Ajax();
    ajax.AddParams("username",name);
    ajax.AddParams("pwd",pwd);
    ajax.AddParams("code",code);
    ajax.Post("/webserv/service.asmx/sk_UserLogin",function(){ if(getServiceValue(arguments[0])=="true") window.location.reload();else alert(getServiceValue(arguments[0]));});
    ajax.RemoveAllParams();
    ajax.Dispose(); 
};

function inittip() {
    var div = document.createElement("div");
    div.id = "searchtips";
    div.className="outdiv";
    div.style.display = "none";
    document.body.appendChild(div);					
document.onclick=function(){ if(document.getElementById('searchtips')!=null){document.getElementById('searchtips').style.display='none';}}

};