var order  = new String();
var isdesc = new Boolean();
var page   = new Number(0);
var locked = new Boolean();

function func_product(productid, e) {

	var query 	  	 = parse_queries(this.location.search);
	var patientid 	 = new Number();
	var type		 = new Number();
	var attachedtype = new Number();
	var isAttache	 = new Boolean();

	if (query['patientid'] != null) 	patientid 	 = query['patientid'];
	if (query['type'] != null) 			type 	  	 = query['type'];
	if (query['isattache'] != null) 	isAttache 	 = true;

	if (!isAttache.valueOf()) {
		open_product_editor(productid, patientid, type, e);
	}
}

function open_product_editor(id, patientid, type, e) {

	var url = 'edit.asp' +
			  '?id=' 			+ id +
			  '&patientid=' 	+ patientid +
			  '&type=' 			+ type;
	//document.getElementById('frm_editor').document.open();
	//document.getElementById('frm_editor').document.write('Loading...');
	//document.getElementById('frm_editor').document.close();
		
    obj = document.getElementById("div_editor");
   
    document.getElementById('div_editor').style.display = 'block';
        
    if(!e) var e = e || window.event;
    
    if(navigator.appName == "Microsoft Internet Explorer") {
		wtop = e.clientY + document.body.scrollTop + 10;
		    
		if (wtop + 370 > document.body.clientHeight && document.body.clientHeight > 370) {
		    wtop = document.body.clientHeight - 370;
		}
    } else  {
		wtop = e.clientY + window.pageYOffset + 10;
		    
		if (wtop + 370 > window.innerHeight && window.innerHeight > 370) {
		    wtop = window.innerHeight - 370;
		}
    }
        
	//obj.style.top = wtop + "px";
	obj.style.top = 50 + "px";
    obj.style.left = 70 + "px";
    obj.style.height = 250 + "px";
    obj.style.width = 800 + "px";
        
   	if(navigator.appName == "Microsoft Internet Explorer")
		frm_editor.location.href = url;
	else
		document.getElementById('frm_editor').src = url;
	
}
function open_product_selector(patientid, attachedtype) {

	var url = 'index.asp' +
			  '?isSearch=1' +
			  '&contactid=' + patientid +
			  '&attachedtype=' + attachedtype;

	frm_editor.document.open();
	frm_editor.document.write('Loading...');
	frm_editor.document.close();
	document.getElementById('div_editor').style.left = 60;
	document.getElementById('div_editor').style.display = 'block';
	document.getElementById('div_editor').style.top = 20;
	frm_editor.location.href = url;
}

function contact_remove(productid, patientid, attachedtype) {

	if (window.confirm('Are you sure to detach this person?')) {
		var url = 'portfolio.asp' +
				  '?productid=' 	+ productid +
				  '&patientid=' 	+ patientid +
				  '&detache';

		AjaxCaller.getText(url, '', display);

		function display(ret_ajax) {
			refresh_list();
			closewin();
		}
	} else {
		return;
	}
}

function closewin() {

	/* Added 11/06/2008 */
	if (window.name == 'item') {
		window.close();
		return;
	}
	
	if (window.opener) {
		history.back(-1);
		return;
	}

	if (parent.document.getElementById('div_editor')) {
		if (parent.document.getElementById('div_editor').style.display == 'block') {
			parent.document.getElementById('div_editor').style.display = 'none';
		}
	}

	if (parent.document.getElementById('editWin2')) {
		history.back(-1);
		return;
	}

	if (parent.document.getElementById('div_product')) {
		parent.document.getElementById('div_product').style.display = 'none';
	}
}

function refresh_list() {
	if (parent.location.href.match(/isSearch=/gi) != null) {
		parent.search();
	} else {

		var url = parent.location.href;
			url = url.replace(/isNew=1/gi, '');

		parent.location.href = url;
	}
}

function page_lock(val) {

	if (val.valueOf()) {
		if (document.getElementById('btn_delete') != null) {
			document.getElementById('btn_delete').disabled = true;
		}
		if (document.getElementById('btn_save_as_new') != null) {
			document.getElementById('btn_save_as_new').disabled = true;
		}
		if (document.getElementById('btn_detach') != null) {
			document.getElementById('btn_detach').disabled = true;
		}
		document.getElementById('btn_save').disabled = true;
		document.getElementById('btn_cancel').disabled = true;
		document.getElementById('div_updating').style.display = 'block';
	} else {
		if (document.getElementById('btn_delete') != null) {
			document.getElementById('btn_delete').disabled = false;
		}
		if (document.getElementById('btn_save_as_new') != null) {
			document.getElementById('btn_save_as_new').disabled = false;
		}
		if (document.getElementById('btn_detach') != null) {
			document.getElementById('btn_detach').disabled = false;
		}
		document.getElementById('btn_save').disabled = false;
		document.getElementById('btn_cancel').disabled = false;
		document.getElementById('div_updating').style.display = 'none';
	}
}

function save(id, patientid, type) {

	if (type == 'SAVE_AS_NEW') {
		if (!window.confirm('Are you sure to duplicate this listing as new?')) return;
	}
		
	var is_new = new Boolean();
	if (id.valueOf() == 0) is_new = true;

	page_lock(true);

	if (type == 'SAVE' || type == 'SAVE_AS_NEW') {
		try {
			var retval = validate(id, patientid);
			if (!retval.valueOf()) {
				page_lock(false);
				return;
			}
		} catch(e) {

		}
	} else if (type == 'DEL') {
		if (!window.confirm('Are you sure to delete?')) {
			page_lock(false);
			return;
		}
		document.getElementById('isdeleted').value = 1;
	}
	submit(id, patientid, is_new);
}

function submit(id, patientid, is_new) {

	var url = new String(document.getElementById('frm_contact').src);
		url = url.replace('_edit.asp', '_save.asp');
		
	var chromeExist = navigator.userAgent.indexOf("Chrome",0);
	if((navigator.appName == "Microsoft Internet Explorer") || (chromeExist != -1))
		AjaxCaller.postText(url, get_post_values(frm_contact, 'form_editor'), display);
	else 
		AjaxCaller.postText(url, get_post_values(document.getElementById('frm_contact'), 'form_editor'), display);

	function display(patientid) {
		if (isNaN(patientid)) {
			alert('Error: Cannot Save Contact.');
			return;
		} else {
			var isNew = new Number(is_new);
			var url = 'save.asp' +
					  '?id=' + id +
					  '&patientid=' + patientid +
					  '&companyid=' + companyid +
					  '&is_new=' + isNew +
					  '&companyname=' + companyname;
			function postdata(productid) {
				if (isNaN(productid)) {
					alert('Error: Cannot Save Products\n' + productid);
					return;
				} else {
					if (is_new.valueOf()) {
						function return_trigger(retval, errmsg) {
							if (retval.valueOf() != -1) {
								
							} else if (retval.valueOf() == -1) {
								alert('Error on Event Triger: [' + errmsg + ']');
							}
						}
						event_trigger('NEW_PRODUCT', 0, productid, return_trigger);
					} else {
						/* Client base events in Update Action */
						try {
							trigger_updating(productid);
						} catch(e) {
						}
					}
					
					var url = new String(this.location.href);
					url = url.replace(/([\?|&]+)id=\d+/gi, '$1id=' + productid);
					this.location.href = url;
					
					/* Modified: 11/06/2008 */
					//refresh_list();
					if (parent.location.pathname  != this.location.pathname) {
						refresh_list();
					} else {
						if (window.opener) window.opener.switchto(16,4);
					}
				}
			}
			AjaxCaller.postText(url, get_post_values(this, 'form_editor'), postdata);
		}
	}
}

function tab_switch(tab_id) {
	if(navigator.appName == "Microsoft Internet Explorer") {
		var tab_num = new Number(document.all('page_tab').length);

		if (!document.all('page_div')[tab_id]) return;

		for (i=0; i<tab_num; i++) {
			if (document.all('page_div')[i]) {
				document.all('page_div')[i].style.display = 'none';
				document.all('page_tab')[i].className 	  = 'tab_off';
			}
		}
		document.all('page_div')[tab_id].style.display = 'block';
		document.all('page_tab')[tab_id].className 	   = 'tab_on';
		
	} else {
		
		var pagedivs = getObjectsByTagAndClass('td','tab_off')
		//alert(pagedivs.length);
		var divs = getObjectsByTagAndClass('div','popupDiv');  
		if (divs != undefined && divs != null)  
		{    
			for (var i = 0; i < divs.length; i++)    
			{      
				if (divs[i].id)  {     
					divs[i].style.display = 'none';  
					pagedivs[i].style.cursor = "pointer";
					pagedivs[i].style.backgroundColor = "#C0C0C0";
					pagedivs[i].style.color = "#000000";
				
					//pagedivs[i].className 	  = 'tab_off';        
				}
			} 
			divs[tab_id].style.display = 'block';
			pagedivs[tab_id].style.cursor = "pointer";
			pagedivs[tab_id].style.backgroundColor = "#93A6DD";
			pagedivs[tab_id].style.color = "#FFFF00";
			//pagedivs[tab_id].className 	   = 'tab_on';
		}
	}
	
	if((tab_id == 6) && (userlevel == 1)) {
		document.getElementById('int0').disabled = true;
		document.getElementById('int1').disabled = true;
		document.getElementById('icon_featured').disabled = true;
		document.getElementById('date1').disabled = true;
	}
}

function getObjectsByTagAndClass(tag, cls) 
{  
	var array = document.getElementsByTagName(tag); 
	var output = new Array(); 
	var matches = 0; 
	for(var i=0;i<array.length;i++) {
		var parts = array[i].className.split(' '); 
		for(var j=0;j<parts.length;j++) 
		{ 
			if(parts[j]==cls) 
			{ 
				output[matches++]=array[i]; 
			} 
		}
	}
	return output; 
} 

function load_states(companyid, status, fieldid, selectedid) {

	if (!document.getElementById(fieldid)) return;

	var title = new String(document.getElementById(fieldid).options[0].text);

	document.getElementById(fieldid).options.length = 1;
	document.getElementById(fieldid).options[0].value = '';
	document.getElementById(fieldid).options[0].text  = 'Loading...';
	document.getElementById(fieldid).disabled = true;

	var url = '/application/products/state_county.asp' +
			  '?status=' + status +
			  '&companyid=' + companyid;
	AjaxCaller.getXml(url, '', build_options);

	function build_options(list) {

		if (list.documentElement == null) return;

		document.getElementById(fieldid).options[0].value = '';
		document.getElementById(fieldid).options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {
		
			if (list.documentElement.childNodes.item(i).nodeType == 3) continue;

			var id   = new Number(list.documentElement.childNodes.item(i).getAttribute('ID'));
			var code = new String(list.documentElement.childNodes.item(i).getAttribute('CODE'));
			var name = new String(list.documentElement.childNodes.item(i).childNodes.item(0).nodeValue);
			
			document.getElementById(fieldid).options.length++;
			var optlen = document.getElementById(fieldid).options.length - 1;
			document.getElementById(fieldid).options[optlen].value = id;
			document.getElementById(fieldid).options[optlen].text  = name;
			if (selectedid == id) {
				document.getElementById(fieldid).selectedIndex = optlen;
			}
		}
		document.getElementById(fieldid).disabled = false;
	}
}

function load_counties(companyid, status, stateid, fieldid, selectedid) {

	if (!document.getElementById(fieldid)) return;

	var title = new String(document.getElementById(fieldid).options[0].text);

	document.getElementById(fieldid).options.length = 1;
	document.getElementById(fieldid).options[0].value = '';
	if (stateid == '') {
		document.getElementById(fieldid).options[0].text  = title;
		return;
	} else {
		document.getElementById(fieldid).options[0].text  = 'Loading...';
		document.getElementById(fieldid).disabled = true;
	}

	var url = '/application/products/state_county.asp' +
			  '?status=' + status +
			  '&stateid=' + stateid +
			  '&companyid=' + companyid;
	AjaxCaller.getXml(url, '', build_options);

	function build_options(list) {

		if (list.documentElement == null) return;

		document.getElementById(fieldid).options[0].value = '';
		document.getElementById(fieldid).options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {
		
			if (list.documentElement.childNodes.item(i).nodeType == 3) continue;
			
			var id   = new Number(list.documentElement.childNodes.item(i).getAttribute('ID'));
			var name = new String(list.documentElement.childNodes.item(i).childNodes.item(0).nodeValue);
			
			document.getElementById(fieldid).options.length++;
			var optlen = document.getElementById(fieldid).options.length - 1;
			document.getElementById(fieldid).options[optlen].value = id;
			document.getElementById(fieldid).options[optlen].text  = name;
			if (selectedid == id) {
				document.getElementById(fieldid).selectedIndex = optlen;
			}
		}
		document.getElementById(fieldid).disabled = false;
	}
}

function load_options(type, fieldid, selectedid) {

	if (!document.getElementById(fieldid)) return;

	var url		= new String('/application/functions/xml_option_list.asp');
	var type	= type.toUpperCase();
	var valueid = new String();
	var textid 	= new String();
	var title 	= new String(document.getElementById(fieldid).options[0].text);

	document.getElementById(fieldid).options.length = 1;
	document.getElementById(fieldid).options[0].value = '';
	document.getElementById(fieldid).options[0].text  = 'Loading...';
	document.getElementById(fieldid).disabled = true;

	switch (type) {
		case 'DOCTORS':
			if (typeof(arguments[3]) == 'undefined') return;
			valueid = 'DOCTORID';
			textid  = 'DOCTORNAME';
			url += '?table=doctors' +
			  	   '&centers.id=' + arguments[3] +
			  	   '&companyid=' + companyid +
			  	   '&order=lname';
			break;
		case 'CATEGORIES':
			valueid = 'CONDITIONID';
			textid  = 'CONDITION';
			url += '?table=categories' +
			  	   '&order=id,parentid,conditionid' +
			  	   '&companyid=' + companyid +
			  	   '&type=1';
			break;
		case 'STATE':

		default:
	}
	AjaxCaller.getXml(url, '', build_options);

	function build_options(list) {

		if (list.documentElement == null) return;

		document.getElementById(fieldid).options[0].value = '';
		document.getElementById(fieldid).options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {

			if (list.documentElement.childNodes.item(i).nodeType == 3) continue;
			
			var value = new Number(list.documentElement.childNodes.item(i).getAttribute(valueid));
			var text  = new String(list.documentElement.childNodes.item(i).getAttribute(textid));

			document.getElementById(fieldid).options.length++;
			var optlen = document.getElementById(fieldid).options.length - 1;
			document.getElementById(fieldid).options[optlen].value = value;
			text =  unescape(text);
			text =  text.replace("&amp;", "and");  
			document.getElementById(fieldid).options[optlen].text  = text;

			if (type.valueOf() == 'CATEGORIES') {
				var parentid = new Number(list.documentElement.childNodes.item(i).getAttribute('PARENTID'));
				if (parentid.valueOf() == '') {
					document.getElementById(fieldid).options[optlen].style.color = 'blue';
					document.getElementById(fieldid).options[optlen].style.backgroundColor = '#aaaaaa';
				}
			}

			if (selectedid.valueOf() == value.valueOf()) {
				document.getElementById(fieldid).selectedIndex = optlen;
			}
		}
		document.getElementById(fieldid).disabled = false;
	}
}

function load_products(query) {

	document.getElementById('div_product_list').innerHTML = '<span class="text11">Loading...</span>';

	//var url = 'list.asp?' + query + '&page=' + page;
	var url = 'list.asp?' + query;

	/* Debug 
	var debug_win = window.open('debug_win');
		debug_win.location.href = url;
	return;		
	*/
	
	AjaxCaller.getText(url, '', display);

	function display(ret_ajax) {

		var re = new RegExp('<!--{TOTAL_COUNT:([^}]+)}-->', 'gi');
		ret_ajax.match(re);
		var count = new Number(RegExp.$1);

		draw_paging(count);

		if (count == 0) ret_ajax = '';
		document.getElementById('div_product_list').innerHTML = ret_ajax;
		if (document.getElementById('div_grid_data') != null) {
			if (window.name.toLowerCase() == 'rightframe') {
				if(navigator.appName == "Microsoft Internet Explorer") 
					document.getElementById('div_grid_data').style.height = (parseInt(parent.document.body.clientHeight) - 195);
				else 
					document.getElementById('div_grid_data').style.height = (parseInt(parent.window.innerHeight) - 195);
			} else {
				document.getElementById('div_grid_data').style.height = 237 + 'px';
			}
		}
		
		// Startup function
		try {
			init_product_list_load();
		} catch(e) {

		}
	}
}

function draw_paging(count) {

	var tabmax  = new Number(10);
	var listmax = new Number(50);
	//var tabpage = new Number((page/tabmax).toFixed());
	var tabpage = parseInt(page/tabmax);
	var html    = new String('<table width="100%" bgcolor="#888888" border="0" cellspacing="0" cellpadding="0" class="text10">');

	if (count == 0) {
		html += '<td width="100%" align="right">Found:' + count + '&nbsp;</td></tr></table>';
	} else {

		if (tabpage > 0) {
			html += '<td nowrap onclick="paging(0);" class="border" style="width:30px; text-align:center; cursor:hand;">Top</td>\n' +
				    '<td nowrap onclick="paging(' + ((tabpage) * tabmax - 1) + ');" class="border" style="width:30px; text-align:center; cursor:pointer;"><<</td>\n';
		}

		var islast = new Boolean();
		for (i=0; i<tabmax; i++) {

			var num_from  = new Number(i * listmax + 1 + (tabpage * tabmax * listmax));
			var num_to    = new Number((i + 1) * listmax + (tabpage * tabmax * listmax));
			var highlight = (page == i + (tabpage * tabmax)) ? '#ffff00' : '#cccccc';

			if (num_to > count) {
				num_to = count;
				islast = true;
			}

			html += '<td nowrap ' +
					'    class="border" ' +
					'    onclick="paging(' + (i + (tabpage * tabmax)) + ');" ' +
					'    style="width:60px; text-align:center; cursor:pointer; background-color:' +  highlight + '">' +
					num_from + '-' + num_to + '</td>\n';

			if (islast.valueOf()) break;
		}

		if (!islast.valueOf()) {
			html += '<td nowrap onclick="paging(' + ((tabpage + 1) * tabmax) + ');" class="border" style="width:30px; text-align:center; cursor:pointer;">>></td>\n' +
			    	//'<td nowrap onclick="paging(' + ((count / listmax).toFixed() - 1) + ');" class="border" style="width:30px; text-align:center; cursor:pointer;">Last</td>\n';
					'<td nowrap onclick="paging(' + (parseInt(count / listmax) - 1) + ');" class="border" style="width:30px; text-align:center; cursor:pointer;">Last</td>\n';
					
		}

		html += '<td align="right">Found:' + count + '&nbsp;</td></tr>' +
				'</table>';
	}
	document.getElementById('div_paging').innerHTML = html;
}


function paging(num) {
	page = num;
	var queries = setquery(document.getElementById('queries').value, 'page', page);
	load_products(queries);
}

function load_contact_list(productid) {

	var url = 'contact_list.asp' +
			  '?productid=' + productid;

	AjaxCaller.getText(url, '', display);

	function display(ret_ajax) {
		var obj = document.getElementById('div_contact_list');
		obj.innerHTML = ret_ajax;
	}
}

function load_saved_documents(productid) {

	var url = 'document/list_saved_documents.asp' +
			  '?productid=' + productid;

	AjaxCaller.getText(url, '', display);

	function display(ret_ajax) {
		var obj = document.getElementById('div_documents');
		obj.innerHTML = ret_ajax;
	}
}

function load_workflow(productid) {

	listid = productid;

	var url = '/application/workflow/contact_workflow.asp' +
			  '?listid=' + productid +
			  '&listtype=PRODUCT';
	if(navigator.appName == "Microsoft Internet Explorer")
		div_workflow.location.href = url;
	else
		document.getElementById('div_workflow').src = url;
}

function load_traffic(productid) {
	var url = '/application/products/trafficstatistics/index.asp' +
			  '?id=' + productid;
	if(navigator.appName == "Microsoft Internet Explorer")
		div_traffic.location.href = url;
	else
		document.getElementById('div_traffic').src = url;
}

function load_faxes(productid) {

	var url = 'fax_list.asp' +
			  '?productid=' + productid;

	AjaxCaller.getText(url, '', display);

	function display(ret_ajax) {
		var obj = document.getElementById('div_fax_list');
		obj.innerHTML = ret_ajax;
	}
}

function openfax(faxid) {
	var url = '../fax/viewFax.html?id=' + faxid;
	var faxwin = window.open(url,'faxwin','resizable=yes,scrollbars=yes,width=640,height=520');
}

function contactedit(contid, type, openwhere){ // contid, type, openwhere 0 - inline 1 - new window

    str = "contid="+contid+"&type="+type;
    if (arguments.length > 3) {
      str = "../contacts/contact_items.asp?" + str + "&tabid=" + arguments[3];
    }
    else {
      str = "../contacts/contact_info.asp?" + str;
    }

    var wtop;

    if (openwhere == 0) {
        obj = document.getElementById("editWin2");
        obj.style.visibility = "hidden";

        obj = document.getElementById("editWin");
        obj.style.visibility = "visible";

        wtop = event.clientY + document.body.scrollTop + 10;

        if (wtop + 370 > document.body.clientHeight && document.body.clientHeight > 370) {
            wtop = document.body.clientHeight - 370;
        }

        obj.style.top = wtop;//event.clientY + document.body.scrollTop + 10;
        obj.style.left = 70;
        obj.style.height = 370;
        obj = document.getElementById("editFrame");
        obj.src = str + "&winid=1";
    }
    else{
        ct = window.open(str+"&winid=4",'contact','resizable=yes,scrollbars=yes,width=640,height=520');
        ct.focus();
    }
    if (contid == 0) {
        obj = document.getElementById("newit");
        obj.style.visibility='hidden';
    }
}

function preview_new_document(productid) {

	var templateid = new String(document.getElementById('templateid').value);
	if (templateid == '') return;

	var url = 'document/preview.asp' +
			  '?productid=' + productid +
			  '&templateid=' + templateid;
			  
	var doc_win = window.open(url, 'doc_win', 'resizable=yes,scrollbars=yes,width=800,height=600');
	doc_win.focus();
}

function preview_saved_document(id) {

	var url = 'document/preview.asp' +
			  '?id=' + id;

	var doc_win = window.open(url, 'doc_win', 'resizable=yes,scrollbars=yes,width=800,height=600');
	doc_win.focus();
}

function open_contact(id) {

	var url = '../contacts/contact_info.asp?contid=' + id + '&type=0&winid=1';

	parent.frm_editor2.document.open();
	parent.frm_editor2.document.write('Loading...');
	parent.frm_editor2.document.close();
	parent.document.getElementById('div_editor2').style.left = 60;
	parent.document.getElementById('div_editor2').style.display = 'block';
	parent.document.getElementById('div_editor2').style.top = parent.document.getElementById('div_editor').style.top;
	parent.frm_editor2.location.href = url;
}

function cancel() {	// for closing the contact editor
	document.getElementById('div_editor2').style.display = 'none';
}

function orderby(field) {
	order = new String(field);
	isdesc = (isdesc.valueOf()) ? false : true;
	page = 0;
	search();
}

/* Action with Selection */

var selected = new Array();

function show_action_menus(evt) {
	var e=(evt)?evt:window.event;
	if (document.getElementById('div_action_menus').style.display == 'block') {
		document.getElementById('div_action_menus').style.display = 'none';
	} else {
		document.getElementById('div_action_menus').style.display = 'block';
	}
	var top =  e.clientY + 10;
	var left = 600;//document.body.clientWidth - parseInt(document.getElementById('div_action_menus').style.width);
	document.getElementById('div_action_menus').style.top = top + "px";
	document.getElementById('div_action_menus').style.left = left + "px";
}

/*function load_group_list() {

	var obj = document.getElementById('listid');
	var title = new String(obj.options[0].text);

	obj.options.length = 1;
	obj.options[0].value = '';
	obj.options[0].text  = 'Loading...';
	obj.disabled = true;
	var text = new String();
	text = 'ID';

	var url = 'action_selection.asp' +
			  '?type=LOAD_GROUP';

	AjaxCaller.getXml(url, '', display);

	function display(list) {

		if (list.documentElement == null) return;

		obj.options[0].value = '';
		obj.options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {
			if(navigator.appName == "Microsoft Internet Explorer") {
				var id   = new Number(list.documentElement.childNodes[i].getAttribute('ID'));
				var name = new String(list.documentElement.childNodes[i].text);
				obj.options.length++;
				obj.options[i + 1].value = id;
				obj.options[i + 1].text  = unescape(name);
			} else {
				i = i + 1;	
				var id   = new Number(list.documentElement.childNodes[i].getAttribute(text));
				var name = new String(list.documentElement.childNodes[i].text);
				obj.options.length++;
				var optlen = obj.options.length - 1;
				obj.options[optlen].value = id;
				obj.options[optlen].text  = unescape(name);
			}
		}
		obj.disabled = false;
	}
}*/

function load_saved_list() {

	if (document.getElementById('listid').value == '') {
		alert('Please Select Group.');
		return;
	}
	page = 0;
	load_products('listid=' + document.getElementById('listid').value + '&');
	show_action_menus();
}

function check_selection(productid, obj) {

	selected[productid] = new Number();

	if (obj.checked) {
		selected[productid] = 1;
		action_with_selection('APPEND', productid);
	} else {
		action_with_selection('REMOVE', productid);
	}
	count_selected();
}

function add_workflow(type) {

	if (type.valueOf() == 0) {
		if (selected.length == 0) {
			alert('No Selected Items.');
			return;
		}
		var tmp_listid = listid;
	} else {
		if (document.getElementById('listid').value == '') {
			alert('Please Select Group.');
			return;
		}
		var tmp_listid = document.getElementById('listid').value;
	}

	var url = '../workflow/add_contacts.asp' +
			  '?listid=' + tmp_listid +
			  '&contacttype=-1';

	var ht = document.body.clientHeight;
    var wt = document.body.clientWidth;
    var dh = 150;
    var dw = 300;
    var dvoffs = 80;
    var left = (wt < dw) ? 0 : (wt - dw) / 2;
    
	document.getElementById('editWin4').style.visibility = "visible";
    document.getElementById('editWin4').style.top = 30 + "px";
    document.getElementById('editWin4').style.left = left + "px";
    document.getElementById('editWin4').style.height = dh + "px";
    document.getElementById('editWin4').style.width = dw + "px";
    if(navigator.appName == "Microsoft Internet Explorer")
		editFrame4.location.href = url;
	else
		document.getElementById('editFrame4').src = url;
}

function action_with_selection(type,evt) {
	var e=(evt)?evt:window.event;
	var url = new String();
		url = 'action_selection.asp' +
			  '?type=' + type +
			  '&listid=' + listid;

	switch(type) {

		case 'APPEND': case 'REMOVE':
			//var productid = new Number(arguments[1]);
			var productid = new String(arguments[1]);
			url += '&productid=' + productid;
			break;

		case 'CLEAR':
			selected = new Array();
			count_selected();
			break;

		case 'DELETE_GROUP':
			if (document.getElementById('listid').value == '') {
				alert('Please Select Group.');
				return;
			}
			if (!window.confirm('Are you sure to delete?')) return;
			url = url.replace(/&listid=(\d)*/i, '&listid=' + document.getElementById('listid').value);
			break;

		case 'SAVE_GROUP':
			if (selected.length == 0) {
				alert('No Selected Items.');
				return;
			}

			do{
				listname = prompt("Save As:","");
			} while(listname != null && listname == "");
			if (listname == null) return;
			url += '&listname=' + listname;
			break;

		case 'ATTACH_CONTACT':
			if (selected.length == 0) {
				alert('No Selected Items.');
				return;
			}
			if (!window.confirm("Are you sure to add to the portfolio?")) return;

			var patientid 	= new Number(arguments[1]);
			var attachtype	= new Number(arguments[2]);
			url += '&attachtype=' + attachtype + '&patientid=' + patientid;
			break;
		
		case 'BROCHURE':
			if (document.getElementById('div_templates').style.display == 'block') {
				document.getElementById('div_templates').style.display = 'none';
				return;
			}
			var top = e.clientY;
			var left = e.clientX - 400;
			document.getElementById('div_templates').style.top = top + "px";
			document.getElementById('div_templates').style.left = left + "px";
			document.getElementById('div_templates').style.display = 'block';
			if(navigator.appName == "Microsoft Internet Explorer")
				frm_templates.location.href = '/application/autoRespond/tools/template_list.asp';
			else
				document.getElementById('frm_templates').src = '/application/autoRespond/tools/template_list.asp'; 
			break;
			
		default:

	}

	AjaxCaller.getText(url, '', display);

	function display(ret_ajax) {
		if (type.valueOf() == 'DELETE_GROUP' ||
			type.valueOf() == 'SAVE_GROUP') {
			load_group_list();
		} else if (type.valueOf() == 'ATTACH_CONTACT') {
			closewin();
		}
	}
}

function count_selected() {

	for (i=0; i<document.getElementsByName('check').length; i++) {
		document.getElementsByName('check')[i].checked = false;
		if (selected[document.getElementsByName('check')[i].value] == 1) document.getElementsByName('check')[i].checked = true;
	}

	var num = new Number();
	for (id in selected) {
		if (selected[id].valueOf() == 1) num++;
	}
	document.getElementById('selected_num').innerHTML = num;
}

function allreset(obj) {

	var arr_id = new Array();
	var isnew = new Number();

	if (obj.checked) isnew = 1;

	for (i=0; i<document.getElementsByName('check').length; i++) {

		var id		  = document.getElementsByName('check')[i].value;
		var ischecked = document.getElementsByName('check')[i].checked;

		selected[id] = new Number(isnew);

		if ((isnew == 1 && ischecked.valueOf() == false) ||
			(isnew == 0 && ischecked.valueOf() == true)) arr_id.push(id);
	}
	count_selected();
	
	var str_ids = new String();
	for (var key in arr_id) {
		if (str_ids != "") str_ids += ",";
		str_ids += arr_id[key];
	}
	
	if (obj.checked) {
		action_with_selection('APPEND', str_ids);
	} else {
		action_with_selection('REMOVE', str_ids);
	}
}


function load_group_list() {

	if (!document.getElementById('listid')) return;
	var obj = document.getElementById('listid');
	var title 	= new String(obj.options[0].text);
	var fieldId = new String();

	obj.options.length = 1;
	obj.options[0].value = '';
	obj.options[0].text  = 'Loading...';
	obj.disabled = true;
	fieldId = 'ID';

	var url = 'action_selection.asp' +
		 '?type=LOAD_GROUP';
	
	AjaxCaller.getXml(url, '', build_options);

	function build_options(list) {

		if (list.documentElement == null) return;

		obj.options[0].value = '';
		obj.options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {
			
			if (list.documentElement.childNodes.item(i).nodeType == 3) continue;
			
			var id  = new Number(list.documentElement.childNodes.item(i).getAttribute(fieldId));
			var name = new String(list.documentElement.childNodes[i].firstChild.nodeValue);
			obj.options.length++;
			var optlen = obj.options.length - 1;
			obj.options[optlen].value = id;
			obj.options[optlen].text  = unescape(name);
		}
		obj.disabled = false;
	}
}

function cancelEvent(evt) {
	var e=(evt)?evt:window.event;	if (window.event) {
	    e.cancelBubble=true;
    } else {
	    e.stopPropagation();
	}
}


function load_category(type, fieldid, selectedid) {

	if (!document.getElementById(fieldid)) return;

	var url		= new String('/application/functions/xml_option_list.asp');
	var type	= type.toUpperCase();
	var valueid = new String();
	var textid 	= new String();
	var title 	= new String(document.getElementById(fieldid).options[0].text);

	document.getElementById(fieldid).options.length = 1;
	document.getElementById(fieldid).options[0].value = '';
	document.getElementById(fieldid).options[0].text  = 'Loading...';
	document.getElementById(fieldid).disabled = true;
	
	valueid = 'CONDITIONID';
	textid  = 'CONDITION';
	url += '?table=categories' +
			'&order=id,parentid,conditionid' +
			'&companyid=' + companyid +
			'&type=1';
	
	AjaxCaller.getXml(url, '', build_options);

	function build_options(list) {

		if (list.documentElement == null) return;

		document.getElementById(fieldid).options[0].value = title;
		document.getElementById(fieldid).options[0].text = title;

		for (var i=0; i<list.documentElement.childNodes.length; i++) {

			if (list.documentElement.childNodes.item(i).nodeType == 3) continue;
			
			var text  = new String(list.documentElement.childNodes.item(i).getAttribute(textid));

			document.getElementById(fieldid).options.length++;
			var optlen = document.getElementById(fieldid).options.length - 1;
			document.getElementById(fieldid).options[optlen].value = unescape(text);
			document.getElementById(fieldid).options[optlen].text  = unescape(text);

			if (type.valueOf() == 'CATEGORIES') {
				var parentid = new Number(list.documentElement.childNodes.item(i).getAttribute('PARENTID'));
				if (parentid.valueOf() == '') {
					document.getElementById(fieldid).options[optlen].style.color = 'blue';
					document.getElementById(fieldid).options[optlen].style.backgroundColor = '#aaaaaa';
				}
			}

			/*if (selectedid.valueOf() == value.valueOf()) {
				document.getElementById(fieldid).selectedIndex = optlen;
			}*/
		}
		document.getElementById(fieldid).disabled = false;
	}
}