window.addEvent('domready', function() {

					var parent_div = $('register_forms');
					var container_div = $('new_content');
					var myFx = new Fx.Tween(parent_div);
					$('underlay').fade(0);
					$('results_div').fade(0);
		
		
		  page2 = false;			
			step_1();
			
  function step_1(){
  			//step1
  						
  						$('details').addEvent('submit', function(e){
  								e.stop();
                  var current_height = parent_div.getStyle('height').toInt();
									
									error = 0;
									
				  var debtlevel = this.debtlevel.value;
                  var income = this.income.value;
                  var expenditure = this.expenditure.value;
									
									var myFx1 = new Fx.Morph(this.debtlevel, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
 									var myFx4 = new Fx.Morph(this.income, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
									var myFx5 = new Fx.Morph(this.expenditure, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
									
										if(debtlevel==""){
												myFx1.start('.error');
												error = 1;
										} else {
												myFx1.start('.normal');
										}

										if(income==""){
												myFx4.start('.error');
												error = 1;
										} else {
												myFx4.start('.normal');
										}
										
										if(expenditure==""){
												myFx5.start('.error');
												error = 1;
										} else {
												myFx5.start('.normal');
										}
  									
  									if(error==0){	
                      
        										myFx.set('height', current_height);
        										
        										parent_div.addClass('ajax-loading');
        										container_div.style.visibility = "hidden";	
        										//container_div.empty();
  										
  									
  									this.set('send', {onComplete: function(response) { 								
  														
  														
  														//var myJSONObject = JSON.decode(response);
  																	//passed = myJSONObject.success;
  																	passed = "true";
	
  																		if(passed=="true"){
          																var nexturl = 'forms/form2.php';
																						
																						var myRequest = new Request.HTML({
                            									url: nexturl,
                            									evalScripts: true,
                            									evalResponse: true,
                            									onSuccess: function(responseTree, 
                                              responseElements, 
                                              responseHTML, 
                                              responseJavaScript){									
                          										
                            														container_div.set('html',responseHTML); 
                            														//$exec(responseJavaScript); 
																												
																												$('contactInfo_btn').addEvents({
        							
                                                  						mouseenter:function(){									
                                                  						$('ContactInfo').set('tween', {
                                          												transition: Fx.Transitions.Quart.easeOut
                                                            			}).tween('right', '0px');
                                                  								
                                                  
                                                  						},
                                                  						mouseleave:function(){
                                                  								
                                                  								$('ContactInfo').set('tween', {
                                          												transition: Fx.Transitions.Quart.easeOut
                                                            			}).tween('right', '-161px');
                                                  
                                                  						}
                                                  			});
																												
                                                														
                            									}
                            									}).get({'rand': $time()+$random(1, 10000)}).chain(function(){
                								 
																 
																
                              																  var child_height = container_div.getStyle('height').toInt();
                                            										myFx.start('height', child_height+5).chain(function(){
                              																	parent_div.removeClass('ajax-loading');
                              																	container_div.style.visibility = "visible";	
                              																	});
																																
																															
                                               								step_2();
																																							
                              														
                                              	});
																						
																					
  																						
  																		
  																	} else {
  																				 parent_div.removeClass('ajax-loading');
                                					 container_div.style.visibility = "visible";	
  																				 
  																  }
  
                      														
  									}});
  									
  									this.send();
  									
  									}
  										
              });
  
  			}
				
				
				
				  function step_2(){
					
					$('form_holder').style.background = "none";
					$('form_holder').setStyle('background-image','url(images/form_bg2.png)');
					$('form_holder').setStyle('background-repeat','no-repeat');
					page2 = true;				
										
  			//step1
  						
  						$('details2').addEvent('submit', function(e){
  								e.stop();
                  var current_height = parent_div.getStyle('height').toInt();
									  
										var error=0;
									 	var fname = this.Firstname.value;
										var lname = this.Lastname.value;
										var email = this.email.value;
										var contact = this.contact.value;
										
										var myEffects = new Fx.Morph(this.Firstname, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
 										var myEffects2 = new Fx.Morph(this.Lastname, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
 										var myEffects3 = new Fx.Morph(this.email, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
 										var myEffects4 = new Fx.Morph(this.contact, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
 						
										
										
										
										if(fname==""){

																				myEffects.start('.error');
					
										error = 1;
										} else {
													 							
																		myEffects.start('.normal');
											
										}
										
										
										if(lname==""){

																				myEffects2.start('.error');
					
										error = 1;
										} else {
													 							
																		myEffects2.start('.normal');
											
										}
										
										if(email==""){

																				myEffects3.start('.error');
					
										error = 1;
										} else {
										
													 	if(!validate(email)){
														myEffects3.start('.error');
														$('emailTD').innerHTML = "<span style='color:red;'>Not Valid</span>";
        										error = 1;
														} else {
																	$('emailTD').innerHTML = "&nbsp;";
																	myEffects3.start('.normal'); 
																			 
														}
													 							
																		
											
										}
										
										if(contact==""){

																				myEffects4.start('.error');
					
										error = 1;
										} else {
													 							
																		if(!checkUKTelephone(contact)){
        														myEffects4.start('.error');
        														$('telTD').innerHTML = "<span style='color:red;'>Not Valid</span>";
                										error = 1;
        														} else {
																		$('telTD').innerHTML = "&nbsp;";
        																	myEffects4.start('.normal'); 
        																			 
        														}
											
										}
										
										
										
										
  									
  									if(error==0){	
                      
        										//myFx.set('height', current_height);
        										
        										//parent_div.addClass('ajax-loading');
        										//container_div.style.visibility = "hidden";	
        										//container_div.empty();
  										
  									
  									//this.set('send', {onComplete: function(response) { 								
  														
															
															
															//$('underlay').style.display = "block";
															//$('underlay').fade(0.8);
															
															//$('results_div').style.display = "block";
															//$('results_div').fade(1);
															
															
  
                      														
  									//}});
  									
  									this.submit();
  									
  									}
  										
              });
  
  			}
				

				
				function image_rotator(){
				
				//image rotator in here
															
															var numpics = 7;
                              var whichpic = 0;
                              
                              var myTimedFunction = function (){
                              
                              if(whichpic!=numpics){
                              var nextpic = whichpic+1;
															
                              
                              var myFx = new Fx.Tween('banner1', {property: 'opacity'});
                              myFx.start(1,0).chain(
                                  //Notice that "this" refers to the calling object (in this case, the myFx object).
                                  function(){ 
                              		
                              		$('current_photo').src = "rotator/"+nextpic+".jpg"; 
                              		
                              						$('current_photo').addEvent('load', function() {
                              									$('banner1').fade('in');
                              						});
                              		
                              		}
                              );
															
															} else {
                              clear_timed();
															$('banner1').empty();
															
															$('results_div').removeClass('ajax-loading-2');
															$('results_text').innerHTML = "<p style='text-align:center;font-weight:bold;'>In order to get the best quote we will need to contact you for further information.</p><p style='text-align:center;'><a href='test_index.php'>close this window</a></p>";
                              }
															
															
                              
                                    if(whichpic!=numpics){
                                    whichpic = nextpic;
                                    }
																		
																	
                              
                              }
                              
                              
                              periodical = myTimedFunction.periodical(5000);
															
															function clear_timed(){
															$clear(periodical);
															}
				
				}
			


				
			
		

});



         


	
	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";
