var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
   
	


function Calprice(){
	
  	start_level=document.getElementById("start_level");
	low_level=document.getElementById("low_level");
	end_level=document.getElementById("end_level");
	high_level=document.getElementById("high_level");
	hours_ss=document.getElementById("hours_ss");
	price_ss=document.getElementById("price_ss");
    pl_unit=document.getElementById("PL_unit");
	time1=document.getElementById("time1");
	price1=document.getElementById("price1");
	
	var high,low,pri,hou,pl_stuat,p,h
	low=low_level.value.split(",");
	high=high_level.value.split(",");
	pri=price_ss.value.split(",");
	hou=hours_ss.value.split(",");
	t=high.length-2;
    pl_stuat=true;
	if (isNaN(start_level.value))
	{alert('The Current Level must is number.');
	pl_stuat=false;
	}
	if (isNaN(end_level.value))
	{alert('The Desired Level must is number.');
	pl_stuat=false;}
	if (parseInt(start_level.value)<parseInt(low[0]))
	{alert('The Current Level must Equivalent to more than '+low[0]+'.');
	pl_stuat=false;}
	if (parseInt(end_level.value)>parseInt(high[t]))
	{alert('The Desired Level must less than or equal to '+high[t]+'.');
	pl_stuat=false;}
	if (parseInt(start_level.value)>=parseInt(end_level.value))
	{alert('The Current Level must less than to Desired Level.');
	pl_stuat=false;}
	var j,k
	if (pl_stuat){
	for(var i = 0; i < low.length-1; i++)
		{
		if (parseInt(start_level.value)>=parseInt(low[i]))
		j=i;
		if (parseInt(end_level.value)>parseInt(high[i]))
		k=i+1;
		else if(parseInt(end_level.value)<=parseInt(high[0]))
		k=0;
		}	

		if (j==k)
		{p=(parseInt(end_level.value)-parseInt(start_level.value))/(parseInt(high[j])-parseInt(low[j]))*parseFloat(pri[j]);
		 h=(parseInt(end_level.value)-parseInt(start_level.value))/(parseInt(high[j])-parseInt(low[j]))*parseFloat(hou[j]);
		 document.level.pl_hours.value=h.toFixed(1)+"Hour";
		 document.level.pl_price.value=p.toFixed(2)+pl_unit.value;
		 time1.value=h.toFixed(1);
		 price1.value=p.toFixed(2);
		 buttom.innerHTML="<input type='button' name='Submit' value='Buy Now' style='width:80px;' />"
		}
		else
		{p=(parseInt(high[j])-parseInt(start_level.value))/(parseInt(high[j])-parseInt(low[j]))*parseFloat(pri[j]);
		 h=(parseInt(high[j])-parseInt(start_level.value))/(parseInt(high[j])-parseInt(low[j]))*parseFloat(hou[j]);
		
		 for(i=j+1;i<k;i++)
		 {  
		 p=p+parseFloat(pri[i]);
		 h=h+parseFloat(hou[i]);
		 }
		
	 p=p+(parseInt(end_level.value)-parseInt(low[k]))/(parseInt(high[k])-parseInt(low[k]))*parseFloat(pri[k]);
	 h=h+(parseInt(end_level.value)-parseInt(low[k]))/(parseInt(high[k])-parseInt(low[k]))*parseFloat(hou[k]);	
			
		 document.level.pl_hours.value=h.toFixed(1)+" Hour";
		 document.level.pl_price.value=p.toFixed(2)+" "+pl_unit.value;
		 time1.value=h.toFixed(1);
		 price1.value=p.toFixed(2);
		 buttom.innerHTML="<input type='button' name='Submit' value='Buy Now' style='width:80px;' onclick='check_form();' />"
		}
	}
	//total.value=((parseFloat(ddQ.value)*parseFloat(ddP.value))*((100-dis)/100)).toFixed(3);
}

function check_form(){

if((start_level.value=="")||(end_level.value==""))
{
window.alert("Tips£º\n plz fill the level£¡")
return false;
}else{
document.level.submit();
}
}
