var responseDiv;

function printPreview(){
	var id = window.document.getElementById("id").value;
	var fldr = window.document.getElementById("fldr").value;
	var width = window.document.getElementById("width").value;
	var fldr = window.document.getElementById("fldr").value;
	var sezione = window.document.getElementById("sezione").value;
	printpop = window.open("page.asp?fldr="+fldr+"&id="+id+"&print=true&sezione="+sezione, "printpopup", "width="+width+", status=no, scrollbars=yes, resizable=yes")
	printpop.focus();
}

function stampa(){
	window.document.getElementById("stampa").style.display='none';
	window.print();
	window.close();
}

function wopen(page,w,h,scroll,resizable){
	if(!scroll) scroll = 0
	if(!resizable) resizable = 0

	popup = window.open(page,"popupwin","status=0,width="+w+",height="+h+",scrollbars="+scroll+",resizable="+resizable+"",true)
	popup.focus()
}

function goTo(mypage){
	if( confirm("Sicuro di voler proseguire?") ){
		window.document.location.href = mypage;
	}
}

function resetForm(page){
	document.location.href = page
}

function upload(nFrm, nIpt, folder){
	w = 300, h = 120
	aw = screen.availWidth
	ah = screen.availHeight

	pop = window.open( "upload.asp?folder="+folder+"&action=set_upload&nFrm="+nFrm+"&nIpt="+nIpt,"myUpload","status=no,scrollbar=no,width="+w+",height="+h )
	pop.moveTo((aw/2-w/2),(ah/2-h/2))
	pop.focus();
}

function setColor(canvas,field){
	color = window.document.getElementById(field).value;
	if(color.length==7){
		window.document.getElementById(canvas).style.backgroundColor = color;
		window.document.getElementById(canvas).style.border='none'
		window.document.getElementById(field).value = color.toUpperCase()

	}
}

var loadedobjects="";

function ajaxpage(url, containerid){
	var page_request = false;
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else{
		return false;
	}

	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid)
	}

	page_request.open('GET', url, true)
	page_request.send(null)
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		if(containerid!="_blank"){
			window.document.getElementById(containerid).innerHTML=page_request.responseText;
		}
		else{
			var pop = window.open("","pop");
			pop.document.write( page_request.responseText );
			pop.document.close();
		}
	}
	else{
		window.document.getElementById(containerid).innerHTML= "<div style='position:absolute;margin:auto;top:50%;left:50%;width:40px;height:20px;'>attendere...</div>";
	}
}

function paint(obj, bool){
	link = obj.replace("_", "")
	window.document.getElementById(link).className = 'sommario';
	if(bool){

		window.document.getElementById(obj).className = '';
		window.document.getElementById(link).style.textDecorationUnderline = true;
	}
	else{
		window.document.getElementById(obj).className = 'sommario';
		window.document.getElementById(link).style.textDecorationUnderline = false;
	}
}

function roll(id, boolRoll){
	var obj = document.getElementById('IMG'+id)
	obj.blur();
	boolRoll = obj.src.indexOf("giu.gif")>0;

	if(document.getElementById){
		if(boolRoll){
			document.getElementById(id).style.display = '';
			obj.src = "images/su.gif"
			obj.title = "Nascondi"
		}
		else{
			document.getElementById(id).style.display = 'none';
			obj.src = "images/giu.gif"
			obj.title = "Mostra"
		}
	}
}
function resetSearch(obj){
	if(obj.value==''){
		obj.value='cerca nel sito...'
	}
}

function setSearch(obj){
	if(obj.value=='cerca nel sito...'){
		obj.value=''
	}
}

function KeyPress(e, Stringa, pagina){
	 try{
	   e = e || window.event;
	   var code = e.keyCode || e.which;

	   //escape
	   if(code == 27){
	   	closeSearchBox()
	   }
		else{
		   document.getElementById('img_cerca').setAttribute('event','');
		   cerca(Stringa, pagina)
		}
	 }
	 catch(e){alert(e)}
}

function closeSearchBox(){
	document.getElementById('searchBox').style.visibility='hidden';
	document.getElementById('img_cerca').src = "images/cerca.gif";
	document.getElementById('img_cerca').setAttribute('event','');
	document.getElementById('img_cerca').title = "cerca nel sito";
}

function cerca(Stringa, pagina){
	responseDiv = document.getElementById("contentSearch");

	var regex = new RegExp("[a-z0-9]");
	if(regex.test(Stringa)){
		try{
			if(document.getElementById('img_cerca').getAttribute("event")=='click' && document.getElementById('img_cerca').src.indexOf("images/stopSearch.gif")>-1){
				closeSearchBox();
			}
			else{
				if(Stringa.length>0&&Stringa!='cerca nel sito...'){
					document.getElementById('img_cerca').src = "images/ajax-loader-small.gif"
					document.getElementById('img_cerca').title = "ricerca in corso..."
					document.getElementById('img_cerca').setAttribute('event','');
					document.getElementById('cerca').focus()

					cercaStringa(Stringa, pagina)
				}
			}
		}
		catch(e){}
	}
	else{
		closeSearchBox()
	}
}

var myRequest = null;

function CreateXmlHttpReq(handler) {
	try{
		var xmlhttp = null;
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		xmlhttp.onreadystatechange = handler;
		return xmlhttp;
	}
	catch(e){}
}

function myHandler(){
	try{
		if (myRequest.readyState == 4 && myRequest.status == 200) {
			var Stringa = myRequest.responseText;
			responseDiv.innerHTML = Stringa;

			if(responseDiv.id=="contentSearch"){
				var searchBox = document.getElementById('searchBox')
				if(searchBox.style.visibility!='visible'){
					searchBox.style.visibility='visible';
					searchBox.style.left = 0;
				}
				try{
					var el = document.getElementById('contentsummary');
		  			el.style.top= 0;
		  		}
		  		catch(e){}
				document.getElementById('img_cerca').src = "images/stopSearch.gif"
				document.getElementById('img_cerca').title = "chiudi"
			}

		}
		else if(myRequest.status > 400){
			var str = "<div class='sContenitore'>"
			str = str + "<br>E' stato riscontrato un errore durante la fase di ricerca."
			str = str + "</div>"
			div.innerHTML = str;

			if(responseDiv.id=="contentSearch"){
				document.getElementById('img_cerca').src = "cerca";
			}
		}
	}
	catch(e){}
}

function cercaStringa(Stringa, pagina) {
	try{
		var Location = location.protocol + "//" + location.hostname + "/"
		myRequest = CreateXmlHttpReq(myHandler);
		myRequest.open("GET",Location+"search.asp?rand="+escape(Math.random())+"&cerca="+Stringa+"&page="+pagina);
		myRequest.send(null);
	}
	catch(e){}
}

function sendRequest(address) {
	try{
		var Location = location.protocol + "//" + location.hostname + "/"
		myRequest = CreateXmlHttpReq(myHandler);
		myRequest.open("GET",Location + address + "&rand="+escape(Math.random()));
		myRequest.send(null);
	}
	catch(e){}
}

function tdEditMng(id){
	var td = document.getElementById(id)
	if(td){
		if(td.style.display==''){
			td.style.display='none';
		}
		else{
			td.style.display='';
		}
	}
}

/* trim() function */
String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};
