//utils.js
//common objects and function for www.adaca.it website
//needs:
//(c)2001 A.M.C. SoftWorks

/*
150.765 ,2 --> 150.77
150.765 ,1 --> 1507.65 --> 1508 --> 150.8
150.765 ,0 --> 151
*/

function sNumberFormat(num,iDecPlaces)
{
	var sNum=""+num+"";
	var sIntPart=sNum.split(".")[0];
	var sDecPart=sNum.split(".")[1]||"";
	if(!iDecPlaces)
		var iDecPlaces=0;
	if(iDecPlaces<0){
		//significative digits
		iDecPlaces*=-1;
		var mod=sIntPart.length;
		if(iDecPlaces>mod)
			iDecPlaces-=mod;
		else
			iDecPlaces=0;
	}
	if(iDecPlaces>0)
	{
		for(var i=0;i<iDecPlaces;i++)
			sDecPart+="0";
		var sFormatted=sIntPart+"."+sDecPart.substr(0,iDecPlaces);
	}
	else
		var sFormatted=sIntPart;
	return sFormatted;
}
function dataValida(sData)
{
	var giorno=0,mese=0,anno=0
	var sGiorno="", sMese=""
	var sSep="/-."
	var iGiorni= new Array(31,29,31,30,31,30,31,31,30,31,30,31)
	for(var j=0;j<sSep.length;j++)
	{
		giorno=parseInt(sData.substring(0,sData.indexOf(sSep.charAt(j))),10)
		mese  =parseInt(sData.substring(sData.indexOf(sSep.charAt(j))+1,sData.lastIndexOf(sSep.charAt(j))),10)
		anno  =parseInt(sData.substring(sData.lastIndexOf(sSep.charAt(j))+1,sData.length),10)
		if(parseInt(giorno,10)>0 && parseInt(mese,10)>0 && parseInt(anno,10)>0)
			break;
	}
	if(j>=sSep.length)
		return null
	else {
		if(anno<100)
			if(anno>=20)
				anno+=1900;
			else
				anno+=2000;
		if(mese<1 || mese>12)
			return null
		else if(giorno<1 || giorno>iGiorni[mese-1])
			return null
		else if(anno<1800 || anno>2099)
			return null
		else if(mese==2 && giorno==29)
			if(anno/400!=Math.round(anno/400))
				if(anno/100==Math.round(anno/100))
					return null
				else
					if(anno/4!=Math.round(anno/4))
						return null
		return new Date(anno,mese-1,giorno)
	}
}
function sStyleEnc(sString, sStyle, sHref)
{
	var sHtml=""
	if (sHref)
		sHtml+="<a href="+sHref+" "
	else
		sHtml+="<font "
	sHtml+="class="+sStyle+">"+sString
	if (sHref)
		sHtml+="</a>"
	else
		sHtml+="</font>"
	return sHtml
}
//       LoadCombo(theCombo,theDataArray,theNameField,theCodeField,theDefault)
//       LoadCombo(theCombo,theDataArray,theNameField,theDefault)
function LoadCombo(theCombo,theDataArray,theNameField)
{
	EmptyCombo(theCombo)
	var a=arguments
	if(a.length==5){
		var theCodeField=a[3]
		var theDefault=a[4]
	}
	else {
		var theCodeField=null
		var theDefault=a[3]
	}
	var theElement,theName,theCode
	for(var i=0;i<theDataArray.length;i++){
		theElement=theDataArray[i]
		if(theCodeField)
			theCode=theElement[theCodeField]
		else
			theCode=i
		theName=theElement[theNameField]
		theCombo.options[theCombo.options.length]=new Option(theName,theCode,theCode==theDefault,theCode==theDefault)
	}
}
function EmptyCombo(theCombo)
{
	for(var i=theCombo.options.length-1;i>0;i--)
		theCombo.options[i]=null
}
function ComboValue(theCombo)
{
	if(theCombo.selectedIndex>=0)
		return theCombo.options[theCombo.selectedIndex].value
	else
		return null
}
function ComboText(theCombo)
{
	if(theCombo.selectedIndex>=0)
		return theCombo.options[theCombo.selectedIndex].text
	else
		return null
}
function SelectCombo(theCombo,theKey)
{
	for(var i=0;i<theCombo.options.length;i++){
		if(theCombo.options[i].value==theKey){
			theCombo.selectedIndex=i
			break
		}
	}
}
function emailValido(sEmail)
{
	var af=sEmail.indexOf('@')
	var al=sEmail.lastIndexOf('@')
	var pl=sEmail.lastIndexOf('.')
	var l=sEmail.length
	//must contain one and only ONE 'at'
	if(af>0 && af==al){
		//must contain at least one dot AFTER the 'at'
		if(pl>0 && pl>al){
			return true;
		}
	}
	return false
}
function AMC_Counter(CID)
{
	//new Image().src="http://counter1.xoom.virgilio.it/xcit-cgi1/counter.gif?"+CID
}
function IsIntPos(checkStr)
{
	var checkOK="0123456789";
	var allValid=true;
	for(var i=0;i<checkStr.length;i++)
		if(checkOK.indexOf(checkStr.charAt(i))<0)
			allValid=false;
	return allValid
}
