/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 Wednesday, December 26, 2007 10:49:10 AM
 HAPedit 3.1.11.111
 - - - - - - - - - - - - - - - - - - - - - - - */
var isBusy=false;
 
function RightString(s, chars) {
    try {
      var r = "";
      r = s.substr(s.length-chars,chars);
      //alert(s.length-chars)
      return r;
    }
    catch (e) {
      return s;
    }
}
 

function setResponseHtml(pUrl, pDiv, debugmode, suppressloadingmessage, timemode, imagediv, imageurl) {
    //if (debugmode==true) alert (pDiv+" / "+isBusy)
    //if (suppressloadingmessage==true) alert("Suppress Loading Image")
 
    if (timemode) {
        var mytime=new Date();
    } else {
        var mytime=0
    }
 
    if (imagediv!=null&&imageurl!=null) {
        if ((imagediv.length>0) && (imageurl.length>0)) {
            document.getElementById(imagediv).src=imageurl
        }
    } else {
        imagediv=""
        imageurl=""
    }
 
    var lHttp = getHTTPObjectHtml();
    var lUrl = pUrl // +"&t="+new Date().getTime();
    //If I am going to use the above, need to change for a ? in the URL first and then append it.
    if (lUrl.indexOf("?")>0) {
        lUrl += "&t="+new Date().getTime();
    } else {
        lUrl += "?t="+new Date().getTime();
    }
 
    lUrl = lUrl.replace("+","%2b");
    if (isBusy) {
        //alert("Error during load of "+pDiv)
        lHttp.onreadystatechange = function () {}
        lHttp.abort();
    }
 
    //window.Async = true;
    lHttp.open("GET", lUrl , true);
    lHttp.onreadystatechange = function() { getHttpResponseText(pDiv, lHttp, pUrl, debugmode, suppressloadingmessage, mytime, imagediv, imageurl); };
    
    if (window.XMLHttpRequest) { // Mozilla check
        if (!isBusy) {
            isBusy = true;            
            lHttp.send(null);
        }
    } else { // IE Check
        isBusy = true;            
        lHttp.send(null);
    }
}
 

function getHTTPObjectHtml() {
    http_request = false;
    if (window.XMLHttpRequest && !(window.ActiveXObject)) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');                
        }            
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                http_request = false;
                }
        }
 
    }
    return http_request;
}
 
function getHttpResponseText(pDiv, pHttp, pURL, debugmode, suppressloadingmessage, mytime, imagediv, imageurl) {
    //if (suppressloadingmessage==true) alert("Suppress Loading Image")
    //alert(suppressloadingmessage)
    var lHttp = pHttp;
 
    if (lHttp == null) // just in case!
        lHttp = gHttp;
    
//    if (debugmode==true) {
//        alert(pHttp.readyState)
//        alert(lHttp.readyState)
//        }
 
    if (lHttp.readyState == 4) {
        isBusy = false;
 
        var status = "";
        try {
            status = lHttp.statusText;
            if (debugmode==true) alert (pURL)
            if (debugmode==true) alert (status)
            if (lHttp.status == 200) {
                var htmlDocument = lHttp.responseText;
                if (debugmode) alert (pDiv+"\n"+htmlDocument);

                if (htmlDocument=="Remove Row") {
                    var rowset = document.getElementById(pDiv)
                    var rows = rowset.getElementsByTagName("TD");
                    for (var i=0; i<rows.length;i++) {
                        rows[i].style.backgroundColor="#333333"
                        rows[i].innerHTML=""
                    }
                    rows[rows.length-1].innerHTML="Item Removed"
                } else {
                  document.getElementById(pDiv).innerHTML = htmlDocument;

                  if (mytime) {
                      var endtime = new Date();
                      var loading = (endtime-mytime)/1000
                      document.getElementById(pDiv+"_time").innerHTML="Launch time: "+RightString("00"+mytime.getHours(),2)+":"+RightString("00"+mytime.getMinutes(),2)+":"+RightString("00"+mytime.getSeconds(),2)+"."+mytime.getMilliseconds()+" - Elapsed Time: "+loading+" Seconds, "+htmlDocument.length+" bytes sent"
                      }

                  if ((imagediv.length>0) && (imageurl.length>0)) {
                      document.getElementById(imagediv).src="/images/ajax_blank.gif"
                  }
                }
            }
        } catch(e) {
            status = "Trouble accessing web page.";
            if (document.getElementById(pDiv)) {
                document.getElementById(pDiv).innerHTML = status;
            }
            if ((imagediv.length>0) && (imageurl.length>0)) {
                document.getElementById(imagediv).src="/images/ajax_unavailable.gif"
            }
        }
    } else {
        if (getAjaxLoadingMessage() != '' && getAjaxLoadingMessage() != null) {
            // only display a loading message if it is defined or false
            //setVisible(pDiv, true);
            if (suppressloadingmessage==null ) {
                //do nothing
            } else {
                if (suppressloadingmessage==false) {
                    if (document.getElementById(pDiv)) {
                        document.getElementById(pDiv).innerHTML = getAjaxLoadingMessage();
                        //+pURL;
                    }
                } else {
                    //do nothing
                }
            }
            //document.getElementById(pDiv).style.backgroundColor="#000000";
        }
        return;
    }
}
 
var gAjaxLoadingMessage = '<center><hr><b>Loading Content...</b> <img src=images/ajax-loader.gif align=center><b> Please Wait...<hr></center>';
 
function setAjaxLoadingMessage(pMessage) {
    gAjaxLoadingMessage = pMessage;
}
 
function getAjaxLoadingMessage() {
    return(gAjaxLoadingMessage);
}
 
 
 
// *******************************************************
 
/** A AJAXConnection class  */
function AJAXConnection(pDiv) {
    this.className = 'AJAXConnection';
    //alert(this.className + ' ' + name);
    
    /** Default construtor
     *
     * name - div name
     */
    {    
        this.name = pDiv;
    }
 
    this.xmlhttpPost = function (pURL, functionObj, pDiv, altDiv) {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
            if (self.xmlHttpReq.overrideMimeType) {
                self.xmlHttpReq.overrideMimeType('text/xml');
                // See note below about this line
            }
        // IE
        } else if (window.ActiveXObject) { // IE
            try {
                self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
            }
        }
        if (!self.xmlHttpReq) {
            alert('ERROR AJAX:( Cannot create an XMLHTTP instance');
            return false;
        }    
        self.xmlHttpReq.open('GET', pURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() { 
            _callBackFunction(self.xmlHttpReq, functionObj, pDiv, altDiv);
        };
        self.xmlHttpReq.send("");
    }
    
    _callBackFunction = function (http_request, functionObj, pDiv, altDiv) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                //alert(http_request.responseText);
                functionObj.callBackFunction(http_request.responseText,pDiv, altDiv);
            } else {
                alert('ERROR: AJAX request status = ' + http_request.status);
            }
        }
  }
}
 
/** A AlertTemplate class */
function AlertTemplate() {  
    this.className = 'AlertTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str) {
        alert(str)
    }    
}
 
/** A HtmlTemplate class */
function HtmlTemplate() {
    this.className = 'HtmlTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str,pDiv) {
        //alert(document.getElementById(pDiv).innerHTML)
        document.getElementById(pDiv).innerHTML=str;
        //alert(str)
        }
}
 
/** TextBoxTemplate class */
function TextBoxTemplate() {
    this.className = 'TextBoxTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str,pDiv) {
        //alert(pDiv+"\n"+document.getElementById(pDiv).value)
        document.getElementById(pDiv).value=str
    }
}
 
/** TextBoxTemplate class */
function ChartTemplate() {
    this.className = 'ChartTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str,pDiv) {
        //alert(pDiv+"\n"+document.getElementById(pDiv).value)
        //projectschedule.setDataXML("");
        projectschedule.initialDataSet=false
        projectschedule.setDataXML(str);
        //alert(projectschedule.setDataXML)
        projectschedule.render("schedule_div");
    }
}
 

/** ValidateTextBoxTemplate class */
function ValidateTextBoxTemplate() {
    this.className = 'ValidateTextBoxTemplate';
    this.callBackFunction = function(str,pDiv,button) {
        var ans=str.substring(2)
        if (str.substring(0,2)=="1:") {
                document.getElementById(pDiv).value=ans
                document.getElementById(button).disabled=false;
        } else {
            if (str.substring(0,2)=="0:") {
                document.getElementById(pDiv).value=ans
                document.getElementById(button).disabled=true;
            } else {
                document.getElementById(pDiv).value=str
                //document.getElementById(button).disabled=false;
            }
        }
    }
}
 
/** A WindowTemplate class */
function WindowTemplate() {  
    this.className = 'WindowTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str) {
        myWindow = window.open("", "", 'toolbar,width=150,height=100')
        myWindow.document.write(str)
        myWindow.document.close() 
    }    
}
 
function StatusTemplate() {
    this.className = 'StatusTemplate';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(str,pDiv) {
        //parse Out the Return into the cells based upon the str value
        //The Last Char if pDiv should be a #1, Strip is off
 
        var newDiv=pDiv.substring(0,pDiv.length-1);
        var cellscontent=str.split("::::");
 
        for (var i=0;i<cellscontent.length-1;i++) {
          var thiscell=cellscontent[i];
          var celltext="";
          var cellstyle=thiscell.split("||");
          if (cellstyle.length==1) {
                celltext=thiscell;
          } else {
          //alert(cellstyle)
          for (var j=0;j<cellstyle.length-1;j++) {
                var pos=cellstyle[j].indexOf("=")
                //alert(cellstyle[j]+" - "+pos)
                if (pos==-1) {
                    celltext += cellstyle[j];
                } else {
                    //alert(cellstyle[j])
                    var command=cellstyle[j].substring(0,pos)
                    var value=cellstyle[j].substring(pos+1,cellstyle[j].length)
                    switch (command.toLowerCase()){
                        case "align":
                            document.getElementById(newDiv+i).align=value
                            break;
                        case "bgcolor":
                            document.getElementById(newDiv+i).bgColor=value
                            break;
                        case "onclick":
                            //document.getElementById(newDiv+i).onclick=function(){value}
                            //document.getElementById(newDiv+i).onClick=function(){alert("11111")}
                            break;
                        case "onmouseover":
                            //value = value.replace(/\"/g,'');
                            //value = value.replace(/\'/g,'"');
                            //value="this.style.backgroundColor=\"#cccccc\""
                            //var c=document.getElementById(newDiv+i).onmouseover
 
                            //document.getElementById(newDiv+i).onmouseover=function(){this.style.backgroundColor="#cccccc"}
                            //var a=document.getElementById(newDiv+i).onmouseover
 
                            //document.getElementById(newDiv+i).onmouseover=function(){value}"
                            //var b=document.getElementById(newDiv+i).onmouseover
                            //alert(a+"\n"+b+"\n"+c)
                            //alert(a==b)
                            break;
                        case "onmouseout":
                            //document.getElementById(newDiv+i).onmouseout=value
                            break;
                        case "colspan":
                            //document.getElementById(newDiv+i).colSpan=2
                            break;
                        default : celltext += cellstyle[j];
                            //alert(celltext)
                        }
                }
          }
          }
          document.getElementById(newDiv+i).innerHTML = celltext;
        }
    }
 
}

