var imagesToLoad = 0;
var imagesLoaded = 0;
emptyImage = new Image();
emptyImage.src = '../images/1tr.gif';
    var browser_name = navigator.appName;
    var browser_version = parseFloat(navigator.appVersion);

function image_map(e, obj) {
	    var xCoord,yCoord;
	    if (document.all) {
            xCoord = event.offsetX;
            yCoord = event.offsetY;
	    } else if (document.getElementById) {
            xCoord = e.clientX - obj.x;
            yCoord = e.clientY - obj.y;
	    }
	    document.form_map['clk.x'].value = xCoord;
	    document.form_map['clk.y'].value = yCoord;
        document.form_map['clk_zoom'].value = 1;
	    document.form_map.submit();
}
/*
function get_coord(e, obj) {
	    var xCoord,yCoord;
	    if (document.all) {
            xCoord = event.offsetX;
            yCoord = event.offsetY;
	    } else if (document.getElementById) {
            xCoord = e.clientX - obj.x;
            yCoord = e.clientY - obj.y;
	    }
	    document.form_map['map_coord_x'].value = xCoord;
	    document.form_map['map_coord_y'].value = yCoord;
}
*/
var IE = document.all?true:false;
var tempX = 0;
var tempY = 0;

function get_coord(e, obj) {

    if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.offsetX;
        tempY = event.offsetY;
    }else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX - obj.x;
        tempY = e.pageY - obj.y;
    }

    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}

    document.form_map['map_coord_x'].value = tempX;
    document.form_map['map_coord_y'].value = tempY;
return true;
}

function change_state(layer_id, state){
		document.getElementById(layer_id).style.display = state;
}

// Object Billboard
function Billboard(id_umg, picPreviewURL, mapPreviewURL, address){
	this.id_umg = id_umg;
	this.picPreviewURL = picPreviewURL;
	this.mapPreviewURL = mapPreviewURL;
	this.address = address;
	this.picImage = null;
	this.mapImage = null;

	// Preloading
	if(this.picPreviewURL){
		this.picImage = new Image();
		this.picImage.src = this.picPreviewURL;
		imagesToLoad++;
	}
	if(this.mapPreviewURL){
		this.mapImage = new Image();
		this.mapImage.src = this.mapPreviewURL;
		imagesToLoad++;
	}
}

function overHandler(id){
	document.pic.src = billboards[id].picPreviewURL ? billboards[id].picImage.src : emptyImage.src;
	document.map.src = billboards[id].mapPreviewURL ? billboards[id].mapImage.src : emptyImage.src;
	document.getElementById('id_umg').innerHTML = billboards[id].id_umg;
	document.getElementById('address').innerHTML = billboards[id].address;
	return true;
}

function outHandler(id){
	document.pic.src = emptyImage.src;
	document.map.src = emptyImage.src;
	document.getElementById('id_umg').innerHTML = '';
	document.getElementById('address').innerHTML = '';
	return true;
}

function change_state(layer_id, state){
	document.getElementById(layer_id).style.display = state;
}

function change_tr_color (obj_id, color){
    document.getElementById('tr' + obj_id).style.background = color;
}

function change_check_state(obj_id){
    if (document.getElementById('ch' + obj_id).checked){
        document.getElementById('ch' + obj_id).checked = false;
        document.getElementById('tr' + obj_id).style.background = '#FF6347';
    }else{
        document.getElementById('ch' + obj_id).checked = true;
        document.getElementById('tr' + obj_id).style.background = '#E6E6FA';
    }
}

function frame_form_submit() {
      if (browser_name == "Netscape") {
        document.getElementById('search_result').contentDocument.resultat.submit();
    }else if (browser_name == "Microsoft Internet Explorer") {
        document.getElementById('search_result').contentWindow.resultat.submit();
    }
}

var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)

    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}


function get_a_point_info(board_id, town_id, scale_id) {

         if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
          {
            document.getElementById("id_umg").innerHTML = '<img src="../images/loadingAnimation.gif" />';
            xmlHttp.open("GET", "index.php?page=billboards&ajax&bill_id=" + board_id + "&rid=" + town_id + "&scid=" + scale_id, true);
            xmlHttp.onreadystatechange = put_a_point_data;
            xmlHttp.send(null);
          }
          else {
            // if the connection is busy, try again after one second
            //setTimeout('get_a_point_info(' + board_id + ',' + town_id + ',' + scale_id +')', 1000);
          }
}

function put_a_point_data() {
      // move forward only if the transaction has completed
      if (xmlHttp.readyState == 4)
      {
        // status of 200 indicates the transaction completed successfully
        if (xmlHttp.status == 200)
        {
               textResponse = xmlHttp.responseText;
               if (textResponse != ''){
                document.getElementById("id_umg").innerHTML = textResponse;
              }else {
                document.getElementById("id_umg").innerHTML = "<font color=\"red\">Нет запрашиваемой информации!</font>";
              }
        }
        else
        {
          alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
      }
}

function post_get_a_point_info() {
    document.getElementById('id_umg').innerHTML = '';
    document.getElementById('address').innerHTML = '';
}

function get_coord_popup() {
    if ( (window.opener.document.form_map.map_coord_x.value == '') || (window.opener.document.form_map.map_coord_x.value == '') ){
        alert("На карте не выбранны координаты!");
    }else{
        document.edit_point.new_coord_x.value = window.opener.document.form_map.map_coord_x.value;
        document.edit_point.new_coord_y.value = window.opener.document.form_map.map_coord_y.value;
        document.edit_point.x1.value = window.opener.document.form_map.x1.value;
        document.edit_point.y1.value = window.opener.document.form_map.y1.value;
    }
}
