﻿function setInfo(formData){

	var info = new Array();
	var expiration = new Date();

	//Get form data.
	info["name"] = formData.name.value;
	info["email"] = formData.from.value;
	info["ship1"] = formData.ship1.value;
	info["city"] = formData.city.value;
	info["state"] = formData.state.value;
	info["zip"] = formData.zip.value;
	info["country"] = formData.country.value;

	//Set the expiration date of the cookie one week from now.
	expiration.setDate(expiration.getDate() + 7); 

	//Set the cookie!
	document.cookie = "name=" + escape(info["name"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	document.cookie = "email=" + escape(info["email"]) + ";expires=" 
					  + expiration.toGMTString + ";domain=niteynitesleepaid.com";

	document.cookie = "ship1=" + escape(info["ship1"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	document.cookie = "city=" + escape(info["city"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	document.cookie = "state=" + escape(info["state"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	document.cookie = "zip=" + escape(info["zip"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	document.cookie = "country=" + escape(info["country"]) + ";expires=" 
					  + expiration.toGMTString() + ";domain=niteynitesleepaid.com";

	return true;

}

//Check and retrieve the cookie info.
function checkCookie()

{
	//Unescape the cookie for easy reading.
	var cookieInfo = unescape(document.cookie);
	var info = new Array();

	if (cookieInfo.length == 0) return info;

	//Separate the cookie list name name=value;
	var cookieList = cookieInfo.split(';');

	//Split cookie data for shipping info.
	for ( i = 0; i < cookieList.length; i++) {
		var nameValue = cookieList[i].split('=');
		if (nameValue[1] != undefined){
			info[i] = nameValue[1];	
		}else{
			info[i] = "";
		}
	}

	

	//Split the name into first and last name (Limitation within Aweber for name data
	//We are only looking for the first and last fields entered.
	var name = info[0].split(" ");

	info.splice(0, 1, name[0]);

	//If the name is more than one word, get the first and last bits.
	if (name.length > 1){
		info.splice(1, 0, name[name.length - 1]);
	}else{
		info.splice(1, 0, "");
	}

	return info;
}


function redirect()

{
	//Change this to whatever thank you page you would like.
	var url = "http://secure.niteynitesleepaid.com/";

	//Get info from the cookie.
	var info = checkCookie();



	if (document.cookie.length == 0){
		window.location = url;
	}else{
		//Send them to Aweber, then to the url above.
		window.location = "http://www.aweber.com/scripts/addlead.pl?meta_web_form_id=1918061773&unit=niteyniteorders&redirect=" + url + "&name=" 
			+ info[0] + " " + info[1] + "&from=" + info[2];
	}

}



