var needUSDsign=false;

function loadUrgency(n,o) {
	var k,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 k,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! downgrade Level
		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);
}

function calcTotal(o) {
	var i,f=o.form;
	var 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;
	var cu=f.curr.selectedIndex;
	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=f.numpages.selectedIndex; f.numpages.options[i].value<minpg[ty] && i<f.numpages.length;) i++;
		f.numpages.selectedIndex=i;
		pg=f.numpages.options[i].value;
	}
	if(pg) {
		if(f.cppusd) f.cppusd.value=price[ty][ur][le]; // cpp
		t=pg*price[ty][ur][le]; // order subtotal
		if(f.qadcheck) {
			adjustQADcheck(f,pg,ur);
			if(f.qadcheck.checked) t+=parseFloat(f.qadcheck.value); // +additional service
		}
		if(f.totalusd) f.totalusd.value=t;
		t=t*currency[cu];
	}
	if(needUSDsign && currency[cu]==1) f.total.value='$'+moneyFmt(t);
	else f.total.value=moneyFmt(t);
//withDisc(f,t);
}
function withDisc(f,t) { }

var defQADcheck=false;
function qadChange(q) {
	defQADcheck=q.checked;
	calcTotal(q);
}
function adjustQADcheck(f,pg,ur) {
	if(pg>10||ur<24) { // qad check not allowed
		if(f.qadcheck.checked) { defQADcheck=true; f.qadcheck.checked=false; }
		f.qadcheck.disabled=true; 
	} else { // restore checkbox state
		f.qadcheck.disabled=false; 
		if(defQADcheck) f.qadcheck.checked=true;
	}
}

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