/* ----------------------------------
| $RCSfile: common.js,v $
| $Date: 2007/10/21 16:15:46 $
| $Revision: 1.10 $
 ---------------------------------- */

function g_http_obj() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }            
    return xmlhttp;
}

function get_geo_data_to_js_array(get_params) {
    if (!REG_GIS_VERSION) REG_GIS_VERSION = Math.random(); //global var
    xmlhttp = g_http_obj();
    xmlhttp.open('GET', WEB_ROOT_RELATIVE+'ajax/sgeo_data.php?'+get_params+"&gis_version="+REG_GIS_VERSION, false);
    xmlhttp.send(null);        
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
			resp = xmlhttp.responseText;
			eval(resp);		
			hide_loading_notice_small();
        }
    }	
}

function show_loading_notice(ltype, modul) {

    // show loading DIV
    t_height = (document.getElementById('header') ? document.getElementById('header').offsetHeight : document.getElementById('header_instant_cm').offsetHeight)
               + document.getElementById('content_top').offsetHeight
               + document.getElementById('footer').offsetHeight
               + document.getElementById('language').offsetHeight;
    isIE = ((navigator.userAgent.indexOf("Win") != -1 ) && (navigator.userAgent.indexOf("MSIE") != -1 ));
    if (isIE && ltype == 'big') {
        // hide select boxes in IE since they can not be overcloacked with transparent div
        for (n=0; n<frm.elements.length; n++) {
            if (frm.elements[n].type == 'select-one') {
                frm.elements[n].style.visibility = 'hidden';
            }    
        }
    }
    scroll_offset = getScrollXY();
    win_sizes = getWinSize();
	if (ltype == 'big') { // page blur on HTTP POST
		my_blur = document.getElementById("g_blur");
		my_blur.style.height = t_height+'px';
		my_blur.style.display = '';
	}
    my_load_notice = document.getElementById("g_loading_notice_" + ltype);
    my_load_notice.style.display = '';
    if (modul == "infrastructure" || modul == "objects") {
        my_load_notice.style.top = Math.round((Math.round(win_sizes[1] / 2) + scroll_offset[1]) - Math.round(document.images["load_notice_img_"+ltype].height) / 2)+'px';
        my_load_notice.style.left = (Math.round(win_sizes[0] / 2) - (Math.round(document.images["load_notice_img_"+ltype].width / 2)))+100+'px';            
    } else {
        my_load_notice.style.top = Math.round((Math.round(win_sizes[1] / 2) + scroll_offset[1]) - Math.round(document.images["load_notice_img_"+ltype].height) / 2)+'px';
        my_load_notice.style.left = (Math.round(win_sizes[0] / 2) - (Math.round(document.images["load_notice_img_"+ltype].width / 2)))+'px';    
    }
    // end show loading div

}

function hide_loading_notice_small() {
    my_load_notice = document.getElementById("g_loading_notice_small");
    my_load_notice.style.display = 'none';
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}


function getWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function ev_cache_map_type() {
    currentMapType = map.getCurrentMapType();
	switch (currentMapType) {
	    case(G_NORMAL_MAP):
		    currMap = "G_NORMAL_MAP";
			break;
        case(G_HYBRID_MAP):
            currMap = "G_HYBRID_MAP";
            break;
        case(G_SATELLITE_MAP):
            currMap = "G_SATELLITE_MAP";
			break;
		default : currMap = "G_NORMAL_MAP";
	}
    g_createCookie("g_app[map_type]", currMap);
}

function get_map_type() {
    cachedMapType = g_readCookie("g_app[map_type]");
	switch (cachedMapType) {
	    case("G_NORMAL_MAP"):
		    return (G_NORMAL_MAP);
			break;
        case("G_HYBRID_MAP"):
            return (G_HYBRID_MAP);
            break;
        case("G_SATELLITE_MAP"):
            return (G_SATELLITE_MAP);
			break;
		default: 
		    return (G_NORMAL_MAP);
	}    
}

function g_el(id) {
    return (document.getElementById(id));    
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

var win_arr = new Array();
var win_index_arr = new Array();

function open_window_full(url, get_params, win_name, vsize, hsize, scrollable, resizable) {
    if(get_params)
       url+= "?" + get_params;
    if(!win_arr[win_name] || win_arr[win_name].closed) {
        win_arr[win_name] = window.open(url ,win_name, "toolbar=no,status=1,resizable="+resizable+",scrollbars="+scrollable+",height="+vsize+",width="+hsize+",screenX=100,screenY=100,top=100,left=100");
        win_index_arr[win_index_arr.length] = win_name;
    } else {
        if(url == win_arr[win_name].location.href)
            win_arr[win_name].focus();
        else
            win_arr[win_name].location.href = url;
        win_arr[win_name].focus();
    }
}