﻿$(function() {   
//    $('#comentarios_lista').hide();
    $('#comentarios_form').hide();
        $(".but_lnk_readComments").click(function() {   
            $('#comentarios_form').hide();
            $('#comentarios_lista').fadeIn('slow');
        });
        $(".but_lnk_writeComments").click(function() {   
            $('#comentarios_lista').hide();
            $('#comentarios_form').fadeIn('slow');
        });
});
jQuery.query = function() { 
            var r = {}; 
            var q = location.search;         
            q = q.replace(/^\?/,''); // remove the leading ?         
            q = q.replace(/\&$/,''); // remove the trailing & 
            jQuery.each(q.split('&'), function(){ 
                    var key = this.split('=')[0]; 
                    var val = this.split('=')[1]; 
                    // convert floats 
                    if(/^[0-9.]+$/.test(val)) 
                            val = parseFloat(val); 
                    // ingnore empty values 
                    if(val) 
                            r[key] = val; 
            }); 
            return r; 
}; 

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); 
}

$(function() {   
    $(".but_lnk_readComments").click(function() {   
    // validate and process form here   
        var did= $.query()['did']; 
        //$('#comentarios_lista').fadeOut('slow');
        $.ajax({
        type: "POST",
        url: "listCommentsByPage.aspx",
        data: "did=" + did ,
        success: function(html) {
//        $('#comentarios_form').html("<div id='message'></div>");
        //$('#comentarios_lista').fadeIn('slow')
        $('#comentarios_lista').html(html)
        //.fadeIn(1500)
      }
     });
    return false;
	});
});


$(function() {   
  $('.error').hide();  
    $(".button").click(function() {   
    // validate and process form here   
       
    $('.error').hide();   
      var name = $("input#name").val();   
        if (name == "") {   
      $("label#name_error").show();   
      $("input#name").focus();   
      return false;   
    }   
        var email = $("input#email").val();   
        if (email == "") {   
          $("label#email_error").show();   
          $("input#email").focus();   
          return false;   
        }else{
            if(email != "") 
                { 
                    if(isValidEmailAddress(email)) 
                    { 
                    }else{
                        $("label#invalidEmail").show();   
                        $("input#email").focus(); 
                        return false;
                    }
            }
        }        
        
        var local = $("input#local").val();   
        if (local == "") {   
      $("label#local_error").show();   
      $("input#local").focus();   
      return false;   
    }  
    
    //var msg = $("textarea#msg").val();
    var msg = $("textarea#msg").html();   
        if (msg == "") {   
      $("label#msg_error").show();   
      $("textarea#msg").focus();   
      return false;   
    }
    
            //var did = $.jqURL.get("did"); 
            var did= $.query()['did']; 
            //alert(did);

    		var dataString = 'did=' + did + '&nome='+ name + '&email=' + email + '&local=' + local +'&msg=' + msg ;
		    //alert (dataString);//return false;
		
		$.ajax({
        type: "POST",
        url: "sendComment.aspx",
        data: "did=" + did + "&nome=" + name + "&email=" + email + "&local=" + local + "&msg=" + msg,
        success: function() {
        $('#comentarios_form').html("<div id='message'></div>");
        $('#message').html("")  
			.append("<div id='thanks_msg' style='width:618px; background-color:#fdfddd'>")
			.append("<div style='padding:0 0 4px 4px; border-bottom:1px dotted #8c8c8c; background-color:#fdfddd''>OBRIGADO PELA SUA PARTICIPAÇÃO</div>")
			.append("<div style='padding:10px 4px 10px 4px; background-color:#fdfddd''>Todos os comentários estão sujeitos a aprovação.</div>")
			.append("</div>")
			.append("<br class='clearfloat' />")
        .fadeIn(1500)
      }
     });
    return false;
	});
});

