﻿	function Set_Cookie( name, value, expires, path, domain, secure ) 
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );

		// convert so expires is in hours
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60;
		}
		
		var expires_date = new Date( today.getTime() + (expires) );
	
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}
	
	// this function gets the cookie, if it exists
	function Get_Cookie( name ) {
	
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
		{
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}

	// this deletes the cookie when called
	function Delete_Cookie( name, path, domain ) {
		if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}

	// Data used by URL Encoding
	var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
	var reserved = "!*'();:@&=+$,/?%#[]";
	var allowed = unreserved + reserved;
	var hexchars = "0123456789ABCDEFabcdef";

	function gethex(decimal) {
		return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
	}
	
	// URL Encode
	function URLEncode(originalString) {
	
		var encoded = "";
		
		for (var i = 0; i < originalString.length; i++ ) {

			var ch = originalString.charAt(i);

			// Check if character is an unreserved character:
			if (unreserved.indexOf(ch) != -1) {

				encoded = encoded + ch;

			} else {
				// If position in the Unicode table is smaller than 128, then we have
				// an ASCII character:
				var charcode = originalString.charCodeAt(i);

				if (charcode < 128) {

					encoded = encoded + gethex(charcode);

				} else {

					encoded = encoded + ch;
					notascii = notascii + ch + " ";

				}

			}

		}

		return encoded;
	}
	
	// function to copy info to cookies and hidden values, then submit
	function CopyInfoToPass(Username_Input, Password_Input, nextpageURL, MIMEType)
	{
	
		var nextpageURL_corrected;
		var index_pathname;
		
		// Copy username and password to hidden input values to be passed on to CGI via POST
		Set_Cookie("Username", Username_Input, 8, "/", "", ""); 
		Set_Cookie("Password", Password_Input, 0.5, "/", "", "");
		
		// if nextpage URL is passed to CGI Location: with http://, it doesn't convert encoding
		// correctly.  Check to see if the file is on the same server.  If so, pass on only
		// the file path and name
		
		if (window.location.hostname == nextpageURL.hostname) {
		
			// nextpage URL is on same host, remove hostname
			// and replace with relative path
			nextpageURL_corrected = ".."; // relative path from cgi-bin directory, moves back to site root
			
			if (nextpageURL.pathname == "/")
			{
				nextpageURL_corrected = nextpageURL_corrected + "/index.shtml";
			}
			else
			{
				nextpageURL_corrected = nextpageURL_corrected + nextpageURL.pathname;
			}
			
		}
		else
		{
			nextpageURL_corrected = nextpageURL;
		}
		
		// Copy next page's URL and MIME type
		document.Submitter.NextPage.value = nextpageURL_corrected;
		document.Submitter.MIMEType.value = MIMEType;
		Set_Cookie("NextPage", nextpageURL_corrected); 
		Set_Cookie("MIMEType", MIMEType);
		
		document.Submitter.submit();

	}
	
	function Logon_Click()
	{
	
		var Username_Input = document.forms.LogonBox.UsernameInput.value;
		var Password_Input = document.forms.LogonBox.PasswordInput.value;
		
		// Check username and password input
		if (Username_Input == "")
		{
			alert("ユーザーIDを記入して下さい。\nPlease enter username!");
			return false;
		}
		if (Password_Input == "")
		{
			alert("パスワードを記入して下さい。\nPlease enter password!");
			return false;
		}

		CopyInfoToPass(Username_Input, Password_Input, window.location, "");
		
		return false;
	
	}
	
	function LogonAgain_Click()
	{
	
		var Username_Input = document.forms.LogonBox.UsernameInput.value;
		var Password_Input = document.forms.LogonBox.PasswordInput.value;
		
		// Check username and password input
		if (Username_Input == "")
		{
			alert("ユーザーIDを記入して下さい。\nPlease enter username!");
			return false;
		}
		if (Password_Input == "")
		{
			alert("パスワードを記入して下さい。\nPlease enter password!");
			return false;
		}

		CopyInfoToPass(Username_Input, Password_Input, Get_Cookie("NextPage"), Get_Cookie("MIMEType"));
		
		return false;
	
	}

	function Logoff_Click()
	{
	
		Delete_Cookie("Username", "/", "");
		Delete_Cookie("Password", "/", "");
		
		window.location = "/index.shtml";
		
	}
	
	//***************************************************
	//
	// OpenLink
	// function to load file when user clicks on link requiring password
	//
	//***************************************************

	function OpenLink(nextpage, MIMEType, separate)
	{
		
		if ((Get_Cookie("Username") == null) || (Get_Cookie("Password") == null))
		{
		
			// no login info is entered yet or user is logged off
			
			alert("JAPIA　ニュースレターを開くにはログオンする必要があります。\nLogon required to view JAPIA newsletters.");
			
			return;
			
		}
		

		if (separate) {
			// open in separate window
			window.open("/cgi-bin/loadpage.exe?NextPage=" + URLEncode(nextpage) + "&MIMEType=" + URLEncode(MIMEType));

		} else {
			// load in same window
			// set cookies and input values to pass to CGI
			CopyInfoToPass(Get_Cookie("Username"), Get_Cookie("Password"), nextpage, MIMEType);
		
		}
		
		return;
		
	}


	//***************************************************
	//
	// Pop up menu functions
	//
	//***************************************************
	
	var currentOpenMenu = "";
	var menuOnOff = 0;
	
	// PopMenu - closes any pop up menu open, then 
	//			  displays menu (obj) at coordinates menuX,menuY
	//			  if menu == null then it won't open anything
	function PopMenu(menu, menuX, menuY)
	{

		hideMenu();

		if((menu) && (menuOnOff))
		{
			document.getElementById(menu).style.left = menuX;
			document.getElementById(menu).style.top = menuY;
			document.getElementById(menu).style.visibility = "visible";
			currentOpenMenu = menu;
		}
		
	}

	// function to hide menu
	function hideMenu()
	{
	
		if (currentOpenMenu != "")
		{
			// There is something open.  Close it
			document.getElementById(currentOpenMenu).style.visibility = "hidden";
			currentOpenMenu = "";
		}
		
	}
	
	function togglePopup(menu, menuX, menuY)
	{
	
		if (!menuOnOff)
		{
		
			// pop up menu is off.  Turn it on
			menuOnOff = 1;
			PopMenu(menu, menuX, menuY);
			
		}
		else
		{
		
			// pop up menu is on.  Turn it off
			menuOnOff = 0;
			hideMenu();
			
		}
	
	}
	
	// function to find absolute x position
	function findPosX(obj)
	{
		var curleft = 0;
		if(obj.offsetParent)
			while(1) 
			{
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.left)
			curleft += obj.left;
	  
	  return curleft;
	}
 
	// function to find absolute y position
	function findPosY(obj)
	{
		var curtop = 0;
		if(obj.offsetParent)
			while(1)
			{
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.top)
			curtop += obj.top;
		return curtop;
	}
	
	function donothing()
	{
	}
