
// detect Browser type

isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;



    var map;
    var pointLat = new Array();
    var pointLng = new Array();
    var pointDsc = new Array();


    var clientX;
    var clientY;
    var CURRENT_POPUP_DLG;



function loadGEOStart() {
	if (GBrowserIsCompatible()) {
    	map = new GMap2(document.getElementById("geo_map"));
        map.addControl(new GSmallMapControl());
  		map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(geoLatStart,geoLngStart), geoDefaultZoomLevel);
        for(i=0; i<pointLat.length; i++) {
        	GeoAddPoint(pointLat[i],pointLng[i],pointDsc[i]);
        }
	}
}


function GeoAddPoint(Lat,Lng,Dsc) {
//        alert(Lat + " + " + Lng + " + " + Dsc);
  		var point = new GLatLng(Lat,Lng);
  		var marker = new GMarker(point);
  		GEvent.addListener(marker, "click", function() {
    		var content = Dsc;
    		marker.openInfoWindowHtml(content);
  		});
  		map.addOverlay(marker);
}

function GeoMapPosition(Lat, Lng) {
//		alert(Lat + "," + Lng);
        map.setCenter(new GLatLng(Lat,Lng), 14);
}


function NavigateHref(url) {	if(url != "") document.location.href=url;
	return false;
}

function getPos(el,sProp) {
        var iPos = 0;
        while (el!=null) {
                iPos+=el["offset" + sProp]
                el = el.offsetParent
        }
        return iPos

}

function getScrollCoords ()
{
  if (typeof window.pageXOffset != 'undefined')
    return {x: window.pageXOffset, y: window.pageYOffset};
  else if ((!document.compatMode || document.compatMode == 'BackCompat') &&
    document.body && typeof document.body.scrollLeft != 'undefined')
  {
    return {x: document.body.scrollLeft, y: document.body.scrollTop};
  }
  else if (document.compatMode == 'CSS1Compat' &&
           document.documentElement && typeof document.documentElement.scrollLeft != 'undefined')
  {
    return {x: document.documentElement.scrollLeft, y:document.documentElement.scrollTop};
  }
  else
    return null;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


function makeRequest(url,params, func)
{
    http_request = null;

    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            http_request.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
            }
        }
    }
    if (!http_request)
    {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = func;
    http_request.open('POST', url);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(params);
//http_request.clear_cache()
}


function showLayer(id)
{
  var elm;
  elm = document.getElementById(id);
  if (elm)
  {
    var scrollCoords = getScrollCoords();
    tmpY = clientY;
    tmpX = clientX;


    elm.style.top = tmpY+'px';
    elm.style.left = tmpX+'px';
    elm.style.display = "";
  }
}

function hideLayer(id)
{
    var elm;
    elm = document.getElementById(id);
    if (elm)
    {
        elm.style.display = "none";
    }
   return false;
}


function fieldOnMouseOver(id_name) {    var elm, elm_btn;
    elm = document.getElementById(id_name);
    if (elm)
    {
        elm.style.border = "1px solid #CCCCCC";
        elm_btn = document.getElementById(id_name+"_edit_btn");
        if(elm_btn) {
        	elm_btn.style.display = "";
        } else {        	div_html = "<div id=\""+id_name+"_edit_btn\"><a href=\"javascript:;\" class=\"small\" onClick=\"return showEditPopUp('"+id_name+"');\">Edit</a></div>";
        	elm.innerHTML = div_html +  elm.innerHTML;
        }

    }



   return false;
}

function fieldOnMouseOut(id_name) {
    var elm, elm_btn;
    elm = document.getElementById(id_name);
    if (elm)
    {
        elm.style.border = "";
        elm_btn = document.getElementById(id_name+"_edit_btn");
        if(elm_btn) {
        	elm_btn.style.display = "none";
        }


    }
   return false;
}

function showEditPopUp(tag_name) {
    if(CURRENT_POPUP_DLG != "") {    	editFieldClose(CURRENT_POPUP_DLG);
    }

	var ctd = document.getElementById(tag_name+"_left_border");

    clientX  = getPos(ctd,"Left")-1;
    clientY  = getPos(ctd,"Top")-1;

    showLayer(tag_name+"_edit_box");

    CURRENT_POPUP_DLG =  tag_name;

	return false;
}

function editFieldClose(tag_name) {
    hideLayer(tag_name+"_edit_box");
    CURRENT_POPUP_DLG = "";
	return false;

}

function postFieldForm(tag_name) {
	var post_str = getFormStrData(tag_name+"_form");
   	var captcha_div = document.getElementById(tag_name+"_captcha");
   	if(captcha_div) {
      		var field_div   = document.getElementById(tag_name+"_edit_box");
      		field_div.style.height = "100px";
        	captcha_div.innerHTML = "";
    } else alert("Hello spam bot!");


    makeRequest('/post_data.php','form='+tag_name+'&'+post_str,AJAX_Response);
	return false;
}

function getFormStrData(form_name) {
	var post_str="";
  	var f = document.getElementById(form_name);
  	if(f) {
  		for(i=0;i<f.elements.length;i++) {
  			e = f.elements[i];
			if(e.type=="button") continue;
			if(e.type == "checkbox" || e.type == "radio") {
				if(e.checked)  post_str = post_str + e.name+"="+escape(e.value)+"&";
			}else
			  post_str = post_str + e.name+"="+escape(e.value)+"&";
  		}
        if(post_str.length>0) post_str = post_str.substr(0,post_str.length-1);
	}
    return post_str;
}


function AJAX_Response()
{
  var result;
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      result = http_request.responseText;
      if(result.substring(0,15)=="[CAPTCHA_ERROR]" ||
         result.substring(0,15)=="[CAPTCHA_ENTER]") {

      	var res = result.substring(1,14);
      	if(res == "CAPTCHA_ERROR") txt = "You typed wrong code, try again";
      	else txt = "Type code shown below";

      	var captcha_url = result.substring(15);
      	var captcha_div = document.getElementById(CURRENT_POPUP_DLG+"_captcha");
      	if(captcha_div) {
      		var field_div   = document.getElementById(CURRENT_POPUP_DLG+"_edit_box");
      		field_div.style.height = "200px";
        	captcha_div.innerHTML = txt +"<br><img src=\""+captcha_url+"\" /><br><input type=\"text\" name=\"kword\" size=\"10\"><br>";
        } else alert("Hello spam bot!");
//      	alert(captcha_url);
      } else {
      	elm = document.getElementById(CURRENT_POPUP_DLG);
      	elm.innerHTML = result;
      	editFieldClose(CURRENT_POPUP_DLG);
      }
    }
  }
}


function buttonCloseOpenClick(div_id,txt_value) {	var id = document.getElementById(div_id);
	var id_but = document.getElementById(div_id+"_button");
	var id_txt = document.getElementById("header_"+div_id);

	if(id && id_but) {		if (id.style.display == "none") {			id.style.display ="";
			id_but.style.backgroundPosition = "-20px 0px";
			if (txt_value!=null) id_txt.innerHTML = "";
		} else {			id.style.display ="none";
			id_but.style.backgroundPosition = "-60px 0px";
			if (txt_value!=null) id_txt.innerHTML = txt_value;
		}
	}
	return true;
}

function  buttonCloseOpenOver(div_id) {
	var id = document.getElementById(div_id);
	var id_but = document.getElementById(div_id+"_button");
	if(id && id_but) {
		if (id.style.display == "none") {
			id_but.style.backgroundPosition = "-40px 0px";
		} else {
			id_but.style.backgroundPosition = "0px 0px";
		}
	}
	return true;
}

function  buttonCloseOpenOut(div_id) {
	var id = document.getElementById(div_id);
	var id_but = document.getElementById(div_id+"_button");
	if(id && id_but) {
		if (id.style.display == "none") {
			id_but.style.backgroundPosition = "-60px 0px";
		} else {
			id_but.style.backgroundPosition = "-20px 0px";
		}
	}
	return true;
}




// interesting procedures. to memorize these examples
function ____trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) {
     temp = temp.replace(obj, '$2');
   }
   return temp;
}


function ___parseResponseHeader(key, header) {

  var lines = header.split("\n");
  var re = new RegExp("^" + key + ":\\s");

  for (var i in lines) {
    if (re.exec(lines[i])) {
      var returnValue = trim(RegExp.rightContext);
      return returnValue;
    }
  }
  return "";
}


function onSearchClick(id_name) {    var search_box = document.getElementById(id_name);
    if(search_box) {    	var search_href = "/as/"+id_name+"/"+encodeURIComponent(search_box.value);
        document.location.href = search_href;
        return false;

	}

}



function getBounds(element)
{
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft - parent.scrollLeft;
    top += parent.offsetTop - parent.scrollTop
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}
