/***************************************************************************************************
* Java script file which contains few generic functions and functions needed for quick links / edit
* and my prices / edit & Email subscription pages. 
* 
* @ Author 			Muhammad A. Noman
* @ Version			1.9
*
* @ Created			August 20, 2006
* @ Last modified	November 01, 2006
* 
* @ Changes
*
* September 28, 06 : 
* Added Email Service related functions (duplicate of my prices excep uses es_pref[_xxx] cookie values
*																				   
* November 01,06
* Email subscription related functions added
***************************************************************************************************/

//============================================= GLOBAL CONSTANTS ===================================

var MAX_QUICK_LINKS=5;
var MAX_MYPRICE_LINKS=10;

var DEFAULT_QL_LIST= "default_1,default_2,default_3,default_4,default_5";

var HTML_COMMA = '%2C';
var HTML_SPACE = '%20';
var HTML_COLON = '%2A';
var EMPTY = '';
var SPACE = ' ';
var COMMA = ',';
var COLON = ':';

//============================================= GENERIC FUNCTIONS ===================================

/**
Checks if a specfied value exist in the provided string.

@ assert		-Both parameters are not null
				-values are seperated using comma (,) and that all %2C has been replaced already.

@ param list	string containing values seperated via (,)
@ param str		string to be used for matching
@ param returns	true if match, false otherwise
*/
function isInArray(list_str, str)
{
	var list_arr=list_str.split(COMMA);
	
	
	for(var i=0; i<list_arr.length; i++)
	{	
		if(list_arr[i]==str)
				return true;
	}
	
	return false;
}

/**
Enumerates the form and sets the form checkboxes to selected state if their value 
appears in the list_str.

@ assert 	-assumes the form name is ql_form
			-replaces %2C with ,
			
@ param listing		string containing selected checkbox values
*/
function setCheckBoxes(list_str)
{
	// enumerate form selectd check boxes only
	for (var i=0; i < document.ql_form.elements.length; i++) 
		if(document.ql_form.elements[i].type == "checkbox" && isInArray(list_str,document.ql_form.elements[i].value))
			document.ql_form.elements[i].checked=true;
		else if(document.ql_form.elements[i].type == "checkbox")
			document.ql_form.elements[i].checked=false;
}


//--------------------------------------------- COOKIE ROUTINES -------------------------------------

/**
Creates or sets a cookie value. Expire (years) should be greater than 0.

@ param name	cookie name
@ param value	cookie value
@ param expires 	yearly
*/
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	value=value.replace(/%2C/gi,COMMA);
	value=value.replace(/%2A/gi,COLON);
	value=value.replace(/%20/gi,EMPTY);
	value=value.replace(/,,/gi,COMMA);
	
	while(value.charAt(0)==COMMA || value.charAt(0)==COLON || value.charAt(0)==SPACE )
		value=value.substring(1,value.length);
	while(value.charAt(value.length-1)==COMMA || value.charAt(value.length-1)==COLON || value.charAt(value.length-1)==SPACE )
		value=value.substring(0,value.length-2);
	
	
	if(value == EMPTY || value==COMMA || value==COLON || value==SPACE)
		value=SPACE;

	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
		expires = expires * 1000 * 60 * 60 * 24 * 365;
		
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	//( ( path ) ? ";path=" + path : "" ) + 
	//( ( domain ) ? ";domain=" + domain : "" ) +
	//******************************************************************************* CHANGE THIS FOR FINAL RELEASE *********
	( ";path=/web;" ) + 
	( "domain=www.ci.com;" ) +
	
	( ( secure ) ? ";secure" : "" );
}


/**
Reads cookie, return null if empty, return str otherwise

@ param name	cookie name
@ return null 	if cookie dosn't exist
*/
function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==SPACE) 
			c = c.substring(1,c.length);
			
		if (c.indexOf(nameEQ) == 0) 
				{
					var res=c.substring(nameEQ.length,c.length);
					
					res=res.replace(/%2C/g,COMMA);
					res=res.replace(/%2A/g,COLON);
					res=res.replace(/%20/g,EMPTY);
					res=res.replace(/,,/g,COMMA);
					
					return res;
				}
	}
	return null;
}

/** 
clears the cookie; You need to clear cache and refresh and it shd show cookie value as null (not "")

@param name 	cookie name
*/
function eraseCookie(name) 
{
	//createCookie(name,"",-1);
	Set_Cookie(name,"",-1);
}


/** 
Counts number of values that reside in cookie.  Splits cookie using %2C (,) and ignores
empty string "" and 'empty' array values.

@param cname 	cookie name
@return size	Returns number of elements in cookie after split. If cookie is null
				meaning (dosnt exist) or empty "" or 'empty' then returns 0
*/
function count_cookie_size( cname )
{
	var size=0;
	var mystrarr=new Array();

	var mystr=readCookie(cname);
	
	if(mystr == null || mystr==EMPTY || mystr==SPACE)
		size=0;
	else
		{
			mystrarr=mystr.split(COMMA);	// %2C == , (required here else won't work)
			size=mystrarr.length;
			if(mystrarr[0]==EMPTY || mystrarr[0]==SPACE)//|| mystrarr[0]=='empty')
				size--;
		}
	return size;
}


//------------------------- Quick-Links related functions -----------------------------------------

/**
initializes quicklinks page by retreving default or cookie values and sets the check boxes.
If cookie returns null (dosn't exist) then uses default list, else uses cookie values

@ assert	-uses the following hardcoded default list "default_1,default_2,default_3,default_4,default_5"
			-uses hardcoded cookie name 'ql_pref'
			-cookie expires in 50 yrs
*/
function init_quick_links()
{
//	var def_list= "default_1,default_2,default_3,default_4,default_5";
	var cc=readCookie('ql_pref');
	
	if(cc==null)
	{
		//-setCheckBoxes(DEFAULT_QL_LIST);
		//-Set_Cookie( 'ql_pref', DEFAULT_QL_LIST, '50', '/', '', '' );
	}
	else
	{
		setCheckBoxes(cc);
		Set_Cookie( 'ql_pref', cc, '50', '/', '', '' );
	}
}

/**
submits the page to save values and then redirects to home page.
(currently home_fla.jsp but should be set to home.jsp for final release)

@ assert 	-If cookie 'ql_pref' size ==0 then sets the cookie value to 'empty'
			-sets ql_sel value to 'redirect'

@return		true (for successful submission)
*/
function enumerate_form()
{
	document.ql_form.ql_sel.value="redirect";
	return true;
}

/**
function which is called on every onClick checkbox. It checks if the selected values
are in range MAX_QUICK_LINKS. If they are out of range, it alerts with a message. Else
it adds / removes the selected / deselected checkbox to the cookie 'ql_pref'

@ param cur_id	checkbo id	
*/
function check_ql_cb(cur_id, isEng)
{
	var isChecked=document.getElementById(cur_id).checked;
	
	// count all the other ones selected
	var ql_selected=0;
	ql_selected+=count_cookie_size("ql_pref");
	
	//-- check if limit reached
	if(isChecked && (ql_selected+1)>MAX_QUICK_LINKS )
	{
		(isEng) ? alert("You can choose a maximum of "+ (ql_selected) +" My Quick Links.") : alert("Vous pouvez choisir un maximum de "+ (ql_selected) +" « Mes liens rapides ».") ;
		document.getElementById(cur_id).checked=false;
		return;
	}

	//-- 
	var cur_cook=readCookie("ql_pref");
	if(cur_cook==null) cur_cook="";				//dont show default checkboxes on edit page
	if(cur_cook!= null)	// check if its not null
	{
		var cur_cook_arr=cur_cook.split(COMMA);
		var new_cook_arr=new Array();
		
		if(isChecked)
		{
			cur_cook_arr.push(cur_id);
			new_cook_arr=cur_cook_arr;
		}
		else
		{
			for(var k=0; k<cur_cook_arr.length; k++)
			{
				if(cur_cook_arr[k]!=cur_id)
					new_cook_arr.push(cur_cook_arr[k]);
			}
		}
			
		Set_Cookie( 'ql_pref', new_cook_arr.join(","), '50', '/', '', '' );
	}
}

/**
Clears all the selected checkboxes. It does it by saving empty cookie then submit page.
when the page loads, it resets the selection and nothing gets selected because of 
emtpy cookie value (ql_pref)

@ return true 	so page submits
*/
function clear_ql_selection()
{
	setCheckBoxes(SPACE);
	Set_Cookie( 'ql_pref', SPACE, '50', '/', '', '' );

	return false;
}

/**
Clears all the selected checkboxes. It does it by saving empty cookie then submit page.
when the page loads, it resets the selection and nothing gets selected because of 
emtpy cookie value (ql_pref)

@ return true 	so page submits
*/
function default_ql_selection()
{
/*	setCheckBoxes(DEFAULT_QL_LIST);
	Set_Cookie( 'ql_pref', DEFAULT_QL_LIST , '50', '/', '', '' );
*/
	setCheckBoxes(SPACE);//deselect everything
	eraseCookie("ql_pref");
	
	return false;
}

//------------------------- My Prices related functions -----------------------------------------

/**
initializes my prices page check boxes. sets/creates the cookie 'ql_pref' with the 
provided values (mpc_str). default_mp_selection() uses this function to set the values to default

@ assert	-If ntab != null means tab switch. 
			-Else assumes that its a new page load or first time page load.
			-Then it reads cookie value and sets the approprite checkboxes.
			-default_mp_selection() uses this function to set the values to default

@ param	mpc_str		cookie value or default listing
@ param ctab		current tab (MFX or SFX or MSX or AIX)
*/
function init_mp_page(mpc_str, ctab)
{
	if(document.ql_form.ntab.value != "null")
	{
		var tmp1=readCookie("mp_pref_"+document.ql_form.ntab.value);
		if(tmp1 != null)
			setCheckBoxes(tmp1)
		return;
	}
	
	if(readCookie("mp_pref")==null)
	mpc_str="";// for clearing default checkbxoes
	
	mp_MFX=new Array();
	mp_SFX=new Array();
	mp_MSX=new Array();
	mp_AIX=new Array();

	var mpc_arr=mpc_str.split(COMMA);
	
	for(var i=0; i<mpc_arr.length; i++)
	{
		var combo=(mpc_arr[i]).split(COLON);
		
		switch(combo[0]) 
		{
		  case 'MFX':   {mp_MFX.push(combo[1]); break}
		  case 'SFX':   {mp_SFX.push(combo[1]); break}
		  case 'MSX':   {mp_MSX.push(combo[1]); break}
		  case 'AIX':   {mp_AIX.push(combo[1]); break}
		  default:  ;         
	   }
	}

	
	Set_Cookie( 'mp_pref_MFX', mp_MFX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'mp_pref_SFX', mp_SFX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'mp_pref_MSX', mp_MSX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'mp_pref_AIX', mp_AIX.join(COMMA), '50', '/', '', '' );
	
	//-- set the check boxes
	switch(ctab) 
	{
	  case 'MFX':   {setCheckBoxes(mp_MFX.toString()); break}
	  case 'SFX':   {setCheckBoxes(mp_SFX.toString()); break}
	  case 'MSX':   {setCheckBoxes(mp_MSX.toString()); break}
	  case 'AIX':   {setCheckBoxes(mp_AIX.toString()); break}
	  default:  ;         
   }
   
}

//-- enumeration routine (my prices form)
/**
reads all the temporary session cookies (mp_pref_MFX && mp_pref_SFX && mp_pref_MSX && mp_pref_AIX)

@ assert 	-Replaces all the html encoded values to ,MFX: etc format
			-populate the cookie 'mp_pref' with the temporay session cookie values (selected tab check boxexs)
			-sets the ntab hidden form field value to 'redirect'

@ return true			
*/
function enumerate_mp_form(mytab)
{
	var mp_MFX_str=readCookie("mp_pref_MFX");
	var mp_SFX_str=readCookie("mp_pref_SFX");
	var mp_MSX_str=readCookie("mp_pref_MSX");
	var mp_AIX_str=readCookie("mp_pref_AIX");
	
	if(mp_MFX_str != null && mp_SFX_str!=null && mp_MSX_str!=null && mp_AIX_str!=null) // CHECK if its cuz of default button
	{
		var all_mp=EMPTY;
		
		if(mp_MFX_str!=null && mp_MFX_str!=EMPTY)	all_mp+="MFX:"+mp_MFX_str.replace(/,/g,",MFX:");
		if(mp_SFX_str!=null && mp_SFX_str!=EMPTY)	all_mp+=",SFX:"+mp_SFX_str.replace(/,/g,",SFX:");
		if(mp_MSX_str!=null && mp_MSX_str!=EMPTY)	all_mp+=",MSX:"+mp_MSX_str.replace(/,/g,",MSX:");
		if(mp_AIX_str!=null && mp_AIX_str!=EMPTY)	all_mp+=",AIX:"+mp_AIX_str.replace(/,/g,",AIX:");
		
		Set_Cookie( 'mp_pref', all_mp, '50', '/', '', '' );
	}
	document.ql_form.ntab.value="redirect";
	return true;
}


/**
function which is called on every onClick checkbox. It checks if the selected values
are in range MAX_MYPRICE_LINKS. If they are out of range, it alerts with a message. Else
it adds / removes the selected / deselected checkbox to the cookie value present in ctab
hidden form field

@ assert	-ctab contains the current tab value (MFX or SFX or MSX or AIX)
			-replaces all html %2C and %2A HTML codes with , and :

@ param cur_id	checkbo id	
*/
function check_mp_cb(cur_id, isEng)
{
	var isChecked=document.getElementById(cur_id).checked;
	
	// count all the other ones selected
	var other_selected=0;
	
	other_selected+=count_cookie_size("mp_pref_MFX");
	other_selected+=count_cookie_size("mp_pref_SFX");
	other_selected+=count_cookie_size("mp_pref_MSX");
	other_selected+=count_cookie_size("mp_pref_AIX");
	
	//-- check if limit reached
	if(isChecked && (other_selected+1)>MAX_MYPRICE_LINKS )
	{
		(isEng) ? alert("You can choose a maximum of "+ (other_selected) +" My Prices.") : alert("Vous pouvez choisir un maximum de "+ (other_selected) +" « Mes Prix ».");
		document.getElementById(cur_id).checked=false;
		return;
	}

	//-- 
	var cur_cook=readCookie("mp_pref_"+document.ql_form.ctab.value);
	if(cur_cook==null) cur_cook="";				//dont show default checkboxes on edit page
	
	if(cur_cook==null)
		cur_cook=EMPTY;
	
	cur_cook=cur_cook.replace(/MFX:/g, EMPTY);
	cur_cook=cur_cook.replace(/SFX:/g, EMPTY);
	cur_cook=cur_cook.replace(/MSX:/g, EMPTY);	
	cur_cook=cur_cook.replace(/AIX:/g, EMPTY);	
	
	var cur_cook_arr=cur_cook.split(COMMA);
	var new_cook_arr=new Array();

	if(isChecked)
		{
			cur_cook_arr.push(cur_id);
			new_cook_arr=cur_cook_arr;
		}
	else
		{
			for(var k=0; k<cur_cook_arr.length; k++)
			{
				if(cur_cook_arr[k]!=cur_id)
					new_cook_arr.push(cur_cook_arr[k]);
			}
		}

	Set_Cookie( 'mp_pref_'+document.ql_form.ctab.value,new_cook_arr.join(COMMA), '50', '/', '', '' );
}

/**
is call when a tab is clicked

@ param cur_tab		current tab value
@ param new_tab		new tab value 
@ return 			return false if both tab values r same o
*/
function set_tab(cur_tab, new_tab)
{
	
	if(cur_tab == new_tab)
		return false;
	else
	{
		document.ql_form.ntab.value=new_tab;
		document.ql_form.submit();
		return true;
	}
}

/**
Clears all the selected checkboxes. It does it by saving empty cookie then submit page.
when the page loads, it resets the selection and nothing gets selected because of 
emtpy cookie value (mp_pref)

@ return true 	so page submits
*/
function clear_mp_selection()
{
	Set_Cookie( 'mp_pref_MFX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'mp_pref_SFX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'mp_pref_MSX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'mp_pref_AIX', SPACE, '50', '/', '', '' );

	setCheckBoxes(SPACE);
	return false;
}

/**
Clears all the selected checkboxes. It does it by saving empty cookie then submit page.
when the page loads, it resets the selection and nothing gets selected because of 
emtpy cookie value (mp_pref)

@ assert	-uses default_mp_selection()
@ return true 	so page submits
*/
function default_mp_selection(mpc_str, ctab)
{
/*	document.ql_form.ntab.value="null";
	init_mp_page(mpc_str, ctab);
*/
	setCheckBoxes(SPACE);//deselect everything
	eraseCookie("mp_pref");
	eraseCookie("mp_pref_MFX");
	eraseCookie("mp_pref_SFX");
	eraseCookie("mp_pref_MSX");
	eraseCookie("mp_pref_AIX");

	return false;
}

//------------------------------------------------- END -----------------------------------------
//------------------------- Email Service related functions -----------------------------------------

/**
initializes email subscription page check boxes. initializes subsopt box. sets/creates the cookies 
'es_pref_xxx' with the provided values (mpc_str). 

@ assert	-If ntab != null means tab switch. 
			-Else assumes that its a new page load or first time page load.
			-Then it reads cookie value and sets the approprite checkboxes.

@ param	mpc_str		cookie value or default listing
@ param ctab		current tab (MFX or SFX or MSX or AIX)
@ param isEng,isTxt,freq,es_alert,es_email 	subopt box options
*/
function init_es_page(mpc_str, ctab, isEng, isTxt,freq, es_alert,es_email)
{
	
	
	if(document.ql_form.ntab.value != "null")
	{
		var tmp1=readCookie("es_pref_"+document.ql_form.ntab.value);
		if(tmp1 != null)
			{
				//-- only init , dont save
				init_es_subsbox(isEng, isTxt, freq, es_alert, es_email);
				setCheckBoxes(tmp1);
				
			}
		return;
	}
	
	
	mp_MFX=new Array();
	mp_SFX=new Array();
	mp_MSX=new Array();
	mp_AIX=new Array();

	var mpc_arr=mpc_str.split(COMMA);
	
	for(var i=0; i<mpc_arr.length; i++)
	{
		var combo=(mpc_arr[i]).split(COLON);
		
		switch(combo[0]) 
		{
		  case 'MFX':   {mp_MFX.push(combo[1]); break}
		  case 'SFX':   {mp_SFX.push(combo[1]); break}
		  case 'MSX':   {mp_MSX.push(combo[1]); break}
		  case 'AIX':   {mp_AIX.push(combo[1]); break}
		  default:  ;         
	   }
	}

	
	Set_Cookie( 'es_pref_MFX', mp_MFX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'es_pref_SFX', mp_SFX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'es_pref_MSX', mp_MSX.join(COMMA), '50', '/', '', '' );
	Set_Cookie( 'es_pref_AIX', mp_AIX.join(COMMA), '50', '/', '', '' );


	//-- set the check boxes
	switch(ctab) 
	{
	  case 'MFX':   {setCheckBoxes(mp_MFX.toString()); break}
	  case 'SFX':   {setCheckBoxes(mp_SFX.toString()); break}
	  case 'MSX':   {setCheckBoxes(mp_MSX.toString()); break}
	  case 'AIX':   {setCheckBoxes(mp_AIX.toString()); break}
	  default:  ;         
   }

	//alert(mpc_str);
	//-- save opts and init
	save_es_subs(document.ql_form.sid.value, isEng, isTxt, freq, es_alert, es_email );
	init_es_subsbox(isEng, isTxt, freq, es_alert, es_email);
	//--
}


/**
is call when a email subscription tab is clicked

@ param cur_tab		current tab value
@ param new_tab		new tab value 
@ return 			return false if both tab values r same o
*/
function set_es_tab(cur_tab, new_tab)
{
	if(cur_tab == new_tab)
		return false;
	else
	{
		//-- save sub opt
		var es_sid,isEng,isTxt,freq,alrt,es_email;
		
		(document.ql_form.chbLanguage[0].checked==true) ? isEng=true : isEng=false;
		(document.ql_form.chbEmailFormat[0].checked==true) ? isTxt=true : isTxt=false;
		
		if(document.ql_form.rbPriceFreq[0].checked==true)
			freq='D';
		else 	if(document.ql_form.rbPriceFreq[1].checked==true)
			freq='W';
		else 	if(document.ql_form.rbPriceFreq[2].checked==true)
			freq='M';
		else 	if(document.ql_form.rbPriceFreq[3].checked==true)
			freq='A';
		else
			freq='D';
			
		alrt=document.ql_form.priceAlertPct.value;
		es_email=document.ql_form.email.value;
		es_sid=document.ql_form.sid.value;
		
		//-- save sub opt on switch tab	
		save_es_subs(es_sid,isEng,isTxt,freq,alrt,es_email);
		
		document.ql_form.ntab.value=new_tab;
		document.ql_form.submit();
		return true;
	}
}


//-- enumeration routine (my prices form)
/**
reads all the temporary session cookies (es_pref_MFX && es_pref_SFX && es_pref_MSX && es_pref_AIX)

@ assert 	-Replaces all the html encoded values to ,MFX: etc format
			-populate the cookie 'es_pref' with the temporay cookie values (selected tab check boxexs)
			-sets the ntab hidden form field value to 'redirect'

@ return true			
*/
function enumerate_es_form(mytab)
{	

	//-- check if new subs and no seleciton made
	if( count_cookie_size('es_pref_MFX')==0 && count_cookie_size('es_pref_SFX')==0 && count_cookie_size('es_pref_MSX')==0 && count_cookie_size('es_pref_AIX')==0 )
		return false;


	//-- save fund choosen
	var mp_MFX_str=readCookie("es_pref_MFX");
	var mp_SFX_str=readCookie("es_pref_SFX");
	var mp_MSX_str=readCookie("es_pref_MSX");
	var mp_AIX_str=readCookie("es_pref_AIX");
		

	var all_mp=EMPTY;
	
	if(mp_MFX_str!=null && mp_MFX_str!=EMPTY)	all_mp+="MFX:"+mp_MFX_str.replace(/,/g,",MFX:");
	if(mp_SFX_str!=null && mp_SFX_str!=EMPTY)	all_mp+=",SFX:"+mp_SFX_str.replace(/,/g,",SFX:");
	if(mp_MSX_str!=null && mp_MSX_str!=EMPTY)	all_mp+=",MSX:"+mp_MSX_str.replace(/,/g,",MSX:");
	if(mp_AIX_str!=null && mp_AIX_str!=EMPTY)	all_mp+=",AIX:"+mp_AIX_str.replace(/,/g,",AIX:");

	Set_Cookie( 'es_pref', all_mp, '50', '/', '', '' );	// for es do save in db atm
	//-- save sub opt
	var es_sid,isEng,isTxt,freq,alrt,es_email;
	
	(document.ql_form.chbLanguage[0].checked==true) ? isEng=true : isEng=false;
	(document.ql_form.chbEmailFormat[0].checked==true) ? isTxt=true : isTxt=false;
	
	if(document.ql_form.rbPriceFreq[0].checked==true)
		freq='D';
	else 	if(document.ql_form.rbPriceFreq[1].checked==true)
		freq='W';
	else 	if(document.ql_form.rbPriceFreq[2].checked==true)
		freq='M';
	else 	if(document.ql_form.rbPriceFreq[3].checked==true)
		freq='A';
	else
		freq='D';
		
	alrt=document.ql_form.priceAlertPct.value;
	es_email=document.ql_form.email.value;
	es_sid=document.ql_form.sid.value;
	
	//-- save sub opt on switch tab	
	save_es_subs(es_sid,isEng,isTxt,freq,alrt,es_email);
	return true;
}


/**
function which is called on every onClick checkbox. It checks if the selected values
are in range MAX_MYPRICE_LINKS. If they are out of range, it alerts with a message. Else
it adds / removes the selected / deselected checkbox to the cookie value present in ctab
hidden form field

@ assert	-ctab contains the current tab value (MFX or SFX or MSX or AIX)
			-replaces all html %2C and %2A HTML codes with , and :

@ param cur_id	checkbo id	
*/
function check_es_cb(cur_id)
{
	
	var isChecked=document.getElementById(cur_id).checked;
	
	//-- 
	var cur_cook=readCookie("es_pref_"+document.ql_form.ctab.value);
	if(cur_cook==null)
		cur_cook=EMPTY;
	
	cur_cook=cur_cook.replace(/MFX:/g, EMPTY);
	cur_cook=cur_cook.replace(/SFX:/g, EMPTY);
	cur_cook=cur_cook.replace(/MSX:/g, EMPTY);	
	cur_cook=cur_cook.replace(/AIX:/g, EMPTY);	
	
	var cur_cook_arr=cur_cook.split(COMMA);
	var new_cook_arr=new Array();

	if(isChecked)
		{
			cur_cook_arr.push(cur_id);
			new_cook_arr=cur_cook_arr;
		}
	else
		{
			for(var k=0; k<cur_cook_arr.length; k++)
			{
				if(cur_cook_arr[k]!=cur_id)
					new_cook_arr.push(cur_cook_arr[k]);
			}
		}

	Set_Cookie( 'es_pref_'+document.ql_form.ctab.value,new_cook_arr.join(COMMA), '50', '/', '', '' );
}

/**
function to clear all check boxes from email subs form
*/
function clear_es_selection()
{
	Set_Cookie( 'es_pref_MFX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'es_pref_SFX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'es_pref_MSX', SPACE, '50', '/', '', '' );
	Set_Cookie( 'es_pref_AIX', SPACE, '50', '/', '', '' );

	setCheckBoxes(SPACE);
	return false;
}


/** 
function to save email subscription options box
*/
function save_es_subs(es_sid, isEng, isTxt, freq, alrt, es_email )
{
	var subs_opts=es_sid+","+isEng+","+isTxt+","+freq+","+alrt+","+es_email;
	Set_Cookie( 'es_subsopt', subs_opts, '50', '/', '', '' );
	
}
/**
function to initialize subscription option box
*/
function init_es_subsbox(isEng, isTxt, freq, alrt, es_email )
{
	
	var cur_cook=readCookie("es_subsopt");
	if(cur_cook==null)
	{
		cur_cook="0,"+isEng+","+isTxt+","+freq+","+alrt+","+es_email;
	}
	
	var subsopt_arr=cur_cook.split(COMMA);
	//-- [0] is sid
	isEng=subsopt_arr[1];
	isTxt=subsopt_arr[2];
	freq=subsopt_arr[3];
	alrt=subsopt_arr[4];
	es_email=subsopt_arr[5];
	
	//-- set option box	
	isEng=="true" ?  document.ql_form.chbLanguage[0].checked=true : document.ql_form.chbLanguage[1].checked=true;
	isTxt=="true" ?  document.ql_form.chbEmailFormat[0].checked=true : document.ql_form.chbEmailFormat[1].checked=true;
	
	switch(freq)
	{
		case 'D': document.ql_form.rbPriceFreq[0].checked=true;break;
		case 'W': document.ql_form.rbPriceFreq[1].checked=true;break;
		case 'M': document.ql_form.rbPriceFreq[2].checked=true;break;
		case 'A': document.ql_form.rbPriceFreq[3].checked=true; break;
		
	}

	document.ql_form.priceAlertPct.value=alrt;
	document.ql_form.email.value=es_email;
	
}


/**
function to set action property for the current page before submission
*/
function set_es_nextPage(newpage)
{
	document.ql_form.action=newpage;
}

//------------------------------------------------- END -----------------------------------------
