//checks if a text is a number > 0 using a . as decimal separator
function IsDouble(sText)
{
   var ValidChars = "0123456789.";
   var Char;

   for (i = 0; i <= sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         return false;
      }
   }
   
   var d = parseFloat(sText);
   if (d<0)
   {
		return false;
	}
	
   return true;
}

//checks if a text is a number > 0 using a . as decimal separator
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var Char;

   for (i = 0; i <= sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         return false;
      }
   }
   
   var i = parseInt(sText);
   if (i<0)
   {
		return false;
	}
	
   return true;
}
//Global XMLHTTP Request object
var XmlHttp;

function CreateXmlHttp()
{
    if(window.ActiveXObject)/*Microsoft support*/
    {
        try 
        { 
            XmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");/*newer browser*/
        } 
        catch(e) 
        { 
            try 
            { 
                XmlHttp  = new ActiveXObject("Msxml2.XMLHTTP"); /*older browser*/
            } 
            catch(e1) 
            { 
                XmlHttp  = null; 
            } 
        } 
    }
    else
    {
        if(XmlHttp==null && window.XMLHttpRequest)/*firefox support*/
        {
            XmlHttp  = new XMLHttpRequest();
        }
    }
}
	
//Called when response comes back from server with the header xml
var header;
function ProcessHeader()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		//alert("MTR 06.068 -> XmlHttp.status = " + XmlHttp.status);
		
		if(XmlHttp.status == 200)
		{
			header = XmlHttp.responseText;
			
			//now get the footer
			GetFooter();
		}
		else
		{
			alert("Error retrieving data! Error = " + XmlHttp.status );
		}
	}
} 

//Called when response comes back from server with footer data
var footer;
function ProcessFooter()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{
			footer = XmlHttp.responseText;
			
			PrintPageHeaderFooter();
		}
		else
		{
			alert("Error retrieving data in ProcessFooter! Error = " + XmlHttp.status);
		}
	}
} 

function StartPrinting() 
{        
	GetHeader();
}

//Get the header html. When complete, go to processheader function wich will do the rest
function GetHeader() 
{        
	CreateXmlHttp();
	
	if(XmlHttp!=null) 
	{ 
		var url = "/DSMNutrinorm/Content/Bedrijf/GetHeader.aspx";

		XmlHttp.onreadystatechange = ProcessHeader;
		
		//alert("06.068 -> In GetHeader(). Te openen URL = " + url);
		XmlHttp.open("GET", url , true); 
		XmlHttp.send(null);
	} 
		        
	return false; 
}

//Get the footer html. When complete, go to the print function wich will do the rest
function GetFooter() 
{        
	
	CreateXmlHttp();
	
	if(XmlHttp!=null) 
	{ 
		var url = "/DSMNutrinorm/Content/Bedrijf/GetFooter.aspx";
		//var url = "/DSMNutrinorm/Templates/NutriNorm/Template/NutriNormPrintOutTemplate.htm";
		
		XmlHttp.onreadystatechange = ProcessFooter;
		
		//alert("06.068 -> In GetFooter(). Te openen URL = " + url);
		XmlHttp.open("GET", url , true); 
		XmlHttp.send(null);
	} 
		        
	return false; 
}

//print a page using the hader and footer that were previously fetched		
function PrintPageHeaderFooter()
{
	var buttonPagePrint = getElementById('ButtonPagePrint');
	var buttonHelp = getElementById('ButtonHelp');
	
	if(buttonPagePrint) hideElement('ButtonPagePrint');
	if(buttonHelp) hideElement('ButtonHelp');
	
	var printArea = getElementById('printarea');
	var headArea = getElementById('headarea');
	
	if(printArea)
	{
		//remove the title because it does not print well
		var title = getElementById('title');
		title.innerHTML = "";
		
		var printAreaHTML = printArea.innerHTML;
		var headAreaHTML = '';
		if(headArea) headAreaHTML = headArea.innerHTML;
		
		var windowHTML = '';
		windowHTML+='<html>\n';
		windowHTML+='<head>\n';
		if(headAreaHTML!='') windowHTML+=header + headAreaHTML;//add the header
		
		windowHTML+='<style>\n';
		windowHTML+='.navbuttons {visibility:hidden;height:0px;}\n';
		windowHTML+='</style>\n';
		windowHTML+='<script>\n';
		windowHTML+='function noerror() { return true; }\n';
		windowHTML+='window.onerror=noerror; \n';
		windowHTML+='</script>\n';
		windowHTML+='</head>\n';
		//windowHTML+='<body marginheight="0" marginwidth="0" class="main" onload="readonlyElements();window.print();">\n';
		windowHTML+='<body marginheight="0" marginwidth="0" class="main" onload="readonlyElements();">\n';
		windowHTML+='<form id="Form1">';
		windowHTML+=printAreaHTML;
		windowHTML+='</form>\n';
		windowHTML+=footer;		//add the footer
		windowHTML+='</body>\n';
		windowHTML+='</html>';		
		
		var printWindow = window.open("","printFriendly","height=600,width=800,left=1,top=1,toolbar=no,menubar=no,scrollbars=no,resizable=no");
		printWindow.document.open();
		printWindow.document.write(windowHTML);
		
		printWindow.document.close();
		printWindow.print();
		
		pause(2000);
		
		printWindow.close();
	}
	
	if(buttonPagePrint) showElement('ButtonPagePrint');
	if(buttonHelp) showElement('ButtonHelp');
}

function pause(numberMillis) 
{
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

function DisplayErrors(label)
{
	var control = document.getElementById(label);

	if (control.value != "")
	{
		alert(control.value);
	}
}

