
/* * 2008.6.9 saito 追加ここから */
//マウスオーバーでランダムにイメージを変更 (pages/eco/room,)
	function swimg(imgname,iName,str){
			imgSrc = str;
			if (!str) imgSrc = "/insertion-pc/html/insertion/pages/eco/img/"+imgname+Math.floor(Math.random()*4 + 1)+".gif";
			document.images[iName].src = imgSrc;
		}
/* * 2008.6.9 saito 追加ここまで */


/* * 2008.5.22 saito 追加ここから */
//マウスオーバーでテキストを表示(pages/pointrally/pr080524/tame) 
	function dispTF(txt)
	{
		document.myFORM.myFLD.value = txt;
	}
/* * 2008.5.22 saito 追加ここま */


/* * 2008.4.15 saito 追加ここから */
//マウスオーバーでイメージを変更(pages/eco,pointrally)
	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];}
	}
/* * 2008.4.15 saito 追加ここまで */



/*
 * イメージを元に戻す
 */
function restoreImage(element) {
	if (element.oSrc) {
		element.src = element.oSrc;
	}
}

/*
 * イメージを変更する
 */
function swapImage(element, url) {
	if (!element.oSrc) {
		element.oSrc = element.src;
	}
	element.src = url;
}

/*
 * Form内のすべてのチェックボックスをチェック（解除）する
 * form: FORM  flag: ture=チェック, false=解除
 * name: 指定された場合、名前の先頭が一致したものだけを対象とする
 */
function checkAll(form, flag, name) {
	var i;
	for (i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == "checkbox") {
			if (name == null || form.elements[i].name.indexOf(name) == 0) {
				form.elements[i].checked = flag;
			}
		}
	}
}

/*
 * タグの属性を生成する。
 * attributes: タグに指定する属性
 */
function createAttributes(attributes) {
	attributes = $H(attributes || {}).map(function(pair){
		return pair.key+'="'+pair.value + '"';
	});
	return attributes.join(' ');
}

/*
 * タグを生成する。
 * tagName: タグの名前  innerHTML: タグの中のHTML  attributes: タグに指定する属性
 */
function createTag(tagName, innerHTML, attributes) {
	var tag = '';
	tag += '<' + tagName;
	tag += ' ' + createAttributes(attributes);
	tag += '>';
	tag += (innerHTML || '');
	tag += '</' + tagName + '>';
	return tag;
}

/*
 * テーブルタグを生成する。
 * cells: セルの中身  cols: 列数
 * tableAttrs, trAttrs, tdAttrs: TABLE,TR,TDタグに指定する属性
 */
function createTableHTML(cells, cols, tableAttrs, trAttrs, tdAttrs) {
	if (cells == null || cells.length == 0) {
		return '';
	}
	if (cols < 1) {
		cols = 1;
	}

	var i;

	cells = $A(cells).map(function(cell){ return createTag('td', cell, tdAttrs); });
	if (cells.length % cols != 0) {
		for (i = cells.length % cols; i < cols; i++) {
			cells.push(createTag('td', null, tdAttrs));
		}
	}

	var rows = new Array();
	for (i = 0; i < cells.length; i += cols) {
		rows.push(createTag('tr', cells.slice(i, i+cols).join(''), trAttrs));
	}

	return createTag('table', rows.join(''), tableAttrs);
}

/*
 * テーブルタグをヘッダつきで生成する。
 * headers: ヘッダの中身  cells: セルの中身
 * tableAttrs, trAttrs, thAttrs, tdAttrs: TABLE,TR,TH, TDタグに指定する属性
 */
function createTableWithHeaderHTML(headers, cells, tableAttrs, trAttrs, thAttrs, tdAttrs) {
	if (headers == null || headers.length == 0) {
		return '';
	}

	var i;
	var cols = headers.length;

	headers = $A(headers).map(function(head){ return createTag('th', head, thAttrs); });
	cells = $A(cells).map(function(cell){ return createTag('td', cell, tdAttrs); });
	if (cells.length > 0 && cells.length % cols != 0) {
		for (i = cells.length % cols; i < cols; i++) {
			cells.push(createTag('td', null, tdAttrs));
		}
	}

	var rows = new Array();
	rows.push(createTag('tr', headers.join(''), trAttrs));
	for (i = 0; i < cells.length; i += cols) {
		rows.push(createTag('tr', cells.slice(i, i+cols).join(''), trAttrs));
	}

	return createTag('table', rows.join(''), tableAttrs);
}

/*
 * チェックボックスタグを生成する。
 */
function createCheckboxHTML(name, label, value, checked, attributes) {
	var allAttributes = {};
	Object.extend(allAttributes, attributes);
	Object.extend(allAttributes, {'type':'checkbox', 'id': name, 'name': name, 'value': value});

	var html = '<input ';
	html += createAttributes(allAttributes)
	if (checked) {
		html += ' checked';
	}
	html += '>';
	html += ' ';
	html += createTag('label', label, {'for': name});
	return html;
}

/*
 * オプションを変更する。
 * element: 変更対象のSELECT要素  options: value,labelを属性にもつ値の配列  selected:選択値
 */
function changeOptions(element, options, selected) {
	if (options == null || options.length == 0) {
		element.length = 1;
		element.options[0].value = '';
		element.options[0].text  = '';
		element.selectedIndex = 0;
	} else {
		element.length = options.length;
		var i;
		var selectedIndex = 0;
		for (i = 0; i < options.length; i++) {
			if (selected != null && options[i].value == selected) {
				selectedIndex = i;
			}
			element.options[i].value = options[i].value || '';
			element.options[i].text  = options[i].label || '';
		}
		element.selectedIndex = selectedIndex;
	}
}

/*
 * XMLでオプションを変更する。
 * element: 変更対象のSELECT要素  xmlDocs: XML
 * ※XMLはoptionという要素を持ち、option要素にはvalue,label属性が指定されているものとする
 */
function changeOptionsWithXML(element, xmlDoc) {
	var options = new Array();
	if (xmlDoc != null) {
		options = $A(xmlDoc.getElementsByTagName('option'));
		options = xmlToHash(options, ['value', 'label']);
	}
	changeOptions(element, options, null);
}

function xmlToHash(elements, attributes) {
	elements = $A(elements || {});
	attributes = $A(attributes || {});

	return elements.map(function(e) {
		var hash = new Array();
		attributes.each(function(a) {
			if (e.getAttribute(a) != null) {
				hash[a] = e.getAttribute(a);
			}
		});
		hash = $H(hash);
		return hash;
	});
}
