
var httpRequest;
 var globi;
 var finalvaue;
 var flg=0;
 
   /**
    * This method is called when the author is selected
    * It creates XMLHttpRequest object to communicate with the 
    * servlet 
    */
    function getProfile1(qry,getH,sel,i){
		
		///alert(code);
		//alert(sel)
	if(sel == "select")
		{
			alert("Select the Country");
			return false;
		}
	globi=i;
	if(i==3)
	{
		document.getElementById("files-root2").innerHTML="<input type='hidden' name='final'>";
	}

	 var v="'";
   	var urlparam=v+getH+v;

	if(i==1)
		{
			url='JavaXML?code=select draft from tbl_portlist where portname='+urlparam+'and countrycode='+sel;
		}
		if(i==2)
		{
			url='JavaXML?code=select draft from tbl_portlist where portname='+urlparam+'and countrycode='+sel;
		}
	if(i==3)
	{
		var url=qry+getH;
	}else{

	}

	if (window.ActiveXObject){
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest){
            httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("GET",url,true);
		httpRequest.onreadystatechange = function() {processRequest1();} 
        httpRequest.send("HTML");
   }
   /**
    * This is the call back method
    * If the call is completed when the readyState is 4
    * and if the HTTP is successfull when the status is 200
    * update the profileSection DIV
    */
    function processRequest1()
    {
        if (httpRequest.readyState == 4)
        {
			if(httpRequest.status == 200)
            {
				//get the XML send by the servlet
				var profileXML = httpRequest.responseXML.getElementsByTagName("Profile")[0];
				//Update the HTML
				updateHTML1(profileXML);
            }
            else
            {
                alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
            }
        }
    }
       
   /**
    * This function parses the XML and updates the 
    * HTML DOM by creating a new text node is not present
    * or replacing the existing text node.
    */
     function updateHTML1(profileXML)
    {
        //The node valuse will give actual data
        var profileText = profileXML.childNodes[0].nodeValue;
	//Here assingn draft for load port//
	//alert(profileText);
	  backupvariable(profileText,globi);
	
	//Create the Text Node with the data received
        var profileBody = document.createTextNode(profileText);
        //Get the reference of the DIV in the HTML DOM by passing the ID
        var profileSection = document.getElementById("profileSection");
        //Check if the TextNode already exist
        if(profileSection.childNodes[0])
       {
           //If yes then replace the existing node with the new one
          // profileSection.replaceChild(profileBody, profileSection.childNodes[0]);
        }
        else
        {
          //If not then append the new Text node
           // profileSection.appendChild(profileBody);
        }       
		var Arr2=profileText.split(",");
var Arr=new Array();
			for(i=1;i<Arr2.length;i++)
			{
			Arr[i]=Arr2[i-1];
			}
		
			if((document.forms[0].loadhid.value!="")&&(document.forms[0].dishid.value!=""))
			{
				if(globi==3)
				{
				}else{
				//	alert("3 here");
				if(globi==2)
				{
			
				totalCalculation();
			
//				var fin=document.forms[0].final.value=profileText;
//			    finalvaue=fin;
			
				flg++;
				
//		var li = document.createElement('li');
			//li.setAttribute('id', 'file-' + gFiles);
	//		li.innerHTML = '&nbsp<span onclick="removeFile()" style="cursor:hand;color:blue;">finalvaue</span>';
	//		document.getElementById('files-root').appendChild(li);
		//	document.getElementById('fileroot').innerHTML=document.write(finalvaue);
			}
			}
 }
	

    }
	function backupvariable(profileText,i)
	{
		if(i!=3)
		{
		 var Arr_may2=profileText.split(",");
	var prof;
 		var Arr_may=new Array();
			for(mee=1;mee<Arr_may2.length;mee++)
			{
			Arr_may[mee]=Arr_may2[mee-1];
			 prof=Arr_may[mee];
			}
		}
		//alert(prof);
		if(i==1)
		{
		document.forms[0].loadhid.value="";
		document.forms[0].loadhid.value=Arr_may2;
	//	alert("1");
		}
		if(i==2)
		{
		document.forms[0].dishid.value="";
		document.forms[0].dishid.value=Arr_may2;
	//	alert("2");
		}
		if(i==3)
		{
		document.forms[0].final.value="";
		document.forms[0].final.value=profileText;
		temp	(document.forms[0].final.value);
	
		}
		return true;
	}
	function totalCalculation()
	{
		
			var loadPort=document.forms[0].loadhid.value;
			var discPort=document.forms[0].dishid.value;
			if(loadPort.length>0&&discPort.length>0)
			{
 
	
		if(loadPort>discPort)
				{
					draft=discPort;
				}
				else
				{
				draft=loadPort;
				}

				
				var draft1=0.0;
				var draft2=0.0;
				var draft3=0.0;
				var draft4=3.0;
				var draft5=4.0;
				var draft6=5.0;
				var draft7=6.0;

				draft1=Math.floor(draft);	
				draft2=draft1+0.5;
				draft3=Math.ceil(draft);
				if((draft>draft1)&&(draft<=draft2))
				{
				draft=draft1;
				}
				if((draft>draft2)&&(draft<=draft3))
				{
				draft=draft2;
				}
				if((draft>draft4)&&(draft<draft5))
				{
				draft=3.0;
				}
				if((draft>draft5)&&(draft<draft6))
				{
				draft=4.0;
				}
				if((draft>draft6)&&(draft<draft7))
				{
				draft=5.0;
				}


				getProfile1('JavaXML?code=select Cargo_MT_GEN from tbl_tcegen_fixed where Draft_M_GEN=',draft,'sel',3)
				
		}else
			{
		//		alert("EMPTY");
			}
	}
//for MaxShipment Qty starts here	
	
	var Globalvar;
    function getProfile(getH,inner,Selectvar,fileroot,qry){

	//alert(getH);
	if(getH == "select")
		{
			alert("Select the Country ");
			return false;
		}
	this.Globalvar=Selectvar;
document.getElementById(fileroot).innerHTML=inner;

		var url=qry+getH+" order by portname asc";
	
	if (window.ActiveXObject){
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	
        }
        else if (window.XMLHttpRequest){
            httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("GET",url,true);
		httpRequest.onreadystatechange = function() {processRequest();} 
        httpRequest.send("HTML");
   }
   /**
    * This is the call back method
    * If the call is completed when the readyState is 4
    * and if the HTTP is successfull when the status is 200
    * update the profileSection DIV
    */
    function processRequest()
    {
		if (httpRequest.readyState == 4)
        {
			if(httpRequest.status == 200)
            {
				//get the XML send by the servlet
				var profileXML = httpRequest.responseXML.getElementsByTagName("Profile")[0];
				//Update the HTML
				updateHTML(profileXML);
            }
            else
            {
                alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
            }
        }
    }
       
   /**
    * This function parses the XML and updates the 
    * HTML DOM by creating a new text node is not present
    * or replacing the existing text node.
    */
     function updateHTML(profileXML)
    {
	//	alert("CALL");
        //The node valuse will give actual data
        var profileText = profileXML.childNodes[0].nodeValue;
//		alert(profileText);
	// document.forms[0].t1.value=profileText;
	//Create the Text Node with the data received
        var profileBody = document.createTextNode(profileText);
        //Get the reference of the DIV in the HTML DOM by passing the ID
        var profileSection = document.getElementById("profileSection");
        //Check if the TextNode already exist

        if(profileSection.childNodes[0])
       {
           //If yes then replace the existing node with the new one
//           profileSection.replaceChild(profileBody, profileSection.childNodes[0]);
        }
        else
        {
		
          //If not then append the new Text node
  //          profileSection.appendChild(profileBody);
        }
				Globalvar=Globalvar+'[';

				var GloClose=']';
		var Arr2=profileText.split(",");
		var Arr=new Array();
		Arr[0]="";

			for(i=1;i<=Arr2.length;i++)
			{
			Arr[i]=Arr2[i-1];
			}
		
		var newArr='=new Option(Arr[';
         var arr =',Arr[';
		 var allTogether;
	//	 alert(Arr.length);
	document.form1.cmbloadport[0]=new Option(' ',' ');
			for(i=0;i<Arr.length;i++)
			{		
							
			allTogether=Globalvar+i+GloClose+newArr+i+GloClose+arr+i+GloClose+')'+';';
			eval(allTogether);
//			 eval(Globalvar+i+GloClose+newArr+i+GloClose+arr+i+GloClose+')');
			}
			 
    }
//		eval(Globalvar+i+GloClose+newArr+i+GloClose+arr+i+GloClose+')'+';');
//			document.form1.cmbloadport[i]=new Option(Arr[i],Arr[i]);
//	alert("Here 	Global"+Globalvar[i]=new Option(Arr[i],Arr[i]));
//				eval(Globalvar[i]=new Option(Arr[i],Arr[i]));
			