function AjaxRequest(rUrl, rMethod, rCallback, rParams)
{
  this.requestObj = getXmlHttpRequestObject();
  this.url = rUrl;
  this.method = rMethod;
  this.callbackfn = rCallback;
  this.parameters = rParams;
  this.result = false;

  this._makeRequest();
  return 1;
  
}

AjaxRequest.prototype._respondToReadyState = function() {
	readyState = this.requestObj.readyState;
  events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
	if (events[readyState] == 'Complete' && this.requestObj.status == 200) {
	  try {
	  	(this.callbackfn)(this.requestObj);
	  } catch (e) {}
	}
	if (events[readyState] == 'Complete')
	  this.requestObj.onreadystatechange =  function() {};
}
	

AjaxRequest.prototype._makeRequest = function()
{
  if (this.method == 'get' && this.parameters.length > 0)
    this.url += (this.url.match(/\?/) ? '&' : '?') + this.parameters;
	this.requestObj.open(this.method, this.url, true);
	var _this = this;
	this.requestObj.onreadystatechange = function() { _this._respondToReadyState() };
      
  var requestHeaders =  ['X-Requested-With', 'XMLHttpRequest', 'X-Lib-Version', '0.1'];
	if (this.method == 'post') {
		requestHeaders.push('Content-type', 'application/x-www-form-urlencoded');
		requestHeaders.push('Content-length', this.parameters.length);
	}
	
  if (this.requestObj.overrideMimeType){
    requestHeaders.push('Connection', 'close');
  }
  for (var i = 0; i < requestHeaders.length; i += 2)
    this.requestObj.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
  this.requestObj.send(this.method == 'post' ? this.parameters : null);
  
}

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return false;	
}


function n_window(theurl,w,h) {
var the_width=w;
var the_height=h;
var from_top=((screen.height/2)-(the_height/2)-30);
var from_left=((screen.width/2)-(the_width/2));
var has_toolbar='no';
var has_location='no';
var has_directories='no';
var has_status='yes';
var has_menubar='no';
var has_scrollbars='yes';
var is_resizable='yes';
var the_atts='width='+the_width+',height='+the_height+',top='+from_top+',screenY='+from_top+',left='+from_left+',screenX='+from_left;
the_atts+=',toolbar='+has_toolbar+',location='+has_location+',directories='+has_directories+',status='+has_status;
the_atts+=',menubar='+has_menubar+',scrollbars='+has_scrollbars+',resizable='+is_resizable;
window.open(theurl,'file',the_atts);

if (!n_window.opener){
	n_window.opener=self;
}
}


function unique(col,tbl) {
var val = eval('document.data_form.'+col+'.value');
var the_width=300;
var the_height=400;
var from_top=((screen.height/2)-(the_height/2)-30);
var from_left=((screen.width/2)-(the_width/2));
var has_toolbar='no';
var has_location='no';
var has_directories='no';
var has_status='yes';
var has_menubar='no';
var has_scrollbars='yes';
var is_resizable='yes';
var the_atts='width='+the_width+',height='+the_height+',top='+from_top+',screenY='+from_top+',left='+from_left+',screenX='+from_left;
the_atts+=',toolbar='+has_toolbar+',location='+has_location+',directories='+has_directories+',status='+has_status;
the_atts+=',menubar='+has_menubar+',scrollbars='+has_scrollbars+',resizable='+is_resizable;
window.open('unique.php?col='+col+'&tbl='+tbl+'&val='+val,'unique',the_atts);

if (!unique.opener){
	unique.opener=self;
}
}

var LastWindowOpened = "";

function closePopup() {

  if (LastWindowOpened != "") {
    if (eval("self." + LastWindowOpened)) {
      if (!(eval("self." + LastWindowOpened + ".closed"))) {
        eval ("self." + LastWindowOpened + ".close()");
      }
    }
  }
}

function openPopup(thisWidth,thisHeight,thisDocument,thisWindowName,windowtools) {

  OpenThisWindow = true;
  if (eval("self." + thisWindowName)){
    if (!(eval("self." + thisWindowName + ".closed"))) {
      eval ("self." + thisWindowName + ".focus()");
      OpenThisWindow = false;
    }
  }

  if (OpenThisWindow){
    if (LastWindowOpened != thisWindowName) {
      closePopup()
    }
     
    if (windowtools == null) {           
      var windowtools = "toolbar=0,location=0,directories=0,status=1,scrollbars=1,resizable=1,alwaysRaised=1,";
    }

    eval(thisWindowName + " = window.open(\"" + thisDocument + "\",\"" + thisWindowName + "\",\"" + windowtools + "width=" + thisWidth + ",height=" + thisHeight + ",top=10,left=10,screeny=25,screenx=50\")");
    LastWindowOpened = thisWindowName;
  }

}

function updatePopup(wWidth,wHeight,thisDocument,thisWindowName,windowtools,ntop,nleft) {

  var ntop = (ntop==null) ? 10: ntop;
  var nleft = (nleft==null) ? 10: nleft;

  OpenThisWindow = true;
  if (eval("self." + thisWindowName)){
    if (!(eval("self." + thisWindowName + ".closed"))) {
      eval ("self." + thisWindowName + ".focus()");
      eval ("self." + thisWindowName + ".location = \"" + thisDocument + "\"");
      OpenThisWindow = false;
    }
  }

  if (OpenThisWindow){
    if (LastWindowOpened != thisWindowName) {
      closePopup()
    }
                
    if (windowtools == null) {           
      var windowtools = "toolbar=0,location=0,directories=0,status=1,scrollbars=1,resizable=1,alwaysRaised=1,";
    }

    eval(thisWindowName + " = window.open(\"" + thisDocument + "\",\"" + thisWindowName + "\",\"" + windowtools + "width=" + wWidth + ",height=" + wHeight + ",top="+ntop+",left="+nleft+",screeny=25,screenx=50\")");
    LastWindowOpened = thisWindowName;
  }

}

function pr_map( c_url) {
    updatePopup(675,630, 'print.php?url='+c_url, 'print_win', 'resizable=1,scrollbars=yes,scrollbars=1,scrolling=yes,');
}

function open_url( c_url) {
//    updatePopup(500,650, c_url, 'from_map', 'resizable=1,scrollbars=yes,scrollbars=1,scrolling=yes,');
	updatePopup(599,580, c_url, 'from_map', 'resizable=1,scrollbars=1,status=no,',100,100);
}


function startdraw(cKoords, cType, oCallingForm, oCoordfield) {
	// käivitab kaardil joonistamise funktsiooni
	// koordinaadid, objekti tüüp (POLYGON,LINESTRING,POINT), väljakutsuva vormi nimi
	
//	flashProxy.call('startdraw', cKoords, cType, 0);
	thisMovie('map').startdraw(cKoords, cType);
	currForm = oCallingForm;
	currCrField = (oCoordfield==null) ? currForm.coords: oCoordfield;
	window.focus();
}

function savecoords(cKoords, nPortion, nSum, pikk_pindala) {
	  if (nPortion == 1) {
		currCrField.value = cKoords;
	    } else {
		currCrField.value = currCrField.value + cKoords;
	  }


	  if (nPortion < nSum) {
		thisMovie('map').request_next_p( nPortion+1, nSum);
		//flashProxy.call('request_next_p', nPortion+1, nSum);
	  } else {
		setTimeout("show('"+currCrField.value+"', 'POLYGON', false);", 100)
		currWindow.focus();
	  }
}

function show(cKoords, cType, lChangeLoc) {
	// objekti näitamine kaardil
	// koordinaadid, objekti tC¼C¼p (POLYGON,LINE,POINT), kas re-tsentreerida kaart valitud objektile
//	flashProxy.call('show', cKoords, cType, lChangeLoc);
	thisMovie('map').show(cKoords, cType, lChangeLoc);
	givenType = cType;
}


function refresh_map() {
//	flashProxy.call('refresh_map');
	thisMovie('map').refresh_map();
}

function thisMovie(movieName) {
    // tagastab viite flashiobjektile
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName] 
        } else {
	    //		return document[movieName];
	    //alert(document.embeds[movieName]);
	    return document.embeds[movieName];
	} 
}
			