/**
 * @class CCIPF
 * 
 * Creates the CCIPF framework API object
 * 
 * Revision History
 * 
 * v1.0: Initial API creation.
 * v1.1: Added support for handing both TSW and Emergence Header/Footer Styles.
 * v1.2: Added new 'isResponsive' flag to load responsive header and footer. Made header/footer optional.
 * v1.3: Added support to load header/footer 2nd time if it fails.
 * v2.0: Support to load new AEM header/footer UI. Removed support for emergence header/footer.
 */
var CCIPF = window.CCIPF = new function() {
	// prevent console.log errors in IE8
	if (!(window.console && console.log)) {
		console = {
			log: function() {}
		};
	}

	var loaderContent = "<div class='pf-loader pf-loader-active'></div>";

	var cssUrl		= "//webcdn.cox.com/ui/presentation/tsw/css/flex2-presentation.css";
	var cssIEUrl	= "//webcdn.cox.com/ui/presentation/tsw/css/presentation-ie.css.jgz";
	
	var headerUrl = "";
	var footerUrl = "";

	// default residential header content (if ajax fails)
	var defaultResidentialHeaderContent = "" +
		"<!-- header -->" +
		"<div id=\"pf-header\" class=\"pf-header-residential noindex\">" +
			"<!-- begin header wrapper -->" +
			"<div class=\"pf-header-wrapper\">" +
			"	<!-- begin menu panel - left side panel on mobile -->" +
			"	<div class=\"pf-menu-panel\">" +
			"		<!-- begin mobile wrapper -->" +
			"		<div class=\"pf-mobile-wrapper\">" +
			"			<!-- begin top header -->" +
			"			<div class=\"pf-top-header\">" +
			"				<div class=\"pf-top-nav\">" +
			"					<!-- left side of top nav -->" +
			"					<ul class=\"pf-top-nav-lob\"><li><img src=\"//webcdn2.cox.com/ui/presentation/tsw/img/header/default-header-res.gif\" \></li><\/ul>" +
			"					<!-- right side of top nav -->" +
			"					<ul class=\"pf-top-nav-overlays\">" +
			"						<li class=\"contact\"><a href=\"https:\/\/www.cox.com\/residential\/contactus.html\" title=\"Contact Us\">Contact Us<\/a><\/li>" +
			"					<\/ul>" +
			"				<\/div>" +
			"			<\/div>" +
			"			<!-- end top header -->" +
			"			<!-- begin main header -->" +
			"			<div class=\"pf-main-header\">" +
			"				<div class=\"pf-main-nav\">" +
			"					<div class=\"pf-main-left-nav\">" +
			"						<ul>" +
			"							<li class=\"header-logo\"><a href=\"https:\/\/www.cox.com\/residential\/home.html\" title=\"Residential Homepage\">Residential Homepage<\/a><\/li>" +
			"						<\/ul>" +
			"						<!-- begin primary link list -->" +
			"						<ul class=\"pf-main-nav-primary-links\">" +
			"							<li><a href=\"https:\/\/www.cox.com\/residential-shop\/order-cox-services.html\" title=\"Shop\">Shop<\/a><\/li>" +
			"							<li><a href=\"https:\/\/www.cox.com\/resimyaccount\/home.html\" title=\"My Account\">My Account<\/a><\/li>" +
			"							<li><a href=\"https:\/\/myemail.cox.net\" title=\"WebMail\">WebMail<\/a><\/li>" +
			"							<li><a href=\"http:\/\/www.cox.com\/residential\/myconnection\/home.html\" title=\"My Connection\">My Connection<\/a><\/li>" +
			"							<li><a href=\"https:\/\/www.cox.com\/residential\/support.html\" title=\"Support\">Support<\/a><\/li>" +
			"						<\/ul>" +
			"					<\/div>" +
			"				<\/div>" +
			"			<\/div>" +
			"			<!-- end main header -->" +
			"		<\/div>" +
			"		<!-- end mobile wrapper -->" +
			"		<!-- begin sub header -->" +
			"		<div class=\"pf-sub-header\">" +
			"			<div class=\"pf-sub-nav pf-search-items\">" +
			"				<div class=\"pf-sub-nav-underlay\">" +
			"					<div class=\"pf-sub-nav-close\"><a><\/a><\/div>" +
			"				<\/div>" +
			"			<\/div>" +
			"		<\/div>" +
			"		<!-- end sub header -->" +
			"	<\/div>" +
			"	<!-- end menu panel - left side panel on mobile -->" +
			"<\/div>" +
			"<!-- end header wrapper -->" +
			"<!-- mobile header -->" +
			"<div class=\"pf-mobile-menu-bar pf-mobile-only\">" +
			"	<ul>" +
			"		<li class=\"header-logo\"><a href=\"https:\/\/www.cox.com\/residential\/home.html\" title=\"Residential Homepage\">Residential Homepage<\/a></li>" +
			"	</ul>" +
			"</div>" +
			"<!-- /mobile header -->" +
		"</div>" +
		"<!-- /header -->";
	
	// default business header content (if ajax fails)
	var defaultBusinessHeaderContent = "" +
		"<!-- header -->" +
		"<div id=\"pf-header\" class=\"pf-header-business noindex\">" +
			"<!-- begin header wrapper -->" +
			"<div class=\"pf-header-wrapper\">" +
			"	<!-- begin menu panel - left side panel on mobile -->" +
			"	<div class=\"pf-menu-panel\">" +
			"		<!-- begin mobile wrapper -->" +
			"		<div class=\"pf-mobile-wrapper\">" +
			"			<!-- begin top header -->" +
			"			<div class=\"pf-top-header\">" +
			"				<div class=\"pf-top-nav\">" +
			"					<!-- left side of top nav -->" +
			"					<ul class=\"pf-top-nav-lob\"><li><img src=\"//webcdn2.cox.com/ui/presentation/tsw/img/header/default-header-biz.gif\" \></li><\/ul>" +
			"					<!-- right side of top nav -->" +
			"					<ul class=\"pf-top-nav-overlays\">" +
			"						<li class=\"contact\"><a href=\"https:\/\/www.cox.com\/business\/contact-us.html\" title=\"Contact Us\">Contact Us<\/a><\/li>" +
			"					<\/ul>" +
			"				<\/div>" +
			"			<\/div>" +
			"			<!-- end top header -->" +
			"			<!-- begin main header -->" +
			"			<div class=\"pf-main-header\">" +
			"				<div class=\"pf-main-nav\">" +
			"					<div class=\"pf-main-left-nav\">" +
			"						<ul>" +
			"							<li class=\"header-logo\"><a href=\"https:\/\/www.cox.com\/business\/home.html\" title=\"Business Homepage\">Business Homepage<\/a><\/li>" +
			"						<\/ul>" +
			"						<!-- begin primary link list -->" +
			"						<ul class=\"pf-main-nav-primary-links\">" +
			"							<li><a href=\"https:\/\/www.cox.com\/business\/products.html\" title=\"Products &amp; Solutions\">Products &amp; Solutions<\/a><\/li>" +
			"							<li><a href=\"https:\/\/www.cox.com\/business\/industry-expertise.html\" title=\"My Account\">Industry Expertise<\/a><\/li>" +
			"							<li><a href=\"https:\/\/www.cox.com\/business\/support.cox\" title=\"Support &amp; Resources\">Support &amp; Resources<\/a><\/li>" +
			"							<li><a href=\"https:\/\/myaccount.coxbusiness.com\" title=\"MyAccount\">MyAccount<\/a><\/li>" +
			"							<li><a href=\"https:\/\/myaccount.coxbusiness.com\/billingtool\/billmanagement.cox\" title=\"Billing Tools\">Billing Tools<\/a><\/li>" +
			"						<\/ul>" +
			"					<\/div>" +
			"				<\/div>" +
			"			<\/div>" +
			"			<!-- end main header -->" +
			"		<\/div>" +
			"		<!-- end mobile wrapper -->" +
			"		<!-- begin sub header -->" +
			"		<div class=\"pf-sub-header\">" +
			"			<div class=\"pf-sub-nav pf-search-items\">" +
			"				<div class=\"pf-sub-nav-underlay\">" +
			"					<div class=\"pf-sub-nav-close\"><a><\/a><\/div>" +
			"				<\/div>" +
			"			<\/div>" +
			"		<\/div>" +
			"		<!-- end sub header -->" +
			"	<\/div>" +
			"	<!-- end menu panel - left side panel on mobile -->" +
			"<\/div>" +
			"<!-- end header wrapper -->" +
			"<!-- mobile header -->" +
			"<div class=\"pf-mobile-menu-bar pf-mobile-only\">" +
			"	<ul>" +
			"		<li class=\"header-logo\"><a href=\"https:\/\/www.cox.com\/business\/home.html\" title=\"Business Homepage\">Business Homepage<\/a></li>" +
			"	</ul>" +
			"</div>" +
			"<!-- /mobile header -->" +
		"</div>" +
		"<!-- /header -->";

	// build end date variable for footer
	var now = new Date();
	var endYear = now.getFullYear();

	// default footer content (if ajax fails)
	var defaultFooterContent = "" +
		"<!-- footer -->" +
		"<div id=\"pf-footer\" class=\"pf-footer-residential noindex\">" +
		"	<!-- top footer -->" +
		"	<div class=\"pf-top-footer\">" +
		"		<div class=\"pf-top-footer-inner\">" +
		"			<div class=\"pf-footer-content pf-clearfix\">" +
		"				<div class=\"pf-footer-skip-col\">" +
		"					<ul>" +
		"						<div class=\"aem-linklist section\">" +
		"							<div>" +
		"								<li class=\"pf-footer-content-header\">I want to...<\/li>" +
		"								<li><a href=\"https:\/\/www.cox.com\/mybill\/home.html\" title=\"View &amp; Pay My Bill\">View &amp; Pay My Bill<\/a><\/li>" +
		"								<li><a href=\"https:\/\/www.cox.com\/resimyaccount\/home.html\" title=\"Manage My Account\">Manage My Account<\/a><\/li>" +
		"								<li><a href=\"https:\/\/myemail.cox.net\" title=\"Check My WebMail\">Check My WebMail<\/a><\/li>" +
		"							<\/div>" +
		"						<\/div>" +
		"					<\/ul>" +
		"					<ul>" +
		"						<div class=\"aem-linklist section\">" +
		"							<div>" +
		"								<li class=\"pf-footer-content-header\">&nbsp;<\/li>" +
		"								<li><a href=\"https:\/\/www.cox.com\/residential\/support.html\">Get Support for My Home Services<\/a><\/li>" +
		"								<li><a href=\"https:\/\/www.cox.com\/business\/support.cox\">Get Support for My Business Services<\/a><\/li>" +
		"								<li><a href=\"https:\/\/www.cox.com\/aboutus\/contact-us\/cox-centers.html\">Find a Retail Location<\/a><\/li>" +
		"							<\/div>" +
		"						<\/div>" +
		"					<\/ul>" +
		"					<ul class=\"pf-footer-content-headerextra\">" +
		"						<li class=\"pf-footer-content-header\">&nbsp;<\/li>" +
		"					<\/ul>" +
		"				<\/div>" +
		"				<div class=\"pf-footer-skip-col-middle\" style=\"width:10%;\">&nbsp;<\/div>" +
		"				<div class=\"pf-footer-skip-col-right\">" +
		"					<ul>" +
		"						<div class=\"aem-linklist section\">" +
		"							<div>" +
		"								<li class=\"pf-footer-content-header\"><a href=\"https:\/\/storewww.cox.com\/residential-shop\/shop.cox\" title=\"Shop\">Shop<\/a><\/li>" +
		"								<li><a href=\"https:\/\/storewww.cox.com\/residential-shop\/shop.cox\" title=\"TV\">TV<\/a><\/li>" +
		"								<li><a href=\"https:\/\/storewww.cox.com\/residential-shop\/shop.cox?hsi=w\" title=\"Internet\">Internet<\/a><\/li>" +
		"								<li><a href=\"https:\/\/storewww.cox.com\/residential-shop\/shop.cox?dt=w\" title=\"Home Phone\">Home Phone<\/a><\/li>" +
		"								<li><a href=\"https:\/\/storewww.cox.com\/store\/ordernow.cox\">Cox Business Services<\/a><\/li>" +
		"							<\/div>" +
		"						<\/div>" +
		"					<\/ul>" +
		"				<\/div>" +
		"			<\/div>" +
		"		<\/div>" +
		"	<\/div>" +
		"	<!-- \/top footer -->" +
		"	<!-- bottom footer -->" +
		"	<div class=\"pf-bottom-footer\">" +
		"		<div class=\"pf-bottom-footer-inner\">" +
		"			<div class=\"pf-other-businesses pf-clearfix\">" +
		"			<\/div>" +
		"			<div class=\"pf-general-info pf-clearfix\" style=\"border:0;padding-top:0;\">" +
		"				<div class=\"pf-copyright\">" +
		"					&copy; 1998-" + endYear + " Cox Communications, Inc." +
		"				<\/div>" +
		"				<div class=\"pf-social-icons\">" +
		"					<a href=\"http:\/\/www.youtube.com\/user\/CoxCommTV\" class=\"pf-social-you-tube\" title=\"YouTube\" target=\"_blank\">YouTube<\/a>" +
		"					<a href=\"http:\/\/instagram.com\/coxcommunications\" class=\"pf-social-instagram\" title=\"Instagram\" target=\"_blank\">Instagram<\/a>" +
		"					<a href=\"https:\/\/www.facebook.com\/coxcommunications\" class=\"pf-social-facebook\" title=\"Facebook\" target=\"_blank\">Facebook<\/a>" +
		"					<a href=\"https:\/\/twitter.com\/coxcomm\" class=\"pf-social-twitter\" title=\"Twitter\" target=\"_blank\">Twitter<\/a>" +
		"					<a href=\"https:\/\/plus.google.com\/u\/0\/+coxcommunications\/posts\" class=\"pf-social-google-plus\" title=\"Google+\" target=\"_blank\">Google+<\/a>" +
		"				<\/div>" +
		"			<\/div>" +
		"		<\/div>" +
		"	<\/div>" +
		"	<!-- \/bottom footer -->" +
		"<\/div>" +
		"<!-- \/footer -->";

	// determine which default header should be used
	var defaultHeaderContent = "";
	if ("cmsresidential" == "cmsbusiness") {
		defaultHeaderContent = defaultBusinessHeaderContent;
	} else if ("cmsresidential" == "cmsbusiness-myaccount") {
		defaultHeaderContent = defaultBusinessHeaderContent; 
	}else if ("cmsresidential" == "cmsbusiness-cbvm") {
		defaultHeaderContent = defaultBusinessHeaderContent; 
	}else {
		defaultHeaderContent = defaultResidentialHeaderContent;
	}

	/**
	 * CCIPF.init
	 * 
	 * this is a placeholder in case someone upgrades to 2.0 and doesn't remove the old
	 * CCIPF.init with header/footer url's from page
	 */
	this.init = function() {
		return;
	};

	/**
	 * CCIPF.exec
	 */
	this.exec = function() {
		console.log("CCIPF.exec");

		// build the header and footer urls
		if ("cmsresidential" == "cmsresidential") {
			headerUrl = "//www.cox.com/webapi/cmsapi/header/cmsresidential";
			footerUrl = "//www.cox.com/webapi/cmsapi/footer/cmsresidential"; 
		} else if ("cmsresidential" == "cmsbusiness") {
			headerUrl = "//www.cox.com/webcbapi/cmsapi/header/cmsresidential";
			footerUrl = "//www.cox.com/webcbapi/cmsapi/footer/cmsresidential"; 
		} else if ("cmsresidential" == "cmsbusiness-cbvm") {
			headerUrl = "//www.cox.com/webcbapi/cmsapi/header/cmsresidential";
			footerUrl = "//www.cox.com/webcbapi/cmsapi/footer/cmsresidential"; 
		} else if ("cmsresidential" == "cmsbusiness-myaccount") {
			headerUrl = "//www.cox.com/webcbapi/cmsapi/header/cmsresidential";
			footerUrl = "//www.cox.com/webcbapi/cmsapi/footer/cmsresidential"; 
		} else {
			headerUrl = "//www.cox.com/presentation/cmsapi/cms/header/cmsresidential";
			footerUrl = "//www.cox.com/presentation/cmsapi/cms/footer/cmsresidential";
		}

		// set up header and footer
		this._initHeader(headerUrl);
		this._initFooter(footerUrl);

		this._CCIcssURL = cssUrl;
		this._CCIcssIEURL = cssIEUrl;

		// load the framework css file
		this._loadCss(this._CCIcssURL);

		// load the IE 9 and below specific framework css file
		if (this._isIE() && this._isIE() <= 9) {
			this._loadCss(this._CCIcssIEURL);
		}

		// load the header (footer comes later)
		this._loadHeader();
	};

	/**
	 * _isIE
	 * 
	 * IE version check
	 */
	this._isIE = function() {
		var myNav = navigator.userAgent.toLowerCase();
		return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
	};

	/**
	 * _script
	 */
	this._script = null;

	/**
	 * _initHeader
	 * 
	 * Create the CoxPFHeader header div and load the throbber.
	 */
	this._initHeader = function(headerUrl) {
		console.log("CCIPF._initHeader");
		this.headerDiv = "CoxPFHeader";

		// create the header div at the top of the body DOM
		var bodyTop = document.body.children[0];
		var bodyTopElem = document.createElement("div");
		bodyTopElem.id = this.headerDiv;
		document.body.insertBefore(bodyTopElem, bodyTop);

		// add the loading animation into the header
		document.getElementById(this.headerDiv).innerHTML = loaderContent;

		this.headerUrl = headerUrl;
	};

	/**
	 * _initFooter
	 * 
	 * Create the CoxPFFooter footer div and load the throbber.
	 */
	this._initFooter = function(footerUrl) {
		console.log("CCIPF._initFooter");

		this.footerDiv = "CoxPFFooter";

		// create the footer div at the bottom of the body DOM
		var bodyBotElem = document.createElement("div");
		bodyBotElem.id = this.footerDiv;
		document.body.appendChild(bodyBotElem);

		// add the loading animation into the footer
		document.getElementById(this.footerDiv).innerHTML = loaderContent;

		this.footerUrl = footerUrl;
	};

	/**
	 * _loadCss
	 * 
	 * Load any external Css files and add to the HTML HEAD
	 */
	this._loadCss = function(url) {
		console.log("CCIPF_loadCss");

		var head = document.getElementsByTagName("head")[0];
		if (!head) {
			fail();
			return;
		}
		this._css = document.createElement('link');
		this._css.rel = 'stylesheet';
		this._css.href = url;
		head.appendChild(this._css);
	};

	/**
	 * _loadJS
	 * 
	 * Load any external JavaScript files and add to the HTML HEAD and issue a success or failure callback.
	 */
	this._loadJS = function(url) {
		console.log("CCIPF_loadJS", url);

		var head = document.getElementsByTagName("head")[0];
		if (!head) {
			fail();
			return;
		}
		this._script = document.createElement('script');
		this._script.type = 'text/javascript';
		this._script.src = url;
		head.appendChild(this._script);
	};

	/**
	 * _loadHeader
	 * 
	 * Load the header
	 */
	this._loadHeader = function() {
		console.log("CCIPF_loadHeader");

		if (CCIPF.headerUrl) {
			CCIPF._loadComponentXHR(CCIPF.headerUrl, CCIPF.headerDiv);
		}
	};
	
	/**
	 * _loadFooter
	 * 
	 * Load the footer
	 */
	this._loadFooter = function() {
		console.log("CCIPF_loadFooter");

		if (CCIPF.footerUrl) {
			CCIPF._loadComponentXHR(CCIPF.footerUrl, CCIPF.footerDiv);
		}
	};
	
	/**
	 * _loadComponentXHR
	 * 
	 * Load an html file and inject the contents into a given DOM element.
	 */
	this._loadComponentXHR = function(url, target) {
		console.log("CCIPF_loadComponentXHR: " + target);
		
		if (target === "CoxPFHeader" ) {
			var displayContent = defaultHeaderContent;
		}

		if (target === "CoxPFFooter" ) {
			var displayContent = defaultFooterContent;
		}

		var contentRequest = CCIPF._createCORSRequest("get", url);
		if (contentRequest) {
			contentRequest.onload = function() {
				document.getElementById(target).innerHTML = contentRequest.responseText;
				console.log("CCIPF content loaded for: " + target);

				if (target === "CoxPFHeader" ) {
					// load the footer on after the header is loaded,
					// otherwise loading presentation.js may fail if timing is off
					CCIPF._loadFooter();
					// eval the header html to execute (any load) any javascript in the response
					CCIPF._processScripts(target);
				}
				
				if (target === "CoxPFFooter" ) {
					// eval the footer html to execute (any load) any javascript in the response
					CCIPF._processScripts(target);
				}
			};
			contentRequest.onerror = function() {
				console.log("CCIPF content error for: " + target + ". Loading default content.");
				document.getElementById(target).innerHTML = displayContent;
			};
			contentRequest.timeout = 15000;
			contentRequest.ontimeout = function() {
				console.log("CCIPF content timeout for: " + target + ". Loading default content.");
				document.getElementById("CoxPFHeader").innerHTML = defaultHeaderContent;
				document.getElementById("CoxPFFooter").innerHTML = defaultFooterContent;
			};
			contentRequest.send();
		} else {
			console.log("CORS Failure");
			document.getElementById("CoxPFHeader").innerHTML = defaultHeaderContent;
			document.getElementById("CoxPFFooter").innerHTML = defaultFooterContent;
			console.log("CCIPF load content failure for: " + target);
		}
	};

	/**
	 * _processScripts
	 * 
	 * Process any internal and external scripts loaded in a block of content
	 */
	this._processScripts = function(target) {
		console.log("CCIPF_processScripts for: " + target);
		
		var targetObj = document.getElementById(target);
		var arr = targetObj.getElementsByTagName('script');
		for (var n = 0; n < arr.length; n++) {
		    if (arr[n].src != "") {
		    	// load any externally referenced js files
		        CCIPF._loadJS(arr[n].src);
		    } else if (arr[n].type != "application/json" && arr[n].type !== "text/template" && arr[n].type !== "application/ld+json") { 
				// evaluate any inline scripts
				CCIPFglobalEval(arr[n].innerHTML);
		    }
		}
	};
	
	/**
	 * _createCORSRequest
	 * 
	 * XDomain Ajax Method 
	 */
	this._createCORSRequest = function(method, url) {
		console.log("CCIPF_createCORSRequest");

		var xhr = new XMLHttpRequest();
		if ("withCredentials" in xhr) {
			xhr.open(method, url, true); // XHR for Chrome/Safari/Firefox
			xhr.withCredentials = true; // Allows for cookie sharing
		} else if (typeof XDomainRequest != "undefined") { //IE: http://msdn.microsoft.com/en-us/library/cc288060%28VS.85%29.aspx
			xhr = new XDomainRequest();
			if (xhr) {
				xhr.open(method, url);
			} else {
				console.log("XDomainRequest error");
			}
		} else {
			xhr = null;
		}
		return xhr;
	};

};

/**
 * CCIPFcompleted
 * 
 * The ready event handler and self cleanup method
 */
function CCIPFcompleted() {
	console.log("CCIPFcompleted");
	// readyState === "complete" is good enough for us to call the dom ready in oldIE
	if (document.addEventListener || event.type === "load" || document.readyState === "complete") {
		CCIPFdetach();
		CCIPF.exec();
	}
}

/**
 * CCIPFglobalEval
 * Evaluates a script in a global context
 * Workarounds based on findings by Jim Driscoll
 * http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
 */
function CCIPFglobalEval( data ) {
	console.log("CCIPFglobalEval");
	//if ( data && jQuery.trim( data ) ) {
	if ( data ) {
		// We use execScript on Internet Explorer
		// We use an anonymous function so that context is window
		( window.execScript || function( data ) {
			window[ "eval" ].call( window, data );
		} )( data );
	}
}
	
/**
 * CCIPFdetach
 * 
 * Clean-up method for dom ready events
 */
function CCIPFdetach() {
	console.log("CCIPFdetach");
	if (document.addEventListener) {
		document.removeEventListener("DOMContentLoaded", CCIPFcompleted, false);
		window.removeEventListener("load", CCIPFcompleted, false);
	} else {
		document.detachEvent("onreadystatechange", CCIPFcompleted);
		window.detachEvent("onload", CCIPFcompleted);
	}
}

/**
 * Add the event handlers so we no longer need the init at the bottom of the html
 */
if (document.addEventListener) {
	// Use the handy event callback
	document.addEventListener("DOMContentLoaded", CCIPFcompleted, false);

	// A fallback to window.onload, that will always work
	window.addEventListener("load", CCIPFcompleted, false);

	// If IE event model is used
} else {
	// Ensure firing before onload, maybe late but safe also for iframes
	document.attachEvent("onreadystatechange", CCIPFcompleted);

	// A fallback to window.onload, that will always work
	window.attachEvent("onload", CCIPFcompleted);
}