
function refreshMain() {

   if (window.opener) {
      window.opener.location.reload();
   }
}

var newWind;

function popUp(URL,WIDTH,HEIGHT) {

   newWind = window.open(URL,"POPUP","HEIGHT="+HEIGHT+",WIDTH="+WIDTH+",scrollbars=yes,resizable=yes,alwaysraised=yes");

//   if (newWind.opener=null) {
      newWind.opener = window;
//   }

   newWind.moveTo(200,200);

}

function popSrc(SRC,WIDTH,HEIGHT) {

   newWind = window.open('',"SOURCE","HEIGHT="+HEIGHT+",WIDTH="+WIDTH+",scrollbars=yes,resizable=yes,alwaysraised=yes");

//   if (newWind.opener=null) {
      newWind.opener = window;
//   }

   newWind.moveTo(200,200);
   var doc = newWind.document;
   doc.write("<HTML><HEAD><TITLE>SOURCE TEXT</TITLE></HEAD>");
   doc.write("<BODY BGCOLOR=#FFFFF><TEXTAREA NAME=comments COLS=90 ROWS=35>\n");
   doc.write(SRC);
   doc.write("\n</TEXTAREA></BODY></HTML>\n");
   
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function modSelect (myform, invcurr) {

alert("found "+ myform.length + " objects on form.");
   for (i=0; i < myform.length; i++) {
      currentObj = myform.elements[i];
      if (currentObj.type == 'checkbox') {
         if (invcurr) {
            currentObj.checked = (currentObj.checked) ? false : true;
         } else {
            currentObj.checked = true;
         }
      }
   }
}


function makeRequestDone(id) {
 
   //alert(id+ "done"); 
   // this is a placeholder function.  It should be re prototyped in 
   // the WMS template with actual stuff to be done when an AJAX call is 
   // completed.   

   // alert(id + " Updated");
}

//Deprecated 3/24/2011, function rolled into loadContents 
//function updateControls(id) {
//
//   var $ID = $(id+"_SEL");
//   if ($ID != null) {
//      id=id+"_SEL";
//   }
//   var $ID = $(id);
//   if (id.substr(0,2) == "AJ") {    
//      if ($ID != null) {
//         var DATA = $(id).innerHTML;
//         //alert(DATA);
//         //if (getElementById(id).innerHTML.search(/Updates Completed/)) {
//         //   alert('Done');
//            $(id).disabled=false;
//            $(id).style.backgroundColor='#aaffaa';
//         //} else { 
//         //   $(id).style.backgroundColor='#ffaaaa';
//         //} 
//      } else {
//         $(id).style.backgroundColor='#ffaaaa';
//      }
//   } 
//   makeRequestDone(id);
//}

// document.all deprecated and no longer used
// replaced with document.getElementById
//
// 2008-09-09 - Added makeRequestDone() finishing function, called from 
// loadContents and parseContents if completed successfully, should be 
// re-created in the page with actual functions.

function makeRequest(url,id,type) {
   // alert("makeRequest");
   if (type == "IFRAME") {
      var HEIGHT = document.body.clientHeight - 210;
      var WIDTH = document.body.clientWidth - 20;
      var IFRAME = "<IFRAME WIDTH="+WIDTH+" HEIGHT="+HEIGHT+" SRC="+url+">";
      IFRAME+="</IFRAME>";
      //document.all(id).innerHTML=IFRAME;
      document.getElementById(id).innerHTML=IFRAME;
   } else if (type == "POPUP") {
      popUp(url,200,200); 
   } else {   
      var http_request = false;
      if (type != 'Silent') {
      document.getElementById(id).innerHTML="<h6>...</h6>";
      //document.all(id).innerHTML="<H6>...</H6>";
//alert("loading menu");
      }
      if (window.XMLHttpRequest) { // Mozilla, Safari, ...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
         }
      } 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('Giving up :( Cannot create an XMLHTTP instance');
         return false;
      }

      // Changed Logic 2011-12-29 to create new loader that runs scripts but DOESN'T change links.
      //if (type != "AJAX") {
      //   http_request.onreadystatechange=function(){loadContents(http_request,id);};
      //} else {
      //   http_request.onreadystatechange=function(){parseContents(http_request,id);};
      //}
      if (type == "AJAX") {
         http_request.onreadystatechange=function(){parseContents(http_request,id);};
      } else if (type == "SCRIPTS") { 
         http_request.onreadystatechange=function(){runContents(http_request,id);};
      } else {
         http_request.onreadystatechange=function(){loadContents(http_request,id);};
      }
      http_request.open('GET', url, true);
      http_request.send(null);
   }
}

function parseContents(http_request,id) {

   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         document.getElementById(id).innerHTML="Parsing Data";
         //document.all(id).innerHTML="Parsing Data";
         var page;
         // Parse the response from the server and update links to 
         // use AJAX requests instead of links.
         // TARGET should be replaced with id, if no target, assume this one.
         var http_array=http_request.responseText.split("HREF");
         //alert("Found links, parsing " + http_array.length + " links");
         page="<!--makeRequest serviced in AJAX mode, using parseContents !-->\n";
         page+=http_array[0];
         for (var loop=1; loop < http_array.length; loop++) {
            chkstring = http_array[loop];
            var endtag = http_array[loop].indexOf(">");
            var newhref = http_array[loop].substring(1,endtag);
            //Scrub " out of link
            newhref = newhref.replace(/\"/g, "");
            //alert(loop+"="+newhref);
            if (newhref[0] == "#") {
               //alert("Skipping ajax a tag");
               page=page+http_array[loop];
            } else {
               var netlink;
               newlink="<A style=\"text-decoration: underline;\" onClick=\"makeRequest('"+newhref+"','"+id+"','AJAX');\">";
               //alert("newtag="+newlink);
               var replace;
               replace=newlink+http_array[loop].substring(endtag+1); 
               //var taginfo = http_array[loop].split
               http_array[loop]=replace; 
               page=page+http_array[loop];
            }
         }


         //alert(http_request.responseText);

         var cookie=getCookie("smithy");
         if (cookie) {   
            page=page+="<BR><A ID=VS onClick=popSrc";
            page=page+="(document.getElementById('"+id+"').innerHTML,800,600);";
            page=page+=">View Source</A>";
         }
         //document.all(id).innerHTML=page;
         document.getElementById(id).innerHTML=page;

         // Parse Javascript stuff
         page = page.replace(/<script/gi, "<SCRIPT");
         page = page.replace(/<\/script/gi, "</SCRIPT");
         var script_array=page.split("<SCRIPT");
         
         for (var loop=1; loop < script_array.length; loop++) {
            var subscript_array = script_array[loop].split(">");  
            var subscript_array = subscript_array[1].split("<");
            //alert("sript =" + subscript_array[0]);
            eval(subscript_array[0]);
         }
         //alert("parseContents complete, finishing request");
         makeRequestDone(id);
      } else {
         alert('There was a problem with the request:', http_request.status);
         //document.all(id).innerHTML="Error:" + htp_request.status;
         document.getElementById(id).innerHTML="Error:" + htp_request.status;
      }
   }
}

function runContents(http_request,id) {

   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         var cookie=getCookie("smithy");
         page ="<!--makeRequest serviced in SCRIPTS mode, using runContents !-->\n";
         page+= http_request.responseText;
         if (cookie) {   
            page+="<BR><A ID=VS onClick=popSrc";
            page+="(document.getElementById('"+id+"').innerHTML,800,600);";
            page+=">View Source</A>";
         }
         //document.all(id).innerHTML=http_request.responseText;
         document.getElementById(id).innerHTML=page;

         // Parse Javascript stuff
         page = page.replace(/<script/gi, "<SCRIPT");
         page = page.replace(/<\/script/gi, "</SCRIPT");
         var script_array=page.split("<SCRIPT");
         
         for (var loop=1; loop < script_array.length; loop++) {
            var subscript_array = script_array[loop].split(">");  
            subscript_array.shift();
            var thisscript = subscript_array.join(">");
            var subscript_array = thisscript.split("</SCRIPT>");
            //alert("script =" + subscript_array[0]);
            eval(subscript_array[0]);
         }
         makeRequestDone(id);
      } else {
         alert('There was a problem with the request:', http_request.status);
         //document.all(id).innerHTML="Error:" + htp_request.status;
         document.getElementById(id).innerHTML="Error:" + htp_request.status;
      }
   }
}

function loadContents(http_request,id) {

   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         //document.all(id).innerHTML=http_request.responseText;
         document.getElementById(id).innerHTML=http_request.responseText;
         // Parse the contents and update WMS controls.
         if (id.substr(0,2) == "AJ") {
            var $ID = $(id+"_SEL");
            if ($ID != null) {
                id=id+"_SEL";
            }
            var $ID = $(id);
            if ($ID != null) {
               var match = new RegExp("Updates Complete");
               if (match.test(http_request.responseText)) {
                  //alert(http_request.responseText);
                  $(id).disabled=false;
                  $(id).style.backgroundColor='#aaffaa';
               } else {
                  $(id).disabled=false;
                  $(id).style.backgroundColor='#ffaaaa';
                  //alert("Update Failed!");
               }
            } else {
               $(id).style.backgroundColor='#ffffaa';
            }
         }
 
         makeRequestDone(id);
         //updateControls(id);
         // Moved UpdateControls code into load so we don't load THEN parse the HTML
      } else {
         alert('There was a problem with the request:', http_request.status);
      }
   }
}

