// Global download variable
var gDownloadFile;
var gbDetailsValidated = new Boolean(false);

///////
// Function: SetCookie
// Params: (String)Name, (String)Value, (Time)Expire
// Return: NONE
// Description: Sets a cookie on the client's browser
///////
function SetCookie(name, value, expire) 
{
	// document.cookie = name + "=" + escape(value) +  ((expire == null) ? "" : ("; expires=" + expire.toGMTString())) + "; domain=palmaresconsulting.com; path=/";
	document.cookie = name + "=" + escape(value) +  ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

///////
// Function: GetCookie
// Params: (String)Name
// Return: (String)value (null if cookie not set)
// Description: Gets a cookie from the client's browser
///////
function GetCookie(Name) 
{   
	var search = Name + "=";
	if (document.cookie.length > 0) { 
		// if there are any cookies
		offset = document.cookie.indexOf(search);
		
		if (offset != -1) { 
			// if cookie exists
			offset += search.length;
			
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
				
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

///////
// Function: Register
// Params: (String)firstname, (String)lastname, (String)email
// Return: NONE
// Description: Registers the user by setting a "palmaresconsultingu" cookie on their browser
///////
function Register(regform)
{
	// var regform = document.regform;

	var today = new Date();
	var expires = new Date()
	// valid for 1 year
	expires.setTime(today.getTime() + 24*365*3600000)

	SetCookie("palmaresconsultinginfo", "first:" + regform.firstname.value + ",last:" + regform.lastname.value + ",email:" + regform.email.value, expires);
}

///////
// Function: CheckRegistration
// Params: NONE
// Return: (String)user (null if not registered)
// Description: Check's whether a user has registered.
///////
function CheckRegistration()
{
	var name;

	name = GetCookie("palmaresconsultinginfo");

	return name;
}

function DoRegistrationWindow()
{
	var registered = 0;

	var regwnd = window.open ('', 'regwnd', 'resizable=yes,scrollbars=yes,status=yes,width=800,height=600');
	regwnd.location.href = "register.html";
	regwnd.opener = self;
}

function DoRegisteredWindow()
{
	var regdwnd = window.open ('', 'regdwnd', 'resizable=yes,scrollbars=yes,status=yes,width=300,height=300');
	regdwnd.location.href = "registered.html";
	regdwnd.opener = self.opener;
}

function DoDownload(dnload) 
{
	var filename = "";

	switch(dnload) {
	case 'adcs_en':
		filename = "palmaresldpen.pdf";
		break;

	case 'autopost_en':
		filename = "palmaresautoposten.pdf";
		break;

	case 'autopost_de':
		filename = "palmaresautopostde.pdf";
		break;

	case 'consult_en':
		filename = "palmaresprojectconsultingen.pdf";
		break;

	case 'invoice_en':
		filename = "palmaresinvoiceen.pdf";
		break;

	case 'forms_en':
		filename = "palmaresformsen.pdf";
		break;

	case 'ums_en':
		filename = "palmaresunifiedmessagingen.pdf";
		break;

	case 'ums_de':
		filename = "palmaresunifiedmessagingde.pdf";
		break;
	}

	location.href = "downloads/" + filename;
}

///////
// Function: Download
// Params: (String)dnload
// Return: NONE
// Description: Handles the user's attempt to download a file from the website
///////
function Download(dnload)
{
	gDownloadFile = dnload;

	// Firstly, are we registered?
	var username = CheckRegistration();
	if (username == null) {
		
		// NO!
		registered = DoRegistrationWindow(dnload);

	} else {

		DoDownload(dnload);

	}
}	

function ValidateRegistration(regform)
{
	var bValid = new Boolean(true);
	var sError = "The following error(s) have been detected:\n\n";
	var sTag = "";
	var itemfocus = null;

	if (regform.firstname.value == "") {
		sError = sError + "*  You have not supplied your First Name\n";
		bValid = false;
		if (sTag == "")
			sTag = "#USER";
		if (itemfocus == null)
			itemfocus = regform.firstname;
	}

	if (regform.lastname.value == "") {
		sError = sError + "*  You have not supplied your Last Name\n";
		bValid = false;
		if (sTag == "")
			sTag = "#USER";
		if (itemfocus == null)
			itemfocus = regform.lastname;
	}

	if (regform.company.value == "") {
		sError = sError + "*  You have not supplied your Company's Name\n";
		bValid = false;
		if (sTag == "")
			sTag = "#COMPANY";
		if (itemfocus == null)
			itemfocus = regform.company;
	}

	if (regform.email.value == "") {
		sError = sError + "*  You have not supplied your email address\n";
		bValid = false;
		if (sTag == "")
			sTag = "#CONTACT";
		if (itemfocus == null)
			itemfocus = regform.email;
	} else {
		// Validate email address
		var sEmail = regform.email.value;
		var bEmailValid = new Boolean(true);

		if (sEmail.indexOf(" ") != -1) 
			bEmailValid = false;
		
		if (sEmail.indexOf("@") < 1) 
			bEmailValid = false;

		if (sEmail.indexOf("@") == sEmail.length - 1) 
			bEmailValid = false;

		if (sEmail.indexOf(".") == -1) 
			bEmailValid = false;

		if (bEmailValid == false) {
			sError = sError + "*  The email address you supplied is not valid\n";
			bValid = false;
			if (sTag == "")
				sTag = "#CONTACT";
			if (itemfocus == null)
				itemfocus = regform.email;
		}
	}

	gbDetailsValidated = bValid;

	if (bValid == false) {
		sError = sError + "\n\nPlease correct the errors before proceeding.";
		window.alert(sError);
		if (sTag != "")
			location.href=sTag;
		if (itemfocus != null) {
			itemfocus.focus();
			itemfocus.select();
		}

	}

	return bValid;
}


