// all rights reserved
// creator alessandro.ilardo@my-sandro.net - 2005

//parse a string into a date
function makeDate(txt){
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if(re.test(txt)){
		var adata = txt.split('/');
		var gg = parseInt(adata[0],10);
		var mm = parseInt(adata[1],10);
		var aaaa = parseInt(adata[2],10);
		var xdata = new Date(aaaa,mm-1,gg)
		if((xdata.getFullYear()==aaaa) && (xdata.getMonth()==mm-1) && (xdata.getDate()==gg))
			return xdata 
		else false;
	}else false; 
}


//change visible status
function changeVisible(value){
	if(document.getElementById(value).style.display!="none"){
			document.getElementById(value).style.display="none";
		}else{
			document.getElementById(value).style.display="block";
			}	
	}
	
//ask before go on
function _delete(question){
	if(confirm(question)) return true;
	else return false;
	}
	
//checks whatever the the user has voted THIS poll
//required an input as the id polls
function poll(form){
	var x = true;
	var id_survey = form.id.value+"=";
	var allCookies = document.cookie;
    var position = allCookies.indexOf(id_survey);
	if(position == -1){
		var expireDate = new Date();
 		expireDate.setMonth(expireDate.getYear()+1);
		document.cookie = id_survey + x + ";expires=" + expireDate.toGMTString();
		return true;
		}else{
			alert("Non è possibile votare più di una volta a sondaggio.");
			return false;
			}
	}

//checks if the email adress passed as parameter is valid
function checkEmail(value){
	if(value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1) return true;
	else return false;
	}
	
//returns the input fields evidenced by an error to the normal status
function resetColor(f){
 for (var i=0; i<f.elements.length; i++) {
  curObj = f.elements[i];
  if (curObj.tagName == 'INPUT' && curObj.type == 'text' && curObj.id != '') {
   document.getElementById(curObj.id).style.border="1px solid";
   document.getElementById(curObj.id).style.borderColor="#cccccc";
  }
 }
}

//change color for an input text where the user has submited an invalid value
function changeColor(_value){
	document.getElementById(_value).style.border="2px solid";
	document.getElementById(_value).style.borderColor="#ff9933";
	document.getElementById(_value).style.backgroundColor="#FFF";
	document.getElementById(_value).style.color="#000000";
	}

//increase char size and keep it stored as cookie
// <body onload="SettingChar();">
function SettingChar(){
  if(document.cookie.indexOf("FontSelected=")!=-1){
	FontSelected = document.cookie;
	var cut = FontSelected.indexOf("FontSelected=");
	var FontSelectedOk = FontSelected.substring(cut+13,cut+14);
    FontSelected = FontSelectedOk;
  } else {
    FontSelected = 1;
  }
  if(FontSelected==""){ FontSelected = 1; }
  DimCar(FontSelected);
}
function DimCar(whatDim){
    if(whatDim<1 || whatDim>3){ whatDim = 1; }
	Content = "11px";
	if(whatDim==1){ Content = "11px"; }
    if(whatDim==2){ Content = "12px"; }
    if(whatDim==3){ Content = "13px"; }
	if(document.getElementById("content")) document.getElementById("content").style.fontSize = Content;
   expireDate = new Date;
   expireDate.setMonth(expireDate.getMonth()+6);
   document.cookie = "FontSelected=" + whatDim + ";expires=" + expireDate.toGMTString();
}

//opens and closes pop up window
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

//display random images
/*<script language='javascript'>
document.write('<img class="homeTitles" style="padding-right:5px;"
src="'+randomImage(["upload/primo_piano/default_1.jpg",50,50,"upload/primo_piano/default_2.jpg",25,25,"upload/primo_piano/default_3.jpg",50,25])+'" 
width="90" height="120" alt="foto" />');
</script>*/
function randomImage(imgArr)
{
  var imgSrc, imgW, imgH, r;
  r = randomNumber(imgArr.length / 3);
  
  imgSrc = imgArr[r * 3];
  imgW = imgArr[(r * 3)+1];
  imgH = imgArr[(r * 3)+2];
  return imgSrc;
}
function randomNumber(limit){
  return Math.floor(Math.random()*limit);
}

//display clock
// <body onload="startclock();">
function startclock() {
var mnames = new Array('Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic');
var dnames = new Array('Dom','Lun','Mar','Mer','Gio','Ven','Sab'); 
var ntime  = new Date();

var nhour = ntime.getHours();
var nmin  = ntime.getMinutes();
var nsec  = ntime.getSeconds();
var nday  = ntime.getDate();
var nyear = ntime.getYear();

if (nyear< 1900) { nyear += 1900; }
if (nsec < 10)   { nsec="0"+nsec; }
if (nmin < 10)   { nmin="0"+nmin; }
if (nday < 10)   { nday="0"+nday; }

document.getElementById("myclock").innerHTML = dnames[ntime.getDay()] + ", " + nday  +  " " + mnames[ntime.getMonth()] + " " + nyear + " - " + nhour + ":" + nmin + ":" + nsec + "&nbsp;";
setTimeout('startclock()', 1000);
}

// check if it's a valid date
// 11/03/2005
function dateValid(txt){
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if(re.test(txt)){
		var adata = txt.split('/');
		var gg = parseInt(adata[0],10);
		var mm = parseInt(adata[1],10);
		var aaaa = parseInt(adata[2],10);
		var xdata = new Date(aaaa,mm-1,gg)
		if((xdata.getFullYear()==aaaa) && (xdata.getMonth()==mm-1) && (xdata.getDate()==gg)) {
			return true;
			
		} else {
			return false;
		}
	} else {
		return false;
	}
}

//checks if the file attached is allowed
// form.file.value
// .jpg.gif.png.pdf.
function checkFile(fil, perm) {
// controllo il tipo di file
	ext = fil.substr(fil.lastIndexOf("."))+".";
	if(perm.indexOf(ext.toLowerCase())<0){
		//alert("Il tipo di file non è un'immagine.");
		return false;
	} else {
		return true;
	}
}

//use this script to write in the text area
// <body onload="init();">
// <textarea id="contenuto" ..
ie4 = (document.all)? true:false

function init()
{
 // init global variables
 ta = document.getElementById("contenuto");

 if(ta.selectionStart != undefined) { // has support for DOM2 Ranging in textarea
   ta.addEventListener("keypress", checkKey, true); // add listener
 }
}

function tag(newTag)
{
//IE support for inserting HTML into textarea
if (document.selection)
{
 //get selected text
 var sel = document.selection;
 var rng = sel.createRange();
 
 input = document.all["contenuto"];  
 input.focus(input.caretPos);
 input.caretPos = document.selection.createRange();
 
 if (sel.type == "Text") // If the selected object is of type text, store it
 {
  text = rng.text;
 
  if ((newTag != "hr") && (newTag != "bullet")) // If tag needs to appear before and after selected text
  {
   // Insert brackets around the newTag, and insert it before and after the selected text.
   rng.text = "<" + newTag + ">" + text + "</" + newTag + ">";
  }
  else // If tag only needs to appear after selected text.
  {  
   // Insert brackets around the newTag, and insert it only after the selected text.
   if (newTag == "hr")
   { input.caretPos.text = "<" + newTag + " />"; }
   else if (newTag == "bullet")
   { input.caretPos.text = "<" + newTag + ">"; }
   else if (newTag == "br")
   { input.caretPos.text = "<" + newTag + " />"; }
  }
 }
 else // If tag only needs to appear once after selected text.
 {  
  // If tag only needs to appear after selected text.
  if (newTag == "hr")
  { input.caretPos.text = "<" + newTag + " />"; }
  else if (newTag == "bullet")
  { input.caretPos.text = "<" + newTag + ">"; }
  else if (newTag == "br")
   { input.caretPos.text = "<" + newTag + " />"; }
  input.focus();
 }
}
else // Mozilla/Netscape support for inserting HTML into textarea
{
 start = ta.selectionStart;
 end = ta.selectionEnd;

 // Split before, sel, after and insert tags in between  
 before = (ta.value).substring(0, start);
 sel = (ta.value).substring(start, end);
 after = (ta.value).substring(end, ta.textLength);
 
 if ((newTag != "hr") && (newTag != "bullet") && (sel.length > 0)) // If tag needs to appear before and after selected text
 {
  ta.value = before + "<" + newTag + ">" + sel + "</" + newTag + ">" + after;

  // set focus in textarea box
  ta.focus();
  ta.selectionStart = end + 5 + (newTag.length * 2);
  ta.selectionEnd = ta.selectionStart;
 }
 else // If tag only needs to appear once after selected text.
 {
  if (newTag == "hr")
  {
   ta.value = before + "<" + newTag + " />" + after;
   // set focus in textarea box
   ta.focus();
   ta.selectionStart = end + 4 + newTag.length - sel.length;
  }
  else if (newTag == "bullet")
  {
   ta.value = before + "<" + newTag + ">" + after;
   // set focus in textarea box
   ta.focus();
   ta.selectionStart = end + 2 + newTag.length - sel.length;
  }
  else if (newTag == "br")
  {
   ta.value = before + "<" + newTag + " />" + after;
   // set focus in textarea box
   ta.focus();
   ta.selectionStart = end + 4 + newTag.length - sel.length;
  }
  ta.selectionEnd = ta.selectionStart;
 }
}
}