////////////////////////////
//
// Cmarz Asia DB JavaScript for GoogleMap API
//
// LastUpdate : 2007/10/11
//
////////////////////////////


//-----------------------------------------
// check lat lng range - for Sample number in the map area
//-----------------------------------------
function count_marker_number(rectObj, marker){
	var north = rectObj.getNorthEast().lat();
	var east  = rectObj.getNorthEast().lng();
	var south = rectObj.getSouthWest().lat();
	var west  = rectObj.getSouthWest().lng();
	var count = 0;
	for( var i = 0; i < marker.length; i++ ){
		if(check_lat_lng_range(marker[i].lat(), marker[i].lng(), north, west, south, east)){
			count++;
		}
	}
	document.myFORM.marker_count.value = count;
}

//-----------------------------------------
// check lat lng range - for Sample number in the map area
//-----------------------------------------
function check_lat_lng_range(lat, lng, north, west, south, east){
	// ### check north, south ###
	if(south < lat && lat < north){
	} else {
		return(false);
	}

	// ### check west, east ###
	if(west == -180 && east == 180){
		return(true);
	}

	// 1  -180 |---W***E---+-----------| 180
	if(west < 0 && east < 0 && west < east){
		if(west <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	// 2  -180 |-----------+---W***E---| 180
	} else if(west > 0 && east > 0 && west < east){
		if(west <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	// 3  -180 |**E--------+--------W**| 180
	} else if(west > 0 && east < 0){
		if(west <= lng && lng <= 180){
			return(true);
		} else if(-180 <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	// 4  -180 |---------W*+*E---------| 180
	} else if(west < 0 && east > 0){
		if(west <= lng && lng <= 0){
			return(true);
		} else if(0 <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	// 5  -180 |***********+***E---W***| 180
	} else if(west > 0 && east > 0 && west > east){
		if(west <= lng && lng <= 180){
			return(true);
		} else if(-180 <= lng && lng <= 0){
			return(true);
		} else if(0 <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	// 6  -180 |***E---W***+***********| 180
	} else if(west < 0 && east < 0 && west > east){
		if(west <= lng && lng <= 0){
			return(true);
		} else if(0 <= lng && lng <= 180){
			return(true);
		} else if(180 <= lng && lng <= east){
			return(true);
		} else {
			return(false);
		}

	} else {
		return(false);
	}
}

/////////////////////////////////////////////////

//-----------------------------------------
// make icons on map
//-----------------------------------------
function createMarker(point, index, html) {
//	var icon = new GIcon(baseIcon);
//	icon.image = "/db/images/" + index + ".png";
//  var marker = new GMarker(point, icon);
	var marker = new GMarker(point, {title:index});

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

  return marker;
}


//-----------------------------------------
// make icons on map - for species bunpu result list
//-----------------------------------------
function createMarkerSpecies(point, index, baseIcon, html) {
	var icon = new GIcon(baseIcon);
	icon.image = "/db/images/" + index + ".png";
//	icon.title = "testaaa";
  var marker = new GMarker(point, icon);

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

  return marker;
}


//-----------------------------------------
// keido ido wo text area ni kakikomu
//-----------------------------------------
function input_latlang_to_textarea(rectObj){

//	var north = rectObj.getNorthEast().lat();
//	var east  = rectObj.getNorthEast().lng();
//	var south = rectObj.getSouthWest().lat();
//	var west  = rectObj.getSouthWest().lng();

	var northeast = rectObj.getNorthEast();
	var southwest = rectObj.getSouthWest();

	document.myFORM.point_northeast.value = northeast;
	document.myFORM.point_southwest.value = southwest;

	var spNE = document.myFORM.point_northeast.value.split(", ");
	var spSW = document.myFORM.point_southwest.value.split(", ");

	// ( , ) wo nozoku
	spNE[0] = spNE[0].slice(1); // north
	spSW[0] = spSW[0].slice(1); // south
	spSW[1] = spSW[1].slice(0,-1); // west
	spNE[1] = spNE[1].slice(0,-1); // east

	spNE[0] = dosuu_henkan(spNE[0], 1); // north
	spSW[0] = dosuu_henkan(spSW[0], 1); // south
	spSW[1] = dosuu_henkan(spSW[1], 0); // west
	spNE[1] = dosuu_henkan(spNE[1], 0); // east

	// print textbox
	document.myFORM.point_north.value = spNE[0];
	document.myFORM.point_south.value = spSW[0];
	document.myFORM.point_west.value = spSW[1];
	document.myFORM.point_east.value = spNE[1];
}


// for sequence
function input_latlang_to_textarea2(rectObj){

	var northeast = rectObj.getNorthEast();
	var southwest = rectObj.getSouthWest();

	document.myFORM2.point_northeast.value = northeast;
	document.myFORM2.point_southwest.value = southwest;

	var spNE = document.myFORM2.point_northeast.value.split(", ");
	var spSW = document.myFORM2.point_southwest.value.split(", ");

	// ( , ) wo nozoku
	spNE[0] = spNE[0].slice(1); // north
	spSW[0] = spSW[0].slice(1); // south
	spSW[1] = spSW[1].slice(0,-1); // west
	spNE[1] = spNE[1].slice(0,-1); // east

	spNE[0] = dosuu_henkan(spNE[0], 1); // north
	spSW[0] = dosuu_henkan(spSW[0], 1); // south
	spSW[1] = dosuu_henkan(spSW[1], 0); // west
	spNE[1] = dosuu_henkan(spNE[1], 0); // east

	// print textbox
	document.myFORM2.point_north.value = spNE[0];
	document.myFORM2.point_south.value = spSW[0];
	document.myFORM2.point_west.value = spSW[1];
	document.myFORM2.point_east.value = spNE[1];
}


//-----------------------------------------
// keido ido 123.4567890 -> 123.45.67.89N
//-----------------------------------------
function dosuu_henkan(innum, north_south) {
	var news;
	if(north_south){
		news = "N";
		if(innum.match(/-/)){
			news = "S";
			innum = innum.substr(1);
		}
	} else {
		news = "E";
		if(innum.match(/-/)){
			news = "W";
			innum = innum.substr(1);
		}
	}

	EH = parseInt(innum);
	EM = parseInt((innum-EH)*60);
	ES = (innum-EH-(EM/60))*3600;
	ESS = (Math.round(ES*10)/10); 
	if(EM<10){EM = "0"+EM;}
	if(ESS<10){ESS = "0"+ESS;}
	if((ESS+"").indexOf(".") == -1){ESS = ESS+".0";}
	outnum = EH+'.'+EM+'.'+ESS +news;

	return(outnum);
}