/***********************************************
* common.js
* 自建通用 javascript
* 2007/11/6
***********************************************/


// --------------------------------------------
//  物件簡化格式
function $(v)  		{ return(document.getElementById(v)); }
function $S(v) 		{ return(document.getElementById(v).style); }
function $N(v)		{ return(document.getElementsByName(v)); }
function agent(v) {	return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0));	}
function isset(v)	{	return((typeof(v)=='undefined' || v.length==0)?false:true);	}
function XYwin(v)	{	var z=agent('msie')?Array(document.body.clientHeight,document.body.clientWidth):Array(window.innerHeight,window.innerWidth); return(isset(v)?z[v]:z);	}
function getElementHeight(v)	{	return	v.offsetHeight;	}	// 取得物件高度
function getElementWidth(v)		{	return 	v.offsetWidth;	}		// 取得物件寬度

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// --------------------------------------------
// 求物件在 document 的 Left 位置
function js_GetObjLeft(v)	{
  var selectedPosX = 0;
  var theElement = $(v);

  while(theElement != null)	{
    selectedPosX += theElement.offsetLeft;
    theElement = theElement.offsetParent;
  	}
return selectedPosX;
}

// --------------------------------------------
// 求物件在 document 的 Top 位置
function js_GetObjTop(v)	{
  var selectedPosY = 0;
  var theElement = $(v);

  while(theElement != null)	{
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  	}
return	selectedPosY;
}

// --------------------------------------------
// 查出物件下各屬性
function	listMember(v) {
var s = "";

for( key in v ) // 使用 in 運算子列舉所有成員
  s += key + ": " + v[key] + " --- ";
return s;
}


// --------------------------------------------
// 一般Sub Window開啟
function	newWindow(link,w,h)  {

var left=(screen.width)?(screen.width-w)/2:100;
var top=(screen.height)?(screen.height-h)/2:100;
var wtype="location=no,toolbar=no,menubar=no,status=no,resizeable=no,scrollbars=yes,titlebar=no,left="+left+",top="+top+",width="+w+",height="+h;
wid = window.open(link,"preview",wtype);
return wid;
}


// --------------------------------------------
// 判別 Browser 版本
var isCSS, isW3C, isIE4, isNN4;
function	initDHTMLAPI() {

if (document.images) {
  isCSS = (document.body && document.body.style) ? true : false;
  isW3C = (isCSS && document.getElementById) ? true : false;
  isIE4 = (isCSS && document.all) ? true : false;
  isNN4 = (document.layers) ? true : false;
  isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
  }
}


// --------------------------------------------
// 查圖片的寬
function js_GetImgWidth(v)	{
var istate;

OriginImage.onload = new function()	{};
OriginImage.src = v;
if(OriginImage.width==0)	{
	OriginImage.onload = new function() { alert('取得圖形長寬...'); };
	}
else	return   OriginImage.width;
}


// --------------------------------------------
// 查圖片的高
function	js_GetImgHeight(v)	{

OriginImage.onload = new function()	{};
OriginImage.src = v;
if(OriginImage.height==0)		{
	OriginImage.onload = new function() { alert('取得圖形長寬...'); };
	}
else	return   OriginImage.height;
}


// --------------------------------------------
// 從 childNodes 中找出要的子物件 (因為IE/FF 子物件位置不同)
function	js_FindNodes(v, finditem)		{
var key, tempobj;

for(key in v.childNodes)		{
	tempobj = v.childNodes[key].id;
	if(tempobj && tempobj.charAt(0)==finditem)	return tempobj;
	}

return	0;
}


// --------------------------------------------
// 跳出登入視窗
function	js_loginlayer(lname)		{

var	boxwidth = 320;
var boxheight = 180;
var boxleft=(screen.width)?(screen.width-boxwidth)/2:100;
var boxtop=(screen.height)?(screen.height-boxheight)/2:100;
var layerwidth = (document.body.scrollWidth)?(document.body.scrollWidth):document.body.clientHeight;
var layerheight = document.body.scrollHeight;

//FF專用濾鏡------------------------------------------
var layerstyle = 'position:fixed; left:0; top:0; width:100%; height:100%; background:black; '
	+ 'filter:Alpha(Opacity=90); -moz-opacity:0.9; opacity:0.9; border:0px solid black; z-index:100;';

var loginHTML = '<DIV ID="login" STYLE="position:absolute; left:'+boxleft+'px; top:100px; width:'
	+ boxwidth + 'px; height:' + boxheight + 'px; ' + 'background:url(../site/images/box_login.gif); border:0px outset black; '
	+ 'filter:alpha(opacity=100); -moz-opacity:1.0; opacity:1.0; z-index:101;">'
	+ '<BR><P><IMG SRC="../site/images/box_login1.gif" STYLE=""></P>'
	+ '<FORM ID="loginform" NAME="loginform" action="../site/login.php" method="post">'
	+ '<INPUT TYPE="hidden" NAME="loginCMD" ID="loginCMD">'
	+ '帳號 <INPUT TYPE="TEXT" SIZE="24" ID="iid" NAME="iid" value="'+lname+'"><br>'
	+ '<FONT COLOR="RED">帳號範例 (Johnny@gmail.com)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></FONT>'
	+ '密碼 <INPUT TYPE="password" SIZE="24" ID="ipass" NAME="ipass"><br><br>'
	+ '<INPUT TYPE="hidden" NAME="screenw" VALUE="'+screen.width+'">'
	+ '<INPUT TYPE="hidden" NAME="screenh" VALUE="'+screen.height+'">'
	+ '<INPUT TYPE="button" onClick="js_loginclick(1);" VALUE="登入">'
	+ '<INPUT TYPE="button" onClick="js_loginclick(2);" VALUE="忘記密碼">'
	+ '<INPUT TYPE="button" onClick="js_loginclick(4);" VALUE="加入會員">'
	+ '<INPUT TYPE="button" onClick="js_loginclick(3);" VALUE="取消">'
	+ '</FORM>'
	+ '</DIV>';

var	loginobj = document.createElement('div');
loginobj.id = 'loginobj';

//-----------IE專用濾鏡------------------------
loginobj.style.position ='fixed';
loginobj.style.left='0px';
loginobj.style.top='0px';
loginobj.style.width='100%';
loginobj.style.height='100%';
loginobj.style.zIndex = 100;
loginobj.style.filter = 'Alpha(Opacity=90)';
loginobj.style.background='black';

loginobj.setAttribute('id','loginobj');

loginobj.className = '';
loginobj.setAttribute('class','');
loginobj.setAttribute('style', layerstyle);
loginobj.innerHTML = loginHTML;

document.body.appendChild( loginobj);
}


function	js_mailto()		{

var	boxwidth = 320;
var boxheight = 180;
var boxleft=(screen.width)?(screen.width-boxwidth)/2:100;
var boxtop=(screen.height)?(screen.height-boxheight)/2:100;
var layerwidth = (document.body.scrollWidth)?(document.body.scrollWidth):document.body.clientHeight;
var layerheight = document.body.scrollHeight;

var layerstyle = 'position:absolute; left:0; top:0; width:100%; height:120%; background:black; '
	+ 'filter:Alpha(Opacity=90); -moz-opacity:0.9; opacity:0.9; border:0px solid black; z-index:100;';

var loginHTML = '<DIV ID="mailto" STYLE="position:absolute; left:'+boxleft+'px; top:100px; width:'
	+ boxwidth + 'px; height:' + boxheight + 'px; ' + 'background:url(../site/images/box_login.gif); border:0px outset black; '
	+ 'filter:alpha(opacity=100); -moz-opacity:1.0; opacity:1.0; z-index:101;">'
	+ '<BR><P><IMG SRC="../site/images/share.gif" STYLE=""></P>'
	+ '<FORM ID="mailform" NAME="mailform" method="post">'
	+ '<Table border="0" cellspacing="0" cellpadding="0" width="300" align="center">'
	+ '<tr><td width="75" align="right" height="30">您的大名：</td><td width="225" align="left"><INPUT id="m_name" TYPE="TEXT" SIZE="24" NAME="m_name"></td></tr>'
	+ '<tr><td width="75" align="right" height="30">親友 E-mail：</td><td width="225" align="left"><INPUT TYPE="TEXT" SIZE="24" NAME="mailaddr"></td></tr>'
  + '<tr><td width="300" align="left" height="30" colspan="2"><font color="red">&nbsp;填寫範例：</font>John@gmail.com<font color="red"> ; </font>Mary@gmail.com <font color="red">(分號區隔)</font></td></tr>'
	+ '</table>'
	+ '<INPUT TYPE="button" onClick="CheckEMail(mailaddr);" VALUE="寄送" >'
	+ '<INPUT TYPE="button" onClick="cancel();" VALUE="取消">'
	+ '</FORM>'
	+ '</DIV>';

var	mailobj = document.createElement('div');
mailobj.id = 'mailobj';
mailobj.setAttribute('id','mailobj');

mailobj.className = '';
mailobj.setAttribute('class','');
mailobj.setAttribute('style', layerstyle);
mailobj.innerHTML = loginHTML;

document.body.appendChild( mailobj);
}

function CheckEMail (type)
{
	switch (type) {
		case 1:
			name=document.getElementById('m_name').value;
			madd=document.getElementById('mailaddr').value;
		  if (name =='') {
				alert("請輸入您的姓名！！");
				return (false);
			} else if (madd =='') {
				alert("請輸入朋友的Email！！");
				return (false);
			}
		 	re = /^[0-9a-zA-Z_\.-]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;
			tt=madd.split(";");
		  for (i=0;i<tt.length;i++) {
				if (!re.test(tt[i])) {
					alert("請打入正確的e-mail！");
					return (false);
				}
			}
			$('mailform').submit();
			break;
		case 2:
			name=document.getElementById('acc_name').value;
			madd=document.getElementById('acc_mail').value;
			txt=document.getElementById('accuse_text').value;
			var op = false;
			for (var k=0; k<accuseform.accuse_option.length; k++)
	    {
	      if (accuseform.accuse_option[k].checked)
	      {
					var op = true;
					break;
	      }
	    }
		  if (name =='') {
				alert("請輸入您的姓名！！");
				return (false);
			} else if (madd =='') {
				alert("請輸入您的Email！！");
				return (false);
			} else if (op==false && txt=='') {
				alert("檢舉理由選項或內容必須擇一填寫");
				return (false);
			}
			re = /^[0-9a-zA-Z_\.-]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;
			tt=madd.split(";");
		  for (i=0;i<tt.length;i++) {
				if (!re.test(tt[i])) {
					alert("請打入正確的e-mail！");
					return (false);
				}
			}
			$('accuseform').submit();
			break;
	}

}

// --------------------------------------------
// 跳出登入視窗
function	js_loginclick(mode)		{

switch(mode)	{
	case 1:		// 登入
		if( $('iid').value.length<5)	{
			alert('輸入帳號太短或是未輸入帳號');
			return	false;
			}
		else	$('loginform').submit();
		break;
	case 2:		// 忘記密碼
		window.location.href = '/site/member_acc_f.php';
		break;
	case 3:		// 取消
		document.body.removeChild( $('loginobj'));
		break;
	case 4:
		window.location.href = '/site/member_step1.php';
		break;
 }

}


// --------------------------------------------
// 跳出mailto視窗
function	cancel()		{
		document.body.removeChild( $('mailobj'));
 }



// --------------------------------------------
// 跳出登入視窗
function	js_logout()		{

if( confirm( '要登出 vow 網站?'))		{
	window.location.href = '/site/logout.php';
	return true;
	}
else	return	false;
}
