//======================================================================================================
//======================================================================================================
function countdown() {
  iniData= new Date(2009,12,18,0,0,0);
  /*
    Data dell'inizio dell'evento, espressa in: anno, mese, giorno, minuti, secondi, millisecondi
  */
  oggi= new Date();
  milDif=(iniData-oggi);
  giorni=parseInt(milDif/86400000);
  milDif=milDif-(giorni*86400000);
  ore=parseInt(milDif/3600000);
  milDif=milDif-(ore*3600000);
  minuti=parseInt(milDif/60000);
  milDif=milDif-(minuti*60000);
  secondi=parseInt(milDif/1000);
  milDif=milDif-(secondi*1000);
  if (giorni <= "0" && ore <= "0" && minuti <= "0" && secondi <= "0")
    /* se è già arrivata la data scrive che siamo arrivati, altrimenti dice i giorni mancanti */
    testo='China Trader Award in corso!';
  else
    testo='mancano solo '+giorni+' giorni ';

    if (document.getElementById)
      document.getElementById("countdown").innerHTML=testo;
    else
      document.all.countdown.innerHTML=testo;
 
setTimeout("countdown()",1000)
}

function documentSubst(str, str1, str2)
{
	while (str.indexOf(str1)>-1)
		str = str.replace(str1,str2)
	return str;
}

function documentTrim(str)
{
	var ret = ''+str;
	while (ret.length>0)
	{
		if (ret.charAt(0)==' ' || ret.charAt(0)=='\t')
			ret = ret.substr(1);
		else
		if (ret.charAt(ret.length-1)==' ' || ret.charAt(ret.length-1)=='\t')
			ret = ret.substr(0,ret.length-1);
		else
			return ret;
	}
	return ret;
}

function documentLeftPad( str, size, padchar )
{
	while (str.length<size)
		str = padchar + str;
	return str;
}

function documentIsdigit( c )
{
	return c.charCodeAt(0)>="0".charCodeAt(0) && c.charCodeAt(0)<="9".charCodeAt(0);
}

function documentAtoi( str )
{
	var ret = "";
	var i = 0;
	while (i<str.length && documentIsdigit(str.charAt(i)))
	{
		if (ret.length>0 || str.charAt(i)!='0')
			ret += str.charAt(i);
		i++;
	}
	if (ret.length==0)
		return 0;
	return parseInt(ret);
}

function documentEmailValida(email)
{
	 var i;
	 var jM
	 //
	 email =  documentTrim(email);
	 //
	 if (email == "")
		return false;

	 // Contiene spazi -> false
	 if (email.indexOf(" ") != -1)
		return false;
	 //
	 i = email.indexOf("@")
	 // Non contiene @ -> false
	 if (i == -1)
	  return false;
	 // @ ? il 1 car -> false
	 if (i == 0)
		return false;
	 // Non contiene . dopo @ -> false
	 j = email.indexOf(".",i+1);
	 if (j == -1)
		return false;
	 // . dopo @ senza carattere -> false
	 if (j == i +1)
		return false;
	 // . ultimo carattere -> false
	 if (j == email.length-1)
		 return false;
	 // Contiene @ dopo @ -> false
	 j = email.indexOf("@",i+1);
	 if (j != -1)
		return false;
	 //
	 return true;
}

function documentTelefonoValido( tel )
{	
	// alert(tel);
	tel =  documentTrim(tel);
	// int i
	var num = 0;
	var spaces = 0;
	// il primo char pu? essere un +
	var i;
	for (i=0;i<tel.length;i++)
	{
		if (tel.charAt(i)=='+')
		{
			if (i!=0)
				return false;
		}
		else
		if (documentIsdigit(tel.charAt(i)))
		{
			num++;
		}
		else
		if (tel.charAt(i)==' ' || tel.charAt(i)=='.')
		{
			spaces++;
		}
		else
		{
			return false;
		}
	}
	//
	return num>=3;
}

function documentCheckNickname(e)
{
	//alert(e);
	var i;
	var ok = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM._@";
	e =  documentTrim(e);
	//
	if (e.length<8 || e.length>16)
		return false;
	//
	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
			return false;
	} 
	return true;
}

function documentCheckPassword( e )
{
	e =  documentTrim(e);
	if (e.length<8 || e.length>16)
		return false;
	return true;
}



//======================================================================================================
//======================================================================================================

function documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	if (v != pDefault )
	{
		if (v.indexOf(".")>-1 || v.indexOf(",")>-1 || isNaN(v))
			v = pDefault;
	}
	return(v);
}

function documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	if (v != pDefault )
	{
		if (v.indexOf(".")>-1 || v.indexOf(",")>-1 || isNaN(v))
			v = pDefault;
		else
		if (parseInt(v)<0)
			v = pDefault;
	}
	return(v);
}

function documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	v = documentSubst(v,",",".");
	if (v != pDefault )
	{
		if (isNaN(v))
			v = pDefault;
	}
	return(v);
}

function documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	v = documentSubst(v,",",".");
	if (v != pDefault )
	{
		if (isNaN(v))
			v = pDefault;
		else
		if (parseFloat(v)<0)
			v = pDefault;
	}
	return(v);
}

function documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var strDate = documentTrim(pVal);
	//
	if (pFormat.length==0)
		pFormat = 'gma';
	if (strDate.length<6 || pFormat.length<3)
		return "";
	//
	var i;
	var dtn = new Array(3);
	var gPos=-1;
	var mPos=-1;
	var aPos=-1;
	//
	for (i=0;i<3;i++)
	{
		switch (pFormat.charAt(i))
		{
		case 'g':	gPos = i; break;
		case 'm':	mPos = i; break;
		case 'a':	aPos = i; break;
		}
	}
	if (gPos<0 || mPos<0 || aPos<0)
		return "";
	//
	var pc = 0;
	for (i=0;i<3;i++)
	{
		// skippo non digit
		while (pc<strDate.length && !documentIsdigit(strDate.charAt(pc)))
			pc++;
		//
		dtn[i] = documentAtoi(strDate.substr(pc));
		// skippo digit
		if (pc<strDate.length)
		{
			pc++;
			while (pc<strDate.length && documentIsdigit(strDate.charAt(pc)))
				pc++;
		}
	}
	// month-1 (0 based)
	dtn[mPos]--;
	// a questo punto verifico
	if (dtn[aPos]<1000)
		dtn[aPos]+=2000;
	//
	var dt = new Date(dtn[aPos],dtn[mPos],dtn[gPos]);
	var buff = "";
	var ret = "";
	//
	for (i=0;i<3;i++)
	{
		if (i==aPos)
			buff = documentLeftPad(dt.getFullYear().toString(),4,"0")
		else
		if (i==mPos)
			buff = documentLeftPad((dt.getMonth()+1).toString(),2,"0")
		else
		if (i==gPos)
			buff = documentLeftPad(dt.getDate().toString(),2,"0")
		if (i>0)
			ret += "/";
		ret += buff;
	}
	//
	return ret;
}

function documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var strDate = documentTrim(pVal);
	//
	if (pFormat.length==0)
		pFormat = 'hms';
	if (strDate.length<2 || pFormat.length<2)
		return "";
	//
	var i;
	var dtn = new Array(3);
	var hPos=-1;
	var mPos=-1;
	var sPos=-1;
	//
	for (i=0;i<3;i++)
	{
		switch (pFormat.charAt(i))
		{
		case 'h':	hPos = i; break;
		case 'm':	mPos = i; break;
		case 's':	sPos = i; break;
		}
	}
	if (hPos<0 || mPos<0 )
		return "";
	//
	var pc = 0;
	for (i=0;i<3;i++)
	{
		// skippo non digit
		while (pc<strDate.length && !documentIsdigit(strDate.charAt(pc)))
			pc++;
		//
		dtn[i] = documentAtoi(strDate.substr(pc));
		// skippo digit
		if (pc<strDate.length)
		{
			pc++;
			while (pc<strDate.length && documentIsdigit(strDate.charAt(pc)))
				pc++;
		}
	}
	//
	// alert(sPos);
	//
	var dt = new Date(1899,11,30,dtn[hPos],dtn[mPos],sPos>-1 ? dtn[sPos] : 0);
	var buff = "";
	var ret = "";
	//
	for (i=0;i<3;i++)
	{
		buff = "";
		if (i==hPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad(dt.getHours().toString(),2,"0");
		}
		else
		if (i==mPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad((dt.getMinutes()).toString(),2,"0");
		}
		else
		if (i==sPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad(dt.getSeconds().toString(),2,"0");
		}
		ret += buff;
	}
	//
	return ret;
}

function documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	return v;
}

function documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	return v;
}

function documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal);
}

function documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentEmailValida(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentTelefonoValido(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentCheckNickname(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentCheckPassword(v))
		v = pDefault;
	//
	return v;
}
//======================================================================================================
//======================================================================================================

function frm_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_I(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_UI(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_F(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_UF(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_D(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_O(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_C(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_T(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal.value);
}

function frm_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_IMG(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_FILE(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal.value);
}

function frm_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_RDOC(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_E(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_TEL(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}


function frm_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_USR(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_PWD(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

//======================================================================================================
//======================================================================================================

function chk_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(true);
}

function chk_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(true);
}

function chk_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}
function chk_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

//======================================================================================================
//======================================================================================================

function documentTodayDate(formato)
{
	var dToday
	var sDate
	var sDay
	var sMonth
	var sYear
	//
	dToday = new Date();
	sDay = documentLeftPad(dToday.getDate(),2,'0');
	sMonth = documentLeftPad((dToday.getMonth()+1).toString(),2,'0')
	sYear = dToday.getFullYear();
	if (formato=="gma")
		sDate = sDay + '/' + sMonth + '/' + sYear
	else
		sDate = sMonth + '/' + sDay + '/' + sYear
	//
	return sDate;
}

function frm_documentTodayDate(frm,ichk,itxt,formato)
{
	if (frm[ichk].checked == true)
	{
		frm[itxt].value = documentTodayDate(formato);
	}
	else
	{
		frm[itxt].value =	'';
	}
}

//======================================================================================================
//======================================================================================================

//======================================================================================================
//======================================================================================================

//
function documentCheckAndSend(frm,subm)
{
	if (documentCheckErrors(frm))
	{
		if (!subm)
			return true;
		else
			frm.submit();
	}
	else
	{
		if (!subm)
			return false;
	}
}

function documentOnSendFormSuccess(frm)
{
	return true;
}

function documenOnSendFormError(frme,descr)
{
	frme.focus();
	//
	alert("Errore.\n"+descr);
	//
	return false;
}

//======================================================================================================
//======================================================================================================

