/* ========================================================================
   << Library for Storing Form Status with Cookie  >>

   Author : Kiyofumi Nakaishi
   Type   : library 
   History:
   1.0   : 11/14/2003 original
   
   Copyright(C) 2003 NCSoft. All rights reserved.
________________________________________________________________________ */

/***********************************************************************
導入時用メモ
formname(フォームの名前)
ckName(そのクッキーの名前)
は任意に変更して下さい。

<script language="JavaScript" src="<?=$url_base.$url_url["js_lib"]?>FormCookie.js"></script>
:
:

<BODY  onLoad="setupForm(document.formname)">
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"><!--
var today = new Date();
var expires = new Date(today.getTime() + (56 * 86400000));
var ckName=file_name_only(location.pathname);
var ConfigCookie = Get_Cookie(ckName);
//--></SCRIPT>

:
:

<FORM name="formname">
....
<input type=button value="クッキーに書き込み" onClick="Set_Form_Cookie(this.form,ckName)">
</FORM>

***********************************************************************/
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));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function setupForm(form) {
    if (ConfigCookie) getValues(form,ConfigCookie);
}

function getValues(form,string) {
	var cnt;
	var el;
//	alert(string);
	if (!form.elements)return;
	for (cnt = 0;cnt < form.elements.length;cnt++){
		el=form.elements[cnt];
		if (el.name.length<=0)continue;
		if (
			el.type=="text" 
		||  el.type=="select-one"
		||	el.type=="checkbox"
		|| el.type=="radio"
		){
    getValue(string,el.name,  el,   el.type);
		}
	}
}

function replace(string,text,by) {
// Replaces text with by in string
    var i = string.indexOf(text);
    var newstr = '';
    if ((!i) || (i == -1)) return string;
    newstr += string.substring(0,i) + by;

    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);
    
    return newstr;
}

function onCheck(string) { if (string.length> 0/*string == "on"*/) return true; return false; }

function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType

    var startPos = string.indexOf(elementName + "=")
    
    if (startPos > -1) {
        startPos = startPos + elementName.length + 1;
        var endPos = string.indexOf("&",startPos);
        if (endPos == -1) endPos = string.length;

//        var elementValue = unescape(string.substring(startPos,endPos));
        var elementValue = (string.substring(startPos,endPos));
		//if (elementValue.length <= 0) {return;}
        if (elementType == "text")     object.value = elementValue;
        else if (elementType == "password") object.value = elementValue;
        else if (elementType == "select-one" )object.value = elementValue;//   object.selectedIndex = elementValue;
        else if (elementType == "checkbox") object.checked = onCheck(elementValue);
        else if (elementType == "radio" && object.value==elementValue)object.checked = true;
    }
}

function Set_Form_Cookie(form,CookieName){
//	var form=document.srch;
	var tmp_array=new Array();
	var tmp_str;
	var cnt;
	var el;
	for (cnt = 0;cnt < form.elements.length;cnt++){
		el=form.elements[cnt];
		if (
			el.type=="text" 
		||  el.type=="select-one"
		){
			tmp_str=el.name+'='+el.value;
			tmp_array.push(tmp_str);

		}else if (
			el.type=="checkbox"
			|| el.type=="radio"
		){
			if (el.checked){
			tmp_str=el.name+'='+el.value;
			tmp_array.push(tmp_str);
			}
		}
	}
	var	CookieValue=tmp_array.join("&");
    CookieValue = replace(CookieValue,"+"," ");
	if (CookieValue.length > 0){
		Set_Cookie(CookieName,CookieValue,expires);
	}
}
function file_name_only(str) { 
	return str.substring(str.lastIndexOf('/') + 1, str.lastIndexOf('.')) 
}

