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 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];}
}

var objWin;

function newWindow(url,winType) {
	win_name = "shafolk_pop";

	win_size = "resizable,scrollbars,status=0,top=40,left=40,width=400,height=400";

	if(winType==0){
		win_name = "shafolk_pop0";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=400,height=550";
	}

	if(winType==1){
		win_name = "shafolk_pop1";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=670,height=500";
	}

	if(winType==2){
		win_name = "shafolk_pop2";
		win_size = "resizable,scrollbars,status,top=40,left=40,width=500,height=400";
	}

	if(winType==3){
		win_name = "shafolk_pop3";
		win_size = "toolbar,location,resizable,scrollbars,status,top=40,left=40,width=600,height=400";
	}

	if(winType==4){
		win_name = "shafolk_pop4";
		win_size = "menubar,resizable,scrollbars,status,top=40,left=40,width=600,height=400";
	}

	if(winType==5){
		win_name = "shafolk_pop5";
		win_size = "menubar,toolbar,resizable,scrollbars,status,top=40,left=40,width=600,height=400";

	}
	if(winType==6){
		//for photo display
		win_name = "shafolk_pop6";
		win_size = "status=0,top=30,left=40,width=640,height=480";
	}
	if(winType==7){
		//for email
		win_name = "shafolk_pop7";
		win_size = "scrollbars,status=0,top=40,left=40,width=600,height=550";
	}
	if(winType==8){
		//for login info
		win_name = "shafolk_pop8";
		win_size = "resizable=0,scrollbars,status=0,top=40,left=40,width=400,height=400";
	}
	if(winType==9){
		// normal popup (ie. album)
		var intWidth = screen.width - 10; //Adjust for the end of screen (don't know why?)
		var intHeight = screen.height - 80;

		win_name = "shafolk_pop9";
		win_size = "location, toolbar, resizable, scrollbars, status=0,top=40,left=40,width="+intWidth+",height="+intHeight;
	}
	if(winType==10){
		// history popup
		win_name = "shafolk_pop10";
		win_size = "scrollbars,top=40,left=40,width=760,height=620";
	}
	if(winType==11){
		// eClass Login
		win_name = "shafolk_pop11";
		win_size = "menubar,scrollbars,resizable,status,top=200,left=240,width=480,height=300";
	}
	if(winType==12){
		win_name = "shafolk_pop12";
		// bridgin course
		// win_size = "resizable=0,status,top=5,left=5,width=475,height=654";
		// table-tennis course
		win_size = "resizable=0,status,top=5,left=5,width=900,height=621";
	}
	if(winType==13){
		win_name = "shafolk_pop13";
		win_size = "resizable=0,status,top=5,left=5,width=452,height=640";
	}
	if(winType==14){
		var intWidth = screen.width - 10; //Adjust for the end of screen (don't know why?)
		var intHeight = screen.height - 80;

		win_name = "shafolk_pop9";
		win_size = "menubar, resizable, scrollbars, status=0,top=40,left=40,width="+intWidth+",height="+intHeight;
	}
	if (newWindow.arguments.length>3) {
	    win_name = "GivenSizeWin";
		var is_scroll = 0;
		var is_resizable = 0;
		if (newWindow.arguments.length>4) {
		    is_scroll = newWindow.arguments[4];
		}
		if (newWindow.arguments.length>5) {
		    is_resizable = (newWindow.arguments[5]==1) ? "1" : "0";
		}
		win_size = "resizable="+is_resizable+",scrollbars="+is_scroll+",status=0,top=40,left=40,width="+newWindow.arguments[2]+",height="+newWindow.arguments[3];
	}
	if (navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion >="4")

	if (objWin)
			objWin.close();

	objWin = window.open (url, win_name, win_size);
	if (navigator.appName=="Netscape" && navigator.appVersion >= "3")
		objWin.focus();

}


function DeleteItems(myPage, objName, objForm) {
	var total = CountCheckedItems(objName, objForm);
	if (total<1)
	{
		alert("請最少選擇一個要刪除的項目");
	} else
	{
		var confmsg = "";
		if (myPage.indexOf("export_to_eclass")!=-1)
		{
			confmsg = "是否確定要匯出這 " + total + " 個項目到eClass?";
		} else
		{
			confmsg = "是否確定要刪除這 " + total + " 個項目?";
		}
		if (confirm(confmsg))
		{
			objForm.method = "post";
			objForm.action = myPage;
			objForm.submit();
		}
	}

	return ;
}

function HandleUsers(myPage, objName, objForm) {
	var total = CountCheckedItems(objName, objForm);
	if (total<1)
	{
		alert("請最少選擇一個用戶");
	} else
	{
		if (myPage.indexOf("reject.php")>-1)
		{
			action_name = "拒絕";
		} else if (myPage.indexOf("accept.php")>-1)
		{
			action_name = "批準";
		} else if (myPage.indexOf("member_add_update.php")>-1)
		{
			action_name = "加入";
		} else if (myPage.indexOf("member_remove.php")>-1)
		{
			action_name = "移除";
		} else if (myPage.indexOf("member_set_admin.php")>-1 || myPage.indexOf("member_set_normal.php")>-1)
		{
			action_name = "身份更改";
		}

		if (confirm("是否確定要" + action_name + "這 " + total + " 個用戶?"))
		{
			objForm.method = "post";
			objForm.action = myPage;
			objForm.submit();
		}
	}

	return ;
}


function EditItem(myPage, objName, objForm) {
	var total = CountCheckedItems(objName, objForm);
	if (total!=1)
	{
		alert("請選擇一個要編輯的項目");
	} else
	{
		objForm.method = "post";
		objForm.action = myPage;
		objForm.submit();
	}

	return ;
}


function CountCheckedItems(objName, objForm) {
	var i = 0;
	var count = 0;
	for (i=0; i<objForm.elements.length; i++)
	{
		if (objForm.elements[i].name==objName)
		{
			if (objForm.elements[i].checked)
			{
				count ++;
			}
		}
	}

	return count;
}


function SwitchCheckAll(objName, objSelf) {
	var i = 0;
	var isCheck = (objSelf.checked);
	var objForm = objSelf.form;
	for (i=0; i<objForm.elements.length; i++)
	{
		if (objForm.elements[i].name==objName)
		{
			objForm.elements[i].checked = isCheck;
		}
	}

	return;
}


function UncheckMaster(objForm) {
	objForm.CheckMaster.checked = false;

	return ;
}



function isIE4(){
	return (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion)>=4);
}

function isN4(){
	return (navigator.appName == "Netscape" && parseInt(navigator.appVersion)>=4)
}


function resizeWin(){
	if((document.myPhoto.width==0)&&(document.myPhoto.height==0))
	{
		window.setTimeout('resizeWin()', 800);
	} else
	{
		var photowidth = document.myPhoto.width;
		var photoheight = document.myPhoto.height;
		if (isIE4())
		{
			photowidth=photowidth + 12;
			photoheight=photoheight + 57;
			document.body.scroll="no";
		}
		if (photowidth > window.screen.width - 60)
		{
			photowidth = window.screen.width - 60;
			if (isIE4())
			{
				document.body.scroll="yes";
			}
		}
		if (photoheight > window.screen.height - 30)
		{
			photoheight = window.screen.height - 30;
			if (isIE4())
			{
				photowidth=photowidth + 13;
				document.body.scroll="yes";
			}
		}
		window.resizeTo(photowidth,photoheight);
	}

	return;
}

function showPhoto(myPhoto, myCaption){
	//document.frmPhoto.myPhoto.value = myPhoto;
	newWindow("/src/show_photo.php?myPhoto="+myPhoto, 6);

	return;
}

function displayTable(tableID, myStyle){
	//block, none, inline
	var currentStyle = document.all[tableID].style.display;
	var newStyle = "none";
	if (typeof(myStyle)!="undefined")
	{
		newStyle = myStyle;
	} else
	{
		newStyle = (currentStyle=="none") ? "block" : "none";
	}

	document.all[tableID].style.display = newStyle;

	return;
}


function ChangeContent( objID, new_content ) {
	var obj = document.getElementById(objID);
	if (obj!=null)
	{
		obj.innerHTML = new_content;
	}
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

// generate options from jArrData and bold selected option
function jSelectPresetText(jArrData, jTarget, jMenu, jPos){

	var listStr = "";
	var fieldValue;
	var count = 0;

	var btn_hide = (typeof(js_btn_hide)!="undefined") ? js_btn_hide : "";

	listStr += "<table bgcolor='#ffffff' style='border:1px solid #FFFFFF;' border='0' cellpadding='0' cellspacing='0'>";
	var box_height = (jArrData.length>10) ? "250" : "150";
	listStr += "<tr><td bgcolor='#CCCCCC' style='border-left:1px solid #CCCCCC;border-top:1px solid #CCCCCC;border-right:1px solid #CCCCCC'>&nbsp;<a href=\"javascript:void(0)\" onClick=\"document.getElementById('" + jMenu + "').style.display= 'none';setDivVisible(false, '"+jMenu+"', 'lyrShim')\" title=\""+btn_hide+"\" ><font face='arial' color='#FFFFFF'><b>x</b></font></a></td></tr>";
	listStr += "<tr><td style='border-left:1px solid #CCCCCC;border-bottom:1px solid #CCCCCC;border-right:1px solid #CCCCCC'><div style=\"overflow: auto; height: " + box_height + "px; width: 160px\"><table width=\"90%\" border='0' align='center' class='tbclip'>";

	if (typeof(jArrData)=="undefined" || jArrData.length==0)
	{
		if (typeof(js_preset_no_record)!="undefined")
		{
			listStr += "<tr><td class='guide' align='center'>"+js_preset_no_record+"</td></tr>";
		}
	} else
	{
		for (var i = 0; i < jArrData.length; i++)
		{
			for (var j = 0; j < jArrData[i].length; j++)
			{
				fieldValue = document.getElementById(jTarget).value.replace(/&/g, "&amp;").replace(/'/g, "&#96;").replace(/`/g, "&#96;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

				listStr += "<tr><td style=\"border-bottom:1px #CCCCCC solid; background-color: ";
				if (fieldValue == jArrData[i][j])
				{
					listStr += "#FFF2C3";
				} else
				{
					listStr += "#FFFFFF";
				}
				listStr += "\">";

				if (fieldValue == jArrData[i][j])
				{
					listStr += "<b>";
				}
				listStr += "<a href=\"javascript:void(0)\" onClick=\"document.getElementById('" + jTarget + "').value = '" + jArrData[i][j] + "' ;document.getElementById('" + jMenu + "').style.display= 'none';setDivVisible(false, '"+jMenu+"', 'lyrShim')\" title=\"" + jArrData[i][j] + "\">" + jArrData[i][j] + "</a>";
				if (fieldValue == jArrData[i][j])
				{
					listStr += "</b>";
				}

				listStr += "</td></tr>";
				count ++;
			}
		}
	}

	listStr += "</table></div></td></tr></table>";

	document.getElementById(jMenu).innerHTML = listStr;

	document.getElementById(jMenu).style.position = "absolute";
	document.getElementById(jMenu).style.zIndex = "9999999";
	document.getElementById(jMenu).style.textalign = "left";

	var pos = findPos(document.getElementById("posimg" + jPos));

	document.getElementById(jMenu).style.left = (pos[0] + 0) + "px";
	document.getElementById(jMenu).style.top = pos[1] + "px";
	document.getElementById(jMenu).style.display= 'block';

	setDivVisible(true, jMenu, "lyrShim");

}


function setDivVisible(state, lay, lay2)
{
	var DivRef = document.getElementById(lay);
	var lay_used = (lay2!="") ? lay2 : "lyrShim";
	var IfrRef = document.getElementById(lay_used);
	if (state)
	{
		DivRef.style.display = 'block';
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = 'block';
	} else
	{
		DivRef.style.display = 'none';
		IfrRef.style.display = 'none';
	}
}

function setChecked(val, obj, element_name){
	len=obj.elements.length;
	var i=0;

	for( i=0 ; i<len ; i++) {
		if (obj.elements[i].name==element_name)
		obj.elements[i].checked=val;
	}
}