/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function submitComment(){
    $("#cloading").show();
    var action=$("#action").val();
    var poll_ident=$("#poll_ident").val();
    var alias=$("#alias").val();
    var name=$("#name").val();
    var email=$("#email").val();
    var cmt=$("#cmt").val();
    var vcode=$("#vcode").val();
    var d = new Date();
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType: "html",
        data:"action="+action+"&poll_ident="+poll_ident+"&name="+name+"&email="+email+"&cmt="+cmt+"&vcode="+vcode,
        success:function(msg){
            if(msg==1){
                $("#cferror").html('Please fill all valid information');
                $("#captcha").attr("src", "securimage_show.php?"+d.getTime());
            }
            else if(msg==2){
   	$("#cferror").html('Please fill all valid email id');
        $("#captcha").attr("src", "securimage_show.php?"+d.getTime());
            }
            else if(msg==3){
   	$("#cferror").html('Please fill valid verification code');
        $("#captcha").attr("src", "securimage_show.php?"+d.getTime());
            }
            else{
                $("#cferror").html('');
        $("#formmsg").html(msg);
            }
        },
        error:function(msg){
            $("#cloading").text('error');
        },
        complete:function(){
            $("#cloading").hide();
        }
        
    });
    return false;
}

function  viewResult(id){
    $("#oloading"+id).show();
    $.ajax({
        type:"POST",
        dataType:"html",
        url:"ajaxaction.php",
        data:"action=viewResult&poll_ident="+id,
        success:function(msg){
            var disprs='<div align="center" style="padding-left:20px;">'+msg+'</div>';
            $("#optionresult"+id).html(disprs);
            $("#votelink"+id).html('<a href="#" class="fpoll green" onclick="return takeVote('+id+');">Take Vote</a>');
        },
        error:function(msg){
            $("#oloading").html(msg);
        },
        complete:function(){
            
            $("#oloading"+id).hide();
            $("#vbutton").hide();
        }
    });
    return false;
}
function  takeVote(id){
    $("#oloading"+id).show();
    $.ajax({
        type:"POST",
        dataType:"html",
        url:"ajaxaction.php",
        data:"action=takeVote&poll_ident="+id,
        success:function(msg){
            var disprs=msg;
            $("#optionresult"+id).html(disprs);
            $("#votelink"+id).html('<a href="#" class="fpoll green" onclick="return viewResult('+id+');">view results</a>');
        },
        error:function(msg){
            $("#oloading").html(msg);
        },
        complete:function(){
            $("#oloading"+id).hide();
             $("#vbutton").show();
        }
    });
    return false;
}

function viewMoreComment(id){
    $("#loadwait").show();
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType:"html",
        data:"action=displayallCmt&pid="+id,
        success:function(msg){
            $("#pollcmt").html(msg);
        },
        error:function(msg){
            $("#pollcmt").html(msg);
        },
        complete:function(){
            $("#loadwait").hide();
            $("#viewmorecmt").hide();
            
        }
    });
    return false;
}

function changePoll(id){
    $("#toploading").show();
   var midpolid=$("#poll_ident").val();
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType:"html",
        data:"action=changeMidPoll&id="+id,
        success:function(msg){
            $("#middlepoll").html(msg);
        },
        error:function(msg){
            $("#middlepoll").html(msg);
        },
        complete:function(){
            getNextPoll(midpolid);
            
        }
    });
    return false;
}

function getNextPoll(midpolid){
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType:"html",
        data:"action=getNextPoll",
        success:function(msg){
            $("#nextpoll").html(msg);
        },
        error:function(msg){
            alert(msg);
        },
        complete:function(){
            getPrevPoll(midpolid);
        }
    });
}
function getPrevPoll(midpolid){
     
     $.ajax({
         type:"POST",
         url:"ajaxaction.php",
         dataType:"html",
         data:"action=getPrevPoll&pid="+midpolid,
         success:function(msg){
             $("#prevpoll").html(msg);
         },
         error:function(msg){
             alert(msg);
         },
         complete:function(){
             $("#toploading").hide();
         }
     });
}

function submitVote(id){
    $("#toploading").show();
    var polid=$("#poll_ident").val();
    var nextpollid=$("#nextpollid").val();
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType:"html",
        data:"action=vote&poll_ident="+polid+"&option_id="+id,
        success:function(msg){
            if(msg==1)
                $("#voteerror").html('Your vote could not count');
            changePoll(nextpollid);        
        },
        error:function(msg){
            alert(msg);
        },
        complete:function(){
            $("#toploading").hide();
        }
    });
    
}

function popCmtWin(url){
    window.open(url,'cmtwindow','height=400, width=500,status=no, title=no, toolbar=no,resizable=no');
    return false;
}

function createPoll(){
    $("#loadingcr").show();
    
    var question=$("#question").val();
    var option1=$("#option1").val();
    var option2=$("#option2").val();
    var option3=$("#option3").val();
    var option4=$("#option4").val();
    var option5=$("#option5").val();
    var email=$("#email").val();
    
    $.ajax({
        type:"POST",
        url:"ajaxaction.php",
        dataType:"html",
        data:"action=createquestion&question="+question+"&option1="+option1+"&option2="+option2+"&option3="+option3+"&option4="+option4+"&option5="+option5+"&email="+email,
        success:function(msg){
            if(msg=='1er'){
                $("#crerror").html('Please fill all valid infomation');
            }
            else if(msg=='2er'){
                $("#crerror").html('Please fill valid email');
            }
            else{
                    window.location.href='http://www.pollindiapoll.com/invite.php?pollid='+msg;
    }
                
        },
        error:function(msg){
            alert(msg);
        },
        complete:function(){
             $("#loadingcr").hide();
        }
    });
    
    
    return false;
}
