var jsonrpc = null;
var ifvPopup = null;

// *********************************************************************************
// Invite Form
// *********************************************************************************
 
function ivfOnInitSendForm( contactsId, mexId , setMex)
{
	try
	{
		jsonrpc = new JSONRpcClient("/JSON-RPC/");
		jsonrpc.ifvMessage.setClientTimeStamp((new Date()).getTime());
		var contacts = jsonrpc.ifvMessage.getContactsList();
		$("#"+contactsId).val(contacts);
		if (setMex)
		{
			var mex = jsonrpc.ifvMessage.getMex();
			$("#"+mexId).val(mex);
		}
	} 
	catch (e)
	{
	}	
}

function ivfOnReloadSendForm(  )
{
	ivfOnInitSendForm('contacts','addressMex',false);
}

function ivfResetForm(contactsId, mexId) 
{
	try 
	{
		$("#"+contactsId).val("");
		$("#"+mexId).val("");
	  	jsonrpc = new JSONRpcClient("/JSON-RPC/");
	  	jsonrpc.ifvMessage.setAll("","");
	}
	catch (e)
	{
	}	
}

function ivfOpenPostProvider(idForm)
{
	$("#"+idForm).get(0).submit();
	
}

/*
function ivfOpenProvider(provideName,listId, mexId)
{
	// salvo i dati
	try
	{
		
		var messageList = $("#"+listId).get(0).value;
		var mex 		= $("#"+mexId).get(0).value;
	  	jsonrpc = new JSONRpcClient("/JSON-RPC/");
	  	var flag = jsonrpc.ifvMessage.setAll(messageList,mex);
	}
  	catch (e)
  	{
  	}	
	// alert(provideName);
	var url = "webmail/?pname="+provideName;  
	document.location.href = url;
}
*/

function ivfOpenProvider(provideName,listId, mexId, autoPost)
{
	var url = "webmail/?pname="+provideName+"&ap="+autoPost;  
	//document.location.href = url;
	ifvPopup = window.open(url, "ifvPopup", 'width=800,height=600,resizable=yes,scrollbars=yes');
	ifvPopup.focus();
}


function ivfSendMail( listId, mexId , nameId )
{
	try
	{
		var messageList = $("#"+listId).get(0).value;
		var mex  = $("#"+mexId).get(0).value;
		var name = $("#"+nameId).get(0).value;
		//
		if (messageList.length>0)
		{
			//alert(messageList);
		  	jsonrpc = new JSONRpcClient("/JSON-RPC/");
		  	var flag = jsonrpc.ifvMessage.send(messageList,mex,name);
		  	//
		  	if (flag==0)
		  		alert("L'invito e' stato inoltrato ai tuoi amici");
		  	else
		  		alert("Si e' verificato un errore");
		}
		else
		{
			alert("Inserisci almeno un indirizzo di mail");
		}
	}
  	catch (e)
  	{
  	}	
}

// *********************************************************************************
// Address Book
// *********************************************************************************

function ifvOnLoginFormInit( errorDivId, error  )
{
	/*
		if (logouturl!=null)
			$.get(logouturl);
	*/
	//
	if (error!=null && error!="")
		$("#"+errorDivId).show();
	else
		$("#"+errorDivId).hide();
}

function ifvOnAddressBookInit( tableId , signatureId , contactId )
{
		try
		{
		  	jsonrpc = new JSONRpcClient("/JSON-RPC/");
		  	// load address book
		  	var signature = jsonrpc.ifvSessionContactList.getSignature();
		  	//alert(signature);
		  	//$("#"+signatureId).html(signature);
		  	var lista = jsonrpc.ifvSessionContactList.getContacts();
		  	//
		  	var rows = "";
		  	for (var i=0;i<lista.length;i++)
		  		rows += ifvMakeHtmlRow(lista[i].email,lista[i].label,contactId,i);
		  	rows += "";
		  	$("#"+tableId).html(rows);
	  	}
	  	catch (e)
	  	{
	  		$("#"+tableId).html(" ");
	  	}
}


function ifvMakeHtmlRow( e, l , cid , index)
{
	var e = "&lt;"+e+"&gt;"
	var ret = "<tr class='row"+(index%2==0 ? 0 : 1)+"'>";
	ret += "<td class='checkbox'><input class='inputcheckbox' type='checkbox' id='"+cid+"' name='"+cid+"' value='"+l+" "+e+"'></td>";
	ret += "<td class='name'>"+l+"</td>"
	ret += "<td class='email'>"+e+"</td>";
	ret += "</tr>";
	return ret;
}

function ifvAddressBookToggle( toggleId , contactId )
{
	var toggle = $("#"+toggleId).get(0).checked;
	//
	var txt = "";
	var ele = document.getElementsByName(contactId);
	if (ele!=null)
	{
		for (var i=0;i<ele.length;i++)
			ele[i].checked = toggle;
	}
}

function ifvAddContacts( source , url )
{
	var txt = "";
	var ele = document.getElementsByName(source);
	if (ele!=null)
	{
		for (var i=0;i<ele.length;i++)
		{
			if (ele[i].checked)
				txt += (txt.length>0 ? " " : "")+ele[i].value+";";
		}
	}
	// add
	try
	{
		jsonrpc = new JSONRpcClient("/JSON-RPC/");
		jsonrpc.ifvMessage.addContactsList(txt);
		//
		opener.ivfOnReloadSendForm(); 
	}
	catch (e)
	{
	}	
	// close
	ifvCloseProvider(url);
}

function ifvCloseProvider(  url )
{
	//document.location.href = url;
	window.close();
}

// *********************************************************************************
// Social
// *********************************************************************************

function ivfShareOnFacebook( url, desc )
{
	var p="u="+escape(url)+"&t=";
	window.open("http://www.facebook.com/sharer.php?"+p,"_blank");
}

function ivfShareOnDigg( url, desc )
{
	var p="url="+escape(url)+"&title="+escape(desc);
	window.open("http://digg.com/submit?phase=2&"+p,"_blank");
}

function ivfShareOnDelicious( url, desc )
{
	var p="url="+escape(url);
	window.open("http://delicious.com/post?"+p,"_blank");
}

function ivfShareOnMySpace( url, desc )
{
	// c=
	var p="u="+escape(url)+"&t="+escape(desc)+"&c="+escape(desc);
	window.open("http://www.myspace.com/index.cfm?fuseaction=postto&l=2&"+p,"_blank");
}

function ivfShareOnTwitter( url, desc )
{
	var p="status="+escape(desc)+"+"+escape(url);
	window.open("http://twitter.com/home?"+p,"_blank");
}

function ivfShareOnNetLog( url, desc )
{
	// http://www.netlog.com/go/manage/links/view=save&origin=external&url=[url]
	var p=escape(url);
	window.open("http://www.netlog.com/go/manage/links/view=save&origin=external&url="+p,"_blank");
}

