// ------------------------------------------------------------------------- //
// - SmartLab Commons Javascript Functions                                 - //
// ------------------------------------------------------------------------- //

/**
 * Opens a pop up window.
 * @param	url		the url to be opened in the new window (defaults to
 *					current location)
 * @param	form	the form whose fields will be passed to the new window
 *					(defaults to none)
 * @param	id		the window unique identifier (defaults to browser assigned)
 * @param	x		horizontal offset of the upper left corner (defaults to
 *					centering the window on the screen)
 * @param	y		vertical offset of the upper left corner (defaults to
 *					centering the window on the screen)
 * @param	width	the new window width (defaults to half screen width)
 * @param	height	the new window height (defaults to half screen height)
 * @param	scroll	defines if the new window has scrollbars (defaults to no)
 * @param	resize	defines if the new window could be resized (defaults to no)
 * @param	options	defines the new window additional options (defaults to
 *					none)
 *
 * @return	the newly created window
 */
function popup(url, form, id, x, y, width, height, scroll, resize, options) {
  if (width == null || width == undefined) {
    width = screen.width / 2;
  }
  if (height == null || height == undefined) {
    height = screen.height / 2;
  }
  if (x == null || x == undefined) {
    x = ((screen.width - width) / 2);
  }
  if (y == null || y == undefined) {
    y = ((screen.height - height) / 2);
  }
  if (scroll == null || scroll == undefined || scroll == false) {
    scroll = 'NO';
  } else {
    scroll = 'YES';
  }
  if (resize == null || resize == undefined || resize == false) {
    resize = 'NO';
  } else {
    resize = 'YES';
  }
  if (options == null || options == undefined) {
    options = ',TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,STATUS=NO,MENUBAR=NO,COPYHISTORY=NO';
  } else {
    options = ',' + options;
  }
  if (form != null && form != undefined) {
    url = url + '?';
    for (i = 0; i < document.forms[form].elements.length; i++) {
      if (document.forms[form].elements[i].name != "" && document.forms[form].elements[i].value != "") {
        url = url + document.forms[form].elements[i].name + "=" + document.forms[form].elements[i].value + "&";
      }
    }
  }
  var popup = window.open(url,id,'WIDTH=' + width + ',HEIGHT=' + height + ',SCREENX=' + x + ',SCREENY=' + y  + ',SCROLLBARS=' + scroll + ',RESIZABLE=' + resize + options);
  popup.focus();
  return popup;
}

/**
 * Closes a pop up window returning main control and focus to the pop up opener.
 * @param	form		the form whose fields will be passed to the window who
 *						opened the popup (defaults to none)
 * @param	location	the url where the window who opened the popup should be
 *						redirected (defaults to the current location without
 *						any parameter)
 */
function popdown(form, location) {
  if (form == null || form == undefined) {
    if (location == null || location == undefined) {
      window.opener.location.reload();
    } else {
      window.opener.location.href = location;
    }
  } else {
    var query = '?';
    for (i = 0; i < document.forms[form].elements.length; i++) {
      if (document.forms[form].elements[i].name != "" && document.forms[form].elements[i].value != "") {
		  var element=document.forms[form].elements[i];		  
		  if(!element.checked && (element.type=="RADIO" || element.type=='radio' || element.type=='checkbox' || element.type=='CHECKBOX')) {
			  continue;
		  }
        query = query + document.forms[form].elements[i].name + "=" + document.forms[form].elements[i].value + "&";
      }
    }
    var url = location;
    if (location == null || location == undefined) {
      url = window.opener.location.href.substring(0, window.opener.location.href.indexOf('?'));
      if (url == "") {
        url = window.opener.location.href;
      }
    }
    window.opener.location.href = url + query;
  }
  window.close();
}

/**
 * Changes the form target location and submits it.
 * @param	action		the url indicating the new target location (required)
 * @param	form		the form whose target location will be changed
 *						(defaults to the first form whose name is not 'login')
 */
 /*
function redirect(action, form) {
  if (form == null || form == undefined) {
    for (i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "login") {
        form = document.forms[i].name;
        break;
      }
    }
  }
  document.forms[form].action = action;
  document.forms[form].submit();
}
*/
function redirect(action, form) {
  if (form == null || form == undefined || form == 'undefined') {
    for (i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "login") {
        form = document.forms[i].name;
        break;
      }
    }  
  }
   if(document.forms[form]!=null && document.forms[form]!=undefined){
  		document.forms[form].action = action;
		document.forms[form].submit();
	}else{
		document.location.href=action;
	}
  
}
/**
 * Sets the ordering and refresh the list
 * @param	column		the ordering column name (required)
 * @param	form		the form containing the ordering column field (defaults
 *						to the first form containing an 'order' element)
 */
function order(column, form) {
  if (form == null || form == undefined) {
    for (i = 0; i < document.forms.length; i++) {
      if (document.forms[i].elements['order'] != null) {
        form = document.forms[i].name;
        break;
      }
    }
  }
  if (document.forms[form].order.value == column) {
    column = '!' + column;
  }
  document.forms[form].order.value = column;
  document.forms[form].action = document.location.href.substring(0, document.location.href.indexOf('?'));
  document.forms[form].submit();
}

/**
 * Sets or unsets all checkbox of a specified group
 * @param	group		the name of the checkbox group to be switched
 *						(defaults to 'selection')
 * @param	form		the form whose checkbox group will be switched
 *						(defaults to the first form whose name is not 'login')
 */
var global_multiselect = true;
function multiselect(group, form) {
  if (form == null || form == undefined) {
    for (i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "login") {
        form = document.forms[i].name;
        break;
      }
    }
  }
  if (group == null || group == undefined) {
    group = 'selection';
  }
  var elements = document.forms[form].elements;
  for (i = 0; i < elements.length; i++) {
    if (elements[i].type == 'checkbox' && !elements[i].disabled && elements[i].name == group) {
      elements[i].checked = global_multiselect;
    }
  }
  global_multiselect = !global_multiselect;
}

function selectRaw(a)
{
  a.style.backgroundColor = '#C1D2EE';
}

function unselectRaw(a,color)
{
  if(color==null){	
  	a.style.backgroundColor = '';
  }else{
  	a.style.backgroundColor = color;
  }
}

function conferma(url) {
	var answer = confirm("Sei sicuro di voler eliminare l'oggetto? ")
	if (answer){
		window.location = url;
	}
	else{
	}
}

function confermaRichiesta(url,messaggio) {
	var answer = confirm(messaggio)
	if (answer){
		window.location = url;
	}
	else{
	}
}

function Mostra() {
    if(detectBrowser()){
        var elementi=document.forms[0].getElementsByTagName('select');
        if(elementi.length>0){
            for (i = 0; i < elementi.length; i++) {
                 elementi[i].style.visibility="hidden";
                 
            }
        }
    }
}

function Nascondi() {
    if(detectBrowser()){
		var elementi=document.forms[0].getElementsByTagName('select');
		if(elementi.length>0){
			for (i = 0; i < elementi.length; i++) {
		      elementi[i].style.visibility="visible";
		    }
		}
    }
}
function detectBrowser()
{
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion>=8)
            return false;
        else if (ieversion>=7)
            return false;
        else if (ieversion>=6)
            return true;
        else if (ieversion>=5)
            return true;
    }else
        return false;
}
function confermaSub(url) {
	var answer = confirm("Eliminando l'obiettivo tutti i sub-obiettivi associati saranno eliminati procedere comunque? ")
	if (answer){
		window.location = url;
	}
	else{
	return;
	}
}
function confermaGruppo(url) {
	var answer = confirm("Sicuro di voler eliminare il gruppo di lavoro? ")
	if (answer){
		window.location = url;
	}
	else{
	return;
	}
}
function confermaRisorsaToGruppo(url) {
	var answer = confirm("Sicuro di voler rimuovere la risorsa dal gruppo di lavoro? ")
	if (answer){
		window.location = url;
	}
	else{
	return;
	}
}
function confermaProgetto(url) {
	var answer = confirm("Eliminando il progetto gli obiettivi, le attivita' e gli eventi associati verranno rimossi,continuare? ")
	if (answer){
		window.location = url;
	}
	else{
	}
}
