function maxChar(form)
{
 var MaxLen=300;
 inputStr = form.msg1.value;
 strlength= inputStr.length;
 if (strlength > MaxLen ) form.msg1.value=inputStr.substring(0,MaxLen);
 form.charsleft.value = (MaxLen - form.msg1.value.length);
 form.charsleft1.value = form.charsleft.value;
 form.msg1.focus();
}

function maxChar1(form)
{
 form.charsleft.value = form.charsleft1.value;
}

function controlla_data()
{
	var day=document.myform.day.options[document.myform.day.selectedIndex].value;
	var month=document.myform.month.options[document.myform.month.selectedIndex].value;
	var year=document.myform.year.options[document.myform.year.selectedIndex].value;
	
	var controllo;
	
	if(year.length==4 && year>=1900)
	{
		year = new String(parseInt(year)-1900);
		controllo=new Date(year,month,day);
		if(day==29 && month==1 && year==100)
		{

		}
		else
		{
			//if ((controllo.getDate() != day) || (controllo.getMonth() != month) || (controllo.getYear() != year))
    			//{
			//	
			//	return false;
      		//}
		}
    }
	else
	{
 		alert("This day doesn't exist "+day+"-"+month+"-"+year); 
		return false;
	}
	return true;
	
}
function textarea_checker()
{
  var theResult=true;
  var value=document.myform.msg1.value;
  
  if (value.length > 300)
  {
     alert("Max 300 characters"); 
    document.myform.msg1.focus();
    document.myform.msg1.select();
	theResult=false;
  }
  return theResult;
}

function controlla_campi(provenienza,giorni)
{
	var caratteri="()[]\":;\\";
	
	var fromMail=document.myform.fromMail.value;
	var toMail=document.myform.toMail.value;
	var the_result=true;
	var msg=controlla_text();
	var i;
	
	var toName=document.myform.toName.value;
	var fromName=document.myform.fromName.value;
		
 	if(fromMail=="your e-mail address (*)" || fromMail==null || fromMail.length==0) 
	{
 		alert("Insert your e-mail"); 
		return false;
	}
	var k=0;
	var inc=0;
	for(i=0;i<fromMail.length;i++)
	{
			var app=fromMail.charAt(i);
			
			if(caratteri.indexOf(app)!=-1)	
			{
 			alert("You have typed an invalid character in e-mail address"); 
			return false;
			}
		
		if(app=="\@")
		{
 			if (i==0){alert("You have input a wrong e-mail address");return false;} 
			else if (fromMail.charAt(i-1)=="." || fromMail.charAt(i+1)==".")
 			{alert("You have input a wrong e-mail address");return false;} 
			
			k++;
		}
		if(app==".")
		{
 			if (i==fromMail.length-1){alert("You have input a wrong e-mail address");return false;} 
 			if (i==0){alert("You have input a wrong e-mail address");return false;} 
 			else if (fromMail.charAt(i-1)=="." || fromMail.charAt(i+1)==".") 						{alert("You have input a wrong e-mail address");return false;} 
			if(k!=0)
			{
				inc++;
			}
		}
		if(app==" ")
		{
 			alert("Don't insert blank space in your e-mail"); 
			return false;
		}
		
	}
	if(k!=1)
	{
 		alert("You have input a wrong e-mail address"); 
		return false;
	}
	if(inc>3 || inc <=0)
	{
 		alert("You have input a wrong e-mail address"); 
		return false;
	}
	
 	if( toMail=="You have input a wrong e-mail address" || toMail==null || toMail.length==0) 
	{
 		alert("Insert recipient e-mail"); 
		return false;
	}
	
	var caratteri2="()[]\":;\\";
	var count2=0;
	var count3=0;
	var count=0;
	inc=0;
	
	for(i=0;i<toMail.length;i++)
	{
		var app2=toMail.charAt(i);
		if(caratteri2.indexOf(app2)!=-1)	

		{
 			alert("You have typed an invalid character in recipient e-mail"); 
			return false;
		}
		
		if(app2==" " && toMail.charAt(i-1)!=",")
		{
 			alert("Don't insert blank spaces in recipient e-mail"); 
			return false;
		}
		
		if(app2==".")
		{
			if (i==0 || i==toMail.length-1)  
			{
 				alert("You have input a wrong recipient e-mail"); 
				return false;
			}
			if(toMail.charAt(i-1)=="." || toMail.charAt(i-1)==",")
			{
 				alert("You have input a wrong recipient e-mail"); 
				return false;
			}
			if(count3!=0)
			{
				inc++;
			}
		}
		
		if(app2=="\@")
		{
			if (i==0 || toMail.charAt(i-1)==",")
			{
 				alert("You have input a wrong recipient e-mail"); 
				return false;
			}
			else if(i!=toMail.length)
			{
						
				if (toMail.charAt(i-1)=="." || toMail.charAt(i+1)==".")
				{
 					alert("You have input a wrong recipient e-mail"); 
					return false;
				}
			}

			count2++;
			count3++;
		}
		if(app2=="," || app2==";")
		{
		
			if(inc==0 || toMail.charAt(i-1)==".")
			{
 				alert("You have input a wrong recipient e-mail"); 
				return false;
			}
				
				count3=0;
				inc=0;
				count++;
			
			
		}	
		if(count>49)
		{
 			alert(""); 
			return false;
		}
	}
	
	if(inc==0)
			{
 				alert("You have input a wrong recipient e-mail"); 
				return false;
			}
	if(count2-1!=count)
	{
 		alert("You have input a wrong recipient e-mail"); 
		return false;
	}
	
	//inizializza variabili 	
	var app="";
	var app2="your name";
	for (i=0;i<app2.length;i++)
	{
	if (app2.charAt(i)!=" ") {app=app+app2.charAt(i);}
	}
	your_name2=app;
	
	//make the same with the other label recipient name
	var app="";
	var app2="recipient name";
	for (i=0;i<app2.length;i++)
	{
	if (app2.charAt(i)!=" ") {app=app+app2.charAt(i);}
	}
	recipient_name2=app;
	//inizializzate
	
 //effettua il controllo	
	//check if digit or not digit your name
	fromName=fromName.toLowerCase();
	toName=toName.toLowerCase();
	var app="";
	for (i=0;i<fromName.length;i++)
	{
	var c=fromName.charAt(i);
	if (c!=" " && c!="!" && c!="?" && c!="=") {app=app+c;}
	}
	fromName=app;
	if (fromName==your_name2){document.myform.fromName.value="";}
	
	// check if digit or ot digit recipient name
	var app="";
	for (i=0;i<toName.length;i++)
	{
	var c=toName.charAt(i);
	if (c!=" " && c!="!" && c!="?" && c!="=") {app=app+c;}
	}
	toName=app;
	if (toName==recipient_name2 || toName==your_name2){document.myform.toName.value="";}
	//fine controllo
	
	var greeting=document.myform.greeting_contents.options[document.myform.greeting_contents.selectedIndex].value;
	if(greeting=="")
	{
		greeting=document.myform.greeting.value;
	}
	if(provenienza=="send_later")
	{
		var controllo=controlla_data();	
		if(!controllo)
		{
			return false;
		}
		controllo=giorni_rimasti(giorni);
		/*if(!controllo)
		{
 			alert("You are out of your limit days");  
			return false;
		}
		*/
		azzera_campo_greeting();
	}
	if(provenienza=="preview")
	{
		var app=0;
		
		document.forms.myform.action="card_preview.pl";
		
		azzera_campo_greeting();

		return true;
	}
	if(provenienza=="send_later")
	{
		azzera_campo_greeting()
		document.myform.action="do_schedule.pl";
	} 
	else if(provenienza=="send_now")
	{
		azzera_campo_greeting();
	}
	document.myform.target="";
	return true;	
}	// fine controlla_campi


function azzera_campo_greeting()
{
var app=0;

			if(document.forms.myform.greeting.value=="Enter your own greeting")
				app=1;
		
			if(document.forms.myform.greeting.value=="inserisci i tuoi saluti")
				app=1;
		
			if(document.forms.myform.greeting.value=="Escribe tus saludos")
				app=1;
				
		    if (document.forms.myform.greeting.value.indexOf("eigene") != -1) {
		    	if (document.forms.myform.greeting.value.indexOf("eingeben") != -1) {
					app=1;
		    	}
		    }
		    
			//if(document.forms.myform.greeting.value=="eigene GrŸ§e eingeben")
			//	app=1;
		
		
			
		if(app==1)
			document.forms.myform.greeting.value="";
	
}

function giorni_rimasti(giorni)
{

	var year=document.myform.year.options[document.myform.year.selectedIndex].value;
	var month=document.myform.month.options[document.myform.month.selectedIndex].value;
	var day=document.myform.day.options[document.myform.day.selectedIndex].value;
	var a=new Date();

	if(a.getYear()==year)
	{
		
		if(a.getMonth()>month)
		{
			return false;
		}
		if(month - a.getMonth()>(giorni/30)+1)
		{
			return false;
		}
		if(a.getMonth() - month==0)
		{
			if((a.getDate()>day) || (day-a.getDate()>giorni))
			{
				return false;
			}
			
		}
		else if((month-a.getMonth())*30-(a.getDate()-day)>giorni)
		{
			return false;
		}
	}
	else
	{
		var dfr=(30-a.getDate()-(-day));
		if(((11-a.getMonth())-(-month))*30-(-(30-a.getDate()-(-day)))>giorni)
		{
			return false;
		}
	}
	
	return true;
	
}	

function controlla_text()
{
	var text=document.myform.msg1.value;
	var i;
	var app2="";
	for(i=0;i<text.length;i++)
	{
		var app=text.charAt(i);
		
		if(app=="\n")
		{
			app2=app2.concat("<br>");
		}
		else if(app=="\"")
		{
			app2=app2.concat("&quot;");
		}
		else
		{
			app2=app2.concat(app);
		}	
		
	}
	document.forms.myform.msg.value=app2;
	return app2;
}

function seleziona ( nome_input, valore_iniziale) {
	// se nel testo c'è il valore iniziale lo butto via
	var contr="no";
	
		if ( nome_input.value == valore_iniziale ) 
		{	
			if(contr=="no" || (contr=="no" && nome_input.name!="toMail"))
			{				
				nome_input.value = "" ;
			}
		}
		// seleziono...
		nome_input.select ( ) ;
	
	return ;
} // fine seleziona

function get_greeting()
{
	var app=document.forms.myform.greeting_contents.options[document.forms.myform.greeting_contents.selectedIndex].value;
	return app;
}
function back_photos1()
{
	var url2=document.forms.myform.url1.value;
	
	self.location=url2;
}
