
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function setLyr(obj,lyr,offsetx,offsety)
{

	var newX = findPosX(obj);
	var newY = findPosY(obj);
	if (lyr == 'testP') newY -= 50;
	var x = document.getElementById(lyr);
	x.style.top = (newY + offsety) + 'px';
	x.style.left = (newX + offsetx) + 'px';
}


function switchimg(url,link,pos)	{
	var ss = '<a href="#" onclick="javascript:popUp(\'displayphoto.php?p='+ link +'\')"><img src="'+url+ '" width="200" hspace="5" vspace="5" border="0"></a>';
	if (pos == 0)
	{
		ss += '<?= $defaultPhotoHelp ?>';
	}
//	alert (ss);
	document.getElementById('memberphotodiv').innerHTML = ss;

}


function showToolTipStatic(imgname,imgpath,divname,on_off)	{
	oo = eval( 'document.'+imgname);
//	setLyr(oo,divname,offsetx,offsety);
	if (on_off == 'on')
	{
		oo.src='images/'+imgpath+'_on.gif';
		document.getElementById(divname).style.visibility = 'visible';
	} else {
		oo.src='images/'+imgpath+'.gif';
		document.getElementById(divname).style.visibility = 'hidden';
	}
}

function showToolTip(imgname,imgpath,divname,on_off,offsetx,offsety)	{
	oo = eval( 'document.'+imgname);
	setLyr(oo,divname,offsetx,offsety);
	if (on_off == 'on')
	{
		oo.src='images/'+imgpath+'_on.gif';
		document.getElementById(divname).style.visibility = 'visible';
	} else {
		oo.src='images/'+imgpath+'.gif';
		document.getElementById(divname).style.visibility = 'hidden';
	}
}

function isValidDate(day,month,year){

var dteDate;

//set up a Date object based on the day, month and year arguments
//javascript months start at 0 (0-11 instead of 1-12)
dteDate=new Date(year,month-1,day);

return ((day==dteDate.getDate()) && (month==1+dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

function showHelp()	{
	showToolTipStatic('helpgif','help','l1','on');
//	document.helpgif.src='images/help_on.gif';
//	document.getElementById('l1').style.visibility = 'visible';
}
function showHelp_off()	{
	showToolTipStatic('helpgif','help','l1','off');
//	document.helpgif.src='images/help.gif';
//	document.getElementById('l1').style.visibility = 'hidden';
}


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];}
}
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_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 swap(name,action)	{
	if (action == 'on')	
		eval('document.'+name+'.src="images/'+name+'_on.gif"');
	else if (action == 'dn')
		eval('document.'+name+'.src="images/'+name+'_dn.gif"');
	else	
		eval('document.'+name+'.src="images/'+name+'.gif"');
}

function Sswap(obj, name,action)	{
	if (action == 'on')	
		eval('document.'+obj+'.src="images/'+name+'_on.gif"');
	else if (action == 'dn')
		eval('document.'+obj+'.src="images/'+name+'_dn.gif"');
	else	
		eval('document.'+obj+'.src="images/'+name+'.gif"');
}


function email_valid(email) {
	l = email.length
	locate = email.indexOf("@")
	if (locate < 0 || locate == 0 || email.length <= 5 || email.charAt(l-1) == '@' || email.indexOf(".") <= 1)
		return 0;
	else
		return 1;
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("match3gphoto = window.open(URL, 'match3gphoto', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=520,height=600,left = 60,top = 20');");
match3gphoto.focus();
}

function popUp2(URL,x,y,w,h) {
day = new Date();
id = day.getTime();
eval("match3gphoto = window.open(URL, 'match3gphoto', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+w+",height="+h+",left = "+x+",top = "+y+"');");
match3gphoto.focus();
}

// <!-- member object -->
function member(id,  sex,imageurl, nickname,age, location1, location2,msg)	{
	this.id = id;
	this.sex = sex;
	this.imageurl = imageurl;
	this.nickname = nickname;
	this.age = age;
	this.location1 = location1;
	this.location2 = location2;
	this.msg = msg;
}

// <!-- member object -->
function member(id,  sex,imageurl, nickname,age, location1, location2,msg,isFriend)	{
	this.id = id;
	this.sex = sex;
	this.imageurl = imageurl;
	this.nickname = nickname;
	this.age = age;
	this.location1 = location1;
	this.location2 = location2;
	this.msg = msg;
	this.isFriend = isFriend;
}

// <!-- member object -->
function member(id, sex, imageurl, nickname,age, location1, location2,msg,isFriend,match_score1,match_score2,canSendSMS, canSendMMS)	{
	this.id = id;
	this.sex = sex;
	this.imageurl = imageurl;
	this.nickname = nickname;
	this.age = age;
	this.location1 = location1;
	this.location2 = location2;
	this.msg = msg;
	this.isFriend = isFriend;
	this.match_score1 = match_score1;
	this.match_score2 = match_score2;
	if (this.match_score1 == undefined)
	{
		this.match_score1 = 0;
	}
	if (this.match_score2 == undefined)
	{
		this.match_score2 = 0;
	}
	this.canSendSMS = canSendSMS;
	this.canSendMMS = canSendMMS;
}

function member(id, sex, imageurl, nickname,age, location1, location2,msg,isFriend,isPFriend,isBFriend,match_score1,match_score2,canSendSMS, canSendMMS)	{
	this.id = id;
	this.sex = sex;
	this.imageurl = imageurl;
	this.nickname = nickname;
	this.age = age;
	this.location1 = location1;
	this.location2 = location2;
	this.msg = msg;
	this.isFriend = isFriend;
	this.isPFriend = isPFriend;
	this.isBFriend = isBFriend;
	this.match_score1 = match_score1;
	this.match_score2 = match_score2;
	if (this.match_score1 == undefined)
	{
		this.match_score1 = 0;
	}
	if (this.match_score2 == undefined)
	{
		this.match_score2 = 0;
	}
	this.canSendSMS = canSendSMS;
	this.canSendMMS = canSendMMS;
}

// <!-- message object -->
function message(id, member_id, isout,isnew,ismark,photopath,name,msgbody,extraphotopath,istick,created,canSendSMS,canSendMMS)	{
	this.id = id;
	this.member_id = member_id;
	this.isout = isout;
	this.isnew = isnew;
	this.ismark = ismark;
	this.photopath = photopath;
	this.name = name;
	this.msgbody = msgbody;
	this.extraphotopath = extraphotopath;
	this.istick = istick;
	this.created = created;
	this.isdeleted = 0;
	this.showinglargeimage = 0;
	this.canSendSMS = canSendSMS;
	this.canSendMMS = canSendMMS;

}

 var ie4 = (document.all) ? true : false;

function insertOneRow(tblname,divprefix,index,html)	{


  var tbl = document.getElementById(tblname);
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(index);
   
  // main td cell
  var cellRight = row.insertCell(0);
  var el = document.createElement('div');
  el.setAttribute('id', divprefix + iteration);
  cellRight.appendChild(el);

  document.getElementById(divprefix + iteration).innerHTML = html;

}

   function escapeToUrl(inn) {
     encodedHtml = escape(inn);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
   } 

   function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

var notFoundStr = "對不起！找不到合適會員！";

function updatefrienddiv(_members,seq, returnStr,uniqueid)	{
	ss = '<a href="#" onClick="clicksendmsglink(' + uniqueid + ');return false;">寫訊息</a>';

	if (_members[seq].isFriend == -1) {
		ss += ' | ';
	} else if (_members[seq].isFriend == 1) {
		ss += ' | 已是朋友 | ';
	} else if (_members[seq].isPFriend == 1) {
		ss += ' | 已是密友 | ';
	} else if (_members[seq].isBFriend == 1) {
		ss += ' | 已列入黑名單 | ';
	} else {
		ss += ' | <a href="js.php?action=addfriend&friend_id=' + _members[seq].id + '&seq='+uniqueid+'" target="js">加入朋友</a> | ';
	} 
	ss += '<a href="blog.php?bm=' + _members[seq].id + '&img=' + _members[seq].imageurl+ '">個人日記</a>'; 

	tmp = 'frienddiv' + String(uniqueid); 
	if(match3guserid == '' || match3guserid == '0')
		ss = '';

	if (returnStr)
	{
		return ss;
	}
	document.getElementById(tmp).innerHTML = ss;
}

var currentPage = 0;
var perPage = 8;

var tcountdlist=0;

function genDisplayDetailedList(_members,displayareaid,isSpecialForFriend,adjustt,photocount)	{
//	alert(adjustt);
	if (adjustt == 1 || adjustt == -1)
	{
	currentPage += adjustt;
	}
	
	if (isSpecialForFriend != undefined && isSpecialForFriend != '') {
		currentPage = 0;
		perPage = 9999;
	} else {
		tcountdlist = currentPage * perPage; 
		// tcountdlist should be added after "more than 1 page support"
		// it should be the same as i 
	}

	var ss = ''; 
	var isp = isSpecialForFriend;
	if (isSpecialForFriend == 'f')
	{
		isp = '';
	}
	var ss = '';
	var ww  = '移至我的密友';
    var ww1 = '移至黑名單';
	var isp1 = 'b';
	var isp2 = isp;

	if (isp == 'p')
	{
		ww = '移至我的朋友';
	} else if (isp == 'b')
	{
		ww   = '移至我的朋友';
		ww1  = '移至我的密友';
		isp  = 'p';
		isp1 = '';
		isp2 = 'b';
	}
		
	ss +='<table border="0" cellpadding="0" cellspacing="0" width="496">';
	ss +='<tr><td width="496"><img src="images/blank.gif" width="496" height="1"></td></tr>';

	//added by jenny
	if (photocount == 0) {
	  ss += '<tr><td class="tdd">' + notFoundStr + '</td></tr>';
	}
	// added end 
	
	for (var i = currentPage * perPage; i <_members.length && i < ( (currentPage+1) * perPage); i++)	{
		
		ss += '<tr><td class="tdmain" height="130" background="images/photoframe2.gif"><table border="0" cellpadding="10" cellspacing="0" height="130" width="496"><tr><td valign="middle" width="110"><center><a href="showmember.php?id='+_members[i].id+'"><img vspace="0" border="0" src="photos/s/'+_members[i].imageurl+'" height="110" width="90"></a></center></td><td class="tdmain" width="386" style="vertical-align=\'top\'"><font style="font-size=11px"><b>' + _members[i].nickname + '</b></a>　　' + _members[i].age + '歲 | ' + _members[i].location1 + ' | ' + _members[i].location2 ;
		
		if (isSpecialForFriend != null && isSpecialForFriend != '')
		{
			ss += '<img src="images/blank.gif" width="50" height="1"><a href="friend.php?action=move'+isp+'friend&id='+_members[i].id+'"><img src="images/move'+isp+'fd.gif" border="0" hspace="5" alt="'+ww+'"></a><a href="friend.php?action=move'+isp1+'friend&id='+_members[i].id+'"><img src="images/move'+isp1+'fd.gif" border="0" hspace="0" alt="'+ww1+'"></a><a href="friend.php?action=delete'+isp2+'friend&id='+_members[i].id+'"><img src="images/x.gif" border="0" hspace="5" alt="刪除"></a>';
		}
		
		ss += '<br><a href="showmember.php?id='+_members[i].id+'">' + _members[i].msg.substring(0,25);

		if (_members[i].msg.substring(0,25) != "") {
			ss +=  '...';
		}
		
		ss += '</a></font><div id="frienddiv' + tcountdlist + '" align="right">';
		
		// alert ("head.js: i: " + i + " tcountdlist: " + tcountdlist);
		ss += updatefrienddiv(_members,i, true,tcountdlist);

		ss += '</div>';
//		if (_members[i].match_score1 > 0)
//		{
		if ( (match3guserid != '' && match3guserid != '0') && (isSpecialForFriend == undefined || isSpecialForFriend == '')) {

				if (_members[i].sex == '男') {
					ss += '<br>他';
				} else if (_members[i].sex == '女') {
					ss += '<br>她';
				}
				
				ss += '配合你: <img src="images/bar_';
				
				if (_members[i].sex == '男') {
					ss += 'blue';
				} else if (_members[i].sex == '女') {
					ss += 'red';
				}
					
				ss += '.gif" height="12" width="'+(_members[i].match_score1*2) + '"> '+_members[i].match_score1+'%<br>你配合';
				
				if (_members[i].sex == '男') {
					ss += '他';
				} else if (_members[i].sex == '女') {
					ss += '她';
				}

				if (match3gusersex == 'f') {
					ss += ': <img src="images/bar_red.gif"';
				} else if (match3gusersex == 'm') {
					ss += ': <img src="images/bar_blue.gif"';
				}

				ss += ' height="12" width="'+(_members[i].match_score2*2)+'"> '+_members[i].match_score2+'%' ;
		}
//		} else {
//			ss += '<br>他配合你: <img src="images/bar_red.gif" height="12" width="1"> 0%<br>你配合他: <img src="images/bar_blue.gif" height="12" width="1"> 0%' ;
//		}
		ss += '</td></tr></table></td></tr>';
		
		// start sendmsg things

		ss +='<tr><td id="sendmsgtd' + tcountdlist + '"><img src="images/blank.gif" width="1" height="15"></td><td><form id="sendmsgform' + tcountdlist + '" action="js.php" method="post" target="js" enctype="multipart/form-data"><input type="hidden" name="action" value="sendmsg"><input type="hidden" name="to_member_id" value="' + _members[i].id + '"><input type="hidden" name="to_nickname" value="' + _members[i].nickname + '"><table id="sendmsgtable' + tcountdlist + '" width="496" height="160" background="images/photoframe2b.gif" border="0" cellpadding="0" cellspacing="0" style="display:none" class="tdd2">';

		if (match3guserapproval_status == '1')
		{
			
			ss += '<tr><td rowspan="99" width="15"><img scr="images/blank.gif" width="15" height="1"></td><td valign="top" colspan="2">輸入你的訊息:<BR><textarea name="msgbody" class="stFrmStyle2" style="width:350px;height:100px"></textarea></td><td width="110" rowspan="2" valign="top"><div id="uploadpreview' + tcountdlist + '"></div></td></tr><tr><td valign="top"><img src="images/blank.gif" width="1" height="5"><br>加入相片:<input type="file" name="f1" class="stFrmStyle2file" onChange="updateUploadG()"></td><td align="left"><br>&nbsp;</td></tr><tr><td valign="top" colspan="3"><input type="radio" name="sendmode" value="1" checked>Match3G 訊息';
			if (_members[i].canSendSMS != undefined && _members[i].canSendSMS != '' && _members[i].canSendSMS != '0' && _members[i].canSendSMS != 'false')
			{
				ss += '&nbsp; <input type="radio" name="sendmode" value="2">SMS & Match3G 訊息';
			}
			if (_members[i].canSendMMS != undefined && _members[i].canSendMMS != '' && _members[i].canSendMMS != '0' && _members[i].canSendMMS != 'false')
			{
				ss += '&nbsp; <input type="radio" name="sendmode" value="3">MMS & Match3G 訊息';
			}
			ss += '</td></tr><tr><td align="left" id="msgsubmittr' + tcountdlist + '"><img src="images/blank.gif" width="1" height="5"><br><input type="button" a href="#" onClick="clicksendmsglink(' + tcountdlist + ');return false;" value="取消"><input type="button" a href="%" onClick="sendmsgformsubmit(' + tcountdlist + ');return false;" value="發送"><br>&nbsp;</td></tr><tr id="msgsavingtr' + tcountdlist + '" style="display:none"><td><center><b><img src="images/waiting.gif" align="middle" hspace="5">發送中...</b></center></td></tr>';

		} else { 
			ss += '<tr><Td class="formErr" align="center">你的必須先完成個人檔案，才可以寫訊息。<BR>請耐心等候，我們將會盡快審核你的檔案，並以電郵通知。</td></tr>';
		} 
		
		ss+='</table></form></td></tr>'; 
		
		// end send msg things

		tcountdlist++;
	}
							
	ss +='</table>';
	ss +='<table width="100%"><tr><td width="50%">';
	if ( currentPage > 0 )
	{
		ss += '<a href="#" onclick="genDisplayDetailedList(newmembers,\'newmemberarea\',0,-1);">上一頁</a>';
	}
	ss += '</td><td width="50%" align="right">';
	if ( ( (currentPage+1) * perPage) < _members.length )
	{
		ss += '<div align="right"><a href="#" onclick="genDisplayDetailedList(newmembers,\'newmemberarea\',0,1);">下一頁</a></div>';
	}
	ss += '</td></tr></table>';
	document.getElementById(displayareaid).innerHTML = ss; 
}


function genDisplayGrid(_members,displayareaid,isSpecialForFriend,adjust)	{

	if (adjust == 1 || adjust == -1)
	{
	currentPage += adjust;
	}

	if (isSpecialForFriend != undefined && isSpecialForFriend != '')
	{
		currentPage = 0;
		perPage = 999;
	}


	var ss = '';
	
	
	ss +='<table border="0"cellpadding="0" cellspacing="0">';
	ss +='';
	ss +='<tr>';
	ss +='<td width="120"><img src="bk.gif" width="120" height="4"></td>';
	ss +='<td width="5" rowspan="99"><img src="bk.gif" width="5" height="1"></td>';
	ss +='<td width="120"><img src="bk.gif" width="120" height="1"></td>';
	ss +='<td width="6" rowspan="99"><img src="bk.gif" width="6" height="1"></td>';
	ss +='<td width="120"><img src="bk.gif" width="120" height="1"></td>';
	ss +='<td width="5" rowspan="99"><img src="bk.gif" width="5" height="1"></td>';
	ss +='<td width="120"><img src="bk.gif" width="120" height="1"></td>';
	ss +='</tr>';
	ss +='';
	ss +='<tr>';

	// added by jenny 
	if (_members.length == 0) {
	  ss += '<tr><td class="tdd">' + notFoundStr + '</td></tr>';
	}
	// added end 

	for (var i = currentPage * perPage; i <_members.length && i < ((currentPage+1)  * perPage); )	{
		for (var j=0; j<4 && i<_members.length && i < ((currentPage+1)  * perPage); j++,i++)	{
			ss += '<td height="165" align="center" background="images/photoframe1.gif" class="tdmain"><center><a href="showmember.php?id='+_members[i].id+'"><img vspace="3" border="0" src="photos/s/'+_members[i].imageurl+'" height="110" width="90"><br><font style="font-size=11px"><b>' + _members[i].nickname + '</b></a><br>' + _members[i].age + ' | ' + _members[i].location1 + ' | ' + _members[i].location2 + '</font></center></td>';
		}
		ss +='</tr>';
		ss +='';
		ss +='<tr><td height="3"><img src="bk.gif" width="100" height="3"></td></tr>';
		ss +='';
	}
							
	ss +='</table>';
	ss +='<table width="100%"><tr><td width="50%">';
	if ( currentPage > 0 )
	{
		ss += '<a href="#" onclick="genDisplayGrid(newmembers,\'newmemberarea\',0,-1);return false;">上一頁</a>';
	}
	ss += '</td><td width="50%">';
	if ( ( (currentPage+1) * perPage) < _members.length )
	{
		ss += '<a href="#" onclick="genDisplayGrid(newmembers,\'newmemberarea\',0,1);return false;"><div align="right">下一頁</div></a>';
	}
	ss += '</td></tr></table>';
	document.getElementById(displayareaid).innerHTML = ss; 
	
}


function updateUploadG()	{
	tmp = 'sendmsgform' + String(currentSeq);
	
	var fs = document.getElementById(tmp).f1.value;

	var yyy = fs.length;
	var i1 = fs.toLowerCase().indexOf(".jpg");

	tmp = 'uploadpreview' + String(currentSeq);

	if (fs == '')
	{
		document.getElementById(tmp).innerHTML = '';
	} else if (i1==-1 || i1!=yyy-4) {
		alert ('只接受 JPG 檔案');
		document.getElementById(tmp).innerHTML = '';
		
		tmp = 'sendmsgform' + String(currentSeq);
		tt = document.getElementById(tmp).msgbody.value;
		document.getElementById(tmp).reset();
		document.getElementById(tmp).msgbody.value = tt;
		//tt = document.sendmsgform.msgbody.value;
		//document.sendmsgform.reset();
		//document.sendmsgform.msgbody.value = tt;
	} else {
		if (ie4)
		{
			var tttt = '<img src="'+ fs +'" width="100" vspace="20">';

			tmp = 'uploadpreview' + String(currentSeq);
			document.getElementById(tmp).innerHTML = tttt;
		}
		
	} 
}


function clicksendmsglink(seq)	{
	currentSeq = seq;
	tmp  = 'sendmsgtable' + String(seq);
	tmp1 = 'sendmsgtd' + String(seq);
	tmp2 = 'sendmsgform' + String(seq);

		if (document.getElementById(tmp).style.display == '')
		{
			document.getElementById(tmp2).reset();			
			document.getElementById(tmp).style.display = 'none';
			document.getElementById(tmp1).style.display = '';
		} else {
			document.getElementById(tmp).style.display = '';
			document.getElementById(tmp1).style.display = 'none';
		}
}

function sendmsgformsubmit(seq)	{
	tmp = 'msgsavingtr' + String(seq);
	document.getElementById(tmp).style.display = '';

	tmp = 'msgsubmittr' + String(seq);
	document.getElementById(tmp).style.display = 'none';
	//document.sendmsgform.submit();

	tmp = 'sendmsgform' + String(seq);

	tmpForm = document.getElementById(tmp);

	tmpForm.submit();
	// document.getElementById(tmp).submit();

}
