// JavaScript Document

var Images = new Array;


function updateBanner(bannerId) {
  if (!window.XMLHttpRequest && window.ActiveXObject) {
	  window.XMLHttpRequest = function () {
		  return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") != -1 ? "Microsoft.XMLHTTP" : "MSXML2.XMLHTTP");
	  };
	}

	if (!window.XMLHttpRequest) {
		return false;
	}

	var myDoc = new XMLHttpRequest();

	myDoc.open('GET', 'countBanner.php?action=ad&id=' + bannerId, false);
	myDoc.send(null);

	return true;
}


function selImg(self) {
 im = replaceCharacters(self.src, '.gif', '_sel.gif');
 img = inArray(Images, im);
 if (img=='NaN') {
  img = new Image();
  img.src = im;
  Images = toArray(Images, im, img);
 }
 self.src = img.src;
}

function unselImg(self) {
 im = replaceCharacters(self.src, '_sel.gif', '.gif');
 img = inArray(Images, im);
 if (img == 'NaN') {
  img = new Image();
  img.src = im;
  Images = toArray(Images, im, img);
 }
 self.src = img.src; // replace
}

function toArray(a, key, value){
 a.push(key, value);
 return a;
}

function inArray(a, key){
 for (i = 0; i<a.length; i+=2){
  if (a[i] == key){
   return a[i+1];
  }
 }
 return 'NaN';
}

function replaceCharacters(conversionString,inChar,outChar) {
 var convertedString = conversionString.split(inChar);
 convertedString = convertedString.join(outChar);
 return convertedString;
}

function checkRegistrationValues(english) {
 emailValidator                    = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
 zipcodeValidator                  = /\d{5}(-\d{4})?/;
 currencyValidator                 = /\$\d{1,3}(,\d{3})*\.\d{2}/;
 timeValidator                     = /^([1-9]|1[0-2]):[0-5]\d$/;
 DateValidator                     = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
 phoneNumberValidator              = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
 internationalPhoneNumberValidator = /^\d(\d|-){7,20}/;
 ipAddressValidator                = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

 var elm = document.getElementById('registration_name');
 if (elm.value=='') {
  if (english) {
   alert('Name is necessary.');
  } else {
   alert('Il campo Nome è obbligatorio.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_surname');
 if (elm.value=='') {
  if (english) {
   alert('Surname is necessary.');
  } else {
   alert('Il campo Cognome è obbligatorio.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_email');
 if (!emailValidator.exec(elm.value)) {
  if (english) {
   alert('Wrong value for field E-mail.');
  } else {
   alert('Valore non è consentito per il campo E-mail.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 if (!elm.value==document.getElementById('registration_email_2').value) {
  if (english) {
   alert('E-mail fields have different values.');
  } else {
   alert('Valori diversi nei campi E-Mail.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_address');
 if (elm.value=='') {
  if (english) {
   alert('Address is necessary.');
  } else {
   alert('Il campo Indirizzo è obbligatorio.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_firm');
 if (elm.value=='') {
  if (english) {
   alert('Corporate is necessary.');
  } else {
   alert('Il campo Azienda è obbligatorio.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_job');
 if (elm.value=='') {
  if (english) {
   alert('Qualification is necessary.');
  } else {
   alert('Il campo Qualifica è obbligatorio.');
  }
  elm.select();
  elm.focus();
  return false;
 }
 elm=document.getElementById('registration_forum_yes');
 if (!elm.checked) {
  if (english) {
   alert('You must agree with the forum policy.');
  } else {
   alert('È necessario accettare il regolamento del forum.');
  }
  return false;
  elm.select();
  elm.focus();
 }

 elm=document.getElementById('registration_privacy_yes');
 if (!elm.checked) {
  if (english) {
   alert('The private data treatment acceptance is mandatory.');
  } else {
   alert('È necessario consentire il trattamento dei Dati Personali.');
  }
  return false;
  elm.select();
  elm.focus();
 }
 var theform = document.getElementById('theForm');
 theform = removeElements(theform);
 theform = addInputElement(theform,'action','register');
 theform = addInputElement(theform,'registration_name',document.getElementById('registration_name').value);
 theform = addInputElement(theform,'registration_surname',document.getElementById('registration_surname').value);
 theform = addInputElement(theform,'registration_email',document.getElementById('registration_email').value);
 theform = addInputElement(theform,'registration_address',document.getElementById('registration_address').value);
 theform = addInputElement(theform,'registration_firm',document.getElementById('registration_firm').value);
 theform = addInputElement(theform,'registration_job',document.getElementById('registration_job').value);
 theform.submit();
 return false;
}

function resend() {
	var theform = document.getElementById('theForm');
        var elm = document.getElementById('persapassword_email');
        emailValidator = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
        if (!emailValidator.exec(elm.value)) {
	        alert('Il valore inserito non corrisponde ad un indirizzo mail valido.');
	        elm.select();
	        elm.focus();
        	return false;
        }
        theform = removeElements(theform);
        theform = addInputElement(theform,'context','persapassword');
        theform = addInputElement(theform,'email',elm.value);
	theform.submit();
	return false;
}
function logout() {
	var theform = document.getElementById('theForm');
	theform = removeElements(theform);
	theform = addInputElement(theform,'context','');
	theform = addInputElement(theform,'subcontext','');
	theform = addInputElement(theform,'action','logout');
	theform.submit();
	return false;
}
function request(ctx, subctx) {
	var theform = document.getElementById('theForm');
	theform = removeElements(theform);
	theform = addInputElement(theform,'context',ctx);
	theform = addInputElement(theform,'subcontext',subctx);
	theform.submit();
	return false;
}
function download(fileName) {
	var theform = document.getElementById('theForm');
	theform = removeElements(theform);
	theform = addInputElement(theform,'action','downloadfile');
	theform = addInputElement(theform,'filename',fileName);
	theform.submit();
	return false;
}
function switchLanguage(context, subcontext) {
	var theform = document.getElementById('theForm');
	theform = removeElements(theform);
	theform = addInputElement(theform,'context',context);
	theform = addInputElement(theform,'subcontext',subcontext);
	theform = addInputElement(theform,'action','switchLanguage');
	theform.submit();
	return false;
}
function addInputElement(theform, thename, thevalue) {
	var elm = document.createElement('input');
	elm.name=thename;
	elm.value=thevalue;
	theform.appendChild(elm);
	return theform;
}
function removeElements(theform) {
	for (var i = theform.elements.length-1; i >= 0 ; i--) {
		theform.removeChild(i);
	}
	return theform;
}

function validateIntegerRelativeChar(keypressEvent) {
	return validateChar(keypressEvent, /[\-0-9]/);
}
function validateDecimalRelativeChar(keypressEvent) {
	return validateChar(keypressEvent, /[\-0-9\.]/);
}
function validateIntegerChar(keypressEvent) {
	return validateChar(keypressEvent, /[0-9]/);
}
function validateDecimalChar(keypressEvent) {
	return validateChar(keypressEvent, /[0-9\.]/);
}
function validateChar(keypressEvent, regexp) {
    var key, keychar;
    key = getKey(keypressEvent);
    // control keys
    if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) return true;
    keychar = String.fromCharCode(key);
    return (regexp.test(keychar));
}
function getKey(event) {
    if (window.event) return window.event.keyCode;
    if (event) return event.which;
	return null;
}

// appearing tip-box - START
   var horizontal_tipbox_offset = "9px"
   var vertical_tipbox_offset   = "0px"
   var ie = document.all
   var ns6 = document.getElementById && !document.all
   function getOffsetPosition(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
     totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
     parentEl=parentEl.offsetParent;
    }
    return totaloffset;
   }
   function iecompattest() {
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
   }
   function clearbrowseredge(obj, whichedge) {
    var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_tipbox_offset)*-1 : parseInt(vertical_tipbox_offset)*-1;
    if (whichedge=="rightedge") {
     var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40;
     dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
     if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure) {
      edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_tipbox_offset);
     }
    } else {
     var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
     dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
     if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) {
      edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight;
     }
    }
    return edgeoffset;
   }
   function showtip(menucontents, obj, e, tipwidth) {
    if ((ie||ns6) && document.getElementById("tipbox")) {
     dropmenuobj=document.getElementById("tipbox");
     dropmenuobj.innerHTML=menucontents;
     dropmenuobj.style.left=dropmenuobj.style.top=-500;
     if (tipwidth!="") {
      dropmenuobj.widthobj=dropmenuobj.style;
      dropmenuobj.widthobj.width=tipwidth;
     }
     dropmenuobj.x=getOffsetPosition(obj, "left");
     dropmenuobj.y=getOffsetPosition(obj, "top");
     dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px";
     dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px";
     dropmenuobj.style.visibility="visible";
     obj.onmouseout=hidetip;
    }
   }
   function hidetip(e){
    dropmenuobj.style.visibility="hidden";
    dropmenuobj.style.left="-500px";
   }
   function createtipbox() {
    var divblock=document.createElement("div");
    divblock.setAttribute("id", "tipbox");
    document.body.appendChild(divblock);
   }
   if (window.addEventListener) {
    window.addEventListener("load", createtipbox, false);
   } else {
    if (window.attachEvent) {
     window.attachEvent("onload", createtipbox);
    } else if (document.getElementById) {
     window.onload=createtipbox;
    }
   }
// appearing tip-box - END
