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;
}

var curQuality=0;
function loadQuality(o,n) {
	if(n>=8&&n<=11) n=1; else n=0; // level 8-11 needs another Quality list
	if(curQuality==n) return;
	curQuality=n;
	s=o.form.quality;
	si=s.selectedIndex;
	for(i=s.length; i>=0; i--) s.remove(i);
	for(i=0; i<qualsA[n].length; i++) {
		option_= new Option(qualsA[n][i],i);
		s.options[i]=option_;
	}
	if(n>0&&si>1) si=1;
	s.selectedIndex=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 checkNumpages(f,u) {
	var _pg=f.numpages.options[f.numpages.selectedIndex].value;
  d=f.drate.checked ? 2:1;
//  if( (d==1 && _pg>maxpg[u]) || (d==2 && _pg>2*maxpg[u]) ){
//    alert('Sorry, we are afraid that at Standard rate we can\ndeliver no more than '+250*maxpg[u]+' words within selected Delivery Time.\nPlease revise the Piece Length, the Delivery Time-frame\nor consider our Double Rate option.\nEeven at Double Rate we are unable to complete\nmore than '+500*maxpg[u]+' words.');
//    while(f.numpages.selectedIndex>=0) { f.numpages.selectedIndex--; _pg=f.numpages.options[f.numpages.selectedIndex].value; if(_pg<=d*maxpg[u]) break; }
//  }
  return _pg>maxpg[u] ? 2:1; // 2 - we really need x2
}

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;
	loadQuality(o,le);
	dr=checkNumpages(f,ur);//doublerate
	qu=f.quality.selectedIndex;
	cu=1;
	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) {
		f.currency.value=currency;// GBP/USD
		cpp=price[ty][ur][le]; // price pp GBPP
		cpp=dr*cpp; // double rate
		cpp=cpp*quCoeff[qu]; // guality correction
		if(f.cppusd) f.cppusd.value=cpp/currency; // $cpp
		t=pg*cpp; // total GBP
		if(f.totalusd) f.totalusd.value=t;
	}
	f.total.value=moneyFmt(t);// GBP
	f.totalusd.value=t/currency;// total USD
//if(qu==2) {f.dsc_code.value=''; f.dsc_code.disabled=true;}
//else {f.dsc_code.disabled=false; f.dsc_code.value=f.dsc_code.defaultValue;}
}

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;
}
