function toggleDiv(whichLayer){
    var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	   elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	   elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	  elem = document.layers[whichLayer];
	 
	if (elem) {
        vis = elem.style;

        // if the style.display value is blank we try to figure it out here
        if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
            vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
        vis.display = (vis.display==''||vis.display=='block')?'none':'block';
    }
}

function showDiv(whichLayer)
{
    var elem, vis;
    if( document.getElementById ) // this is the way the standards work
       elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
       elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
      elem = document.layers[whichLayer];
      
    if (elem) {

        vis = elem.style;

        // if the style.display value is blank we try to figure it out here
        vis.display = 'block';
    }
}

function hideDiv(whichLayer)
{
    var elem, vis;
    if( document.getElementById ) // this is the way the standards work
       elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
       elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
      elem = document.layers[whichLayer];
      
    if (elem) {

        vis = elem.style;

        // if the style.display value is blank we try to figure it out here
        vis.display = 'none';
    }
}

function hideElement (elementId) {
var element;
if (document.all)
element = document.all[elementId];
else if (document.getElementById)
element = document.getElementById(elementId);
if (element && element.style)
element.style.display = 'none';
}

function showElement (elementId) {
var element;
if (document.all)
element = document.all[elementId];
else if (document.getElementById)
element = document.getElementById(elementId);
if (element && element.style)
element.style.display = '';
}

function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
}

function filePreview(t,i)
{
    sLoc='file.php?type='+t+'&id='+i;
    var w = window.open(sLoc);
    if(!w) alert('Rilevato blocco pop-up');
}

function copyValueToField(sourceFieldValue, destFieldId)
{
    var f = document.getElementById(destFieldId);
    if (f) { f.value = sourceFieldValue; }
}

function updateCf(strCfFieldName, strValue) 
{
    var f = document.getElementById(strCfFieldName);
    if (f && f.value == "") copyValueToField(strValue, strCfFieldName);
}

function select_all(formid, name, value) {
    formblock= document.getElementById(formid);

    if(!formblock) return;
    forminputs = formblock.getElementsByTagName('input');

	for (i = 0; i < forminputs.length; i++) {
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		
		if (regex.test(forminputs[i].getAttribute('name'))) {
			if (value == '1') {
			  forminputs[i].checked = true;
			} else {
			  forminputs[i].checked = false;
			}
	    }
    }
}