
function checkField(value,regexp) {

  if (value.search(regexp) == -1) {
    return false;
  }
    
  return true;
 
} 

function showError(Id)
{
	var value;
	var text;
	
	text = ScriptMessagesArray[Id]!=null?ScriptMessagesArray[Id]:'##'+Id+'##';
	if (ScriptMessagesArray[Id]!=null && showError.arguments[1]!=null)
	{		
		text = text.replace(/%1/, showError.arguments[1]);		
	}
	
	alert(text);
}

function checkFilled()
{
	
	//alert ("in checkFilled");
	
	for (i=0;i<checkFilled.arguments.length;i++)
	{
		control = document.getElementById( checkFilled.arguments[i] );
		
		//alert(" giulio nome campo = " + control.id );

		if (control.value=="" )
		 
		{
			with(control){focus()}
			showError("PleaseFillThisField");
			return false;
		}
	}
	return true;
}

function checkFillAtLeastOne()
{
	for (i=0; i<checkFillAtLeastOne.arguments.length; i++)
	{
		control = document.getElementById( checkFillAtLeastOne.arguments[i] );
		if (control.value != "" )
			return true;
	}
	
	control = document.getElementById( checkFillAtLeastOne.arguments[0] );
	with(control){focus()}
	//showError("PleaseFillAtLeastOne");
	alert("Effettuare almeno una scelta");
	
	return false;
}

function checkFilledGroup()
{
var Valid = true;
var AllEmpty = true;

	for (i=0;i<checkFilledGroup.arguments.length;i++)
	{
		control = document.getElementById( checkFilledGroup.arguments[i] );
		
		if (control.value!="" )
			{AllEmpty = false; break;}
	}
	
	if (AllEmpty) {return true;}
	
	for (i=0;i<checkFilledGroup.arguments.length;i++)
	{
		control = document.getElementById( checkFilledGroup.arguments[i] );
		
		
		if (control.value=="" )
			{Valid = false; break;}
	}

	if ( !Valid )
	{
		control = document.getElementById( checkFilledGroup.arguments[0] );
		control.focus();
		showError("PleaseFillGroup");
	}

	return Valid;
}

function checkRadios(){
var Valid = false;

	for (i=0;i<checkRadios.arguments.length;i++)
	{
		control = document.getElementById( checkRadios.arguments[i] );
		if ( control.checked )
		{
			Valid = true;
			break;
		}
	}
	
	if ( !Valid )
	{
		control = document.getElementById( checkRadios.arguments[0] );
		control.focus();
		showError("PleaseSelectOne");
	}
	
	return Valid;
}

function checkMinLen(){
var Ids = new Array;
var MinVals = new Array;
var Count = 0;
var InpObj = '';
var Valid = true;

for (i=0;i<checkMinLen.arguments.length-1;i++)
	{
	Ids[Count] = checkMinLen.arguments[i];i++
	MinVals[Count] = checkMinLen.arguments[i];	
	InpObj = document.getElementById(Ids[Count]);
	if (InpObj.value.length >= MinVals[Count]){Valid = true}
	else
		{
		with(InpObj){focus();select()}
		showError("AllowMinLength",MinVals[Count]);
		Valid = false;
		break;
		}
	Count++
	}
return Valid;	
}

function checkMaxLen(){
var Ids = new Array;
var MaxVals = new Array;
var Count = 0;
var InpObj = '';
var Valid = true;

for (i=0;i<checkMaxLen.arguments.length-1;i++)
	{
	Ids[Count] = checkMaxLen.arguments[i];i++
	MaxVals[Count] = checkMaxLen.arguments[i];	
	InpObj = document.getElementById(Ids[Count]);
	if (InpObj.value.length <= MaxVals[Count]){Valid = true}
	else
		{
		with(InpObj){focus();select()}
		showError("AllowMaxLength",MaxVals[Count]);
		Valid = false;
		break;
		}
	Count++
	}
return Valid;	
}


function checkMin(){
var Ids = new Array;
var MinVals = new Array;
var Count = 0;
var InpObj = '';
var Valid = true;

for (i=0;i<checkMin.arguments.length-1;i++)
	{
	Ids[Count] = checkMin.arguments[i];i++
	MinVals[Count] = checkMin.arguments[i];	
	InpObj = document.getElementById(Ids[Count]);
	if (InpObj.value >= MinVals[Count]){Valid = true}
	else
		{
		with(InpObj){focus();select()}
		showError("AllowMinValue",MinVals[Count]);
		Valid = false;
		break;
		}
	Count++
	}
return Valid;
}

function checkMax(){
var Ids = new Array;
var MaxVals = new Array;
var Count = 0;
var InpObj = '';
var Valid = true;

for (i=0;i<checkMax.arguments.length-1;i++)
	{
	Ids[Count] = checkMax.arguments[i];i++
	MaxVals[Count] = checkMax.arguments[i];	
	InpObj = document.getElementById(Ids[Count]);
	if (InpObj.value != '' && InpObj.value <= MaxVals[Count]){Valid = true}
	else
		{		
		with(InpObj){focus();select()}
		showError("AllowMaxValue",MaxVals[Count]);
		Valid = false;
		break;
		}
	Count++
	}
return Valid;	
}

function isNumeric(){
var InpObj = new Array;
var Valid = true;
for (i=0;i<isNumeric.arguments.length;i++)
	{
	InpObj[i] = document.getElementById(isNumeric.arguments[i]);
	if (isNaN(InpObj[i].value))
		{
		Valid = false;		
		with(InpObj[i]){focus();select()}	
		showError("IsNumericOnly");
		break;
		}
	else{Valid = true}	
	}
return Valid;	
}

function checkDecPoint(){
var InpObj = new Array;
var MaxDecPP = new Array;
var DecPP = 0;
var Count = 0;
var Valid = true;
for (i=0;i<checkDecPoint.arguments.length-1;i++)
	{
	InpObj[Count] = document.getElementById(checkDecPoint.arguments[i]);i++
	MaxDecPP[Count] = checkDecPoint.arguments[i];
	if (InpObj[Count].value.lastIndexOf('.')!=-1)
	{
		if (MaxDecPP[Count] == 0)
		{
			Valid = false;		
			break;
		}
		if (InpObj[Count].value.substring(InpObj[Count].value.lastIndexOf('.') + 1,InpObj[Count].value.length).length > MaxDecPP[Count])
		{
			Valid = false;		
			break;
		}
		else{Valid = true}			
	}
	else{Valid = true}			
	Count ++
	}
	
	if ( !Valid )
	{
		with(InpObj[Count]){focus();select()}
		showError("AllowDecValue",MaxDecPP[Count]);
	}
return Valid;
}

function checkTextArea(){
var InpObj = new Array;
var MaxLengths = new Array;
var Count = 0;
var Valid = true;
for (i=0;i<checkTextArea.arguments.length-1;i++)
	{
	InpObj[Count] = document.getElementById(checkTextArea.arguments[i]);i++
	MaxLengths[Count] = checkTextArea.arguments[i];	
	if (InpObj[Count].value.length < MaxLengths[Count]){Valid = true}
	else
		{
		with(InpObj[Count]){focus();select()}	
		showError("AllowMaxLength",MaxLengths[Count]);
		Valid = false;
		break;
		}
	Count++
	}
return Valid;	
}

function valiDate(){
	var InpObj = document.getElementById(valiDate.arguments[0]);
	var inputTXT = InpObj.value;
	var dateMask = valiDate.arguments[1];
	var separators = new Array;
	var counter = 0;
	var Year = '';
	var Month = '';
	var Day = '';
	var Valid = true;
	var YearLength = 0;	
	var MyDate1 = '';
	var MyDate2 = '';
	var MyDate3 = '';

	var separ2 = '';

for (q=0;q<inputTXT.length;q+=1)
	{
	if (inputTXT.substr(q,1) == '.')
		{
		separ2 = separ2 + 'p'
		}	
	else
		{	
		separ2 = separ2 + inputTXT.substr(q,1)
		}
	}			

inputTXT = separ2;

//alert(inputTXT)

for (i=0; i<dateMask.length; i+=1)
	{		
	if ((dateMask.substr(i,1).match('y') == null) &&
	(dateMask.substr(i,1).match('m') == null) &&
	(dateMask.substr(i,1).match('d') == null))
		{	
		if (dateMask.substr(i,1) == '.'){separators[counter] = 'p'}
		else{separators[counter] = dateMask.substr(i,1)}	
		Nx = 1+i;
		if ((dateMask.substr(Nx,1).match('y') == null) &&
		(dateMask.substr(Nx,1).match('m') == null) &&
		(dateMask.substr(Nx,1).match('d') == null)) 
			{			
			separators[counter] = separators[counter] + dateMask.substr(Nx,1);
			i+=1;
			}
		counter+=1
		}
	else
		{
		if (dateMask.substr(i,1) == 'y')
			{
			YearLength += 1;
			}
		}
	}

MyDate1 = inputTXT.substr(0,inputTXT.search(separators[0]));
MyDate2 = inputTXT.replace(MyDate1 + separators[0],'');
MyDate2 = MyDate2.substr(0,MyDate2.search(separators[1]));
MyDate3 = inputTXT.replace(MyDate1 + separators[0] + MyDate2 + separators[1],'');

//alert(MyDate1);
//alert(MyDate2);
//alert(MyDate3);

if (separators.length > 2)
	{
	if (MyDate3.match(separators[2]) != null)
		{
		MyDate3 = MyDate3.replace(separators[2],'')
		}
	else
		{
		Valid = false
		}
	}


MyDate1isNaN = isNaN(MyDate1);
MyDate2isNaN = isNaN(MyDate2);
MyDate3isNaN = isNaN(MyDate3);


//for (i=0;i<separators.length;i+=1){alert('separator No'+ i +': \'' + separators[i] + '\'')}


if (MyDate1isNaN || MyDate2isNaN || MyDate3isNaN)
	{
	Valid = false
	}
else
	{
	if (dateMask.search('y') < dateMask.search('m') && dateMask.search('m') < dateMask.search('d')) 
		{
		Year = MyDate1;
		Month = MyDate2;
		Day = MyDate3;
		}
	if (dateMask.search('y') < dateMask.search('d') && dateMask.search('d') < dateMask.search('m'))
		{
		Year = MyDate1;
		Day = MyDate2;
		Month = MyDate3;		
		}	
	if (dateMask.search('m') < dateMask.search('y') && dateMask.search('y') < dateMask.search('d'))
		{
		Month = MyDate1;		
		Year = MyDate2;
		Day = MyDate3;		
		}
	if (dateMask.search('m') < dateMask.search('d') && dateMask.search('d') < dateMask.search('y'))
		{
		Month = MyDate1;
		Day = MyDate2;		
		Year = MyDate3;		
		}
	if (dateMask.search('d') < dateMask.search('y') && dateMask.search('y') < dateMask.search('m'))
		{
		Day = MyDate1;
		Year = MyDate2;
		Month = MyDate3;	
		}
	if (dateMask.search('d') < dateMask.search('m') && dateMask.search('m') < dateMask.search('y'))
		{
		Day = MyDate1;
		Month = MyDate2;
		Year = MyDate3;		
		}


var testDate = new Date(Year,(Month-1),Day,1,1,1);

//testYear = parseInt(testDate.getYear());

testMonth = parseInt(testDate.getMonth());
testDay = parseInt(testDate.getDate());

if ((Month-1) != testMonth || Day != testDay){Valid = false}

		
	if (Year.length != YearLength || Month.length > 2 || Day.length > 2)
		{
		Valid = false
		}
	else
		{
		if (Month.length == 1){Month = '0' + Month}
		else if (Day.length == 1){Day = '0' + Day}
		}
	}

// <Modified by = "Rich">
// <Comment>Date must be grater or equal than 1900-01-01</Comment>

if ( Valid && ( Year < 1900 || Year > 2078 ) )
{
	with( InpObj )
	{
		focus();
		select();
	}	
	alert( "L'anno deve essere maggiore di 1900 e minore di 2078" );

	return false
}

if (valiDate.arguments[2] != null || valiDate.arguments[2] == 'Array')
{
	return new Array(Year,Month,Day)
}
else
{
	if ( !Valid )
	{
		with( InpObj )
		{
			focus();
			select();
		}	
		alert( "Il formato della data non e' valido" );
	}
	
	return Valid
}

// </Modified>

}

function checkFutureDate()
{
	var id = checkFutureDate.arguments[0];
	var d = convertToDate( checkFutureDate.arguments[0], checkFutureDate.arguments[1] );
	
	var InpObj = '';
	var Valid = false;
	var dateNow = new Date();
	
	if ( d >= dateNow )
		Valid = true;
	else
	{
		InpObj = document.getElementById( id );
		with( InpObj )
		{
			focus();
			select();
		}
		showError("DateMustBeFuture");
	}

		
	return Valid;
}

function checkPastDate()
{
	var id = checkPastDate.arguments[0];
	var d = convertToDate( id, checkPastDate.arguments[1] );
	
	var InpObj = '';
	var Valid = false;
	var dateNow = new Date();
	
	if ( d <= dateNow )
		Valid = true;
	else
	{
		InpObj = document.getElementById( id );
		with( InpObj )
		{
			focus();
			select();
		}
		showError("DateMustBePast");
	}
		
	return Valid;
}

function checkBeforeDate()
{
	var id = checkBeforeDate.arguments[0];
	var d = convertToDate( id, checkBeforeDate.arguments[2] );
	var dcomp = checkBeforeDate.arguments[1];
	
	
	var InpObj = '';
	var Valid = false;
	
	if ( d <= dcomp )
		Valid = true;
	else
	{
		InpObj = document.getElementById( id );
		with( InpObj )
		{
			focus();
			select();
		}
		showError("DateMustBeBefore", dcomp.toLocaleString() );
	}
		
	return Valid;
}

function checkAfterDate()
{
	var id = checkAfterDate.arguments[0];
	var d = convertToDate( id, checkAfterDate.arguments[2] );
	var dcomp = checkAfterDate.arguments[1];
	
	var InpObj = '';
	var Valid = false;
	
	if ( d >= dcomp )
		Valid = true;
	else
	{
		InpObj = document.getElementById( id );
		with( InpObj )
		{
			focus();
			select();
		}
		showError("DateMustBeAfter", dcomp.toLocaleString() );
	}
		
	return Valid;
}

function convertToDate(){

dateArray = valiDate(convertToDate.arguments[0],convertToDate.arguments[1],true);

if (convertToDate.arguments[2] == null)
	{
	dateArray[1] = dateArray[1] - 1;
	var date = new Date();
	date.setYear(dateArray[0]);
	date.setMonth(dateArray[1]);
	date.setDate(dateArray[2]);
	date.setHours(0);
	date.setMinutes(0);
	date.setSeconds(0);

	}
else
	{
	var date = new Array();
	date[0] = dateArray[0];
	date[1] = dateArray[1];
	date[2] = dateArray[2];
	}


return date;
}

function transferTo(url)
{
	window.location.replace(url);
}

function transferMainTo(url)
{
	window.parent.location.replace(url);
}

function goBack()
{
	window.history.back;
}

function openNewWin(url) {
	window.open(url, "", "location,menubar,status,toolbar=false,scrollbars,resizable=yes,width=500px,height=350px")
}

function printPage() {
	for (i=0;i<printPage.arguments.length;i++)
	{
		document.getElementById(printPage.arguments[i]).style.visibility = "hidden"
	}
	window.print();
}

//Show specified Tab for lxTabControl
function showTab(idTab, classPrefix, nTabs, nTab)
{
	for(i=0; i<nTabs; i++)
	{
		document.getElementById(idTab + "_" + i).style.display = "none";
		document.getElementById(idTab + "_tab_" + i).className = classPrefix + "tab"; 
	}
	document.getElementById(idTab + "_" + nTab).style.display = "block";
	document.getElementById(idTab + "_tab_" + nTab).className = classPrefix + "activetab"; 
	document.getElementById(idTab + "_current").value = nTab;
}

//Selects a row in lxListControl
function lxListSelectRow(idRow, idTarget, classPrefix, valueRow)
{
	//Set the selected value in the hidden input field
	document.getElementById(idTarget).value = valueRow;
	//Remove current highlight
	var currentRowId = document.getElementById(idTarget + "_current").value;
	var currentRow = document.getElementById(idTarget + "_row" + currentRowId);
	if (currentRow)
		currentRow.className = classPrefix + "row";
	
	//Set highlight
	document.getElementById(idTarget + "_row" + idRow).className = classPrefix + "activerow";
	document.getElementById(idTarget + "_current").value = idRow;
}

function searchCVOperatorChange(cbOperator)
{
	if (cbOperator.value == "bw") {
		document.getElementById("sCVageAndCell").style.visibility = "visible";
		document.getElementById("sCVageToCell").style.visibility = "visible";
	} else {
		document.getElementById("sCVageAndCell").style.visibility = "hidden";
		document.getElementById("sCVageToCell").style.visibility = "hidden";	
	}
}