var LoadingData
var ClientWidth, ClientHeight, Ajax_Form_Left, Ajax_Form_Top
var Url = {
  encode : function (string) {
    return escape(this._utf8_encode(string));
  },
  decode : function (string) {
    return this._utf8_decode(unescape(string));
  },
  _utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);
      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      }
    return utftext;
  },
  _utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while ( i < utftext.length ) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  }
}

function Ajax_Start() {
  try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
  try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
  alert("XMLHttpRequest not supported");
  return null;
}

function Ajax_Get(pg,tgt,modal) {
  if (modal) {DisplayWaiting()}
  var ajx = Ajax_Start()
  ajx.onreadystatechange = function () {
    if (ajx.readyState==4) {
      if (ajx.status==200) {
        document.getElementById(tgt).innerHTML=ajx.responseText
        if (tgt=="FFMP_GeoCoords") {FFMP_Ajax_Update=false;FFMP_NewView();return}
        if (tgt=="Ajax_Return_Data") {Ajax_Copy_Data();return}
        if (modal) {HideWaiting(false)}
        if (tgt=="Ajax_Return_Form") {
          var obj=document.getElementById("Ajax_Form")
          if (obj.Ajax_Name_Field.value!="hidden") {
            obj.Ajax_Name_Field.select()
            obj.Ajax_Name_Field.focus()
          }
          else {
            obj.Ajax_Value_Field.select()
            obj.Ajax_Value_Field.focus()
          }
        }
      }
    }
  }
  ajx.open("GET", pg+"&t="+new Date().getTime()); //make connection
  ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
  ajx.send(null); //send value
}

function Ajax_Request(rt,pg,w,h,l,t,c) {
  if (rt=="Form") {
    var obj=document.getElementById("Ajax_Return_Form");
    obj.style.width=w+"px";
    obj.style.height=h+"px";
    if (c) {
      GetWindowArea();
      l=Math.round((ClientWidth/2)-(w/2));
      t=Math.round((ClientHeight/2)-(h/2));
    }
    obj.style.left=l+document.body.scrollLeft+"px";
    obj.style.top=t+document.body.scrollTop+"px";
    obj.style.visibility="visible";
    obj.innerHTML="<p style=\"font-size: 14px; font-weight: bold\">Loading - Please Wait</p>";
  }
  Ajax_Get(pg+"&Ajax_Return="+rt+"&Ajax_Form_Width="+w+"&Ajax_Form_Height="+h,"Ajax_Return_"+rt,false);
}

function Ajax_Form_Submit(s,f) {
  if (s) {
    var qs='';
    var q='?';
    var d=document.getElementById(f);
    for (var e=0;e<d.length;e++) {
      if (e==1) {q='&';}
      qs=qs+q+d.elements[e].name+'='+Url.encode(d.elements[e].value);
    }
  }
  document.getElementById("Ajax_Return_Form").style.visibility="hidden";
  document.getElementById("Ajax_Return_Form").innerHTML="";
  if (s) {Ajax_Request("Data","ajax.asp"+qs,0,0,0,0,false);}
}

function Ajax_Copy_Data() {
  var fc, fn, fv;
  fc=1;
  while (document.getElementById("Ajax_Return_Data_Field"+fc)!=undefined) {
    fn=document.getElementById("Ajax_Return_Data_Field"+fc).innerHTML;
    fv=document.getElementById("Ajax_Return_Data_Value"+fc).innerHTML;
      if (fn=="message") {window.alert(fv);}
      else {document.getElementById(fn).innerHTML=fv;}
    fc++;
  }
}

function round(nmb,r) {
  return Math.round(nmb*Math.pow(10,r))/Math.pow(10,r);
}

function GetWindowArea() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    ClientWidth = window.innerWidth;
    ClientHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    ClientWidth = document.documentElement.clientWidth;
    ClientHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    ClientWidth = document.body.clientWidth;
    ClientHeight = document.body.clientHeight;
  }
}

function DisplayWaiting() {
  document.getElementById("Ajax_Loading").style.visibility="visible";
  LoadingData=setTimeout("HideWaiting(true)",16000);
}

function HideWaiting(m) {
  clearTimeout(LoadingData);
  document.getElementById("Ajax_Loading").style.visibility="hidden";
  if (m) {window.alert("No response from the server!\nYour request may not have been processed.\nPlease refresh the page.");}
}

