//Google Map-------------->
function initialize(lat_in,lng_in) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(lat_in,lng_in), 15); //3.056267,101.7540 MinangRia
		map.savePosition();
        GEvent.addListener(map,"click", function(overlay,latlng) {     
        	var myHtml = "Congratulation, You have marked here.<br>As your client will find your product on the map.";
	        map.openInfoWindow(latlng, myHtml);
			var cord = latlng.toString().split(",");
			var tmpx = document.getElementById("googlemap_x");
			tmpx.value = cord[0].substr(1,cord[0].length);
			var tmpy = document.getElementById("googlemap_y");
			tmpy.value = cord[1].substr(0,(cord[1].length-1));
        });
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
	}
}
function loadMap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(mapDiv, {logoPassive: true});
    bounds = new GLatLngBounds();
    for (var i = 0; i < businesses.length; i++) {
      bounds.extend(new GLatLng(businesses[i].lat, businesses[i].lng));
    }
    var latSpan = bounds.toSpan().lat();
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    var newBounds = map.getBounds();
    var newLatSpan = newBounds.toSpan().lat();
    if (latSpan/newLatSpan > .90) { map.zoomOut(); }

    for (var i = 0; i < businesses.length; i++) {
      var marker = createMarker(i);
      map.addOverlay(marker);
    }
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  }
}
function zoomToAll() {
  map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}
function createMarker(ind) {
  var business = businesses[ind];
  var marker = new GMarker(new GLatLng(business.lat, business.lng));
  GEvent.addListener(marker, 'click', function() {
    marker.html = ['<table width="200"><tr><td><span class="b12"><b>', business.name, '</b><br>','<a href="',business.link,'">View Details</a></span></td></tr></table>'].join('');
    currentMarker = marker;
    marker.openInfoWindowHtml(marker.html);
  });
  return marker;
}
function formatAddressForMaps(business) {
  var address = business.street + ' ' + business.city + ' ' + business.state + ' ' + business.zip;
  return escape(address.replace(' ', '+'));
}
function _cel(elementType, id) {
  var element = document.createElement(elementType);
  element.id = id;
  return element;
}
function loadScript() {
  if (!isLoaded) {
    isLoaded = true;
    var div = document.createElement('div');
    div.className = 'message';
    div.innerHTML = 'Loading...';
    div.style.left = (500/2 - 53) + 'px';
    div.style.top = 500/2 + 'px'; 
    mapDiv.appendChild(div);
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://maps.google.com/maps?file=api&v=2.x' + 
                 '&async=2&callback=loadMap&key=ABQIAAAA_cF5D6Hk0ufmKyjRhyajBxQ90SzgVZj4N2UlWZ2laQC5Qfo_XhRppWIj0xb8yVVwGQtSSt_b4jg0Gg';
    document.body.appendChild(script);
  }
}
function loadMapGadget() {
  containerDiv = document.getElementById('container');
  mapDiv = document.getElementById('map');

  mapDiv.onclick = function (e) {
    clickedX = (window.event && window.event.offsetX) || e.clientX;
    clickedY = (window.event && window.event.offsetY) || e.clientY;
    loadScript(); 
  };

  mapDiv.style.cursor = 'pointer';

  var urlString = ['http://maps.google.com/staticmap?markers='];
  var markerString = [];
  for (var i = 0; i < businesses.length; i++) {
    markerString.push(businesses[i].lat + ',' + businesses[i].lng + ',red');
  }
  urlString.push(markerString.join('|'));
  urlString.push('&size=500x300');
  urlString.push('&key=ABQIAAAA_cF5D6Hk0ufmKyjRhyajBxQ90SzgVZj4N2UlWZ2laQC5Qfo_XhRppWIj0xb8yVVwGQtSSt_b4jg0Gg');
  mapDiv.style.background = 'url(\'' + urlString.join('') + '\')';

}
//End Google ------------->

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=700,height=400');");
}
function popUp2(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=700,height=600');");
}
function popUp3(URL,width,height) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=" + width + ",height=" + height + "');");
}
function opentab (tabname) {
	var e=document.getElementsByTagName("div");
	for(var i=0;i<e.length;i++){e[i].style.display = 'none';}
	if(document.getElementById(tabname).style.display == 'none') {
		document.getElementById(tabname).style.display = '';
		mainmember.style.display = 'none';
	} else {
		document.getElementById(tabname).style.display = 'none';
		mainmember.style.display = '';
	}		
}
function PrintDocket(){
	window.frames['myiframe'].focus(); 
	window.frames['myiframe'].print(); 
}
function ParentWindow(url){
	top.opener.location = url;	
	window.close();
}
function ParentWindow2(url){
	top.opener.top.location = url;	
	window.close();
}
function generateRow2(divName,listname,userid,imgid,itemid) {
	var d=opener.document.getElementById(divName);
	var e=opener.document.getElementById(listname);
	d.innerHTML	+= 	"<img src=\"../file/" + userid + "/phototemp/" + imgid + ".gif\" />";
	e.value = e.value + "," + itemid;
	window.close();
}


function generateRow(divName,listname,userid,imgid,itemid) {
  var ni = opener.document.getElementById(divName);
  var numi = opener.document.getElementById('theValue');
  var num = (opener.document.getElementById('theValue').value -1)+ 2;
  var e=opener.document.getElementById(listname);
  e.value = e.value + "," + itemid;
  numi.value = num;
  var newdiv = opener.document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = '<a href="#phototag" onclick="removeElement(\''+divName+'\',\''+listname+'\',\''+itemid+'\',\''+divIdName+'\')"><img src="../file/' + userid + '/phototemp/' + imgid + '.gif" border="0" /></a>';
  ni.appendChild(newdiv);
  window.close();
}
function removeElement(divName,listname,itemid,divNum) {
  var d = document.getElementById(divName);
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
  var e=document.getElementById(listname);
  var arrTemp = e.value.split(',');
  var valueTemp = '';
  for(i=1;i<arrTemp.length;i++){
	if (itemid == arrTemp[i]) {
	} else {
		valueTemp = valueTemp + "," + arrTemp[i];
	}
  }
  e.value = valueTemp;
}