/** COOKIE MGT FUNCTIONS ***/
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
/** END COOKIE MGT FUNCTIONS ***/

/** QUERYSTRING MGT FUNCTIONS ***/
function URLencode(sStr) {
  return escape(sStr)
     .replace(/\+/g, '%2B')
        .replace(/\"/g,'%22')
           .replace(/\'/g, '%27');
}
/** END QUERYSTRING MGT FUNCTIONS ***/

//this function is deprecated, use prototype.js
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';
    }
}

//this function is deprecated, use prototype.js
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';
    }
}

//this function is deprecated, use prototype.js
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';
    }
}

//this function is deprecated, use prototype.js
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';
}

//this function is deprecated, use prototype.js
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;
			}
	    }
    }
}