﻿// JScript File
function ReplaceContent(objectid, newcontent){
    try {
        var newChild = document.createElement("span");
        var oldChild = document.getElementById(objectid);
        newChild.innerHTML = newcontent;
        oldChild.parentNode.replaceChild(newChild,oldChild);
        return false;
	} catch (e) {
		return true; // script failed: try firing default href
	}	
}
function HTMLEncode(text){
    var out = "";
    try{
        out = text.replace(/\x26/gi,"&amp;").replace(/\x3c/gi,"&lt;").replace(/\x3e/gi,"&gt;").replace(/\x22/gi,"&quot;").replace(/\x27/gi,"&#39");
    }catch(e){alert("Failed to decode text: " + e + "\n" + text);}
    return out;
}
function pauseJavascript(mills)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < mills);
} 
//IE is very bad! it returns from the window.open function before initializing the
//document property of the window! This will help mitigate that problem.
function SafeWindowConstructor(windowId, width,height){
    try{
        var pWindow = window.open("" , windowId, "height=" + height + ",width=" + width + ", resizable=yes");
        if (document.all) pauseJavascript(600);
        var attempts = 0;
        var d = null;
        while (attempts < 15){
            if (typeof(pWindow.name)=="string"){
                return pWindow;
            }else{
            pauseJavascript(600);
            }
            
        };       
        return null;
    }catch(e){
        return null;
    }
}

var lastPopupContentCall = null;

function PopupContent(content,windowId, title, width, height){
   //The try is for IE. IE throws a null reference exception if you try to check if a value is null.
    try{
       //only allow calls every 500 ms (handle double-clicks)
       
       if (typeof(lastPopupContentCall) != "undefined"){
            var date = new Date();
            //Return false, as if the popup succeeded.
            if (date-lastPopupContentCall < 500) return false;
       }
       
   }catch(e){}
   
   lastPopupContentCall = new Date();
   //try {
    

        var pWindow = SafeWindowConstructor(windowId, width,height);
        
        var d = null;
        try{
            d = pWindow.document;
        }catch (e){
            try{ pWindow.close();}catch(e){}
            return true;
        }
        if (d == null) return true;
        
        try{
            if (d.body.childNodes.length > 0)
            {
                //HACK!!! Closes window, then re-opens it.
                pWindow.close();
                
                // pWindow.focus();
                //return false;
                //pWindow.document.clear();
                pWindow = SafeWindowConstructor(windowId, width,height);
                
                try{
                    d = pWindow.document;
                }catch (e){
                    try{ pWindow.close();}catch(e){}
                    return true;
                }
            }
        }catch(e){
            return true;
        }
        
        if (d == null) return true;
        
        d.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
        d.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        d.write("<head>");
        d.write("<title>" + title + "</title>");
        d.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/popups.css\" ></link>");
        d.write("<script type=\"text/javascript\" src=\"/scripts/popupMethods.js\" ></script>");
 
        d.write("</head><body>");
        d.write("<script type=\"text/javascript\"> ");
        var encodedContent =  HTMLEncode(content);
        d.write("DocWrite(HTMLDecode(\"" +encodedContent + "\")); ");
        d.write("</script> ");
        d.write("</body> ");
        d.write("</html>");
        pWindow.focus();

        return false;
	//} catch (e) {
	//    alert(e);
	//	return true; // script failed: try firing default href
	//}	
}
