// JavaScript Document
// Global variables 
var mxML  = 1000;	// Default value (Millileters)
var ocMin = 0,		// Calculated Minimum units (before converting to a fraction)
		ocMax = 0,		// Calculated Maximum units (before converting to a fraction)
		ocU   = -1;		// Units for result
var pMin = 0,			// Minimum & maximum percentages
		pMax = 0;
// On initial load, 
function onLoadDo() {
	ocU = document.MixCalc.OCUnits.value;	// (Units/Millileter) for NU Units
	OnChgPur();														// Gets default purpose & updates
	document.MixCalc.ctrSize.focus();
}
// Quantity value changed
function OnChgSize() {
	var f = document.MixCalc,
			v = parseNumVal(f.ctrSize.value);
	if( v==null ) {alert("Please enter a valid number"); f.ctrSize.focus(); f.ctrSize.select(); return false;}	
	mxML = v * f.ctrUnits.value;
	return UpdateMix();
}
// Change container size units (no update necessary)
function OnChgSizeUn() {
	var f = document.MixCalc,
			v = mxML / f.ctrUnits.value;
	f.ctrSize.value = (v>=10)? Math.round(v): cvtFrac(v,4);
}
// select - document.MixCalc.task.value
function OnChgTask() {
	var f = document.MixCalc;
	S2(0);														// Set as active control
	// Extract value of selected item and split apart by commas
	aArgs = f.task.value.split(",");
	if( aArgs.length < 2 )
		ShowNotes(null,null);
	else {			
		switch( parseInt(aArgs[0]) ) {
			// Standard mix 0-Spray, 1-General, 2-Strong (0,[0|1|2],iMsg)
			case 0: nSelMix = parseInt(aArgs[1]);
							var sPerc = f.purpose.options[nSelMix].value,		// pMin[,pMax] (percentages)
									i = sPerc.indexOf(",");
							pMin = parseFloat(sPerc)/100.0,
							pMax = (i<0)? pMin: parseFloat(sPerc.substring(i+1)/100.0);
							f.purpose.selectedIndex = nSelMix;
							UpdateMix();
							ShowNotes(f.task.options[f.task.selectedIndex].text,aTxt[aArgs[2]]);
							break; 
			case 1:	// Fixed percentage 1,nPerc,iMsg
							pMin = pMax = parseFloat(aArgs[1])/100.0;
							f.purpose.selectedIndex = 0;			// 'select mix'
							UpdateMix();
							ShowNotes(f.task.options[f.task.selectedIndex].text,aTxt[aArgs[2]]);
							break; 
			default: alert( "Unknown task type "+aArgs[0]+"?" );
}	}	}
// select - document.MixCalc.OCUnits.value
function OnChgPur() {
	var sPerc = document.MixCalc.purpose.value,		// pMin[,pMax] (percentages)
			i = sPerc.indexOf(",");
	pMin = parseFloat(sPerc)/100.0,
	pMax = (i<0)? pMin: parseFloat(sPerc.substring(i+1)/100.0);
	S2(1);																				// Set as active control
	ShowNotes(null,null);
	return UpdateMix();
}
// Units for Natures Ultimate in mix
function OnChgNUun() {
	var f = document.MixCalc,
			u = f.OCUnits.value;
	var vMin = ocMin / u,			// Units to display
			vMax = ocMax / u;
	// If DROPS, round to next full drop
	if( f.OCUnits.selectedIndex==0 ) {vMin = Math.ceil(vMin); vMax = Math.ceil(vMax);}
	// Update Display
	f.OCMin.value = (vMin>=10)? Math.round(vMin): cvtFrac(vMin,4);	// Update displayed Max & Min
	f.OCMax.value = (vMax>=10)? Math.round(vMax): cvtFrac(vMax,4);
}
// Update calculated values
function UpdateMix() {
	var f = document.MixCalc;
	// GLOBALS - Update limits (ml)
	ocMin = mxML * pMin;
	ocMax = mxML * pMax;
	// Calculate best units to use
	if( ocMin<1 ) u=0;					// 0-Drops
	else if( ocMin<10 ) u=1;		// 1-Millilitres		
	else if( ocMin<20 ) u=2;		// 2-Teaspoons
	else if( ocMin<30 ) u=3;		// 3-Tablespoons
	else if( ocMin<1000 ) u=4;	// 4-Ounces (Brit)
	else u=7;										// 7-Litres
	// Select appropriate units
	f.OCUnits.selectedIndex = u;
	OnChgNUun();
	return true;			
}
// Notes - Change notes in bottom cell (null restores)
function ShowNotes(sHdr, sTxt) {
	var oHdr = findObj("NotesHdr");
	if( oHdr ) oHdr.innerHTML = (sHdr==null)? "Shake or mix well before each use.": sHdr;
	var oTxt = findObj("NotesTxt");
	if( oTxt ) oTxt.innerHTML = (sTxt==null)? "<p class=nt><b><u>Note:</u></b> Depending on the nature and age of the contaminant being removed and the surface from which it is being removed, the above values may need further adjustment for optimum performance.</p>": sTxt;
}
// Returns number as reduced fraction with divisor = nDiv
function cvtFrac(fVal,nDiv) {
	var nInt = parseInt(fVal), 
			nFrc = parseInt( (fVal-nInt)*nDiv + 0.5);
	if( nFrc<=0 ) return nInt;
	if( nFrc>=nDiv ) return nInt + 1;
	while( nFrc>1 && (f=gcd(nFrc,nDiv))>1 ) {nFrc/=f; nDiv/=f;}
	return ((nInt>0)? nInt+"-": "") + nFrc+"/"+nDiv;			
}
// Returns greatest common divisor
function gcd(v1,v2) {
	while(true) {if((v = v2%v1)==0) return v1; v2=v1; v1=v;}
}
// Read numeric string in form '12','12.34','1-3/4', or '1 3/4'
function parseNumVal(sVal) {
	var reInt=/^\s*(\d+)\s*$/,
			reFloat=/^\s*(\d*\.\d+)\s*$/,
			reFrac0=/^\s*(\d+)\s*\/\s*(\d+)\s*$/,
			reFrac1=/^\s*(\d+)[\s-]+(\d+)\s*\/\s*(\d+)\s*$/;
	if( reInt.test(sVal) ) return parseInt(sVal);
	if( reFloat.test(sVal) ) return parseFloat(sVal);
	if( reFrac0.test(sVal) ) {reFrac0.exec(sVal); return RegExp.$1/RegExp.$2;}
	if( reFrac1.test(sVal) ) {reFrac1.exec(sVal); return parseFloat(RegExp.$1)+RegExp.$2/RegExp.$3;}
	return null;
}
// Round values close to integers to the integers
function RoundInt(v) {
	var f = v - parseInt(v);
	if( f<.0001 || f>.9999 ) v = Math.round(v);
	return v;
}
// Select Step2 type (0-Task, 1-Purpose)
var s2Sel = 1;
function S2(n) {
	if( n!=s2Sel ) {
		var c0, c1, oU, oL;
		if(n) {c0="#BBFFA4"; c1="#66FF33"} else {c0="#66FF33"; c1="#BBFFA4"}   		
		oU = findObj("TaskU");
		oL = findObj("TaskL");
		oU.style.backgroundColor = oL.style.backgroundColor = c0;
		oU = findObj("PurpU");
		oL = findObj("PurpL");
		oU.style.backgroundColor = oL.style.backgroundColor = c1;
		s2Sel = n;
		if( n==1) {
		  document.MixCalc.task.value = 0;
		}
}	}
// Retrieve DIV object with given ID
function findObj(theObj) {
	var oID = false;
	if(!oID && document.all) oID=document.all[theObj];
	if(!oID && document.getElementById) oID=document.getElementById(theObj);
	return oID;
}
function BookmarkPg(title) {
	if( (navigator.appName=="Microsoft Internet Explorer")&&(parseInt(navigator.appVersion)>=4)&&(navigator.userAgent.toLowerCase().indexOf("mac")==-1) ) {
		window.external.AddFavorite(window.location,title);
	} else {
		window.alert("To Bookmark this page,\r Windows users please press CTRL+D on the keyboard.\r Macintosh users please press Command+D on the keyboard.");
}	}
