﻿/* Contest stuff */

function CloseVoteForm() {
    $('div[VoteForm="1"]').hide(500);
}

function RateDesign(designid) {
    CloseVoteForm();
    $('#divRateBox_' + designid).show(500);
}

function CloseRateDesignForm(designid) {
    $('#divRateBox_' + designid).hide(500);
}

function ToggleReply(id) {
    $('div[ReplyForm="1"]').hide(500);
    $('#divReply_' + id).show(500);
}

function ToggleSignin(id) {
    $('div[ReplyForm="1"]').hide(500);
    $('#divSignIn_' + id).show(500);
}

function VoteUP(id) {
    var username = $('#txtUsername');
    var usernametxt = "";
    var password = $('#txtPassword');
    var passwordtxt = "";
    var notify = "0";
    if (username != null) {
        usernametxt = username.val();
    }
    if (password != null) {
        passwordtxt = password.val();
    }
    if ($('#chbkIsNotify').is(':checked')) {
        notify = "1";
    }
    
    if (id > 0) {
        $.ajax({
            url: "/rate/design/" + id + "/",
            global: false,
            type: "POST",
            data: ({ u: usernametxt, p: passwordtxt, n: notify }),
            dataType: 'json',
            success: function (response) {
                if (response.voteStatus == 1 || response.voteStatus == 2) {
                    $("#divVoteForm").hide(500);
                    $('#divVoteStatus').html(response.output);
                    window.setTimeout(CloseVoteForm, 2000);
                }
                else {
                    $('#divVoteStatus').html("<font color='red'>" + response.output + "</font>");
                }
            }
        });
    }
}

function PostComment(designID) {
    var username = $('#txtUsername');
    var usernametxt = "";
    var password = $('#txtPassword');
    var passwordtxt = "";
    var comment = $('#comment');
    if (username != null) {
        usernametxt = username.val();
    }
    if (password != null) {
        passwordtxt = password.val();
    }
    if (comment.val().length > 0 && designID > 0) {
    	$.ajax({
    		url: "/postcomment/design/" + designID + "/",
    		global: false,
    		type: "POST",
    		data: ({ u: usernametxt, p: passwordtxt, c: comment.val() }),
    		dataType: 'json',
    		success: function (response) {
    			if (response.postStatus == 1) {
    				$("#divCommentForm").hide(500);
    				$('#divCommentStatus').html(response.output);
    				$('#comment_placeholder').after('<div style="padding: 10px; border: 1px solid white;"><div class="normal black">' + comment.val() + '</div><div class="small gray" style="margin-top: 15px;">Posted by: You</div></div><div style="height: 20px; clear: both;"></div><div style="height: 20px; clear: both;"></div>');
    				window.setTimeout(CancelComment, 2000);
    			}
    			else {
    				$('#divCommentStatus').html("<font color='red'>" + response.output + "</font>");
    			}
    		}
    	});
    } else {
        $('#divCommentStatus').html("<font color='red'>Please enter your comment</font>");
    }
}

function VoteFormAPICall(id) {
    $('div[VoteForm="1"]').hide(500);
    $('div[VoteForm="1"]').html("");

    $.ajax({
        url: '/rateform/design/' + id + '/',
        success: function (data) {
            $('#divVote_' + id).html(data);
            $('#divVote_' + id).show(500);
        }
    });
}

var dsc_s = false;

function GetCommentForm(designid, commentid) {
	if (!dsc_s) {
		dsc_s = true;
		if (commentid == 0) {
			$('div[IsFirst="1"]').hide(500);
		}
		$.ajax({
			url: '/commentform/design/' + designid + '/' + commentid + '/',
			success: function (data) {
				$('#divCommentForm_' + commentid).html(data);
				$('#divCommentForm_' + commentid).show(500);
			}
		});
	}
}

function CancelComment() {
	dsc_s = false;
    $('div[CommentFrom="1"]').hide(500);
    $('div[CommentFrom="1"]').html("");
}

/* End Contest stuff */
