var gAutoPrint = true; // Flag for whether or not to automatically call the print function



//FUNCTION FOR DISPLAYING PRINT VIEW
function PrintView()
{	
	if (document.getElementById != null)
	{
	   var html;
	   html = '<HTML>\n<HEAD>                                                                       \n';
	   html += '<script>															                \n';
       html += 'var getCuteEditorContent = "false";                                                 \n';
       var TextAreas = document.getElementsByTagName("TEXTAREA"); 
       // Loop :  Gets all the document text Areas to check for text Area that belongs to cute Editor.
       // Logic to get the cuteEditor control (if any exist in document),its properties,Generated HTML of cuteEditor,text Area contol(if inside cuteEditor), and adds it to PrintWindow Script        
       for (i = 0; i < TextAreas.length; i++)                                    
       {                                                                              
          var  parentAncestorId =  getNthAncestor(TextAreas[i], 4).id;   
          var  parentAncestor =    getNthAncestor(TextAreas[i], 4);   
          // Regular Expression to search inside parent Id  of text Area.     
          var rExp = /CE__ctl0__ctl0_cphOuter_cphSecondary_cuteEditor/gi;                
          var result = parentAncestorId.search(rExp)                                   
          if (result != -1) 
          {                                             
                var cuteEditor = parentAncestor;
                if (cuteEditor != null)
                { 
                    var cuteEditorHTML = cuteEditor.getHTML();
                    var cuteEditorHTML = cuteEditorHTML.replace(/"/gi,"'");
                    var cuteEditorHTML = cuteEditorHTML.replace(/\n/gi,'');			                                           
                    var cuteEditorHTML = '' +cuteEditorHTML+ '';
                    html += 'var cuteEditorHTML=document.createElement("DIV")                       \n'; 
                    html += 'cuteEditorHTML.innerHTML="' + cuteEditorHTML + '"                      \n';
                    html += 'var cuteEditorHeight ="' + cuteEditor.scrollHeight + '"                \n';
                    html += 'var cuteEditorId     ="' + cuteEditor.id+  '"                          \n';
                    html += 'var cuteEditorTextAreaId ="' + TextAreas[i].id + '"                    \n'; 
                    // getCuteEditorContent is only set to true if there is a cute Editor on the page.
                    html += 'getCuteEditorContent = "true";                                         \n';
                }  
           }  
        }      
	    html += '    function subInDivs()                                                           \n';    
        html += '    {                                                                              \n';
        html += '    var collTextAreas = document.all.tags("TEXTAREA");                             \n';
        html += '    var oNewDiv;                                                                   \n';    
        html += '    var sTemp;                                                                     \n';
        html += '    var i;                                                                         \n';
        html += '    var cuteEditorId = "CE__ctl0__ctl0_cphOuter_cphSecondary_cuteEditor_ID"         \n';
        html += '                                                                                   \n';
        html += '    for (i = 0; i < collTextAreas.length; i++)                                     \n';
        html += '    {                                                                              \n';
        html += '  // Get the 4th Level Parent of textArea if it is cuteEditor skip word wrapping   \n';
        html += '       var  parentAncestor =  getNthAncestor(collTextAreas[i], 4).id;              \n';
        html += '       rExp = /CE__ctl0__ctl0_cphOuter_cphSecondary_cuteEditor/gi;                  \n';
        html += '       var result = parentAncestor.search(rExp)                                    \n';
        html += '       if (result == -1)                                                           \n';
        html += '       {                                                                           \n';
        html += '           oNewDiv = document.createElement("DIV");                                \n';
        html += '           oNewDiv.style.width = collTextAreas(i).clientWidth;                     \n';
        html += '           oNewDiv.style.height = collTextAreas(i).clientHeight;                   \n';
        html += '           oNewDiv.style.border="1px solid lightgrey"                              \n';
        html += '           oNewDiv.id = collTextAreas(i).uniqueID + "_div";                        \n';       
        html += '                                                                                   \n';
        html += '           // Replace all line breaks with HTML line breaks.                       \n';
        html += '           sTemp = collTextAreas(i).value.replace(/\\n/gi,"<BR>");                 \n';     
        html += '                                                                                   \n';
        html += '           // Match two spaces with two non-breaking spaces.                       \n';     
        html += '           sTemp = sTemp.replace(/\s\s/gi,"&#xa0;&#xa0;");                         \n';
        html += '                                                                                   \n';    
        html += '           oNewDiv.innerHTML = sTemp;                                              \n';        
        html += '           oNewDiv.style.wordWrap="break-word";                                    \n';        
        html += '           oNewDiv.style.styleFloat=\'left\';                                      \n';        
        html += '           collTextAreas(i).parentNode.insertBefore(oNewDiv,                       \n';    
        html += '           collTextAreas(i));                                                      \n';
        html += '           collTextAreas(i).style.display = "none";                                \n';    
        html += '           oNewDiv = null;                                                         \n';
        html += '         }                                                                         \n';
        html += '       }                                                                           \n';    
        html += '    }                                                                              \n';
        html += '                                                                                   \n';        
        html += '    function putBackTextAreas()                                                    \n';     
        html += '     {                                                                             \n';     
        html += '     var collTextAreas = document.all.tags("TEXTAREA");                            \n';
        html += '     var oDivToRemove;                                                             \n';
        html += '    var i;                                                                         \n';
        html += '                                                                                   \n';
        html += '    for (i = 0; i < collTextAreas.length; i++)                                     \n';    
        html += '    {                                                                              \n';
        html += '    oDivToRemove = document.all(collTextAreas(i).uniqueID + "_div");               \n';
        html += '    if (oDivToRemove != null)                                                      \n';
        html += '    {                                                                              \n';
        html += '    oDivToRemove.removeNode(true);                                                 \n';
        html += '    }                                                                              \n';    
        html += '    collTextAreas(i).style.display = "";                                           \n';
        html += '    }                                                                              \n';        
        html += '                                                                                   \n';   
        html += '   }                                                                               \n';
        html += '                                                                                   \n';
	    html += 'function disableEvents()	        								\n';
	    html += '{	           													    \n';
	    html += '	AssignCuteEditorContent();               					    \n';
	    html += '	subInDivs();               										\n';
	    html += '	var pageLinks = document.links;									\n';
	    html += '	 														        \n';
	    html += '	for (var i=0; i<pageLinks.length; i++)							\n';
	    html += '	{																\n';
	    html += '		pageLinks[i].href = "javascript:void(0);";					\n';
	    html += '		pageLinks[i].target = "";					                \n';
	    html +=	'	}																\n';
	    html += '																	\n';
	    html += '			var inputTags = document.getElementsByTagName("input");	\n';
	    html += '																	\n';
	    html += '			for (var k=0; k<inputTags.length; k++)					\n';
	    html += '			{														\n';
        html += '				if (inputTags[k].name != "btnPrint")                \n';
        html += '				{                                  					\n';
	    html += '				    inputTags[k].disabled = true;					\n';
	    html += '                   inputTags[k].onclick="function ex(){void(0);}"; \n';
	    html += '				    inputTags[k].onsubmit="function ex(){void(0);}";\n';		
        html += '				}                               					\n';
	    html += '			}														\n';
	    html += '																	\n';
	    html += '			var textTags = document.getElementsByTagName("textarea"); \n';
	    html += '																	\n';
	    html += '			for (var k=0; k<textTags.length; k++)					\n';
	    html += '			{														\n';
	    html += '				textTags[k].disabled = true;						\n';
	    html += '				                            						\n';
	    html += '			}														\n';	
        html += '																	\n';		
	    html += '			var ddlTags = document.getElementsByTagName("select");  \n';
	    html += '																	\n';
	    html += '			for (var k=0; k<ddlTags.length; k++)					\n';
	    html += '			{														\n';
	    html += '				ddlTags[k].disabled = true;					    	\n';
	    html += '			}														\n';
	    html += '																	\n';		
	    html += '			var divTags = document.getElementsByTagName("div");     \n';
	    html += '																	\n';
	    html += '			for (var k=0; k<divTags.length; k++)					\n';
	    html += '			{														\n';
	    html += '				divTags[k].disabled = true;					    	\n';
	    html += '				divTags[k].onclick = "";		                    \n';
	    html += '			}														\n';
	    html += '																	\n';		
	    html += '			var imgTags = document.getElementsByTagName("img");     \n';
	    html += '																	\n';
	    html += '			for (var k=0; k<imgTags.length; k++)					\n';
	    html += '			{														\n';
	    html += '				imgTags[k].disabled = true;					    	\n';
	    html += '				imgTags[k].onclick = "";		                    \n';
	    html += '			}														\n';		
	    html += '}																	\n';
	    html += 'function getNthAncestor(node, n)                                   \n';
	    html += '{                                                                  \n';
	    html += '    var parent = node;                                             \n';
	    html += '    for(var i = 0; i < n; i++)                                     \n';
	    html += '    {                                                              \n';
	    html += '       parent = parent.parentNode;                                 \n';
	    html += '    }                                                              \n';
	    html += '    return parent;                                                 \n';
	    html += '}                                                                  \n';
	    // Function Gets cuteEditor and textArea Id from the script .             
	    // Gets the cute Editor Iframe object from document tree .                          
	    // Creates a new Div Object with properties of Iframe Object and HTML from parent Window.                     
        // Assigns Div Object Before Iframe Control.
        // Removes Iframe Control from the tree.                                                                                   
	    html += 'function AssignCuteEditorContent()                                                 \n';
	    html += '{                                                                                  \n'; 
	    html += 'if (getCuteEditorContent == "true")                                                \n';
        html += '{                                                                                  \n';
        html += '   var cuteEditor = document.getElementById(cuteEditorId);                         \n';
        html += '   if (cuteEditor != null)                                                         \n';
        html += '   {                                                                               \n';
        html += '																                    \n';   
        html += '   var editorTextArea = document.getElementById(cuteEditorTextAreaId);             \n';     
        html += '																                    \n';
        html += '   var iframe = editorTextArea.parentNode.childNodes[0];                           \n';
        html += '   var iframeParent = editorTextArea.parentNode;                                   \n';
        html += '   var NewDiv = document.createElement("DIV");                                     \n';
        html += '   NewDiv.style.overflow="visible"                                                 \n'; 
        html += '   NewDiv.innerHTML = "";                                                          \n';
        html += '   NewDiv.innerHTML = cuteEditorHTML.innerHTML;                                    \n';
        html += '   NewDiv.style.height = "100%" ;                                                  \n';
        html += '   cuteEditor.style.height=cuteEditorHeight;                                       \n';
        html += '   NewDiv.style.borderTop =  iframe.style.borderTop;                               \n';
        html += '   NewDiv.style.borderLeft =  iframe.style.borderLeft;                             \n';
        html += '   NewDiv.style.borderRight =  iframe.style.borderRight;                           \n';
        html += '   NewDiv.style.borderBottom = iframe.style.borderBottom;                          \n';
        html += '   NewDiv.style.backgroundColor = iframe.style.backgroundColor;                    \n';
        html += '   editorTextArea.parentNode.insertBefore(NewDiv,iframe);                          \n';
        html += '   iframeParent.removeChild(iframeParent.childNodes[1]);                           \n';
        html += '																                    \n';
        html += '   //cuteEditor.EditorWysiwygModeCss = "/Kidstraining3.0/StyleSheet/Styles.css";   \n';
        html += '   }                                                                               \n';
        html += '}                                                                                  \n';
        html += '}                                                                                  \n';
	    html +='</script>															                \n';
		
		
		
		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			
			if (headTags.length > 0)
			{
				html += headTags[0].innerHTML;
			}
		}
			
	
		html += "																	\n";
		html += "<SCRIPT language='JScript' type='text/JScript' src='/kidstraining3.0/Script/Print.js'></SCRIPT>\n";
		html += "<link rel='stylesheet' type='text/css' href='/kidstraining3.0/stylesheet/styles.css'/>\n";
		html += "</HE" + "AD>														";
		html += "<BODY onLoad='disableEvents();' onbeforeprint='subInDivs();' >		\n";
		html += "	<form name='frmPrint'>											\n";
		html += "		<TD class='button'>											\n";
		if (IS_BROWSER_IE)
		{
		    html += "           <span class=bodyTextRed>If you are not able to print images, please go to Tools > Internet Options > Advanced > Check 'Print background colors and images' under Printing options</span><br/><br/>";
		}
		else //if (IS_BROWSER_FF)
		{
            html += "           <span class=bodyTextRed>If you are not able to print images, please go to File > Page Setup > check 'Print Background(color & images)' under options</span><br/><br/>";
		}
		html += "			<input type='button' name='btnPrint' value='Print the page' OnClick='PrintDocument();'>\n";
		html += "		</TD>														\n";
		html += "	</form>															\n";
		html += "<span id='printable'>												\n";
		html += "																	\n";
		
		var printReadyElem = document.getElementById("printReady");

		if (printReadyElem != null )
		{   
		    // remove script tag
            RemoveScriptTag(printReadyElem);
		    // Logic for hiding the print icon
		    HidePrintIcon(printReadyElem, "hidden");

           
           

            //AssignCuteEditorContent (printReadyElem.innerHTML);
            
 
			html += printReadyElem.innerHTML;
			
			HidePrintIcon(printReadyElem, "visible");
		}
		else
		{
			alert("Could not find the printReady section in the HTML");
			return;
		}
		
		var printWin = window.open("","PrintView","width=655, height=650, left=100, top=45, scrollbars=yes");
		
		html += "</span>";
		html += "\n</BO" + "DY>\n</HT" + "ML>";
		
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
	}
	else
	{
		alert("Sorry, the print ready feature is only available in latest browsers.");
	}
}

function HidePrintIcon(elem, visible)
{
    // -- Logic for hiding the print icon
    var spanTags = elem.getElementsByTagName("span");
	
    for (var k=0; k<spanTags.length; k++)
    {	
        if (spanTags[k].id == GetClientIdHome("printIcon"))
        {
            spanTags[k].style.visibility=visible;
        }
    }	
}

function RemoveScriptTag(elem)
{
    // -- Logic for hiding the print icon
    var scriptTags = elem.getElementsByTagName("script");
	
    for (var k=0; k<scriptTags.length; k++)
    {	
        scriptTags[k].text = "";
    }	
}

//FUNCTION FOR PRINTING DOCUMENT
function PrintDocument()
{	
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			
			if (headTags.length > 0)
			{
				html += headTags[0].innerHTML;
			}
		}	

		html += "\n</HE" + "AD>\n<BODY>\n";
		
		var printReadyElem = document.getElementById("printable");

		if (printReadyElem != null )
		{
			html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printable section in the HTML");
			return;
		}
		
		html += "\n</BO" + "DY>\n</HT" + "ML>";

		var printWin = window.open("","PrintView","width=610, height=650, left=100, top=45, scrollbars=yes");

		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		printWin.print();
	}
	else
	{
		alert("Sorry, the print ready feature is only available in latest browsers.");
	}
}

function getNthAncestor(node, n)                                   
{   // Function gets the node and returns the nth level node above it in document tree.                                                               
    var parent = node;                                            
    for(var i = 0; i < n; i++)                                   
	{                                                              
	   parent = parent.parentNode;                               
	}                                                            
    return parent;                                                 
}                                                                  
