/**************************************************
*   OGGETTO RITORNATO DALLE FUNZIONI DI CONTROLLO
***************************************************/
function ReturnValue(outcome, value)
{
    this.outcome = outcome;
    this.value = value;
}
/**************************************************
*   CONTROLLO DELLE DATE
***************************************************/
/*
Descrizione:	Funzioni di controllo lato client
Autore:			Michele Castellini
Data:			14/10/2004
*/

function DateFieldOnBlur(source)
{
     //  istanzio un nuovo return value
    var returnValue = new ReturnValue(true, "");        
	var day			= 0;
	var month		= 0;
	var year		= 0;
	
	// <<<<<<<<<<<<<<<<<<<Recupero dei dati>>>>>>>>>>>>>>>>>>>>> 

	if(source.length>0)
	{
		if(ContainsOnly(source, "0123456789/.-"))
		{
			if((source.length == 8)&&(source.indexOf("/")==-1)&&(source.indexOf(".")==-1))
			{				
				day		= parseInt(source.substr(0,2), 10);
				month	= parseInt(source.substr(2,2), 10);
				year	= parseInt(source.substr(4,4), 10);
				
				returnValue.outcome = (!(isNaN(day)||isNaN(month)||isNaN(year)));		
			}
			else if ((source.length == 6)&&(source.indexOf("/")==-1)&&(source.indexOf(".")==-1))
			{
			    day		= parseInt(source.substr(0,2), 10);
				month	= parseInt(source.substr(2,2), 10);
				year	= parseInt(source.substr(4,2), 10);
				
				returnValue.outcome = (!(isNaN(day)||isNaN(month)||isNaN(year)));	
			}
			else
			{	
				var items = new Array();
				
				// Determino il separatore
				var separator = "";

				if ((source.indexOf("/")!=-1)&&(source.indexOf(".")==-1)&&(source.indexOf("-")==-1))
				{
					separator = "/";
				}
				else if ((source.indexOf("/")==-1)&&(source.indexOf(".")!=-1)&&(source.indexOf("-")==-1))
				{
					separator = ".";
				}
				else if ((source.indexOf("/")==-1)&&(source.indexOf(".")==-1)&&(source.indexOf("-")!=-1))
				{
					separator = "-";
				}
				else
				{
					returnValue.outcome = false;
				}			
				
				// Separo le parti
				if(returnValue.outcome)
				{
				    items = source.split(separator);
				    returnValue.outcome = (items.length==3);
				}

				// Passo ai numerici
				if(returnValue.outcome)
				{
					day		= parseInt(items[0], 10);
					month	= parseInt(items[1], 10);
					year	= parseInt(items[2], 10);
					returnValue.outcome = (!(isNaN(day)||isNaN(month)||isNaN(year)));	
				}		
			}
		}	
				
		// <<<<<<<<<<<<<<<<<<<Controllo dei dati>>>>>>>>>>>>>>>>>>>>>>
		if(returnValue.outcome)
		{	
		    if( year.length > 2 )
		    {
			  if((year<1900||year>2100)&&year!=9999)
			  {
				 returnValue.outcome = false;
			  }
            }
		    if(month<1||month>12) 
			{
				returnValue.outcome = false;
			}
			else if(day<1) 
			{
				returnValue.outcome = false;
			}
			else
			{	
				switch(month)
				{				
					case 2:
						if(((year%4==0)&&(year%100!=0))||(year%400==0))
						{
							returnValue.outcome = (day<=29);
						}
						else
						{
							returnValue.outcome = (day<=28);
						}
						break;
						
					case 4: case 6: case 9: case 11:
						returnValue.outcome = (day<=30);
						break;
						
					default:
						returnValue.outcome = (day<=31);
						break;				
				}	
			}
		}
	
		// <<<<<<<<<<<<<<<<<<<<<Ricostruzione dei dati>>>>>>>>>>>>>>>>>>>>>>>	
		if(returnValue.outcome)
		{
			var buffer = new String();	
			// Giorno
			buffer = "00" + day.toString();
			buffer = buffer.substr(buffer.length - 2, 2); 
			returnValue.value += buffer;

			// Mese
			buffer = "00" + month.toString();
			buffer = buffer.substr(buffer.length - 2, 2); 
			//returnValue.value += "-" + buffer;
			returnValue.value += "/" + buffer;
			
			// Anno
			
			 var anno = new String();
			 anno = year.toString();
			if( anno.length == 1)  
			{
			    buffer = "200" + year.toString();
			}
			else if (anno.length == 2)
			{
			buffer = "20" + year.toString();
			}
			 
			
		    if (anno.length == 4)
			{
			    buffer = "0000" + year.toString();
			    buffer = buffer.substr(buffer.length - 4, 4);
			}
			//buffer = buffer.substr(buffer.length - 4, 4);
			//returnValue.value += "-" + buffer;
			returnValue.value += "/" + buffer;
		}	
		else
		{
		    returnValue.outcome = false;
			DecodeError(0020);
		}
	}
	else if(Trim(source).lenght == 0)
	{
	    returnValue.outcome = true;
	    returnValue.value = "";
	}
	
	return returnValue;
}

/*
Descrizione:	Controlla che la data sia formalmente giusta. Viene utilizzato per le date
                composte dal solo anno e mese (MM/yyyy)
Specifica:      DRM_009
Autore:			Filippo Mariotti
Data:			06/03/2006
*/
function MMYYYYDateFieldOnBlur(source)
{
    //  istanzio un nuovo return value
    var returnValue = new ReturnValue(true, "");
    
	var month		= 0;
	var year		= 0;
	
	// <<<<<<<<<<<<<<<<<<<Recupero dei dati>>>>>>>>>>>>>>>>>>>>> 

	if(source.length>0)
	{
		if(ContainsOnly(source, "0123456789/.-"))
		{
			if((source.length == 6)&&(source.indexOf("/")==-1)&&(source.indexOf(".")==-1))
			{
				month	= parseInt(source.substr(0,2), 10);
				year	= parseInt(source.substr(2,4), 10);
				returnValue.outcome = (!(isNaN(month)||isNaN(year)));	
			}
			else
			{	
				var items = new Array();
				
				// Determino il separatore
				var separator = "";

				if ((source.indexOf("/")!=-1)&&(source.indexOf(".")==-1)&&(source.indexOf("-")==-1))
				{
					separator = "/";
				}
				else if ((source.indexOf("/")==-1)&&(source.indexOf(".")!=-1)&&(source.indexOf("-")==-1))
				{
					separator = ".";
				}
				else if ((source.indexOf("/")==-1)&&(source.indexOf(".")==-1)&&(source.indexOf("-")!=-1))
				{
					separator = "-";
				}
				else
				{
					returnValue.outcome = false;
				}			
				
				// Separo le parti
				if(returnValue.outcome)
				{
				    items = source.split(separator);
				    returnValue.outcome = (items.length == 2);
				}

				// Passo ai numerici
				if(returnValue.outcome)
				{
					month	= parseInt(items[0], 10);
					year	= parseInt(items[1], 10);
					returnValue.outcome = (!(isNaN(month)||isNaN(year)));	
				}		
			}
		}	
				
		// <<<<<<<<<<<<<<<<<<<Controllo dei dati>>>>>>>>>>>>>>>>>>>>>>
		if(returnValue.outcome)
		{	
			if((year<1900||year>2100)&&year!=9999)
			{
				returnValue.outcome = false;
			}
			else if(month<1||month>12) 
			{
				returnValue.outcome = false;
			}
		}
	
		// <<<<<<<<<<<<<<<<<<<<<Ricostruzione dei dati>>>>>>>>>>>>>>>>>>>>>>>	
		if(returnValue.outcome)
		{
			var buffer = new String();	

			// Mese
			buffer = "00" + month.toString();
			buffer = buffer.substr(buffer.length - 2, 2);
			returnValue.value = buffer;
			
			// Anno
			buffer = "0000" + year.toString();
			buffer = buffer.substr(buffer.length - 4, 4);
			returnValue.value += "/" + buffer;
		}	
		else
		{
		    returnValue.outcome = false;
			DecodeError(0020);
		}
	}
	else if(Trim(source).lenght == 0)
	{
	    returnValue.outcome = true;
	    returnValue.value = "";
	}
	
	return returnValue;
}

function DateField_OnBlur(field)
{
	var result = DateFieldOnBlur(field.value);
	return updateView(result, field);
}

function DateField_OnFocus(field)
{
    field.select();
}

/*
Descrizione:	Controlla che la data sia formalmente giusta. Viene utilizzato per le date
                composte dal solo anno e mese (MM/yyyy)
Specifica:      DRM_009
Autore:			Filippo Mariotti
Data:			06/03/2006
*/
function MMYYYYDateField_OnBlur(field)
{
	var result = MMYYYYDateFieldOnBlur(field.value);
	return updateView(result, field);
}

function DateField_OnKeyPress()
{	
	var keyString = String.fromCharCode(event.keyCode);	
	if(event.keyCode==44)
	{		
		event.keyCode=46;
	}
	else if(!ContainsOnly(keyString, "0123456789/.-"))
	{
		event.keyCode = 0;
		event.returnValue = false;
	}
	return event.returnValue;
}


// funzione per aprire i popup
function ApriHelp()
{
	window.open('../docs/help generico.pdf', 'help', 'directories=no,location=no, menubar=no,status=no,titlebar=yes,toolbar=no,center=yes');
}
function selezione(dati)
{
	var indice;
	var id;

	// IDORDINE
	indice = dati.indexOf('#');
	id = dati.substr(0, indice);
	this.document.Form1.IDORDINE.value = id;
	dati = dati.substr(indice+1, dati.length);

	// IDSCAMBIO
	indice = dati.indexOf('#');
	id = dati.substr(0, indice);
	this.document.Form1.IDSCAMBIO.value = id;
	dati = dati.substr(indice+1, dati.length);
	
	// IDSTOCK
	indice = dati.indexOf('#');
	id = dati.substr(0, indice);
	this.document.Form1.IDSTOCK.value = id;
	dati = dati.substr(indice+1, dati.length);	

	// IDUTENTE
	//this.document.Form1.IDUTENTE.value = dati;
	
	this.document.Form1.submit();
	 
	//alert(this.document.Form1.IDORDINE.value+", "+this.document.Form1.IDSCAMBIO.value+", "+this.document.Form1.IDSTOCK.value+", "+this.document.Form1.IDUTENTE.value);	
}

// funzione per modificare lo style di un elemento
function ChangeClassName(target, styleName)
{
	target.className = styleName;
}

// funzioni relative all'effetto push dei button image
function ButtonOnMouseDown(down)
{
	var fileName = event.srcElement.src;
	if(fileName.toLowerCase().lastIndexOf("_down.gif")==fileName.length - 9)
	{
		if(!down)		 
		{
			fileName = fileName.substring(0, fileName.length - 9) + ".gif";
			event.srcElement.src = fileName;
		}
	}
	else
	{
		if(down)		
		{
			fileName = fileName.substring(0, fileName.length - 4) + "_down.gif";
			event.srcElement.src = fileName;
		}
	}
}

function ButtonOnMouseUp()
{
	var fileName = event.srcElement.src;
	if(fileName.ToLower().EndsWith("_down.gif"))
	{
		fileName = fileName.Substring(0, fileName.Lenght - 9) + ".gif";
		event.srcElement.src = fileName;
	}
}
/*
Descrizione:	Funzione di gestione del calendarietto
Autore:			Massimiliano Gaudenzi
Data:			04/03/2004
*/
/*function openCalendar(targetControl, date, submitOpener)
{
	window.open("calendar.aspx?target_control=" + targetControl + "&date=" + date + "&submit_opener=" + submitOpener, "calendar", "toolbar=no,location=no,menubar=no,resizable=no,width=230,height=230");
}
*/

function openCalendar(target_control)
{
    //  ricavo la data nel controllo
    date = document.getElementById(target_control).value;

	window.open("calendar.aspx?target_control=" + target_control + "&date=" + date , 'calendar', 'scrollbars=yes,toolbar=no,location=no,menubar=no,resizable=no,width=240,height=260');
	return false;
}

function openForum()
{   
    //alert('not auth'); 
	window.open("http://www.laborsadelvino.com/forumbdv/index.php" , 'forumbdv', 'scrollbars=yes,toolbar=yes,location=no,menubar=no,resizable=yes,width=640,height=480');
	return false;
}

function openForumAuth(uname,pwd)
{   
    //alert('auth');
    //alert("/forumbdv/login.php?username=" + uname + "&password=" + pwd + "&login=Log+in");
	window.open("http://www.laborsadelvino.com/forumbdv/login.php?username=" + uname + "&password=" + pwd + "&login=Log+in" , 'forumbdv', 'scrollbars=yes,toolbar=yes,location=no,menubar=no,resizable=yes,width=640,height=480');
	return false;
}

function openForumAdmin(uname,pwd)
{   
    //alert('auth');
    //alert("/forumbdv/login.php?username=" + uname + "&password=" + pwd + "&login=Log+in");
	window.open("http://www.laborsadelvino.com/forumbdv/login.php?username=" + uname + "&password=" + pwd + "&login=Log+in&login=Log+in&admin=1&redirect=admin/index.php" , 'forumbdv', 'toolbar=yes,location=no,menubar=no,resizable=yes,width=640,height=480');
	return false;
}

// Funzione che permette di scrivere solo determinati caratteri
function ContainsOnly(test, allowed)
{
	var returnValue = true;

	for(i=0; i<test.length; i++)	
	{
		if(allowed.indexOf(test.substr(i, 1))==-1)
		{
			returnValue = false;
			break;
		}
	}
	
	return returnValue;
}

//	gestione del passaggio di focus in base al risultato
function updateView(result, source)
{
	//  se il controllo ha avuto esito positivo (result non vale false)
    if(result.outcome)
    {
        //  valorizzo il campo text box
        source.value = result.value;
    }
    else
    {
        //  riporto il focus sul controllo e seleziono il testo
        source.focus();
        source.select();
        event.returnValue = false;
    }
}

function CurrencyField_OnFocus()
{
	var returnValue	= true;
	var source = new String();
	source = event.srcElement.value;
	source.replace(".","");		
	if (!ContainsOnly(source, "0123456789,"))
	{
		event.returnValue = false;
	}
	event.srcElement.select();
	return event.returnValue;	
}
function AskSaveConfirmSpedizione()
{	
	event.returnValue = confirm("Attenzione, una volta  confermate le spese di spedizione non e' piu' possibile modificarle, procedere ugualmente?");
}

function AskDeleteConfirm()
{
	event.returnValue = confirm("Richiesta di cancellazione dati. Confermare?");
}
