var lib_location = "http://new.volvocars.com/financeCalculator/";
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/
var IE7 = (document.all && !window.opera && window.XMLHttpRequest && navigator.userAgent.toString().toLowerCase().indexOf('Trident/4.0') == -1) ? true : false;
var iframeAdded = false;

jQuery(function() {
	openSoftOffer = function(url, xmlstr) {
		//add overlay
		document.body.appendChild(_getTransparentOverlay());
		
		//add iframe
		document.body.appendChild(_getCalcHTML());	
	
		if(iframeAdded == false) {
			//add form and submit
		    jQuery("body").append('<form enctype="application/x-www-form-urlencoded" action="'+url+'" method="POST" name="frmXML" target="softoffer" style="display: none;"><textarea rows="5" cols="30" readonly="true" name="txtXML" id="iframe_txtXML">'+xmlstr+'</textarea></form>');
		
			//for multiple opens
			iframeAdded = true;
		} else {
			$("#iframe_txtXML").val(xmlstr);
		}
				
	    document.frmXML.submit();
	}
});

function _getTransparentOverlay() {
	var transparentOverlay = document.createElement('div');
	
	var viewportwidth;
	var viewportheight;
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
	  viewportwidth = window.innerWidth,
	  viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	   viewportwidth = document.documentElement.clientWidth,
	   viewportheight = document.documentElement.clientHeight
	} else { // older versions of IE
	   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	if(viewportheight < 900) { viewportheight = 900; }
	
	//set style
	transparentOverlay.style.height = viewportheight + "px";
	transparentOverlay.style.width = viewportwidth + "px";
	
	if(IE6 || IE7) {
		transparentOverlay.style.filter = "alpha(opacity=80)";
	} else {
		transparentOverlay.style.opacity = "0.8";
	}
	
	transparentOverlay.style.zIndex = "90";
	transparentOverlay.style.backgroundColor = "#000";
	transparentOverlay.style.position = "absolute";
	transparentOverlay.style.top = "0px";
	transparentOverlay.style.left = "0px";
	
	transparentOverlay.id = "fcalc-transparent-overlay";
	
	return transparentOverlay;
}

function _getCalcHTML() {
	var extension = (IE6) ? "gif" : "png";
	
	var html =  '<div class="fcalc-overlay-container" style="margin:0 auto; position:relative;text-align:left;width:900px;"><div id="fcalc-overlay" style="height:757px; left: 7px; position:absolute; top:20px;width:885px;">'
	html += '	<img src="' + lib_location + 'images/fcalc_ol_top.' + extension + '" width="885" height="12" alt="" class="pngfix" /><div id="fcalc-overlay-top" style="background:url(' + lib_location + 'images/bg_header.jpg) repeat-x top left; height:21px;"><a href="javascript: hideCalc();"><img src="' + lib_location + 'images/fcalc_ol_close.png" width="21" height="21" alt="X" title="X" id="fcalc-overlay-close" style="float:right;margin-right:7px;" /></a></div>';
	html += '	<iframe name="softoffer" height="610" width="885" style="overflow: hidden;" frameborder="0" scrolling="no"></iframe>';
	html += '	<img src="' + lib_location + 'images/fcalc_ol_btm.' + extension + '" width="885" height="12" alt="" class="pngfix" />';
	html += '</div></div>';

	
	
	var calcOverlay = document.createElement('div');
	calcOverlay.id = "fcalc-overlay-base";
	calcOverlay.innerHTML = html;
	calcOverlay.style.position = "absolute";
	calcOverlay.style.top = "0px";
	calcOverlay.style.left = "0px";
	calcOverlay.style.width = "100%";
	calcOverlay.style.textAlign = "center";
	calcOverlay.style.zIndex = "100";
	
	return calcOverlay;
}	

function hideCalc() {
	var base = document.getElementById('fcalc-overlay-base');
	base.parentNode.removeChild(base);
	
	base = document.getElementById('fcalc-transparent-overlay');
	base.parentNode.removeChild(base);
}
