// 获得层对象
function GetDiv(id){
	return document.getElementById(id);
} 

// 弹出对话框
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;
}   
 
//删除确认
function DeleteConfirm( sURL ) {
	if(confirm("是否删除当前记录", "确认")) {
		ajax( sURL ,
	        function( data ) {   
	          	if( data != null && data != "") {
	          		alert(data);
	          	} else {
    				window.location.reload(); 
    			}
            } 
	    );
   	}
} 
 
// 上传（input为输入框控件,uploadType为Image或Attach，rootPath为当前页面与网站根目录的相对路径)
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;
	}
	
	// 返回值:保存文件名|原始文件名
  	var tmpArray, fileName
  	
  	tmpArray = ret.split("|");
  	fileName = tmpArray[0];
  	
	input.value = fileName;
}


//弹出窗口
function OpenWindow(url, width, height, scrollbars, x, y) {
	var win,x,y;
   	//
   	if(win!=null)
     		win.close();
   	if(width==null)
     		width=500;
   	if(height==null)
     		height=250;
   	if(scrollbars==null||scrollbars=='')
     		scrollbars='no'; 
   	else
     		scrollbars='yes';    
   	//
   	if(x==null||y==null)
     	{
     		x=Math.ceil((window.screen.width-width)/2);
      		y=Math.ceil((window.screen.height-height)/2);
     	}  
   //
   	win=window.open(url,"","width="+width+",height="+height+",toolbar=no,statusbar=no,menubar=no,scrollbars="+scrollbars+",resizable=no,location=no,status=no");
   	win.moveTo(x,y);
   	win.focus();
}   

// 获取某个对象模的位置
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 doZoom(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调用( callback是回调函数，为null时自动用淡出窗口提示结果）
function ajax(url, callback) {		
	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 ) { 
	           	if( callback != null ) {
	           		callback( HttpRequest.responseText ); 
				} else {
					if ( HttpRequest.responseText != "" ) {
						FadeBox(HttpRequest.responseText);
					}
				}
	        } else {
	        	FadeBox( "Ajax Error! Status = " + HttpRequest.status );
	        }
	    } 
	};
	
	HttpRequest.open( "POST", url, true );
	HttpRequest.setRequestHeader("If-Modified-Since", "0");
	HttpRequest.send(null); 
}  

// Part Refresh
function PartRefresh( divId, url ) {
	var div = GetDiv(divId);
	//ShowWaiting(true);
	
	ajax( url, function(html) {
		//ShowWaiting(false);
		var div = GetDiv(divId);
		if( div != null ) {
			div.innerHTML = html;
		} else {
			alert("Div " + divId + " does not exist!");
		}
	});
}

function AjaxSubmit( theForm, callback ) {
	var parameters = "";
	for ( var i = 0; i < theForm.length; i ++ ) {
		if( parameters != "" ) {
			parameters += "&";
		}
		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();
					}
				}
	        } else {
	        	FadeBox( "Ajax Error! Status = " + HttpRequest.status );
	        }
	    } 
	};
	
	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 ShowWaiting( divParentId, bShow ) {
	var div = GetDiv("Waiting");
	if( div == null ) {
		div = CreateDiv("Waiting", "<img src=\"/img/Waiting.gif\">");
	}
	
	var divParent = GetDiv(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 = GetDiv(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;
}

//淡入淡出提示窗口
function FadeBox( sMessage ) {
	var div;
	div = GetDiv("Fade");
	if ( div == null ) {
		div = document.createElement("div");
		div.setAttribute("id", "FadeBox");
		div.className = "FadeBox";
		div.style.cssText = "position:absolute;border:1px solid #DDDDDD;color:#FFFFFF;background:#FF0000;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);
}


// 浮动窗口
function FloatBox(url) {
	var divFloatBox = GetDiv("FloatBox");
	var divFloatBoxMasker = GetDiv("FloatBoxMasker");
	var divFloatBoxClose = GetDiv("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:0px;top:0px;width:" + document.documentElement.scrollWidth + "px;height:" + (document.documentElement.scrollHeight) + "px;filter:alpha(opacity=60);opacity:0.6;";
	 	document.body.appendChild(divFloatBoxMasker);
		  
		divFloatBoxClose = document.createElement("DIV");
		divFloatBoxClose.id = "FloatBoxClose";
		divFloatBoxClose.style.cssText = "position:absolute;z-index:33;top:150px;";
		divFloatBoxClose.innerHTML = "<img src=\"../Img/Close.gif\" style=\"cursor:hand;\" onclick=\"javascript:CloseFloatBox();\">";
		document.body.appendChild(divFloatBoxClose);
	}	
		
    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 + 150;
   	
	divFloatBoxClose.style.left = divFloatBox.offsetLeft + divFloatBox.offsetWidth + "px";
	divFloatBoxClose.style.top = document.documentElement.scrollTop + 158;
	
	divFloatBox.innerHTML = "Please wait a moment...";
	
	divFloatBox.style.display = "block";
   	divFloatBoxMasker.style.display = "block";
   	divFloatBoxClose.style.display = "block";
	
	ajax( url, 
		function(data) {
			// 让窗口自动扩展
			divFloatBox.style.left = "0px";
			divFloatBox.style.width = ""; 
			divFloatBox.style.height = "";
			
			GetDiv("FloatBox").innerHTML = data;
						
			iFloatBoxWidth = GetDivPos( divFloatBox, "Width");
			iFloatBoxHeight = GetDivPos( divFloatBox, "Height");
			
		    divFloatBox.style.left = (document.body.clientWidth - iFloatBoxWidth)/2 + "px";
		    divFloatBox.style.top = document.documentElement.scrollTop + 150;
			divFloatBoxClose.style.left = divFloatBox.offsetLeft + divFloatBox.offsetWidth - 22 + "px";
			divFloatBoxClose.style.top = document.documentElement.scrollTop + 158;
		}
	);   
}

// 关闭浮动窗口
function CloseFloatBox() {
	var divFloatBox = GetDiv("FloatBox");
	var divFloatBoxMasker = GetDiv("FloatBoxMasker");
	var divFloatBoxClose = GetDiv("FloatBoxClose");
	
	if( divFloatBox == null ) {
		return;
	}
	
	divFloatBox.style.display = "none";
   	divFloatBoxMasker.style.display = "none";
   	divFloatBoxClose.style.display = "none";	
}	

function ReloadWindow( text ) {
	if( text != null && text != "" ) {
		alert(text);
	}
	
	window.location.reload(); 
}

// 显示层
function ShowDiv(divId) {	
	var div = document.getElementById(divId);
 	if(div == null)
 		return;
 	div.style.display = "";
}

// 隐藏层
function HideDiv(divId) {	
	var div = GetDiv(divId);
 	if(div == null)
 		return;
 	div.style.display = "none";
}

// Switch Show and Hide
function ShowHideDiv( divId ) {
	var div = GetDiv(divId);
 	if(div == null)
 		return;
 	if( div.style.display == "") {
 		div.style.display = "none";
 	} else {
 		div.style.display = "";
 	}
}

// 显示子菜单在指定位置
function DropDownMenu(parentDiv, subMenuId) {	
	var div = GetDiv(subMenuId);
 	if(div == null || div.style.display == "") {
 		return;
 	}
 	
 	div.style.display = "";
	div.style.left = GetDivPos(parentDiv, "Left") + "px";
	div.style.top = GetDivPos(parentDiv, "Top") + GetDivPos(parentDiv, "Height") + "px";
}

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 {
		fade (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 = GetDiv( divId );
	if( div == null ) {
		return;
	}
	
	div.style.left = (document.body.clientWidth - div.offsetWidth)/2 + "px";
	div.style.top = document.documentElement.scrollTop + 150;
}
