function preload() {
  if (!document.images) return;
  var imgs = new Array();
  var arguments = preload.arguments;
  for (var i = 0; i < arguments.length; i++) {
    imgs[i] = new Image();
    imgs[i].src = arguments[i];
  }
}


function check_contact()
{
	var element=document.getElementById('contact');
	if ((element.name.value == "")||(element.email.value == "")||(element.comments.value == ""))
		{
			alert("You have to fill all the required fields!")
		}
	
	else if ((element.email.value.indexOf("@") == -1)||(element.email.value.indexOf(".") == -1))
		{ 
			alert("Invalid eMail address!") 
				}
	else element.submit();
} 

function check_jobs()
{
	var element=document.getElementById('contact');
	if ((element.name.value == "")||(element.email.value == "")||(element.comments.value == "")||(element.attachment.value == ""))
		{
			alert("You have to fill all the required fields!")
		}
	
	else if ((element.email.value.indexOf("@") == -1)||(element.email.value.indexOf(".") == -1))
		{ 
			alert("Invalid eMail address!") 
				}
	
	else element.submit();
} 

function isNumeric(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++)
		{
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
				{
					IsNumber = false; 
				}
		}   return IsNumber;
	}


function check_owlsnest_waitinglist()
{
	var element=document.getElementById('iscrizione');
	if ((element.firstname.value == "")||(element.lastname.value == "")||(element.email.value == ""))
		{
			alert("You have to fill all the required fields!")
		}
	
	else if ((element.email.value.indexOf("@") == -1)||(element.email.value.indexOf(".") == -1))
		{ 
			alert("Invalid eMail address!") 
				}
	else element.submit();
} 


function check_form_owlsnest()
{
	var element=document.getElementById('iscrizione');
	
	if ((element.firstname.value == "")||(element.lastname.value == "")||(element.email.value == "")||(element.username.value == "")||(element.age.value == ""))
		{
			alert("You have to fill all the fields!")
		}
	
	else if ((element.email.value.indexOf("@") == -1)||(element.email.value.indexOf(".") == -1))
		{ 
			alert("Invalid eMail address!") 
				}
	else if (!isNumeric(element.age.value))
		{ 
			alert("The age must be a number!") 
				}
	else if (element.age.value<18)
		{ 
			alert("You must be 18 or over to partecipate!") 
				}
	else if (element.age.value<18)
		{ 
			alert("You must be 18 or over to partecipate!") 
				}
	else if ((element.sex_male.checked==false)&&(element.sex_female.checked==false))
		{ 
			alert("You have to select your sex!") 
				}
	else if (element.education.selectedIndex==0)
		{ 
			alert("You have to select your education!") 
				}
	else if ((element.marital_status_single.checked==false)&&(element.marital_status_married.checked==false))
		{ 
			alert("You have to select your marital status!") 
				}
	else if (element.employment.selectedIndex==0)
		{ 
			alert("You have to select your employment status!") 
				}
	else if (element.current_games.selectedIndex==0)
		{ 
			alert("You have tell us how many games on Facebook do you currently play!") 
				}
	else if (element.often_play.selectedIndex==0)
		{ 
			alert("You have tell us how often do you play games on Facebook!") 
				}
	else if ((element.type_games1.checked==false)&&(element.type_games2.checked==false)&&(element.type_games3.checked==false)&&(element.type_games4.checked==false)&&(element.type_games5.checked==false)&&(element.type_games6.checked==false)&&(element.type_games7.checked==false)&&(element.type_games8.checked==false)&&(element.type_games9.checked==false))
		{ 
			alert("You have to tell us what type of games do you most often play!") 
				}
	else if ((element.affiliated_yes.checked==false)&&(element.affiliated_no.checked==false))
		{ 
			alert("You have to tell us if you are employed or affiliated with any company that develops, markets, publishes or licenses games, or that publishes information about games!") 
				}
	else if (element.affiliated_yes.checked)
		{ 
			alert("If you are employed or affiliated with any company that develops, markets, publishes or licenses games, or that publishes information about games, unfortunately you are not eligible to participate in this community.") 
				}
	else if ((element.how_found1.checked==false)&&(element.how_found2.checked==false)&&(element.how_found3.checked==false)&&(element.how_found4.checked==false))
		{ 
			alert("You have to tell us how did you find out about DreamsLair Entertainment and the Owl’s Nest!") 
				}	
	else if ((element.how_found4.checked)&&(element.how_found4_text.value == ""))
		{ 
			alert("You have to fill the field Other!") 
				}
	else if (element.agreement.checked==false)
		{ 
			alert("You have to accept the Beta Test Panel Agreement!") 
				}
	else element.submit();
} 


function check_prova()
{
	var element=document.getElementById('iscrizione');
	alert(element.education.selectedIndex);
	
} 

$(document).ready(function () 
{  
	var usernameValidated = false;
	var emailValidated = false;
	var validateUsername = $('#validaUsername');
	$('#username').keyup(function ()
	{	    
		var t = this;
		if (this.value != this.lastValue && this.value !='') 
		{      
			if (this.timer) 
				clearTimeout(this.timer);

			this.timer = setTimeout(function () 
			{        
				$.ajax({         
				   		url: 'Scripts/validateUsername.php',          
				   		data: 'username=' + t.value,          
				   		dataType: 'text',          
				  		type: 'post',          
				  		success: function (msg) {            
				   			validateUsername.html(msg);
							var submitButton = $('#send');
							if(msg == 'Username not available')
							{
								usernameValidated = false;
							 	(submitButton).attr("disabled", "disabled"); 
							}
							else
							{
								usernameValidated = true;
								if(emailValidated == true)
								{
							 	(submitButton).removeAttr("disabled"); 
								}
							}			
										 	  }        
					});      
			}, 500);   
			
			this.lastValue = this.value;  
			
			}  
		});
	var validateEmail = $('#validaEmail');
	$('#email').keyup(function ()
	{	    
		var t = this;
		if (this.value != this.lastValue && this.value !='') 
		{      
			if (this.timer) 
				clearTimeout(this.timer);

			this.timer = setTimeout(function () 
			{        
				$.ajax({         
				   		url: 'Scripts/validateEmail.php',          
				   		data: 'email=' + t.value,          
				   		dataType: 'text',          
				  		type: 'post',          
				  		success: function (msg) {            
				   			validateEmail.html(msg);
							var submitButton = $('#send');
							if(msg == 'Email already used')
							{
								emailValidate = false;
							 	(submitButton).attr("disabled", "disabled"); 
							}
							else
							{
								emailValidated = true;
								if(usernameValidated == true)
								{
							 	(submitButton).removeAttr("disabled"); 
								}
							}			
										 	  }        
					});      
			}, 500);   
			
			this.lastValue = this.value;  
			
			}  
		});
	
	});
