$.metadata.setType("attr", "validate");
var $j = jQuery.noConflict();

$j(document).ready(function(){    

  // open all external links in new window
  $j('a[href^=http:], a[href^=https:]').click(function() {
    window.open($j(this).attr('href'));
    return false;
  })
  
   // replace legend tag to match the live site design                            
  $j('.agegroup fieldset legend').replaceWith('<label class="floatLeft">Age Group:</label>')          
          
  // button hovers
  $j(".submit-button,.send-button")
    .bind("mouseover", function() { $j(this).addClass("hover"); })
    .bind("mouseleave", function() { $j(this).removeClass("hover")
  }).attr('value','');
                                         
  // live email validation
  $j("#email").keyup(function(){        
    var email = $j("#email").val();        
    if(email != 0) {
      if(isValidEmailAddress(email)) {
        $j("#emailmsg").html("Valid Email Address");            
        $j("#emailmsg").css({"color": "green"});                            
      } else {
        $j("#emailmsg").html("Invalid Email Address");            
        $j("#emailmsg").css({"color": "red"});                                
      }
    } else {
      $j("#emailmsg").css({"display": "none"})
    }        
  });            
         
  // form modal box
  $j('#submitWindow').jqm({
    modal: true,
    overlay: 80
  });            
                  
  // bind 'onelife-form'
  $j('#onelife-form').ajaxForm(function(data) { 
    var error_message = data.split("<error-message>");        
    //check returned error message
    if (error_message.length==2) {
      $j('#callbackmsg').html(error_message[0]);
    } else {
      $j('#callbackmsg').html('Email invitation has been sent.');
      $j('#submitWindow').fadeOut(3000, function() {
          $j('#submitWindow').jqmHide();
      });            
    }  
  }); 
  
  //clear value on click
  $j('#friends-email').click(function(){                        
    if ($j('#friends-email').val()=="Enter your friend(s) email") {
      $j('#friends-email').val('');
    }
  });

//clear value on click
  $j('#emailsender').click(function(){                        
    if ($j('#emailsender').val()=="Enter your name") {
      $j('#emailsender').val('');
    }
  });
  
  //set default value if non specified
  $j('#friends-email').blur(function(){                        
    if ($j('#friends-email').val()=="") {
      $j('#friends-email').val('Enter your friend(s) email');
    }            
  });
  
//set default value if non specified
  $j('#emailsender').blur(function(){                        
    if ($j('#emailsender').val()=="") {
      $j('#emailsender').val('Enter your name');
    }            
  });

  //clear values on call
  $j('#sendtofriend').click(function(){          
    $j('#callbackmsg').html('');
    
  $j('#emailsender').val('Enter your name');        
    $j('#friends-email').val('Enter your friend(s) email');
    $j('#message').val('');
  });
  
  //hide email to friend on esc keyup
  $j(document).keyup(function(e){ 
    if (e.keyCode==27) {
      $j('#submitWindow').jqmHide();
    }
  });             
});    
      
// form validation
jQuery(function() {                
  var form = jQuery("#formDownload").validate();                
  jQuery("#reset").click(function() {
  form.resetForm();
  });
});
      
var isValidEmailAddress = function(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}             

