// seleziona e deseleziona un gruppo di checkbox
function setCheckboxes(form_name, do_check) {
	var numberOf_elements = document.forms[form_name].elements.length;

	for (var i = 0; i < numberOf_elements; i++) {
		var this_element = document.forms[form_name].elements[i];
		if (this_element.type == "checkbox")
			this_element.checked = do_check;
	}

	return true;
}

// visualizza l'anteprima di una icona
function icon_preview(preview_element, icon_path, select_name) {
	//alert(select_name.value);
	//alert(document.getElementById(preview_element).src);
	//alert(document.getElementById(preview_element).style.visibility);
	if (select_name.value!='') {
	   document.getElementById(preview_element).style.visibility='visible';
	   document.getElementById(preview_element).style.position='relative';
	   document.getElementById(preview_element).src = icon_path + select_name.value;
	}
	else {
	   document.getElementById(preview_element).style.visibility='hidden';
	   document.getElementById(preview_element).style.position='absolute';
	}
}

// ----------------------------------------------------------------------------
// HasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function HasClassName(objElement, strClass) {

	// if there is a class
	if (objElement.className) {

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for (var i = 0; i < arrList.length; i++) {
			// if class found
			if (arrList[i].toUpperCase() == strClassUpper) {
				// we found it
				return true;
			}
		}
	}

	// if we got here then the class name is not there
	return false;

}
// 
// HasClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function AddClassName(objElement, strClass, blnMayAlreadyExist) {

	// if there is a class
	if (objElement.className) {
		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// if the new class name may already exist in list
		if (blnMayAlreadyExist) {
			// get uppercase class for comparison purposes
			var strClassUpper = strClass.toUpperCase();

			// find all instances and remove them
			for (var i = 0; i < arrList.length; i++) {
				// if class found
				if (arrList[i].toUpperCase() == strClassUpper) {
					// remove array item
					arrList.splice(i, 1);

					// decrement loop counter as we have adjusted the array's contents
					i--;

				}
			}
		}

		// add the new class to end of list
		arrList[arrList.length] = strClass;

		// add the new class to beginning of list
		//arrList.splice(0, 0, strClass);
      
		// assign modified class name attribute
		objElement.className = arrList.join(' ');

	}
	// if there was no class
	else {
		// assign modified class name attribute
		objElement.className = strClass;

	}
}
// 
// AddClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function RemoveClassName(objElement, strClass) {

	// if there is a class
	if (objElement.className) {

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for (var i = 0; i < arrList.length; i++) {

			// if class found
			if (arrList[i].toUpperCase() == strClassUpper) {
				// remove array item
				arrList.splice(i, 1);

				// decrement loop counter as we have adjusted the array's contents
				i--;

			}
		}

		// assign modified class name attribute
		objElement.className = arrList.join(' ');

	}
	// if there was no class
	// there is nothing to remove

}
// 
// RemoveClassName
// ----------------------------------------------------------------------------

// visualizza o nascondi un oggetto
function show_hide(object_id, icon_element, icon_path){
	div = document.getElementById(object_id);
	
	if (icon_element != '') {
		image = document.getElementById(icon_element);
	}

	/*alert(HasClassName(div, 'hidden'));
	alert('Prima: ' + div.className);*/

	if (HasClassName(div, 'hidden') == true) {
		/*AddClassName(div, 'show', true);*/
	   RemoveClassName(div, 'hidden');
	   if (icon_element != '') {
	   	//image.src = icon_path + "view_right.gif";
	   	image.src = icon_path + "view_down.gif";
	   }
	}
	else {
	   AddClassName(div, 'hidden', true);
	   /*RemoveClassName(div, 'show');*/
	   if (icon_element != '') {
	   	//image.src = icon_path + "view_down.gif";
	   	image.src = icon_path + "view_right.gif";
	   }
	}
	/*alert('Dopo: ' + div.className);*/
}

// limita i caratteri presenti in una textarea o in un textbox
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};
	}
}
function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}

// sostituto di target="_blank"
function externalLinks() {
	if (!document.getElementsByTagName)
		return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
 		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

// trova lo stile leggendo nel css
function getStyleProp(x,prop){
	if(x.currentStyle)
   	return(x.currentStyle[prop]);
	if(document.defaultView.getComputedStyle)
   	return(document.defaultView.getComputedStyle(x,'')[prop]);
	return(null);
}
// crea un cookie
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 expires = '';
  document.cookie = name+'='+value+expires+'; path=/';
}
// legge i dati di un cookie
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;
}
// setta il font size
function setFontSize(action) {
	var body = document.getElementsByTagName("body")[0];
	var default_size = "62.5%";
	var min_size = 62.5;
	var max_size = 100;
	var size_unit = 6.25;
	
	// trovo la dimensione dei font in uso
	if (readCookie('dg_font_size') == null)
		var current_size = ( getStyleProp(body, "fontSize") == "" || getStyleProp(body, "fontSize") == "undefined" ? default_size : getStyleProp(body, "fontSize") );
	else
		var current_size = unescape ( readCookie('dg_font_size') )
	
	// in caso la dimensione sia salvata in pixel, la trasformo in percentuale
	if (current_size.match("px"))
		var current_size = String ( ( parseFloat ( current_size ) * size_unit ) ) + "%";
	
	// trovo la nuova dimensione dei font
	if (action == "increase" && parseFloat ( current_size ) < max_size)
		var new_font_size = String ( ( parseFloat ( current_size ) + size_unit ) ) + "%";
	
	else if (action == "decrease" && parseFloat ( current_size ) > min_size)
		var new_font_size = String ( ( parseFloat ( current_size ) - size_unit ) ) + "%";
		
	else {
		var new_font_size = current_size;
	}
	
	// imposto la nuova dimensione dei font
	//alert(new_font_size);
	body.style.fontSize = new_font_size;
	
	// salvo la nuova dimensione dei font nel cookie
	if (action == "increase" || action == "decrease" || readCookie('dg_font_size') == null)
		createCookie('dg_font_size', escape ( body.style.fontSize ), 365);
}

// carica le funzioni che devono essere caricate onload
window.onload = function() {
	setFontSize('read');
	externalLinks();
}

// casella che si cancella al click del mouse
function delDefaultValue(elem) {
	elemChange = document.getElementById(elem);
	if (elemChange.value == elemChange.defaultValue) {
		elemChange.value='';
	}
	elemChange.style.color = '#000';
}

function checkEmptyValue(elem) {
	elemChange = document.getElementById(elem);
	if (elemChange.value == '') {
		elemChange.style.color = '#bbb';
		elemChange.value = elemChange.defaultValue;
	}
}
