/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function showMenu(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display='block';
	} else {
		alert('Errore - Menu non disponibile.');
	}
}

function hideMenu(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display='none';
	} else {
		alert('Errore - Menu non disponibile.');
	}
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (dtStr==''){
		alert("Per favore, inserisci la data di riferimento per l'articolo")
		return false
	}
	if (pos1==-1 || pos2==-1){
		alert("Il formato della data deve essere : gg/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Per favore, inserisci un mese valido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Per favore, inserisci un giorno valido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Per favore, inserisci anno di 4 cifre valido tra il "+minYear+" e il "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Per favore, inserisci una data valida")
		return false
	}
return true
}

function scegliPid(val,frmact,frmval) {
  if ((frmact) && (frmval)) {
    document.menuform.action=document.menuform.action+'&'+frmact+'='+frmval;
  }
  document.menuform.pid.value=val;
  document.menuform.submit();
}

// Display weather div
var wdsip=true;
function displayWeather() {
	if (document.getElementById('phpweather')) {
		d=document.getElementById('phpweather');
		wdsip=(wdsip)?false:true;
		d.style.display=(wdsip)?'none':'block';
	}
}

// Hide and Show layers
function displayDiv(nome,closeAll) {
	if (document.getElementById(nome)) {
		if ((closeAll) && (divArray)) {
			for (var i=0;i<divArray.length;i++) {
				document.getElementById(divArray[i]).style.display='none';
			}
		} else {
			document.getElementById(nome).style.display=(((document.getElementById(nome).style.display=='') || (document.getElementById(nome).style.display=='none'))?'block':'none');
		}
	}
}

function roundNumber(rnum,rlength) {
	if ((rnum > 8191) && (rnum < 10485)) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}

function strRoundNumber(rnum,rlength) {
	if ((rnum > 8191) && (rnum < 10485)) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	newnumber=''+newnumber;
	var strnum=newnumber.split('.');
	if ((strnum.length==2) && (strnum[1].length==2))
		return newnumber.replace('.',',');
	else if ((strnum.length==2) && (strnum[1].length==1))
		newnumber=newnumber+'0';
	else
		newnumber=newnumber+'.00';
	return newnumber.replace('.',',');
}

function trim(str) {
	if (str=='')
		return '';
	return str.replace(/^\s*|\s*$/g,"");
}

function validate_select(sl,nq) {
	if (sl.selectedIndex==-1)
		return false;
	var ivs=0;
	for (ivs = 0; ivs < sl.length; ivs++) {
		if ((ivs==sl.selectedIndex) && (sl.options[ivs].value=='null'))
			return false;
		if ((nq) && (ivs==sl.selectedIndex) && (sl.options[ivs].value=='qualsiasi'))
			return false
		else if (ivs==sl.selectedIndex)
			return true;
	}
	return false;
}

function validate_radio(but,rv) {
	// place any other field validations that you require here
	// validate but
	var myOption = -1;
	var ivr=0;
	for (ivr=0; ivr<but.length; ivr++) {
		if (but[ivr].checked) {
			myOption = ivr;
			break;
		}
	}
	if (myOption == -1)
		return false;
	if (rv)
		return but[myOption].value;
	return true;
}

function doBlink() {
	var blink = document.all.tags("blink")
	for (var i=0; i<blink.length; i++)
		blink[i].style.visibility = blink[i].style.visibility =='' ? 'hidden' : '';
}

function startBlink() {
	if (document.all)
		setInterval("doBlink()",700)
}

/**
 * SCRIP PER IL CARICAMENTO DEI PID
 */
var mousex = 0;
var mousey = 0;
var algor = 0;

function init() {
  document.onmousemove = getMouseXY; // update(event) implied on NS, update(null) implied on IE
  getMouseXY();
}

var pw,ph;
function getPageDim() {
if (self.innerHeight) // all except Explorer
{
	pw = self.innerWidth;
	ph = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
{
	pw = document.documentElement.clientWidth;
	ph = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
	pw = document.body.clientWidth;
	ph = document.body.clientHeight;
}
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}

/**
 * DHTML phone number validation script.
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone) {
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	if (isInteger(s) && s.length >= minDigitsInIPhoneNumber)
		return true;
	alert('Il numero di telefono inserito non \u00E8 valido.');
	return false;
}

function isNumber(strString) {
	//  check for valid numeric strings
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0)
		return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (digits.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

/**
 * Script funzionali per Ajax
 */

function nodes2Del(nome,re) {
	var hd=document.getElementsByTagName('head');
	var i=0;
	for (i=0; i<hd.length; i++) {
		if (hd[i].childNodes) {
			var n=0;
			var ncss=-1;
			for(n=0;n<hd[i].childNodes.length;n++) {
				if ((nome=='LINK') && (hd[i].childNodes[n].nodeName.toUpperCase()==nome) && (re.test(hd[i].childNodes[n].href))) {
					hd[i].removeChild(hd[i].childNodes[n]);
					return true;
				}
				if ((nome=='SCRIPT') && (hd[i].childNodes[n].nodeName.toUpperCase()==nome) && (re.test(hd[i].childNodes[n].src))) {
					hd[i].removeChild(hd[i].childNodes[n]);
					return true;
				}
			}
		}
	}
	return false;
}

/**
 * DHTML email validation script.
 */

function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Formato email non valido")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Formato email non valido")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Formato email non valido")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Formato email non valido")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Formato email non valido")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Formato email non valido")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Formato email non valido")
		    return false
		 }
		 return true
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function js_in_array(the_needle, the_haystack) {
	var the_hay = the_haystack.toString();
	if(the_hay == ''){
		return false;
	}
	var the_pattern = new RegExp(the_needle, 'g');
	var matched = the_pattern.test(the_haystack);
	return matched;
}

// Calcolo offset

function calcOffsetLeft(obj){
	return parsePar(obj,"offsetLeft")
}

function calcOffsetTop(obj){
	return parsePar(obj,"offsetTop")
}

function calcWidth(obj) {
	if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){
		return obj.offsetWidth-2
	}else{
		return obj.offsetWidth
	}
}

function parsePar(obj,attr){
  var kb=0;
  while(obj){
    kb+=obj[attr];
    obj=obj.offsetParent
  }
  return kb
}

var _imgField=null;
function addBigUteImg(obj) {
	if (document.getElementById(obj+'_s'))
		var _imgField_s=document.getElementById(obj+'_s');
	else
		return false;
	_imgField=document.getElementById(obj);
	_imgField_s.onmouseover=showBigUteDiv;
	_imgField_s.onmouseout=hideBigUteDiv;
	var _imgDiv=document.createElement("DIV");
	_imgDiv.id=obj+'_d';
	var _imgImg = new Image();
	_imgImg.src = _imgField.src;
	var ca=1;
	var Aa=1;
	_imgDiv.style.borderRight="black "+ca+"px solid";
	_imgDiv.style.borderLeft="black "+ca+"px solid";
	_imgDiv.style.borderTop="black "+Aa+"px solid";
	_imgDiv.style.borderBottom="black "+Aa+"px solid";
	_imgDiv.style.width=_imgImg.width+'px';
	_imgDiv.style.height=_imgImg.height+'px';
	_imgDiv.style.left=calcOffsetLeft(_imgField)+"px";
	_imgDiv.style.top=calcOffsetTop(_imgField)+"px";
	_imgDiv.style.zIndex="1";
	_imgDiv.style.paddingRight="0";
	_imgDiv.style.paddingLeft="0";
	_imgDiv.style.paddingTop="0";
	_imgDiv.style.paddingBottom="0";
	_imgDiv.style.visibility="visible";
	_imgDiv.style.position="absolute";
	_imgDiv.style.backgroundColor="white";
	_imgDiv.innerHTML='<img src="'+_imgField.src+'" align="middle" border="0"/>';
	_imgDiv.onmouseover=showBigUteDiv;
	_imgDiv.onmouseout=hideBigUteDiv;
	document.body.appendChild(_imgDiv);
	return true;
}

function showBigUteDiv() {
	if ((_imgField) && (document.getElementById(_imgField.id+'_d')))
		document.getElementById(_imgField.id+'_d').style.visibility="visible";
	return true;
}

function hideBigUteDiv() {
	if ((_imgField) && (document.getElementById(_imgField.id+'_d')))
		document.getElementById(_imgField.id+'_d').style.visibility="hidden";
	return true;
}

function showBigUteImg(obj) {
	if (document.getElementById(obj+'_d'))
		document.body.removeChild(document.getElementById(obj+'_d'));
	
	if (addBigUteImg(obj)) {
		return true;
	} else {
		return false;
	}
}

