var widgets = { graph:'/cgi-bin/infxwidgets/widget-excel-graph.pl', directory:'/cgi-bin/infxwidgets/widget-global-directory.pl' };

function letter_filter(letter){
	var obj = rtnEl('search-letter');
	if(obj){
		obj.value = letter;
		filter_results('letter');
	}
}

function load_graph_popup(gid){
	$.ajax({
		url: widgets.graph,
		type:'post',
		data: {'ac':'graph','mode':'popup','gid':gid},
		success: function(data) {
			action_callback(data);
		}
	});
}

function load_directory_popup(id){
	$.ajax({
		url: widgets.directory,
		type:'post',
		data: {'ac':'directory_popup','id':id},
		success: function(data) {
			action_callback(data);
		}
	});
}

function filter_results(type){
	_popup.create({ html:"<img src='/images/loading-bar.gif' alt='searching our database'/>" });
	var params = {'script':widgets.directory,'ac':'filter','callback':"action_callback(data);",'frmname':'main_form'};
	var reset = [];
	if(type == 'search'){
		reset = ['id','letter','location','department'];
	}else if(type == 'letter'){
		reset = ['id','search','location','department'];
	}else if(type == 'location'){
		reset = ['id','search','letter','department'];
	}else if(type == 'department'){
		reset = ['id','search','letter','location'];
	}
	for(var x=0;x<reset.length;x++){
		params[reset[x]] = '';
	}
	ajax_submit_form(params);
}

function action_callback(response){
	var json;
	eval("json = "+response+";");

	if(json.action == 'popup'){
		_popup.create({ html:json.content, closeicon:true });
		if(json.javascript) eval(json.javascript);
		
		activate_telephone_numbers();
	}else if(json.action == 'graph_popup'){
		_popup.create({ html:json.content, width:850, height:550, closeicon:true });
		if(json.javascript) eval(json.javascript);
	}else if(json.action == 'login'){
		_popup.close();
		login_popup({'ac':'login_dialog'});
	}else if(json.content != ''){
		$('#ajax_area').html(json.content)
		_popup.close();
		
		activate_telephone_numbers();
	}
}

function activate_telephone_numbers(){
	/* check for iPhone */
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = ((agent.indexOf(/iPhone/i)!=-1));
	is_iphone = (agent.indexOf(/Android/i)!=-1)?1:is_iphone;
	is_iphone = (agent.indexOf(/BlackBerry/i)!=-1)?1:is_iphone;
	is_iphone = (agent.indexOf(/webOS/i)!=-1)?1:is_iphone;
	
	if (is_iphone) {
		/* iPhone telephone number formatting */
		/* adds 'tel:' scheme formatted phone number to all microformatted telephone number on the page */
		$('.tel .number:not(.nodial)').each(function(){
			var telNo = $(this).text().replace(/\(.*\)/,'').replace(/\s+/g,'');
			var wrapLink = '<a href="tel:'+telNo+'"></a>';
			$(this).wrap(wrapLink);
		});
	} 
}
