var options = { 
        target:        '#output2',   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: false        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };
function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    alert('About to submit: \n\n' + queryString); 
    return true; 
} 

function showResponse(responseText, statusText)  {
	if(responseText.Response.ResponseMessage.Error == 1)
	{
		$('.errors').show().html(responseText.Response.ResponseMessage.Message);
		$('#txtNameTo').blur(checkToField);
		if($.cookie('txt_Message')) $('#txtMessage').attr('value', $.cookie('txt_Message'));
	}
	else
		$('#share_holder').fadeOut(1000, function () {
			$('#shareForm').clearForm();
			//$('#shareForm').html('');
	        $('.pp_overlay3').css('display','block');
	        $('.errors').hide().html('');
	        $('.share_link').attr('jsclicked', '');
	        $.blockUI({message:$('#share_holder_thankyou')});
	        $('.btn_close1').click(function() {
	            $('#share_holder').fadeOut('slow');
	            $('#share_holder_thankyou').fadeOut('slow');
	            $.unblockUI();
	            $('.share_link').attr('jsclicked', '');
	            $('.pp_overlay3').fadeOut('1000');
	        });
	        $('#share_holder_thankyou').show();
	        $('#share_holder_thankyou').css('margin-top','0px');
	        $('#share_holder').css('display','none');
	    });
}

function load_share(div_id){
	$('.share_link').click(
		function(){
			if($(this).attr('jsclicked') == 'on')
			{
				div_id.fadeOut('slow');
				div_id.css('margin-top','-900px');
				$.unblockUI();
				$(this).attr('jsclicked', '');
                $('.pp_overlay3').fadeOut('1000');
			}
			else
			{
				
				$('#shareForm').clearForm();
				if(div_id.html() == '')
				{
					var url = $(this).attr('href');
					$.blockUI({message:div_id});
					$.get(url,{},
						function(response){
							$('.pp_overlay3').fadeIn('1000');
							div_id.append(response).fadeIn('slow');
							clearInput();
							$('.btn_close').click(
								function(){
									$('.errors').hide().html('');
									div_id.fadeOut('slow');
									div_id.css('margin-top','-900px');
									$.unblockUI();
									$('.share_link').attr('jsclicked', '');
                                    $('.pp_overlay3').fadeOut('1000');
								}
							);
							$('.btn_send_email').click(
								function(){
									$.cookie('txt_Message', $('#txtMessage').val(), { expires: 14, path: '/'});
									$('#productId').val(currentProductId);
									$('#shareForm').ajaxSubmit(options);
									
								}
							);
						}
					);
				}
				else
				{
                    $('.pp_overlay3').fadeIn('1000');
					div_id.fadeIn('slow');
					div_id.css('margin-top','0px');
					$.blockUI({message:div_id});
				}
				if($.cookie('txt_Message')) $('#txtMessage').attr('value', $.cookie('txt_Message'));
				//$('#shareForm').clearForm();
				$(this).attr('jsclicked', 'on');
			}		
			return false;
		}
	);
}
$(document).ready(function(){
var div_id = $('#share_holder');
load_share(div_id);
});
