$(document).ready(function(){

	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);
	};

	

	$("#newsletter_form").submit(function()
	{
		if( !isValidEmailAddress($('#newsletter_email').val()))
		{
			alert('NOT VALID Email Address');
			return false;
		}
		
		if ($('#newsletter_name').val() == '')
		{
			alert('Please enter Your name');
			return false;
		}		
		
	
		action = $('#newsletter_form').attr('action');	
		$.ajax({
			type: "GET",
			data: $('#newsletter_form').serialize(),
			dataType: "html",
			url: action,
			beforeSend: function() {
			    //$('#loader').show();
			    //$('#btnNewsletter').attr('disabled','disabled');
				$('#newsletter').html('<img src="/images/ajax-loader.gif" style="margin: 10px auto;" />');
			},
			complete: function(){
				//$('#loader').hide();
				//$('#newsletter').html('');
			},			
			success: function(data) {
				if (data == 'OK')
					$('#newsletter').html('<span class="newsletter_msg">Thank you, Your name and email address was added to newsletter list.</span>');
				else 
					$('#newsletter').html('<span class="newsletter_msg_error">We are sorry but there was a problem during adding Your name and email address to newsletter list.</span>');
				
			}
		});
		
	  return false;	
	});
});


/*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none 
    ---------------------------------------------------------*/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = document.getElementById(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}

/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {


    //if ($('tabContent'+num).style.display == 'none'){
	if (document.getElementById('tabContent'+num).style.display == 'none'){
	
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = document.getElementById(temph);
                if (!h){
                    var h = document.getElementById('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = document.getElementById(tempc);
				
                if(c.style.display != 'none'){
                    //if (animate || typeof animate == 'undefined')
                        //Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
						//Effect.toggle(tempc,'appear',{duration:0.3, queue:{scope:'menus', limit: 3}});
                    //else
                        toggleDisp(tempc);
                }
            }
        }
        var h = document.getElementById('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        h.blur();
        var c = document.getElementById('tabContent'+num);
        c.style.marginTop = '1px';		
        //if (animate || typeof animate == 'undefined'){
            //Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
			//Effect.toggle('tabContent'+num,'appear',{duration:0.4, from: 0.2, queue:{scope:'menus', position:'end', limit: 3}});
			
        //}else{		
            toggleDisp('tabContent'+num);
        //}
    }
}

