var needUSDsign=false;

function loadUrgency(n,o) {
	var i,s=o.form.urgency;
	var 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++;
	}
	si= si<0 ? 0:si;
	s.selectedIndex= si>=s.options.length ? s.options.length-1:si;
}

function loadLevel(n,o) {
	var i,s=o.form.level;
	var 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++;
	}
	si= si<0 ? 0:si;
	s.selectedIndex= si>=s.options.length ? s.options.length-1:si;
}

function chgType(o) {
	var i,f=o.form;
	var si=o.options[o.selectedIndex].value;
	loadUrgency(si,o);
	loadLevel(si,o);
	if(f.numpages.options[f.numpages.selectedIndex].value<minpg[si]) { // upgreade pages to minpages by doctype
		for(i=f.numpages.selectedIndex; f.numpages.options[i].value<minpg[si] && i<f.numpages.length;) i++;
		f.numpages.selectedIndex=i;
	}
	chgUrg(o,1); // downgrade Urgency?
}

function chgUrg(o,silent) {
	var i,ui=99999,f=o.form;
	var t=f.doctype.options[f.doctype.selectedIndex].value;
	var u=f.urgency.options[f.urgency.selectedIndex].value;
	var l=f.level.options[f.level.selectedIndex].value;
	if(!price[t][u][l]){ // there is no price for this combination! downgrade Urgency
		if(!silent) alert('Sorry, we can not guarantee this Urgency\nfor given Service and Level');
		for(i in urg[t]) if(price[t][i][l]) ui=Math.min(i,ui);
		for(i=0; i<f.urgency.options.length; i++) if(f.urgency.options[i].value==ui) {f.urgency.selectedIndex=i;break;}
	}
	calcTotal(o);
}

function chgLevel(o,silent) {
	var i,li=0,f=o.form;
	var t=f.doctype.options[f.doctype.selectedIndex].value;
	var u=f.urgency.options[f.urgency.selectedIndex].value;
	var l=f.level.options[f.level.selectedIndex].value;
	if(!price[t][u][l]){ // there is no price for this combination!
		if(!silent) alert('Sorry, we can not guarantee this Level\nfor given Service and Urgency');
		for(i in lvl[t]) if(price[t][u][i]) li=Math.max(i,li);
		for(i=0; i<f.level.options.length; i++) if(f.level.options[i].value==li) {f.level.selectedIndex=i;break;}
	}
	calcTotal(o);
}

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;
	var i,s=o.form.quality;
	var 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 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) {
	var i,f=o.form;
	var cpp=0,t="0.00";
	if(f.cppusd) f.cppusd.value='0';
	var ty=f.doctype.options[f.doctype.selectedIndex].value;
	var ur=f.urgency.options[f.urgency.selectedIndex].value;
	var le=f.level.options[f.level.selectedIndex].value;
	loadQuality(o,le);
	var dr=checkNumpages(f,ur);//doublerate
	var qu=f.quality.selectedIndex;
	var cu=1;
	var 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]*dr*quCoeff[qu]; // price/pg in GBPP * double rate * guality correction
		if(f.cppusd) f.cppusd.value=cpp/currency; // $cpp
		t=pg*cpp; // total, GBP
	}
	f.total.value=moneyFmt(t); // GBP
	if(f.totalusd) 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;}
//withDisc(f,t);
}
function withDisc(f,t) { }

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