﻿

function GetObject(sId){
	return document.getElementById( sId );
} 

function TableSort( orderBy ) {
	GetObject("OrderBy").value = orderBy;
	Refresh();
}

// Dialog
function Dialog(url, dwidth, dheight) {	
	if(dwidth == null)
     	dwidth = 500;
   	if(dheight == null)
     	dheight = 250;
     		
	var ret = showModalDialog(url, "", "dialogWidth:" + dwidth + "pt;dialogHeight:" + dheight + "pt;help:0;status:0");
	return ret;
}   
 
// Delete Confirm
function DeleteConfirm( sURL ) {
	if(confirm("是否确认删除当前记录?", "确认")) {
		ajax( sURL, "", 
	        function( data ) {   
	          	if( data != null && data != "") {
	          		alert(data);
	          	} else {
    				Refresh();
    			}
            } 
	    );
   	}
} 
 

// Upload File（input: the input control,uploadType:Image Or Attach，rootPath: Relative path to the root of Web)
function Upload(input, uploadType, rootPath) {	
	if(rootPath == null) {
		rootPath = "..";
	}
	
	var ret = Dialog(rootPath + "/Pub/Upload.asp?UploadType=" + uploadType, 500, 180);
	if(ret == null || ret == "") {
		return;
	}
	
	// Return Value: Saved file Name | Upload Origin File Name
  	var tmpArray, fileName
  	
  	tmpArray = ret.split("|");
  	fileName = tmpArray[0];
  	
	input.value = fileName;
}

function UploadFinish( sRet, sId ) {		
		var arrRet = sRet.split(",");
	  	var sSaveFileName = arrRet[0];
	  	var sOriginFileName = arrRet[1];
	  	var sThumbFileName = arrRet[2];
	  
	  	var preview = GetObject(sId + "Preview");
	  	if( preview != null ) {
	  		preview.innerHTML = "<img src='/Upload/" + sSaveFileName + "' height='100'>";	  		
	  	}
	  	
	  	var InputCtl = GetObject(sId);
	  	InputCtl.value = sSaveFileName;
}

// Open New Window
function OpenWindow(url, width, height, scrollbars, x, y) {
   	if(width==null)	width=500;
   	if(height==null) height=250;
   	if(scrollbars==null||scrollbars=='') {
     		scrollbars='auto'; 
   	} else {
     		scrollbars='yes';    
    }
    
   	if(x==null||y==null) {
     		x=Math.ceil((window.screen.width-width)/2);
      		y=Math.ceil((window.screen.height-height)/3);
	}  
	
   	var win = window.open(url, "", "width="+width+",height="+height+",toolbar=no,statusbar=no,menubar=no,scrollbars="+scrollbars+",resizable=yes,location=no,status=no");
   	win.moveTo(x,y);
   	win.focus();
}   

// Get Position Of Div
function GetDivPos(obj, flag) {
 	if( flag == "Width" ) {
 		return obj.offsetWidth;
 	} else if( flag == "Height" ) {
 		return obj.offsetHeight;
 	}
 		
 	var val = 0;
 	while( obj ) {
 		if( flag == "Left" ) {
 			val += obj.offsetLeft;
 		} else if( flag == "Top" ) {
 			val += obj.offsetTop;
 		} 
 		
 		obj = obj.offsetParent;
 	}
 	
 	return val;
}

function Zoom(size, lineHeight) {	
	var obj = document.getElementById("NewsContent"); 

	obj.style.fontSize = size + 'pt';
	obj.style.lineHeight = lineHeight + 'px'; 
}


function ChangeClass(obj, cssName) {
	obj.className = cssName;
}

// Ajax Call( callback: function name，FadeBox will be used if callback is null）
function ajax(url, param, callback) {		
	var HttpRequest;
	if(window.ActiveXObject) { 
		HttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	} else if(window.XMLHttpRequest) { 
		HttpRequest = new XMLHttpRequest(); 
	} 
		
	HttpRequest.onreadystatechange = function() { 		        
		if(HttpRequest.readyState == 4 ) { 
		   	if( HttpRequest.status == 200 ) { 
	           	if( callback != null ) {
	           		callback( HttpRequest.responseText ); 
				} else {
					if ( HttpRequest.responseText != "" ) {
						FadeBox(HttpRequest.responseText);
					}
				}
	        } else {
	        	alert( "Ajax Status = " + HttpRequest.status + "\n" + HttpRequest.responseText);
	        	CloseFloatBox();
	        }
	    } 
	};
	
	HttpRequest.open( "POST", url, true );
	HttpRequest.setRequestHeader("If-Modified-Since", "0");
	HttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	HttpRequest.send(param); 
}  

// Part Refresh
function PartRefresh( divId, url ) {
	var div = GetObject(divId);
	//ShowWaiting(true);
	
	ajax( url, "", function(html) {
		//ShowWaiting(false);
		var div = GetObject(divId);
		if( div != null ) {
			div.innerHTML = html;
		} else {
			alert("Div " + divId + " does not exist!");
		}
	});
}


function AjaxSubmit( theForm ) {
    return AjaxSubmitEx( theForm, null );
}

function AjaxSubmitEx( theForm, callback ) {
	var parameters = "";
	for ( var i = 0; i < theForm.length; i ++ ) {
		if( parameters != "" ) {
			parameters += "&";
		}
		if( theForm[i].type != "checkbox") {
			parameters += theForm[i].name + "=" + encodeURI(theForm[i].value);
		} else {
			if( theForm[i].checked ) {
				parameters += theForm[i].name + "=" + encodeURI(theForm[i].value);
			}
		}
	}
	
	if(window.ActiveXObject) { 
		var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	} else if(window.XMLHttpRequest) { 
		var HttpRequest = new XMLHttpRequest(); 
	} 
		
	HttpRequest.onreadystatechange = function() { 		        
			if(HttpRequest.readyState == 4 ) { 
		   		if( HttpRequest.status == 200 ) { 		   			           	
	           	    var responseText = Trim(HttpRequest.responseText);
	           	    if( callback != null ) {
	           			CloseFloatBox();
	           			callback( responseText ); 
				    } else {
						if ( responseText != "" ) {
							FadeBox( responseText );
						} else {
							CloseFloatBox();
							RefreshPage();
						}
					}
	        } else {
	        	alert( "Ajax Status = " + HttpRequest.status + "\n" + HttpRequest.responseText);
	        	CloseFloatBox();
	        }
	    } 
	};
	
	HttpRequest.open( "POST", theForm.action, true );      
	HttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	HttpRequest.setRequestHeader("Content-length", parameters.length);
	HttpRequest.setRequestHeader("Connection", "close");
	HttpRequest.send( parameters );
	return false;
}

function FormPartRefresh( formId, divId, sURL ) {
	var parameters = "";
	var theForm = GetObject(formId);
	if( theForm == null ) {
	    return;
	}
	
	for ( var i = 0; i < theForm.length; i ++ ) {
		if( parameters != "" ) {
			parameters += "&";
		}
		
		if( theForm[i].type != "checkbox") {
			parameters += theForm[i].name + "=" + escape(theForm[i].value);
		} else {
			if( theForm[i].checked ) {
				parameters += theForm[i].name + "=" + escape(theForm[i].value);
			}
		}
	}
	
	if(window.ActiveXObject) { 
		var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	} else if(window.XMLHttpRequest) { 
		var HttpRequest = new XMLHttpRequest(); 
	} 
		
	HttpRequest.onreadystatechange = function() { 
			if(HttpRequest.readyState == 4 ) { 
		   		if( HttpRequest.status == 200 ) { 
	           		GetObject(divId).innerHTML = HttpRequest.responseText;
	        } else {
	        	alert( "Ajax Status = " + HttpRequest.status + "\n" + HttpRequest.responseText);
	        	CloseFloatBox();
	        }
	    } 
	};
	
	HttpRequest.open( "POST", sURL, true );      
	HttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	HttpRequest.setRequestHeader("Content-length", parameters.length);
	HttpRequest.setRequestHeader("Connection", "close");
	HttpRequest.send( parameters );
	return false;
}
function ShowWaiting( divParentId, bShow ) {
	var div = GetObject("Waiting");
	if( div == null ) {
		div = CreateDiv("Waiting", "<img src=\"/img/Waiting.gif\">");
	}
	
	var divParent = GetObject(divParentId);
	if( divParent != null ) {
		div.offsetLeft = divParent.offsetLeft;
		div.offsetTop = divParent.offsetTop;
	}
		
	if(bShow) {
		div.style.display = "";
	} else {
		div.style.display = "none";
	}
}

function CreateDiv(divId, sInnerHTML) {
	var div = GetObject(divId);
	if( div == null ) {
		div = document.createElement("div");
		div.setAttribute("id", divId);
		document.body.appendChild(div);
	}
		
	div.style.cssText = "position:absolute;left:0px;top:0px;";
	div.innerHTML = sInnerHTML;
	return div;
}

// RefreshPage if the Refresh Function Exists
function RefreshPage() {
	if( typeof(Refresh) == "function") {
		Refresh();
	}
}

//FadeBox
function FadeBox( sMessage ) {
	
	var div;
	div = GetObject("Fade");
	if ( div == null ) {
		div = document.createElement("div");
		div.setAttribute("id", "FadeBox");
		div.className = "FadeBox";
		div.style.cssText = "position:absolute;border:1px solid #FF0000;color:#000000;background:#F7F7F7;padding:20px;z-index:999;";
		document.body.appendChild(div);
		div.innerHTML = sMessage;
	} else {
		div.innerHTML = sMessage;
		div.style.display = "";
	}
	
	// 一定要在append之后才能取到div.offsetWidth
	div.style.left = (document.documentElement.clientWidth - div.offsetWidth) / 2 + "px";
	div.style.top = (document.documentElement.scrollTop + 300 ) + "px";		
	
	opacity = 1;	
	var timer = setInterval(
		function() {
			opacity -= 0.01; 
			
			if( document.all ) { // IE
				div.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
			} else {
				div.style.opacity = opacity; // FF
			}
			
			if(opacity <= 0 ) {clearInterval( timer ); div.style.display="none";}
	},1);
}


// FloatBox
function FloatBox(url) {
	var divFloatBox = GetObject("FloatBox");
	var divFloatBoxMasker = GetObject("FloatBoxMasker");
	var divFloatBoxClose = GetObject("FloatBoxClose");
	  
	// Create Div
	if ( divFloatBox == null ) {
		divFloatBox = document.createElement("DIV");
		divFloatBox.id = "FloatBox";
		divFloatBox.style.cssText = "position:absolute;border:#999999 solid 1px;background:#FFFFFF;z-index:22;top:150px;";				
		document.body.appendChild(divFloatBox);
		
		divFloatBoxMasker = document.createElement("DIV");
		divFloatBoxMasker.id = "FloatBoxMasker";
		divFloatBoxMasker.style.cssText = "position:absolute;background:#DDDDDD;z-index:11;left:" + document.documentElement.scrollLeft + "px;top:" + document.documentElement.scrollTop + "px;width:" + document.documentElement.scrollWidth + "px;height:" + (document.documentElement.clientHeight) + "px;filter:alpha(opacity=60);opacity:0.6;";
	 	document.body.appendChild(divFloatBoxMasker);
		  
	}	
		
	divFloatBox.style.width = 200 + "px";
	divFloatBox.style.height = 50 + "px";
	divFloatBox.style.left = (document.body.clientWidth - 200)/2 + "px";
	divFloatBox.style.top = document.documentElement.scrollTop + 80;
   	
	divFloatBox.innerHTML = "Please wait a moment...";
	
	divFloatBox.style.display = "block";
   	divFloatBoxMasker.style.display = "block";
	
	sCloseButtonHtml = "<img src='/Img/Close.gif' alt='' style='position:absolute;z-index:33;top:7px;right:10px;cursor:pointer;' onclick='CloseFloatBox();'>";
	
	ajax( url, "",
		function(data) {
			// 让窗口自动扩展
			divFloatBox.style.left = "0px";
			divFloatBox.style.width = ""; 
			divFloatBox.style.height = "";
			
			GetObject("FloatBox").innerHTML = data + sCloseButtonHtml;
						
			iFloatBoxWidth = GetDivPos( divFloatBox, "Width");
			iFloatBoxHeight = GetDivPos( divFloatBox, "Height");
			
		    divFloatBox.style.left = (document.body.clientWidth - iFloatBoxWidth)/2 + "px";
		    divFloatBox.style.top = document.documentElement.scrollTop + 80;
		    
		    if(GetObject("FloatBoxHeader") != null) { // Drag
		        GetObject("FloatBoxHeader").onmousedown = function(e) {BeginDrag(GetObject("FloatBox"), e);};
		    }
		}
	);   
}

// CloseFloatBox
function CloseFloatBox() {
	var divFloatBox = GetObject("FloatBox");
	var divFloatBoxMasker = GetObject("FloatBoxMasker");
	
	if( divFloatBox == null ) {
		return;
	}
	
	divFloatBox.style.display = "none";
   	divFloatBoxMasker.style.display = "none";
}	

function ReloadWindow( text ) {
	window.location.reload(); 
}

// ShowDiv
function ShowDiv(divId) {	
	var div = document.getElementById(divId);
 	if(div == null)
 		return;
 	div.style.display = "";
}

// HideDiv
function HideDiv(divId) {	
	var div = GetObject(divId);
 	if(div == null)
 		return;
 	div.style.display = "none";
}

// Switch Show and Hide
function ShowHideDiv( divId ) {
	var div = GetObject(divId);
 	if(div == null)
 		return;
 	if( div.style.display == "") {
 		div.style.display = "none";
 	} else {
 		div.style.display = "";
 	}
}

function TreeOpenClose(branchNode, branchId) {
	var branch = GetObject(branchId);
	if( branch.style.display == "") {
		branch.style.display = "none";
		branchNode.className = "TreeClose";
	} else {
		branch.style.display = "";
		branchNode.className = "TreeOpen";
	}
}
			
// Show DropDownMenu in specific position
function DropDownMenu(parentDiv, subMenuId) {	
	var subMenuDiv = GetObject(subMenuId);
 	if(subMenuDiv == null || subMenuDiv.style.display == "") {
 		return;
 	}
 	
 	subMenuDiv.style.display = "";
	subMenuDiv.style.left = GetDivPos(parentDiv, "Left") + "px";
	subMenuDiv.style.top = GetDivPos(parentDiv, "Top") + GetDivPos(parentDiv, "Height") + "px";
	
	var iParentWidth = GetDivPos(parentDiv, "Width");
	if( GetDivPos(subMenuDiv, "Width") < iParentWidth ) {
		subMenuDiv.style.width = iParentWidth + "px";	
	}
	
	//var iHeight = GetDivPos(subMenuDiv, "Height");
	//subMenuDiv.style.height = "0px";	
	//setTimeout("DropDownMenuAnimate('" + subMenuId + "', " + iHeight + ")", 1);
}

// 动画
function DropDownMenuAnimate(subMenuId, iHeight) {
	var subMenuDiv = GetObject(subMenuId);
	var iOldHeight = GetDivPos(subMenuDiv, "Height");
	if( iOldHeight < iHeight ) {
		if( (iOldHeight + iHeight/10) < iHeight ) {
			subMenuDiv.style.height = (iOldHeight + iHeight/10) + "px";	
		}else {
			subMenuDiv.style.height = iHeight + "px";	
		}
		setTimeout("DropDownMenuAnimate('" + subMenuId + "', " + iHeight + ")", 1);
	}
}


function CheckNeedInput( control, fieldName ) {
	if( control.value == "") {
		alert( fieldName + "不能为空!" );
		control.focus();
		return false;
	}
	
	return true;
}


function PubToCartResponse(data) {
	if( data == "" ) return;
	
	if( data == "您还未登录或登录已超时，请重新登录!" ) {
		alert(data);
		window.location = "Login.asp";
	} else {
		FadeBox (data);
	}
}

function ShowTab(sTabIds) {  // ID用分号分隔，第一个是当前选中的ID;标题的ID是版块ID号后加Title		
		var arr = sTabIds.split(";");
			
		if ( arr.length < 1) {
			return;
		}
		
		for( i = 1; i < arr.length; i ++) {
			document.getElementById(arr[i] + "Title").className = "TabTitle";
			document.getElementById(arr[i]).className = "Tab";
		}
		
		document.getElementById(arr[0]+ "Title").className = "ActiveTabTitle";
		document.getElementById(arr[0]).className = "ActiveTab";
}


function Trim( str ) {
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

// Set Div to Center
function CenterDiv( divId ) {
	var div = GetObject( divId );
	if( div == null ) {
		return;
	}
	
	div.style.left = (document.body.clientWidth - div.offsetWidth)/2 + "px";
	div.style.top = document.documentElement.scrollTop + 150;
}

function TurnToPage(pageId) {
	if( pageId > 0 ) {
		GetObject("Page").value = pageId;
	}
	
	Refresh();
}

var GetEvent = new Function('e', 'if (!e) e = window.event;return e')

// <div onmouseover='BeginTrag(this);'>
function BeginDrag( obj, e ) {
	bDragFlag = true;
	obj.style.position = "absolute";
	x = GetEvent(e).clientX;
	y = GetEvent(e).clientY;
	document.onmousemove = function(e){
		if(!bDragFlag) {
			return false;
		}
		
		obj.style.left = obj.offsetLeft + GetEvent(e).clientX - x + "px";
		obj.style.top = obj.offsetTop + GetEvent(e).clientY - y + "px";
			
		x = GetEvent(e).clientX;
		y = GetEvent(e).clientY;			
	}
	
	document.onmouseup = function(e){bDragFlag=false;document.onmousemove = null;document.onmouseup =null;};
}


// Change URL Param to a HashTable, Param Example: ItemCount=2&Name1=1&Price1=2&Name2=3&Price2=5
function ParseURLParam( sParam ) {
	var aValues = new Array();
	
	var arr = sParam.split("&");
	for( var i = 0; i < arr.length; i ++ ) {
		var arrItem = arr[i].split("=");
		if( arrItem[0] != "" ) {
			aValues[arrItem[0]] = "";
			if(arrItem.length > 1) {
				aValues[arrItem[0]] = arrItem[1];	
			}
		}
	}
	
	return aValues;
}

// Check a value is defined
function IsUndefined( val ) {
	if( v == "undefined" ) {
		return true;
	}else {
		return false;
	}
}

// Check Number Value
function IsNumber(oNum) {
  	if(!oNum) return false;
  	var strP=/^\d+(\.\d+)?$/;
  	if(!strP.test(oNum)) return false;
  	try{
  		if(parseFloat(oNum)!=oNum) return false;
  	} catch(ex) {
   		return false;
  	}
  	
	return true;
}

function ToNumber( val ) {
	return parseFloat(val);
}

function CloseWindow() {
	if( window.opener != null && window.opener != undefined ) { 
		window.opener.location.reload();
		window.close();
	}
}

function AddToFav(sTitle) { 
    var sURL = "http://" + window.location.host;
    sURL = window.location.href;
    
    try { 
        window.external.addFavorite(sURL, sTitle); 
    } catch (e) { 
        try { 
            window.sidebar.addPanel(sTitle, sURL, ""); 
        } catch (e) { 
            alert("加入收藏失败,请手动添加."); 
        } 
    } 
} 

function SetHomePage() { 
    var sPageURL = window.location.href;
    try { 
    	if (document.all) { 
	        document.body.style.behavior='url(#default#homepage)'; 
	        document.body.setHomePage(sPageURL); 
	    } 
	    else if (window.sidebar) { 
	        if(window.netscape) { 
	            try { 
	                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
	            } 
	            catch (e) { 
	                alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项signed.applets.codebase_principal_support 值该为true" ); 
	            } 
	        } 
	        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch); 
	        prefs.setCharPref('browser.startup.homepage',sPageURL); 
	    }  
    } catch (e) { 
    	alert("加入收藏失败,请手动添加." + e); 
    } 
} 

