function mailpage(){
    mail_str = "mailto:?subject=Check out " + document.title;
    mail_str += "&body=I thought you might be interested in this site about West Sac City Council candidate " + document.title;
    mail_str += ". You can view his site at, " + location.href;
    location.href = mail_str;
}


findIndex = function(obj, value){
    var ctr = "";
    for (var i=0; i < obj.length; i++) {
        if (obj[i].name == value) {
            return i;
        }
    }
    return ctr;
};


function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
        return false;
   }else{
        return true;
   }
}




function addEmail(formData, jqForm, options){
    //alert("addEmail");
    var queryString = $.param(formData);
    //alert(queryString);  
    var theIndex = findIndex(formData, 'email_address');
    var theemail = formData[theIndex].value;
    if(validateEmail(theemail)){
        //alert('Valid Email Address');
        $.ajax({
            type: "POST",
            url: "/add_new_email/", 
            data: queryString,
            success: function(msg){
                //alert("HttpResponse: " + msg);
            }
        });
        return true; 
    }else{
        return false; 
    };
    
}


function addEmailPostSubmit(responseText, statusText){
    replyMessage = "Thank you for signing up for Mark\'s mailing list.  Your email is secure and will only be used to inform you about upcoming events and information regarding Mark's work on City Council."; 
    alert(replyMessage);
}






function addSupporter(formData, jqForm, options){
    //alert("addSupporter");
    var goodtogo = 0;
    var queryString = $.param(formData);
    //alert(queryString);  
    var theIndex = findIndex(formData, 'email');
    var theemail = formData[theIndex].value;
    var theIndex = findIndex(formData, 'posts');
    var theposts = formData[theIndex].value;
    var theIndex = findIndex(formData, 'pw');
    var thepw = formData[theIndex].value;
    var theIndex = findIndex(formData, 'username');
    var theusername = formData[theIndex].value;

    if(validateEmail(theemail)){
        goodtogo = goodtogo + 1;
    }else{
        alert('Please enter a valid email address ...');
        return false;
    }
    
    if(theposts == "Y"){
        if(thepw != ""){
            goodtogo = goodtogo + 1;
        }else{
            alert('Please enter a password if you want to post to this site...');
            return false;
        }
        if(theusername != ""){
            goodtogo = goodtogo + 1;
        }else{
            alert('Please enter a Display Name if you want to post to this site...');
            return false;
        }
    }
   
    if(goodtogo > 0){
        //alert('Good to go');
        $.ajax({
            type: "POST",
            url: "/add_new_supporter/", 
            data: queryString,
            success: function(msg){
                //alert(msg);
            }
        });
        return true; 
    }else{
        return false; 
    };
}


function addSupporterPostSubmit(responseText, statusText){
    replyMessage = "Thank you for your interest in Mark\'s work on the West Sacramento City Council.  Your information is secure and will only be used to inform you about upcoming events and information regarding Mark's work on City Council."; 
    alert(replyMessage);
}



function addPost(formData, jqForm, options){
    //alert("addPost");
    postsarea = $("#postsarea");
    var finalQueryString = $.param(formData);
    var theIndex = findIndex(formData, 'post');
    var thebody = formData[theIndex].value;
    $.ajax({
        type: "POST",
        url: "/add_post/", 
        data: finalQueryString,
        success: function(msg){
            //alert("HttpResponse: " + msg);
            if (msg == 'count0'){
                alert("Please go to GET EMAIL UPDATES to register to post ...");
            }else if(msg == 'inbutnopw'){
                alert("Please enter your password or go to GET EMAIL UPDATES to register to post ...");
            }else if(msg == 'inbut2ormore'){
                alert("Please enter your password or go to GET EMAIL UPDATES to register to post ...");
            }else{
                //alert("Please refresh this page ...");
            }
        }
    }); 
    return true; 
}

function addPostPostSubmit(responseText, statusText){
    $('#addpostdialog').dialog('close');
    //alert("OK");
}



function update_posts(issueid) {
            postsarea = $("#postsarea");
            most_recent = postsarea.find("div:first");
            thediv = most_recent.attr('id');
            $.getJSON("/update_posts/" + thediv + "/" + issueid + "/",
                function(data) {
                    jQuery.each(data, function() {
                        postsarea.prepend(
                            '<div id="' + this.pk + '">'
                            + '<b>' + this.fields.poster + ':</b>&nbsp;&nbsp;' + this.fields.body
                            + '</div><br/>'
                        );
                    });
                }
            );
        }






$(function() {    

    $("#addpostdialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 300,
        width: 650,
        modal: true,
    });
    
    $('#add_post').click(function() {
        $('#addpostdialog').dialog('open');
    });
    
});




