﻿//URI类
function Request() {
	this.Location = document.location;
	var me = this;
	
	this.FilePath = me.Location.pathname;
	this.Path = me.FilePath.substr(0, me.FilePath.lastIndexOf('/') + 1);
	this.FileName = me.FilePath.substr(me.FilePath.lastIndexOf('/') + 1);
	
	this.QueryString = function (kn) {
		var kstr = document.location.search.substr(1);
		if (kn==null) return kstr;
		if (kstr.length == 0) return '';
		var arr = kstr.split("&");
		if (typeof(kn)=='number') return decodeURIComponent(arr[kn].split('=')[1]);
		
		for(var i in arr) {
			var tItem = arr[i].split('=');
			if (tItem.length>2) {
				var temp = new Array();
				for(var j=1; j<tItem.length; j++) {
					temp[j-1] = tItem[j];
				}
				tItem[1] = temp.join('=');
			}
			if (tItem[0].toLowerCase().trim() == kn.toLowerCase().trim()) return decodeURIComponent(tItem[1]);
		}
		
		return '';
	}
	
	this.SetQuery = function (arr) {
		var kstr = document.location.search.substr(1);
		if (kstr.length == 0) {
			var tArr = new Array();
			for (var i in arr) {
				var temp = arr[i];
				if(temp[1]!=null) tArr.push(temp[0] + '=' + encodeURIComponent(temp[1]));
			}
			kstr = tArr.join('&');
		} else {
			var specIndex = kstr.indexOf('&');
			for (var i in arr) {
				var tArr = arr[i], nIndex = kstr.indexOf(tArr[0] + '=');
				
				if (nIndex < 0) {
					if (tArr[1]!=null) {
						if (kstr.length>0) kstr += '&';
						kstr += tArr[0] + '=' + encodeURIComponent(tArr[1]);
					}
				} else {
					var ta, tb
					if (nIndex > specIndex) {
						nIndex = kstr.indexOf('&' + tArr[0] + '=');
						ta = kstr.substring(0, nIndex + 1);
						tb = kstr.substr(nIndex + 1);
					} else {
						ta = kstr.substring(0, nIndex);
						tb = kstr.substr(nIndex);
					}
					var nsp = tb.indexOf('&');
					if (nsp<0) {
						tb = '';
					} else {
						tb = tb.substr(nsp);
					}
					if (tArr[1]==null) {
						kstr = ta.substr(0, ta.length-1) + tb;
					} else {
						kstr = ta + tArr[0] + '=' + encodeURIComponent(tArr[1]) + tb;
					}
				}
			}
		}
		
		return kstr;
	}
}

var ggSearchKind = -1;
var ggSearchIsOrder = true;

function ggSearch() {
	var SBox = null;
	this.Kind = ggSearchKind;
	this.bOrder = ggSearchIsOrder;
	var me = this;
	
	var objKey, objPKBox, objPKey1, objPKey2, objWhere, objOrder;
	
	this.Init = function() {
		if (me.Kind < 0) return;
		SBox = document.getElementById('SearchBox');
		if (!SBox) return;
		
		var r = new Request(), html = '<div>'
		
		switch(me.Kind) {
			case 0:
			case 1:
			case 2:
			case 3:
				//商品搜索
				var orderValue = r.QueryString('sort'), whereValue = r.QueryString('sw');
				var keyValue = r.QueryString('sk'), pk1Value = r.QueryString('sk1'), pk2Value = r.QueryString('sk2');
				if (pk1Value=='') pk1Value = '0';
				if (pk2Value=='') pk2Value = '0';
				var bBuied = (r.QueryString('bd')=='1')
				
				html += '<input type="text" id="tkey"' + (whereValue=='pp' ? ' style="display:none"' : '') + ' value="' + keyValue + '" />';
				html += '<span id="pkeyBox"' + (whereValue!='pp' ? ' style="display:none"' : '') + '><input type="text" id="pkey1" size="7" value="' + pk1Value + '" />&nbsp;-&nbsp;<input type="text" id="pkey2" size="7" value="' + pk2Value + '" /></span>';
				html += '&nbsp;<select id="swhere">';
				html += '<option value="pn">商品名称</option>';
				html += '<option value="bh"' + (whereValue=='bh' ? ' selected="selected"' : '') + '>编号</option>';
				if (me.Kind!=1) html += '<option value="zs"' + (whereValue=='zs' ? ' selected="selected"' : '') + '>种色</option>';
				html += '<option value="pp"' + (whereValue=='pp' ? ' selected="selected"' : '') + '>价格</option>';
				html += '</select>&nbsp;';
				html += '<button id="searchBT">搜索</button>&nbsp;';
				if (me.bOrder) {
					html += '<select id="order">';
					html += '<option value="default">默认排序</option>';
					html += '<option value="dt,down"' + (orderValue=='dt,down' ? ' selected="selected"' : '') + '>新' + (bBuied?'售':'品') + '在前</option>';
					html += '<option value="dt,up"' + (orderValue=='dt,up' ? ' selected="selected"' : '') + '>新' + (bBuied?'售':'品') + '在后</option>';
					if (!bBuied) {
						html += '<option value="price,down"' + (orderValue=='price,down' ? ' selected="selected"' : '') + '>价高到低</option>';
						html += '<option value="price,up"' + (orderValue=='price,up' ? ' selected="selected"' : '') + '>价低到高</option>';
					}
					html += '</select>&nbsp;';
				}
				html += '<button id="oldObject"' + (bBuied ? ' style="color:#18B239"' : '') +'>';
				if ( r.FilePath.indexOf('/prod/') > -1 )
					html += '历史商品';
				else if ( r.FilePath.indexOf('/fas/') > -1 )
					html += '历史作品';
				else if ( r.FilePath.indexOf('/bid/') > -1 )
					html += '历史拍品';
				else
					html += '历史商品';
				html += '</button>';
				
				SBox.innerHTML = html + '</div>';
				
				objKey = document.getElementById('tkey');
				objPKBox = document.getElementById('pkeyBox');
				objPKey1 = document.getElementById('pkey1');
				objPKey2 = document.getElementById('pkey2');
				objWhere = document.getElementById('swhere');
				
				try {
					objKey.onkeypress = objPKey1.onkeypress = objPKey2.onkeypress = function() {
						if (event.keyCode == 13) {
							me.doSearch();
							return false;
						}
					}
					objWhere.onchange = function() {
						var b = (this.value=='pp');
						objKey.style.display = b ? 'none' : '';
						objPKBox.style.display = b ? '' : 'none';
					}
					document.getElementById('searchBT').onclick = function() {
						me.doSearch();
					}
					document.getElementById('oldObject').onclick = function() {
						switch(me.Kind) {
							case 0:
							case 2:
								document.location = rootPath + '/prod/Index.aspx?bd=1';
								break;
							case 1:
								document.location = rootPath + '/bid/Index.aspx?bd=1';
								break;
							case 3:
								document.location = rootPath + '/fas/Index.aspx?bd=1';
								break;
							default:
								break;
						}
					}
					document.getElementById('order').onchange = function() {
						me.doOrder(this);
					}
				} catch(ex) {}
				break;
			case 4:
				//论坛搜索
				var whereValue = r.QueryString('w'), keyValue = r.QueryString('k');
				
				html += '<input type="text" id="tkey" value="' + keyValue + '" />';
				html += '&nbsp;<select id="swhere">';
				html += '<option value="8"' + (whereValue=='8' ? ' selected="selected"' : '') + '>标题</option>';
				html += '<option value="9"' + (whereValue=='9' ? ' selected="selected"' : '') + '>作者</option>';
				html += '<option value="10"' + (whereValue=='10' ? ' selected="selected"' : '') + '>内容</option>';
				html += '</select>&nbsp;';
				html += '<button id="searchBT">搜索</button>';
				
				SBox.innerHTML = html + '</div>';
				
				objKey = document.getElementById('tkey');
				objWhere = document.getElementById('swhere');
				
				try {
					objKey.onkeypress = function() {
						if (event.keyCode == 13) {
							me.doSearch();
							return false;
						}
					}
					document.getElementById('searchBT').onclick = function() {
						me.doSearch();
					}
				} catch(ex) {}
				break;
			default:
				break;
		}
	}
	
	//搜索
	this.doSearch = function() {
		var r = new Request(), arr = new Array();
		switch(me.Kind) {
			case 0:
			case 1:
			case 2:
			case 3:
				arr[0] = new Array('sw', objWhere.value);
				if (objWhere.value=='pp') {
					var k1 = objPKey1.value.replace(/\'/g, ''), k2 = objPKey2.value.replace(/\'/g, '')
					var v1 = k1, v2 = k2;
					if (isNaN(v1))
						v1 = 0;
					else
						v1 = parseFloat(v1);
					if(v1<0) v1 = 0;
					if (isNaN(v2))
						v2 = 0;
					else
						v2 = parseFloat(v2);
					if(v2<0) v2 = 0;
					if(v1==0){k1=k2;v1=v2;}
					if(v2==0){k2=k1;v2=v1;}
					
					arr[1] = new Array('sk1', (k1.length==0?null:k1));
					arr[2] = new Array('sk2', (k2.length==0?null:k2));
					arr[3] = new Array('sk', null);
					arr[4] = new Array('sort', (v1>v2?'price,down':(v1==v2?null:'price,up')))
				} else {
					var va = objKey.value.replace(/\'/g, '')
					arr[1] = new Array('sk1', null);
					arr[2] = new Array('sk2', null);
					arr[3] = new Array('sk', (va.length==0?null:va));
				}
				break;
			case 4:
				arr[0] = new Array('w', objWhere.value);
				var va = objKey.value.replace(/\'/g, '')
				arr[1] = new Array('k', (va.length==0?null:va));
				break;
			default:
				break;
		}
		var qs = r.SetQuery(arr);
		if (qs.length!=0) qs = '?' + qs;
		
		switch(me.Kind) {
			case 0:
			case 1:
				document.location = r.Path + 'Index.aspx' + qs;
				/*
				if(r.FileName.length==0)
					document.location = r.Path + 'Index.aspx' + qs;
				else if(r.FileName.substr(0, 2).toLowerCase()=='in')
					document.location = r.Path + 'Index.aspx' + qs;
				else if(r.FileName.substr(0, 1).toLowerCase()=='p')
					document.location = r.Path + 'Index.aspx' + qs;
				else if (r.FileName.toLowerCase()=='c20.aspx')
					document.location = r.Path + 'Index.aspx' + qs;
				else {
					var fn = r.FileName.replace(/c(\d+)(p(\d+)n(\w*)).aspx/, 'c$1.aspx');
					document.location = r.Path + fn + r.SetQuery(arr);
				}
				*/
				break;
			case 2:
				document.location = rootPath + '/prod/Index.aspx' + qs;
				break;
			case 3:
				document.location = rootPath + '/fas/Index.aspx' + qs;
				break;
			case 4:
				document.location = rootPath + '/bbs/Index.aspx' + qs;
				break;
			default:
				break;
		}
	}
	
	//排序
	this.doOrder = function(obj) {
		var r = new Request(), arr = new Array();
		arr[0] = new Array('sort', (obj.value=='default'?null:obj.value));
		var qs = r.SetQuery(arr);
		if (qs.length!=0) qs = '?' + qs;
		
		switch(me.Kind) {
			case 0:
			case 3:
				if(r.FileName.length==0)
					document.location = r.Path + 'Index.aspx' + qs;
				else if(r.FileName.substr(0, 2).toLowerCase()=='in')
					document.location = r.Path + 'Index.aspx' + qs;
				else {
					var fn = r.FileName.replace(/c(\d+)(p(\d+)n(\w*)).aspx/, 'c$1.aspx');
					document.location = r.Path + fn + qs;
				}
				break;
			case 1:
				if(r.FileName.length==0)
					document.location = r.Path + 'Index.aspx' + qs;
				else if(r.FileName.substr(0, 2).toLowerCase()=='in')
					document.location = r.Path + 'Index.aspx' + qs;
				else if (r.FileName.toLowerCase()=='c20.aspx')
					document.location = r.Path + 'Index.aspx' + qs;
				else {
					var fn = r.FileName.replace(/c(\d+)(p(\d+)n(\w*)).aspx/, 'c$1.aspx');
					document.location = r.Path + fn + '?' + r.SetQuery(arr);
				}
				break;
			case 2:
				document.location = rootPath + '/prod/Index.aspx' + qs;
				break;
			default:
				break;
		}
	}
}
