// Cookie routine for download documents
// we create a new user login by setting a cookie for 4 months and with their email


function download_file(obj)
{
	username=getCookie('username');
	if (username!=null && username!="")
	{
		// User exists, just go to download, but log filename and ip in cookie
		createFileCookie('filename',escape(obj));
		window.location = "php/get_pdf_script.php";
	}
	else
	{
		// User do not exist, show the login question and create a cookie for the selected file!
		createFileCookie('filename',escape(obj));
		jQuery.slimbox("setup_images/login_banner.gif", "<iframe frameborder='0' width='100%' height='170' src='cookie_login_code.php'>");	
	}
}


function register_email_limited()
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var adr = document.form1.emailbox.value;
	
	if (adr == "info@hatteland-display.com")
	{
		alert('"Sorry. That e-mail address is protected!');
		return;
	}
	
    if(reg.test(adr) == false)
	{
		obj=getCookie('filename');
		alert('The e-mail address you entered seems not to be valid!');
	}		
    else // email address is valid, now store cookie and log it
	{
		createUserCookie('username',escape(document.form1.emailbox.value)); 
		window.location = "php/get_pdf_script.php";
	}
}



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));
    }
  }
}



function createFileCookie(name,value) 
{
	var date = new Date();
	date.setTime(date.getTime()+360000);  // file cookie only active for approx 5 minutes
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}


function createUserCookie(name,value) 
{
	var date = new Date();
	date.setTime(date.getTime()+(240*24*60*60*1000)); // 240 is days = 8months
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}




// Other common routines

function linker()
{
}


function go_url(url)
{
top.location.href = url;
}


function go_top()
{
top.location.href = "http://www.hatteland-display.com";
}
