var cookie1 = "joiku_email_light";
var cookie2 = "joiku_email_trial";
var cookie3 = "joiku_email_other";
var cookie4 = "joiku_email_boost"
var expires = 60 * 60 * 24 * 30; // month

function change(choice)
{
	var pic
	if(choice == 0)
	{
		document.getElementById('popupEmail').value = ""
		document.getElementById('popupEmail').disabled = true
		pic = "images/popUp/btnDownload.png"
		showEmailHint(false)
		document.getElementById('submitAndDownload').disabled = false
	}
	else
	{
		pic = "images/popUp/btnSubmitDownload.png"
		document.getElementById('popupEmail').disabled = false
		showEmailHint(true)
		document.getElementById('submitAndDownload').disabled = true
	}
	document.getElementById('submitAndDownload').src = pic
}

function showEmailHint(show)
{
	if(show) document.getElementById('popupEmail').value = "[Your e-mail address]"
	else document.getElementById('popupEmail').value = ""
	
	document.getElementById('submitAndDownload').disabled = false
}

function showSuccesfulDownload(productId)
{
	switch(productId)
	{
		case 310: setCookie(cookie1,"1",expires); break;
		case 451: setCookie(cookie2,"1",expires); break;
		case 576: setCookie(cookie4,"1",expires); break;
		default:  setCookie(cookie3,"1",expires); break;
	}
	
	var phoneListIndex = document.download_form.phonemodel.selectedIndex;
	var selectedPhone = document.download_form.phonemodel[phoneListIndex].value;
	var email = document.getElementById('popupEmail').value;

	document.getElementById('popUpBox').innerHTML = 
		'<div id="popUpBox">' +
			'<div id="popUpCloseButton"><a href="javascript:closeLayer(\'background\')"><img src="images/popUp/popupClose.png" /></a></div>' +
			'<div id="popUpHeader"><h1>Thank you for using Joiku.com! Your download will begin shortly.</h1></div>' +
			'<form id="popUpForm2" action="?action=products&amp;mode=download" method="post">' +
				'<input type="hidden" name="product_id" value="' + productId + '" />' +
				'<input type="hidden" name="phonemodel" value="' + selectedPhone + '" />' +
				'<input type="hidden" name="email" id="popupEmail" value="' + email + '" />' +
			'</form>' +
			'<div style="margin-left:180px;"><a href="javascript:closeLayer(\'background\')">Back to Joiku.com</a></div>' +
		'</div>';

	document.getElementById('popUpForm2').submit();
}

function showSuccesfulDownload2(productId)
{
	switch(productId)
	{
		case 310: setCookie(cookie1,"1",expires); break;
		case 451: setCookie(cookie2,"1",expires); break;
		case 576: setCookie(cookie4,"1",expires); break;
		default:  setCookie(cookie3,"1",expires); break;
	}
	
	var phoneListIndex = document.download_form2.phonemodel.selectedIndex;
	var selectedPhone = document.download_form2.phonemodel[phoneListIndex].value;
	var email = document.getElementById('popupEmail').value;

	document.getElementById('popUpBox').innerHTML = 
		'<div id="popUpBox">' +
			'<div id="popUpCloseButton"><a href="javascript:closeLayer(\'background\')"><img src="images/popUp/popupClose.png" /></a></div>' +
			'<div id="popUpHeader"><h1>Thank you for using Joiku.com! Your download will begin shortly.</h1></div>' +
			'<form id="popUpForm2" action="?action=products&amp;mode=download" method="post">' +
				'<input type="hidden" name="product_id" value="' + productId + '" />' +
				'<input type="hidden" name="phonemodel" value="' + selectedPhone + '" />' +
				'<input type="hidden" name="email" id="popupEmail" value="' + email + '" />' +
			'</form>' +
			'<div style="margin-left:180px;"><a href="javascript:closeLayer(\'background\')">Back to Joiku.com</a></div>' +
		'</div>';

	document.getElementById('popUpForm2').submit();
}

function showEmailDialog(productId)
{
	// Show popup once a month for the two products and for the rest
	var show_popup = true;
	if(getCookie(cookie1) != "" & productId == 310) show_popup = false;
	else if(getCookie(cookie2) != "" & productId == 451) show_popup = false;
	else if(getCookie(cookie4) != "" & productId == 576) show_popup = false;
	else if(getCookie(cookie3) != "" & productId != 310 & productId != 451) show_popup = false;
	
	if(show_popup)
	{
		createLayer('background','emailDialog')
		var container = document.createElement('div')
		container.setAttribute('id', 'background');	

		var ie=document.all;
		var ns6=document.getElementById && !document.all;	
		winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
		winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
		var top = winheight/2 -167 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
		var left = winwidth/2 -200;

		container.style.zIndex = '100';
		container.style.position = 'absolute';
		container.style.top = (top>0)?top + 'px':0;
		container.style.left = (left>0)?left + 'px':0;
		container.style.padding = '10px';

		details = document.createElement('div')
		details.setAttribute('id', 'emailDialog')

		var phoneListIndex = document.download_form.phonemodel.selectedIndex;
		var selectedPhone = document.download_form.phonemodel[phoneListIndex].value;

		if(ie) {
			style = 'style="position: relative"';
			style2 = 'style="margin-top: -20px"';	
		}	
		else {
			style = 'style="position: absolute"';
			style2 = 'style="margin-top: 15px"';
		}

		details.innerHTML = 		

			'<div id="popUpBox">' +
			'<div id="popUpCloseButton" ' + style + '>' +
			'<a href="javascript:closeLayer(\'background\')"><img src="images/popUp/popupClose.png"></a>' +
			'</div>' +
			'<div id="popUpHeader" ' + style2 + '><h2>Get product updates to your email!</h2></div>' + 
			'<div id="popUpText"><p>Subscription is not compulsory. You can unsubscribe at any time.<br/>You can also subscribe later at Joiku.com main page.</p></div>' +		
			'<div id="popUpForm">' +
			'<form action="?action=products&amp;mode=download" method="post" onSubmit="showSuccesfulDownload(' + productId + ')">' +
			'<input type="hidden" name="phonemodel" value="' + selectedPhone + '">' +
			'<input type="hidden" name="product_id" value="' + productId + '" />' +
			'<div style="margin-right: 5px; margin-bottom:5px;"><input type="radio" name="subscribe" value="1" onClick="change(1);" /></div>' +
			'<div style="width: 390px; margin-right: 5px; margin-bottom:5px;">Yes, I would like to receive product updates from Joiku to my email.</div>' +
			'<div style="margin-right: 5px; margin-bottom:5px;"><input type="radio" name="subscribe" value="0" onClick="change(0);" /></div>' +
			'<div style="width: 390px; margin-right: 5px; margin-bottom:15px;">No, thanks.</div>' +
			'<div style="width: 200px; margin-top: 20px;">' +
			'<input type="text"  id="popupEmail" name="email" value="[Your e-mail address]" onClick="showEmailHint(false);" disabled="true" />' +
			'</div>' +
			'<div style="width: 200px; margin-top: 20px;">' +
			'<input type="image" id="submitAndDownload" src="images/popUp/btnDownload.png" disabled="true" />' +
			'</div>' +
			'</form>' +
			'</div>' +
			'</div>'

			container.appendChild(details)

			document.body.appendChild(container)
	}
	else // Download directly
	{
		document.getElementById('download_form').submit();
	}
}

function showEmailDialog2(productId)
{
	// Show popup once a month for the two products and for the rest
	var show_popup = true;
	if(getCookie(cookie1) != "" & productId == 310) show_popup = false;
	else if(getCookie(cookie2) != "" & productId == 451) show_popup = false;
	else if(getCookie(cookie4) != "" & productId == 576) show_popup = false;
	else if(getCookie(cookie3) != "" & productId != 310 & productId != 451) show_popup = false;
	

	if(show_popup)
	{
		createLayer('background','emailDialog')
		var container = document.createElement('div')
		container.setAttribute('id', 'background');	

		var ie=document.all;
		var ns6=document.getElementById && !document.all;	
		winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight;
		winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
		var top = winheight/2 -167 + ((window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop);
		var left = winwidth/2 -200;

		container.style.zIndex = '100';
		container.style.position = 'absolute';
		container.style.top = (top>0)?top + 'px':0;
		container.style.left = (left>0)?left + 'px':0;
		container.style.padding = '10px';

		details = document.createElement('div')
		details.setAttribute('id', 'emailDialog')

		var phoneListIndex = document.download_form2.phonemodel.selectedIndex;
		var selectedPhone = document.download_form2.phonemodel[phoneListIndex].value;

		if(ie) {
			style = 'style="position: relative"';
			style2 = 'style="margin-top: -20px"';	
		}	
		else {
			style = 'style="position: absolute"';
			style2 = 'style="margin-top: 15px"';
		}

		details.innerHTML = 		

			'<div id="popUpBox">' +
			'<div id="popUpCloseButton" ' + style + '>' +
			'<a href="javascript:closeLayer(\'background\')"><img src="images/popUp/popupClose.png"></a>' +
			'</div>' +
			'<div id="popUpHeader" ' + style2 + '><h2>Get product updates to your email!</h2></div>' + 
			'<div id="popUpText"><p>Subscription is not compulsory. You can unsubscribe at any time.<br/>You can also subscribe later at Joiku.com main page.</p></div>' +		
			'<div id="popUpForm">' +
			'<form action="?action=products&amp;mode=download" method="post" onSubmit="showSuccesfulDownload2(' + productId + ')">' +
			'<input type="hidden" name="phonemodel" value="' + selectedPhone + '">' +
			'<input type="hidden" name="product_id" value="' + productId + '" />' +
			'<div style="margin-right: 5px; margin-bottom:5px;"><input type="radio" name="subscribe" value="1" onClick="change(1);" /></div>' +
			'<div style="width: 390px; margin-right: 5px; margin-bottom:5px;">Yes, I would like to receive product updates from Joiku to my email.</div>' +
			'<div style="margin-right: 5px; margin-bottom:5px;"><input type="radio" name="subscribe" value="0" onClick="change(0);" /></div>' +
			'<div style="width: 390px; margin-right: 5px; margin-bottom:15px;">No, thanks.</div>' +
			'<div style="width: 200px; margin-top: 20px;">' +
			'<input type="text"  id="popupEmail" name="email" value="[Your e-mail address]" onClick="showEmailHint(false);" disabled="true" />' +
			'</div>' +
			'<div style="width: 200px; margin-top: 20px;">' +
			'<input type="image" id="submitAndDownload" src="images/popUp/btnDownload.png" disabled="true" />' +
			'</div>' +
			'</form>' +
			'</div>' +
			'</div>'

			container.appendChild(details)

			document.body.appendChild(container)
	}
	else // Download directly
	{
		document.getElementById('download_form2').submit();
	}
}

function createLayer(target,del) {
	var newdiv = document.createElement('div');
    newdiv.setAttribute('id', 'layer');
      
    //newdiv.style.zIndex = '0';
    
	newdiv.style.width = '100%';
	newdiv.style.height = document.body.clientHeight + 'px';
	newdiv.style.background = "#000000";
	newdiv.style.position = 'absolute';
	newdiv.style.top = 0;
	newdiv.style.left = 0;
	newdiv.style.opacity = '0.85';
	newdiv.style.filter = 'alpha(opacity=85)';
	if(del)
		newdiv.setAttribute('onclick','closeLayer("'+target+'","'+del+'");');
	else
		newdiv.setAttribute('onclick','closeLayer("'+target+'");');
	document.body.appendChild(newdiv);

}

function closeLayer(id, del, caroussel) {
	var div = document.getElementById('layer');
	document.body.removeChild(div);
	div = document.getElementById(id);
	document.body.removeChild(div);
	
	if (del) {
		document.body.removeChild(document.getElementById(del));
	}
	if (document.getElementById('caroussel') && !caroussel)
		document.getElementById('caroussel').style.visibility = 'visible';
}

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function setCookie(name, value, expires)
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if(expires)
	{
		expires = expires * 1000;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" );
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}