var xmlHttpReq           = null;
var xmlHttpReqLeftStep2  = null;
var xmlHttpReqRightStep2 = null;
var xmlHttpReqUpStep2    = null;
var xmlHttpReqDownStep2  = null;
var xmlHttpReqLeftStep3  = null;
var xmlHttpReqRightStep3 = null;
var xmlHttpReqUpStep3    = null;
var xmlHttpReqDownStep3  = null;
var isIE                 = true;



function clearOptionValue(selectObj)
{
	while (selectObj.firstChild != undefined) {
		selectObj.removeChild(selectObj.firstChild);
	}
}



function loadCompleteStep3(xmlHttpReq, kubun, prevValue)
{
	var leftEyeStep3Obj  = document.getElementById('left_eye_step3');
	var rightEyeStep3Obj = document.getElementById('right_eye_step3');
	var careAStep3Obj    = document.getElementById('care_A_step3');
	var careBStep3Obj    = document.getElementById('care_B_step3');
	var optionObj        = null;
	var i                = 0;
	var resStrArray      = new Array();
	var resStr           = xmlHttpReq.responseText;
	var cnt              = 0;


	if (kubun == 'left') {
		clearOptionValue(leftEyeStep3Obj);
	} else if (kubun == 'right') {
		clearOptionValue(rightEyeStep3Obj);
	} else if (kubun == 'up') {
		clearOptionValue(careAStep3Obj);
	} else if (kubun == 'down') {
		clearOptionValue(careBStep3Obj);
	}
	
	optionObj       = document.createElement("option");
	optionObj.value = '';
	optionObj.text  = '箱数';
	if (isIE) {
		if (kubun == 'left') {
			leftEyeStep3Obj.add(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep3Obj.add(optionObj);
		} else if (kubun == 'up') {
			careAStep3Obj.add(optionObj);
		} else if (kubun == 'down') {
			careBStep3Obj.add(optionObj);
		}
		
	} else {
		if (kubun == 'left') {
			leftEyeStep3Obj.appendChild(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep3Obj.appendChild(optionObj);
		} else if (kubun == 'up') {
			careAStep3Obj.appendChild(optionObj);
		} else if (kubun == 'down') {
			careBStep3Obj.appendChild(optionObj);
		}
	}
	
	if (resStr.length > 0) {
		
		eval("resStrArray = " + resStr);
		
		if (resStrArray[0][0] != '') {
			cnt = resStrArray[0][0];
		}

		//for (i = 1; i <= parseInt(cnt); i++) {
		for (i = 1; i <= 10; i++) {
			optionObj       = document.createElement("option");
			optionObj.value = i;
			optionObj.text  = i + '箱';
			
			if (isIE) {
				if (kubun == 'left') {
					leftEyeStep3Obj.add(optionObj);
				} else if (kubun == 'right') {
					rightEyeStep3Obj.add(optionObj);
				} else if (kubun == 'up') {
					careAStep3Obj.add(optionObj);
				} else if (kubun == 'down') {
					careBStep3Obj.add(optionObj);
				}
				
			} else {
				if (kubun == 'left') {
					leftEyeStep3Obj.appendChild(optionObj);
				} else if (kubun == 'right') {
					rightEyeStep3Obj.appendChild(optionObj);
				} else if (kubun == 'up') {
					careAStep3Obj.appendChild(optionObj);
				} else if (kubun == 'down') {
					careBStep3Obj.appendChild(optionObj);
				}
			}
			
			if (prevValue == i) {
				optionObj.selected = true;
			}
			
		}
		
	}

	return 0;
}



function getStep3(kubun, pageStatus, prevStep2Value, prevStep3Value)
{
	var step2Value = '';
	

	if (pageStatus == 'current') {
		if (kubun == 'left') {
			step2Value = document.getElementById('left_eye_step2').value;
		} else if (kubun == 'right') {
			step2Value = document.getElementById('right_eye_step2').value;
		} else if (kubun == 'up') {
			step2Value = document.getElementById('care_A_step2').value;
		} else if (kubun == 'down') {
			step2Value = document.getElementById('care_B_step2').value;
		}
	} else {
		step2Value = prevStep2Value;
	}

	if (pageStatus == 'current') {
		xmlHttpReq.abort();
		xmlHttpReq.open("GET", "get_goods.php?status=step3&step2=" + step2Value, true);
		xmlHttpReq.onreadystatechange = function()
										{
											if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
												loadCompleteStep3(xmlHttpReq, kubun, '');
											}
										}
		xmlHttpReq.send(null);
	
	} else {
		if (kubun == 'left') {
			xmlHttpReqLeftStep3.abort();
			xmlHttpReqLeftStep3.open("GET", "get_goods.php?status=step3&step2=" + step2Value, true);
			xmlHttpReqLeftStep3.onreadystatechange = function()
												{
													if (xmlHttpReqLeftStep3.readyState == 4 && xmlHttpReqLeftStep3.status == 200) {
														loadCompleteStep3(xmlHttpReqLeftStep3, kubun, prevStep3Value);
													}
												}
			xmlHttpReqLeftStep3.send(null);
		} else if (kubun == 'right') {
			xmlHttpReqRightStep3.abort();
			xmlHttpReqRightStep3.open("GET", "get_goods.php?status=step3&step2=" + step2Value, true);
			xmlHttpReqRightStep3.onreadystatechange = function()
												{
													if (xmlHttpReqRightStep3.readyState == 4 && xmlHttpReqRightStep3.status == 200) {
														loadCompleteStep3(xmlHttpReqRightStep3, kubun, prevStep3Value);
													}
												}
			xmlHttpReqRightStep3.send(null);
		} else if (kubun == 'up') {
			xmlHttpReqUpStep3.abort();
			xmlHttpReqUpStep3.open("GET", "get_goods.php?status=step3&step2=" + step2Value, true);
			xmlHttpReqUpStep3.onreadystatechange = function()
												{
													if (xmlHttpReqUpStep3.readyState == 4 && xmlHttpReqUpStep3.status == 200) {
														loadCompleteStep3(xmlHttpReqUpStep3, kubun, prevStep3Value);
													}
												}
			xmlHttpReqUpStep3.send(null);
		} else if (kubun == 'down') {
			xmlHttpReqDownStep3.abort();
			xmlHttpReqDownStep3.open("GET", "get_goods.php?status=step3&step2=" + step2Value, true);
			xmlHttpReqDownStep3.onreadystatechange = function()
												{
													if (xmlHttpReqDownStep3.readyState == 4 && xmlHttpReqDownStep3.status == 200) {
														loadCompleteStep3(xmlHttpReqDownStep3, kubun, prevStep3Value);
													}
												}
			xmlHttpReqDownStep3.send(null);
		}
	}
	
}



function loadCompleteStep2(xmlHttpReq, kubun, prevValue)
{
	var leftEyeStep2Obj  = document.getElementById('left_eye_step2');
	var rightEyeStep2Obj = document.getElementById('right_eye_step2');
	var leftEyeStep3Obj  = document.getElementById('left_eye_step3');
	var rightEyeStep3Obj = document.getElementById('right_eye_step3');
	
	var careAStep2Obj    = document.getElementById('care_A_step2');
	var careBStep2Obj    = document.getElementById('care_B_step2');
	var careAStep3Obj    = document.getElementById('care_A_step3');
	var careBStep3Obj    = document.getElementById('care_B_step3');
	
	var optionObj        = null;
	var i                = 0;
	var resStrArray      = new Array();
	var resStr           = xmlHttpReq.responseText; 


	/*----------------------------------------------------------*/
	if (kubun == 'left') {
		clearOptionValue(leftEyeStep2Obj);
	} else if (kubun == 'right') {
		clearOptionValue(rightEyeStep2Obj);
	} else if (kubun == 'up') {
		clearOptionValue(careAStep2Obj);
	} else if (kubun == 'down') {
		clearOptionValue(careBStep2Obj);
	}
	
	optionObj       = document.createElement("option");
	optionObj.value = '';
	optionObj.text  = '商品を選択してください。';
	if (isIE) {
		if (kubun == 'left') {
			leftEyeStep2Obj.add(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep2Obj.add(optionObj);
		} else if (kubun == 'up') {
			careAStep2Obj.add(optionObj);
		} else if (kubun == 'down') {
			careBStep2Obj.add(optionObj);
		}
		
	} else {
		if (kubun == 'left') {
			leftEyeStep2Obj.appendChild(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep2Obj.appendChild(optionObj);
		} else if (kubun == 'up') {
			careAStep2Obj.appendChild(optionObj);
		} else if (kubun == 'down') {
			careBStep2Obj.appendChild(optionObj);
		}
	}

	/*----------------------------------------------------------*/
	if (kubun == 'left') {
		clearOptionValue(leftEyeStep3Obj);
	} else if (kubun == 'right') {
		clearOptionValue(rightEyeStep3Obj);
	} else if (kubun == 'up') {
		clearOptionValue(careAStep3Obj);
	} else if (kubun == 'down') {
		clearOptionValue(careBStep3Obj);
	}
	
	optionObj       = document.createElement("option");
	optionObj.value = '';
	optionObj.text  = '箱数';
	if (isIE) {
		if (kubun == 'left') {
			leftEyeStep3Obj.add(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep3Obj.add(optionObj);
		} else if (kubun == 'up') {
			careAStep3Obj.add(optionObj);
		} else if (kubun == 'down') {
			careBStep3Obj.add(optionObj);
		}
		
	} else {
		if (kubun == 'left') {
			leftEyeStep3Obj.appendChild(optionObj);
		} else if (kubun == 'right') {
			rightEyeStep3Obj.appendChild(optionObj);
		} else if (kubun == 'up') {
			careAStep3Obj.appendChild(optionObj);
		} else if (kubun == 'down') {
			careBStep3Obj.appendChild(optionObj);
		}
	}	
	
	/*----------------------------------------------------------*/
	if (resStr.length > 0) {
		
		eval("resStrArray = " + resStr);

		for (i = 0; i < resStrArray.length; i++) {
			optionObj       = document.createElement("option");
			optionObj.value = resStrArray[i][0];
			optionObj.text  = resStrArray[i][1];
			if (isIE) {
				if (kubun == 'left') {
					leftEyeStep2Obj.add(optionObj);
				} else if (kubun == 'right') {
					rightEyeStep2Obj.add(optionObj);
				} else if (kubun == 'up') {
					careAStep2Obj.add(optionObj);
				} else if (kubun == 'down') {
					careBStep2Obj.add(optionObj);
				}
				
			} else {
				if (kubun == 'left') {
					leftEyeStep2Obj.appendChild(optionObj);
				} else if (kubun == 'right') {
					rightEyeStep2Obj.appendChild(optionObj);
				} else if (kubun == 'up') {
					careAStep2Obj.appendChild(optionObj);
				} else if (kubun == 'down') {
					careBStep2Obj.appendChild(optionObj);
				}
			}
			
			if (prevValue == resStrArray[i][0]) {
				optionObj.selected = true;
			}
			
		}
		
	}

	return 0;
}



function getStep2(kubun, pageStatus, prevValue)
{
	var step1Value = '';
	
	
	if (kubun == 'left') {
		step1Value = document.getElementById('left_eye_step1').value;
	} else if (kubun == 'right') {
		step1Value = document.getElementById('right_eye_step1').value;
	} else if (kubun == 'up') {
		step1Value = document.getElementById('care_A_step1').value;
	} else if (kubun == 'down') {
		step1Value = document.getElementById('care_B_step1').value;
	}

	if (pageStatus == 'current') {
		xmlHttpReq.abort();
		xmlHttpReq.open("GET", "get_goods.php?status=step2&step1=" + step1Value, true);
		xmlHttpReq.onreadystatechange = function()
										{
											if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
												loadCompleteStep2(xmlHttpReq, kubun, '');
											}
										}
		xmlHttpReq.send(null);
	} else {
		if (kubun == 'left') {
			xmlHttpReqLeftStep2.abort();
			xmlHttpReqLeftStep2.open("GET", "get_goods.php?status=step2&step1=" + step1Value, true);
			xmlHttpReqLeftStep2.onreadystatechange = function()
												{
													if (xmlHttpReqLeftStep2.readyState == 4 && xmlHttpReqLeftStep2.status == 200) {
														loadCompleteStep2(xmlHttpReqLeftStep2, kubun, prevValue);
													}
												}
			xmlHttpReqLeftStep2.send(null);
		} else if (kubun == 'right') {
			xmlHttpReqRightStep2.abort();
			xmlHttpReqRightStep2.open("GET", "get_goods.php?status=step2&step1=" + step1Value, true);
			xmlHttpReqRightStep2.onreadystatechange = function()
												{
													if (xmlHttpReqRightStep2.readyState == 4 && xmlHttpReqRightStep2.status == 200) {
														loadCompleteStep2(xmlHttpReqRightStep2, kubun, prevValue);
													}
												}
			xmlHttpReqRightStep2.send(null);
		} else if (kubun == 'up') {
			xmlHttpReqUpStep2.abort();
			xmlHttpReqUpStep2.open("GET", "get_goods.php?status=step2&step1=" + step1Value, true);
			xmlHttpReqUpStep2.onreadystatechange = function()
												{
													if (xmlHttpReqUpStep2.readyState == 4 && xmlHttpReqUpStep2.status == 200) {
														loadCompleteStep2(xmlHttpReqUpStep2, kubun, prevValue);
													}
												}
			xmlHttpReqUpStep2.send(null);
		} else if (kubun == 'down') {
			xmlHttpReqDownStep2.abort();
			xmlHttpReqDownStep2.open("GET", "get_goods.php?status=step2&step1=" + step1Value, true);
			xmlHttpReqDownStep2.onreadystatechange = function()
												{
													if (xmlHttpReqDownStep2.readyState == 4 && xmlHttpReqDownStep2.status == 200) {
														loadCompleteStep2(xmlHttpReqDownStep2, kubun, prevValue);
													}
												}
			xmlHttpReqDownStep2.send(null);
		}
	}
}



function selectedSyohin(str)
{
	var syohinList         = null;
	var leftEyeStep2Value  = 0;
	var leftEyeStep3Value  = 0;
	var rightEyeStep2Value = 0;
	var rightEyeStep3Value = 0;
	var careAStep2Value    = 0;
	var careAStep3Value    = 0;
	var careBStep2Value    = 0;
	var careBStep3Value    = 0;
	
	
	if (str != '') {
	
		syohinList = str.split(',');
		
		/*----------------------------------------------------------*/
		if (syohinList[0] != '') {
			if (syohinList[1] != '') {
				leftEyeStep2Value = syohinList[1];
			}
			getStep2('left', 'prev', String(leftEyeStep2Value));
		}
		
		
		if (syohinList[3] != '') {
			if (syohinList[4] != '') {
				rightEyeStep2Value = syohinList[4];
			}
			getStep2('right', 'prev', String(rightEyeStep2Value));
		}
		
		if (syohinList[6] != '') {
			if (syohinList[7] != '') {
				careAStep2Value = syohinList[7];
			}
			getStep2('up', 'prev', String(careAStep2Value));
		}
		
		if (syohinList[9] != '') {
			if (syohinList[10] != '') {
				careBStep2Value = syohinList[10];
			}
			getStep2('down', 'prev', String(careBStep2Value));
		}
		
		
		/*----------------------------------------------------------*/
		if (syohinList[0] != '') {
			if (syohinList[2] != '') {
				leftEyeStep3Value = syohinList[2];
			}
			getStep3('left', 'prev', String(leftEyeStep2Value), String(leftEyeStep3Value));
		}
		
		
		if (syohinList[3] != '') {
			if (syohinList[5] != '') {
				rightEyeStep3Value = syohinList[5];
			}			
			getStep3('right', 'prev', String(rightEyeStep2Value), String(rightEyeStep3Value));
		}
		
		if (syohinList[6] != '') {
			if (syohinList[8] != '') {
				careAStep3Value = syohinList[8];
			}
			getStep3('up', 'prev', String(careAStep2Value), String(careAStep3Value));
		}
		
		if (syohinList[9] != '') {
			if (syohinList[11] != '') {
				careBStep3Value = syohinList[11];
			}
			getStep3('down', 'prev', String(careBStep2Value), String(careBStep3Value));
		}	

	}
	
}



function createXMLHttpRequest()
{
	if (window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (ex2) {
				return null;
			}
		}
	} else if (window.XMLHttpRequest) {
		isIE = false;
		return new XMLHttpRequest();
	}

	return null;
}



function init()
{
	xmlHttpReq = createXMLHttpRequest();
	
	xmlHttpReqLeftStep2  = createXMLHttpRequest();
	xmlHttpReqRightStep2 = createXMLHttpRequest();
	xmlHttpReqUpStep2    = createXMLHttpRequest();
	xmlHttpReqDownStep2  = createXMLHttpRequest();
	
	xmlHttpReqLeftStep3  = createXMLHttpRequest();
	xmlHttpReqRightStep3 = createXMLHttpRequest();
	xmlHttpReqUpStep3    = createXMLHttpRequest();
	xmlHttpReqDownStep3  = createXMLHttpRequest();
}
