myN6 = document.getElementById; // ブラウザ(N6)のチェック用
myIE = document.all;            // ブラウザ(IE)のチェック用
myN4 = document.layers;         // ブラウザ(N4)のチェック用
var url = window.location.href;
var orgWidth = 80;
var numBottles = 0;
var bottlesArray = new Array();
var filename;
var curWidth = 0;
window.onresize = resizeBottles;
window.onload = init;
pos1 = url.lastIndexOf("/");
pos2 = url.indexOf("?");
if( pos2 != -1)
{
	filename = url.substr(pos1+1, pos2-pos1-1);
}
else
{
	filename = url.substr(pos1+1);
}

function init()
{
	for( i = 0;i < 4;i++)
	{
		bottlesArray[i] = new bottle( "", -1);
	}
	cursel=MM_findObj("cursel");
	for( i=0;i < cursel.length;i++)
	{
		if( parseInt(cursel[i].value) == 1)
		{
			cursel[i].checked = true;
		}
	}
}

function listItem( src, catalog, title, subtitle, code, price, href)
{
	this.src = src;
	this.catalog = catalog;
	this.title = title;
	this.subtitle = subtitle;
	this.code = code;
	this.price = price;
	this.href = href;
}
function extractElement() {
	list = arguments[0];
	if(arguments.length == 1)
	{
		num = 10000;	// try large number
	}
	else
	{
		num = arguments[1];
	}
	if(arguments.length == 3)
	{
		sep = arguments[2];
	}
	else
	{
		sep = ",";	// default separator
	}
	result = new Array();
	var first = 0;
	var last = 0;
	if( !list.length )
	{
		return result;
	}
	for (var i = 0; i < num; i ++) {
		first = (i == 0) ? 0 : last+1;
		nextIndex = list.indexOf(sep,first);
		last = (nextIndex == -1) ? list.length : nextIndex;
		result[i] = list.substring(first,last);
		if(nextIndex == -1)
		{
			i++;
			break;
		}
	}
//	this.length = i;
	return result;
}
function initListTable(db)
{
	var i = 0;

	while( db.indexOf("*") > 0 )
	{
		pos = db.indexOf("*");
		temp = extractElement( db.substring( 0, pos), 7, "|" );
		this[i] = new listItem( temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6] );
		db = db.substring( pos+1, db.length );
		i++;
	}
	this.length = i;
//	alert( "Array length =" + this.length );
//	alert( "Array[last].check =" + this[this.length-1].check );
}

function bottle( name, code)
{
	this.name = name;
	this.code = code;
}
function drawBottle(src, catalog, title, subtitle, code, href)
{
	name = catalog + " - " + title;
	s = '<a href="#" onclick="return clickAction(\'' + name + '\', \'' + code + '\',\'' + href + '\');">';
	s += '<img border="0" src="' + src + '" ';
	s += 'alt="' + title + ' - ' + subtitle + '" ';
	s += 'title="' + title + ' - ' + subtitle + '"';
	s += ' width="' + curWidth + '"';
	s += '></a> ';
	return(s);
}
function MM_findObj(n, d) { //v3.0
  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); return x;
}
function clickAction(name, code, href)
{
	cursel = 0;
	curselobj=MM_findObj("cursel");
	for( j=0;j < curselobj.length;j++)
	{
		if( curselobj[j].checked )
		{
			cursel = parseInt(curselobj[j].value);
			break;
		}
	}
//alert(cursel);
	if( 1 <= cursel && cursel <= 4 )
	{
		curimg=MM_findObj("bottle" + cursel);
		re = /\d+/i;
		pos = code.search(re);
		for( i=0;i < 4;i++)
		{
			if(bottlesArray[i].code == parseInt(code.substr(pos),10))
			{
				alert(msg4);
				return(false);
			}
		}
		curimg.src = "http://aura-soma.co.jp/products/images/equi/M2/B" + PadtoString( parseInt(code.substr(pos),10), 3,"0") + ".jpg";
		bottlesArray[cursel-1] = new bottle( name, parseInt(code.substr(pos),10));
		nextsel=MM_findObj("cursel");
		if( cursel <= 3 )
		{
			for( j=0;j < nextsel.length;j++)
			{
				if( parseInt(nextsel[j].value) == (cursel + 1))
				{
					nextsel[j].checked = true;
				}
			}
		}
		else if(cursel == 4 )
		{
			for( j=0;j < nextsel.length;j++)
			{
				if( parseInt(nextsel[j].value) == cursel )
				{
					nextsel[j].checked = false;
				}
			}
		}
	}
	return(false);
}

function reading()
{
	j = 0;
	for( var i = 0; i < 4; i++)
	{
		if( bottlesArray[i].name != "")
		{
			j++;
		}
	}
	if( j != 4 )
	{
		alert(msg1);
	}
	else
	{
		url = "";
		str = msg2;
		for( var i = 0; i < 4; i++)
		{
			str += (i+1) + msg3 + bottlesArray[i].name + "\n";
			if( i == 0)
			{
				url += "B" + PadtoString( bottlesArray[i].code, 3, "0" );
			}
			else
			{
				url += "-B" + PadtoString( bottlesArray[i].code, 3, "0" );
			}
		}
		if( confirm(str) )
		{
			url = "reading-" + url + ".html";
			window.location.href = url;
		//	window.open( url, "_blank");
		}
		return(false);
	}
}

function changeSize( dir )
{
	width = 0;
	width = ParseQuery( url, "width" );
	if( width == null )	{	width = curWidth;	}
	else 	{	width = eval(width); }
	if( dir == "0" ) { width = orgWidth; }
	else if( dir == "++" ) { width += 10; }
	else if( dir == "+" )	{ width += 5; }
	else if( dir == "-" ) { width -= 5; }
	else if( dir == "--" ) { width -= 10; }
	window.location = filename + "?width=" + width;
	return(false);
}
function fitSize()
{
	if( document.all )	// For IE 5 or higher
	{
		if( navigator.userAgent.toLowerCase().indexOf('opera') != -1)
		{
//alert("opera")
			winHeight = window.innerHeight;
			winWidth = window.innerWidth;
		}
		else
		{
//alert("IE")
			winHeight = document.documentElement.offsetHeight;
			winWidth = document.documentElement.offsetWidth;
		}
	}
	else if(document.getElementById)	// For Netscape 6 or higher
	{
//alert("NS")
		winHeight = window.innerHeight;
		winWidth = window.innerWidth;
	}
	else if(document.layers)	// For Netscape 4.X. 
	{
//alert("NS4")
		winHeight = 600;
		winWidth = 800;
	}
//alert(winWidth + "," + winHeight);
	windowArea = winWidth * 0.80 * (winHeight - 150);
//alert(windowArea);
	width = Math.floor(Math.sqrt(windowArea / 1.6 / bottleList.length));
//	width = Math.floor(Math.sqrt(windowArea / 1.675 / bottleList.length));
//alert(width);
//	window.location = filename + "?width=" + width;
	return(width);
}
function resizeBottles()
{
	width = ParseQuery( url, "width" )
	if( width != null )
	{
		curWidth = width;
	}
	else
	{
		curWidth = fitSize();
		for( i = 0; i < document.images.length; i++)
		{
			if((document.images[i].src).indexOf("/products/images/equi/M/") != -1 )
			{
				document.images[i].width = curWidth;
			}
		}
	}
}
