//Copyright 2005 BGT Partners (www.bgtpartners.com)
//v1.2
//BF 05.10.06 fix for update on checkbox and radio 

function createCookie (name) {

	f = document.forms["registration"];

	//Set cookie functional variables
	var path = "/";
	var domain = "avaya.com";
	var expiration_date = new Date("January 1, 2099");
	expiration_date = expiration_date.toGMTString();

	//Set cookie form variables
	var e = f.elements;
	var cookieExists = false;
	var cookie_text = unescape(readCookie("userdata"));
	if ( cookie_text == false || cookie_text == "undefined" || cookie_text == "false" ) { cookie_text = ""; }
	else { cookieExists = true; }

	for(i=0; i<e.length; i++){
		e[i].name 	= e[i].name.replace("|","-");
		e[i].name	= e[i].name.replace("&","^");
		e[i].value	= e[i].value.replace("|","-");
		e[i].value	= e[i].value.replace("&","^");

		if ( cookieExists ) {
			
			if (e[i].type == "checkbox" || e[i].type == "radio") //BF 05.10.06 need to check for check box or radio and CHECKED
			{ 
				if (e[i].checked == true) { cookie_text = updateCookie(e[i].name,e[i].value,cookie_text); }
			}
			else { cookie_text = updateCookie(e[i].name,e[i].value,cookie_text); } //not radio or checkbox
			
		}
		else {  /*  Append/create if not found  */
			//checkbox and radio button fields
			if (e[i].type == "checkbox" || e[i].type == "radio")
			{
				if (e[i].checked == true) { cookie_text = cookie_text + e[i].name + "&" + e[i].value + "|"; }
								
			}
			else { cookie_text = cookie_text + e[i].name + "&" + e[i].value + "|"; }
		}
	}

	if (!cookieExists) { //Added to accommodate premium content, only added on creation of cookie, if it doesn't exist
		cookie_text = cookie_text.substring(0,cookie_text.length-1)+"|url&|urlcode&";
	}
	document.cookie = 	"userdata=" + escape(cookie_text) +
						"; expires=" + expiration_date +
						"; path=" + path +
						"; domain=" + domain;

	//Add 30 days and write
	var today = new Date();
	var dExp = Date.parse(today);
	dExp += (30 * 24 * 3600000); //3600000 = hours, 60000 = minutes, * 1000 = seconds
	var newDate = new Date(dExp);
	document.cookie = 	name + "_exp=" + newDate +
						"; expires=" + expiration_date +
						"; path=" + path +
						"; domain=" + domain;

	document.cookie = 	name + "=1" +
						"; expires=" + expiration_date +
						"; path=" + path +
						"; domain=" + domain;

}

function updateCookie (fn,fv,cookie_text) {

	var ph1;
	var ph2=0;
	ph1 = cookie_text.indexOf(fn + "&");

	if (ph1 != -1) { /* If the fieldname is found in the cookie string... */
		for (var i=ph1;i<cookie_text.length;i++) { /* Find the location of the end of the  */
			if (cookie_text.substring(i,i+1) == "|"  || i==cookie_text.length ) 
			{ ph2 = i; break; }
		}
		ph = cookie_text.substring(ph1,ph2);
		cookie_text = cookie_text.replace(ph, fn + "&" + fv);
	}
	else {
		cookie_text = fn + "&" + fv + "|" + cookie_text;
	}
	return cookie_text;
}

function readCookie(name) {

    var CookieString = document.cookie;
    var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces;
    var ReturnValue = "";
    var x = 0;

	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
		CookiePieces = CookieSet[x].split ('=');
		if (CookiePieces[0].substring (0,1) == ' ')
			{ CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length); }
		if (CookiePieces[0] == name)
			{ ReturnValue = CookiePieces[1]; }
	}
	if (ReturnValue == "") { ReturnValue = false; }

	return ReturnValue;
}
