document.onLoad=checkBrowser();
function checkBrowser()
{
	var s=window.location.href.toString();
	var l;
	if(s.indexOf("?")==-1)
	{   
		l=s.length;
	}   
	else
	{   
		l=s.indexOf("?");
	}
	if (window.navigator.userAgent.indexOf("MSIE")<1)
	{
//		if(s.lastIndexOf("admin.php")>0)
//		{
//			alert("請使用 Internet Explorer 進入後臺管理！");
//			history.back();
//			return;
//		}
		document.location.href="ffox/"+s.substring(s.lastIndexOf("/")+1);
	}
}

//圆角表格内容
function getFrame(pic,info,title,sid,no)
{
	RGB=new Array("","#6dcff6"," #fff467","#39b44a","#bc8cbe","#f7941d");
	var str="<v:roundrect arcsize='10%' style='position:relative;top:1;left:1;width:80;height:80;'"+
	"strokeweight='2' strokecolor='#"+RGB[no]+"'><img src='"+pic+"' style='position:relative;left:1px;top:8px;'"+
	"width='69' height='69' border='0' style='cursor:hand;' onClick=\"location.href='detail.php?sid="+sid+"'\" info='"+info+"'></v:roundrect><br><a href='detail.php?sid="+sid+"'><div align='center' style='padding-top:3px;' class='txt12'>"+title+"</div></a><br>";
	return str;
}

function SetText(u)
{
	var xml=new ActiveXObject("Microsoft.XMLHTTP");
	xml.open("GET",u,false);
	xml.send();
	return xml.responseText;
	delete(xml);
}

function setCookie(name,value)
{
	var Days = 30;
	var exp  = new Date(); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) return unescape(arr[2]);
	else return null;
}
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

//更換圖片
var tmpobj=null;
function ChangeImage(obj)
{
	if(!obj.url)
	{
		//pic.src="./images/none.jpg";
		return;
	}
	if(tmpobj)
	{
		tmpobj.firstChild.style.color="#eeeeee";
	}
	tmpobj=obj;
	obj.firstChild.style.color="#000000";
	pic.src=obj.url;
	pic.info=obj.info;
}
//圖片預覽功能——開始
function showDiv(obj)
{
	if(!obj.src)return;
	preview.src=obj.src;
	previewinfo.innerHTML=obj.info;
	msg.style.display="";
	fulldiv.style.display="";
	fulldiv.style.width=document.body.clientWidth;
	fulldiv.style.height=document.body.offsetHeight+document.body.scrollTop;
	msg.style.left=(document.body.clientWidth)/2-preview.width/2;
	msg.style.top=(document.body.offsetHeight)/2-preview.height/2-50+document.body.scrollTop/2;
}

function closeDiv()
{
	msg.style.display="none";
	fulldiv.style.display="none";
}

var ms=0;
function did(obj)
{
	ms=obj;
	event.srcElement.setCapture();
	x=document.all(ms).style.pixelLeft-event.x;
	y=document.all(ms).style.pixelTop-event.y;
}

document.onmousemove=function()
{
	if(ms)
	{
		document.all(ms).style.pixelLeft=x+event.x;
		document.all(ms).style.pixelTop=y+event.y;
	}
}	

document.onmouseup=function()
{
	if(ms)
	{
		event.srcElement.releaseCapture();
		ms=0;
	}
}
//圖片預覽功能——結束

//縮略圖計算
function Thumbnail(obj,w,h)
{
	var aWidth=280;
	var aHeight=280;
	if(w)aWidth=w;
	if(h)aHeight=h;
	var image=new Image();
	image.src=obj.src;
	if(image.width>aWidth && image.width/aWidth>=image.height/aHeight)
	{  
		obj.width=aWidth;
		obj.height=(image.height*aWidth)/image.width;     
	}
	else if(image.height>aHeight && image.width/aWidth<image.height/aHeight)
	{
		obj.height=aHeight;
		obj.width=(image.width*aHeight)/image.height; 
	}
	else
	{
		obj.width=image.width;  
		obj.height=image.height;
	}
}

//列印
function printPage(sid)
{
    window.open("print.php?sid="+sid,"","width=800,height=600,top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, location=no, status=no");
}
//搜索
function goSearch(str)
{
	if(!str)
	{
		alert("請輸入要搜索的關鍵字");	
		Search.focus();
		return;
	}
	document.location.href="data.php?search="+encodeURI(str);
}

//獲取品種
function getData()
{
	var sid=form1.type.value;
	if(!sid)return;
	var str=SetText("ajax.php?cmd=getData&sid="+sid);
	if(str&&sid)
	{
		form1.types.innerHTML="";
		str=str.split("##");
		for(i=0;i<str.length;i++)
		{
			if(str[i])
			{
				str2=str[i].split("$$");
				var op= document.createElement("OPTION");
				op.text=str2[1];
				op.value=str2[0];
				form1.types.add(op);
			}
		}
	}
	else
	{
		form1.types.innerHTML="";
		var op= document.createElement("OPTION");
		op.text="　　　";
		form1.types.add(op);
	}
}
//檢測編碼
function CheckCode(sid)
{
	if(form1.code.value==0)return false
	var n=SetText("ajax.php?cmd=checkcode&code="+form1.code.value+"&sid="+sid);
	if(n=="ERROR")
	{
		alert("編號重復，請重新輸入編號！");
		form1.code.value="";
		form1.code.focus();
	}
}

//刪除圖片
function delpic(sid)
{
	if(!sid)return;
	if(confirm("是否刪除此圖片？"))
	{
		if(SetText("ajax.php?cmd=delpic&sid="+sid)=="OK")
		{
			location.href=location.href;
		}
	}
}

//刪除資料
function delinfo(sid)
{
	if(!sid)return;
	if(confirm("是否刪除此資料？\n此操作將刪除所有與此資料相關的圖片！"))
	{
		var rv=SetText("ajax.php?cmd=delinfo&sid="+sid);
		if(rv=="OK")
		{
			location.href='setting_modify.php';
		}
	}
}

//Auto Complete
var l=0;
function AutoComplete(obj)
{
	if(!obj.value)
	{
		list.style.display="none";
		return;
	}
	if(list.style.display=="none")
	{
		list.style.left=getAbsoluteLeft(obj);
		list.style.top=getAbsoluteTop(obj)+obj.offsetHeight;
		list.style.width=obj.offsetWidth;
		list.style.display="";
	}
	if(l>obj.value.length||obj.value.length==1)
	{
		getText(obj.value);
	}
	l=obj.value.length;
	CheckText(obj);
}

function getAbsoluteLeft( ob )
{
	if(!ob){return null;}
	var obj = ob;
	var objLeft = obj .offsetLeft;
	while( obj != null && obj .offsetParent != null && obj .offsetParent.tagName != "BODY" )
	{
		objLeft += obj .offsetParent.offsetLeft;
		obj = obj .offsetParent;
	}
	return objLeft;
}
  
function getAbsoluteTop( ob )
{
	if(!ob){return null;}
	var obj = ob;
	var objTop = obj.offsetTop;
	while( obj != null && obj .offsetParent != null && obj .offsetParent.tagName != "BODY" )
	{
		objTop += obj .offsetParent.offsetTop;
		obj = obj .offsetParent;
	}
	return objTop ;
}

function getText(str)
{
	var sltObj=document.getElementById("slt");
	var str=decodeURI(SetText("ajax.php?cmd=getText&str="+encodeURI(str)));
	if(str)
	{
		sltObj.innerHTML="";
		str=str.split(",");
		for(i=0;i<str.length;i++)
		{
			str2=str[i].split("$$");
			var op= document.createElement("OPTION");
			op.text=str2[1];
			op.value=str2[0];
			sltObj.add(op);
		}
	}
}

function CheckText(obj)
{
	if(!obj.value)return;
	var ObjA=document.getElementById("slt");
	var del="";
	for(i=0;i<ObjA.length;i++)
	{
		if(ObjA[i].text.substring(0,obj.value.length).toLowerCase()!=obj.value.toLowerCase())
		{
			del+=i+",";
		}
	}
	if(del)
	{
		del=del.substring(0,del.length-1).split(",");
		for(j=del.length-1;j>=0;j--)
		{
			ObjA.remove(del[j]);
		}
	}
}

function CopyText(obj)
{
	var txtObj=document.getElementById("txt");
	txtObj.sid=obj.options[obj.selectedIndex].value;
	txtObj.value=obj.options[obj.selectedIndex].text;
}

function openwin()
{
	window.showModalDialog("type.php?type="+form1.typeid.value,"","dialogWidth:450px;dialogHeight:650px;help:no;scroll:auto;status:no");
}
function createtype()
{
	if(!form1.typeid.value)return;
	var str=SetText("ajax.php?cmd=getType&id="+form1.typeid.value);
	if(str)
	{
		form1.littype.innerHTML="";
		str=str.split("##");
		for(i=0;i<str.length;i++)
		{
			if(str[i])
			{
				str2=str[i].split("$$");
				var op= document.createElement("OPTION");
				op.text=str2[1];
				op.value=str2[0];
				form1.littype.add(op);
			}
		}
	}
	else
	{
		form1.littype.innerHTML="";
	}
}