
function gr(id){return getRef(id);}
function gs(id){return getSty(id);}
function gsh(id){gs(id).display='none';}
function gsw(id){gs(id).display='block';}
function gswi(id){gs(id).display='inline';}
function gsd(id){return gs(id).display;}
function gsb(id){return gs(id).backgroundColor;}
function gri(id,h){gr(id).innerHTML=h;} 
function grv(id){return gr(id).value;} 

function alert1(m){return;/*gr('drep').innerHTML+=m+"<br>";*/}

function calc_class()
{
	this.is_total = false;
	this.is_qn=false;
	this.is_start = true;
	this.tid = null;
	this.bob = null;
	
	this.vn_start='';
	this.n_box=2;
	this.vht = 1/61023.744094732;//inch
	this.wht = 1;//lbs
	this.vst = 1;//cbm
	this.wst = 1;//lbs
	this.fvlm= 0;
	this.fwlm= 0;
	this.vlm_act = false;
	this.e = Array('srv','ocnt','ocity','dcnt','dcity','cmd','vlm');
	this.def_price = "USD 0";

	this.sl = function()
	{
		gs('calc_ldr').width = gs('calc_block').width = gr('calc_table').offsetWidth;
		gs('calc_ldr').height = gs('calc_block').height = gr('calc_table').offsetHeight;
	}
	this.lock = function(e)
	{
		this.sl();
		this.lk();
		if(e == 'ocnt')gsh("tab_dcnt");
		if(e == 'ocity')gri("td_dcity",'');
		if(e == 'dcnt')gsw("tab_cmd");
		if(e)
		{
			if(e != 'cmd')gsh("tab_cmd"); 
			gri('total_sum',this.def_price);
			this.is_total = false;
		}
	}
	this.lk = function(){gsw('tC_block');}
	this.unlock = function(){gsh('tC_block');}
	this.req = function(name){r="";for(i=0;i<this.e.length;i++){r+=this.e[i]+'='+grv(this.e[i]);if(name==this.e[i])break;r+='&';}return r;}
	this.act = function(o)
	{
		name = o.name;
		this.sbl2();
		if(o.options[0].value == 0) o.remove(0);
		this.vlm_act = (name == 'cmd') ? true : false;
		if(name == 'ocnt' || name == 'dcnt')gs(name+"_s").background = "url(imgs/calc/gs.gif) no-repeat center";
		if(name == 'cmd'){this.ch_vt(gr('vlm_type'));this.ch_wt(gr('wlm_type'));}
		//if(name == 'dcity' || name == 'cmd'){gr('vlm').focus();gr('vlm').select();}
		//if(this.is_qn){this.is_qn=false;this.o_brf();}
		this.lock(name);
		ReqOnSer(this.req(name));
	}
	this.act3 = function()
	{
		this.sbl2();
		this.vlm_act = true;
		this.lock('cmd');
		ReqOnSer(this.req('cmd'));
	}
	this.act1 = function(name)
	{
		if(name == 'ocnt')gs(name+"_s").background = "url(imgs/calc/gs.gif) no-repeat center";
		//this.sl();
		this.lk();
		ReqOnSer(this.req(name));
	}
	this.act2 = function()
	{
		this.bl('ocnt');
	}
	this.set_option = function(name,opt)
	{
		switch(name)
		{
			case 'ocnt': t='1';break;
			case 'ocity':t='2';break;
			case 'dcnt': t='3';break;
			case 'dcity':t='4';break;
			case 'cmd':  t='5';break;
		}
		sel = "<select tabindex="+t+" id="+name+" name="+name+" onChange='c.act(this)'>";
		sel += "<option value=0>"+opt[0]+"</option>";
		for(i=1;i<opt.length;i++)
			sel += "<option title=\""+opt[i]+"\" value=\""+opt[i]+"\">"+opt[i]+"</option>";
		sel += "</select>";
		gr('td_'+name).innerHTML = sel;
		gs(name).backgroundColor = "#9dcb8f";//"yellow";
		if(name=='dcnt')gsw("tab_"+name);
		this.bl(name);
	}
	this.set_cmd = function(opt)
	{
		
		sel = "<select id=cmd name=cmd tabindex=5 onChange='c.act(this)'>";
		for(i=0;i<opt.length;i++)
			sel += "<option value=\""+opt[i]+"\""+ ( ( opt[i] == "Personal/Household Goods, not for sale" ) ? " selected " : "" ) +">"+opt[i]+"</option>";
		sel += "</select>";
		gr('td_cmd').innerHTML = sel;
		gsw("tab_cmd");
		this.act(getRef('cmd'));
	}	
	this.set_input = function(name,opt)
	{
		getRef('td_'+name).innerHTML = "<span class=tC_inp>"+opt[0]+"</span><input type=hidden id="+name+" name="+name+" value=\""+opt[0]+"\" readonly=1 />";
		this.lock(name);
		if(name=='cmd' || name=='dcnt')getSty("tab_"+name).display = 'block';
		if(name=='cmd'){this.act3();return;}
		ReqOnSer(this.req(name));
	}
//---
	this.get_tab_info = function(m1,m2)
	{
		str = "<table class=clc_line border=0 cellspacing=0 cellpadding=0>";
		for(i=0;i<m1.length;i++)
			str += "<tr><td width=160>"+m1[i]+"</td><td align=right><strong>"+m2[i]+"</strong></td></tr>";
		str += "</table>";
		return str;
	}
//---
	this.cmd = function(tab1,tab2,tab3,tab4)
	{
		gri('rate_det',this.get_tab_info(tab1,tab2));gri('rate_com',tab3);gri('vw_info',tab4+" <a href=javascript:c.show_help('hlp5') title='Click for help. This link will open a new window.'>?</a>");gsw('rate_com');gsw('tab_vw');this.vlm();
	}
	this.is_num = function(v)
	{
		re = /,/gi;
		n = new Number(v.replace(re, "."));
		if(!isNaN(n)) return n;
		alert("Enter number only!"); 
		return false;
		//return v;
	}
	this.rd = function(n){n=parseFloat(n);if(!isNaN(n)){ n*=Math.pow(10, 2); n=Math.round(n); n/=Math.pow(10, 2);} else {return "NaN";} return n;}
	this.sinf=function(){getRef('td_hhg').innerHTML = gr('cmd').value+": "+this.fvlm+" cbm, "+this.fwlm+" lbs"; alert1(gr('td_hhg').innerHTML);}
	this.tws = function(){this.fwlm=r=this.rd(grv('wlm')*this.wst);gr('wlm_cbm').innerHTML=r;gr('wlm_s').value=r;this.sinf();}
	this.tvs = function(){this.fvlm=r=this.rd(grv('vlm')*this.vst);gr('vlm_cbm').innerHTML=r;gr('vlm_s').value=r;this.sinf();}
	this.vlm = function()
	{
		//gri('drep','');
		alert1('run vlm');
		if(!this.vlm_act){alert("Please, Enter Full Origin and Destination Information Before!");return false;}
		s = getRef('vlm');
		if(s.value=='0')return;
		if(s.value.length<1 || !this.is_num(s.value)){alert('Enter Estimated Volume!');return false;}else{s.value=this.is_num(s.value);}
		this.tvs();
		this.wlm();
	}
	this.wlm = function()
	{
		alert1('run wlm');
		if(!this.vlm_act){alert("Please, Enter Full Origin and Destination Information Before!");return;}
		s = getRef('wlm');
		//if(s.value.length<1 || s.value=='0'){alert("Enter Weight!"); s.focus(); s.select(); return;}
		if(s.value=='0')return;
		if(s.value.length<1 || !this.is_num(s.value)){alert('Enter Estimated Weight!');return false;}else{s.value=this.is_num(s.value);}
		this.tws();
		this.sl();
		this.lk();
		s = "&vlm="+this.fvlm;
		ReqOnSer("cmd_info=1&"+this.req('cmd')+s);
	}
//---
	this.price = function()
	{
		if(grv('vlm')=='0' || grv('wlm')=='0'){alert('Enter Estimated Volume and Weight!');return;}
		this.vlm();
	}
	this.total = function(sum)
	{
		alert1('run total');
		getRef('ocf').innerHTML = "$"+sum;
		gr('sfr_pr').value = sum;
		sum = Number(sum);
		ds = (sum+89)*0.0379 > 25 ? (sum+89)*0.0379 : 25;
		getRef('dsf').innerHTML = "$"+Math.floor(ds);
		it = getRef('tpr').value = new String( Math.floor( Number(sum)+Number(89)+Number(ds) ) );
		getRef('total_sum').innerHTML = "&nbsp;USD " + it;
		getRef('td_ror').innerHTML = getRef('ocnt').value+", "+getRef('ocity').value;
		getRef('td_rds').innerHTML = getRef('dcnt').value+", "+getRef('dcity').value;
		this.sinf();
		//getRef('td_hhg').innerHTML = getRef('cmd').value+": "+getRef('vlm_s').value+"cbm, "+getRef('wlm_s').value+"lbs";
		this.is_total = true;
		if( getSty('hh2').display == 'none' )getSty('lrt').display = 'block';
		gsw('tab_route');
		alert1('end total');
	}
//---
	this.get_vw = function(i)
	{
		return "<div id=drow"+i+"><table border=0 cellspacing=0 cellpadding=0><tr><td><input type=text id=vn"+i+" name=vn"+i+" style='width:40px; border:1px solid #999;' onBlur='c.tv();c.tw();' value=1 /></td></td><td>&nbsp;</td><td><input type=text id=vl"+i+" name=vl"+i+" style='width:40px; border:1px solid red;' onBlur='c.tv()' /></td><td><input type=text id=vw"+i+" name=vw"+i+" style='width:40px; border:1px solid green;' onBlur='c.tv()' /></td><td><input type=text id=vh"+i+" name=vh"+i+" style='width:40px; border:1px solid blue;' onBlur='c.tv()' /></td><td>&nbsp;&nbsp;</td><td><input type=text id=vt"+i+" name=vt"+i+" style='width:40px; border:none;' readonly=1 /></td><td>&nbsp;</td><td><input type=text name=wp"+i+" id=wp"+i+" style='width:40px; border:1px solid;' onBlur='c.tw()' /><td><input type=text name=wt"+i+" id=wt"+i+" style='width:40px; border:none;' readonly=1 /></td><td><input type=image src=/imgs/calc/reset1.gif onclick='c.del_box("+i+")' /></tr></table></div>";
	}
	this.add_box = function()
	{
		getRef('box_list').innerHTML += this.get_vw(this.n_box);
		this.n_box++;
	}
	this.del_box = function(i)
	{
		gr('vn'+i).value=0;gsh('drow'+i);this.tv();this.tw();
	}
	this.reset_box = function()
	{
		getRef('box_list').innerHTML = this.vn_start;
		this.n_box=2;this.tv();this.tw();
	}
	this.tv = function()
	{
		sum = 0;
		
		for(i=1;i<this.n_box;i++)
		{
			t = 0;
			vn = this.is_num(getRef('vn'+i).value);
			l = this.is_num(getRef('vl'+i).value);
			w = this.is_num(getRef('vw'+i).value);
			h = this.is_num(getRef('vh'+i).value);
			if(l && w && h && vn)
				sum += getRef('vt'+i).value = this.rd(vn*l*w*h*this.vht);
//				alert(vn+" x "+l+" x "+w+" x "+h+" x "+this.vht);
		}
		sum = this.rd(sum);
		getRef('vlm').value = sum;
		gri('vlm_cbm',sum);
		this.vlm();
	}
	this.tw = function()
	{
		sum = 0;
		for(i=1;i<this.n_box;i++)
		{
			nn = this.is_num(getRef('vn'+i).value);
			vv = this.is_num(getRef('wp'+i).value);
			if(vv && nn)
				sum+= getRef('wt'+i).value = this.rd(vv*nn*this.wht);
		}
		sum = this.rd(sum);
		getRef('wlm').value = sum;
		gri('wlm_cbm',sum);
		this.vlm();
	}
	this.qcl = function()
	{
		gsh('q_atr');
		gsw('bsq');
	}
	this.send_quote = function(o)
	{
		if(!this.is_total){alert("Please complete previous steps first.\nSelect ORIGIN & DESTINATION and enter estimated VOLUME & WEIGHT.");return;}
		o.style.display = 'none';gsw('q_atr');
	}
	this.quote = function()
	{
		getRef('quote').value='1';
		getRef('calc').submit();
	}
	this.o_brf = function(){gsw('div_bf');gsw('bkpc');gsh('bkp');document.location="#obf";}
	this.open_brf = function()
	{
		if(!this.is_total){alert("Please complete previous steps first.\nSelect ORIGIN & DESTINATION and enter estimated VOLUME & WEIGHT.");return;}
		this.o_brf();
	}
	this.close_brf = function()
	{
		gsh('div_bf');gsh('bkpc');gsw('bkp');
		document.location = "#obf_s";
	}
	this.sh = function()
	{
		if(this.vn_start=='')this.vn_start = gr('box_list').innerHTML;
		getSty('vlm_b').display = 'block';getSty('hlpc').display ='block';
		getRef('vlm_type').value = 'cbm';getRef('vlm_type').disabled = true;getRef('vlm').readOnly = true;gs('vlm').color='gray';
		getRef('wlm_type').value = 'lbs';getRef('wlm_type').disabled = true;getRef('wlm').readOnly = true;gs('wlm').color='gray';
		this.ch_vt(getRef('vlm_type'));this.ch_wt(getRef('wlm_type'));
		c.tv();c.tw();
		gri('ent1','<font color="#999999">Total Estimated Volume</font>');gri('ent2','<font color="#999999">Total Estimated Weight</font>');gsh('need_help');
		gri('vw_help',"<div style='font:bold 14px Arial;'>Hard to estimate total volume and weight?<br>Try to enter dimensions and weight for each item.&nbsp;<a href=javascript:c.show_help('hlp7') title='Click for help. This link will open a new window.' style='font:11px;'>?</a></div>");
		document.location = "#obf_s";
	}
	this.hh = function()
	{
		getSty('vlm_b').display = 'none';getSty('hlpc').display ='none';
		getRef('vlm_type').disabled = false;getRef('wlm_type').disabled = false;
		getRef('vlm').readOnly = false;getRef('wlm').readOnly = false;
		gs('vlm').color='black';gs('wlm').color='black';
		gri('ent1','Enter Estimated Volume');gri('ent2','Enter Estimated Weight');gsw('need_help');
		gri('vw_help',"Enter estimated total Volume and Weight / Get total freight price on your shipment&nbsp;<a href=javascript:c.show_help('hlp6') title='Click for help. This link will open a new window.'>?</a>");//Enter estimated total Volume and Weight on your shipment
	}
	this.ch_vd = function(o)
	{
		switch(o.value)
		{
			case 'cm': this.vht = 0.000001;break;
			case 'meter': this.vht = 1;break;
			case 'inch': this.vht = 1/61023.744094732;break;
			case 'feet': this.vht = 1/35.314666721;break;
		}
		this.tv();
	}
	this.ch_wd = function(o)
	{
		switch(o.value)
		{
			case 'lbs': this.wht = 1;break;
			case 'kg': this.wht = 2.2;break;
		}
		this.tw();
	}
	this.ch_vt = function(o)
	{
		switch(o.value)
		{
			case 'cbm': this.vst = 1;break;
			case 'cbf': this.vst = 1/35.314666721;break;
		}
		this.vlm();
	}
	this.ch_wt = function(o)
	{
		switch(o.value)
		{
			case 'lbs': this.wst = 1;break;
			case 'kg': this.wst = 2.2;break;
		}
		this.vlm();
	}
	this.bl = function(o){this.tid = setInterval("gbl( )", 500);this.bob = o;}
	this.gbl = function(){getSty(this.bob).backgroundColor = getSty(this.bob).backgroundColor=='white' ? '#9dcb8f':'white';}
	this.sbl = function(o){if(o!= this.bob)return;this.sbl2();}
	this.sbl2 = function(){if(this.bob==null)return;clearInterval(this.tid);getSty(this.bob).backgroundColor = "white";this.bob = null;}
	this.show_help = function(ur){help_win = window.open("/help_file.php#"+ur,"subWind","height=400,width=300,scrollbars,title=no");help_win.focus();}

}
function gbl(){c.gbl();}
//---
if (!window.XMLHttpRequest){window.XMLHttpRequest = function(){var types=['Microsoft.XMLHTTP','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];for(var i=0;i<types.length;i++){try{return new ActiveXObject(types[i]);}catch(e){}}return false;}}
var req;
function processReqChange(){if(req.readyState==4){c.unlock();eval(req.responseText);}}
function ReqOnSer(url)
{
	//document.calc.adr.value = "answer.php?" + url;
    req = null;
    req=new XMLHttpRequest();
    if(req){req.onreadystatechange = processReqChange;req.open("GET", "/answer.php?" + url + "&r=" + Math.random(), true);req.send(null);}
}
 