
/********************** Load Images **********************/
if (document.images) {
	pic1= new Image(1,47);  pic1.src="img/tab/a.png"; 
	pic2= new Image(16,47);  pic2.src="img/tab/ab.png"; 
	pic3= new Image(16,47);  pic3.src="img/tab/ac.png"; 
	pic4= new Image(1,47);  pic4.src="img/tab/b.png"; 
	pic5= new Image(16,47);  pic5.src="img/tab/ba.png"; 
	pic6= new Image(16,47);  pic6.src="img/tab/bbl.png"; 
	pic7= new Image(16,47);  pic7.src="img/tab/bbr.png"; 
	pic8= new Image(16,47);  pic8.src="img/tab/bc.png"; 
	pic9= new Image(1,47);  pic9.src="img/tab/c.png"; 
	pic10= new Image(16,47);  pic10.src="img/tab/ca.png"; 
	pic11= new Image(16,47);  pic11.src="img/tab/cb.png";
	pic12= new Image(9,9);  pic12.src="img/dn9.png";
	pic13= new Image(9,9);  pic13.src="img/dr9.png";
	pic14= new Image(9,9);  pic14.src="img/rt9.png";
	pic15= new Image(25,136);  pic15.src="img/cal_lt.png";
	pic16= new Image(25,136);  pic16.src="img/cal_rt.png";
	
	pic17= new Image(7,7);  pic17.src="img/box/a_tl.png";
	pic18= new Image(1,7);  pic18.src="img/box/a_t.png";
	pic19= new Image(11,7);  pic19.src="img/box/a_tr.png";
	pic20= new Image(7,1);  pic20.src="img/box/a_l.png";
	pic21= new Image(1,1);  pic21.src="img/box/a_m.png";
	pic22= new Image(11,1);  pic22.src="img/box/a_r.png";
	pic23= new Image(7,11);  pic23.src="img/box/a_bl.png";
	pic24= new Image(1,11);  pic24.src="img/box/a_b.png";
	pic25= new Image(11,11);  pic25.src="img/box/a_br.png";
	
	pic26= new Image(7,7);  pic26.src="img/box/aa_tl.png";
	pic27= new Image(1,7);  pic27.src="img/box/aa_t.png";
	pic28= new Image(10,7);  pic28.src="img/box/aa_tr.png";
	pic29= new Image(7,1);  pic29.src="img/box/aa_l.png";
	pic30= new Image(1,1);  pic30.src="img/box/aa_m.png";
	pic31= new Image(10,1);  pic31.src="img/box/aa_r.png";
	pic32= new Image(7,10);  pic32.src="img/box/aa_bl.png";
	pic33= new Image(1,10);  pic33.src="img/box/aa_b.png";
	pic34= new Image(10,10);  pic34.src="img/box/aa_br.png";
	
	pic35= new Image(6,6);  pic35.src="img/box/aaa_tl.png";
	pic36= new Image(1,6);  pic36.src="img/box/aaa_t.png";
	pic37= new Image(6,6);  pic37.src="img/box/aaa_tr.png";
	pic38= new Image(6,1);  pic38.src="img/box/aaa_l.png";
	pic39= new Image(1,1);  pic39.src="img/box/aaa_m.png";
	pic40= new Image(6,1);  pic40.src="img/box/aaa_r.png";
	pic41= new Image(6,6);  pic41.src="img/box/aaa_bl.png";
	pic42= new Image(1,6);  pic42.src="img/box/aaa_b.png";
	pic43= new Image(6,6);  pic43.src="img/box/aaa_br.png";
	
	pic44= new Image(19,24);  pic44.src="img/red_arrow_dn.png";
	pic45= new Image(18,18);  pic45.src="img/close.png";
	pic46= new Image(18,18);  pic46.src="img/plus.png";
}

var flag=0;

/* winWidth, winHeight is the absolute position of the mouse click */
var winWidth;
var winHeight;
/* deltaX, deltaY is the absolute position of the upper left corner of the page (indicating how much it has scrolled in x, or y) */
var deltaX;
var deltaY;
/* clickX, clickY is the absolute position of the mouse click */
var clickX = 0;
var clickY = 0;

var leftPos=30;
var topPos=30;

var rightMargin=30;
var leftMargin=30;
var topMargin=10;
var bottomMargin=30;

var d=document;

var contact_open = new Array();

var link_stop=0;

var	name_array = new Array();
var	cal_stay_visible = 0;
var	cal_visible = 0;
var	cal_mm = 0;
var	cal_yy = 0;
var	cal_count = 0;

var	current_scroll_x = 0;
var	current_scroll_y = 0;
var	scroll_x = 0;
var	scroll_y = 0;
var	scroll_vx = 0;
var	scroll_vy = 0;
var	scrolling = 0;
var	scrolltime_old = 0;
var	scrolltime = 0;

var	document_mouse_x = 0;
var	document_mouse_y = 0;


var employees = new Object();
var	default_tasks_array = new Array();

var	employee_id;

var	appt_timestamp;
var	appt_timestamp_arr = new Object(); /* index is the contact_id */
var	appt_name_arr = new Object(); /* index is the contact_id */
var	appt_overlay_arr = new Object(); /* index is the contact_id - shows that the overlay has been displayed for that window */
var	top_fixed_flag=0; /* when scrolling past a certain point, fix the top of the page so it doesn't sroll off of the page */

var	overlay_display_flag=0; /* when when overlay is displayed, any additional overlays are pushed into a buffer */
var	overlay_buffer = new Object(); /*holds the overlay messages if a new one comes in while displayed */
var	onclose_buffer = new Object(); /*holds the onclose functions if a new overlay comes in while displayed */
var	overlay_pointer_push = 0; /*the position to push the next overlay message */
var	overlay_pointer_pop = 0; /*the position to pop the next overlay message */

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = mousemove; /* the mousemove function */



function toggle_contact(contact_id){
	
	if(!(link_stop==1)) {
		setTimeout('link_start()',400);
		link_stop=1;
		if(!(contact_open[contact_id]==1)){
			slidedown_contact(contact_id);
		} else {
			slideup_contact(contact_id);
		}
	}
}



function link_start(){
	link_stop=0;
}




function slideup_contact(contact_id){
	contact_open[contact_id]=0;
	s1_element = document.getElementById('s1_'+contact_id);
	s2_element = document.getElementById('s2_'+contact_id);
	tog_element=document.getElementById('tog_'+contact_id);
	
	/*s1_element.innerHTML = 'daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>daslkfj sdfkj dsafkj lsadkfj lasf<br>'; 
	*/
	
	Effect.SlideUp(s1_element, { duration:0.2 });
	Effect.SlideDown(s2_element, { duration:0.2 });
	tog_element.innerHTML = '<a href="#" onclick="toggle_contact('+contact_id+'); return false;"><IMG SRC="img/rt9.png" width="9" height="9" border="0"></a>'; 
}





function slidedown_contact(contact_id){
	
		/*new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_test&id='+contact_id, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});*/
	/*
	show_overlay('testing1');
	show_overlay('testing2');
	show_overlay('testing3');
	*/
			contact_open[contact_id]=1;
	
	/*alert(document_mouse_y);*/
	/*scroll_y=document_mouse_y-getScrollY()-62;*/
	scroll_element('contact_'+contact_id, 51);
	/*pos=get_element_position('contact_'+contact_id);
	scroll_vy=0;
	scroll_y=pos['y']-getScrollY()-51;*/
	pageScroll();
	
	s1_element = document.getElementById('s1_'+contact_id);
	s2_element = document.getElementById('s2_'+contact_id);
	tog_element=document.getElementById('tog_'+contact_id);
	
	
	
	new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_desc&id='+contact_id, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
	
	s1_element.innerHTML = '\
	<TABLE cellpadding=0 cellspacing=0 border=0><TR class=body>\
	<TD><img src="img/forceprint.gif" width=1 height=200 border=0></TD>\
	\
	<TD valign=top>\
	<div id="m1_'+contact_id+'">\
	<TABLE cellpadding=0 cellspacing=0 border=0 width=400><TR class=body><TD>\
	<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>\
	</TD></TR></TABLE>\
	</div>\
	</TD>\
	<td width=15></td>\
	<TD valign=top>\
	<div id="m2_'+contact_id+'">\
	<TABLE cellpadding=0 cellspacing=0 border=0 width=400><TR class=body><TD>\
	<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>\
	</TD></TR></TABLE>\
	</div>\
	</TD>\
	\
	</TR>\
	</TABLE>';
	
	toggle_after_scroll(contact_id);
	/*Effect.SlideDown(s1_element, { duration:0.6});
	Effect.SlideUp(s2_element, { duration:0.2 });
	
	tog_element.innerHTML = '<a href="#" onclick="toggle_contact('+contact_id+'); return false;"><IMG SRC="img/dn9.png" width="9" height="9" border="0"></a>'; */
}

function	toggle_after_scroll(contact_id){
	if(scrolling==1){
		setTimeout('toggle_after_scroll('+contact_id+')',20);
	} else {
		s1_element = document.getElementById('s1_'+contact_id);
		s2_element = document.getElementById('s2_'+contact_id);
		tog_element=document.getElementById('tog_'+contact_id);
		Effect.SlideDown(s1_element, { duration:0.6});
		Effect.SlideUp(s2_element, { duration:0.2 });
		tog_element.innerHTML = '<a href="#" onclick="toggle_contact('+contact_id+'); return false;"><IMG SRC="img/dn9.png" width="9" height="9" border="0"></a>'; 
	}	
}


function	login_redirect(str){
	temp = str.split('name="referer" value="');
	if(temp[1]) {
		temp2 = temp[1].split('"');
		temp3 = temp2[0].split('?');
		if(temp3[1]) end_str='&ajax_login_redirect='+urlencode(location.href); else end_str='?ajax_login_redirect='+urlencode(location.href);
		window.location.href = temp2[0]+end_str;
	} else {
		window.location.href = unescape(window.location.pathname+window.location.search);
	}
}



var	RxAjax = function(t) {
	/*alert(t.responseText);*/
	JSONData = t.responseText;
	if(JSONData.substr(0,2) != '[{') login_redirect(t.responseText); /* redirect if commands have been processed */
	
	ajaxObject=JSON.parse(JSONData);   
	
	iii=0;
	for (ind in ajaxObject) { // go through each command to send to the appropriate function
		if(ajaxObject[ind]['cmd']) {
			
			iii++; /* count how many commands have been processed */
			window[ajaxObject[ind]['cmd']](ajaxObject[ind]['dat']); /* call the function named in 'cmd' with the argument passed in 'dat' */
			/*switch(ajaxObject[ind]['cmd']) {
			  case 'ajax_fill_comp':
				ajax_fill_comp(ajaxObject[ind]['dat']);
				break;
				
			  case 'ajax_fill_desc':
				ajax_fill_desc(ajaxObject[ind]['dat']);
				break;
				
			  case 'ajax_contact_action':
				ajax_contact_action(ajaxObject[ind]['dat']);
				break;
				
			}*/
			
		}
	}
}


/*var FillContactAjax = function(t) {*/
function	ajax_fill_desc(dat){
	/*alert(t.responseText);*/
	/*alert(location.href);*/
	/*if(t.responseText.indexOf('~@^') == -1) login_redirect(t.responseText);
	var str='';
	
	var level0 = new Array();
	level0 = t.responseText.split('~@^');*/
	contact_id = dat.id;
	
	m1_element = document.getElementById('m1_'+contact_id);
	m1_element.style.display = 'none';
	m1_element.innerHTML = '<TABLE cellpadding=0 cellspacing=0 border=0 width=400><TR class=body><TD>'+js_replace(dat.company_info_html)+dat.contact_history_html+'</TD></TR></TABLE>';
	Effect.SlideDown(m1_element, { duration:0.2});
	
	people_element = document.getElementById('people_'+contact_id);
	people_element.innerHTML = dat.people_html;
	/*
	level0[0] is the contact_id
	level0[1] is the html for the contact history
	level0[2] is the people list for filling the people array
	level0[3] is the html for the people box
	level0[4] is the html for the this/next contact input
	level0[5] is the html for the company info block
	*/
	
	m2_element = document.getElementById('m2_'+contact_id);
	m2_element.style.display = 'none';
	m2_element.innerHTML = '<TABLE cellpadding=0 cellspacing=0 border=0 width=400><TR class=body><TD>'+js_replace(dat.this_next_html)+'</TD></TR></TABLE>';
	
	/*alert(dat.this_next_html);
	alert(contact_id);*/
	
	/*level1 = level0[2].split('~#^');*/
	var iii=0;
	name_array[contact_id] = new Array();
	for (ind in dat.people) { // go through each person
		if(dat.people[ind]) {
			/*alert(dat.people[ind]);*/
			name_array[contact_id][iii]=dat.people[ind];
			iii++;
		}
	}
	/*while(level1[iii]){
		name_array[contact_id][iii]=level1[iii];
		iii++;
	}*/
	
	/**********************Begin Custom Autocompleter**********************/
	input_id='contact_person_' + contact_id;
	div_id='contact_person_dv_' + contact_id;
	arr=name_array[contact_id];
	
	autoc = custom_autocompleter(input_id, div_id, arr);
	/*alert(dumpObj(autoc, 'autoc', '-', 10));
	alert(dumpObj(autoc.getUpdatedChoices, 'autoc.getUpdatedChoices', '-', 10));*/
	
	/**********************End Custom Autocompleter**********************/
	
	
	Effect.SlideDown(m2_element, { duration:0.2, afterFinish: function() { scroll_element('contact_'+contact_id, 51); }});
	document.forms['contactform_'+contact_id].reset();
	document.forms['contactform_'+contact_id].description.value='';
	document.forms['contactform_'+contact_id].id.value=contact_id;
	/*dat = document.getElementById('contact_date['+contact_id+']'); 
	dat.value = 'test';*/
	/*Event.observe('contact_date_'+contact_id, 'focus', function(event){ show_cal('contact_date_'+contact_id);});
	Event.observe('contact_date_'+contact_id, 'blur', function(event){ hide_cal('contact_date_'+contact_id);});*/
	
	/*calendar_div.style.visibility = 'visible';*/

	/*calendar_div = document.getElementById('calendar_div['+contact_id+']');
	calendar_div.innerHTML = 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest';*/
	
	/*m1_element = document.getElementById('m1_'+contact_id);
	alert(calendar_div.innerHTML);
	alert('test');*/

}




function	ajax_tz_ds(dat){
	
	alert('Computer\'s Clock Changed... Click to Continue');
	window.location = window.location.href;
}




function	ajax_fill_comp(dat){
	
	contact_id = dat.id;
	
	div = document.getElementById('comp_'+contact_id);
	div.innerHTML = dat.html;
}




function	ajax_alert(dat){
	
	show_overlay(dat.html);
}


function	ajax_onload(dat){
	/*alert(dat.cheese);
	qqq=dat.stuff;
	alert(qqq.one);
	alert(qqq.two);*/
	
	appt_timestamp_arr = dat.appt_timestamp_arr;
	appt_name_arr = dat.appt_name_arr;
	/*for (var contact_id in appt_timestamp_arr) {
		alert(appt_timestamp_arr[contact_id]);
		alert(appt_name_arr[contact_id]);
	}*/
}




function	custom_autocompleter(input_id, div_id, arr){

	autoc = new Autocompleter.Local(input_id, div_id, arr, { selector: function(instance) {
			
			var ret       = []; // Beginning matches
			var partial   = []; // Inside matches
			var entry     = instance.getToken();
			var count     = 0;
			/*alert(dumpObj(instance, 'instance', '-', 10));*/
			instance.options.choices=40;
			
			for (var i = 0; i < instance.options.array.length &&  ret.length < instance.options.choices ; i++) { /* go through each element in the options array */
				
				var elem = instance.options.array[i];
				var foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase()) : elem.indexOf(entry);
				
				if ((entry.length<1)&&(foundPos == -1)) {
					ret.push("<li>" + elem + "</li>");
				} else {
					while (foundPos != -1) {
						if (foundPos == 0 && elem.length != entry.length) { 
							ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" + elem.substr(entry.length) + "</li>");
							//ret.push("<li>" + elem + "</li>");
							break;
						} else if (entry.length >= 1 && 
								instance.options.partialSearch && foundPos != -1) {
							if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
								partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" + elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(foundPos + entry.length) + "</li>");
								break;
							}
						}
						
						foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : elem.indexOf(entry, foundPos + 1);
						
					}
				}
			}
			if (partial.length)
					ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
					return "<ul>" + ret.join('') + "</ul>";
		}
	});
	
	
	
	
	autoc.show = function () {
		this.update.style.zIndex = 255; /* jdg added this line to fix overlapping problem */
		if (Element.getStyle(this.update, "display") == "none")
				this.options.onShow(this.element, this.update);
		if (!this.iefix && Prototype.Browser.IE && Element.getStyle(this.update, "position") == "absolute")
		{
			new Insertion.After(this.update, "<iframe id=\"" + this.update.id + "_iefix\" " + "style=\"display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);\" " + "src=\"javascript:false;\" frameborder=\"0\" scrolling=\"no\"></iframe>");
			this.iefix = $(this.update.id + "_iefix");
		}
		if (this.iefix)
				setTimeout(this.fixIEOverlapping.bind(this), 50);
	}
	
	
	autoc.addObservers = function (element) {
		/*Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));*/
		Event.observe(element, "click", this.onClick.bindAsEventListener(this));
	}
	
	autoc.onFocus = function (event) {
		this.activate();
		this.index=-1; /* jdg added this line to prevent the tab from triggering index zero from autofilling */
		/*alert('hi');*/
	}
	Event.observe(autoc.element, "focus", autoc.onFocus.bindAsEventListener(autoc));
	
	
	autoc.onObserverEvent = function () {
		/*alert('test');*/
		this.changed = false;
		if (this.getToken().length >= 0)
		{
			this.startIndicator();
			this.getUpdatedChoices();
		}
		else
		{
			this.active = false;
			this.hide();
		}
	}
	
	
	return(autoc);
}






function	show_cal(cal_input_id){
	/*alert(cal_input_id);*/
	if(cal_visible!=1){
		dat = document.getElementById(cal_input_id);
		/*calendar_div = document.getElementById('calendar_div['+contact_id+']');*/
	  var calendar_div = addDiv(document.body, 'calendar_div_'+cal_input_id, { 'display': 'none', 'position' : 'absolute' } );
		calendar_div.style.zIndex = 200;
		/*ccc=tcal(contact_id, get_timestamp(2008, 5, 1, 0, 0, 0));*/
		/*ccc=tcal(contact_id, time());*/
		mm=parseInt(date("m", time()), 10); // the 10 at the end means base 10 integer - 
		yy=parseInt(date("Y", time()), 10); // the 10 at the end means base 10 integer - 
		cal_mm=mm;
		cal_yy=yy;
		cal_count=3;
		ccc=cal_table(cal_input_id, yy, mm, 3);
		
		/*tcal($employee, mktime(0,0,0,date("m")+1,1,date("Y")), '');*/
		calendar_div.innerHTML = '<div id="'+cal_input_id+'_0">'+ccc+'</div>';
		Position.clone(dat, calendar_div, {setHeight: false, setWidth: false, offsetTop: dat.offsetHeight, offsetLeft: -170});
		
		new Effect.Appear(calendar_div, { duration:0.0 });
		cal_visible=1;
	}
}

function	cal_table(cal_input_id, yy, mm, count){
	ccc='<table cellpadding=0 cellspacing=0 border=0 bgcolor=#FFFFFF><tr valign=top>';
	ccc += '<td><a href="#"  onmouseover="cal_stay_visible=1;" onmouseout="cal_stay_visible=0;" onclick="javascript: return(false);"><IMG SRC="img/cal_lt.png" WIDTH=25 HEIGHT=136 BORDER=0></a></td>';
	sp='';
	for(ii=1; ii<=count; ii++){
		ccc += sp+'<td>'+tcal(cal_input_id, get_timestamp(yy, mm, 1, 0, 0, 0))+'</td>';
		mm++; if(mm>12) {mm=1; yy++;}
		sp='<td>&nbsp;&nbsp;</td>';
	}
	ccc += '<td><a href="#"  onmouseover="cal_stay_visible=2;" onmouseout="cal_stay_visible=0;" onclick="javascript: return(false);"><IMG SRC="img/cal_rt.png" WIDTH=25 HEIGHT=136 BORDER=0></a></td>';
	ccc += '</tr></table>';
	return(ccc);
}



function	show_cal_next(cal_input_id){
	cal_mm=cal_mm+cal_count;
	while(cal_mm>12){
		cal_yy++;
		cal_mm -= 12;
	}
	c0 = document.getElementById(cal_input_id+'_0');
	ccc=cal_table(cal_input_id, cal_yy, cal_mm, cal_count);
	c0.innerHTML = ccc;
}



function	show_cal_prev(cal_input_id){
	cal_mm=cal_mm-cal_count;
	while(cal_mm<1){
		cal_yy--;
		cal_mm += 12;
	}
	c0 = document.getElementById(cal_input_id+'_0');
	ccc=cal_table(cal_input_id, cal_yy, cal_mm, cal_count);
	c0.innerHTML = ccc;
}



function	hide_cal(cal_input_id){
	/*alert(cal_input_id);*/
	if(cal_stay_visible==2){
		show_cal_next(cal_input_id); 
		dat = document.getElementById(cal_input_id); 
		dat.focus()
	} else if(cal_stay_visible==1){
		show_cal_prev(cal_input_id); 
		dat = document.getElementById(cal_input_id); 
		dat.focus()
	} else {
		calendar_div = document.getElementById('calendar_div_'+cal_input_id);
		new Effect.Fade(calendar_div, { duration:0.2 });
		cal_visible=0;
		remove_element('calendar_div_'+cal_input_id);
	}
}



/****************** this next section is golden, allows me to dump objects, so I can modify the functions, etc. *******************/
 
function dumpObj(obj, name, indent, depth) {
	var MAX_DUMP_DEPTH = 10;
	if (depth > MAX_DUMP_DEPTH) {
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	if (typeof obj == "object") {
		var child = null;
		var output = indent + name + "\n";
		indent += "\t";
		for (var item in obj)
		{
			try {
				child = obj[item];
			} catch (e) {
				child = "<Unable to Evaluate>";
			}
			if (typeof child == "object") {
				output += dumpObj(child, item, indent, depth + 1);
			} else {
				output += indent + item + ": " + child + "\n";
			}
		}
		return output;
	} else {
		return obj;
	}
}




function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}





/*
n,j,Y,t,F,Y,w,j,M,Y,m,d
*/

function tcal(cal_input_id, timestamp){
/*
	alert(time());
	alert(date('M j Y-- H:i:s -- w', time()));
	alert(date('M j Y-- H:i:s -- w', get_timestamp(2008, 12, 25, 0, 0, 0)));
	alert(NthDay(3,0,12,2008));
	alert(easter_date(2008));
	alert(LastInMonth(4, 12, 2008));
	holidays=get_holidays(2008);
	alert(dumpObj(holidays));
	alert(holidays['2008-01-01']);
*/
	mm=date('n', timestamp);
	dd=date('j', timestamp);
	yyyy=date('Y', timestamp);
	
	holiday=get_holidays(yyyy);
	
	days_in_month=date('t', timestamp);
	
	/*time_first=timestamp-((dd-1)*86400);*/
	time_first=get_timestamp(yyyy, mm, 1, 0, 0, 0)
	
	str = '<TABLE cellspacing=1 cellpadding=3 BORDER=0 bgcolor=#999999>\
	<TR BGCOLOR=#BBBBBB NOWRAP=TRUE CLASS="body">\
	<TD COLSPAN=7><CENTER><FONT COLOR=#000000>' + date("F Y", timestamp) + '</FONT></CENTER></TD>\
	</TR>\
	<TR BGCOLOR=#FFFFFF NOWRAP=TRUE CLASS="body">\
	<TD WIDTH=14%><CENTER><FONT COLOR=#000000>S</FONT></CENTER></TD>\
	<TD WIDTH=14%><CENTER><FONT COLOR=#000000>M</FONT></CENTER></TD>\
	<TD WIDTH=15%><CENTER><FONT COLOR=#000000>T</FONT></CENTER></TD>\
	<TD WIDTH=14%><CENTER><FONT COLOR=#000000>W</FONT></CENTER></TD>\
	<TD WIDTH=14%><CENTER><FONT COLOR=#000000>T</FONT></CENTER></TD>\
	<TD WIDTH=15%><CENTER><FONT COLOR=#000000>F</FONT></CENTER></TD>\
	<TD><CENTER><FONT COLOR=#000000>S</FONT></CENTER></TD>\
	</TR>';
	
	color='#EEEEEE';
	str += '<TR BGCOLOR=' + color + ' CLASS="cal">';
	
	
	if(date("w", time_first)>0) {
		for(i=0;i<date("w", time_first); i++){
			str += '<TD bgcolor=#ffffff><IMG SRC="img/forceprint.gif" WIDTH=1 HEIGHT=1 BORDER=0 ALIGN=bottom></TD>';
		}
	}
	
	for(i=0; i<days_in_month; i++) {
		current_day=time_first+i*86400;
		clr='#EEEEEE'; 
		if(holiday[date('Y-m-d', current_day)] ) clr='#FFFF44';
		if(date('j M Y', current_day)==date("j M Y", time()+get_time_zone()*60*60)) clr='#55FF55';
		
		str += '<TD ALIGN=CENTER VALIGN=TOP onmouseover="style.backgroundColor=\'#FFFF44\';" onmouseout="style.backgroundColor=\'' + clr + '\';" ONMOUSEDOWN="dat = document.getElementById(\''+cal_input_id+'\'); dat.value = \'' + date("m/d/Y", current_day) + '\';" BGCOLOR=' + clr + '><font color=#0000FF>' + date("j", current_day) + '</font></TD>';
		
		if(date("w", current_day)==6){
			str +=  '</TR><TR BGCOLOR=' + color + ' CLASS="cal">';
		}
		last_day=current_day;
	}
	
	if(	date("w", last_day)<6) {
		for(i=date("w", last_day);i<6; i++){
			str += '<TD bgcolor=#ffffff><IMG SRC="../images/forceprint.gif" WIDTH=1 HEIGHT=1 BORDER=0 ALIGN=bottom></TD>';
		}
	}
	
	str += '</TR></TABLE>';
	return(str);
}














function submit_event_form(contact_id) {
	form=document.getElementById('contactform_'+contact_id);
	if((ck_field(form.contact_person)||(form.kind.value == -1)) && ck_pulldown(form.kind) && ck_pulldown(form.temperature)){ /* allow no contact person for kind=NO CONTACT */
		/*alert(form_string(form));
		alert(contact_id);*/
		new Ajax.Request('in_contacts.php?nc='+nc()+'&'+form_string(form), {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		div = document.getElementById('contact_'+contact_id);
		div.innerHTML = '<BR><BR><BR><BR><BR><IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0><BR><BR><BR><BR><BR>';
	} else {
		if(!ck_pulldown(form.kind)) alert_noinput('kind_' + contact_id, 'Select Interaction');
		if(!ck_pulldown(form.temperature)) alert_noinput('temperature_' + contact_id, 'Select Temperature');
		if((!ck_field(form.contact_person)) && (form.kind.value != -1)) alert_noinput('contact_person_' + contact_id, 'Enter Name'); /* allow no contact person for kind=NO CONTACT */
	}
}


/*var ContactActionAjax = function(t) {*/
	
function	ajax_contact_action(dat){
	/*if(t.responseText.indexOf('~@^') == -1) login_redirect(t.responseText);
	var str='';
	
	var level0 = new Array();
	level0 = t.responseText.split('~@^');*/
	
	contact_id = dat.id;
	
	new Effect.SlideUp('contact_'+contact_id, { duration:0.3 });
}




function submit_comp_form(contact_id) {
	form=document.getElementById('comp_form_'+contact_id);
	if(ck_field(form.contact_name)){
		new Ajax.Request('in_contacts.php?nc='+nc()+'&'+form_string(form), {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		/*action=ajax_new_ca&id=189189&contact_name=Mark&username=&*/
		div=document.getElementById('comp_'+contact_id);
		div.innerHTML = div.innerHTML + '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
		close_popup('comp_input_'+contact_id);
	} else {
		if(!ck_field(form.contact_name)) alert_noinput('comp_contact_name_' + contact_id, 'Enter Name');
	}
}





function	alert_noinput(input_id, message) {
	alert_id=input_id+'_alert';
	if(!document.getElementById(alert_id)) var myDiv = addDiv(document.body, alert_id, {
		'display': 'none', 
				'background-color' : 'white', 
				'position' : 'absolute',
				'border-style' : 'double', 
				'border-color' : 'red',
				'z-index' : '3000'
			} );
	inp = document.getElementById(input_id);
	po=document.getElementById(alert_id);
	po.innerHTML='<table border=0 cellpadding=3 cellspacing=0 bgcolor=white><tr class=body><td align=center>' + message + '<BR><IMG SRC="img/red_arrow_dn.png" WIDTH=19 HEIGHT=24 BORDER=0></td></tr></table>';
	Position.clone(inp, po, {setHeight: false, setWidth: false, offsetTop: -48, offsetLeft: 5});
	Effect.Appear(po, { duration:0.0 });
	Effect.Shake(po, { duration:0.25, distance:3});
	setTimeout('puff_away(\''+alert_id+'\')',900);
}



function	popup_comp_input(contact_id){
	html = '<table bgcolor=#8888AA cellpadding=2 cellspacing=0 border=0><tr><td><table cellspacing=0 cellpadding=2 border=0 bgcolor=#D5D5E8><tr><td align=right width=18>\
			<a href="#" onclick="close_popup(\'comp_input_'+contact_id+'\'); return(false);"><IMG SRC="img/close.png" WIDTH=18 HEIGHT=18 BORDER=0></a>\
			</td><td class=body align=center>\
			<B>New Complimentary Account</B></td></tr>\
	<tr><td bgcolor=#FFFFFF align=center colspan=2 class=body>\
	<table cellpadding=10 cellspacing=0 border=0><tr class=t_bd><td nowrap=true>\
	<FORM action="javascript:submit_comp_form(\''+contact_id+'\');" name="comp_form_'+contact_id+'" id="comp_form_'+contact_id+'">\
			<INPUT TYPE=HIDDEN NAME="action" VALUE="ajax_new_ca">\
	<INPUT TYPE=HIDDEN NAME="id" VALUE="'+contact_id+'">\
			New Account For: <INPUT TYPE=text NAME="contact_name" id="comp_contact_name_'+contact_id+'" VALUE="" SIZE=12 maxLength=60><div class="autocomplete" id="comp_contact_name_dv_'+contact_id+'" style="display:none"></div><INPUT TYPE=submit NAME=Submit VALUE="Create"><BR>\
	<div id="comp_existing1_'+contact_id+'"><a href="#" onclick="document.getElementById(\'comp_existing1_'+contact_id+'\').style.display=\'none\'; document.getElementById(\'comp_existing2_'+contact_id+'\').style.display=\'inline\'; return(false);">Link to Existing Account</a></div>\
	<div id="comp_existing2_'+contact_id+'" style="display:none">Enter Existing Username:<INPUT TYPE=text NAME="username" VALUE="" SIZE=12 maxLength=20></div>\
	</FORM>\
	</td></tr></table>\
	</td></tr></table></td></tr></table>';
	
	div=new_popup('comp_'+contact_id, 'comp_input_'+contact_id, -10, -31, html);
	
	input_id='comp_contact_name_' + contact_id;
	div_id='comp_contact_name_dv_' + contact_id;
	arr=name_array[contact_id];
	
	autoc = custom_autocompleter(input_id, div_id, arr);

}





function	popup_task_input(contact_id){
	html = '<table bgcolor=#8888AA cellpadding=2 cellspacing=0 border=0 STYLE="z-index: 200"><tr><td><table cellspacing=0 cellpadding=2 border=0 bgcolor=#D5D5E8><tr><td align=right width=18>\
			<a href="#" onclick="close_popup(\'task_input_'+contact_id+'\'); return(false);"><IMG SRC="img/close.png" WIDTH=18 HEIGHT=18 BORDER=0></a>\
			</td><td class=body align=center>\
			<B>New Task</B></td></tr>\
	<tr><td bgcolor=#FFFFFF align=center colspan=2 class=body>\
	<table cellpadding=10 cellspacing=0 border=0><tr class=t_bd><td nowrap=true>\
	<FORM action="javascript:submit_task_form(\''+contact_id+'\');" name="task_form_'+contact_id+'" id="task_form_'+contact_id+'">\
			<INPUT TYPE=HIDDEN NAME="action" VALUE="ajax_new_task">\
	<INPUT TYPE=HIDDEN NAME="id" VALUE="'+contact_id+'">';
	
	html  += 'For: &nbsp;<SELECT NAME="owner_id">';
	for(var ii in employees) {
		if(ii == employee_id) sel=' selected'; else sel='';
		html += '<OPTION value="'+ii+'"'+sel+'>'+employees[ii];
	}
	html += '</SELECT>&nbsp;&nbsp;';
	
	html += '<INPUT TYPE=text NAME="description" id="task_description_'+contact_id+'" VALUE="" SIZE=22 maxLength=512><div class="autocomplete" id="task_description_dv_'+contact_id+'" style="display:none"></div><BR>';
	
	html += '<INPUT TYPE=submit NAME=Submit VALUE="Add New Task"><BR>\
	</FORM>\
	</td></tr></table>\
	</td></tr></table></td></tr></table>';
	
	div=new_popup('task_'+contact_id, 'task_input_'+contact_id, -4, -22, html);
	
	input_id='task_description_' + contact_id;
	div_id='task_description_dv_' + contact_id;
	arr=default_tasks_array;
	
	autoc = custom_autocompleter(input_id, div_id, arr);
}



function submit_task_form(contact_id) {
	form=document.getElementById('task_form_'+contact_id);
	if(ck_field(form.description)){
		new Ajax.Request('in_contacts.php?nc='+nc()+'&'+form_string(form), {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		
		div=document.getElementById('task_'+contact_id);
		div.innerHTML = div.innerHTML + '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
		close_popup('task_input_'+contact_id);
	} else {
		if(!ck_field(form.description)) alert_noinput('task_description_' + contact_id, 'Enter Task');
	}
}


function	ajax_fill_task(dat){
	
	contact_id = dat.id;
	
	div = document.getElementById('task_'+contact_id);
	div.innerHTML = js_replace(dat.html);
}


function	ajax_fill_task2(dat){
	
	contact_id = dat.id;
	task_id = dat.task_id;
	
	div = document.getElementById('task_'+contact_id+'_'+task_id);
	div.innerHTML = '';
}


function submit_completed_task(contact_id, task_id) {
	if(confirm('Mark Task as Completed?')){
		form=document.getElementById('task_form_'+contact_id);
		
		new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_completed_task&id='+contact_id+'&task_id='+task_id, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		
		div=document.getElementById('task_'+contact_id);
		div.innerHTML = div.innerHTML + '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
	}
}

function submit_completed_task2(contact_id, task_id) {
	if(confirm('Mark Task as Completed?')){
		form=document.getElementById('task_form_'+contact_id);
		
		new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_completed_task2&id='+contact_id+'&task_id='+task_id, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		
		div=document.getElementById('task_'+contact_id+'_'+task_id);
		div.innerHTML = '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
	}
}


function send_task_reminder(contact_id, task_id) {
	form=document.getElementById('task_form_'+contact_id);
	
	new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_send_task_reminder&id='+contact_id+'&task_id='+task_id, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
	
	div=document.getElementById('remind_task_'+task_id);
	div.innerHTML = '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
}


function	ajax_task_reminder_sent(dat){
	
	contact_id = dat.id;
	task_id = dat.task_id;
	
	div = document.getElementById('remind_task_'+task_id);
	div.innerHTML = '';
}





function open_wb(emp, str) {
	new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_wb_display&from_employee_id='+emp+'&str='+urlencode(str), {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
}




function submit_wb_form() {
	form=document.getElementById('wb_overlay_form');
	if(ck_field(form.message)){
		new Ajax.Request('in_contacts.php?nc='+nc()+'&'+form_string(form), {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
		
		div=document.getElementById('wb_overlay_span');
		div.innerHTML = '<IMG SRC="img/busyspin.gif" WIDTH=16 HEIGHT=16 BORDER=0>';
	} else {
		alert_noinput('wb_message', 'Enter Message');
	}
}




function	ajax_new_wb_sent(dat){
	
	junk = dat.id;	
	hide_overlay();
}







function	new_popup(parent_id, div_id, dx, dy, html){
	/*add_div_absolute(parent_id, div_id);*/
	addDiv(document.body, div_id, { 'display': 'none', 'position' : 'absolute' } );
	inp = document.getElementById(parent_id);
	po=document.getElementById(div_id);
	po.innerHTML=html;
	Position.clone(inp, po, {setHeight: false, setWidth: false, offsetTop: dy, offsetLeft: dx});
	Effect.SlideDown(po, { duration:0.2 });
	po.style.zIndex="9000";
	return(po);
}


function	close_popup(div_id){
	if(po=document.getElementById(div_id)) Effect.SlideUp(po, { duration:0.2 });
}

function	remove_element(div_id){
	if(div=document.getElementById(div_id)){
		/*parent = div.parentNode;*/
		div.parentNode.removeChild(div);
	}
}


function	add_div_absolute(parent_id_str, id_str){
	remove_element(id_str);
	if(parent_id_str) {
		parent_element = document.getElementById(parent_id_str); 
	} else {
		parent_element = document.body;
	}
  if(!document.getElementById(id_str)) var myDiv = addDiv(parent_element, id_str, { 'display' : 'none', 'position' : 'absolute' } );
	return(document.getElementById(id_str));
}


function	puff_away(div_id){
	Effect.Puff(document.getElementById(div_id), { duration:0.3});	
}




function edge_watch(element, t){
	var elementDimensions = Element.getDimensions(element);
	var eWidth = elementDimensions.width;
	var eHeight = elementDimensions.height;
	var moveX = winWidth - eWidth + deltaX - rightMargin;
	var moveY = winHeight - eHeight + deltaY - bottomMargin;
	
	move=0;
	if(leftPos>moveX) {leftPos=moveX; move=1;}
	if(topPos>moveY) {topPos=moveY; move=1;}
		/*alert(leftPos+' '+topPos);*/
 new Effect.Move(element, { x: leftPos, y: topPos, mode: 'absolute', duration: t });
}



var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}






function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function nc(){
	var d = new Date();
	return(d.getMilliseconds());	
}

function date_str(timestamp){
	
	var date_var = new Date(timestamp * 1000);
	str=date_var.toGMTString();
	
	var temp = new Array();
	temp = str.split(' ');
	return(temp[0]+' '+temp[2]+' '+temp[1]+', '+temp[3]);

}

function DateFromTimestamp(t){
	d = new Date(t * 1000);
	return(d);
}

function get_timestamp(year, month, day, h, m, s){
	/*var dat = new Date(year, (month-1), day, h, m, s);
	return(Math.floor(dat.getTime()/1000.0));*/
	var d = Date.UTC(year, (month-1), day, h, m, s);
	return(Math.floor(d/1000.0));
}

function	time(){
	var d = new Date();
	return(Math.floor(d.getTime()/1000.0));
}

function time_ms(){
	var d = new Date();
	return(d.getTime());
}

function	date(str, ts){
d = new Date(ts*1000);
return(d.formatDate(str));
}


/*
function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d;
}
*/
/*
For example:
document.write(NthDay(3,7,11,1997)+'<BR>');
*/
/*
function NthDay(nth,weekday,month,year) {
    return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
}
*/
function	NthDay(nth, day_of_week, month, year){ /* NthDay(2, 0, 12, 2008) is the day of the month of the second Sunday in December 2008 */
	d1 = parseInt(date('w', get_timestamp(year, month, 1, 0, 0, 0)), 10); // the 10 at the end means base 10 integer - day of week (0-6) for the first day of the month
	first_occurrence=day_of_week-d1+1;
	if(first_occurrence<1) first_occurrence += 7;
	return(first_occurrence+(nth-1)*7);
}

function	LastInMonth(day_of_week, month, year){ /* LastInMonth(0, 12, 2008) is the last Sunday in December 2008 */
	d1 = parseInt(date('w', get_timestamp(year, month, 1, 0, 0, 0)), 10); // the 10 at the end means base 10 integer - day of week (0-6) for the first day of the month
	first_occurrence=day_of_week-d1+1;
	if(first_occurrence<1) first_occurrence += 7;
	days_in_month = parseInt(date('t', get_timestamp(year, month, 1, 0, 0, 0)), 10); // the 10 at the end means base 10 integer - number of days in the month
	flag=0;
	while(first_occurrence<=days_in_month){
		first_occurrence +=7;
	}
	first_occurrence-=7;
	return(first_occurrence);
}

function easter_date(Y) {
	Y = parseInt(Y, 10);
    var C = Math.floor(Y/100);
    var N = Y - 19*Math.floor(Y/19);
    var K = Math.floor((C - 17)/25);
    var I = C - Math.floor(C/4) - Math.floor((C - K)/3) + 19*N + 15;
    I = I - 30*Math.floor((I/30));
    I = I - Math.floor(I/28)*(1 - Math.floor(I/28)*Math.floor(29/(I + 1))*Math.floor((21 - N)/11));
    var J = Y + Math.floor(Y/4) + I + 2 - C + Math.floor(C/4);
    J = J - 7*Math.floor(J/7);
    var L = I - J;
    var M = 3 + Math.floor((L + 40)/44);
    var D = L + 28 - 31*Math.floor(M/4);

    return (get_timestamp(Y, M, D, 0, 0, 0));
}


function get_holidays(year) {
	ret = new Object();
	
	// First off, the simple ones
	
	ret[year + '-01-01'] = 'New Year`s Day';
	//ret[year + '-02-14'] = 'Valentine`s Day';
	//ret[year + '-06-14'] = 'Flag Day';
	ret[year + '-07-04'] = 'Independence Day';
	ret[year + '-11-11'] = 'Veteran`s Day';
	ret[year + '-12-25'] = 'Christmas';
	
	
	//and now, the more complex ones
	
	//Martin Luther King, Jr. Day
	//third Monday in January 
	ret[date( 'Y-m-d', get_timestamp(year, 1, NthDay(3, 1, 1, year), 0, 0, 0))] = 'Martin Luther King, Jr. Day';
	
	//Presidents` Day
	//third Monday in February
	ret[date( 'Y-m-d', get_timestamp(year, 2, NthDay(3, 1, 2, year), 0, 0, 0))] = 'Presidents` Day';
	
	// Easter
	//the Sunday after the first full moon which falls on or after the Spring Equinox
	ret[date( 'Y-m-d', easter_date(year))] = 'Easter';
	
	//Memorial Day
	//last Monday in May
	ret[date( 'Y-m-d', get_timestamp(year, 5, LastInMonth(1, 5, year), 0, 0, 0))] = 'Memorial Day';
	
	//Labor Day
	//first Monday in September
	ret[date( 'Y-m-d', get_timestamp(year, 9, NthDay(1, 1, 9, year), 0, 0, 0))] = 'Labor Day';
	
	//Columbus Day
	//second Monday in October
	//ret[date( 'Y-m-d', strtotime( '1 week monday', strtotime( "October 1, $year" ) ) + 43200 )] = 'Columbus Day';
	
	//Thanksgiving
	//fourth Thursday in November
	ret[date( 'Y-m-d', get_timestamp(year, 11, NthDay(4, 4, 11, year), 0, 0, 0))] = 'Thanksgiving';
	
	
	return(ret);
	
 }

function	js_replace(str){
	ret='';
	
	level0 = str.split('js_replace[');
	ret += level0[0];
	
	var ii=1;
	while(level0[ii]){
		level1 = level0[ii].split(']');
		
		switch(level1[0]){
		  case 'a_box_start':
			ret += '<table cellpadding=0 cellspacing=0 border=0>\
<tr>\
<td width=7 height=7><IMG SRC="img/box/a_tl.png" width="7" height="7" border="0"></td>\
<td height=7 background="img/box/a_t.png"><IMG SRC="img/forceprint.gif" width="1" height="7" border="0"></td>\
<td width=11 height=7><IMG SRC="img/box/a_tr.png" width="11" height="7" border="0"></td>\
</tr>\
<tr class=body>\
<td width=7 background="img/box/a_l.png"><IMG SRC="img/forceprint.gif" width="7" height="1" border="0"></td>\
<td background="img/box/a_m.png">\
<table cellpadding=3 cellspacing=0 border=0><tr class=body><td>';
			break;
		  case 'a_box_end':
			ret += '</td></tr></table>\
</td>\
<td width=11 background="img/box/a_r.png"><IMG SRC="img/forceprint.gif" width="11" height="1" border="0"></td>\
</tr>\
<tr>\
<td width=7 height=11><IMG SRC="img/box/a_bl.png" width="7" height="11" border="0"></td>\
<td height=11 background="img/box/a_b.png"><IMG SRC="img/forceprint.gif" width="1" height="11" border="0"></td>\
<td width=11 height=11><IMG SRC="img/box/a_br.png" width="11" height="11" border="0"></td>\
</tr>\
</table>';
			break;
		  case 'aa_box_start':
			ret += '<table cellpadding=0 cellspacing=0 border=0>\
<tr>\
<td width=7 height=7><IMG SRC="img/box/aa_tl.png" width="7" height="7" border="0"></td>\
<td height=7 background="img/box/aa_t.png"><IMG SRC="img/forceprint.gif" width="1" height="7" border="0"></td>\
<td width=10 height=7><IMG SRC="img/box/aa_tr.png" width="10" height="7" border="0"></td>\
</tr>\
<tr class=body>\
<td width=7 background="img/box/aa_l.png"><IMG SRC="img/forceprint.gif" width="7" height="1" border="0"></td>\
<td background="img/box/aa_m.png">\
<table cellpadding=0 cellspacing=0 border=0><tr class=body><td>';
			break;
		  case 'aa_box_end':
			ret += '</td></tr></table>\
</td>\
<td width=10 background="img/box/aa_r.png"><IMG SRC="img/forceprint.gif" width="10" height="1" border="0"></td>\
</tr>\
<tr>\
<td width=7 height=10><IMG SRC="img/box/aa_bl.png" width="7" height="10" border="0"></td>\
<td height=10 background="img/box/aa_b.png"><IMG SRC="img/forceprint.gif" width="1" height="10" border="0"></td>\
<td width=10 height=10><IMG SRC="img/box/aa_br.png" width="10" height="10" border="0"></td>\
</tr>\
</table>';
			break;
		  case 'aaa_box_start':
			ret += '<table cellpadding=0 cellspacing=0 border=0>\
<tr>\
<td width=6 height=6><IMG SRC="img/box/aaa_tl.png" width="6" height="6" border="0"></td>\
<td height=6 background="img/box/aaa_t.png"><IMG SRC="img/forceprint.gif" width="1" height="6" border="0"></td>\
<td width=6 height=6><IMG SRC="img/box/aaa_tr.png" width="6" height="6" border="0"></td>\
</tr>\
<tr class=body>\
<td width=6 background="img/box/aaa_l.png"><IMG SRC="img/forceprint.gif" width="6" height="1" border="0"></td>\
<td background="img/box/aaa_m.png">\
<table cellpadding=0 cellspacing=0 border=0><tr class=body><td>';
			break;
		  case 'aaa_box_end':
			ret += '</td></tr></table>\
</td>\
<td width=6 background="img/box/aaa_r.png"><IMG SRC="img/forceprint.gif" width="6" height="1" border="0"></td>\
</tr>\
<tr>\
<td width=6 height=6><IMG SRC="img/box/aaa_bl.png" width="6" height="6" border="0"></td>\
<td height=6 background="img/box/aaa_b.png"><IMG SRC="img/forceprint.gif" width="1" height="6" border="0"></td>\
<td width=6 height=6><IMG SRC="img/box/aaa_br.png" width="6" height="6" border="0"></td>\
</tr>\
</table>';
			break;
		}
		if(level1[1]) ret += level1[1];
		
		var iii=2;
		while(level1[iii]){ /* if there are more right brackets after the js replace, we must replace them */
			ret += ']'+level1[iii];
			iii++;
		}
		ii++;
	}
	return(ret);
}

function	form_string(obj) { /* recursively walk the DOM and get the values of form input objects the urlencode the values */
	/*alert('start');*/
	var ret = '';
	var i;
	for (i=0; i<obj.childNodes.length; i++) {
		/*alert('0');*/
		/*alert(obj.childNodes[i].tagName + ' ' + obj.childNodes[i].type + ' ' + obj.childNodes[i].name + ' ' + obj.childNodes[i].value);*/
		/*alert('0.5');*/
		switch (obj.childNodes[i].tagName){
			
		  case 'input':
		  case 'INPUT':
			switch(obj.childNodes[i].type) { 
				
			  case 'text':
			  case 'TEXT':
				ret += obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + '&';
				break;
				
			  case 'hidden':
			  case 'HIDDEN':
				/*alert('hidden');*/
				ret += obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + '&';
				/*alert('hidden2');*/
				break;
				
			  case 'checkbox':
			  case 'CHECKBOX':
				if (obj.childNodes[i].checked) {
					ret += obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + '&';
				} else {
					ret += obj.childNodes[i].name + "=" + '&';
				}
				break;
				
			  case 'radio':
			  case 'RADIO':
				if (obj.childNodes[i].checked) {
					ret += obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + '&';
				}
				break;
			}
			
			break;
			
		  case 'textarea':
		  case 'TEXTAREA':
			ret += obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + '&';
			break;
			
		  case 'select':
		  case 'SELECT':
			var sel = obj.childNodes[i];
			ret += sel.name + "=" + urlencode(sel.options[sel.selectedIndex].value) + '&';
			break;
		}
		/*alert('1');*/
		if(obj.childNodes[i].hasChildNodes()) {/*alert('recursion');*/ ret += form_string(obj.childNodes[i]);}
		/*alert('2');*/
	}
	return(ret);
}

function urlencode(str) {
	/*alert('urlenc1');*/
	str = escape(str);
	str = str.replace(/\+/g, '%2B');
	str = str.replace(/%20/g, '+');
	str = str.replace(/\*/g, '%2A');
	str = str.replace(/\//g, '%2F');
	str = str.replace(/@/g, '%40');
	/*alert('urlenc2');*/
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

function ck_pulldown(mt){
	if (mt.selectedIndex == 0) ret=0; else ret=1;
	return(ret);
}

function ck_field(mt){
	if (mt.value.length<1) ret=0; else ret=1;
	return(ret);
}

/* not yet tested function for adding an event listner - http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/ */
function addEvent(obj,evType,fn,useCapture){
	var ret=false;
	if(obj!=null){
		if(obj.addEventListener){
			obj.addEventListener(evType,fn,useCapture);
			ret=true;
		}
		else if(obj.attachEvent){
			obj.attachEvent("on"+evType,fn);ret=true;
		}
	}
	return ret;
}


function addDiv(parent_element, id, style) {
	var r = document.createElement("div");
	if(id) r.id = id;
	if(style) for(var x in style) r.style[x] = style[x];
	parent_element.appendChild(r);
	return(r);
}


function addElement(element_str, parent_element, id, style) {
	var r = document.createElement(element_str);
	if(id) r.id = id;
	if(style) for(var x in style) r.style[x] = style[x];
	parent_element.appendChild(r);
	return(r);
}


function pageScroll() {
	
	var dt_ref = 10;
	
	if((scrolltime==0) || ((time_ms()-scrolltime_old)>dt_ref*50)){
		scrolltime=time_ms();
		scrolltime_old=scrolltime-dt_ref;
	} else {
		scrolltime_old=scrolltime;
		scrolltime=time_ms();
	}
	dt=scrolltime-scrolltime_old;

	if((scroll_y >=1) || (scroll_y <=-1)) {
		scrolling=1;
		dy=scroll_y;
		if(scroll_vy<100) scroll_vy += 1*(dt/dt_ref);
		if(dy >= scroll_vy*(dt/dt_ref)) dy=scroll_vy*(dt/dt_ref);
		if(dy <= -scroll_vy*(dt/dt_ref)) dy=-scroll_vy*(dt/dt_ref);
		/*if(dy >= scroll_y) dy=scroll_y;
		if(dy <= -scroll_y) dy=-scroll_y;*/
		window.scrollBy(0,dy); // horizontal and vertical scroll increments
		scroll_y -= dy;
		/*alert(dy);*/
		scrolldelay = setTimeout('pageScroll()',10); // scrolls every 100 milliseconds
	} else {
		scroll_y=0;
		scroll_vy=0;
		scrolling=0;
	}
}



function	scroll_element(element_str, down_from_top){
	pos=get_element_position(element_str);
	scroll_vy=0; scroll_y=pos['y']-getScrollY()-down_from_top;
	pageScroll();
}




function mousemove(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX;
		tempY = event.clientY;
  } else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
	document_mouse_x = tempX;
	document_mouse_y = tempY;
	return(true);
}

function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return(scrOfX);
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return(scrOfY);
}

function on_scroll() {
	current_scroll_x=getScrollX();
	current_scroll_y=getScrollY();
	
	if( (current_scroll_y>44) && (top_fixed_flag != 1)) {
		top_fixed_flag=1;
		po=document.getElementById('appt_div');
		po.style.top = 0;
		/*po.style.position='fixed';*/ /*this line was commented out and replaced with the next to fix i.e. bug */
		set_position_fixed(po, 0, 0);
	} else if( (current_scroll_y<44) && (top_fixed_flag == 1)) {
		top_fixed_flag=0;
		po=document.getElementById('appt_div');
		release_position_fixed(po);
		po.style.top = 44;
		po.style.position='absolute';
	}
}


function	get_element_position(div_id){
  var tempDiv = addDiv(document.body, 'temp_hgytjfnbvg', { 'display': 'none', 'position' : 'absolute' } );
	inp = document.getElementById(div_id);
	po=document.getElementById('temp_hgytjfnbvg');
	po.innerHTML='-';
	Position.clone(inp, po, {setHeight: false, setWidth: false, offsetTop: 0, offsetLeft: 0});
	/*alert(parseInt(po.style.left, 10)+' '+parseInt(po.style.top, 10));*/
	remove_element('temp_hgytjfnbvg');
	ret = new Object();
	ret['x'] = parseInt(po.style.left, 10); // the 10 at the end means base 10 integer - 
	ret['y'] = parseInt(po.style.top, 10); // the 10 at the end means base 10 integer - 
	return(ret);
}

function	appt_fn(){
	
	str='';
	
	flag=0;
	for (var contact_id in appt_timestamp_arr) {
		/*alert(appt_timestamp_arr[contact_id]);
		alert(appt_name_arr[contact_id]);*/
		dt=appt_timestamp_arr[contact_id]-time();
		
		//store the current ms remaining in a variable named temp
		temp = dt;
		past=0;
		if(temp<0){
			temp=-temp;
			dt=-dt;
			past=1;
		}
		if((flag==0)&&(past==0)){
			str += date("H:i:s", dt)+' '+appt_name_arr[contact_id]+'<br>';
			flag=1;
		}
		if((past==0)&&(dt<=(60*2))&&(readCookie('appt_close_'+contact_id+'_'+appt_timestamp_arr[contact_id])!=1)&&(appt_overlay_arr[contact_id]!=1)) {
			offset_seconds=parseInt(date("Z", appt_timestamp_arr[contact_id]), 10); // "Z" is the number of seconds from GMT - the 10 at the end means base 10 integer - 
			
			show_overlay('<B>Appointment Reminder</B><br>'+date("h:i A", (appt_timestamp_arr[contact_id]+offset_seconds))+' <A HREF="in_contacts.php?nc='+nc()+'&action=input&id='+contact_id+'&ref_id='+contact_id+'" onclick="createCookie(\'appt_close_'+contact_id+'_'+appt_timestamp_arr[contact_id]+'\', \'1\', .3);">'+appt_name_arr[contact_id]+'</A>', 'createCookie(\'appt_close_'+contact_id+'_'+appt_timestamp_arr[contact_id]+'\', \'1\', .3);');
			/*alert('appt_close_'+contact_id+'_'+appt_timestamp_arr[contact_id]);*/
			appt_overlay_arr[contact_id]=1;
			watchApptClose('appt_close_'+contact_id+'_'+appt_timestamp_arr[contact_id]);
		}
	}
	
	
/*	days = Math.floor(temp/864000);
	temp = temp-(days*864000);
	hr = Math.floor(temp/3600);
	temp = temp-(hr*3600);
	min = Math.floor(temp/60);
	temp = temp-(min*60);
	sec = Math.floor(temp);*/
	div = document.getElementById('appt_div');
	div.innerHTML='<span style="font-size: 11px;">'+str+'</span>';
	
	setTimeout('appt_fn()',1000);
}



function	watchApptClose(cookie_name){
	if(readCookie(cookie_name)==1){
		overlay_html = document.getElementById('overlay_html');
		temp = overlay_html.innerHTML.split(cookie_name);
		if(temp[1]) { /* if the cookie name was found, then the appointment overlay is still open but the cookie is set so it must have been closed in another window */
			hide_overlay();
		}
	} else {
		/*alert('watchApptClose(\''+cookie_name+'\')');*/
		setTimeout('watchApptClose(\''+cookie_name+'\')',1000);
	}
}



function	show_overlay(html, onclose, width){
	if(!onclose) onclose='';
	if(overlay_display_flag==1){
		overlay_buffer[overlay_pointer_push] = html;
		onclose_buffer[overlay_pointer_push] = onclose;
		overlay_pointer_push++;
	} else {
		overlay_display_flag=1;
		if(html=='@^!*~'){ /* this means the last overlay was closed with something on the stack... so pop the next value off of the stack */
			html=overlay_buffer[overlay_pointer_pop];
			onclose=onclose_buffer[overlay_pointer_pop];
			overlay_pointer_pop++;
			if(overlay_pointer_push == overlay_pointer_pop) {
				overlay_pointer_push = 0;
				overlay_pointer_pop = 0;
			}
		}

		/*		  <div id="overlay" style="position:fixed;display:none;background-color:#888;opacity:0.65;filter: alpha(opacity=\'65\');z-index:1000;top:0;left:0;width:100%;height:100%;">.</div>
		  <div id="overlay_html" style="position:fixed;display:none;z-index:1001;top:120;width:100%;"></div>
*/				
		/*alert(navigator.appName);*/
		/************ BEGIN SHIM **************/
		/*if (navigator.appName=="Microsoft Internet Explorer") {
			if(!document.getElementById('overlay_shim')) { // another hack to make up for the poor programming practices of microsoft
			  var overlay_shim = addElement('', document.body, 'overlay_shim', { 'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)', 'width': '100%', 'height': '100%', 'zIndex': '999' });
			} else {
				overlay_shim = document.getElementById('overlay_shim');
			}
			set_position_fixed(overlay_shim, 0, 0);
			overlay_shim.src = 'javascript:false;';
			overlay_shim.scrolling = 'no';
			overlay_shim.style.display = 'inline';
		}*/
		/************* END SHIM *************/
		/*
		if(!document.getElementById('overlay')) {
			var overlay = addDiv(document.body, 'overlay');
			overlay.style.cssText = 'display:none;background-color:#888;opacity:0.65;filter: alpha(opacity=\'65\');z-index:1000;top:0;left:0;width:100%;height:100%;';
		} else {
			overlay = document.getElementById('overlay');
		}
		set_position_fixed(overlay, 0, 0);
	alert(dumpObj(overlay.style, 'overlay.style', '-', 10));
	alert(dumpObj(overlay2.style, 'overlay2.style', '-', 10));
		
		if(!document.getElementById('overlay_html')) {
		  var overlay_html = addDiv(document.body, 'overlay_html', { 'width': '100%', 'zIndex': '1001' });
		} else {
			overlay_html = document.getElementById('overlay_html');
		}
		set_position_fixed(overlay_html, 0, 200);
		
		
		overlay.innerHTML = '<span style="font-size: 81px;">SDK</span>';
		overlay_html.innerHTML = '<center><table cellpadding=0 cellspacing=0 border=1><tr><td width=400 bgcolor=white>\
				<table width="100%"><tr><td align=right>\
		<a href="#" onclick="hide_overlay(); '+onclose+' return(false);"><IMG SRC="img/close.png" WIDTH=18 HEIGHT=18 BORDER=0></a>\
		</td></tr></table>\
		<table cellpadding=2 cellspacing=0 border=0 width="100%"><tr><td width=20></td><td class=body>'+html+'<BR><BR><center></center></td><td width=20></td></tr></table></td></tr></table></center>';
		
		Effect.Appear(overlay2, { duration:0.0 });
		Effect.Appear(overlay_html, { duration:0.0 });*/
		
		
		/*setOpacity(overlay, 0.05);*/
		
		if(!(width)) width=400;
		
		var overlay = document.getElementById('overlay');
		var overlay_html = document.getElementById('overlay_html');
		set_position_fixed(overlay, 0, 0);
		set_position_fixed(overlay_html, 0, 100);
		overlay_html.innerHTML = '<center><table cellpadding=0 cellspacing=0 border=1><tr><td width='+width+' bgcolor=white>\
				<table width="100%"><tr><td align=right>\
		<a href="#" onclick="hide_overlay(); '+onclose+' return(false);"><IMG SRC="img/close.png" WIDTH=18 HEIGHT=18 BORDER=0></a>\
		</td></tr></table>\
		<table cellpadding=2 cellspacing=0 border=0 width="100%"><tr><td width=20></td><td class=body>'+html+'<BR><BR><center></center></td><td width=20></td></tr></table></td></tr></table>';
		overlay.style.display = 'inline';
		overlay_html.style.display = 'inline';

	}
}

function	hide_overlay(){
	var overlay = document.getElementById('overlay');
	var overlay_html = document.getElementById('overlay_html');
	overlay_html.style.display = 'none';	
	overlay.style.display = 'none';	
	overlay_html.innerHTML = '';
	overlay_display_flag=0;
	if(overlay_pointer_push != overlay_pointer_pop) show_overlay('@^!*~');
}

function setOpacity(element, value) {
	element.style.opacity = value;
	element.style.MozOpacity = value;
	element.style.filter = 'alpha(opacity=\'' + value*100 + '\')';
	element.style.width = '50%';
	alert(element.style.position);
	alert(dumpObj(element.style, 'element.style', '-', 10));
}

function	set_position_fixed(element, x, y){ /* ie doesn't support position:fixed; */
	if (element.style.setExpression) { // ie
		element.style.position = 'absolute';
		element.style.setExpression('left', x+"+current_scroll_x+'px'"); 
		element.style.setExpression('top', y+"+current_scroll_y+'px'");
		
		document.body.style.backgroundImage = "url('img/forceprint.gif')"; 
		document.body.style.backgroundAttachment = 'fixed'; 
	} else {
		element.style.position = 'fixed'; // !ie
		element.style.left = x; // !ie
		element.style.top = y; // !ie
	}
}

function	release_position_fixed(element){ /* release ie specific functions; */
	if (element.style.setExpression) { // ie
		element.style.removeExpression('top');
		element.style.removeExpression('left');
	}
}




var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;
setFocusEvents();

function setFocusEvents()	{
	active_element = document.activeElement;
	if (isIE)	{
		document.onfocusout = function() {	onWindowBlur();	      }
		document.onfocusin = function()	 {	onWindowFocus();     }
	}	else	{
		window.onblur = function()	  {	onWindowBlur();	         }
		window.onfocus = function()	 {	onWindowFocus()       }
	}
}

function onWindowFocus()	{
	/*if(!(hasFocus)) show_overlay('focus');*/
	hasFocus = true;
}

function onWindowBlur()	{
	if (active_element != document.activeElement) {
		active_element = document.activeElement;
		return;
	}
	hasFocus = false;
	/*show_overlay('blur');*/
}

function tz_test(tz_server, ds_server){
var visitortime = new Date();
	tz_daylightsavingadjusted = -visitortime.getTimezoneOffset()/60;
	tz = get_time_zone();
	if(tz_daylightsavingadjusted != tz) ds=1; else ds=0;
	if((tz!=tz_server)||(ds!=ds_server)) new Ajax.Request('in_contacts.php?nc='+nc()+'&action=ajax_tz_ds&tz='+tz+'&ds='+ds, {method: 'get', asynchronous:true, parameters:'', onSuccess:RxAjax, onFailure:errFunc});
}


function get_time_zone(){
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 100;
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;
     for (intMonth=0;intMonth < 12;intMonth++){
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
            intOffset = (dtDate.getTimezoneOffset() * (-1));
        }
    } 
    return (intOffset/60);
}

