//chain types 
var DOUBLE_PITCH_ATTACHMENT_CHAIN = 1;
var SINGLE_PITCH_CONVEYOR_LAMBDA = 2;
var DOUBLE_PITCH_CONVEYOR_LAMBDA = 3;
var RS_ATTACHMENT_CHAIN = 4;
//

function setATALG(){
var ATALG;
var RT = document.chainSelection.RT.value;
var TN = TN_CHAIN_SIZES[document.chainSelection.CN.value]['tn'];
var KW = TN_CHAIN_SIZES[document.chainSelection.CN.value]['kw'];
var G = TN_CHAIN_SIZES[document.chainSelection.CN.value]['g'];

	if ( RT=='S' && G>0  &&  (TN == 1 || TN == 3))
	 ATALG ='plain, a1, a2, k1, k2, sa1, sa2, sk1, sk2, gk1, d1, d3'
	else if( RT=='O' || G==0 &&  (TN == 1 || TN == 3))
	 ATALG = 'plain, a1, a2, k1, k2, sa1, sa2, sk1, sk2, d1, d3'
	else if( ((TN==2 || TN==4)) &&  KW ==0)
	 ATALG = 'plain, a1, k1, sa1, sk1, d1, d3'
	else if((TN==4 ) &&  KW >0 )
	 ATALG = 'plain, a1, k1, sa1, sk1, aa1, saa1, kk1, skk1, d1, d3, wa1, wa2, wsa1, wsa2, wk1, wk2, wsk1, wsk2'
	else
	 ATALG='ERROR'
 
 document.chainSelection.ATALG.value = ATALG;
 
  if(document.chainSelection.repeating.value != '2')
 	document.chainSelection.cycle_num.value = '0'

 
 if(document.chainSelection.links_in_cycle_num.value != ''){
 document.chainSelection.NUM.value = document.chainSelection.links_in_cycle_num.value
 }
 
 document.chainSelection.PT.value = TN_CHAIN_SIZES[document.chainSelection.CN.value]['p'];
 document.chainSelection.submit();
}


var TN_CHAIN_SIZES = {
"RS25":{"tn":RS_ATTACHMENT_CHAIN,"p":0.250,"g":0.000,"kw":0.000,"lambda":0},
"RS35":{"tn":RS_ATTACHMENT_CHAIN,"p":0.375,"g":0.000,"kw":0.000,"lambda":1},
"RS41":{"tn":RS_ATTACHMENT_CHAIN,"p":0.500,"g":0.000,"kw":0.000,"lambda":0},
"RS40":{"tn":RS_ATTACHMENT_CHAIN,"p":0.500,"g":0.000,"kw":0.500,"lambda":1},
"RS50":{"tn":RS_ATTACHMENT_CHAIN,"p":0.625,"g":0.000,"kw":0.625,"lambda":1},
"RS60":{"tn":RS_ATTACHMENT_CHAIN,"p":0.750,"g":0.000,"kw":0.750,"lambda":1},
"RS80":{"tn":RS_ATTACHMENT_CHAIN,"p":1.000,"g":0.000,"kw":1.000,"lambda":1},
"RS100":{"tn":RS_ATTACHMENT_CHAIN,"p":1.250,"g":0.000,"kw":1.250,"lambda":1},
"RS120":{"tn":RS_ATTACHMENT_CHAIN,"p":1.500,"g":0.000,"kw":0.000,"lambda":0},
"RS140":{"tn":RS_ATTACHMENT_CHAIN,"p":1.750,"g":0.000,"kw":0.000,"lambda":0},
"RS160":{"tn":RS_ATTACHMENT_CHAIN,"p":2.000,"g":0.000,"kw":0.000,"lambda":0},
"C2040":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":1.000,"g":0.161,"kw":0.000,"lambda":1},
"C2050":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":1.250,"g":0.201,"kw":0.000,"lambda":1},
"C2060H":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":1.500,"g":0.240,"kw":0.000,"lambda":1},
"C2080H":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":2.000,"g":0.319,"kw":0.000,"lambda":1},
"C2100H":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":2.500,"g":0.000,"kw":0.000,"lambda":1},
"C2120H":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":3.000,"g":0.000,"kw":0.000,"lambda":0},
"C2160H":{"tn":DOUBLE_PITCH_ATTACHMENT_CHAIN,"p":4.000,"g":0.000,"kw":0.000,"lambda":0}
// ,
// "RSC35-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":0.375,"g":0.000,"kw":0.000},
// "RSC40-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":0.500,"g":0.000,"kw":0.000},
// "RSC50-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":0.625,"g":0.000,"kw":0.000},
// "RSC60-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":0.750,"g":0.000,"kw":0.000},
// "RSC80-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":1.000,"g":0.000,"kw":0.000},
// "RSC100-LAMBDA":{"tn":SINGLE_PITCH_CONVEYOR_LAMBDA,"p":1.250,"g":0.000,"kw":0.000},
// "C2040-LAMBDA":{"tn":DOUBLE_PITCH_CONVEYOR_LAMBDA,"p":1.000,"g":0.161,"kw":0.000},
// "C2050-LAMBDA":{"tn":DOUBLE_PITCH_CONVEYOR_LAMBDA,"p":1.250,"g":0.201,"kw":0.000},
// "C2060-LAMBDA":{"tn":DOUBLE_PITCH_CONVEYOR_LAMBDA,"p":1.500,"g":0.240,"kw":0.000},
// "C2080-LAMBDA":{"tn":DOUBLE_PITCH_CONVEYOR_LAMBDA,"p":2.000,"g":0.319,"kw":0.000},
// "C2100-LAMBDA":{"tn":DOUBLE_PITCH_CONVEYOR_LAMBDA,"p":2.500,"g":0.398,"kw":0.000}
};

function onChainSizeChanged(cn){
// 	alert(cn.value);
    $('info_cycle_num').style.display = 'none';
	$('info').innerHTML = '';
	var tn = TN_CHAIN_SIZES[cn.value]['tn']
	
	if(tn ==  DOUBLE_PITCH_ATTACHMENT_CHAIN || tn == DOUBLE_PITCH_CONVEYOR_LAMBDA){
// 		$('wrap_RT').style.display = '';
		document.chainSelection.RT.disabled = false;
	}else{
		document.chainSelection.RT.value = 'S';
// 		$('wrap_RT').style.display = 'none';
		document.chainSelection.RT.disabled = true;
	}


/*	
	if(tn ==  SINGLE_PITCH_CONVEYOR_LAMBDA || tn == DOUBLE_PITCH_CONVEYOR_LAMBDA){
		document.chainSelection.MAT.disabled = false;
	}else{
		document.chainSelection.MAT.value = '';
		document.chainSelection.MAT.disabled = true;
	}
*/	
	setMinTotalLen(TN_CHAIN_SIZES[cn.value].p);
	onTotalLenChanged(document.chainSelection.total_length);
// 	alert(document.chainSelection.cycle_num.value);

	handleMaterial()
}

function onLenTypeChanged(){
	onTotalLenChanged(document.chainSelection.total_length)
}

function onTotalLenChanged(tl){
	$('info_cycle_num').style.display = 'none';
	var info_length = $('info_length');
	info_length.style.display = 'none';
	
	var pitch = TN_CHAIN_SIZES[document.chainSelection.CN.value].p
	
	if(document.chainSelection.length_type.value == "pitches"){
		tl.value = Math.round(tl.value)
		
		if(tl.value < 2)
			tl.value = 2;
		
		var len = tl.value * pitch;
	}else{
		var len = tl.value;
	}

	
	if( isNaN(len) || len > 600){
	 info_length.innerHTML = 'Max. total length is 600 inch.';
	 info_length.style.display = '';
	 tl.value = '600';
	 return false;
	}
	else if(len <  tl.minLen){
	 info_length.innerHTML = 'Min. total length is: ' +  tl.minLen + ' inches.';
	 info_length.style.display = '';
	 tl.value = tl.minLen;
	 return false;
	}

	var remain = len % pitch;
// 	$('info').innerHTML = $('info').innerHTML + "remain :" + remain + '<br>';
	
	var linksNum = Math.floor(len / pitch)
	if(remain != 0){
		if(remain >= pitch){
			 linksNum = linksNum - 1
		}
		
		
		info_length.innerHTML = "<b>Warning.</b> "+ len +" cannot be used with "+ pitch +". Enter " + (linksNum * pitch) + ' or ' + ((linksNum +1 ) * pitch);
		info_length.style.display = '';
// 		info_length.innerHTML = "<b>Warning.</b> Selected pitch is "+ pitch +". Length of " + (linksNum * pitch) + ' INCH would be used.<br>';
	}
	
// 	if(document.chainSelection.cycle_num.value !='0'){
// 		
// 	}
		document.chainSelection.cycle_num.value='1';
		document.chainSelection.cycle_num.dobra='1';
		document.chainSelection.links_in_cycle_num.value = '';
	document.chainSelection.NUM.value  = linksNum;
// 	$('info').innerHTML = $('info').innerHTML + "len / pitch :" + (len / pitch) + '<br>';
	
}

function setMinTotalLen(pitch){
	document.chainSelection.total_length.value  = pitch * 2;
	document.chainSelection.total_length.minLen = pitch * 2;
}

function onChangeRepeating(f){
	if( f.value != '2' ){
// 		$('wrap_cycle_num').style.visibility = 'hidden';
		$('wrap_cycle_num').style.display = 'none';
		$('wrap_links_in_cycle_num').style.visibility = 'hidden';
		
		document.chainSelection.cycle_num.value='1';
		document.chainSelection.links_in_cycle_num.value = '';
	}
	else{
// 		$('wrap_cycle_num').style.visibility = 'visible';
		$('wrap_cycle_num').style.display = '';
		$('wrap_links_in_cycle_num').style.visibility = 'visible';
		document.chainSelection.cycle_num.value='1';
		document.chainSelection.cycle_num.dobra='1';
	}

}

function onChangeCycleNum(num){
	var info = $('info_cycle_num');
	var info_links = $('info_links_in_cycle_num');
	info.style.display = 'none';
	info_links.style.display = 'none';

// alert((document.chainSelection.NUM.value % num ))
	if( (document.chainSelection.NUM.value != num) && ( document.chainSelection.NUM.value  % num )  == 0 ){
		document.chainSelection.links_in_cycle_num.value = document.chainSelection.NUM.value / num;
		document.chainSelection.cycle_num.dobra = num;
	}
	else{
		info.innerHTML ="<b>Warning.</b> " + num + " cannot be used with " + document.chainSelection.total_length.value + " length and " +  TN_CHAIN_SIZES[document.chainSelection.CN.value].p + "inch pitch"
		info.style.display = '';
// 		alert(num + " cannot be used with " + document.chainSelection.total_length.value + " length and " +  TN_CHAIN_SIZES[document.chainSelection.CN.value].p + " pitch");
// 		17 cannot be used with 100 length and 0.375 pitch
		document.chainSelection.cycle_num.value = document.chainSelection.cycle_num.dobra;
	}
	
	if(document.chainSelection.links_in_cycle_num.value % 2 != 0){
		info_links.innerHTML = 'The repeating section should be an even number of pitches.';
		info_links.style.display = '';
	}
		
}

function handleMaterial(){
	if(TN_CHAIN_SIZES[document.chainSelection.CN.value]['lambda'] == 1 && document.getElementById('mat_lambda') == null){
		var lambda = document.createElement('OPTION');
		lambda.innerHTML = 'Conveyor Lambda';
		lambda.value = 'CL';
		lambda.id='mat_lambda';
		document.chainSelection.MAT.appendChild(lambda);
	}
	else if(TN_CHAIN_SIZES[document.chainSelection.CN.value]['lambda'] == 0 && document.getElementById('mat_lambda') != null){
		var lambda = document.getElementById('mat_lambda');
		lambda.parentNode.removeChild(lambda);
	}
	
}
var i = 0
for (var p in TN_CHAIN_SIZES){
	document.chainSelection.CN.options[i] = new Option(p + " (" + TN_CHAIN_SIZES[p].p + '" pitch)', p)
	i++
}

if(partserver != "0"){
	document.chainSelection.user_mail.value = user_mail
	// document.chainSelection.user_format.value = user_format
	// document.chainSelection.user_idnr.value = user_idnr
	document.chainSelection.language.value = language
}
onChainSizeChanged(document.chainSelection.CN);
onChangeRepeating(document.chainSelection.repeating)


