var OverlayAPI = null, OverlayElementId = "demoOverlay", OverlayUrl = "http://" +  VolusionStoreDomain + "/v/demo-setup2/shopping-cart-software-free-trial.asp";
var GAStepName = "/free-trial/form-1";
function CloseWizardCancelOnClick(override) {
	document.getElementById('close_box').style.display = 'none';
	if(typeof(override)=="object")
	{
		override=false;
	}
	if(typeof(override)=="boolean" && override==false)
	{
		ProcessEventMain(null,'free trial',GAStepName,'close trial',null);
	}
}
function CloseWizardOnClick() {
	document.getElementById('close_box').style.display = 'block';
}
function OpenWizardOnClick() {
	CloseWizardCancelOnClick(true);

	if (document.getElementById("DemoSetupIFrame").src.indexOf("blank.html") != -1) {
		document.getElementById("DemoSetupIFrame").src = OverlayUrl;
	}
}

function CreateWizardOverlay(id) {
	return $(id).overlay({
	//oneInstance: false,
	onBeforeLoad: OpenWizardOnClick,
	onBeforeClose: CloseWizardCancelOnClick,
	closeOnClick: false,
	expose: {
		color: '#949494',
		loadSpeed: 0,
		maskId: 'demo_mask'
		},
	api: true
	});
}

$(document).ready(function() {
	var overlayDiv = document.createElement('div');
	overlayDiv.id = OverlayElementId;
	overlayDiv.className = OverlayElementId;

	var overlayHtml = '<div class="wizard_wrapper demoWrapper" id="wizard_wrapper">{0}<div id="demoBG_T"></div><a class="close demoClose" id="closeDemo"></a><div id="demoBGIFrameWrapper"><iframe allowtransparency="true" id="DemoSetupIFrame" src="//' + VolusionStoreDomain + '/blank.html" frameborder="0"></iframe></div><div id="demoWrapper_B_Content"><div id="DemoOverlayLiveChat">Have questions or need assistance? One of our expert consultants will<br /> be happy to help you. <a href="javascript: ProcessEventMain(null,\'free trial\',\'' + GAStepName + '\',\'Live Chat\',null);LaunchVolusionChat(0);">Click here</a> for live chat or call 800.646.3517</div><div id="secureServer"><img src="//' + VolusionStoreDomain + '/v/demo-setup2/images/lock_graphic.png" alt="safe" id="lock" /><a id="secureLink">Secure server</a></div></div><div id="demoWrapper_B"></div><div id="demoBG_B"></div></div>';

	var overlayCloseHtml = '<div id="close_box" style="display: none;"><div class="close_box_content"><h2> Are you sure you want to close your 14-day trial?</h2><div class="btn_wrapper"><a class="submit_btn close">Yes</a><a class="submit_btn" onclick="CloseWizardCancelOnClick();">No</a></div></div></div>';
	
	overlayHtml = overlayHtml.replace("{0}", overlayCloseHtml);

	overlayDiv.innerHTML = overlayHtml;
	document.body.appendChild(overlayDiv);
	
	if ($(document).pngFix) $(document).pngFix();

	OverlayAPI = CreateWizardOverlay("a.demoLink");

	var curLocation = window.location.href;
	var forwardKeys = ["email", "Customers.Custom_Field_Industry"];
	for (var i = 0; i < forwardKeys.length; i++) {
		var keyName = forwardKeys[i];
		var position = curLocation.indexOf("&" + keyName + "=");
		if (position == -1) continue;
		position += keyName.length + 2;
		if (curLocation.indexOf("&", position) != -1) {
			var keyValue = curLocation.substr(position, curLocation.indexOf("&", position) - position);
		}
		else {
			var keyValue = curLocation.substr(position);
		}
		keyValue = decodeURIComponent(keyValue);
		OverlayUrl = (OverlayUrl.indexOf("?") == -1) ? OverlayUrl + "?" : OverlayUrl + "&";
		OverlayUrl = OverlayUrl + keyName + "=" + keyValue;
	}

	if (curLocation.indexOf("?trigger=tryitfree") != -1) {
		OverlayAPI.load();
	}
});

function ProcessEventMain(pageFieldObj,category,action,label,value){

try
	{
		if(value==null)
		{
			if(pageFieldObj)
			{
				if(pageFieldObj.value!='')
				{
					value = pageFieldObj.value;				
				}
			}
		}
		pageTracker._trackEvent(category, action, label, value);
	}catch(err){
		
	}
}
