/*****************************************************************
                            FILE DI FUNZIONI GLOBALI e COSTANTI
*****************************************************************/

var CN_RECIPIENT = "info@dedalo.org"

/* ---------------------------------------------------------------------------------
*  popUpWin()
*  Questa Funzione apre in pop up la pagina passatagli nella variabile theURL
*  Richiamare le funzione in questo modo:
*  javascript:popUpWin('pagina.htm','nomeWin','width=500,height=550,scrollbars=yes');
*/
function OpenPopUp(theURL,theName,theSize) 
{
  var arrayImpost = theSize.split("=");
  var winWidth = parseInt(arrayImpost[1]);
  var winHeight = parseInt(arrayImpost[2]);
  var screenWidth = ( screen.width - winWidth ) / 2;
  var screenHeight = ( screen.height - winHeight ) / 2;
  window.open(theURL,theName,theSize + ",screenX=" + screenWidth + ",screenY=" + screenHeight + ",left=" + screenWidth + ",top=" + screenHeight + ",location=no,status=no,scrollbars=no,resizable=no,directories=no,toolbar=no");
}

/* ---------------------------------------------------------------------------------
*  OpenImgPopUp()
*  Questa Funzione apre in pop up l'immagine passatagli nella variabile thePath
*  Richiamare le funzione in questo modo:
*  javascript:OpenImgPopUp('pathimmagini/immagine.gif,'Titolo pop Up','nomeWin','width=500,height=550,scrollbars=yes');
*/
function OpenImgPopUp(imgPath,theTitle,theName,theSize) 
{
  var arrayImpost = theSize.split("=");
  var winWidth = parseInt(arrayImpost[1]);
  var winHeight = parseInt(arrayImpost[2]);
  var screenWidth = ( screen.width - winWidth ) / 2;
  var screenHeight =( screen.height - winHeight ) / 2;
  window.open("../common/imgPopUp.asp?img=" + imgPath + "&title=" + theTitle,theName,theSize + ",screenX=" + screenWidth + ",screenY=" + screenHeight + ",left=" + screenWidth + ",top=" + screenHeight + ",location=no,status=no,resizable=no,directories=no,toolbar=no,menubar=no");
}

/* ---------------------------------------------------------------------------------
*  ClosePopUp()
*  Questa Funzione chiude il pop up caricando sotto la pagina mandata in szNewpage
*  Richiamare le funzione in questo modo:
*  javascript:ClosePopUp('pagina.asp','main');
*/
function ClosePopUp(szNewpage, szTarget) 
{
	
	if (document.all) 
	{
		window.document.LogPopUp.target = szTarget;
		window.document.LogPopUp.action = szNewpage;
		window.document.LogPopUp.submit();
	}
	else
		opener.top.main.location=szNewpage;
		
	window.close();

}

/*****************************************************************
IsStringValid
Description:      Funzione generica per controllare che una stringa contenga solo
                         i caratteri indicati in szCompareString
Return:             boolean true - stringa corretta
*****************************************************************/
function IsStringValid(szPassedString, szCompareString)
{
	var szTmpString = new String;
	var nLength = 0;
	var nIndex;
	
	szTmpString = szPassedString;
	nLength = szTmpString.length;

	for (nIndex = 0; nIndex < nLength; nIndex++)
		{
		if (szCompareString.indexOf(szTmpString.charAt(nIndex), 0) < 0)
			{
			return false;
			}
		}
	return true;
}


//questa funziona controlla se l'ultimo carattere premuto è "code" (Invio vale '13')
function ifConfirm(code)
{
	if (window.event.keyCode == code) 
		alert("Hai premuto invio");
}

//questa funzione calcola quanti caratteri sono ancora disponibili nella compilazzione di una textarea
function calcCharLeft(txtArea,crLeft,maxLength)
{
   var bAlert = true
   //ifConfirm('13')
   if (txtArea.value.length > maxLength)
   {		
	if (bAlert)
	{
		alert("Hai terminato i caratteri disponibili !");
		bAlert = false
	}
	charleft = 0 ;		
	if (txtArea.value.charCodeAt(maxLength-1)==13) charleft = 1;		
	txtArea.value = txtArea.value.substring(0,maxLength - charleft);
   } 
   else
   {		
	charleft = maxLength - txtArea.value.length
	bAlert = true
   }
   crLeft.value = charleft
}

//questa funzione fa il trim delle stringhe ritornando la stringa pulita o false se la stringa diventa vuota
function trim(strText)
{ 
	// this will get rid of leading spaces 
	while (strText.substring(0,1) == ' ') 
	   strText = strText.substring(1, strText.length);
	
	// this will get rid of trailing spaces 
	while (strText.substring(strText.length-1,strText.length) == ' ')
	   strText = strText.substring(0, strText.length-1);
	
	if (strText == "")
		return false;
	else
		return strText;
}

function checkEmail(Email)
{
	// verifica la correttezza di un indirizzo e-mail
	// controllando che ci siano '@' e '.' 
	//controlli sul carattere @
	if ( -1 == Email.indexOf("@"))
		{
		return false;
		}
	if (0 == Email.indexOf("@"))
		{		
		return false;
		}
	if ((Email.length-1) == Email.lastIndexOf("@"))
		{		
		return false;
		}
	//controlli sul carattere .
	if ( -1 == Email.indexOf("."))
		{		
		return false;
		}
	var szSubAddress;
	szSubAddress=Email.substr(Email.lastIndexOf("@"))
	if (-1 == szSubAddress.indexOf("."))
		{
		return false;
		}
	if (Email.indexOf(".") -1 == Email.lastIndexOf("@"))
		{
		return false;
		}
	if (Email.indexOf(".@") != -1)
		{
		return false;
		}							
	if (Email.indexOf("@.") != -1)
		{
		return false;
		}				
	if ((Email.length-1) == Email.lastIndexOf("."))
		{
		return false;
		}
	if (!IsStringValid(Email, "@._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"))		
		{
		return false;
		}
	return true;
}

function preSubmit(form)
{
	var szCorrectFormat
	var bAction = true
	var szEmail = trim(form.Subscription.value);
	var szNome = trim(form.Nome.value);

	if (!szEmail){ 
		alert ("Inserisci il tuo indirizzo di posta elettronica");
	}else if (!szNome){ 
		alert ("Inserisci il tuo nome");
	}else
	{

		szCorrectFormat = checkEmail(szEmail)

		if (!szCorrectFormat)
			alert ("Inserisci un corretto indirizzo di posta elettronica");
		else
		{
			//if (form.NwLAction[1].checked)
			//	if (!confirm("Sei sicuro di voler rimuovere la tua mail dalla Newsletter di Dedalo?"))
			//		bAction = false;

			if (bAction)
			{
				form.Subscription.value = szEmail;
				form.submit();
			}
		}
	}
}


