// JavaScript Document
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function setTab(index,tabs) {
  for(i=1;i<=tabs;i++) {
    var tabLeft = document.getElementById('tabbutton'+i+'-left');
    var tabCenter = document.getElementById('tabbutton'+i);
    var tabRight = document.getElementById('tabbutton'+i+'-right');
    var tabPanel = document.getElementById('tab-panel'+i);
    if (i==index) {
      tabLeft.className = 'active-tabbutton-left';
      tabCenter.className = 'active-tabbutton';
      tabRight.className = 'active-tabbutton-right';
      tabPanel.style.display = '';
    } else {
      tabLeft.className = 'tabbutton-left';
      tabCenter.className = 'tabbutton';
      tabRight.className = 'tabbutton-right';
      tabPanel.style.display = 'none';
    }
  }
}
function setProperties(group,items) {
  str = '';
  if (items!='') {
    cb_array = items.split('|');
  } else {
    cb_array = new Array();
  }
  for(var i in properties) {
    if ((properties[i][0]==0) || (properties[i][0]==group)) {
      if (items!='') {
        if (cb_array.indexOf(i)<0) {
          cb = '';
        } else {
          cb = ' checked';
        }
      } else {
        cb = '';
      }
      str = str + '<input type="checkbox"'+cb+' name="PROPERTY['+i+']" id="PROPERTY['+i+']" value="'+properties[i][1]+'"> <label for="PROPERTY['+i+']">'+properties[i][2]+'</label> ';
    }
  }
  var dest = document.getElementById('PROPERTIES_DIV');
  dest.innerHTML = str;
}
function setActivity(group) {
  var dest = document.getElementById('ACTIVITY2');
  dest.options.length=0
  for(var i in groupitems) {
    if (groupitems[i][1]==group) {
      dest.options[dest.options.length] = new Option(groupitems[i][2],groupitems[i][0]);
    }
  }
  dest.style.width = "200px";
}
function open_detail(obj) {
  var open1 = document.getElementById('OPEN1');
  var open2 = document.getElementById('OPEN2');
  if (!obj.checked) {
    open1.style.display = '';
    open2.style.display = 'none';
  } else {
    open1.style.display = 'none';
    open2.style.display = '';
  }
}
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
function pack_click(obj) {
  var pack = document.getElementById(obj);
  pack.checked = true;
}
function pack_over(obj) {
}
function pack_out(obj) {
}
var properties = new Array();
var groupitems = new Array();

function check_form() {
  var obj_focus = null;
  if (document.CAPTUREFORM.POINAME.value=='') {obj_focus = document.CAPTUREFORM.POINAME;} else
  if (document.CAPTUREFORM.POIZIP.value=='')  {obj_focus = document.CAPTUREFORM.POIZIP;} else
  if (document.CAPTUREFORM.POICITY.selectedIndex==0) {obj_focus = document.CAPTUREFORM.POICITY;} else
  if (document.CAPTUREFORM.POISTREET.value=='') {obj_focus = document.CAPTUREFORM.POISTREET;} else
  if (document.CAPTUREFORM.POISTREETTYPE.selectedIndex==0) {obj_focus = document.CAPTUREFORM.POISTREETTYPE;} else
  if (document.CAPTUREFORM.POIHOUSE.value=='') {obj_focus = document.CAPTUREFORM.POIHOUSE;} else
  if (document.CAPTUREFORM.ACTIVITY.selectedIndex==0) {obj_focus = document.CAPTUREFORM.ACTIVITY;} else
  if (document.CAPTUREFORM.ACCEPT.checked==false) {obj_focus = document.CAPTUREFORM.ACCEPT;}
  if (obj_focus) {
    alert(error_str);
    obj_focus.focus();
    return false;
  }
}
function check_form2() {
  var obj_focus = null;
  if (document.CAPTUREFORM2.NAME.value=='') {obj_focus = document.CAPTUREFORM2.NAME;} else
  if (document.CAPTUREFORM2.ZIP.value=='') {obj_focus = document.CAPTUREFORM2.ZIP;} else
  if (document.CAPTUREFORM2.CITY.selectedIndex==0) {obj_focus = document.CAPTUREFORM2.CITY;} else
  if (document.CAPTUREFORM2.STREET.value=='') {obj_focus = document.CAPTUREFORM2.STREET;} else
  if (document.CAPTUREFORM2.STREETTYPE.selectedIndex==0) {obj_focus = document.CAPTUREFORM2.STREETTYPE;} else
  if (document.CAPTUREFORM2.HOUSE.value=='') {obj_focus = document.CAPTUREFORM2.HOUSE;} else
  if (document.CAPTUREFORM2.PHONE1.value=='') {obj_focus = document.CAPTUREFORM2.PHONE1;} else
  if (document.CAPTUREFORM2.EMAIL.value=='') {obj_focus = document.CAPTUREFORM2.EMAIL;} else
  if (document.CAPTUREFORM2.ACCEPT.checked==false) {obj_focus = document.CAPTUREFORM2.ACCEPT;}
  if (obj_focus) {
    alert(error_str);
    obj_focus.focus();
    return false;
  }
}
// Get the HTTP Object
function getHTTPObject() {
	var xhr = false;//set to false, so if it fails, do nothing
	if(window.XMLHttpRequest) {//detect to see if browser allows this method
		var xhr = new XMLHttpRequest();//set var the new request
	} else if(window.ActiveXObject) {//detect to see if browser allows this method
		try {
			var xhr = new ActiveXObject("Msxml2.XMLHTTP");//try this method first
		} catch(e) {//if it fails move onto the next
			try {
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");//try this method next
			} catch(e) {//if that also fails return false.
				xhr = false;
			}
		}
	}
	return xhr;//return the value of xhr
}
function doWork(compname,region) {
    var httpObject = getHTTPObject();
    if (httpObject != null) {
        if (compname!='POICITY') {
          httpObject.open("GET", "getcities2.php?COMPNAME="+compname+"&REGION="+region, true);
        } else {
          httpObject.open("GET", "getcities.php?COMPNAME="+compname+"&REGION="+region, true);
        }
        httpObject.send(null);
        var regionElem = '__'+compname;
        httpObject.onreadystatechange = function() {
          if (httpObject.readyState == 4) {
            var dest = document.getElementById(regionElem);
            dest.innerHTML = httpObject.responseText;
          }
        }
    }
}

var httpObject = null;
var httpObject2 = null;
var httpObject3 = null;
function change_image(dest,img) {
	var dest = document.getElementById(dest);
	dest.src = img;
}
function change_combobox(src,dest) {
  var dest = document.getElementById(dest);
	dest.innerHTML = src.options[src.selectedIndex].text;
}
function upload_show() {
  w = 450;
  h = 200;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open('fileupload.php',
    'UploadWindow',
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
  win.close;
}
function go_upload() {
  var httpObject = getHTTPObject();
  if (httpObject != null) {
      var str = document.getElementById('NOTE').value;
      var param = "P1=" + document.getElementById('POINAME').value +
        "&P2=" + document.getElementById('POIZIP').value +
        "&P3=" + document.getElementById('POIREGION').value +
        "&P4=" + document.getElementById('POICITY').value +
        "&P5=" + document.CAPTUREFORM.POICITY.options[document.CAPTUREFORM.POICITY.selectedIndex].text +
        "&P6=" + document.getElementById('POISTREET').value +
        "&P7=" + document.getElementById('POISTREETTYPE').value +
        "&P8=" + document.CAPTUREFORM.POISTREETTYPE.options[document.CAPTUREFORM.POISTREETTYPE.selectedIndex].text +
        "&P9=" + document.getElementById('POIHOUSE').value +
        "&P10=" + document.getElementById('EMAIL').value +
        "&P11=" + document.getElementById('EAST').value +
        "&P12=" + document.getElementById('NORTH').value +
        "&P13=" + str.replace(/\n/g,'|');
      httpObject.open("POST", "step1.php");
      httpObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      httpObject.send(param);
      httpObject.onreadystatechange = function() {
        if (httpObject.readyState == 4) {
          var dest = document.getElementById('gps_preview');
          dest.innerHTML = httpObject.responseText;
        }
      }
  }
}
function load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		var POIZIP = document.getElementById('POIZIP').value;
		var POISTREET = document.getElementById('POISTREET').value;
		var POIHOUSE = document.getElementById('POIHOUSE').value;
		var POISTREETTYPE = document.CAPTUREFORM.POISTREETTYPE.options[document.CAPTUREFORM.POISTREETTYPE.selectedIndex].text;
		var POICITY = document.CAPTUREFORM.POICITY.options[document.CAPTUREFORM.POICITY.selectedIndex].text;
		var address = "'"+POIZIP+' '+POISTREET+' '+POISTREETTYPE+' '+POIHOUSE+', '+POICITY+"'";
		map.setUIToDefault();
		var geocoder = null;
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(center) {
			if (!center) {
				document.getElementById('EAST').value='';
				document.getElementById('NORTH').value='';
				go_upload();
			} else {
				map.setCenter(center, 13);
				var marker = new GMarker(center, {draggable: true});
				document.getElementById('EAST').value=marker.getLatLng().lng();
				document.getElementById('NORTH').value=marker.getLatLng().lat();
				map.addOverlay(marker);
				go_upload();
		}});
	}
}
