/* document must also have the wz_tooltip and json javascript script in it */

function WallVote(logged_in, sku, pid, pos, type)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&pid='+pid+'&a=vote&pos='+pos+'&ptype='+type+'">login</a></u> to vote!', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
    }
    else
    {
        var url = 'process.aspx';
        var params = 'type=wallRating&sku='+sku+'&pid='+pid+'&pos='+pos+'&post_type='+type;
        
        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallVoteResult });
    }
}

function wallVoteResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype="+response.type+"&a=vote#"+response.type+"_"+response.pid; 
    }
    else
    {
        $(response.type+"VoteResponse_"+response.pid).style.display = "inline";
        $(response.type+"VoteResponse_"+response.pid).innerHTML = response.error;   
    }        
}
function DeleteWallPost(pid)
{
    var url = 'process.aspx';
    var params = 'type=deleteWallPost&pid='+pid;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: deletePostResult });
}

function deletePostResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        $("postDiv_"+response.pid).className = "deletedPost";
        $("postDiv_"+response.pid).innerHTML="Your post has been deleted.";
    }
    else
    {
         $("postDiv_"+response.pid).innerHTML = "<span class='error'>You do not have permission to delete this post.</span>" + $("postDiv_"+response.pid).innerHTML;
    }        
}
function FollowWallPost(pid)
{
    var url = 'process.aspx';
    var params = 'type=followpost&pid='+pid;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: followWallPostResult });
}

function followWallPostResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        $("postNotification_"+response.pid).innerHTML="You got it!  You are now subscribed to this post.";
    }
    else
    {
         $("postNotification_"+response.pid).innerHTML = "<span class='error'>Unable to set up email notification at this time.</span>";
    }        
}
function DeleteWallComment(pid)
{
    var url = 'process.aspx';
    var params = 'type=deleteWallComment&pid='+pid;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: deleteCommentResult });
}

function deleteCommentResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        $("commentDiv_"+response.pid).className = "deletedComment";
        $("commentDiv_"+response.pid).innerHTML="Your post has been deleted.";
    }
    else
    {
         $("commentDiv_"+response.pid).innerHTML = "<span class='error'>You do not have permission to delete this post.</span>" + $("postDiv_"+response.pid).innerHTML;
    }         
}
function updateWallProfile(bio, location)
{
    var url = 'process.aspx';
    var params = 'type=updatewallprofile&bio='+bio+'&location='+location;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: updateWallProfileResult });
}

function updateWallProfileResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        tt_HideInit();
        $("bio").innerHTML = bio_text;
        $("location").innerHTML = location_text;
    }
    else
    {
        $("profileError").innerHTML = "There has been an error with your submission. Please make sure you are still logged in and try again.";
         //$("postDiv_"+response.pid).innerHTML = "<span class='error'>You do not have permission to delete this post.</span>" + $("postDiv_"+response.pid).innerHTML;
    }        
}
function WallReview(logged_in, sku, title, text, stars)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=review">login</a></u> to review', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
    }
    else
    {
        if(stars==0) {$('reviewMsg').innerHTML = "Please choose a star rating."; return; }
        if(text=="") {$('reviewMsg').innerHTML = "Please enter your review."; return; }
        
        var url = 'process.aspx';
        var params = 'type=wallreview&title='+escape(title)+'&text='+escape(text)+'&stars='+stars+'&sku='+sku;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallReviewResult });
    }
}

function wallReviewResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype=post&a=submit#post_"+response.pid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    { 
        $('reviewMsg').innerHTML = response.error;   
    }        
}
function WallVideo(logged_in, sku, title, text, video_url)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=video">login</a></u> to upload', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
    }
    else
    {
        if(video_url=="") {$('videoMsg').innerHTML = "Please enter a video URL."; return; }
        if(title=="") {$('videoMsg').innerHTML = "Please enter a title for your video."; return; }
        
        var url = 'process.aspx';
        var params = 'type=wallvideo&title='+escape(title)+'&text='+escape(text)+'&video_url='+escape(video_url)+'&sku='+sku;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallVideoResult });
    }
}

function wallVideoResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype=post&a=submit#post_"+response.pid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    { 
        $('videoMsg').innerHTML = response.error;   
    }        
}
function WallQuestion(logged_in, sku, title, text)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=review">login</a></u> to ask your question', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
    }
    else
    {
        if(text=="") {$('reviewMsg').innerHTML = "Please enter your question."; return; }
        
        var url = 'process.aspx';
        var params = 'type=wallquestion&title='+escape(title)+'&text='+escape(text)+'&sku='+sku;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallQuestionResult });
    }
}

function wallQuestionResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype=post&a=submit#post_"+response.pid;
    }
    else
    {
        $('questionMsg').innerHTML = response.error; 
    }        
}
function WallComment(logged_in, sku, pid, text)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=review">login</a></u> to respond', CLICKCLOSE, true, STICKY, true,  FADEOUT, 200);
    }
    else
    {
        if(text=="") {$('commentMsg').innerHTML = "Please enter your text here."; return; }
        
        var url = 'process.aspx';
        var params = 'type=wallcomment&pid='+pid+'&text='+escape(text)+'&sku='+sku;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallCommentResult });
    }
}

function wallCommentResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.cid+"&a=submit&ptype=comment#comment_"+response.cid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    {
        $("commentMsg_"+response.pid).innerHTML = response.error;
    }        
}

function WallFlag(logged_in, sku, pid, text, type)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&ptype='+type+'&sku='+sku+'&a=flag&pid='+pid+'">login</a></u> to flag', CLICKCLOSE, true, STICKY, true,  FADEOUT, 200);
    }
    else
    {        
        var url = 'process.aspx';
        var params = 'type=wallflag&sku='+sku+'&pid='+pid+'&text='+escape(text)+"&ptype="+type;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallFlagResult });
    }
}

function wallFlagResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&a=flag&ptype=" + response.type + "#" + response.type + "_"+response.pid;
    }
    else
    {
        //what to do on error
    }        
}

function RemoveWallFlag(logged_in, sku, pid, type)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&ptype='+type+'&sku='+sku+'&a=flag&pid='+pid+'">login</a></u> to remove this flag', CLICKCLOSE, true, STICKY, true,  FADEOUT, 200);
    }
    else
    {        
        var url = 'process.aspx';
        var params = 'type=wallremoveflag&pid='+pid+"&ptype="+type+"&sku="+sku;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallRemoveFlagResult });
    }
}

function wallRemoveFlagResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&a=unflag&ptype="+response.type+"#"+response.type+"_"+response.pid;
    }
    else
    {
        //what to do on error
    }        
}

function ViewFlagged(type, id)
{
    $(type+"_"+id).style.display='block';
    $(type+'_flag_'+id).style.display='none';
}

function OpenFlagForm(logged_in, sku, id, type)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&ptype='+type+'&sku='+sku+'&a=flag&pid='+id+'">login</a></u> to flag', CLICKCLOSE, true, STICKY, true,  FADEOUT, 200);
    }
    else
    {   
        if($(type+"flagopen_"+id).style.display == 'none') { $(type+"flagopen_"+id).style.display = "block"; }
        else { $(type+"flagopen_"+id).style.display = "none"; }
    }
}

function ShowTooltip(id,text)
{
    Tip(text);
}

function openWallForm(obj, type)
{
    $(type+'InitialInput').style.display="none";
    $(type+'Form').style.display='block';
    if($(type+'FormTitle') != null) { $(type+'FormTitle').focus(); }
}

function closeWallForm(type)
{
    $(type+'Form').style.display='none';
    $(type+'InitialInput').style.display="block";
}
function CloseFlagForm(type, id)
{
    $(type+'flagopen_'+id).style.display='none';
}

function openCommentForm(id)
{
    $('commentInitial_'+id).style.display="none";
    $('commentForm_'+id).style.display='block';
    $('commentText_'+id).focus();
}

function closeCommentForm(id)
{
    $('commentInitial_'+id).style.display="block";
    $('commentForm_'+id).style.display='none';
}

function OpenEditForm(id, type)
{
    $('edit'+type.toTitleCase()+'_'+id).style.display = "block";
}

function CloseEditForm(id, type)
{
    $('edit'+type.toTitleCase()+'_'+id).style.display = "none";
}

function SubmitReviewEdit(sku, id, stars, title, text)
{
    if(stars==0) {$('reviewEditMsg').innerHTML = "Please choose a star rating."; return; }
    if(text=="") {$('reviewEditMsg').innerHTML = "Please enter your review."; return; }
    
    var url = 'process.aspx';
    var params = 'type=editwallreview&title='+escape(title)+'&text='+escape(text)+'&stars='+stars+'&sku='+sku+'&id='+id;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallReviewEditResult });
}

function wallReviewEditResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype=post&a=edit#post_"+response.pid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    { 
        $('reviewMsg').innerHTML = response.error;   
    }        
}
function SubmitQuestionEdit(sku, id, title, text)
{
    if(text=="") {$('reviewEditMsg').innerHTML = "Please enter your review."; return; }
    
    var url = 'process.aspx';
    var params = 'type=editwallquestion&title='+escape(title)+'&text='+escape(text)+'&sku='+sku+'&id='+id;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallQuestionEditResult });
}

function wallQuestionEditResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.pid+"&ptype=post&a=edit#post_"+response.pid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    { 
        $('questionMsg').innerHTML = response.error;   
    }        
}
function SubmitCommentEdit(sku, id, text)
{
    if(text=="") {$('editcommentMsg').innerHTML = "Please enter your comment."; return; }
    
    var url = 'process.aspx';
    var params = 'type=editwallcomment&text='+escape(text)+'&sku='+sku+'&id='+id;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallCommentEditResult });
}

function wallCommentEditResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var url = window.location.href;
        window.location = "/" + response.sku + ".html?success=true&pid="+response.cid+"&ptype=comment&a=edit#comment_"+response.cid;
        //if(url.indexOf("type=view") == -1){ window.location = url.substring(0, url.indexOf("?")) + "?success=true&pid="+response.pid+"&a=submit#post_"+response.pid; }
        //else{ window.location = url + "&success=true&pid="+response.pid+"&a=submit&#post_"+response.pid; }
    }
    else
    { 
        $('editcommentMsg').innerHTML = response.error;   
    }        
}
function toggleComments(id)
{
    if($("comments_"+id).style.display == "none")
    {
        $("comments_"+id).style.display = "block";
        $("hideCommentsIcon_"+id).src = $("hideCommentsIcon_"+id).src.replace("view", "hide");
    }
    else
    {
        $("comments_"+id).style.display = "none";
        $("hideCommentsIcon_"+id).src = $("hideCommentsIcon_"+id).src.replace("hide", "view");
    }
}

function SetRating(rating)
{
    stars = rating;
    rating_width = rating * 25;
	$('current-rating').style.width = rating_width+'px';
	SetRatingText(rating);
	return stars;
} 

function SetRatingText(n)
{
    switch(n)
    {
        case 0:
            $('ratingText').innerHTML=''; 
        break;
        case 1:
            $('ratingText').innerHTML='Just awful.';
        break;
        case 2:
            $('ratingText').innerHTML='Can\'t Recommend';
        break;
        case 3:
            $('ratingText').innerHTML='It\'s ok.';
        break;
        case 4:
            $('ratingText').innerHTML='Pretty good.';
        break;
        case 5:
            $('ratingText').innerHTML='Awesome!';
        break;
    }
}

function SetEditRating(type, id, rating)
{
    stars = rating;
    rating_width = rating * 25;
	$('current-rating-'+id).style.width = rating_width+'px';
	SetRatingText(id, rating);
	return stars;
} 

function SetEditRatingText(type, id, n)
{
    switch(n)
    {
        case 0:
            $(type+'starstext_'+id).innerHTML=''; 
        break;
        case 1:
            $(type+'starstext_'+id).innerHTML='Just awful.';
        break;
        case 2:
            $(type+'starstext_'+id).innerHTML='Can\'t Recommend';
        break;
        case 3:
            $(type+'starstext_'+id).innerHTML='It\'s ok.';
        break;
        case 4:
            $(type+'starstext_'+id).innerHTML='Pretty good.';
        break;
        case 5:
            $(type+'starstext_'+id).innerHTML='Awesome!';
        break;
    }
}

function RemoveRatingText()
{
    SetRatingText(stars)
}

function RemoveEditRatingText(type, id)
{
    SetEditRatingText(type, id, stars)
}

function OpenBioEdit()
{
    TagToTip('bioEdit', CLOSEBTN, true, STICKY, true, WIDTH, 400, DELAY, 0);
    $('WzBoDy').getElementsBySelector('[id="bioTxt"]').each(function(s){s.value=bio_text;});
    $('WzBoDy').getElementsBySelector('[id="locationTxt"]').each(function(s){s.value=location_text;});
}
function OpenTagEdit()
{
    TagToTip('tagEdit', CLOSEBTN, true, STICKY, true, WIDTH, 400, DELAY, 0);
    
    $A($('WzBoDy').getElementsByClassName("selectedWallTag")).each(function(s){s.className="";});
    var tags_array = tags_text.split(" // ");
    for(t=0; t<tags_array.length; t++)
    {
        $('WzBoDy').getElementsBySelector('[id="tags'+tags_array[t].split(' ').join('')+'"]').each(function(s){s.className="selectedWallTag";});
    }
}

function addTags(tag, link)
{
    if(tags_text.indexOf(tag) > -1)
    {
        tags_text = tags_text.replace("// " + tag, "");
        tags_text = tags_text.replace(tag, "");
        link.className = "";
    }
    else
    {
        if(tags_text!="") { tags_text = tags_text + " // "; }
        tags_text = tags_text + tag;
        link.className = "selectedWallTag";
    }
    
    var url = 'process.aspx';
    var params = 'type=updatewalltags&tags='+tags_text;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: addTagsResult });  
}

function addTagsResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        $("wallTags").innerHTML = tags_text; 
//        tt_HideInit();        
    }
    else
    {
        //what to do on error
    }        

}

function ShowPostType(imagePath,type)
{
    $("all_tab").src= imagePath + "/wall/all_tab.gif";
    $("review_tab").src= imagePath + "/wall/review_tab.gif";
    $("question_tab").src= imagePath + "/wall/question_tab.gif";
    $("image_tab").src= imagePath + "/wall/image_tab.gif";
    
    $(type+"_tab").src= imagePath + "/wall/" + type + "_tab2.gif";
    

    document.getElementsByClassName("userRemark").each(function(s) { s.style.display="block"; });
    document.getElementsByClassName("userActions").each(function(s) { s.style.display="block"; });

    if(type != "review" && type != "all")
    {
         document.getElementsByClassName("reviewDiv").each(function(s) { s.style.display="none"; }); 
         if($("writeReview") != null)
         {
            $("writeReview").style.display = "none";
         }
    }
    if(type != "question" && type != "all")
    { 
        document.getElementsByClassName("questionDiv").each(function(s) { s.style.display="none"; }); 
         if($("writeQuestion") != null)
         {
            $("writeQuestion").style.display = "none";
         }         
    }
    if(type!= "image" && type!= "all")
    {
        document.getElementsByClassName("imageDiv").each(function(s) { s.style.display="none"; }); 
        document.getElementsByClassName("videoDiv").each(function(s) { s.style.display="none"; }); 
         if($("uploadImageVideo") != null)
         {
            $("uploadImageVideo").style.display = "none";
         }           
    }
 }