//Copyright 2008 BGT Partners (www.bgtpartners.com)
//v1

function createCookie (name) {

	f = document.forms["registration"];
	
	var SRE054_Vals = ""

	for (var x=0; x<registration.SRE054.length; x++){
		if (registration.SRE054[x].checked == true){
			SRE054_Vals = SRE054_Vals + "," + registration.SRE054[x].value;
		}
		
	}
	
	SRE054_Vals = SRE054_Vals.substring(1, SRE054_Vals.length);
	
	//if (e["sre054"])	{e["sre054"].value = SRE054_Vals; }

	//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;
	//console.debug("e form elements", e);
	var cookieExists = false;
	var cookie_text = unescape(readCookie("calauserdata"));
	if ( cookie_text == false || cookie_text == "undefined" || cookie_text == "false" ) { cookie_text = ""; }
	else { cookieExists = true; }

		
	for(i=0; i<e.length; i++){
		
		try {
			
			if( e[i].name && e[i].name.length > 0 && e[i].value) {
				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) { 
							if (e[i].name == "SRE054"){
								cookie_text = updateCookie(e[i].name,SRE054_Vals,cookie_text); 
							}
							else{
							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) { 
							if (e[i].name == "SRE054"){
								cookie_text = updateCookie(e[i].name,SRE054_Vals,cookie_text); 
							}
							else{
							cookie_text = cookie_text + e[i].name + "&" + e[i].value + "|"; }
						}
					}
					else { cookie_text = cookie_text + e[i].name + "&" + e[i].value + "|"; }
				}
		
			}
			
		}
		catch(e) { }
		
	}

	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 = 	"calauserdata=" + 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 + "=1" +
						"; expires=" + newDate +
						"; 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;
}
