// JavaScript Document



function sort_articles()
{	
	var before= '', after ='', f = document.form1;	
	var tmpArr = Array();
	for(var i=0; i<f.length-2; i++)
	{
		before = before + f.elements[i].name +'='+f.elements[i].value+'&';
		tmpArr = f.elements[i].name.split('_');
		f.elements[i].value = tmpArr[1]+'_'+(i+1);
		after = after + f.elements[i].name +'='+f.elements[i].value+'&';
	}
	return true;
}

function create_overlay()
{
	new Insertion.Bottom('body',"<div id='TB_overlay'></div>");
	toggle_animation();
}

function remove_overlay()
{
	new Element.remove($('TB_overlay'));
}

function toggle_animation()
{
	var obj = $('TB_overlay');
	
	obj.className = (obj.className == 'loader_animation')? '' : 'loader_animation';
	if(obj.className == '')
	{
		remove_overlay();
	}
}

function load_AJAX_URL(id, URL, pars, method)
{
	var myAjax = new Ajax.Updater(id, URL, {method: method, parameters: pars, onSuccess: remove_overlay, onFailure: failed});
}


function next(URL)
{
	create_overlay();
	load_AJAX_URL('Application-Area', URL, '', 'get');
}

function save(URL)
{
	create_overlay();
	
	//	Last Call Made
	convertCalenderDate('data[Record][call_back_date]', true);

	// Attach <CHATTEXT> tag in between the Actual Description
	document.record_form.current_account_description.value = '<chattext>'+document.record_form.current_account_description.value+'</chattext>';
	var pars = Form.serialize('record_form');
	load_AJAX_URL('Application-Area', URL, pars, 'post');
}

function convertCalenderDate(objName, flag)
{
	var listArray = $(objName).value.split('/');
	if(flag)
	{
		$(objName).value = listArray[2]+'-'+listArray[0]+'-'+listArray[1]+' '+$F('hours')+':'+$F('minutes')+':00';
	}
	else
		$(objName).value = listArray[2]+'-'+listArray[0]+'-'+listArray[1];

}

function show()
{
//	alert('Loaded');
}

function failed()
{
	alert('failed');
}

function toggle_online_status()
{
	var linkDiv = $('ONLINE_STATUS');
	var linkId = $('ONLINE_STATUS_LINK');
	var flag = 1;
	
	if(linkId.className == 'online-status')
	{
		linkDiv.innerHTML = '<a href="javascript:void(0);" class="offline-status" id="ONLINE_STATUS_LINK" onClick="toggle_online_status(); return false;">I\'m Offline now</a>';
		flag = 0;
	}
	else
	{
		linkDiv.innerHTML = '<a href="javascript:void(0);" class="online-status" id="ONLINE_STATUS_LINK" onClick="toggle_online_status(); return false;">I\'m Online now</a>';
	}


	var URL = release_status_url+flag;
//	alert(URL);
	var myRequest = new Ajax.Request(URL, {method: 'get', parameters: '', onComplete: show});
//	load_AJAX_URL('ONLINE_STATUS', URL, '', 'post');
}

function search_record(flag, record_id)
{
	if(!record_id)
		record_id = $F('search_record_txt');

	if(record_id == '')
	{
		alert('Warning: Provide Record ID to begin Search');
		return;
	}
	if(!flag)
		var URL = '/agents/fetch/3/'+record_id;
	else
		var URL = '/salespeople/fetch/3/'+record_id;

	next(URL);
}

function quick_display_record(record_id)
{
	var URL = '/records/fetch/'+record_id;
	next(URL);
}


function sendRecord(flag, record_id)
{
	
	//	Local Updation to Chat Window
	var myDate = new Date();
	var temp;
	var str = '<p><a href = "javascript:void(0);" onClick = "quick_display_record('+record_id+');"<b>'+$F("username")+' sent Record ID: '+record_id+'</b></a></p>';
	new Insertion.Top('Message-History', str);
	
	temp = $('data[Message][messages]').value;
	$('data[Message][messages]').value =str;

//	Update the Database with the current message
	var pars = Form.serialize('chatForm');

	$('data[Message][messages]').value =temp;

	var myRequest = new Ajax.Request('/messages/send/', {method: 'post', parameters: pars});

}
 
function sendMsg()
{

	//	Local Updation to Chat Window
	var myDate = new Date();
	var str = "<p><b>"+$F('username')+" says:</b><em> "+myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds()+"</em><br>"+$F('data[Message][messages]')+"</p>";
	new Insertion.Top('Message-History', str);
	
	$('data[Message][messages]').value =str;

//	Update the Database with the current message
	var pars = Form.serialize('chatForm');

	$('data[Message][messages]').value ='';

	var myRequest = new Ajax.Request('/messages/send/', {method: 'post', parameters: pars});
}
