// JavaScript Document

	// a basic function which means i dont have to type the whole
	// 'document.getElementById' line everytime :)
	function getbyid(id)
	{
		return document.getElementById(id);
	}
	
	function setCookie(name, val)
	{
		var date = new Date(2982, 03, 27, 9, 0, 0, 0);
		document.cookie = name+"="+val+";expires="+date.toGMTString();
	}
	
	function getCookie(name)
	{
		var results = document.cookie.match (" " + name + '=(.*?)(;|$)');
	
		if (results)
		{
			return (unescape(results[1]));
		}
		else
		{
			results = document.cookie.match (name + '=(.*?)(;|$)');
	
			if (results)
				return (unescape(results[1]));
			else
				return null;
		}
	}
	
	// checks if a field is filled in
	function filledIn(fname)
	{
		var val = getbyid(fname).value;
		if(val == null)
			return false;
		else if(trim(val) == "")
			return false
		else
			return true;
	}
	
	// trim whitespace from a string
	function trim(str)
	{
		try
		{
			return str.replace(/^\s*|\s*$/g,"");
		}
		catch(e)
		{
			return "";
		}
	}
	
	function getFile(href, mandatory)
	{
		try
		{
			var h = href.toString();
			h = h.substring(h.indexOf("downloads"), h.length).replace("/", "\\");
			setCookie("href", h);
			setCookie("mandatory", mandatory);
			var win = window.open("getFile.php");
			win.focus();
		}
		catch(e){alert(e.message);}
		return false;
	}
	
	function getRestrictedFile(href)
	{
		try
		{
			// work out if this is an IP or PP download
			var product = "IP";
			var index = href.toString().indexOf("imageprofile");
			if(index == -1)
				product = "PP";
			setCookie("product", product);
			
			// build href
			var h = href.toString();
			h = h.substring(h.indexOf("clientresources"), h.length).replace("/", "\\");
			setCookie("href", h);
			
			// open window
			var win = window.open("getRestrictedFile.php");
			win.focus();
		}
		catch(e){}
		return false;
	}
	
	function saveContactInfo()
	{
		// these fields are on both pages
		setCookie("name", getbyid("tbName").value);
		setCookie("company", getbyid("tbCompany").value);
		setCookie("email", getbyid("tbEmail").value);
		setCookie("where", getbyid("tbWhere").value);
		setCookie("news", getbyid("lstNews").value);
		setCookie("contact", getbyid("lstContact").value);
		
		try
		{
			// these fields are only on the mandatory page
			setCookie("position", getbyid("tbPosition").value);
			setCookie("phone", getbyid("tbPhone").value);
		}
		catch(e){}
	}
	
	function clearCookies()
	{
		document.cookie = "name=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "company=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "position=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "email=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "phone=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "where=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "news=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "contact=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		document.cookie = "pp_password=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "ip_password=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	
	