﻿function getaquote() {
    var selectedState = $('#ddlStateList').val();
    if (selectedState == "")
    { alert('Please select a state'); }
    else
    { window.open("https://online.safeauto.com/process/getaquote/startnewquoteprocess.aspx?__usershomestate=" + selectedState, "_blank"); }
}

function startEQ(loc) {
    var fo = new FlashObject(loc + "dtj3_equilizer.swf", "beat", "77", "74", "8.0", "#ffffff");
    fo.addParam("menu", "false");
    fo.addParam("wmode", "transparent");
    fo.addParam("base", loc);
    fo.write("beat");
}

function startJingle(loc) {
    var fo = new FlashObject(loc + "player_trans.swf", "audio", "270", "10", "8.0", "#ffffff");
    fo.addParam("menu", "false");
    fo.addParam("wmode", "transparent");
    fo.addParam("base", loc);
    fo.write("audio");
}

function submitComment(urldata) {
    var commentdata = {
        commentdata: $('#CommentEntryText').attr('value'),
        commentby: $('#CommentEntryBy').attr('value'),
        challenge: $('#captchaResponse').attr('value'),
        gid: $('#gid').attr('value'),
        uid: $('#cid').attr('value'),
        vid: $('#vid').attr('value')
    };
    //string comment, string commentby, string challenge, string uid, string vidId
    $.ajax({
    	type: 'POST',
    	url: urldata,
    	data: JSON.stringify(commentdata),
    	contentType: 'application/json; charset=utf-8',
    	dataType: 'json',
    	success: function (msg) {
    		var boxtoshow;
    		var recaptchame = false;
    		if (msg == true) {
    			boxtoshow = '#CaptchaResponseSuccessBox';
    			recaptchame = true;
    			$('#CommentEntryText').val('');
    			$('#CommentEntryBy').val('');
    			$('#captchaResponse').val('');
    		}
    		else
    			boxtoshow = '#CaptchaResponseFailureBox';

    		$('#commentEntry').slideUp(500, function () {
    			openstate = false;
    			$(boxtoshow).dialog({
    				modal: true,
    				autoOpen: false,
    				width: 600,
    				maxWidth: 600,
    				minWidth: 600,
    				show: 'fade',
    				hide: 'fade',
    				close: function (event, ui) { if (recaptchame) { recaptcha(2); recaptchame = false; } },
    				shadow: true,
    				resizable: false,
    				buttons: { "Ok": function () {
    					$(this).dialog("close");
    				}}
    			});
    			$(boxtoshow).dialog("open");
    		});    		
    		
    	},
    	error: function (result) {
    		alert('unable to retrieve your request');
    	}
    });			
}

function submitVote() {
    var votedata = {
        challenge: $('#captchaResponse2').attr('value'),
        gid: $('#gid2').val(),
        uid: $('#cid2').val(),
        vid: $('#vid').val()
    }
    //string comment, string commentby, string challenge, string uid, string vidId
    $.ajax({
    	type: 'POST',
    	url: '../Vote/VoteChallenge',
    	data: JSON.stringify(votedata),
    	contentType: 'application/json; charset=utf-8',
    	dataType: 'json',
    	success: function (msg) {
    		var boxtoshow;
    		var recaptchame = false;
    		if (msg == true) {
    			boxtoshow = '#CaptchaResponseSuccessBox2';
    			recaptchame = true;
    		}
    		else
    			boxtoshow = '#CaptchaResponseFailureBox2';

    		votingopen = false;
    		$('#VoteCaptchaSection').slideUp(500, function () {
    			openstate = false;
    			$(boxtoshow).dialog({
    				modal: true,
    				autoOpen: false,
    				width: 600,
    				maxWidth: 600,
    				minWidth: 600,
    				show: 'fade',
    				hide: 'fade',
    				close: function (event, ui) { if (recaptchame) { recaptcha(1); recaptchame = false; } },
    				shadow: true,
    				resizable: false,
    				buttons: { "Ok": function () {
    					$(this).dialog("close");
    				}
    				}
    			});
    			$(boxtoshow).dialog('open');
    		});
    		return true;
    	},
    	error: function (msg) {
    		return false;
    	}
    });

}

function deleteComment(cid) {
    var commentdata = { cid: cid.toString() };
    //string comment, string commentby, string challenge, string uid, string vidId
    $.ajax({
        type: 'POST',
        url: '../Comment/TD',
        data: JSON.stringify(commentdata),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            if (msg == true)
                location.replace(location.href);
            else
                alert('Unable to delete at this time.');
        }
    });
}


function recaptcha(wc) {
    if (wc == 1)         
        recaptchaVote();       
    else
        recaptchaComment();
}


function recaptchaVote() 
    {
        var votedata = {
            gid: $('#gid2').attr('value'),
            uid: $('#cid2').attr('value'),
            vid: $('#vid').attr('value')
        }
        $('#captchaResponse2').attr('value', '');
        //string comment, string commentby, string challenge, string uid, string vidId
        $.ajax({
            type: 'POST',
            url: '../Vote/ChangeCaptcha',
            data: JSON.stringify(votedata),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                var boxtoshow;
                if (msg.ChangeGranted) {
                    $('#votecapimage').fadeOut(500, function () {
                        $(this).attr('src', msg.NewURL);
                        $('#votecapimage').fadeIn(500);
                    });
                    $('#cid2').attr('value', msg.UID);
                    boxtoshow = '#CaptchaChangeSuccessBox';
                }
                else {
                    boxtoshow = '#CaptchaChangeFailureBox';


                    $(boxtoshow).dialog({
                        modal: true,
                        autoOpen: false,
                        width: 600,
                        maxWidth: 600,
                        minWidth: 600,
                        show: 'fade',
                        hide: 'fade',
                        shadow: true,
                        resizable: false,
                        buttons: { "Ok": function () { $(this).dialog("close"); } }
                    });

                    $(boxtoshow).dialog('open');
                }
            }
        });
        return false;
 }

 function recaptchaComment() {
     var votedata = {
         gid: $('#gid').attr('value'),
         uid: $('#cid').attr('value'),
         vid: $('#vid').attr('value')
     }
     $('#captchaResponse').attr('value', '');
     //string comment, string commentby, string challenge, string uid, string vidId
     $.ajax({
         type: 'POST',
         url: '../Vote/ChangeCaptcha',
         data: JSON.stringify(votedata),
         contentType: 'application/json; charset=utf-8',
         dataType: 'json',
         success: function (msg) {
             var boxtoshow;
             if (msg.ChangeGranted) {
                 $('#commentcapimage').fadeOut(500, function () {
                     $(this).attr('src', msg.NewURL);
                     $('#commentcapimage').fadeIn(500);
                 });
                 $('#cid').attr('value', msg.UID);
                 boxtoshow = '#CaptchaChangeSuccessBox';
             }
             else {
                 boxtoshow = '#CaptchaChangeFailureBox';


                 $(boxtoshow).dialog({
                     modal: true,
                     autoOpen: false,
                     width: 600,
                     maxWidth: 600,
                     minWidth: 600,
                     show: 'fade',
                     hide: 'fade',
                     shadow: true,
                     resizable: false,
                     buttons: { "Ok": function () { $(this).dialog("close"); } }
                 });

                 $(boxtoshow).dialog('open');
             }
         }
     });
     return false;

 }
