var needUSDsign=false;
function chgType(o) {
	f=o.form;
	si_=o.options[o.selectedIndex].value;
	loadUrgency(si_,o);
	loadLevel(si_,o);
	// reset pages to minpages by doctype
	for( i=0; minpg[si_]>0 && i<f.numpages.length; i++){
		if(f.numpages.options[i].value>=minpg[si_]){
			if(f.numpages.selectedIndex<i) f.numpages.selectedIndex=i;
			break;
		}
	}
	calcTotal(o);
}

function loadUrgency(n,o) {
	s=o.form.urgency;
	si=s.selectedIndex;
	for(i=s.length; i>=0; i--) s.remove(i);
	i=0;
	for(k in urg[n]) {
		option_= new Option(urg[n][k],k);
		s.options[i]=option_;
		i++;
	}
	s.selectedIndex= si>=s.options.length ? s.options.length-1:si;
}

function loadLevel(n,o) {
	s=o.form.level;
	si=s.selectedIndex;
	for(i=s.length; i>=0; i--) s.remove(i);
	i=0;
	for(k in lvl[n]) {
		option_= new Option(lvl[n][k],k);
		s.options[i]=option_;
		i++;
	}
	s.selectedIndex= si>=s.options.length ? s.options.length-1:si;
}

function chgUrg(o) {
	f=o.form;
	t=f.doctype.options[f.doctype.selectedIndex].value;
	u=f.urgency.options[f.urgency.selectedIndex].value;
	l=f.level.options[f.level.selectedIndex].value;
	if(!price[t][u][l]){ // no price for this combination!
		alert('Sorry, we can not guarantee this Urgency \n for given Service and Level');
		for(u=u; u>=0; u--) if(price[t][u][l]>0) { setSelByVal(f.urgency,u); break; }
	}
	calcTotal(o);
}

function chgLevel(o) {
	f=o.form;
	t=f.doctype.options[f.doctype.selectedIndex].value;
	u=f.urgency.options[f.urgency.selectedIndex].value;
	l=f.level.options[f.level.selectedIndex].value;
	if(!price[t][u][l]){ // no price for this combination!
		alert('Sorry, we can not guarantee this Level \n for given Service and Urgency');
		for(l=l; l>=0; l--) if(price[t][u][l]>0) { setSelByVal(f.level,l); break; }
	}
	calcTotal(o);
}

function calcTotal(o) {
	f=o.form;
	t="0.00";
	if(f.cppusd) f.cppusd.value='0';
	ty=f.doctype.options[f.doctype.selectedIndex].value;
	ur=f.urgency.options[f.urgency.selectedIndex].value;
	le=f.level.options[f.level.selectedIndex].value;
	cu=f.curr.selectedIndex;
	pg=f.numpages.options[f.numpages.selectedIndex].value;
	if(minpg[ty]>0 &&pg<minpg[ty]) {
		alert('Sorry, Number of pages can not be less than '+minpg[ty]);
		for( i=0; i<f.numpages.length; i++){
			if(f.numpages.options[i].value>=minpg[ty]){
				f.numpages.selectedIndex=i;
				pg=f.numpages.options[i].value;
				break;
			}
		}
	}
	if(pg) {
		if(f.cppusd) f.cppusd.value=price[ty][ur][le]; // cpp
		t=pg*price[ty][ur][le]; // diss total
		if(f.totalusd) f.totalusd.value=t;
		t=t*currency[cu];
	}
	t=moneyFmt(t);
	if(needUSDsign && currency[cu]==1) t='$'+t;
	f.total.value=t
}

function moneyFmt(amnt) {
	amnt=''+Math.round(amnt*100)/100;
	x=amnt.indexOf('.');
	if(x<0) amnt+='.00';
	else if(amnt.length-x==2) amnt+='0';
	return amnt;
}
