jQuery(document).ready(function($) {

  $('.shadow').click(function(){
    $('#side-claim-content input.required, #main-claim-content input.required, #topofform input.required').each(function(){
      $(this).removeClass("form_errors");
      $('.required_message, .required_message_right').hide();
    });
    
    $('#side-claim-content').find('input, select, textarea, .form_line').css('z-index',0);
    $('#main-claim-content').find('input, select, textarea, .form_line').css('z-index',0);
    if($.browser.msie && parseInt($.browser.version)==6)
    {
      $('#main-claim-content select').css('display','inline-block');
    }
    $('#main-claim-submit').css('position','relative');
    
    $(this).fadeOut();
  });
  
  $('#sidebar-claim-submit').click(function(){
    if($('#side-claim-content form input.form_errors').size()>0 && $('#side-claim-content .shadow').css('display')=='block')
    {
      /*$('.required_message:visible').css('left',($('.required_message:visible').position().left+$('.required_message:visible').width())+'px');*/
      $('.required_message:visible').dequeue().animate({right:"+=20px"},100).animate({right:"-=20px"},130)
        .animate({right:"+=15px"},160).animate({right:"-=15px"},210)
        .animate({right:"+=10px"},220).animate({right:"-=10px"},230)
        .animate({right:"+=5px"},240).animate({right:"-=5px"},250);
    }
    
    if($('#side-claimm #topofform form input.form_errors').size()>0 && $('#side-claimm #topofform form .shadow').css('display')=='block')
    {
     /* alert('dfbdfb'); */
      /*$('.required_message:visible').css('left',($('.required_message:visible').position().left+$('.required_message:visible').width())+'px');*/
      $('.required_message:visible').dequeue().animate({right:"+=20px"},100).animate({right:"-=20px"},130)
        .animate({right:"+=15px"},160).animate({right:"-=15px"},210)
        .animate({right:"+=10px"},220).animate({right:"-=10px"},230)
        .animate({right:"+=5px"},240).animate({right:"-=5px"},250);
    }
  });
  
  $('#main-claim-submit, #CONTACT').click(function(){
    if($('#main-claim-content form input.form_errors').size()>0 && $('#main-claim-content .shadow').css('display')=='block')
    {
      $('.required_message_right, .required_message').css('left',($('.required_message_right, .required_message').position().left)+'px');
      $('.required_message_right:visible, .required_message:visible').dequeue().animate({left:"+=20px"},100).animate({left:"-=20px"},130)
        .animate({left:"+=15px"},160).animate({left:"-=15px"},210)
        .animate({left:"+=10px"},220).animate({left:"-=10px"},230)
        .animate({left:"+=5px"},240).animate({left:"-=5px"},250);
    }
  });
	  
  $('#side-claim-content input, #side-claim-content textarea, #side-claim-content select').each(function(){
    $(this).keyup(function(){
      if(checked_once)
      {
        if($(this).attr('data-store')!="")
        {
	        $(this).parent().find('.required_message:visible').hide();
	        $(this).removeClass("form_errors");
	      }
	      
	      if($('#side-claim-content .form_errors').size()==0)
	      {
	        $('.shadow').fadeOut();
	        $('#side-claim-content').find('input, select, textarea, .form_line').css('z-index',0);
	        if($.browser.msie && parseInt($.browser.version)==6)
          {
            $('#main-claim-content select').css('display','inline-block');
          }
	      } 
      }
    });
  });

  $('#main-claim-content input, #main-claim-content textarea, #main-claim-content select').each(function(){
    $(this).keyup(function(){
      if(checked_once)
      {
        if($(this).attr('data-store')!="")
        {
	        $(this).parent().find('.required_message_right:visible, .required_message:visible').hide();
	        $(this).removeClass("form_errors");
	      } 
	      
	      if($('#main-claim-content .form_errors').size()==0)
	      {
	        $('.shadow').fadeOut();
	        $('#main-claim-content').find('input, select, textarea, .form_line').css('z-index',0);
	        $('#main-claim-submit').css('position','relative');
	        if($.browser.msie && parseInt($.browser.version)==6)
          {
            $('#main-claim-content select').css('display','inline-block');
          }
	      } 
      } 
    });
  });
});

var valid = 0;
var checked_once = false;
	
function validateFormSidebar(passed_form,direction,with_scroll,telephone_checked){
	
	telephone_checked==false;
	checked_once = true;
	if (with_scroll == null){
    	with_scroll = true;
  	}
	
	var my_form = $(passed_form);
		
	$(my_form).find('input.required').each(function(){
		  
	  //check fields have been filled in
	  if($(this).val()=="")
	  {
	    if(direction=='left')
	    {
	      $('#'+direction+'_'+$(this).attr('name')+'_msg').css({'top':'3px','position':'absolute'});
	    }else{
	      $('#'+direction+'_'+$(this).attr('name')+'_msg').css({
	        'top':$(this).position().top+'px',
	        'left':($(this).position().left+$(this).width()+10)+'px',
	        'position':'absolute'
	      });
	    }
	    $('#'+direction+'_'+$(this).attr('name')+'_msg').css('display','block');
	    
	    $(this).addClass("form_errors");
	    if($(this).attr('name')=="telephone_number")
	    {
	      telephone_checked = true;
	    }
	  }else{
	    $('#'+direction+'_'+$(this).attr('name')+'_msg').hide();
	    $(this).removeClass("form_errors");
	  }
	  
	  //check telephone number is valid
	  if($(this).attr('name')=="telephone_number" && !telephone_checked)
	  {
	    var fixedtel = $(this).attr('value')
	    fixedtel = fixedtel.replace(/[^\d]/g, "");
	    if(fixedtel.length < 10)
	    {
	      if(direction=='left')
	      {
	        $('#'+direction+'_'+$(this).attr('name')+'_msg_1').css({'top':'3px','position':'absolute'});
	      }else{
	        $('#'+direction+'_'+$(this).attr('name')+'_msg_1').css({
	          'top':$(this).position().top+'px',
	          'left':($(this).position().left+$(this).width()+10)+'px',
	          'position':'absolute'
	        });
	      }
	      $('#'+direction+'_'+$(this).attr('name')+'_msg_1').css('display','block');
	      
	      $(this).addClass("form_errors");		    
	    }else{
	      $('#'+direction+'_'+$(this).attr('name')+'_msg_1').hide();
	      $(this).removeClass("form_errors");
	    }
	  }
	});
	
	if($(my_form).find('input.form_errors').size()>0)
	{
	  $(my_form).find('input, select, textarea').css({'z-index':500,'position':'relative'});
	  $(my_form).find('div.form_line').css({'z-index':150,'position':'relative'});
    $(my_form).find('.shadow').width($(document).width());
    $(my_form).find('.shadow').height($(document).height());
    $(my_form).find('.shadow').css('opacity','0.5');
    if($.browser.msie && parseInt($.browser.version)==6)
    {
      $('form select').css('display','none');
      $(my_form).find('select').css('display','inline-block');
    }
   
    $(my_form).find('.shadow').fadeIn(500,function(){
    	 $(my_form).find('input[name="name"]').css('z-index',500);
    	$(my_form).find('input[name="name"]').css('zoom',1);
    });
    if(with_scroll)
    {
      $('html, body').animate({scrollTop: $(my_form).offset().top}, 'slow');
    }
	  return false;
	}else{
	  $(my_form).find('input, select, textarea').css('z-index',0);
	  $('#main-claim-submit').css('position','relative');
	  $(my_form).find('.shadow').fadeOut();
	  return true;
	}
}
