function chk_auth() {

	if((document.cookie.indexOf(escape("vauth")) != -1) && (document.cookie.indexOf(escape("-")) != -1))
	{
		//alert('ck_auth cookie is: ' + document.cookie);
		index	= document.cookie.indexOf("vauth");
	
		cookieStart	= (document.cookie.indexOf("=", index) + 1);
		cookieEnd		= (document.cookie.indexOf(";", index));
		
	if(cookieEnd == -1) {
				cookieEnd = document.cookie.length;
		}
		vauth	= document.cookie.substring(cookieStart, cookieEnd);
		return vauth;
	} else {
		return "false";
	} 

}

function require_cookie(utype,page) {
	
	// FUNCTION FOR ALL PAGES REQUIRING LOGIN
	var url;
	//alert('url is ' + url);
	var auth_cookie = chk_auth();
	if(auth_cookie == 'false') {
		//alert('here I am!: ' + auth_cookie);
		if(page == 'text'){
			return "guest";
		}
		else if(utype == 'perf') {
			window.location.href='/performer.html';
    		} else if(utype == 'vwr') {
      		// logged in cookie is not set ... need to login
      			window.location.href='/home.shtml';
    		}
	} else {
	// logged in cookie is set ... show user information
		//alert('req cookie is ' + auth_cookie);
		var auth_arr = auth_cookie.split("-");
		var ut=get_utype_str(auth_arr[2]);
		//alert('ut is ' + ut);
		//alert(' utype is ' + utype);
		if(utype != ut) {
		// user type set on page is not equal to user's type
			if(ut == 'vwr') {
				window.location.href='/home.shtml';
			} else {
				window.location.href='/performer/mypage.shtml';
			}
		} else {
			if(page == 'text') {
				return utype;
			}
			if(page == 'utype') {
				return auth_arr[2];
			}
		}
	}
}

function get_utype_str(type){

	if(type == 210) {
		return 'perf';
	} else {
			return 'vwr';
	}

}

function display_home_page(page) {

	var auth_cookie = chk_auth();
	if(auth_cookie != 'false') {
		// show viewer information
		var uname;		
		var utype;
		var auth_arr = auth_cookie.split("-");
		var	uname=auth_arr[0];
		var	utype=auth_arr[2];
		var ut = get_utype_str(utype);
		if(page == 'perf') {
			if(ut != page) {
				window.location.href="/home.shtml";
			}
		}
		redirect_users(utype,uname);
	
	} else {
	 	if(page == 'perf') {
			get_perf_login_form();
		} else {	
			get_login_form();
		}
	}
	/*if((document.cookie.indexOf(escape("err")) != -1) && (document.cookie.indexOf(escape("vauth")) == -1))
	{
		get_error_form();	
	} */

}

function redirect_users(utype,uname) {
	
	if(utype == 210) {	
		var url = '/performer/mypage.shtml';
		window.location.href=url;
	} else if (utype == 205 || utype == 206 || utype == 207) {
		get_user_info(uname);
	} 
}

function get_login_form() {
	document.write('<form method="post" action="/fast-cgi/authenticate.fcgi">');
  document.write('<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">');
	
	if((document.cookie.indexOf(escape("err")) != -1) && (document.cookie.indexOf(escape("vauth")) == -1))
	{
		get_error_form();	
	}

  document.write('<tr>');
	var auth_cookie = get_cookie('vauth');
	var auth_arr =  new Array('', '', '');
	if(auth_cookie) {
	 auth_arr = auth_cookie.split("-");
	}
  //RIE USERNAME FOR VIEWERS
  document.write('<td colspan="2">ユーザーネーム：</td></tr><tr><td colspan="2"><input type=text name="username" value="' + auth_arr[0] + '" size="15" maxlength="60"></td></tr><tr>');
  document.write('<td colspan="2">パスワード：</td></tr><tr><td colspan="2"><input type=password name="password" value="' + auth_arr[1] + '" size="15" maxlength="60"></td></tr><tr>');
  document.write('<td>');
	document.write('<input type="checkbox" name="remember"> パスワードを記憶する </td></tr>');
	document.write('<tr><td>');
	document.write('<input type="submit" name="password222" value="ログイン"></td></tr></table></form>'); 

  //document.write('<td align="right" class="sss">ユーザーネーム</td>');
  //document.write('<td align="right"> <input type=text name="username" size="10" maxlength="60"></td>');
  //document.write('</tr><tr>');
  //document.write('<td align="right" class="sss">パスワード</td>');
  //document.write('<td width="50" align="right"><input type=password name="password" size="10" maxlength="60"></td>');
  //document.write('</tr><tr>');
  //document.write('<td class="sss">&nbsp;</td>');
  //document.write('<td align="right"> <input type="submit" name="password222" value="ログイン"></td>');
  //document.write('</tr></table></form>');
  
}

function get_perf_login_form() {
 	document.write('<table width="420" border="0">');
 	document.write('<tr>');
	document.write('<td width="200" height="32">');
	document.write('<form method="post" action="/fast-cgi/authenticate.fcgi">');
	document.write('<table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">');
	document.write('<tr>'); 
	document.write('<td width="105" height="33" align="right" class="ss">');

	// RIE CHANGE ID TO THE JAPANESE TEXT FOR USERNAME
	document.write('ユーザーネーム');
	// rie end change 

	document.write('</td>');
	document.write('<td align="right"> <div align="left">'); 
	document.write('<input name="username" size="10" maxlength="120">');
	document.write('</div></td>');
	document.write('</tr><tr>');
	document.write('<td align="right" class="ss">パスワード</td>');
	document.write('<td width="109" align="right"> <div align="left">');
	document.write('<input type=password name="password" size="10" maxlength="120">');
	document.write('</div></td>');
	document.write('</tr><tr>');
	document.write('<td class="ss">&nbsp;</td>');
	document.write('<td align="right"><div align="left">'); 
	document.write('<input type="submit" name="password222" value="GO">');
	document.write('</div></td>');
	document.write('</tr>');
	document.write('</table></form></td>');
	document.write('<td width="181"><form method="post" action="/fast-cgi/authenticate.fcgi">');
	document.write('<table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">');
	document.write('<tr>'); 
	document.write('<td width="105" height="33" align="right" class="ss">USER NAME</td>');
	document.write('<td align="right"> <div align="left">'); 
	document.write('<input name="username" size="10" maxlength="120">');
	document.write('</div></td>');
	document.write('</tr><tr>');
	document.write('<td align="right" class="ss">PASSWORD</td>');
	document.write('<td width="109" align="right"> <div align="left">');
	document.write('<input type=password name="password" size="10" maxlength="120">');
	document.write('</div></td>');
	document.write('</tr><tr>');
	document.write('<td class="ss">&nbsp;</td>');
	document.write('<td align="right"><div align="left">'); 
	document.write('<input type="submit" name="password223" value="GO">');
	document.write('</div></td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</form></td></tr></table>');

}

function get_user_info(username) {
	
	var active_user = get_user();
	document.write('<table width="100%" border="0" align="left" cellpadding="0" cellspacing="1">');
  document.write('<tr>');
  document.write('<td width="77" height="17" align="left" class="ss"> ユーザーネーム');
	document.write('<br> [ ');
	document.write(username);
  document.write(' ] </td>');
  document.write('</tr>');
	document.write('<td width="77" height="17" align="left" class="ss">現在の残高 ');
	document.write('<br> [ ');
	document.write(active_user.balance);
	document.write(' ]</td></tr>');
	document.write('<tr><td width="77" height="17" align="left" class="ss">');
        //document.write('<br><a href="#" onClick="deleteCookie();">ログアウト</a>');
	document.write('<input type="image" align="bottom" src="../../images/buttons/logout.jpg" name="submit" onClick="deleteCookie();">');
        document.write('</td>');
	document.write('</tr></table>');
 
}

function deleteCookie()
{
	var name = 'vauth';
	var result = get_cookie(name);
  
	var curCookie = name + "=" + //escape(result) +
		// ((path == null) ? "" : "; path=" + path) +
		//((domain == null) ? "" : "; domain=" + domain) +
		//"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		"; expires=Thu, 01-Jan-70 00:00:01 GMT" + 
		//"; path=" + "" +
		"; domain=" + ".exshot.com"; 
		//"; secure" + "";
	document.cookie = curCookie;
//	document.write('cookie is ');
//	document.write(curCookie);
	window.location.href="/home.shtml";
   
}

function deletePerfCookie()
{
	var name = 'vauth';
	var result = get_cookie(name);
  
	var curCookie = name + "=" + //escape(result) +
		// ((path == null) ? "" : "; path=" + path) +
		//((domain == null) ? "" : "; domain=" + domain) +
		//"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		"; expires=Thu, 01-Jan-70 00:00:01 GMT" + 
		"; path=" + "/" +
		"; domain=" + ".exshot.com"; 
		//"; secure" + "";
	document.cookie = curCookie;
//	document.write('cookie is ');
//	document.write(curCookie);
	window.location.href="/home.shtml";
   
}


function get_cookie(Name)
{
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) { 
			offset += search.length
		// set index of beginning of value
		end = document.cookie.indexOf(";", offset);
		// set index of end of cookie value
		if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function get_error_form() {

 // document.write('<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">');
  //MASA OR RIE ERROR MESSAGE 02122004
	document.write('<tr>');
	document.write('<td align="left"><font color="red">ユーザーネーム又はパスワードが間違っています。<br>もう一度正しいアカウントを入力してください。</font></td>');
	document.write('</tr>');//</table>');
}


