/*

YOU CAN COPY AND USE THIS FILE IN YOUR WEB PAGES, BUT DO NOT MODIFY LINES BELOW PLEASE.

@LIBRARY: AJAX GOPHENGINE
@AUTHOR: ALESSANDRO ROMANI
@VERSION: 1.0

*/

/* GENERIC FUNCTIONS */

// Resize a window to specified width and height
function resizeWindow(setX,setY,setWidth,setHeight) {
self.resizeTo(setWidth,setHeight);
self.moveTo(setX,setY);
}
// Get X-axis mouse position
function getPositionX(e) {
var e = e ? e : window.event;
X = e.clientX;
return X;
}
// Get Y-axis mouse position
function getPositionY(e) {
var e = e ? e : window.event;
Y = e.clientY;
return Y;
}
// Show a modal window
function showModal(event,setMaxWidth,content) {
var posX = getPositionX(event);
var posY = getPositionY(event);
document.getElementById('modal_bottom').innerHTML=content;
document.getElementById('modal_box').style.display='block';
document.getElementById('modal_box').style.left=posX+"px";
document.getElementById('modal_box').style.top=posY+"px";
document.getElementById('modal_box').style.maxWidth=setMaxWidth+"px";
}
// Hide a modal window
function hideModal() {
document.getElementById('modal_box').style.display='none';
}

/* HOME FUNCTIONS */

var banners = new Array();
var links = new Array();

// Create banners list
banners[0] = "http://www.gdr-online.com/banner/segnalato.gif";
banners[1] = "http://data.nimenia.org/banner/bottega.gif";
banners[2] = "http://www.vividadrop.com/store/banner/Vivida_Drop_VividaDrop-banner.gif";
// Create links list
links[0] = "http://www.gdr-online.com/";
links[1] = "http://www.acaciagiuseppe.it/";
links[2] = "http://www.vividadrop.com/";
// Set the show criteria
function changeBanner()
{
i=Math.round(Math.random()*2);
document.getElementById('banner').src=banners[i];
setTimeout("changeBanner()",10*1000);
}
// Set the url with the showing banner
function openBanner()
{
window.open(links[i]);
}

/* INTERFACE FUNCTIONS */

// Create main interface
function constructNimenia() {
navigation('menu_left.asp','menu_left','true');
navigation('menu_top.asp','menu_top','true');
navigation('map.asp','main_box','true');
loading('loading','container');
fadeIn(0,10,'container',500);
}
// Create chat interface
function constructChat() {
var message=document.getElementsByTagName('form')[0].elements[1];
if(!message){return};
message.focus();
getChatData();
navigation('immagine_locazione.asp','immagine_locazione');
}
// Hide the container before the page loading
function loading(obj_show,obj_hide) {
document.getElementById(obj_show).style.visibility = "hidden";
document.getElementById(obj_hide).style.visibility = "visible";
}
// Fade in effect
function fadeIn(startValue,stopValue,objID,duration) {
var obj = document.getElementById(objID);
var duration = duration;
obj.style.opacity=startValue/10;
obj.style.filter='alpha(opacity=' + startValue*10 + ')';
startValue++;
if (startValue<=stopValue) {
setTimeout("fadeIn(" + startValue + "," + stopValue + ",'" + objID + "'," + duration + ")",duration);
}
}
// Fade out effect
function fadeOut(startValue,stopValue,objID,duration) {
var obj = document.getElementById(objID);
var duration = duration;
obj.style.opacity = startValue/10;
obj.style.filter='alpha(opacity=' + startValue*10 + ')';
startValue--;
if (startValue>=stopValue) {
setTimeout("fadeOut(" + startValue + "," + stopValue + ",'" + objID + "'," + duration + ")",duration);
}
}

/* MAIN FUNCTIONS */

// Create XMLHttp Object
function getXMLHttpObject() {
try {
var xmlhttp = new XMLHttpRequest();
} catch (e) {
try {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
alert("Ajax non supportato!");
}
}
}
return xmlhttp;
}
// Handle data with GET method
function getHandler(objID,xmlhttp) {
var obj = document.getElementById(objID);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
// Handle data with POST method
function postHandler(objID,xmlhttp) {
var obj = document.getElementById(objID);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
// Get data from forms
function getSendData(formName) {
var sendData = '';
var formElement;
var lastElementName = '';
for (i = 0; i < formName.elements.length; i++) {
formElement = formName.elements[i];
switch (formElement.type) {
case 'text':
case 'hidden':
case 'password':
case 'textarea':
case 'select-one':
sendData += formElement.name + '=' + escape(formElement.value) + '&'
break;
case 'radio':
if (formElement.checked) {
sendData += formElement.name + '=' + escape(formElement.value) + '&'
}
break;
case 'checkbox':
if (formElement.checked) {
if (formElement.name == lastElementName) {
if (sendData.lastIndexOf('&') == sendData.length-1) {
sendData = sendData.substr(0, sendData.length - 1);
}
sendData += ',' + escape(formElement.value);
}
else {
sendData += formElement.name + '=' + escape(formElement.value);
}
sendData += '&';
lastElementName = formElement.name;
}
break;
}
}
sendData = sendData.substr(0, sendData.length - 1);
return sendData;
}
// Send data
function postData(serverPage,objID,formName,async) {
var xmlhttp = getXMLHttpObject();
xmlhttp.open("POST",serverPage,async);
xmlhttp.onreadystatechange = function() {postHandler(objID,xmlhttp);}
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
var sendData = getSendData(formName);
xmlhttp.send(sendData);
}
// Main menù function
function navigation(serverPage,objID,async) {
var xmlhttp = getXMLHttpObject();
xmlhttp.open("GET",serverPage,async);
xmlhttp.onreadystatechange = function() {getHandler(objID,xmlhttp);}
xmlhttp.send(null);
}
// Open a pop-up
function openPopUp(url,name,attributes) {
window.open(url,name,attributes);
}
// View preview content
function preview(objID,content,effect) {
var obj=document.getElementById(objID);
obj.innerHTML=content;
if (effect=="fadein") fadeIn(0,10,objID,100);
}
function loading() {
document.getElementById("loading").style.visibility = "hidden";
document.getElementById("container").style.visibility = "visible";
}
// Add an event listener
function addEventHandler(objID,action,refFunction,capture) {
var obj=document.getElementById(objID);
if(obj == null || typeof(obj) != "object") {
obj = document;
}
if (obj.addEventListener) {
action=action.replace("on","");
obj.addEventListener(action,refFunction,capture);
}
else
{
obj.attachEvent(action,refFunction);
}
}
// Remove an event listener
function removeEventHandler(objID,action,refFunction,capture) {
var obj=document.getElementById(objID);
if(obj == null || typeof(obj) != "object") {
obj = document;
}
if (obj.removeEventListener) {
action=action.replace("on","");
obj.removeEventListener(action,refFunction,capture);
}
else
{
obj.detachEvent(action,refFunction);
}
}

/* DRAG & DROP FUNCTIONS */

var X=0, Y=0, OT=0, OL=0, L=0, T=0, offsetL=0, offsetT=0, leftDistance=0, topDistance=0, isDraggable=true, draggableElement=null, defaultZindex=0;
// Initialize drag
function startDrag(e) {
var e = e ? e : window.event;
var target = window.event ? event.srcElement : e.target;
if (target.className == "isdraggable") {
isDraggable = true;
draggableElement = target;
parentElement = draggableElement.parentNode;
X = e.clientX;
Y = e.clientY;
offsetL = parentElement.offsetLeft;
offsetT = parentElement.offsetTop;
deltaX = X - offsetL;
deltaY = Y - offsetT;
if (isNaN(parseInt(draggableElement.style.left))) {draggableElement.style.left=0;}
if (isNaN(parseInt(draggableElement.style.top))) {draggableElement.style.top=0;}
L = parseInt(draggableElement.style.left);
T = parseInt(draggableElement.style.top);
draggableElement.style.zIndex=1000;
document.onmousemove = followDrag;
stopEvent(e);
}
}
// Set the left and top positions on the object during the mouse movement
function followDrag(e) {
var e = e ? e : window.event;
if (isDraggable) {
leftDistance = L + e.clientX - deltaX - parentElement.offsetLeft;
topDistance = T + e.clientY - deltaY - parentElement.offsetTop;
draggableElement.style.left = leftDistance + 'px';
draggableElement.style.top = topDistance + 'px';
stopEvent(e);
}
}
// Initialize drop
function stopDrag(e,objID,action) {
var e = e ? e : window.event;
var draggableElementId = draggableElement.id;
draggableElement.style.zIndex=defaultZindex;
isDraggable = false;
draggableElement = null;
document.onmousemove = null;
var xmlhttp = getXMLHttpObject();
if (action=="map_update_coords") serverPage = 'map/map_update_coords.asp?cod_locazione=' + draggableElementId + '&posx=' + leftDistance + '&posy=' + topDistance;
xmlhttp.open("GET",serverPage,true);
xmlhttp.onreadystatechange = function() {getHandler(objID,xmlhttp);}
xmlhttp.send(null);
}
// Stop event while another event has been executed
function stopEvent(e) {
var e = e ? e : window.event;
if (e.stopPropagation) e.stopPropagation();
else e.cancelBubble=true;
if (e.preventDefault) e.preventDefault();
else e.returnValue=false;
}

/* CHAT FUNCTIONS */

// Handle chat data
function getChatHandler(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var obj=document.getElementById('messages');
obj.innerHTML=xmlhttp.responseText;
toBottom();
}
}
// Handle chat messages with POST method
function postChatHandler(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
clearTimeout(chatmessages);
getChatData();
document.getElementsByTagName('form')[0].elements[2].disabled=true;
document.getElementsByTagName('form')[0].elements[1].value='';
document.getElementsByTagName('form')[0].elements[1].focus();
setTimeout('enableForm()',5*1000);
}
}
// Handle chat messages with GET method
function getChatData() {
getXMLHttpObject();
xmlhttp.onreadystatechange=function() {getChatHandler(xmlhttp);}
xmlhttp.open('GET','display.asp',true);
xmlhttp.send(null);
chatmessages = setTimeout('getChatData()',15*1000);
}
// Keep scroll to bottom
function toBottom() {
document.getElementById('messages').scrollTop=document.getElementById('messages').scrollHeight;
}
// Check text length
function checkTextLength() {
var max=500;
var lunghezza=document.getElementsByTagName('form')[0].elements[1].value.length;
document.getElementsByTagName('form')[0].elements[3].value=max-lunghezza;
}
// Handle chat messages with POST method
function postChatData() {
getXMLHttpObject();
var place=document.getElementsByTagName('form')[0].elements[0].value;
var message=document.getElementsByTagName('form')[0].elements[1].value;
if(message.length==0){return false};
xmlhttp.onreadystatechange= function() {postChatHandler(xmlhttp);}
xmlhttp.open('POST','post.asp',true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
xmlhttp.send('place='+escape(place)+'&message='+escape(message));
}
// Enable submit form after sending
function enableForm() {
document.getElementsByTagName('form')[0].elements[2].disabled=false;
}
// Refresh chat messages
function refreshChatData() {
clearTimeout(chatmessages);
getChatData();
}
// Back to map
function exitChat() {
clearTimeout(chatmessages);
navigation('check.asp?cod_locazione=1','main','false');
}
// Show PG functions
function showFunctions() {
var funzioni = document.getElementById('funzioni');
if (funzioni.style.display == "none")
funzioni.style.display = "";
else
funzioni.style.display = "none";
}
// Create a PG function from menù
function addFunction(functionName) {
var message = document.getElementsByTagName('form')[0].elements[1];
if (functionName=="allontana" || functionName=="arresta" || functionName=="esilia") {
var msgbox1=prompt("Nickname","");
var msgbox2=prompt("Motivazione","");
var msgbox3=prompt("Durata","gg-mm-aaaa");
if (msgbox1.length==0 || msgbox2.length==0) {
var strContent="";
} else {
var strContent="/"+functionName+"/"+msgbox1+"/"+msgbox2+"/"+msgbox3;
}
} else {
var msgbox1=prompt(""+functionName+"","");
if (msgbox1.length==0) {
var strContent="";
} else {
var strContent="/"+functionName+" "+msgbox1;
}
}
message.value  += strContent;
message.focus();
showFunctions();
}

/* MAIL FUNCTIONS */

// Handle mail data
function getMailHandler(xmlhttp) {
var obj=document.getElementById('controllo');
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
// Handle mail messages with GET method
function getMailData() {
getXMLHttpObject();
xmlhttp.onreadystatechange=function() {getMailHandler(xmlhttp);}
xmlhttp.open('GET','controllo.asp',true);
xmlhttp.send(null);
mailmessages = setTimeout('getMailData()',60*1000);
}
// Update default mail button with new mail button
function newMail() {
document.immagine_posta.src="http://www.nimenia.it/experimental/nuova_posta.gif";
}

