/*
//=======================================================================
//							time_entry_cookie.js 
//=======================================================================
//Functions used in Time entry Webview/105TimeEntry/Runtime
//
//function start_load(cookieName)
//function start_unload(cookieName)
//function final()
//function  populate_cookie(cookieName)
//function  delete_cookie(cookieName)
//function  populate_form_fields(cookieName)
//function getCookie(name) 
//function parseCookie(cookie)
//function setFields(name,value)
//update_individual_field(CookieName,FieldName)
//
*/


var debug = false;
var _finished='false'; 
//var cookieName=document.Form.Cookie_Name;

//---------------------------------------------------------------------------------

function start_load(cookieName)
{
	//alert('load');
	
	switch (cookieName){
		case 'PreCost' :
			populate_form_fields(cookieName);			
			break
		case 'Retainer' :
			if(document.clientside.delete_cookie.value!='Y')
			{
			populate_form_fields(cookieName);
			}
			break
		case 'Divide' :
			populate_form_fields(cookieName);						
			break
		case 'Overrides' :
			populate_form_fields(cookieName);						
			break
		case 'ReportOptions' :
			populate_form_fields(cookieName);						
			break
		default :
		//if a fromlist hidden field is populated then a timecard index has been passed in so  don't populate from the cookie
		//window.alert("start_load"+cookieName );		
		if((document.Form.fromlist.value == 'Y') && (document.Form.screenchange.value != 'Y'))
		{
		//alert('delete_cookie');
		
		}
		else
		{
		if(document.clientside.delete_cookie.value!='Y')
			{
			populate_form_fields(cookieName);
			}
		else

			{
				if(document.Form.dt_timecard_tworkdt1)
				{
				update_individual_field('TimeEntry' , 'dt_timecard_tworkdt1')
				
				}
				if(document.Form.dt_costcard_date)
				{
				update_individual_field('Expense' , 'dt_costcard_date')
				//delete_cookie('Expense');
				}
			}
		}


	}
	
	
	/*
	if(cookieName!='PreCost' && cookieName!='Retainer' && cookieName!='Overrides' && cookieName!='ReportOptions')
	{
		//if a fromlist hidden field is populated then a timecard index has been passed in so  don't populate from the cookie
		//window.alert("start_load"+cookieName );		
		if((document.Form.fromlist.value == 'Y') && (document.Form.screenchange.value != 'Y'))
		{
		
		}
		else
		{
		if(document.clientside.delete_cookie.value!='Y')
			{
				populate_form_fields(cookieName);
			}
		else
			{
				if(document.Form.dt_timecard_tworkdt1)
				{
					update_individual_field('TimeEntry' , 'dt_timecard_tworkdt1')
				}
				if(document.Form.dt_costcard_date)
				{
					update_individual_field('Expense' , 'dt_costcard_date')
					delete_cookie('Expense');
				}
			}
		}
	}
	else
	{
	populate_form_fields(cookieName);
	}
	*/
}

//---------------------------------------------------------------------------------


function start_unload(cookieName)
{
	//alert('unload');
	//window.alert(_finished);

	//if(_finished=='true')
	//{
		//window.alert(_finished);
		//delete_cookie(cookieName);	

		
	//}

	//if(_finished=='false')
	//{
		//window.alert(_finished);
		//populate_cookie(cookieName);	
	//}
	
	populate_cookie(cookieName);	
		
}

//---------------------------------------------------------------------------------


function final()
{
	
	_finished='true';
	
}

//---------------------------------------------------------------------------------

function  populate_cookie(cookieName)
{

	var updated ='';
	var curCookie;
	var field;
	
	curCookie= cookieName+ "=";
	
	for(i=0;i<document.Form.length;i++)
	{
		field=document.Form.elements[i];
		//alert(field.type + field.tagName + field.name + )
	
		if(field.type=="text" || field.tagName=="SELECT" 
		|| field.tagName=="TEXTAREA" || field.name=="group"
		|| field.name=="costcode"
		|| field.name=="index" ||field.name=="nm_amt"
		|| field.name.indexOf("costcard")!=-1
		|| field.name.indexOf("timecard")!=-1
		|| field.name.indexOf("UserDef")!=-1
		|| field.name.indexOf("proforma")!=-1)
		{
		updated=updated + field.name +" "+ field.value +"\n"
		curCookie+= "&" +field.name+"="+escape(field.value);
		//alert(curCookie)
		}
	}
	
	curCookie+="&end";
	if(curCookie.length>5000) 
	alert("overflowed");
	curCookie+=  ";expires=Wednesday, 31-Dec-2025 23:30:00 GMT" ;
	curCookie+=  ";path=/";
	document.cookie=curCookie;
if(debug)
{
alert("adding to cookie;"+ updated)
alert(curCookie)
}


}
//---------------------------------------------------------------------------------

function  delete_cookie(cookieName)
{

if(debug)
{
alert("deleting cookie"+ cookieName)
}
	
	 if (getCookie(cookieName))
	{
 	        document.cookie =cookieName + "=" +
 	        ";path=/"+
  	       "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   	}
}

//---------------------------------------------------------------------------------
function  save_index()
{
	if(document.clientside.refresh_tindex.value!='')
	{
		curCookie= "TimePrevious=";
		curCookie+="&previndex=" + document.clientside.refresh_tindex.value;
		curCookie+="&timecard_dtmatter=" + document.clientside.last_matter_added.value;
		curCookie+="&unassigned_matter=" + document.Form.unassigned_matter.value;
		curCookie+="&end";
		curCookie+=  ";expires=Wednesday, 31-Dec-2025 23:30:00 GMT" ;
		curCookie+=  ";path=/";
		document.cookie=curCookie;
		document.Form.unassigned_matter.value = ''
		if(document.all.unassigned_matter_desc)
		{
			document.all.unassigned_matter_desc.innerHTML=''
		}
	}
}


//---------------------------------------------------------------------------------

function  populate_form_fields(cookieName)
{
	//alert(cookieName);
	value=getCookie(cookieName);
	//alert(value);
	if(value!=null)
		parseCookie(value);
}
//---------------------------------------------------------------------------------

function getCookie(name) 


{
      var prefix = name + "="
      var cookieStartIndex = document.cookie.indexOf(prefix)
      if (cookieStartIndex == -1)
         return null
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
         prefix.length)
      if (cookieEndIndex == -1)
         cookieEndIndex = document.cookie.length
     return document.cookie.substring(cookieStartIndex +
        prefix.length, cookieEndIndex)

}
//---------------------------------------------------------------------------------

function parseCookie(cookie)
{ 
	var name;
	var value;
	var excluded;
	var updated='';
	excluded = document.Form.excluded_fields.value;
	//alert(excluded);
	index=-1;
	while((index=cookie.indexOf("&"))>-1)
	{
		text=cookie.substring(0,index)
		cookie=cookie.substring(index+1);		
	
		name=text.substring(0,text.indexOf('='));
		value=text.substring(text.indexOf('=')+1);
		//alert(name + excluded + excluded.indexOf(name))
		if((value!="")  && (excluded.indexOf(name)== -1))
		{
			setFields(name,value);
			updated=updated + name +" "+ value +"\n"
		}
		if(name=='UserDef5_desc' && (excluded.indexOf('UserDef5_desc')== -1))
		{
			if(document.all.lbl_UserDef5_Desc)
			{
				document.all.lbl_UserDef5_Desc.innerHTML = unescape(value)
			}
		}

	}

		

if(debug)
{
alert("populating from cookie;"+ updated +"\n")
}

}
//---------------------------------------------------------------------------------

function setFields(name,value)
{
	for(i=0;i<document.Form.length;i++)
	{
		if(document.Form.elements[i].name==name)
		  	document.Form.elements[i].value=unescape(value);
	}

	
}
//---------------------------------------------------------------------------------

function update_individual_field(CookieName,FieldName)
{
	var name;
	var value;
	var Field;
	var cookie = getCookie(CookieName);
	var Fieldindex;
	var AmpIndex;
	var EqualIndex;
	var text;
	var cookieLen;
 
	if(cookie)
	{
		Fieldindex = cookie.indexOf(FieldName)

		cookieLen = cookie.length

		text = cookie.substring(Fieldindex,cookieLen)

		AmpIndex = text.indexOf("&")

		EqualIndex = text.indexOf("=")


		value=text.substring(EqualIndex+1,AmpIndex);
		//alert(value)

		if((value!=""))
		{
			setFields(FieldName,value);
			
		}
	}

}

//---------------------------------------------------------------------------------

function get_individual_field(CookieName,FieldName)
{
	var name;
	var value;
	var Field;
	var cookie = getCookie(CookieName);
	var Fieldindex;
	var AmpIndex;
	var EqualIndex;
	var text;
	var cookieLen;
  		//alert(cookie)
		if (cookie)
		{
		Fieldindex = cookie.indexOf(FieldName)
		//alert (Fieldindex)
		cookieLen = cookie.length

		text = cookie.substring(Fieldindex,cookieLen)

		AmpIndex = text.indexOf("&")

		EqualIndex = text.indexOf("=")


		value=text.substring(EqualIndex+1,AmpIndex);
		return value
		}
		else
		{
		return ''
		}

}
//---------------------------------------------------------------------------------

function  populate_for_timer1(cookieName, oDoc)
//Puts all fields and their values into a cookie.
{
	var updated ='';
	var curCookie;
	var field;
	
	curCookie= cookieName+ "=";
	
	for(i=0;i<oDoc.document.Form.length;i++)
	{
		field=oDoc.document.Form.elements[i];
		fieldName = field.name;
		if(fieldName != 'clientmsg')
			curCookie+= "&" +fieldName  +"="+escape(oDoc.document.Form.elements[fieldName].value);
	}
	//alert(curCookie)
	curCookie+="&end";
	curCookie+=  ";expires=Wednesday, 31-Dec-2025 23:30:00 GMT" ;
	curCookie+=  ";path=/";

	oDoc.document.cookie=curCookie;

}
//---------------------------------------------------------------------------------

function  populate_for_timer(cookieName, oDoc)
//Puts all fields and their values into an xml doc which has been defined in the HTML so that it can be seen by a modal dialog.
{
	
//alert('populate_cookie_for_timer')
	oPostDoc = new ActiveXObject("MSXML2.DOMDocument.4.0")
	oPostDoc.preserveWhiteSpace = true;
	var oNode = oPostDoc.createNode(NODE_ELEMENT, "DICT", "")
	oPostDoc.appendChild ( oNode);
	
	newrowid=''

	addDictItem(oPostDoc,'timecard_dtmatter',get_field_value('timecard_dtmatter',newrowid));
	addDictItem(oPostDoc,'dt_timecard_tworkdt1',get_field_value('dt_timecard_tworkdt1',newrowid));
	addDictItem(oPostDoc,'nm_timecard_tworkhrs',get_field_value('nm_timecard_tworkhrs',newrowid));
	//addDictItem(oPostDoc,'timecard_tmatter',get_field_value('timecard_tmatter',newrowid)); 	 
	addDictItem(oPostDoc,'mdesc1',get_field_value('mdesc1',newrowid));
	//addDictItem(oPostDoc,'lbFrqClients',get_field_value('lbFrqClients',newrowid));
	//addDictItem(oPostDoc,'clname1',get_field_value('clname1',newrowid));
	//addDictItem(oPostDoc,'clnum',get_field_value('clnum',newrowid));
	addDictItem(oPostDoc,'timecard_tstatus',get_field_value('timecard_tstatus',newrowid));
	addDictItem(oPostDoc,'timecard_tstatus_desc',get_field_value('timecard_tstatus_desc',newrowid));
	addDictItem(oPostDoc,'timecard_tcode1',get_field_value('timecard_tcode1',newrowid));
	addDictItem(oPostDoc,'timecard_tcode',get_field_value('timecard_tcode',newrowid));
	addDictItem(oPostDoc,'timecard_tledger',get_field_value('timecard_tledger',newrowid));
	addDictItem(oPostDoc,'timedesc_tddesc',get_field_value('timedesc_tddesc',newrowid));
	addDictItem(oPostDoc,'UserDef1',get_field_value('UserDef1',newrowid));
	addDictItem(oPostDoc,'UserDef2',get_field_value('UserDef2',newrowid));
	addDictItem(oPostDoc,'UserDef3',get_field_value('UserDef3',newrowid));
	addDictItem(oPostDoc,'UserDef4',get_field_value('UserDef4',newrowid));
	addDictItem(oPostDoc,'UserDef5',get_field_value('UserDef5',newrowid));
	addDictItem(oPostDoc,'timecard_task',get_field_value('timecard_task',newrowid));
	addDictItem(oPostDoc,'timecard_task_desc',get_field_value('timecard_task_desc',newrowid));
	addDictItem(oPostDoc,'nm_user_pct_complete',get_field_value('nm_user_pct_complete',newrowid));
	addDictItem(oPostDoc,'nm_hrs_to_complete',get_field_value('nm_hrs_to_complete',newrowid));
	addDictItem(oPostDoc,'nm_psa_worked_hrs',get_field_value('nm_psa_worked_hrs',newrowid));	
	addDictItem(oPostDoc,'pmcount',get_field_value('pmcount',newrowid));	
	addDictItem(oPostDoc,'precision',get_field_value('precision',newrowid));
	addDictItem(oPostDoc,'hrsUnit',get_field_value('hrsUnit',newrowid));		
	addDictItem(oPostDoc,'minsUnit',get_field_value('minsUnit',newrowid));
	addDictItem(oPostDoc,'time_status',get_field_value('time_status',newrowid));	
	addDictItem(oPostDoc,'bill_status',get_field_value('bill_status',newrowid));
	addDictItem(oPostDoc,'UserDef5_desc',get_field_value('UserDef5_desc',newrowid));	 
	addDictItem(oPostDoc,'adesc1',get_field_value('adesc1',newrowid));
	//alert(oPostDoc.documentElement.xml)

}


//---------------------------------------------------------------------------------

function get_field_value(fieldname,newrowid)	
{
	//gets gets value of a field given teh name and the row id of field

	//alert('fieldname=' +  fieldname + ' newrowid=' + newrowid )
	if(newrowid!='')
	{
		strRow = '_new_' + newrowid
	}
	else
	{
		strRow=''
	}
	fieldname = fieldname + strRow;
	//alert(fieldname)
	if(document.Form[fieldname])
	{
		//alert(document.Form[fieldname].value)
		return document.Form[fieldname].value
	}
	else
	{
		return ''
	}
}




