/*
	check browser , OS
*/
var isNav = (navigator.appName == "Netscape");
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
var isW3C = (document.getElementById) ? true : false;
var isWin = (navigator.userAgent.indexOf("Win") != -1);
var isMac = (navigator.userAgent.indexOf("Mac") != -1);
var isUnix = (navigator.userAgent.indexOf("X11") != -1);

/* 
	cookieEnabledCheck
*/
function chkCookieEnabled(){
	var cookieEnabled=false;
	if(typeof document.cookie=="string") {
		if(document.cookie.length==0) {
			document.cookie="test";
			cookieEnabled=(document.cookie=="test");
			document.cookie="";
		} else {
			cookieEnabled=true;
		}
	}
	return cookieEnabled;
}

/*
	cookies.js
	Example File From "JavaScript and DHTML Cookbook"
	Published by O'Reilly & Associates
	Copyright 2003 Danny Goodman
*/

// utility function to retrieve a future expiration date in proper format;
// pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire; all three
// parameters required, so use zeros where appropriate

function getExpDate(days,hours,minutes) {
	var expDate=new Date();
	if(typeof days == "number" && typeof hours == "number" && typeof Minutes == "number") {
		expDate.setDate(expDate.getDate()+parseInt(days));
		expDate.setHours(expDate.getHours()+parseInt(hours));
		expDate.setMinutes(expDate.getMinutes()+parseInt(minutes));
		return expDate.toGMTString();
	}
}

// utility function called by getCookie()
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf(";",offset);
	if(endstr==-1) {
		endstr=document.cookie.length;
	}
	return unescape(document.cookie.substring(offset,endstr));
}

// primary function to retrieve cookie by name
function getCookie(name) {
	var arg =name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while(i<clen) {
		var j=i+alen;
		if(document.cookie.substring(i,j)==arg) {
			return getCookieVal(j);
		}
		i=document.cookie.indexOf(" ",i)+1;
		if(i==0) break; 
	}
	return null;
}

// store cookie value with optional details as needed
function setCookie(name,value,expires,path,domain,secure) {
	document.cookie=name+"="+escape (value)+
		((expires) ? "; expires="+expires : "") +
		((path) ? "; path="+path : "") +
		((domain) ? "; domain="+domain : "") +
		((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
	if(getCookie(name)) {
		document.cookie = name+"=" +
			((path) ? "; path="+path : "") +
			((domain) ? "; domain="+domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/*
	自前の乱数
*/
var	ixInit=0;
function ranInit(ix){
	ixInit=ix;
	return;
}
function Rand(){
	var	mm=1664501;
	var	ia=1229,ic=351750;
	var	val=0.0,aa=0.0;

	ixInit=(ia*ixInit+ic)%mm;
	aa=1.0/mm;
	val=ixInit*aa;
	return val;
}
/* 今日を初期値とするn1〜n2間の乱数生成 */
function dayRand2(n1,n2){
//	var n1=1,n2=105;		min & max # 
	var now;
	var iy=0,im=0,id=0,ix;
	var val=0;

	now=new Date();
	iy=now.getYear();	im=now.getMonth();
	id=now.getDate();
	ix=(im+1+id*100)*1000;
	//val=Math.floor(Math.random()*(n2-n1+1))+n1;	//js-ver.1.1
	ranInit(ix);
	val=Math.floor(Rand()*(n2-n1+1))+n1;
	now=null;
	return val;
}
/*
	シェルソート関数 index付き
	シェルソートとソート元インデックス試験
	ソート前index情報初期化
*/
var stIndx;		/* ソート前のインデックス位置情報 */

function set_arrayindexInit(stindx){
	var i;

	for(i=0;i<stindx.length;i++){
		stindx[i]=i;
	}

	return;
}
/* ソート後のデータindex情報 */
function GetArrayIndex(indx) {
	var i,n;

	for(i=0,n=0;i<stIndx.length;i++){
		indx[n]=stIndx[i];	n++;
	}
	return n;
}

function swap2(sarray,indx1,indx2){
	var wstr;
	wstr=sarray[indx1];
	sarray[indx1]=sarray[indx2];
	sarray[indx2]=wstr;
}
function swap2_indx(sindxs,indx1,indx2){
	var n;
	n=sindxs[indx1];
	sindxs[indx1]=sindxs[indx2];
	sindxs[indx2]=n;
}
/* shell ソート */
function ShellSort(sarray,comparator){
	var forward,indx,start,pend;
	var hop=0;

	// 配列前index情報
	stIndx=new Array(sarray.length);
	set_arrayindexInit(stIndx);
	// shell sort
	start=0;	pend=sarray.length;
	hop=Math.floor((pend-start)/2);		//強制的に整数(実数処理される)
	forward=start+hop;		indx=start;
	// pivot 基準に左と右のグループ振り分け
	str="";
	while(hop>0){
		forward=start+hop;
		while(forward<pend){
			indx=forward-hop;
			while(comparator(sarray[indx],sarray[indx+hop])>0){
				// hop幅離れた要素を入れ替える
				swap2(sarray,indx,indx+hop);
				swap2_indx(stIndx,indx,indx+hop);
				// 次のhop幅離れた要素
				if(indx-hop>= 0) {
					indx-=hop;
				} else {
					break;
				}
			}
			forward++;
		}
		// pop幅を半分にする
		hop/=2;
		hop=Math.floor(hop);	// 強制的に整数.さもないと0.625等となる
	}
	return 0;
}
// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
	var str= elem.value;
	var re = /.+/;
	if(!str.match(re)) {
		alert("Please fill in the required field.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
//validates that the entry is a positive or negative number
function isNumber(elem) {
	var str= elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str=str.toString();
	if(!str.match(re)) {
		alert("Enter only numbers into the field.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	}
	return true;
}
// validates that the entry is 16 characters long
function isLen16(elem) {
	var str= elem.value;
	var re = /\b.{16}\b/;
	if(!str.match(re)) {
		alert("Entry does not contain the required 16 characters.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
	var str= elem.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if(!str.match(re)) {
		alert("Verify the e-mail address format.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
// validate that the user made a selection other than default
function isChosen(select) {
	if(select.selectedIndex == 0) {
		alert("Please make a choice from the list.");
		return false;
	} else {
		return true;
	}
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
	var valid=false;
	for(var i=0;i<radio.length;i++) {
		if(radio[i].checked) {
			return true;
		}
	}
	alert("Make a choice from the radio buttons.");
	return false;
}

function focusElement(formName, elemName) {
	var elem=document.forms[formName].elements[elemName];
	elem.focus();
	elem.select();
}

// access counter URL and XML file name
function get_acc_url(opt)
{
	if(opt==1)	return "あなたは初めての訪問者です。";
	return "http://face.u-aizu.ac.jp/counter/count.cgi?cf=121971/iskw|it=K";
}
function get_xml_fnm(inum)
{
	return "haiku"+inum+".xml";
}
function putSessionValues(opt,flg) {	/* opt=1:for test   flg=1:for second page */
	var cval,str,url2;
	url2=get_acc_url(opt);
	if(opt==1)	str=url2;
	else		str="あなたは<img src=\""+url2+"\"/>人目の訪問者です。";
	if(navigator.cookieEnabled){
		cval=(getCookie("count1")) ? getCookie("count1"):"";
		if(cval){
			if(opt==1)	str="あなたは"+cval+" 人目の訪問者です。";
			else		str="ご訪問ありがとうございます。";
			if(!flg)	document.getElementById("counter").innerHTML=str;
		} else {
			setCookie("count1","2",getExpDate(0,1,0));
			document.getElementById("counter").innerHTML=str;
		}
	} else {
		document.getElementById("counter").innerHTML=str;
	}
}

