﻿/// <reference path="~/Assets/Scripts/Generic/jquery-1.2.6.min.js" />

function printTips()
{
	//create an iframe
	var iframe = document.createElement("iframe");
	iframe.frameBorder = 0;
	iframe.frameMargin = 0;
	iframe.framePadding = 0;
	iframe.height = 200;
	iframe.id = "tempprintiframe";
	iframe.name = "tempprintiframe";
	//hide the iframe
	//dealt with by css
	
	//load fuel-saving-tips.aspx into the iframe
	iframe.src = "fuel-saving-tips.aspx";
		
	//wait for the iframe to load
	//might not need to do this as print dialog may wait for load
	
	//put the iframe onto the page
	document.body.appendChild(iframe);       
	
	//print the iframe
	tryprint();
	//remove the iframe
	//can't as ffx print won't work, will have to leave it behind
}


var retries = 0;
function tryprint()
{
	
	var iframe;
	if ($.browser.msie)
	{ 
		//alert(window.frames.length);
		//TODO: get the correct frame otherwise we're not able to reliably add other iframes to the site
		iframe = window.frames[1];// document.getElementById('tempprintiframe'); 
	} 
	else 
	{ 
		iframe = window.frames['tempprintiframe'];
	}
	
	if(retries >=60)
	{
		return;
	}
	else
	{
		retries++;
	}
	
	if(
		($.browser.msie && (iframe == undefined || iframe.document == undefined))
		||
		(!$.browser.msie &&(iframe == undefined || iframe.document == undefined || iframe.document.title == ""))
		)
	{
		setTimeout(tryprint, 100);
	}
	else
	{
		iframe.focus();
		iframe.print();
	}
}