﻿function openSfStartStopWindow(){
	openWindow(sfStartStopHtml, 'SfUnknownWindow', 480, 540);
}
function openSfLoadWindow(){
	openWindow(sfLoadHtml, 'sfKnownHtml', 480, 600);
}
// Hzから入力回転数をセットする
function setInputSpeed(){
	var inputspeed = document.productSelection.inputspeed.value;
	var ratio = document.productSelection.ratio.value;
	var hz = document.productSelection.hz.value;
	var inputspeedOBJ = document.productSelection.inputspeed;
	var hzOBJ = document.productSelection.hz;
	// もっとの入力回転数選択された番号を取得
	var selectedInputSpeed = getSelectNumber('inputspeed');
	
	if(hz==50){
		inputspeedOBJ.length=2;
		inputspeedOBJ.options[0].text = "";
		inputspeedOBJ.options[1].text = "1450";
		
		inputspeedOBJ.options[0].value = "-1";
		inputspeedOBJ.options[1].value = "1450";
		setSelectCheckByNumber('inputspeed', 1);				
		checkInverter();
	}else if(hz==60){
		inputspeedOBJ.length=2;
		inputspeedOBJ.options[0].text = "";
		inputspeedOBJ.options[1].text = "1750";
		
		inputspeedOBJ.options[0].value = "-1";
		inputspeedOBJ.options[1].value = "1750";
		setSelectCheckByNumber('inputspeed', 1);			
	}else {
		// ディフォルト
		inputspeedOBJ.length=3;
		inputspeedOBJ.options[0].text = "";
		inputspeedOBJ.options[1].text = "1450";
		inputspeedOBJ.options[2].text = "1750";		
		
		inputspeedOBJ.options[0].value = "-1";
		inputspeedOBJ.options[1].value = "1450";
		inputspeedOBJ.options[2].value = "1750";
		setSelectCheckByNumber('inputspeed', 0);			
	}
	if(inputspeed>0 && ratio>0){
		speedCalculationByRatio();
	}
	// 極数のセット
	setPole();	
}

// 低速軸連結方式が変更されたら
function changeShaftConnection(){
	var shaftconnection = document.productSelection.slowshaftconnection.value;
	var pitchradius = document.productSelection.pitchradius;
	var loadtype = document.productSelection.loadtype;
	var loadposition = document.productSelection.loadposition;
	
	if(shaftconnection==0 || shaftconnection==-1){
		pitchradius.disabled = true;
		pitchradius.style.backgroundColor='#D4D0C8';
		
		loadtype[0].disabled = true;
		loadtype[0].style.backgroundColor='#D4D0C8';
		loadtype[1].disabled = true;
		loadtype[1].style.backgroundColor='#D4D0C8';
		loadtype[2].disabled = true;
		loadtype[2].style.backgroundColor='#D4D0C8';						
		
		loadposition.disabled = true;
		loadposition.style.backgroundColor='#D4D0C8';				
	}else {
		pitchradius.disabled = false;
		pitchradius.style.backgroundColor='#FFFFFF';
		
		loadtype[0].disabled = false;
		loadtype[0].style.backgroundColor='#FFFFFF';
		loadtype[1].disabled = false;
		loadtype[1].style.backgroundColor='#FFFFFF';
		loadtype[2].disabled = false;
		loadtype[2].style.backgroundColor='#FFFFFF';					
		
		loadposition.disabled = false;
		loadposition.style.backgroundColor='#FFFFFF';	
	}
}

// 出力回転数を計算
function speedCalculationByRatio(){
	var inputspeed = document.productSelection.inputspeed.value;
	var outputspeed = document.productSelection.outputspeed.value;
	var ratio = document.productSelection.ratio.value;
	
	var inputspeedOBJ = document.productSelection.inputspeed;
	var outputspeedOBJ = document.productSelection.outputspeed;
	var ratioOBJ = document.productSelection.ratio;	
	
	if(inputspeed!=-1 && ratio!=-1){		// 入力回転数と減速比によって計算
		outputspeed = inputspeed/ratio;
		if(outputspeed>100){
			keta = 0;
		}
		if(outputspeed<100 && outputspeed>10){
			keta = 1;
		}
		if(outputspeed<10 && outputspeed>1){
			keta = 2;
		}
		if(outputspeed<1){
			keta = 3;
		}						
		outputspeed = Keisan45(outputspeed,keta);
		outputspeedOBJ.value = outputspeed;
	}
	
	// 極数のセット
	setPole();
	
	// 電源周波数
	if(inputspeed==980 || inputspeed==1450){
		setSelectCheck('hz', '50');
	}else if(inputspeed==1165 || inputspeed==1750){
		setSelectCheck('hz', '60');
	}else {
		setSelectCheck('hz', '-1');
	}	
}

// 極数のセット
function setPole(){
	// 本機種は4極のみです
	pole = '4';
	document.productSelection.pole.value=pole;	
}

function speedCalculationByOutputspeed(){
	var inputspeed = document.productSelection.inputspeed.value;
	var outputspeed = document.productSelection.outputspeed.value;
	var ratio = document.productSelection.ratio.value;
	
	var inputspeedOBJ = document.productSelection.inputspeed;
	var outputspeedOBJ = document.productSelection.outputspeed;
	var ratioOBJ = document.productSelection.ratio;	
	
	if(inputspeed!=-1 && outputspeed!=""){	// 入力回転数と出力回転数によって計算
		ratio = inputspeed/outputspeed;
		
		var l = ratioOBJ.length;		
		var selectedIndex = -1;
		var selectedIndex1 = -1;
		var selectedIndex2 = -1;
		for(i=0;i<l;i++){
			if(ratioOBJ[i].value==ratio){
				selectedIndex = i;
				break;
			}else if(ratioOBJ[i].value>ratio) {
				selectedIndex2 = i;
				break;
			}else if(ratioOBJ[i].value<ratio){
				selectedIndex1 = i;
			}
		}
		if(selectedIndex==-1){	// 同じの減速比がなし
			limit1 = ratio-ratioOBJ[selectedIndex1].value;
			limit2 = ratioOBJ[selectedIndex2].value-ratio;
			
			if(limit1==limit2){		// ちょうど真中
				selectedIndex = selectedIndex2;
			}else if(limit1>limit2){
				selectedIndex = selectedIndex2;
			}else {
				selectedIndex = selectedIndex1;
			}
		}
		
		ratioOBJ[selectedIndex].selected = true;	
	}
}

// 低速軸方向・取付方法隠しField値のセット（取付方向　選択された画像パス）
function setMountingStyle(imagefile,code){
	var mountingstyle = document.getElementById("mountingstyle");
	var msimage = document.getElementById("msimage");
	var imagepath = "hyponic/" + imagefile;
	
	mountingstyle.value = code;
	msimage.value = imagepath;
	
	setLoadposition(code,true);	// 荷重位置のセット
}

// 荷重位置のセット
function setLoadposition(mountingstyle,doalert){
	var loadpositionOBJ = document.getElementById("loadposition");
	var lfimage = document.getElementById("lfimage");
	var slowshaftconnection = document.getElementById("slowshaftconnection");
	if(mountingstyle=='Y'){	// RNYMホローシャフト
		if(loadpositionOBJ.length!=10){
			loadpositionOBJ.length=10;
			loadpositionOBJ.options[0].text = "　";
			loadpositionOBJ.options[1].text = "10mm";
			loadpositionOBJ.options[2].text = "20mm";
			loadpositionOBJ.options[3].text = "30mm";
			loadpositionOBJ.options[4].text = "40mm";
			loadpositionOBJ.options[5].text = "50mm";
			loadpositionOBJ.options[6].text = "60mm";
			loadpositionOBJ.options[7].text = "70mm";
			loadpositionOBJ.options[8].text = "80mm";
			loadpositionOBJ.options[9].text = "90mm";		
			
			loadpositionOBJ.options[0].value = "-1";
			loadpositionOBJ.options[1].value = "10";
			loadpositionOBJ.options[2].value = "20";
			loadpositionOBJ.options[3].value = "30";
			loadpositionOBJ.options[4].value = "40";
			loadpositionOBJ.options[5].value = "50";
			loadpositionOBJ.options[6].value = "60";
			loadpositionOBJ.options[7].value = "70";
			loadpositionOBJ.options[8].value = "80";
			loadpositionOBJ.options[9].value = "90";
		}
		
		// 荷重位置例の写真
		if(lfimage.style.display!=""){
			lfimage.style.display='';
		}
			
		loadpositionOBJ.options[0].selected = true;
		
		if(slowshaftconnection.value>=1 && doalert){
			window.alert('荷重位置をご確認ください');
			loadpositionOBJ.focus();
		}				
	}else if(mountingstyle=='F' || mountingstyle=='H') {		// RNHM  RNFM 中実軸
		if(loadpositionOBJ.length!=4){
			loadpositionOBJ.length=4;
			loadpositionOBJ.options[0].text = "　";
			loadpositionOBJ.options[1].text = "軸根本";
			loadpositionOBJ.options[2].text = "軸中央";
			loadpositionOBJ.options[3].text = "軸端";		
			
			loadpositionOBJ.options[0].value = "-1";
			loadpositionOBJ.options[1].value = "0.8";
			loadpositionOBJ.options[2].value = "1.0";
			loadpositionOBJ.options[3].value = "1.4";
		}	
		// 荷重位置例の写真
		lfimage.style.display='none';
		
		loadpositionOBJ.options[0].selected = true;
		if(slowshaftconnection.value>=1 && doalert){
			window.alert('荷重位置をご確認ください');
			loadpositionOBJ.focus();
		}		
	}		
}


function changeVolt(){
	var voltOBJ = document.getElementById("volt");
	var standardmotor = document.getElementById("standardmotor");
	var invertermotor = document.getElementById("invertermotor");
	var camotor = document.getElementById("camotor");
	var cbmotor = document.getElementById("cbmotor");
	var ccmotor = document.getElementById("ccmotor");
	volt = voltOBJ.value;
	if(volt==-1 || volt==200){
		standardmotor.disabled = false;
		invertermotor.disabled = false;
		camotor.disabled = false;
		cbmotor.disabled = false;
		ccmotor.disabled = false;
	}else if(volt==100){
		standardmotor.disabled = true;
		invertermotor.disabled = true;
		camotor.disabled = false;
		cbmotor.disabled = false;
		ccmotor.disabled = false;	
	}else if(volt==400){
		standardmotor.disabled = false;
		invertermotor.disabled = false;
		camotor.disabled = true;
		cbmotor.disabled = true;
		ccmotor.disabled = true;	
	}
}

function setParse(){
	var phasediv= document.getElementById("phasediv");
	var phase = document.getElementById("phase");
	var standardmotor = document.getElementById("standardmotor");
	var invertermotor = document.getElementById("invertermotor");
	var camotor = document.getElementById("camotor");
	var cbmotor = document.getElementById("cbmotor");
	var ccmotor = document.getElementById("ccmotor");
	
	if(standardmotor.checked==true || invertermotor.checked==true){
		phase.value = '3';
		phasediv.innerHTML = "3相";
		if(invertermotor.checked==true){	// インバータモータチェックボックスによって、周波数、入力回転数のセット
			checkInverter();
		}
	}else if(camotor.checked==true || cbmotor.checked==true || ccmotor.checked==true){
		phase.value = '1';
		phasediv.innerHTML = "単相";	
	}
}

// インバータモータチェックボックスによって、周波数、入力回転数のセット
function checkInverter(){
	// インバータモータチェックボックス状態
	var invertermotor = document.getElementById("invertermotor");
	var inputspeed = document.getElementsByName("inputspeed");
	
	if(!invertermotor.checked){
		return;	
	}else{	// チェックされた
		var hz = document.getElementById("hz");
		// 60Hzではなければ
		if(hz.value!='60'){
			// もっとの入力回転数選択された番号を取得
			var selectedInputSpeed = getSelectNumber('inputspeed');				
			// 60Hzにセットする
			setSelectCheck('hz', '60');
			// 入力回転数をセットする
			setInputSpeed();
			// もともと選択されたリストに選択させる
			inputspeed[0].options[selectedInputSpeed].selected = true;
			// 極数のセット
			setPole();			
			// 出力回転数を計算させる
			speedCalculationByRatio();
			window.alert("インバータモータが選択された場合は電源周波数が60Hzのみになります\n出力回転数をご確認してください");
			inputspeed.focus();
		}
	}
}

function setDefaultData(){	
	// Mounting Styleの画像ファイル名
	setTextBoxValue('msimage', msimage);
	// 取付方法
	setTextBoxValue('mountingstyle', mountingstyle);
	// MountingStyle
	setRadioCheck('selMountingStyle', selMountingStyle);
	// 電源電圧
	setSelectCheck('volt', volt);	
	// 電源周波数
	setSelectCheck('hz', hz);
	// モータ容量
	setSelectCheck('motorpower', motorpower);	
	// 極数
	if(pole == 0) pole = '';
	setTextBoxValue('pole', pole);	
	// モータタイプ
	setRadioCheck('motortype', motortype);
	// ブレーキ
	setCheckboxCheck('brake', brake);
	// モータオプション
	setRadioCheck('motoroption', motoroption);		
	
	// 入力回転数
	setInputSpeed();
	setSelectCheck('inputspeed', inputspeed);
	// 出力回転数
	if(outputspeed==0)　outputspeed = '';
	setTextBoxValue('outputspeed', outputspeed);		
	// 減速比
	setSelectCheck('ratio', ratio);	
	
	// SF
	var viewSfInfo = false;
	if(sf=='' || sf==0)　{
		sf="1.00";
	} else 　{
		viewSfInfo = true;
	}
	setTextBoxValue('sf', sf);	
		
	// SF選定情報をHiddenにセットする
	setTextBoxValue('sf_loadtypeValue', sf_loadtypeValue);
	setTextBoxValue('sf_runtimeValue', sf_runtimeValue);
	setTextBoxValue('sf_startstopValue', sf_startstopValue);
	setTextBoxValue('sf_momentValue', sf_momentValue);
	// SF選定情報の表示		
	sf_loadtypeObj.innerHTML	 = sf_loadtypeValue;	// 衝撃の程度
	sf_runtimeObj.innerHTML		 = sf_runtimeValue;		// 運転時間
	sf_startstopObj.innerHTML	 = sf_startstopValue;	// 始動・停止頻度
	sf_momentObj.innerHTML		 = sf_momentValue;		// 負荷係数		
											
	// 情報テーブル
	if(sf!='' && viewSfInfo) {
		sfinfotbObj.style.display='';
	} else {
		sfinfotbObj.style.display='none';
	}
		
	// 低速軸連結方式
	setSelectCheck('slowshaftconnection', slowshaftconnection);
	// ピッチ円半径
	if (pitchradius==0) pitchradius = '';
	setTextBoxValue('pitchradius', pitchradius);
	// 衝撃の程度
	setRadioCheck('loadtype', loadtype);		
	// 荷重位置のセット
	setLoadposition(mountingstyle,false);
	// 荷重位置値のセット
	setSelectCheck('loadposition', loadposition);

	// 各チェック
	changeShaftConnection();
	changeVolt();
	setParse();	
}
