/**
* Get alsoview data.
* Author: Jin.h.Gao
*/

var GetAlsoView = function (baseURL)
{
	if(baseURL)
		this.baseURL = baseURL;
	else
		this.baseURL = "http://www.ob1b.com:8182/AlsoViewWeb/";
		// this.baseURL = "http://localhost:8080/AlsoViewWeb/";
}

GetAlsoView.prototype = {
	go:function() // 主函数
	{
		
	    var url = window.location.toString().toLowerCase();
	    var urlList = ['productinfo_view.action','product/item-'];
		var flag = false;
	    for(var i=0;i<urlList.length;i++)
	    {
	        if(url.indexOf(urlList[i])>0)
	        {
	            flag = true;
	            break;
	        }
	    }
		
	    if (!flag)
	    {
	        return ;
	    }
		var sno = this.getCurrentNo();
		this.sendRequest(sno);
	},
	getCurrentNo:function()	// 根据链接获取当前商品ID
	{
		var url = window.location.toString();
		var re = /product\/item-(\w+).html/i;
		if(re.test(url))
		{
			re.exec(url);
			var current_id = RegExp.$1;
			return current_id;
		}
		else
		{
			var pos = "product.itemId=";
			var index = url.indexOf(pos);
			var current_id = "";
			if (index > -1)
			{
				var endIndex = url.indexOf("&", index);
				if ( endIndex < index)
				{
					endIndex = url.length;
				}
				current_id = url.substring(index + pos.length , endIndex);
				current_id = current_id.replace(/#+$/g, "");
				return current_id;
			}else
			{
				return 0;
			}
		}
	    /*
	    var myregexp = new RegExp("sysno=\\d+");
	    var matchs = myregexp.exec(url.toLowerCase());
	    if (matchs != null)
	    {
	        var result = matchs[0].substring(6);
	        return result;
	    }
	    else
	    {
	        return 0;
	    }
	    */
	},
	sendRequest:function(pid)	// 发送请求获取AlsoBuy数据
	{
		if (pid)
	    {
	        pid = encodeURIComponent(pid);
	        var url = this.baseURL + "ShowRelated?productId=" + pid + "&upToCategory=false";
	        var script = document.createElement("script");
	        script.src = url;
	        script.type = 'text/javascript';
	        script.defer = true;
			document.getElementsByTagName("head")[0].appendChild(script);
	    }
	}
}

var getAlsoView = new GetAlsoView();
getAlsoView.go();
//alert("exiting");