

function getBrowserType()
{
	try{this.AgentName=navigator.userAgent.toLowerCase();}catch(e){this.AgentName="";}
	if (this.IsDom&&navigator.appName=="Netscape")
		return("n4");
	else if (this.AgentName.indexOf("safari")>=0)
		return("sf");
	else if (this.AgentName.indexOf("opera")>=0)
		return("op");
	else if (this.AgentName.indexOf("mac")>=0)
		return("mc");
	else return("ie");
}

function getBrowserTypeOLD()
{
	
	var retValue = 'n4'; // valore di default se non riconosce nessun tipo
	
	if ( (navigator.userAgent.toLowerCase().indexOf('msie') + 1) ) 
		// Internet explorer
		retValue = 'ie'; 
	else
		if (!(navigator.userAgent.toLowerCase().indexOf('compatible') + 1)) 
			// Netscape 4 o 6
			if (navigator.userAgent.toLowerCase().charAt(8)==4)
				// Netscape 4
				retValue = 'n4'; 
			else 
				if (navigator.userAgent.toLowerCase().charAt(8)==5)
					// Netscape 6
					retValue = 'n6';
					
	return(retValue);
}


function findRootFrame(currentFrame)
{
	return(window.top);
}


function refreshTopBarFrame()
{
	// aggiorno il frame topbar
	if (window.top.frames['topbar'])
		window.top.frames['topbar'].window.location.reload();
}

function refreshDesktopFrame()
{
	// aggiorno il frame topbar
	if (window.top.frames['desktop'])
		window.top.frames['desktop'].window.location.reload();
}

function setHelp(helpText)
{
	// memorizzo il tipo di browser utilizzato dal client
	var browserType = getBrowserType();
	
	// trovo il frame root del nostro sito
	var helpFrame = window.top.frames['topbar'];
					
	// aggiorno il testo del titolo
	if (helpFrame != null)
	{
		switch (browserType)
		{
			case 'ie':
			case 'n6':
				if (helpFrame.document.getElementById('HelpBox') != null)
				{
					helpFrame.document.getElementById('HelpBox').innerHTML = helpText;
					
					if (helpText != '')
					{
						var noInfiniteLoop = 0;
						while ((noInfiniteLoop < 200) && (helpFrame.document.getElementById('HelpBox').offsetHeight > 66))
						{
							
							helpFrame.document.getElementById('HelpBox').innerHTML = '';
							helpFrame.document.getElementById('HelpBox').style.width = helpFrame.document.getElementById('HelpBox').offsetWidth + 10;
							helpFrame.document.getElementById('HelpBox').innerHTML = helpText;
							noInfiniteLoop++;
						}
					}
					else
					{
						helpFrame.document.getElementById('HelpBox').style.width = 260;
						helpFrame.document.getElementById('HelpBox').style.height = 66;
						helpFrame.document.getElementById('HelpBox').style.left = 0;
					}
				}
				break;
			case 'n4':
				if (helpFrame.document.HelpBox != null)
					helpFrame.document.HelpBox.document.open();
					helpFrame.document.HelpBox.document.writeln(helpText);
					helpFrame.document.HelpBox.document.close();
				break;
		}
	}
}

function goToBasket()
{
	
	if ((window.top.frames['desktop']) && (window.top.frames['topbar']))
		window.top.frames['desktop'].document.location = window.top.frames['topbar'].document.getElementById('BasketLink').href;
	return(false);
}

function refreshBasketPreview(sMainLabel, sBasketURL, sGoToBasketLabel, sOrderNumberLabel, sBasketUID, sBasketAlfacode, sNumRowsLabel, sNumRows, sDateLabel, sDate, sLastInsertedLabel, sLastInserted, bMultiDesktop)
{
	var sBasketPreview = '';
	
	if ((sBasketAlfacode != null) && (sBasketAlfacode != ''))
	{
		sBasketPreview += "<TABLE class='desktop_base12' id='tBasketPreview' cellSpacing='1' cellPadding='1' width='140px' border='0'>";
		sBasketPreview += "<TR>";
		sBasketPreview += "		<TD class='desktop_headingA' vAlign='middle' noWrap align='center' colSpan='2'>";
		sBasketPreview += "			" + sMainLabel + "&nbsp" + "<INPUT class=modifier_smaller type=button onclick='return(goToBasket());' value='" + sGoToBasketLabel + "'>";
		sBasketPreview += "		</TD>";
		sBasketPreview += "</TR>";
		sBasketPreview += "<TR>";
		sBasketPreview += "		<TD class='desktop_base11' noWrap>";
		if (bMultiDesktop == 'true')
			sBasketPreview += "			<span class='modifier_smaller'>" + sOrderNumberLabel + "</span>&nbsp<a id='BasketLink' href='#' onclick='javascript:SetCurrentTabUrl(\"/Basket/AddChange.aspx?eBasketUID=" + sBasketUID + "\");'>" + sBasketAlfacode + "</a>";
		else
			sBasketPreview += "			<span class='modifier_smaller'>" + sOrderNumberLabel + "</span>&nbsp<a id='BasketLink' href='/Basket/AddChange.aspx?eBasketUID=" + sBasketUID + "' target='desktop'>" + sBasketAlfacode + "</a>";
		sBasketPreview += "		</TD>";
		sBasketPreview += "		<TD class='desktop_base11' noWrap>";
		sBasketPreview += "			<span class='modifier_smaller'>" + sNumRowsLabel + "</span>&nbsp;<span class='modifier_smaller'>" + sNumRows + "</span>";
		sBasketPreview += "		</TD>";
		sBasketPreview += "</TR>";
		sBasketPreview += "<TR>";
		//sBasketPreview += "		<TD class='desktop_base11' noWrap>";
		//sBasketPreview += "			<span class='modifier_smaller'>" + sDateLabel + "</span>&nbsp;<span class='modifier_smaller'>" + sDate + "</span>";
		//sBasketPreview += "		</TD>";
		sBasketPreview += "		<TD class='desktop_base11' noWrap colspan='2'>";
		sBasketPreview += "			<span class='modifier_smaller'>" + sLastInsertedLabel + "</span>&nbsp;<span class='modifier_smaller'>" + sLastInserted + "</span>";
		sBasketPreview += "		</TD>";
		sBasketPreview += "</TR>";
		sBasketPreview += "</TABLE>";
	}	
	
	if (window.top.frames['topbar'])
		window.top.frames['topbar'].document.getElementById('dBasketPreview').innerHTML = sBasketPreview;
}

function sortOptions(what)
{
    var copyOption = new Array();
    for (var i = 0; i < what.options.length; i++)
        copyOption[i] = new Array(what[i].value, what[i].text);

    copyOption.sort(function(a,b) { return a[0]-b[0]; });

    for (var i = what.options.length-1; i > -1; i--)
        what.options[i] = null;

    for (var i=0; i < copyOption.length; i++)
        var defaultSelected = false;
		var selected = false;
		var text = copyOption[i][1];
		var value = copyOption[i][0];
		var optionName = new Option(text, value, defaultSelected, selected)
		what.options[object.length] = optionName;
		what.options[object.length-1].selected = false;

}

function ListBoxNotPresentToPresent(NotPresent, Present)
{
	for (x = 0; x < NotPresent.length; x++)
	{
		if (NotPresent.options[x].selected)
		{
			Present.options[Present.length] = new Option(NotPresent.options[x].text, NotPresent.options[x].value);
			NotPresent.options[x] = null;
			x--;
		}
	}
	//sortOptions(Present);
}

function ListBoxNotPresentToPresentAll(NotPresent, Present)
{
	while (NotPresent.length > 0)
	{
		Present.options[Present.length] = new Option(NotPresent.options[0].text, NotPresent.options[0].value);
		NotPresent.options[0] = null;
	}
	//sortOptions(Present);
}

function ListBoxPresentToNotPresent(NotPresent, Present)
{
	for (x = 0; x < Present.length; x++)
	{
		if (Present.options[x].selected)
		{
			NotPresent.options[NotPresent.length] = new Option(Present.options[x].text, Present.options[x].value);
			Present.options[x] = null;
			x--;
		}
	}
	//sortOptions(NotPresent);
}

function ListBoxPresentToNotPresentAll(NotPresent, Present)
{
	while (Present.length > 0)
	{
		NotPresent.options[NotPresent.length] = new Option(Present.options[0].text, Present.options[0].value);
		Present.options[0] = null;
	}
	//sortOptions(Present);
}


function textAreaMaxLength_OnKeyPress(textBox, maxLength)
{
	if (maxLength && textBox.value.length > maxLength-1)
		return false;
}

function textAreaMaxLength_OnPaste(textBox, maxLength)
{
	if (maxLength)
		return false;
}

function textAreaMaxLength_OnBeforePaste(textBox, maxLength)
{
	if (maxLength)
	{
		maxLength = parseInt(maxLength);
		var oTR = document.selection.createRange();
		var iInsertLength = maxLength - textBox.value.length + oTR.text.length;
		var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
		oTR.text = sData;
		
		return false;
	}
}

function PrintPageOLD() {  
	var IE = (navigator.appName == "Microsoft Internet Explorer");   
	var VERSION = parseInt(navigator.appVersion);
	if (VERSION > 3)
	{    
		if (IE)
		{     	
			var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';      
			document.body.insertAdjacentHTML('beforeEnd', WebBrowser);       	
			WebBrowser1.ExecWB(6, 2);      	
			WebBrowser1.outerHTML = "";      
		}    
		else
		{
			self.print();    
		}  
	}  
	else
	{ 
		alert("Printer not supported");
	}
}

function PrintPage() {  
	var IE = (navigator.appName == "Microsoft Internet Explorer");   
	var VERSION = parseInt(navigator.appVersion);
	if (VERSION > 3)
	{    
		self.print();    
	}  
	else
	{ 
		alert("Printer not supported");
	}
}

function encryptPassword()
{
	setMaxDigits(128);
	key = new RSAKeyPair(
	"010001",
	"",
	"aa21803c85279ecb21275c1fff004992250c68e633cc05102ccd53d116f6fff6ce9af75b61f2e8e5e2e683247e25cb6e6f3c9b98f163d318ef3b97f15a124d4b"
	);

	var sPassword = sSessionCheckcode + document.getElementById('tbPassword').value;
	if (sPassword.length <= 60)
	{
		//var sEncryptedPassword = encryptedString(key, sPassword);
		var sEncryptedPassword = encryptedString(key, "alkjfasl;fdjl;askjfdlajslf;jasdl;jfl;asjfljasl;fdjl;asjf;ljasl;dfjlasjfljopwehtyp9ow4y8et9p8y4398ty498yt84t98y489yt498yt0000077777");
		document.getElementById('tbPassword').value = sEncryptedPassword;
	}
}

///////////////////////////// gestione tab

function SetCurrentTabUrl(sUrl, sLabel)
{
	var MainTabsCount = 4;
	var SubTabsCount = 10;
	
	var desktop = window.top.frames['desktop'];
	
	// procedo solo con la giusta versione del browser
	if (getBrowserType() == 'ie')
	{
		// se ancora non abbiamo la pagina per il multidesktop la carico
		if ((desktop.igtab_getTabById == null) || (desktop.igtab_getTabById("uwtMain") == null))
			desktop.window.location = '/MultiDesktop.aspx?FirstUrl=' + URLencode(sUrl);

		if ((desktop.igtab_getTabById != null) && (desktop.igtab_getTabById("uwtMain") != null))
		{
			var webTab = desktop.igtab_getTabById("uwtMain");
			var iCurrentTabIndex = webTab.getSelectedIndex();
			var iMainTabIndex = Math.floor(iCurrentTabIndex / SubTabsCount) * SubTabsCount;
			
			// setto il tab
			webTab.Tabs[iMainTabIndex].setTargetUrl('about:blank');
			if (sUrl.indexOf('?') != -1)
				webTab.Tabs[iMainTabIndex].setTargetUrl(sUrl + '&TabID=' + iMainTabIndex);
			else
				webTab.Tabs[iMainTabIndex].setTargetUrl(sUrl + '?TabID=' + iMainTabIndex);
			
			//if (sLabel != "")
			//	webTab.Tabs[iMainTabIndex].setText(sLabel);
				
			// seleziono il tab
			if (iMainTabIndex != iCurrentTabIndex)
				webTab.setSelectedIndex(iMainTabIndex);
		}
	}
	else
		desktop.location = sUrl;
}

function SetCurrentTabSubTabUrl(sUrl, sLabel)
{
	var MainTabsCount = 4;
	var SubTabsCount = 10;
	
	var desktop = window.top.frames['desktop'];
	
	// procedo solo con la giusta versione del browser
	if (getBrowserType() == 'ie')
	{
		if ((desktop.igtab_getTabById != null) && (desktop.igtab_getTabById("uwtMain") != null))
		{
			// pulisco l'url da eventuali TabID che non ci interessano
			sUrl = sUrl.replace("TabID=", "OldTabID=");
			
			var webTab = desktop.igtab_getTabById("uwtMain");
			var iCurrentTabIndex = webTab.getSelectedIndex();
			var iMainTabIndex = Math.floor(iCurrentTabIndex / SubTabsCount) * SubTabsCount;
			var iSubTabIndex = iMainTabIndex + 1;
			while ((iSubTabIndex < (iMainTabIndex + SubTabsCount)) && (webTab.Tabs[iSubTabIndex].visible))
				iSubTabIndex++;
			
			// innanzitutto azzero il tab
			webTab.Tabs[iSubTabIndex].setTargetUrl('about:blank');
			
			// setto il tab
			if (sUrl.indexOf('?') != -1)
				webTab.Tabs[iSubTabIndex].setTargetUrl(sUrl + '&TabID=' + iSubTabIndex);
			else
				webTab.Tabs[iSubTabIndex].setTargetUrl(sUrl + '?TabID=' + iSubTabIndex);
			//if (sLabel != "")
			//	webTab.Tabs[iSubTabIndex].setText(sLabel);
				
			// seleziono il tab
			if (iSubTabIndex != iCurrentTabIndex)
			{
				webTab.Tabs[iSubTabIndex].setVisible(true);
				webTab.setSelectedIndex(iSubTabIndex);
			}
		}
	}
	else
		desktop.location = sUrl;
}

function CloseCurrentSubTab(sUrl, bRefreshMainTab)
{
	var MainTabsCount = 4;
	var SubTabsCount = 10;
	
	var desktop = window.top.frames['desktop'];
	
	// procedo solo con la giusta versione del browser
	if (getBrowserType() == 'ie')
	{
		if ((desktop.igtab_getTabById != null) && (desktop.igtab_getTabById("uwtMain") != null))
		{
			var webTab = desktop.igtab_getTabById("uwtMain");
			var iCurrentTabIndex = webTab.getSelectedIndex();
			var iMainTabIndex = Math.floor(iCurrentTabIndex / SubTabsCount) * SubTabsCount;
			
			// se e' un subtab lo chiudo
			if (iMainTabIndex != iCurrentTabIndex)
			{
				// se richiesto e se il maintab e' pertinente, aggiorno il main tab
				if (bRefreshMainTab)
				{
					if (webTab.Tabs[iMainTabIndex].getTargetUrl().indexOf(sUrl.substring(0, sUrl.indexOf('.aspx'))) != -1)
					{
						sCurrentMainTabUrl = webTab.Tabs[iMainTabIndex].getTargetUrl();
						webTab.Tabs[iMainTabIndex].setTargetUrl('about:blank');
						webTab.Tabs[iMainTabIndex].setTargetUrl(sCurrentMainTabUrl);
					}
				}
				
				// azzero il tab
				webTab.setSelectedIndex(iCurrentTabIndex - 1);
				webTab.Tabs[iCurrentTabIndex].setTargetUrl('about:blank');
				webTab.Tabs[iCurrentTabIndex].setVisible(false);
			}
			else
			{
				// effettuo il redirect all'url specificata
				if (sUrl.indexOf('?') != -1)
					webTab.Tabs[iCurrentTabIndex].setTargetUrl(sUrl + '&TabID=' + iCurrentTabIndex);
				else
					webTab.Tabs[iCurrentTabIndex].setTargetUrl(sUrl + '?TabID=' + iCurrentTabIndex);
			}
		}
	}
	else
		desktop.location = sUrl;
}

function SetCurrentTabUrlStaticFrames(sUrl)
{
	var desktop = window.top.frames['desktop'];
	
	// se ancora non abbiamo la pagina per il multidesktop la carico
	if ((desktop.igtab_getTabById == null) || (desktop.igtab_getTabById("uwtMain") == null))
		desktop.window.location = '/MultiDesktop.aspx?FirstUrl=' + URLencode(sUrl);

	if ((desktop.igtab_getTabById != null) && (desktop.igtab_getTabById("uwtMain") != null))
	{
		var webTab = desktop.igtab_getTabById("uwtMain");
		var iCurrentTabIndex = webTab.getSelectedIndex();
		if (sUrl.indexOf('?') != -1)
			webTab.Tabs[iCurrentTabIndex].setTargetUrl(sUrl + '&TabID=' + iCurrentTabIndex);
		else
			webTab.Tabs[iCurrentTabIndex].setTargetUrl(sUrl + '?TabID=' + iCurrentTabIndex);
	}
}


///////////////////////////// funzioni specifiche

function Appointment_AddChange_SetAppointmentDayPreview(oCalendar, newValue, oEvent)
{
	// ottengo il puntatore al controllo principale
	//alert(oCalendar.uniqueId.substring(0, oCalendar.uniqueId.indexOf('wdcStartDate')).replace(/:/g, 'x').replace(/_/g, 'x') + 'wdcStartDate_input');
	//alert(newValue);
	//wdcCalendar = document.getElementById(oCalendar.uniqueId.substring(0, oCalendar.uniqueId.indexOf('wdcStartDate')).replace(/:/g, 'x').replace(/_/g, 'x') + 'wdcStartDate_input');
	//alert(wdcCalendar.value);
	StartDate = newValue.getYear() + "_" + (newValue.getMonth() + 1) + "_" + newValue.getDate(); //.replace(/-/g, '_');
	StructureUID = GetElementById('ddlStructureUID').options[GetElementById('ddlStructureUID').selectedIndex].value;
	LocalUID = GetElementById(oCalendar.uniqueId.substring(0, oCalendar.uniqueId.indexOf('wdcStartDate')).replace(/:/g, '_') + 'ddlLocalUID').value;
	
	GetElementById('AppointmentDayPreview').innerHTML = AddChange.GetAppointmentDayPreview(StartDate, StructureUID, LocalUID, oCalendar.uniqueId).value;
}

///////////////////////////// datagrid utility

function GetElementById(controlToFind)
{
	return document.getElementById(controlToFind);
}

function GetElementByIdInfragistics(controlToFind)
{
	return igedit_getById(controlToFind);
}

function GetSameRowControl(fromFullName, fromName, toName)
{
	return (fromFullName.replace(fromName, toName));
}

function GetDatagridControl(userControl, datagridName, rowNumber, controlName)
{
	if ((userControl != null) && (userControl != ""))
	{
		foundControl = GetElementById(userControl + "_" + datagridName + "__ctl" + rowNumber + "_" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementById(userControl + ":" + datagridName + ":_ctl" + rowNumber + ":" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementById(userControl + "x" + datagridName + "xxctl" + rowNumber + "x" + controlName);
		if (foundControl != null)
			return foundControl;
	}
	else
	{
		foundControl = GetElementById(datagridName + "__ctl" + rowNumber + "_" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementById( datagridName + ":_ctl" + rowNumber + ":" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementById(datagridName + "xxctl" + rowNumber + "x" + controlName);
		if (foundControl != null)
			return foundControl;
	}
}

function GetDatagridControlInfragistics(userControl, datagridName, rowNumber, controlName)
{
	if ((userControl != null) && (userControl != ""))
	{
		foundControl = GetElementByIdInfragistics(userControl + "_" + datagridName + "__ctl" + rowNumber + "_" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementByIdInfragistics(userControl + ":" + datagridName + ":_ctl" + rowNumber + ":" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementByIdInfragistics(userControl + "x" + datagridName + "xxctl" + rowNumber + "x" + controlName);
		if (foundControl != null)
			return foundControl;
	}
	else
	{
		foundControl = GetElementByIdInfragistics(datagridName + "__ctl" + rowNumber + "_" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementByIdInfragistics( datagridName + ":_ctl" + rowNumber + ":" + controlName);
		if (foundControl != null)
			return foundControl;
		foundControl = GetElementByIdInfragistics(datagridName + "xxctl" + rowNumber + "x" + controlName);
		if (foundControl != null)
			return foundControl;
	}
}


function GetDatagridFocusedControl(userControl, datagridName, controlName)
{
	row = 2;
	while (true)
	{
		if (userControl != '')
		{
			controlToFind = userControl + "_" + datagridName + "__ctl" + row + "_" + controlName;
			controlToFind2 = userControl + "x" + datagridName + "xxctl" + row + "x" + controlName;
		}
		else
		{
			controlToFind = datagridName + "__ctl" + row + "_" + controlName;
			controlToFind2 = datagridName + "xxctl" + row + "x" + controlName;
		}
				
		if ((GetElementById(controlToFind) != null) && (GetElementById(controlToFind).focus))
			return controlToFind;
			
		if ((GetElementById(controlToFind2) != null) && (GetElementById(controlToFind2).focus))
			return controlToFind2;
			
		if ((GetElementById(controlToFind) == null) && (GetElementById(controlToFind2) == null))
			return('');
			
		row++;
	}
}

///////////////////////////// comandi per mostrare/nascondere le righe delle tabelle

function showTableRow(tableRowName)
{
	if (GetElementById(tableRowName) != null)
		GetElementById(tableRowName).style.display = "";
}

function hideTableRow(tableRowName)
{
	if (GetElementById(tableRowName) != null)
		GetElementById(tableRowName).style.display = "none";
}

function switchTableRowDisplay(tableRowName)
{
	if (GetElementById(tableRowName) != null)
	{
		if (GetElementById(tableRowName).style.display == "none")
			GetElementById(tableRowName).style.display = "";
		else
			GetElementById(tableRowName).style.display = "none";
	}
}




///////////////////////////// cambio valore controlli al volo

function doSubmit()
{
	window.top.frames['desktop'].document.forms[0].submit();
}

function setValueTextBox(controlName, textToSet)
{
	if (window.top.frames['desktop'])
		if (window.top.frames['desktop'].document.getElementById(controlName) != null)
			window.top.frames['desktop'].document.getElementById(controlName).value = textToSet;
}

function setValueDropDownList(controlName, valueToSet)
{
	if (window.top.frames['desktop'])
		if (window.top.frames['desktop'].document.getElementById(controlName) != null)
		{
			control = window.top.frames['desktop'].document.getElementById(controlName);
			for (x = 0; x < control.length; x++)
				if (control.options[x].value == valueToSet)
					control.selectedIndex = x;
		}
}


//////////////////////////// URL Encoding

function URLencode(sStr)
{
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

