var http_request = false;
var id = 0;
var category = 0;
var rw = 0
var host = "";
var stop=0;
var tempX = 0
var tempY = 0
var diffX = 0
var diffY = 0
var verticalMov=true;
var margin;
var rt = 0;
var thumb = 0;
var host = "";
var scrolling = false;

function resetHistoryButton()
{
		var lang = imGetLayer("imjs_lang").value();
		var cont='<div class="button"><img src="images/index_page_button_'+lang+'.png" width=\'218px\' height=\'39px\' onclick=\'location.href="index.php"\' style=\'cursor:pointer\'/></div>';
    if (imGetLayer('historyButton') == null && imGetLayer('gotoIndex').innerHTML != cont)
      imGetLayer('gotoIndex').innerHTML = cont;
    else
      imGetLayer('historyButton').innerHTML = cont;
}

function createHttpRequest() {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
}

function alertResString(id) {
	createHttpRequest();
	http_request.onreadystatechange = alertResStringResp;
	http_request.open('GET', host + 'local.php?id=' + id, true);
	http_request.send(null);
}

function alertResStringResp() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			alert(unescape(http_request.responseText));
		}
		else {
			alert('There was a problem with the request.');
		}
	}
}

function viewIndexButton(lang)
{
    var cont='<div class="button"><img src="images/index_page.png" width=\'218px\' height=\'39px\' onclick=\'location.href="index.php"\' style=\'cursor:pointer\'/></div>';
    if (imGetLayer('historyButton') == null && imGetLayer('gotoIndex').innerHTML != cont)
      imGetLayer('gotoIndex').innerHTML = cont;
    else if (imGetLayer('historyButton').innerHTML != cont)
      imGetLayer('historyButton').innerHTML = cont;  
}

function viewCategory(id,order,page) {
	createHttpRequest();
	http_request.onreadystatechange = viewCategoryResponse;
	http_request.open('GET', host + 'category.php?cat=' + id + '&order=' + order + '&page=' + page + '&limit=21', true);
	http_request.send(null);
}

function viewCategoryResponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById('websites').innerHTML = unescape(http_request.responseText);
			scroll(0,0);
		}
		else {
			alert('There was a problem with the request.');
		}
	}
}

function fullStars(n) {
	for(i=1;i<=n;i++)
		document.getElementById('s'+i).src = 'images/star.png';
	for(i=n+1;i<=5;i++)
		document.getElementById('s'+i).src = 'images/star_vuota.png';
}

function voteWebsite(id,vote) {
	createHttpRequest();
	http_request.onreadystatechange = voteWebsiteResponse;
	http_request.open('GET', host + 'details.php?action=1&id=' + id + '&vote=' + vote, true);
	http_request.send(null);
}

function viewCommentForm()
{
  var form=imGetLayer("addCommentForm");
  if (form.style.display!='block')
      form.style.display='block';
  else
      form.style.display='none';
      
  return false;
}

function voteWebsiteResponse() {
	var vv = "";

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			results = http_request.responseText.split("|");
			document.getElementById('yv').innerHTML = unescape(results[0]);
			vvr = parseInt(results[1]);
			if(results[1] != "" && vvr >= 1) {
				for(j = 1;j <= vvr;j++)
					vv = vv + "<img src=\"images/star.png\" class='imageSmall'/>";
				for(k = vvr+1;k <= 5;k++)
					vv = vv + "<img src=\"images/star_vuota.png\" class='imageSmall'/>";
			}
			else
				for(k = 1;k <= 5;k++)
					vv = vv + "<img src=\"images/star_vuota.png\" />";
			document.getElementById('vv').innerHTML = vv;
			document.getElementById('nvv').innerHTML = results[2];
		}
		else {
			alert('There was a problem with the request.');
		}
	}
}

function viewWebsite(id) {

	//Versione originale con apertura della finestra in JS
	/*createHttpRequest();
	http_request.onreadystatechange = viewWebsiteResponse;
	http_request.open('GET', host + 'details.php?id=' + id, true);
	http_request.send(null);*/
	
	//Versione modificata con visualizzazione dei dati in una nuova pagina
	
	location.href="viewWebsite.php?id="+id;
}

function viewWebsiteCat(id, cat, order, page, limit) {

	//Versione originale con apertura della finestra in JS
	/*createHttpRequest();
	http_request.onreadystatechange = viewWebsiteResponse;
	http_request.open('GET', host + 'details.php?id=' + id, true);
	http_request.send(null);*/
	
	//Versione modificata con visualizzazione dei dati in una nuova pagina
	
	location.href="viewWebsite.php?id="+id+"&cat="+cat+"&order="+escape(order)+"&limit="+limit+"&page="+page+"&viewRet";
}

function viewWebsiteResponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			imZIZoom(unescape(http_request.responseText));
		}
		else {
			//alert('There was a problem with the request.');
		}
	}
}

function checkRequestForm() {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.getElementById('form_realname').value != "" && document.getElementById("form_email").value != "" && document.getElementById("form_website").value != "" && document.getElementById("form_website").value != "http://" && document.getElementById("form_title").value != "") {
		if(filter.test(document.getElementById("form_email").value)) {
			if(document.getElementById('form_contract').checked)
				return true;
			else {
				alertResString(43);
				return false;
			}
		}
		else {
			alertResString(40);
			return false;
		}
	}
	else {
		alertResString(22);
		return false;
	}
}

//PopUp function
function imGetLayer(sName) {return document.all?document.all[sName]:document.getElementById?document.getElementById(sName) : "";}

function imZIZoom(sCode) {
	thumb = 0;
	var imZIdiv_Backg = imGetLayer("imZIBackg");
	imZIdiv_Backg.innerHTML = "<div id=\"imZIPopUp\" style='top:"+(tempY+15-diffY)+"px; left:"+(tempX+15-diffX)+"px;' >" +unescape(sCode)+ "</div>";
	imZIdiv_Backg.style.display = "block";
}

function viewSearch()
{
	var searchBox = imGetLayer("searchBox");
	if (searchBox.style.display=='block')
		searchBox.style.display='none';
	else
		searchBox.style.display='block';
	
}

function resetSearchForm()
{
	imGetLayer("descr").value="";
	imGetLayer("url2").value="";
	imGetLayer("lang").selectedIndex=0;
	imGetLayer("version").selectedIndex=0;
	imGetLayer("order").selectedIndex=0;
}

function checkCommentForm()
{
  var name=imGetLayer("name").value;
  var email=imGetLayer("email").value;
  var message=imGetLayer("message").value;
  var license=imGetLayer("license").checked;
  
  //Campi completati
  if (name=="" || email=="" || message=="")
  {
    alertResString(22);
    return false;
  }
  
  //Email valida
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(email)) {
    alertResString(40);
    return false;
  } 
  
  //Licenza accettata
  if (!license)
  {
    alertResString(43);
    return false;
  }
    
  return true;
}

function segnaleComment(id)
{
  createHttpRequest();
	http_request.onreadystatechange = segnaleCommentResponse;
	http_request.open('GET', host + 'viewWebsite.php?segnale='+id, true);
	http_request.send(null);
  
  return false;
}

function segnaleCommentResponse()
{
  var vv = "";
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			alertResString(69);
		}
		else {
			//alert('There was a problem with the request.');
		}
	}
  
  return false;
}

function viewLicense()
{
  DIV=imGetLayer("dLicense");
  if (DIV.style.display=="none") DIV.style.display="block";
  else DIV.style.display="none"; 
}
function imSwapImg(oDiv, sImgName) {
if (sImgName != "")
oDiv.style.backgroundImage = "url(" + sImgName + ")"; else
oDiv.style.backgroundImage = ""; 
}
function imOpenLocation(sNewLocation){
document.location = sNewLocation; 
}   

function viewThumb(id, thumb)
{
	//solo per locale
	//var host="http://www.incomedia.info/websitex5.com/templates/";
	
	var i=0;
	while (imGetLayer("img"+i)!=null)
	{
	 changeClass('img'+i, 'wsThumb unselImage');
	 i++;
  }
  
	changeClass('img'+thumb, 'wsThumb selImage');
	var image = imGetLayer("imZIPreview");
	image.src = host + "files/medium_" + id + "_" + thumb + ".jpg";
} 

function viewDescr(id) {  
  stop=1;
	createHttpRequest();
	http_request.onreadystatechange = viewDescrResponse;
	http_request.open('GET', host + 'minidetails.php?id=' + id, true);
	http_request.send(null);
}

function viewDescrResponse() {
    if (stop) {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				imZIZoom(unescape(http_request.responseText));
			}
			else {
				//alert('There was a problem with the request.');
			}
		}
	}
}   

function getMouseXY(e) {
	  if (IE) { 
	    tempX = event.clientX + document.documentElement.scrollLeft;
	    tempY = event.clientY + document.documentElement.scrollTop;
	  } else {  
	    tempX = e.pageX;
	    tempY = e.pageY;
	  }  
	  
	  if (tempX < 0){tempX = 0}
	  if (tempY < 0){tempY = 0} 
	  
	  var margin=30;
	  //Select the position
	  
	  //Vertical
	  if (tempY + 150/2 > document.documentElement.clientHeight + document.documentElement.scrollTop - margin) {
	  	tempY=document.documentElement.clientHeight + document.documentElement.scrollTop - margin - 150;
	  }
	  else if (tempY - 150/2 < document.documentElement.scrollTop + margin)
	  {
	  	tempY=document.documentElement.scrollTop + margin/2;
	  }
	  else {
	  	tempY=tempY - 150/2;
	  }
	  
	  //Lateral	  	
	  if (tempX - document.documentElement.scrollLeft > document.documentElement.clientWidth - 500 - 2*margin)
	  	tempX = tempX - 500 - margin;
	  else
	    tempX = tempX + margin;
	  	
	  var imZIdiv_Backg = imGetLayer("imZIPopUp");	 
	  
	  if (imZIdiv_Backg) {

		  imZIdiv_Backg.style.left=tempX+"px";
	 
	 	  if (verticalMov) {
		  	imZIdiv_Backg.style.top=tempY+"px"
		  	verticalMov=false;
		  }
	  }
	  
	  return true
	}

var IE = document.all?true:false
if (!IE) {
	document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getMouseXY;

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  } 
  
function imZIHide(){
	stop=0;
	verticalMov=1;
	imGetLayer("imZIBackg").style.display = "none";
	//imGetLayer("imZIPopUp").innerHtml = "";
}

function scroll_gallery(size)
{
	if (!scrolling) {
		obj=imGetLayer("imGallery_scroll");
		
		var l = obj.style.left.substring(0, obj.style.left.indexOf("px", 0));
		
		animateScroll_gallery(l, l-size, size, 10);
	}
}

function animateScroll_gallery(from , to, size, v)
{
	obj=imGetLayer("imGallery_scroll");
	obj.style.left = from+"px";
	var w = obj.style.width.substring(0, obj.style.width.indexOf("px", 0));
	w*=1;
	to*=1;
	from*=1;
	size*=1;
	v*=1;
	scrolling = true; 
	
	if (-w < to) {
		if (to != 0) {
			if (to < from - 10)
			{ 
				if ((from - to) > size/2)
					v = v - 2;
				else if (v < -4)
					v = v + 2;
				setTimeout("animateScroll_gallery("+(from + v)+", "+to+", "+size+", "+v+")", 1);
			}
			else
			{
				obj.style.left = to+"px";
				scrolling = false;
			}
		}
		else if (from < 0)
		{
			 if (-from > size/2)
					v = v - 2;
				else if (v < -4)
					v = v + 2;
			 setTimeout("animateScroll_gallery("+(from - v)+", 0, "+size+", "+v+")", 1);
		}
		else
		{
				obj.style.left = "0px";
				scrolling = false;
		}
	}
	else
	{
		animateScroll_gallery(from, 0, size, v);
	}
}