function makeRPCCall() {
	ufXMLR = XMLRPC.getService("http://community.downlovers.it/RPC");
	ufXMLR.add("index_rpc", "index_rpc");
	return ufXMLR;
}


function getDedica(evt, userId) {
	if(checkGo()==true) {
		l = evt.clientX;
		t = evt.clientY;
		l = t+0;
		t = t+50;
		document.getElementById('d_floating').style.display='block';
		document.getElementById('d_floating').style.top=t+'px';
		document.getElementById('d_floating').style.left=l+'px';
		document.getElementById('d_dedica_body').value='';
	}
}


function setDedica(dedicaBody, userId) {
	if(checkGo()==true) {
		if(dedicaBody) {
			var connection=makeRPCCall();
                        data=connection.index_rpc('insertDedica_rpc', {'dedica_body':encode64(dedicaBody), 'user_id':userId}, ['Community', 'Element','DedicaBox']);
			document.getElementById('d_msgPar').innerHTML=data;
			setTimeout("window.location.reload()", 150);
		} else {
			alert('Inserire il testo della dedica');
		}
	}
}



/* Metodi per i post della bacheca */
function getPost(evt, categoryId, userId, postId) {
	if(checkGo()==true) {
		l = evt.clientX;
		t = evt.clientY;
		l = t+0;
		t = t+50;
		document.getElementById('p_floating').style.display='block';
		document.getElementById('p_floating').style.top=t+'px';
		document.getElementById('p_floating').style.left=l+'px';
		document.getElementById('p_post_body').value='';
		document.getElementById('p_post_title').value='';
		document.getElementById('p_checkField').value='500';
		document.getElementById('p_hidden_category').value=categoryId;
		document.getElementById('p_hidden_post').value=postId;
		var connection=makeRPCCall();
		data=connection.index_rpc('getPost_rpc', {'category_id':categoryId, 'user_id':userId, 'post_id':postId}, ['Community', 'Element','PostBox']);
		document.getElementById('p_categoryTitle').innerHTML='<b>'+XMLRPC.decodeBase64(data['category_title'])+'</b>';
		document.getElementById('p_msgPar').innerHTML=XMLRPC.decodeBase64(data['msg']);
		document.getElementById('p_post_body').value=XMLRPC.decodeBase64(data['post_body']);
		document.getElementById('p_post_title').value=XMLRPC.decodeBase64(data['post_title']);
	}
}

function setPost(postBody, postTitle, categoryId, userId, postId) {
	if(checkGo()==true) {
		if(postBody && postTitle) {
			var connection=makeRPCCall();
                        data=connection.index_rpc('insertPost_rpc', {'post_body':encode64(postBody), 'post_title':encode64(postTitle), 'category_id':categoryId, 'user_id':userId, 'post_id':postId}, ['Community', 'Element','PostBox']);
			document.getElementById('p_msgPar').innerHTML=data;
			setTimeout("window.location.reload()", 150);
		} else {
			alert('Inserire il post');
		}
	}
}

/* Metodi per i commenti della bacheca */
function getComment(evt, postId, userId) {
	if(checkGo()==true) {
		l = evt.clientX;
		t = evt.clientY;
		l = t+0;
		t = t+50;
		document.getElementById('c_floating').style.display='block';
		document.getElementById('c_floating').style.top=t+'px';
		document.getElementById('c_floating').style.left=l+'px';
		document.getElementById('c_comment_body').value='';
		document.getElementById('c_checkField').value='500';
		document.getElementById('c_hidden_post').value=postId;
		var connection=makeRPCCall();
		data=connection.index_rpc('getComment_rpc', {'user_id':userId, 'post_id':postId}, ['Community', 'Element','CommentBox']);
		document.getElementById('c_postTitle').innerHTML='<b>'+XMLRPC.decodeBase64(data['post_title'])+'</b>';
		document.getElementById('c_msgPar').innerHTML=XMLRPC.decodeBase64(data['msg']);
		//document.getElementById('c_comment_body').value=XMLRPC.decodeBase64(data['comment_body']);
	}
}

function setComment(commentBody, userId, postId) {
	if(checkGo()==true) {
		if(commentBody) {
			var connection=makeRPCCall();
                        data=connection.index_rpc('insertComment_rpc', {'comment_body':encode64(commentBody), 'post_id':postId, 'user_id':userId}, ['Community', 'Element','CommentBox']);
			document.getElementById('c_msgPar').innerHTML=data;
			setTimeout("window.location.reload()", 150);
		} else {
			alert('Inserire il commento');
		}
	}
}


/* Invio di una Friend Request */
function sendFriendRequest(userFrom, userTo) {
	var connection = makeRPCCall();
	data = connection.index_rpc('sendFriendRequest_rpc', {'user_from':userFrom, 'user_to':userTo}, ['Community','User','Friends']);
	alert(data[1]);
}

/* Invia di un messaggio */
function writeMessage(userFrom, userTo, msgTitle, msgText) {
	if(msgTitle && msgText) {
		var connection=makeRPCCall();
		data=connection.index_rpc('writeMessage_rpc', {'user_from':userFrom, 'user_to':userTo, 'msg_title':encode64(msgTitle), 'msg_text':encode64(msgText)}, ['Community','User','Message']);
		document.getElementById('msgPar').innerHTML=data[1];
		if(data[0]==1) setTimeout("window.location.reload()", 200);
	} else {
		alert('Inserire il titolo e il testo del messaggio!');
	}
}


function showMessageLayer(evt, idLayer, idElement, idUserTo, nickName) {
  if (parseInt(navigator.appVersion)>3)
    if (navigator.appName=="Netscape")
      winW = window.innerWidth;
      winH = window.innerHeight;
    if (navigator.appName.indexOf("Microsoft")!=-1)
      winW = document.documentElement.clientWidth;
      winH = document.documentElement.clientHeight;
  t = evt.clientY+10;
  l = evt.clientX+20;
  if(t+220 > winH)
    t = winH-220;
  if(l+370 > winW)
    l = winW-370;
  document.getElementById(idLayer).style.display = 'block';
  document.getElementById(idLayer).style.top=t+'px';
  document.getElementById(idLayer).style.left=l+'px';
  document.getElementById('user_nick').innerHTML = 'Messaggio per <i>'+nickName+'</i>';
  document.getElementById('msg_title').value = '';
  document.getElementById('msg_text').value = '';
  document.getElementById(idElement).value = idUserTo;
}


function logBanner(cookie, idBanner, idType) {
	var connection = makeRPCCall();
	if(!cookie) {
		cookie = 'dwlAdv';
        }
	data = connection.index_rpc('logBanner_sql', {'cookie':cookie, 'id_banner':idBanner, 'id_type':idType});
}

flagg = 0;

function showMsg(container, element, msgId, msgStatus, obj) {
	oDiv=document.getElementById(container);
        oP=oDiv.getElementsByTagName("p");
        oD=oDiv.getElementsByTagName("div");
	for (j=0 ; j<oP.length; j++){ 
		if(oP[j].id==element) {
			showLayer(element);
			oD[j].className='grigio';
		} else {
			oP[j].style.display='none';
			if (oD[j]!=null) oD[j].className='bianco';
		}
	}

	if(arguments.length==5) {
		if(msgStatus==1 && flagg!=msgId) {
			newMsg = parseInt(document.getElementById('boxLoginMsg').innerHTML);
			if(newMsg>0) {
				document.getElementById('boxLoginMsg').innerHTML=newMsg-1;
			}
			var connection = makeRPCCall();
			data = connection.index_rpc('updateMessage_sql', {'msg_id':msgId, 'status_id':2}, ['Community','User','Message']);
			obj.innerHTML=obj.firstChild.innerHTML;
			flagg=msgId;
		}
	}
}


function showLayer(idElement) {
  document.getElementById(idElement).style.display = 'block';
}


function closeLayer(idElement) {
  document.getElementById(idElement).style.display = 'none';
}

function maxlength(element1, element2, maxlimit) {
	if (document.getElementById(element1).value.length > maxlimit) {
		document.getElementById(element1).value = document.getElementById(element1).value.substring(0, maxlimit);
	} else {
		document.getElementById(element2).value = maxlimit - document.getElementById(element1).value.length;
        }
}


/* base64 encoding */
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}


function checkGo() {
		return true;
}

function showHide()
{
  if (document.getElementById('estesa').checked == true)
  {
   document.getElementById('dis1').style.display='';
   document.getElementById('dis2').style.display='';
   document.getElementById('dis3').style.display='';
   document.getElementById('dis4').style.display='';
   document.getElementById('dis5').style.display='';
  } else {
   document.getElementById('dis1').style.display='none';
   document.getElementById('dis2').style.display='none';
   document.getElementById('dis3').style.display='none';
   document.getElementById('dis4').style.display='none';
   document.getElementById('dis5').style.display='none';
   document.getElementById('citta').value = '';
   document.getElementById('isArtist').selectedIndex = 0;
   document.getElementById('instrument').selectedIndex = 0;
   document.getElementById('workingarea').selectedIndex = 0;
   document.getElementById('zodiac').selectedIndex = 0;
  }
}


function MultiSelector( list_target, max ){this.list_target = list_target;this.count = 0;this.id = 0;if( max ){this.max = max;} else {this.max = -1;};this.addElement = function( element ){if( element.tagName == 'INPUT' && element.type == 'file' ){element.name = 'file_' + this.id++;element.multi_selector = this;element.onchange = function(){var new_element = document.createElement( 'input' );new_element.type = 'file';this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';};if( this.max != -1 && this.count >= this.max ){element.disabled = true;};this.count++;this.current_element = element;} else {alert( 'Error: not a file input element' );};};this.addListRow = function( element ){var new_row = document.createElement( 'div' );var new_row_button = document.createElement( 'input' );new_row_button.type = 'button';new_row_button.value = 'Delete';new_row.element = element;new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;};new_row.innerHTML = element.value;new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );};};
