//Send email for newsletter

//Extend JQuery with a function to clear a text box
$.fn.clearme = function() {
	//var cName = this.value;
	//alert(cName);
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};


$("#email").clearme();

//Function to check for valid email address
function isValidEmailAddress(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);
}

//Send email for newsletter
$(function() {    
  $(".button_signup").click(function() {  
	//1. Validate the address
    var email = $("input#email").val();  
    if (!isValidEmailAddress(email)) {  
		$("input#email").focus();  
		$("#signup_error").html("Please enter a valid email address");
		return false;  
    } 
	//2. Send the form
	var dataString = 'email=' + email ;  
	
	$.ajax({
		url: "Services/Subscriber.aspx",  
		data: dataString, 
		success: function() { 
		$("fieldset.newsletter_box").hide();
		$(".newsletter_box p").hide();
		$("#signup_error").html("");
		$(".newsletter_box .qh_box_body_container").append("<p class=\"thanks_email\">Thank you, your email address has been submitted.</p><span class=\"button_minor signup\"><input type=\"button\" id=\"newsletter\" name=\"newsletter\" value=\"add another email\" class=\"fields_back thanks_email\"></span>");
		$(".fields_back").click(function(){
			$("fieldset.newsletter_box").show();
			$(".newsletter_box p").show();
			$(".thanks_email").remove();
			$("input#email").focus().val('');	
		});

		}
	});
  });  
});  


//HIDE AND SHOW VIEW DETAILS
// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {

viewDetailsLinks()
});

function viewDetailsLinks() {

    var showText = 'view details';
    var hideText = 'hide details';


    // initialise the visibility check
    var is_visible = false;

    $('.details_button').append(' <a href="#" class="toggleLink">' + showText + '</a>')


    // hide all of the elements with a class of 'toggle'
    $('.toggle').hide();

    // capture clicks on the toggle links
    $('a.toggleLink').click(function() {

        // switch visibility
        is_visible = !is_visible;


        //change the link text for the other button
        $(this).parent().parent().parent().parent().children('.details').children('.carrier').children('.details_button').children().html((!is_visible) ? showText : hideText);

        // toggle the display - uncomment the next line for a basic "accordion" style
        $(this).parent().parent().parent().parent().children('.details').children('.toggle').toggle('slow');

        // return false so any link destination is not followed
        return false;

   
    });
}
