// AXSMARINE DISTANCE TABLE //

var totaldays = 0;
var totaldist = 0;

function submite()//user press return
{
	alert('submite');
}


function build()
{	
	
	msg("build();");

	if(!verify())return;

	var classe="a";
	
	
	var htm="<th width='300'>Port</th>";
	htm+="<th width='60'>In port</th>";
	htm+="<th width='60'>Speed</th>";
	htm+="<th width='100'>Via</th>";
	htm+="<th>Min.Draft</th>";
	htm+="<th width='250'>Distance</th>";
	htm+="<th width='16'><img src='gif/cancel2.gif' align='absmiddle' onclick='clear_all()' title='Clear all' STYLE='cursor:pointer'></th>";

	totaldays = 0;
	totaldist = 0;
	
	for(var i=0; i < sequence.length; i++)
	{
	
		var port = sequence[i][1];//port str
		if(!sequence[i][2])sequence[i][2]=0;//days in port
		var daysinport = sequence[i][2];
		if(!sequence[i][3] || sequence[i][3]<1)sequence[i][3]=$("speed").value;//speed
		
		var speed = sequence[i][3];
		var distance = sequence[i][4];
		if(!distance)distance=0;

		//console.log("distance = "+sequence[i][4]);

		if(sequence[i][5])var min_draft = sequence[i][5]; else var min_draft ="";
		var vias = sequence[i][7];
		
		if(!vias)
		{
			//msg("build() error: !via");///must be a array
			sequence[i][7]=new Array();
			vias=new Array();
			//continue;
		}

		var viastr=new Array();
		
		for(var j = 0; j < vias.length; j++ )viastr[viastr.length] = viap[vias[j]][0];
		
		if(i>0 && viastr.length==0)viastr[viastr.length]="select";

		if(classe=="a")classe="b"; else classe="a";

		htm+="<tr class=" + classe + " style='cursor:pointer' onclick='init_row(" + i + ");blu(this)'>";// ONCLICK=pop("+i+")
		htm+="<td width='300'><input id=p" + i + " name=p" + i + " value=\"" + port + "\" onfocus=dlini(this,1) onblur='dlb(this);get_frto(this);' onkeydown='if (event.keyCode == 13)submite()' STYLE='width:300'>";
		htm+="<td align='right'><input id=ds"+i+" value="+daysinport+" style='width:30' class='r' onblur=set_days(this.value," + i + ")>days</td>";// Days in port
		totaldays=(totaldays*1+daysinport*1);
		
		if( i > 0 )htm+="<td align='right'><input id=spd"+i+" value="+speed+" style='width:30' class='r' onblur=set_spd(this.value," + i + ")>kts</td>";// Speed
		else htm+="<td></td>";//speed 0
		
		htm+="<td align='center'><a href='#via' onclick=pop_via("+i+") TITLE='Select via'>" + viastr.join(" ; ") + "</a></td>";// Via
		htm+="<td>" + min_draft + "</td>";// Min.Draft str

		var nbdays = Math.round(distance/(speed*24)*100);
		totaldays = (totaldays*1+nbdays/100);
		var strdays = "nm ("+nbdays/100+"days)";
		var redo = "";


		if(i>0)htm+="<td align='right' id=dist"+i+">" + distance + strdays + redo + "</td>";
		else htm+="<td></td>";// Distance

		totaldist = (totaldist*1 + distance*1);//

		htm+="<td><img src='gif/cancel2.gif' align='absmiddle' onclick=remstep("+i+") title='Remove this step'></td>";// Delete
		htm+="</tr>";
	}

	//propose another sequence
	if(classe=="a")classe = "b";else classe = "a";
	

	htm+="<tr class='" + classe + "'>";//ONCLICK=pop("+i+")
	
	htm+="<TD WIDTH=300><INPUT ID=p"+i+" NAME=p"+i+" ONFOCUS=dlini(this,1) ONBLUR='dlb(this);get_frto(this);' STYLE='width:300'></TD>";//stay free

	htm+="<TD ALIGN=RIGHT></TD>";//days in port
	htm+="<TD ALIGN=RIGHT></TD>";//speed
	htm+="<td></td>";//via
	
	htm+="<td></td>";//

	var totalstr = "<b style='font-size:13px'>Total : " + totaldist + "nm - " + Math.round(totaldays*100)/100 + " days</b>";
	htm+="<td class='c' id='dist" + i + "'>" + totalstr + "</td>";
	htm+="<td></td>";
	htm+="</tr>";
	htm+="</table><center>";
	
	htm+="<button title='Go !'><img src='gif/accept.gif' align='absmiddle'> Go</button> &nbsp;&nbsp;";//ask youri if you have any questions about this button
	
	if(i > 2) htm+="<button onclick=drawit() title='Draw all segments'><img src='gif/edit.gif' align='absmiddle'> Draw all</button> &nbsp;&nbsp;";//draw all
	if( i >= 1 ) htm+="<button onclick=clear_all() title='Clear all'><img src='gif/cancel.gif' align='absmiddle'> Clear all</button>";//clear all	
	
	htm+="</center>";
	
	calcDates(false);//calculate arrival date

	$("BOX").innerHTML = "<table border=1 width='100%' style='background-color:#D5E4F2;border-color:#B8CFE9;padding:2;layout:fixed'>" + htm;

		//reFocus();
}

function autoFocus()//set the focus to the last element
{
	var i = sequence.length;
	msg("Focus on #" + i);
	try{
		$("p" + i).focus();
	}
	catch( e )
	{
		alert( "Error:" + e.description );
	}
}


function calcDates( retro )//compute the ETA date thanks to the start date and duration (in days)
{
	//alert("calcDates() - retro=" + retro + " totaldays=" + totaldays );
	
	
	if( retro == true )var dat = $("arrival").value;
	else var dat = $("departure").value;
	if(dat == "")return;
	
	dat = dat.split("/");
	//#43525 - 06Dec2011 - Jmabulay - start
	//d = parseInt(dat[0]);
	d = parseInt( dat[0],10 );
	//#43525 - 06Dec2011 - Jmabulay - ends
	
	M = parseInt( dat[1] - 1);
	Y = parseInt( dat[2] );
	
	//#ticket 38033 start
	var dtt=parseInt(totaldays); 	
	
	//var dat1 = new Date( Y , M , ( d + Math.round( totaldays ) ) , 12 , 0 , 0 );
	//var dat2 = new Date( Y , M , ( d - Math.round( totaldays ) ) , 12 , 0 , 0 );
	var compu_dt1=new Date(Y+'/'+M+'/'+d);
	compu_dt1.setDate(compu_dt1.getDate()+dtt);
	var dat1 = new Date( compu_dt1.getFullYear() , parseInt(compu_dt1.getMonth()+1) , compu_dt1.getDate() , 12 , 0 , 0 );
	
	var compu_dt2=new Date(Y+'/'+M+'/'+d);
	compu_dt2.setDate(compu_dt2.getDate()-dtt);
	var dat2 = new Date( compu_dt2.getFullYear() , parseInt(compu_dt2.getMonth()+1) , compu_dt2.getDate() , 12 , 0 , 0 );	
	//#ticket 38033 end
	
	//alert(dat1 + dat2);
	
	if( retro == true)
	{
		//alert( $("arrival").value + " minus " + totaldays + "days => " + dat2.f("dd/MM/yyyy") );
		$("departure").value = dat2.f("dd/MM/yyyy");
	}
	else {
		if(dat1 != "Invalid Date") $("arrival").value = dat1.f("dd/MM/yyyy");
	}
}


function verify(){//verify if the sequence is complete (drawable)
	
	for(var i=0;i<sequence.length;i++){
		
		if(sequence[i][4]==0){//DISTANCE
			//msg("distance ["+i+"] must be calculated");
			get_dist(i);
			return false;
		}
		//return false;
	}
	return true;
}



function init_row( num ){//lorsqu'on clique sur une ligne du tableau	
	
	//msg("init_row("+num+")<BR>sequence["+num+"][0]="+sequence[num][0]+"<BR>sequence["+num+"][2]"+sequence[num][2]);

	cleare();//clear map

	if(num==0)
	{
		msg("Start point : "+sequence[num][1]);
		show_startpoint( sequence[num][0] );
		return;
	}else{
		msg(sequence[(num-1)][1]+" to "+sequence[num][1]);
	}

	var fr = sequence[num][1];//from str
	var to = sequence[(num-1)][1];//to str

	//msg("From '"+from+"' to '"+to+"', "+nm+"nm");

	//ROUTE//
	c=sequence[num][8];//route
	
	draw_seg(num);

	//START POINT//
	if(c && c.length>0){
		var point = new GLatLng(c[0][0], c[0][1]);
		var title = sequence[(num-1)][1];
		var marker =new GMarker(point,{icon:iconStart, title: title });
		marker.title=title;
		map.addOverlay(marker);			
		GEvent.addListener(marker, "click", function(){point_detail(this.title);});
	}

	//END POINT//
	if(c && c.length-1 > 0)
	{
		var point = new GLatLng(c[c.length-1][0], c[c.length-1][1]);
		var title = title;
		var marker=new GMarker(point,{icon:iconEnd, title: title });
		marker.title=title;
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function(){point_detail(this.title);});
	}
	
	//VIA POINTS//
	var vias = sequence[num][6];
	//alert("vias="+vias);
	if(vias)for(var i=0; i < vias.length; i++)createminipoint(vias[i]);

}


document.body.scroll = "no";



