window.addEvent('domready', function() {

	$('production_register').addEvent('submit', function(e){
			e.stop();
			var error = 0;
			var fname = this.fname.value;
			var lname = this.lname.value;
			var email = this.email.value;
			var confirmemail = this.confirmemail.value;
			var password = this.password.value;
			var passwordconfirm = this.passwordconfirm.value;
			var tel = this.tel.value;
			var city = this.city.value;
			var job_title = this.job_title.value;
			
			
			var myFx1 = new Fx.Morph(this.fname, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx2 = new Fx.Morph(this.lname, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx3 = new Fx.Morph(this.email, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx4 = new Fx.Morph(this.confirmemail, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx5 = new Fx.Morph(this.password, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx6 = new Fx.Morph(this.passwordconfirm, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx7 = new Fx.Morph(this.tel, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx8 = new Fx.Morph(this.city, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			var myFx9 = new Fx.Morph(this.job_title, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
			
			
			if(fname==""){
				myFx1.start('.error');
				error = 1;
			} else {
				myFx1.start('.normal_input');
			}
			
			if(lname==""){
				myFx2.start('.error');
				error = 1;
			} else {
				myFx2.start('.normal_input');
			}
			
			if(email==""){
				myFx3.start('.error');
				error = 1;
			} else {
				if(email!=confirmemail){
					myFx3.start('.error');
					myFx4.start('.error');
					error = 1;
				} else {
					myFx3.start('.normal_input');
					myFx4.start('.normal_input');
				}
			}	
			
			if(password==""){
				myFx5.start('.error');
				error = 1;
			} else {
				if(password!=passwordconfirm){
					myFx5.start('.error');
					myFx6.start('.error');
					error = 1;
				} else {
					myFx5.start('.normal_input');
					myFx6.start('.normal_input');
				}
			}	
			
			if(tel==""){
				myFx7.start('.error');
				error = 1;
			} else {
				if(!checkUKTelephone(tel)){
					$('tel_error').innerHTML = "<p style='margin-top:0px;margin-bottom:0px;'><i>Please enter a <b>valid telephone number</b>.</i></p>";
					myFx7.start('.error');
					error = 1;
				} else {
				myFx7.start('.normal_input');
				}
			}
			
			if(city==""){
				myFx8.start('.error');
				error = 1;
			} else {
				myFx8.start('.normal_input');
			}
			
			if(job_title==""){
				myFx9.start('.error');
				error = 1;
			} else {
				myFx9.start('.normal_input');
			}

			if(error==0){
				this.set('send', {onComplete: function(response) { 
		        			if (response==1){
								window.location = "production/index.php";	
							} else {
								var errorObject = JSON.decode(response);
								alert(errorObject.email);
							}
				}});
				
				this.send();
			}
			
	});		
});

function validate(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   var address = email;
	   if(reg.test(address) == false) {
	      return false;
	   } else {
		 return true;
		 }
}

function checkUKTelephone (telephoneNumber) {

	  // Convert into a string and check that we were provided with something
	  var telnum = telephoneNumber + " ";
	  if (telnum.length == 1)  {
	     telNumberErrorNo = 1;
	     return false
	  }
	  telnum.length = telnum.length - 1;
	  
	  // Don't allow country codes to be included (assumes a leading "+")
	  var exp = /^(\+)[\s]*(.*)$/;
	  if (exp.test(telnum) == true) {
	     telNumberErrorNo = 2;
	     return false;
	  }
	  
	  // Remove spaces from the telephone number to help validation
	  while (telnum.indexOf(" ")!= -1)  {
	    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
	  }
	  
	  // Remove hyphens from the telephone number to help validation
	  while (telnum.indexOf("-")!= -1)  {
	    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
	  }  
	  
	  // Now check that all the characters are digits
	  exp = /^[0-9]{10,11}$/;
	  if (exp.test(telnum) != true) {
	     telNumberErrorNo = 3;
	     return false;
	  }
	  
	  // Now check that the first digit is 0
	  exp = /^0[0-9]{9,10}$/;
	  if (exp.test(telnum) != true) {
	     telNumberErrorNo = 4;
	     return false;
	  }
		
		// Disallow numbers allocated for dramas.
		 
	  // Array holds the regular expressions for the drama telephone numbers
	  var tnexp = new Array ();
		tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
		tnexp.push (/^02079460[0-9]{3}$/);
		tnexp.push (/^01914980[0-9]{3}$/);
		tnexp.push (/^02890180[0-9]{3}$/);
		tnexp.push (/^02920180[0-9]{3}$/);
		tnexp.push (/^01632960[0-9]{3}$/);
		tnexp.push (/^07700900[0-9]{3}$/);
		tnexp.push (/^08081570[0-9]{3}$/);
		tnexp.push (/^09098790[0-9]{3}$/);
		tnexp.push (/^03069990[0-9]{3}$/);
		
		for (var i=0; i<tnexp.length; i++) {
	    if ( tnexp[i].test(telnum) ) {
	      telNumberErrorNo = 5;
	      return false;
	    }
		}
	  
	  // Finally check that the telephone number is appropriate.
	  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
		if (exp.test(telnum) != true) {
	     telNumberErrorNo = 5;
	     return false;
	  }
	  
	  // Telephone number seems to be valid - return the stripped telehone number  
	  return telnum;
}

	var telNumberErrorNo = 0;
	var telNumberErrors = new Array ();
	telNumberErrors[0] = "Valid UK telephone number";
	telNumberErrors[1] = "Telephone number not provided";
	telNumberErrors[2] = "UK telephone number without the country code, please";
	telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
	telNumberErrors[4] = "The telephone number should start with a 0";
	telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
