// JavaScript Document //
var pullmat, bushtype, beartype;
var detailsArray = [["size",0],
					["btype",0],
					["pmaterial",2],
					["pprofile",4],
					["diam",0],
					["adiam",0],
					["pwidth",0],
					["pwidthI",0],
					["bearingt",1],
					["mclear",0],
					["mclearM",0],
					["threadst",0]];
var groovesArray = ["MX","2G","3G","3M"];
function resetAll(id) {
// this function will reset values of all form elements starting with 
// element located inside "d"+id div
if ( id < 4 ) {
	for ( j=0; j<groovesArray.length; j++ ) {
		ddi(groovesArray[j]);
	}
	for ( i=1; i<5; i++ ) {
		gid='grooves'+i;
		document.getElementById(gid).checked=false;	
	}
}
if ( id < 8 ) { 
	document.sspng.pwidth.value = "";
	document.sspng.pwidthI.value = "";	
	document.sspng.mclear.value = "";
	document.sspng.mclearM.value = "";	
}
if ( id < 10 ) { 
	document.sspng.mclear.value = "";
	document.sspng.mclearM.value = "";
}
for ( i=id; i<detailsArray.length; i++ ) {
	if ( detailsArray[i][1]!='0' ) {
		for (j=1; j<detailsArray[i][1]+2; j++) {
			var did=detailsArray[i][0]+j;
			document.getElementById(did).checked=false;
		}
	} else {
			var didZ=detailsArray[i][0]+1;
			document.getElementById(didZ).checked=false;
	}
}
}

function pprofilefF(val1,id1,id2) {
profile = val1;
for ( j=0; j<groovesArray.length; j++ ) {
	ddi(groovesArray[j]);
}
for ( i=1; i<5; i++ ) {
	gid='grooves'+i;
	document.getElementById(gid).checked=false;	
}
show(id1,id2);
}

function pprofilef(val1,id1,id2) {
profile = val1;
for ( j=0; j<groovesArray.length; j++ ) {
	ddi(groovesArray[j]);
}
for ( i=1; i<5; i++ ) {
	gid='grooves'+i;
	document.getElementById(gid).checked=false;	
}
ddv(profile);
show(id1,id2);
}

function adiamf() {
if ( profile == 'XL' || profile == 'MX') 
	{show('7','7');}
else {show('6','6');}
}

function setpm(val1,id1,id2) {
// this function will set idler pulley material for future reference
pullmat = val1;
show(id1,id2);
}

function setpmX(val1,id1,id2) {
// this function will set idler pulley material for future reference
pullmat = val1;
if (bushtype == "NB") { show('12',id2); }
else {show(id1,id2);}
}

function setbt(val1,id1,id2) {
// this function will set bushing type for future reference
bushtype = val1;
show(id1,id2);
}

function pwidthf() {
if (pullmat == "A") {show('8','8')}
else {show('10','8')}
}

function pwidthIf() {
if (pullmat == "A") {show('8','8')}
else { show('9','8') }

}

function showNB() {
if (bushtype == "NB") { show('12','11'); }
else {show('11','11');}
}

function bearingtf(val) {
vbearingt = val;
if ( profile == 'XL' || profile == 'MX' || profile == 'F') 
	{show('9','9');}
else {show('10','9');}
}

function ddv(id) {
// this function will make an element visible
dd=document.getElementById(id);
dd.style.display="block";
}

function ddi(id) {
// this function will make an element invisible
dd=document.getElementById(id);
dd.style.display="none";
}

function show(id1,id2) {
// this function will make elements starting with 'd'+id2  invisible
// then make element id1 visible
var eid1='d'+id1;
for (var i=id2; i<13; i++) {
	var di = 'd'+i;
	ddi(di);
	}
ddv(eid1);
resetAll(id2);
}

function reload_url(url) {
	document.location.href=url;
}

function formCheck(form) {
// check if pwidth and mclear are filled if pullmat is not X
// pwidthI and mclear for english profiles (F,XL,MX)
// pwodth and mclearM for metric profiles (2G,3G,3M,5M)
if (pullmat != "X") {
 if (profile == 'F' || profile == 'XL' || profile == 'MX' ) {
  if (document.sspng.pwidthI.value==""){
   alert("Please, enter the pulley width in x.xx inches.");
   document.sspng.pwidthI.focus();
   return false;
  } 
  if (document.sspng.mclear.value==""){
   alert("Please, enter minimum clearance from the bottom edge of pulley to your mounting surface in x.xx or .xx decimal inches.");
   document.sspng.mclear.focus();
   return false;
  } 
  // other functions for english profiles will go here
	if (formatEnglish(document.sspng.pwidthI.value) == false) {
		alert("Please, enter the pulley width in x.xx inches.");
		document.sspng.pwidthI.focus();
	    document.sspng.pwidthI.select();
		return false;
	}
	if (formatEnglish(document.sspng.mclear.value) == false) {
		alert("Please, enter minimum clearance from the bottom edge of pulley to your mounting surface in x.xx inches.");
		document.sspng.mclear.focus();
	    document.sspng.mclear.select();
		return false;
	}
//	if (document.sspng.mclear.value < 0.18) {
//  		alert("You need to change the minimum clearance from bottom edge of pulley to mounting surface to at least 0.18 inches.");
//		document.sspng.mclear.focus();
//	    document.sspng.mclear.select();
//		return false;
//	}

	if (document.sspng.mclear.value < 0.21 && pullmat == 'A' && vbearingt == 'S') {
  		alert("You need to change the minimum clearance from the bottom edge of pulley to mounting surface to at least 0.21 inches.");
		document.sspng.mclear.focus();
		document.sspng.mclear.select();
		return false;
	}
	if (document.sspng.mclear.value < 0.18 && pullmat == 'A' && vbearingt == 'B') {
  		alert("You need to change the minimum clearance from the bottom edge of pulley to mounting surface to at least 0.18 inches.");
		document.sspng.mclear.focus();
		document.sspng.mclear.select();
		return false;
	}
	if (document.sspng.mclear.value < 0.18 && pullmat == 'L') {
  		alert("You need to change the minimum clearance from the bottom edge of pulley to mounting surface to at least 0.18 inches.");
		document.sspng.mclear.focus();
		document.sspng.mclear.select();
		return false;
	}

	if (document.sspng.pwidthI.value < 0.29) {
  		alert("You need to change the pulley width to at least 0.29 inches.");
		document.sspng.pwidthI.focus();
		document.sspng.pwidthI.select();
		return false;
	}	
	
 } else {
   if (document.sspng.pwidth.value==""){
   alert("Please, enter the pulley width in xx.x millimeters (1 mm = .039 inches).");
   document.sspng.pwidth.focus();
   return false;
  } 
  if (document.sspng.mclearM.value==""){
   alert("Please, enter minimum clearance from the bottom edge of pulley to your mounting surface in xx.x millimeters.");
   document.sspng.mclearM.focus();
   return false;
  } 
  // other functions for metric profiles will go here
	if (formatMetric(document.sspng.pwidth.value) == false) {
		alert("Please, enter the pulley width in xx.x millimeters (1 mm = .039 inches).");
		document.sspng.pwidth.focus();
	    document.sspng.pwidth.select();
		return false;
	}
	if (formatMetric(document.sspng.mclearM.value) == false) {
		alert("Please, enter minimum clearance from the bottom edge of pulley to your mounting surface in xx.x millimeters.");
		document.sspng.mclearM.focus();
	    document.sspng.mclearM.select();
		return false;
	}
//	if (document.sspng.mclearM.value < 4.6) {
//		alert("You need to change the minimum clearance from bottom edge of pulley to mounting surface to at least 4.6 mm.");
//		document.sspng.mclearM.focus();
//	    document.sspng.mclearM.select();
//		return false;
//  }

	if (document.sspng.mclearM.value < 5.3 && pullmat == 'A' && vbearingt == 'S') {
		alert("You need to change the minimum clearance from bottom edge of pulley to mounting surface to at least 5.3 mm.");
		document.sspng.mclearM.focus();
		document.sspng.mclearM.select();
		return false;
  }
	if (document.sspng.mclearM.value < 4.6 && pullmat == 'A' && vbearingt == 'B' ) {
		alert("You need to change the minimum clearance from bottom edge of pulley to mounting surface to at least 4.6 mm.");
		document.sspng.mclearM.focus();
		document.sspng.mclearM.select();
		return false;
  }
	if (document.sspng.mclearM.value < 4.6 && pullmat == 'L') {
		alert("You need to change the minimum clearance from bottom edge of pulley to mounting surface to at least 4.6 mm.");
		document.sspng.mclearM.focus();
		document.sspng.mclearM.select();
		return false;
  }
  
	if (document.sspng.pwidth.value < 7.4) {
  		alert("You need to change the pulley width to at least 7.4 mm.");
		document.sspng.pwidth.focus();
		document.sspng.pwidth.select();
		return false;
	}    

 }//end of second if statement
}//end of first if statement
}//end of function

// format for metric xx.x or x.x (in mm)
function formatMetric(val) {
var str = val;
str = str.toString();
// if str has 3 chars then it should be x.x
if (str.length == 3) {
	var one = str.charCodeAt(0);
	var two = str.charCodeAt(1);
	var thr = str.charCodeAt(2);
	if ( (one < 48 || one > 57) || (two != 46) || (thr < 48 || thr > 57) ) {
		return false;
	}
} else if (str.length == 4) {
// if str has 4 chars then it should be xx.x
	var one = str.charCodeAt(0);
	var two = str.charCodeAt(1);
	var thr = str.charCodeAt(2);
	var fou = str.charCodeAt(3);
	if ( (one < 48 || one > 57) || (two < 48 || two > 57) || (thr != 46) || (fou < 48 || fou > 57) ) {
		return false;
	} 
//else - then there is an error
} else { 
		return false; 
}
} //end of function

// format for english x.xx or .xx (in inches)
function formatEnglish(val) {
var str = val;
str = str.toString();
// if str has 3 chars then it should be .xx
if (str.length == 3) {
	var one = str.charCodeAt(0);
	var two = str.charCodeAt(1);
	var thr = str.charCodeAt(2);
	if ( (two < 48 || two > 57) || (one != 46) || (thr < 48 || thr > 57) ) {
		return false;
	}
} else if (str.length == 4) {
// if str has 4 chars then it should be x.xx
	var one = str.charCodeAt(0);
	var two = str.charCodeAt(1);
	var thr = str.charCodeAt(2);
	var fou = str.charCodeAt(3);
	if ( (one < 48 || one > 57) || (thr < 48 || thr > 57) || (two != 46) || (fou < 48 || fou > 57) ) {
		return false;
	} 
//else - then there is an error
} else { 
		return false; 
}

} //end of function


