//	FOLD1: Fast Online Lighting Design
//	(c) copyright 2004-2005 FOLD Systems / M.J. Meijer
//	licensed use only
//	E: info@fold1.com W: www.fold1.com
//
//	$Name:  $
//	$Revision: 3.1 $
//	$Author: maarten $
//
//
//	fold1.js
//
function firstFocus()
{
	var i;
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      {
         if ((TForm.elements[i].type=="text")||
           (TForm.elements[i].type=="textarea")||
           (TForm.elements[i].type.toString().charAt(0)=="s"))
         {
            document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}

function getSpecRef() {
		return self.document.getElementById("spec");
	}

function selectCeiling() {
	var spec = self.document.getElementById('spec');
	var ctype = spec.ctype.options[spec.ctype.selectedIndex].value;

	if(ctype >= 20) {
		spec.cor.disabled = false;
		spec.cor.selectedIndex = 1;
	}
	if(ctype >= 1) {
		spec.cor.disabled = false;
		spec.cor.selectedIndex = 1;
	} else {
		spec.cor.disabled = true;
		spec.cor.selectedIndex = 0;
	}
	if(ctype == 1) { // fixed ceiling
		spec.mount.selectedIndex = 2;
	} else { // suspended ceiling
		spec.mount.selectedIndex = 1;
		}
return true;
}


function toDigitsOnly(s) {
	if(navigator.appName.indexOf("Netscape")!= -1)
		if(s == '')
			return '0';
		else
			return s;
	if (navigator.appName.indexOf("Microsoft")== -1) {
		var n ='';
		for(i = 0; i < s.length; i++) {
			switch( s[i] ) {
				case ',':
					n += '.';
					break
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case '.':
					n += s[i];
					break;
				default:
					break;
				}
			}
		if(n.length == 0) n = '0';
	//	item.value = n;
		return n;
		}
	// else MSIE
	var val = parseFloat(s.replace(/,/, "."));
	if(isNaN(val)) {
//		alert( "setting to zero");
		return 0;
	} else {
		return ((Math.floor(100 * val)) / 100);
		}
	}

function calcK() {
	var spec = getSpecRef();
	spec.w.value = toDigitsOnly( spec.w.value ) ;
	var w = parseFloat( spec.w.value );
	spec.l.value = toDigitsOnly( spec.l.value );
	var l = parseFloat( spec.l.value );
	spec.ph.value = toDigitsOnly( spec.ph.value );
	var ph = parseFloat( spec.ph.value );
	spec.wh.value = toDigitsOnly( spec.wh.value );
	var wh = parseFloat( spec.wh.value );

	var h = ph - wh;
	spec.h.value = Math.round(100 * h) / 100;

	var kf = ( w * l ) / ( ( w + l ) * h );
	var ka = kf;

	spec.k.value = Math.round(100 * kf) / 100;
	if( kf < 0.7 )      { ka = "0.60"; }
	else if (kf < 0.90) { ka = "0.80"; }
	else if (kf < 1.12) { ka = "1.00"; }
	else if (kf < 1.37) { ka = "1.25"; }
	else if (kf < 1.75) { ka = "1.50"; }
	else if (kf < 2.25) { ka = "2.00"; }
	else if (kf < 2.75) { ka = "2.50"; }
	else if (kf < 3.50) { ka = "3.00"; }
	else if (kf < 4.50) { ka = "4.00"; }
	else                { ka = "5.00"; }
	spec.ka.value = ka;
	return true;
}


function newK() {
	if(checkRoom() == false) {
		return false;
		}
	calcK();
	return true;
	}

function newLux() {
	var spec = getSpecRef();
	var appl = spec.luxadvisor.options[spec.luxadvisor.selectedIndex].text;
	spec.application.value = appl;
	if(appl.charAt(appl.length-1) == '*') {
		setIndustrial();
	} else {
		setCommercial();
	}

	var lux1 = parseFloat( spec.luxadvisor.options[spec.luxadvisor.selectedIndex].value );
	spec.lux.value = lux1;
	return true;
	}

function checkLux() {
	var spec = getSpecRef();
	var lux1 = parseFloat( spec.luxadvisor.options[spec.luxadvisor.selectedIndex].value );
	spec.lux.value = toDigitsOnly( spec.lux.value );
	var lux = parseFloat( spec.lux.value );

	if( spec.luxadvisor.selectedIndex == 0) {
		show_error(1); // "Kies een toepassing van de ruimte."
		spec.luxadvisor.focus();
		return false;
		}

	if( lux1 != lux && (lux < 20.0 || lux > 2000.0)) {
		show_error(2); // "Luxwaarde moet tussen 20 en 2000 zijn"
		spec.lux.value = lux1;
		spec.lux.focus();
		return false;
		}
	return true;
	}

function checkTime() {
	var spec = getSpecRef();
	var uren = parseFloat( spec.uren.options[spec.uren.selectedIndex].value );

	if( spec.uren.selectedIndex == 0 || uren <= 0.0 || uren > 8760.0) {
		show_error(3); // "Kies een bedrijfsttijd"
		spec.uren.focus();
		return false;
		}
	return true;
	}

function checkCeiling() {
	var spec = getSpecRef();

	if( spec.ctype.selectedIndex == 0) {
		show_error(4); // "Kies een plafondtype"
		spec.ctype.focus();
		return false;
		}
	return true;
	}


function newDep() {
	var spec = getSpecRef();
	spec.MF.value  = toDigitsOnly( spec.MF.value );
	var mf = parseFloat( spec.MF.value );

	if( mf <= 0.0 || mf >= 1.0) {
		show_error(5); // "Behoudfactor moet tussen 0.0 en 1.0 zijn"
		spec.MF.value = 0.8;
		spec.MF.focus();
		return false;
		}
	return true;
	}

function checkKWH() {
	var spec = getSpecRef();
	spec.kwh.value = toDigitsOnly( spec.kwh.value );
	var x = parseFloat( spec.kwh.value );

	if( x < 0.0 || x > 1.0) {
		show_error(6); // "Energietarief moet tussen 0.00 en 1.00 zijn"
		spec.kwh.value = 0.16;
		spec.kwh.focus();
		return false;
		}
	return true;
	}

function checkInstall() {
	var spec = getSpecRef();
	spec.install.value = toDigitsOnly( spec.install.value );
	var x = parseFloat( spec.install.value );

	if( x < 0.0 || x > 100.0) {
		show_error(7); // "Installatiekosten moet tussen 0.00 en 100.00 zijn"
		spec.install.value = 35;
		spec.install.focus();
		return false;
		}
	return true;
	}

function checkDiscount() {
	var spec = getSpecRef();
	spec.discount.value = toDigitsOnly( spec.discount.value );
	var percent = parseFloat( spec.discount.value );

	if( percent < 0.0 || percent > 100.0) {
		show_error(8); // "Korting moet tussen 0 en 100 zijn"
		spec.discount.value = 0;
		spec.discount.focus();
		return false;
		}
	return true;
	}

function checkLDiscount() {
	var spec = getSpecRef();
	spec.ldiscount.value = toDigitsOnly( spec.ldiscount.value );
	var percent = parseFloat( spec.ldiscount.value );

	if( percent < 0.0 || percent > 100.0) {
		show_error(9); // "Korting moet tussen 0 en 100 zijn"
		spec.ldiscount.value = 0;
		spec.ldiscount.focus();
		return false;
		}
	return true;
	}

function checkRoom() {
	var spec = getSpecRef();
	spec.w.value = toDigitsOnly( spec.w.value ) ;
	var w = parseFloat( spec.w.value );
	spec.l.value = toDigitsOnly( spec.l.value );
	var l = parseFloat( spec.l.value );
	spec.ph.value = toDigitsOnly( spec.ph.value );
	var ph = parseFloat( spec.ph.value );
	spec.wh.value = toDigitsOnly( spec.wh.value );
	var wh = parseFloat( spec.wh.value );
	var h = ph - wh;

	if(l < 2 || l > 80) {
		show_error(10); // "'Lengte' moet tussen 2.0 en 80.0 zijn"
		spec.l.value = spec.l.defaultValue;
		spec.l.focus();
		return false;
		}

	else if(w < 1.8 || w > 80) {
		show_error(11); // "'Breedte' moet tussen 1.8 en 80.0 zijn"
		spec.w.value = spec.w.defaultValue;
		spec.w.focus();
		return false;
		}

	else if( w > l) {
		show_error(12); // "'Breedte' moet kleiner dan 'Lengte' zijn"
		spec.w.value = l;
		spec.l.value = w;
		spec.w.focus();
		return false;
		}

	else if(ph < 2 || ph > 12) {
		show_error(13); // "'Plafond' moet tussen 2.0 en 12.0 zijn"
		spec.ph.value = spec.ph.defaultValue;
		spec.ph.focus();
		return false;
		}

	else if(wh < 0 || wh > ph) {
		show_error(14); // "'Werkvlak' moet 0.0 of groter zijn en kleiner dan 'Plafond' zijn"
		spec.wh.value = spec.wh.defaultValue;
		spec.wh.focus();
		return false;
		}

	else if(h < 1.2) {
		show_error(15); // "Verschil 'Plafond' en 'Werkvlak' moet 1.2 of groter zijn. Verhoog 'Plafond' of verlaag 'Werkvlak'"
		spec.wh.value = spec.wh.defaultValue;
		spec.wh.focus();
		return false;
		}
	return true;
	}

function checkForm() {
	if (!checkCeiling() )
		return false;
	if (!checkRoom() )
		return false;
	else if( !checkLux() )
		return false;
	else if( !checkTime() )
		return false;
	else if( !checkDiscount() )
		return false;
	else if( !checkLDiscount() )
		return false;
	else if( !newDep() )
		return false;
	return true;
	}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires.toGMTString() : "") +
		( (path) ? ";path=" + path : "") +
		( (domain) ? ";domain=" + domain : "") +
		( (secure) ? ";secure" : "");
	}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
		}
	return null;
	}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}

function browserAcceptsCookies() {
	var accept = false;
	SetCookie('cookietest', 'accept cookies?');

	if(GetCookie('cookietest') == 'accept cookies?')
		accept = true;
	// clean up!
	DeleteCookie ('cookietest');
	return accept;
	}

function changePic(id, src, explanation) {
	myImage(id).src = src;
	myImage(id).alt = src;
	if(arguments.length >= 3)
		myImage(id).title = explanation;

}

function myImage(id) {
	// browser supports DOM
	return self.document.getElementById(id);
}

var req;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

var errorText = new Array();

function show_error(errNo) {
	alert(errorText[errNo]);
}

function setIndustrial() {
	var spec = getSpecRef();
	if(spec.reflectance.options[0].value == "50 30 10")
		return;
// reflectances
	spec.reflectance.options.length=0;
	spec.reflectance.options[0] = new Option("50 30 10", "50 30 10", true, false);
	spec.reflectance.options[1] = new Option("30 10 10", "30 10 10", false, false);
// ceiling types
	spec.ctype.selectedIndex=1;
	spec.ctype.options[2].disabled=true;
	spec.ctype.options[3].disabled=true;
	spec.ctype.options[4].disabled=true;
//	orientation
	spec.cor.disabled = false;
	spec.cor.selectedIndex = 1;
//	mounting
	spec.mount.selectedIndex = 2;
	spec.merken.selectedIndex = 2;
}

function setCommercial() {
	var spec = getSpecRef();
	if(spec.reflectance.options[0].value == "80 50 30")
		return;
// reflectances
	spec.reflectance.options.length=0;
	spec.reflectance.options[0] = new Option("80 50 30", "80 50 30", false, false);
	spec.reflectance.options[1] = new Option("70 50 20", "70 50 20", true, false);
	spec.reflectance.options[2] = new Option("70 30 20", "70 30 20", false, false);
	spec.reflectance.options[3] = new Option("50 30 10", "50 30 10", false, false);
	spec.reflectance.selectedIndex = 1;
// ceiling types
	spec.ctype.selectedIndex=0;
	spec.ctype.options[2].disabled=false;
	spec.ctype.options[3].disabled=false;
	spec.ctype.options[4].disabled=false;
// orientation
	spec.cor.disabled = true;
	spec.cor.selectedIndex = 0;
// mounting
	spec.mount.selectedIndex = 0;
	spec.merken.selectedIndex = 0;
}

function showHide(elementId) {
	var element = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
	if(element.style.display != 'none' && element.style.display != '') {
		element.style.display = 'none';
	} else {
		element.style.display = 'block';
	}
}
//-->
