// JavaScript Document
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];}
}
function CheckAll(theForm, boxname) {
    for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf(boxname) !=-1)
            theForm.elements[i].checked = true;
}

function unCheckAll(theForm, boxname) {
    for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf(boxname) !=-1)
            theForm.elements[i].checked = false;
}

function CheckAll2(field) {
	for (i = 0; i < field.length; i++)
	field[i].checked = true ;		
}

function unCheckAll2(field) {
	for (i = 0; i < field.length; i++)
	field[i].checked = false ;		
}

function NewWindow(mypage, myname, w, h, scroll, pos) {
	var win=null;
	if(pos=="random") {
		LeftPosition = (screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition = (screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	} else
	if(pos=="center") {
		LeftPosition = (screen.width)?(screen.width-w)/2:100;
		TopPosition = (screen.height)?(screen.height-h)/2:100;
	} else
	if(pos=="corner") {
		LeftPosition = (screen.width-w);
		TopPosition = (screen.height-h);
	} else {
		LeftPosition=0;
		TopPosition=20;
	}
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win = window.open(mypage,myname,settings);
}


function popup(URL, name, width, height, scrollbar) {
	window.open(URL,name,'width='+width+',height='+height+', scrollbars='+scrollbar+ ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no')	
}


function DisplayImage(picURL,picTitle,picWidth,picHeight){
	if(picTitle == "Member Image") {
		showImage(picURL,picTitle,picWidth,picHeight);
	} else {
		var top = (screen.height-parseInt(picHeight))/2;
		var left = (screen.width-parseInt(picWidth))/2;
		newWindow=window.open('http://www.e-moment.com/image_popup.php?height='+parseInt(picHeight)+'&width='+parseInt(picWidth)+'&title='+picTitle+'&url='+picURL,'newWin','toolbar=no,width='+picWidth+',height='+picHeight+',top='+top+',left='+left);
		newWindow.focus();
	}
}


function showImage(picURL,picTitle,picWidth,picHeight){
	var WindowWidth = getWindowWidth();
	var WindowHeight = getWindowHeight();
	if(  (WindowWidth<=1024 && parseInt(picWidth)>=900)
		|| (WindowHeight<=900 && parseInt(picHeight)>=900) ) {
		picWidth = parseInt(picWidth)*0.7;
		picHeight = parseInt(picHeight)*0.7;
	}
	document.all.popupImageContent.innerHTML = "<iframe scrolling='no' frameborder='0' allowtransparency='1' width='"+picWidth+"' height='"+(picHeight)+"' src='imagePopup.php?height="+parseInt(picHeight)+"&width="+parseInt(picWidth)+"&title="+picTitle+"&url="+picURL+"'></iframe>";
	//picHeight = parseInt(picHeight)+20;
	showPopUp('popupImage',picWidth,picHeight);
	window.location.href = '#t';
	return false;
}

function name_card() {
	document.all.card.innerHTML="<iframe style=\"width:150px;height:180px;\" src=\"../namecard.php\" frameborder=\"0\" scrolling=\"no\" margin=\"0\"; ></iframe>";
}

function open_name_card(page) {
	document.all.card.innerHTML="<iframe style=\"width:150px;height:180px;\" src=\"../namecard.php\" frameborder=\"0\" scrolling=\"no\" margin=\"0\"; ></iframe>";
	if(page!="") {
		switch(page) {
			case 'ejournal': 	page_frame = eval('eJournalContent'); break;
			case 'ememoir': 	page_frame = eval('eMemoirContent'); break;
			case 'emuseum': 	page_frame = eval('eMuseumContent'); break;
		}
		page_frame.location.reload();
	}
}
function close_namecard() {
	document.all.card.innerHTML="";
}
function getURL(page, target) {
	if(target=="_self") {
		location.href = page;
	} else {
		if(target=='info_table_logged_in') target_frame = eval('info_table');
		else target_frame = eval(target);		
		target_frame.location.href = page;
		if(target=='info_table') close_namecard();
	}
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function bookmark(){
	bookmarkurl="http://www.e-moment.com"
	bookmarktitle="e-moment | e一刻"
	if (document.all){
	window.external.AddFavorite(bookmarkurl,bookmarktitle)
	}
}


var isNS = ( (navigator.userAgent.indexOf("Netscape")!=-1) && (parseInt(navigator.appVersion) >= 4));
var isSafari = ( navigator.userAgent.indexOf("Safari")!=-1 );
function getWindowWidth() {
	if (isNS || isSafari) {
		return window.innerWidth;
	} else {
		return document.body.clientWidth;
	}
}
function getWindowHeight() {
	if (isNS || isSafari) {
		return window.innerHeight;
	} else {
		return document.body.clientHeight;
	}
}
function getDocumentHeight() {
	if (isNS) {
		return window.offsetHeight;
	} else if (navigator.userAgent.indexOf("Safari")!=-1) {
		return document.body.offsetHeight;
	} else {
		return document.body.scrollHeight;
	}
}

function showPopUp(el,width, height) {
	var WindowWidth = getWindowWidth() - 16 ;
	var WindowHeight = getWindowHeight();
	var cvr = document.getElementById("mask");
	var dlg = document.getElementById(el);
	var dlgTable = document.getElementById(el+'Table');
	if(width == null) width=400;
	if(height == null) height=300;
	dlgTable.style.width = width;
	dlgTable.style.height = height;
	dlg.style.left = (WindowWidth - width)/2;
	if(height > WindowHeight) {
		dlg.style.top = 0;
	} else {
		dlg.style.top = (WindowHeight - height)/2;
	}
	cvr.style.display = "block";
	dlg.style.display = "block";
	if (document.body.style.overflow = "hidden") {
		cvr.style.width = WindowWidth;
		if(getDocumentHeight()>getWindowHeight()) {
			cvr.style.height = getDocumentHeight();
		} else {
			cvr.style.height = getWindowHeight();
		}
	}
	document.body.style.overflowY = "scroll";
	return false;
}
function closePopUp(el) {
	var cvr = document.getElementById("mask");
	var dlg = document.getElementById(el);
	cvr.style.display = "none";
	dlg.style.display = "none";
	document.body.style.overflowY = "scroll";
	return false;
}
function initDialog(el,width, height) {
	showPopUp(el,width, height);
	closePopUp(el);	
}

//Get X position
function getXpos(element) {
	var x=0;
	while(element) {
		x += element.offsetLeft;
		element=element.offsetParent;
	}
	return x;
}
//Get Y position
function getYpos(element) {
	var y=0;
	while(element) {
		y += element.offsetTop;
		element=element.offsetParent;
	}
	return y;
}

echoContent = function(filePath, params, resultDivName, status_str, end_function_name) {
  if (window.XMLHttpRequest) {
	var xmlhr = new XMLHttpRequest();
  } else {
	var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }

  xmlhr.open('POST', filePath);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

  xmlhr.onreadystatechange = function() {
	var resultDiv = document.getElementById(resultDivName);
	resultDiv.style.display = 'block';
	if (xmlhr.readyState == 1) {
	  if(status_str!=false) {  
	  	if(status_str==null) resultDiv.innerHTML = '<b>Loading...</b>';
		else if(status_str=='LOADING_MESSAGE') resultDiv.innerHTML = "<div style='text-align:center;padding:50px;font-weight:bold;'>載入中......請稍侯</div>";
	  	else resultDiv.innerHTML = '<b>'+status_str+'</b>';
	  }
	} else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
	  if (xmlhr.responseText) {
		resultDiv.style.display = 'block';
		resultDiv.innerHTML = xmlhr.responseText;
		if(end_function_name != null) eval(end_function_name);
	  }
	} else if (xmlhr.readyState == 4) {
	  alert('Invalid response received - Status: ' + xmlhr.status);
	}
  }
  xmlhr.send(params);
}
