/* document must also have the wz_tooltip and json javascript script in it */

function WallVote(logged_in, sku, pid, pos, type, wurl)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&wtype=product&url='+wurl+'&sku='+sku+'&pid='+pid+'&a=vote&pos='+pos+'&ptype='+type+'">login</a></u> to vote!', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
        _gaq.push(['_trackEvent', 'Wall', 'attemptedVoteOn'+type, sku]);
    }
    else
    {
        var url = 'process.aspx';
        var params = 'type=wallRating&url='+wurl+'&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.url + "?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 WallAjaxVote(logged_in, sku, pid, pos, type, wurl)
{
    if(!logged_in)
    {
        Tip('You must be logged in to vote.', CLICKCLOSE, true, STICKY, true, FADEOUT, 200);
        _gaq.push(['_trackEvent', 'Wall', 'attemptedVoteOn'+type, sku]);
    }
    else
    {
        var url = 'process.aspx';
        var params = 'type=wallRating&url='+wurl+'&sku='+sku+'&pid='+pid+'&pos='+pos+'&post_type='+type;
        
        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: wallAjaxVoteResult });
    }
}

function wallAjaxVoteResult(result){
    clearcursor();
    var response = result.responseText.parseJSON();
    if(response.success == "true")
    {
        var type = "likes";
        if(response.positive == "false")
        {
            type = "dis" + type;
        }
        var current = $(type + "_" + response.pid).innerHTML;
        $(type+ "_" + response.pid).innerHTML = parseInt(current) + 1;
    }
    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).each(function(s) { s.className = "deletedPost"; });
        $$("#postDiv_"+response.pid).each(function(s) { s.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 BanIp(ip,siteId,userId,bannedBy)
{
    var url = 'process.aspx';
    var params = 'type=banip&ip='+ip+'&site_id='+ siteId + '&user_id=' + userId + '&banned_by=' + bannedBy;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: banIpResult });
}

function banIpResult(result){
    clearcursor();
    if(result.responseText == "success")
    {
        alert("IP Banned");
    }
    else
    {
        alert("Error Banning IP"); 
    }         
}

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;
    }        
}
//used for submitting wall reviews that do not have pros, cons or recommended... calls the updated wallreview with "" for pros, cons, recommended
function WallReview(logged_in, sku, title, text, stars, wtype, wurl)
{
    WallReviewFull(logged_in, sku, title, "", "", text, "", stars, wtype, wurl);
}
function WallReviewFull(logged_in, sku, title, pros, cons, text, recommended, stars, wtype, wurl)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=review&wtype='+wtype+'&url='+escape(wurl)+'">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+'&wtype='+wtype+'&url='+escape(wurl)+'&pros='+pros+'&cons='+cons+'&recommended='+recommended;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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, wtype, wurl)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=video&wtype='+wtype+'&url='+escape(wurl)+'">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+'&wtype='+wtype+'&url='+escape(wurl);

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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, wtype, wurl)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&sku='+sku+'&a=question&wtype='+wtype+'&url='+escape(wurl)+'">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+'&wtype='+wtype+'&url='+escape(wurl);

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?success=true&pid="+response.pid+"&ptype=post&a=submit#post_"+response.pid;
    }
    else
    {
        $('questionMsg').innerHTML = response.error; 
    }        
}
//DEPRECATED - calls the appropriate function with "product" wall_type selected as default
function WallComment(logged_in, sku, pid, text, wurl) 
{
    WallCommentFull(logged_in, sku, pid, text, 'product', wurl);
}
function WallCommentFull(logged_in, sku, pid, text, wtype, wurl)
{
    if(!logged_in)
    {
        Tip('Hey, <u><a href="/controller.aspx?type=view&info=WallLogin&wtype='+wtype+'&url='+wurl+'&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+'&url='+wurl;

        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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).each(function(s) { s.style.display='block'; });
    $$("#"+type+'_flag_'+id).each(function(s) { s.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(); }
    if(isDefined("uploadType")) { uploadType = type; }
}

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, wurl)
{
    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&url='+wurl+'&title='+escape(title)+'&text='+escape(text)+'&stars='+stars+'&sku='+sku+'&id='+id;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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, wurl)
{
    if(text=="") {$('questionEditMsg_'+id).innerHTML = "Please enter your text."; return; }
    
    var url = 'process.aspx';
    var params = 'type=editwallquestion&title='+escape(title)+'&text='+escape(text)+'&sku='+sku+'&id='+id+"&url="+wurl;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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, wurl)
{
    if(text=="") {$('editcommentMsg').innerHTML = "Please enter your comment."; return; }
    
    var url = 'process.aspx';
    var params = 'type=editwallcomment&text='+escape(text)+'&sku='+sku+'&id='+id+'&url='+wurl;

    waitcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', 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.url + "?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+'+response.pid).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)
{
    eval("stars"+id+" = rating;");
    rating_width = rating * 25;
	$('current-rating-'+id).style.width = rating_width+'px';
	SetEditRatingText(id, rating);
	return rating;
} 

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, eval('stars' + id));
}

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 OpenInitialBioEdit()
{
    TagToTip('bioEdit', CLOSEBTN, true, STICKY, true, WIDTH, 400, DELAY, 0, FIX, ['location', -8, -160]);
    //$('WzBoDy').getElementsBySelector('[id="bioTxt"]').each(function(s){s.value="What drives you? What's your level of training? How did you get started? Let everyone know a little about yourself.";s.onFocus="this.value='';";});
    //$('WzBoDy').getElementsBySelector('[id="locationTxt"]').each(function(s){s.value="Tell the other community members where you practice.";s.onFocus="this.value='';";});
}

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: 'post', 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)
{
    if($("all_tab") != null) {$("all_tab").className= "dark"; }
    if($("review_tab") != null) { $("review_tab").className= "dark"; }
    if($("global_tab") != null) { $("global_tab").className= "dark"; }
    if($("question_tab") != null) { $("question_tab").className= "dark"; }
    if($("image_tab") != null) { $("image_tab").className= "dark"; }
    if($("comment_tab") != null) { $("comment_tab").className= "dark"; }
    if($("product_comment_tab") != null) { $("product_comment_tab").className= "dark"; }
    
    if($(type+"_tab") != null) { $(type+"_tab").className = "light"; }
    
    $$(".userActions").each(function(s) { s.style.display="table"; });
    $$(".userRemark").each(function(s) { s.style.display="table"; });

    if(type != "review" && type != "all")
    {
         $$(".reviewDiv").each(function(s) { s.style.display="none"; }); 
         if($("writeReview") != null)
         {
            $("writeReview").style.display = "none";
         }
    }
    if(type != "question" && type != "all")
    { 
        $$(".questionDiv").each(function(s) { s.style.display="none"; }); 
         if($("writeQuestion") != null)
         {
            $("writeQuestion").style.display = "none";
         }         
    }
    if(type!= "image" && type!= "all")
    {
        $$(".imageDiv").each(function(s) { s.style.display="none"; }); 
        $$(".videoDiv").each(function(s) { s.style.display="none"; }); 
         if($("uploadImageVideo") != null)
         {
            $("uploadImageVideo").style.display = "none";
         }           
    } 
    if(type!= "comment" && type!= "all")
    {
        $$(".commentDiv").each(function(s) { s.style.display="none"; }); 
    }        
    if(type!= "product_comment" && type!= "all")
    {
        $$(".product_commentDiv").each(function(s) { s.style.display="none"; }); 
    }            
    if(type != "global")
    {
         if($("globalWall")!=null){ $("globalWall").style.display="none"; }
    }else
    {
        if($("globalWall")!=null)
        { 
             $("globalWall").style.display="table";
             $$("#globalWall .userRemark").each(function(s) { s.style.display="table"; }); 
        }
    }
    _gaq.push(['_trackEvent', 'Wall', 'changedTab', type]);
 }
 

//set the buttons and text for anonymous vs logged in required posting
function SetAnonymousPosting(anonymousPosting,loggedIn)
{
    if(anonymousPosting)
    {
        $$('span[id $= LoginOptional]').each(function(element) {element.show();});
        $$('div[id $= WallLoginPanel]').each(function(element) {element.removeClassName("notLoggedIn");});
        $$('span[id $= PreviewOrLabel]').each(function(element) {element.show();});
        $('wallDisclaimer').hide();

        if(loggedIn)
        {
            $$('img[id $= PreviewPostButton]').each(function(element) {element.src = ImagePath + "/postComment.gif";});
        }
        else
        {
            $$('img[id $= PreviewPostButton]').each(function(element) {element.src = ImagePath + "/postAnon.gif";});
        }
    }
    else //no anonymous posting
    {
        $$('span[id $= LoginOptional]').each(function(element) {element.hide();});
        $('wallDisclaimer').show();
        if(loggedIn)
        {
            $$('div[id $= WallLoginPanel]').each(function(element) {element.removeClassName("notLoggedIn");});
            $$('img[id $= PreviewPostButton]').each(function(element) {element.src = ImagePath + "/postComment.gif";});
        }        
        else
        {
            $$('div[id $= WallLoginPanel]').each(function(element) {element.addClassName("notLoggedIn");});
            $$('img[id $= PreviewPostButton]').each(function(element) {element.src = ImagePath + "/spacer.gif";});
        }
        $$('span[id $= PreviewOrLabel]').each(function(element) {element.hide();});
    }
}

//calls process.aspx to login the facebook user 
// returns "error" or the user object
//YH 5/10
function LoginFacebookUser(accessToken,userId,returnFunction)
{
        var url = 'process.aspx';
        var params = 'type=fblogin&token=' + accessToken + '&uid=' + userId;
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });

}

//calls process.aspx to login the site user
// returns "error" or the user object
//YH 5/25/10
function LoginSiteUser(email,password,rememberMe,returnFunction)
{
        var url = 'process.aspx';
        var params = 'type=login_siteuser&email=' + email + '&password=' + password + '&remember_me=' + rememberMe;       
        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {               
                returnFunction(result.responseText);
            }        
        });
}

//calls process.aspx to logout the site user
//YH 2/1/11
function LogoutSiteUser(returnFunction)
{
        var url = 'process.aspx';
        var params = 'type=logout_siteuser';       
        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {               
                delete_cookie(".ASPXAUTH");
                delete_cookie("RecentOrderNumber");
                delete_cookie("userName");  
                delete_cookie("orderNumber");
                delete_cookie("displayName");
                delete_cookie("userId");
                returnFunction(result.responseText);
            }        
        });
}


//calls process.aspx to login the wall user 
// returns "error" or the display name of the user
//YH 5/25/10
function CreateWallUser(displayName,email,password,rememberMe,returnFunction)
{
        var url = 'process.aspx';
        var params = 'type=create_walluser&display_name=' + displayName + '&email=' + email + '&password=' + password + '&remember_me=' + rememberMe;       
        waitcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {               
                returnFunction(result.responseText);
            }        
        });
}


//YH 5/10
function CreateWallReview(text,pros,cons,stars,recommended,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
    var params = 'type=wallreview&title=&text='+escape(text)+'&stars='+stars+'&sku='+sku+'&wtype='+wtype+'&url='+wurl+'&pros='+escape(pros)+'&cons='+escape(cons)+'&recommended='+recommended+'&owns='+owns+'&allow_anon=true';
    clearcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//YH 5/25/10
function CreateWallQuestion(title,text,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
    var params = 'type=wallquestion&title='+escape(title)+'&text='+escape(text)+'&sku='+sku+'&wtype='+wtype+'&url='+wurl+'&owns='+owns+'&allow_anon=true';
    clearcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//this function creates a post of type comment, not to be confused with a wall_comment
//YH 5/25/10
function CreateWallCommentPost(text,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
    var params = 'type=wallcommentpost&text='+escape(text)+'&sku='+sku+'&wtype='+wtype+'&url='+wurl+'&owns='+owns+'&allow_anon=true';
    clearcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//this function creates a post of type prouct comment
//YH 4/4/11
function CreateWallProductComment(text,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
    var params = 'type=wallproductcomment&text='+escape(text)+'&sku='+sku+'&wtype='+wtype+'&url='+wurl+'&owns='+owns+'&allow_anon=false';
    clearcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//this function creates a post of type video
//RT 7/1/2010
function CreateWallVideo(title,text,video_url,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
        var params = 'type=wallvideo&title='+escape(title)+'&text='+escape(text)+'&video_url='+escape(video_url)+'&sku='+sku+'&wtype='+wtype+'&url='+escape(wurl);
        clearcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//this function creates a post of type video
//RT 7/1/2010
function CreateWallImage(title,text,img_path,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
        var params = 'type=wallimage&title='+escape(title)+'&text='+escape(text)+'&img_path='+escape(img_path)+'&sku='+sku+'&wtype='+wtype+'&url='+escape(wurl);
        clearcursor();
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}

//this function creates a wall_comment
//YH 5/26/10
function CreateWallComment(text,pid,owns,sku,wtype,wurl,returnFunction)
{
    var url = 'process.aspx';
    var params = 'type=wallcomment&pid=' + pid + '&text='+escape(text)+'&sku='+sku+'&wtype='+wtype+'&url='+wurl+'&owns='+owns+'&allow_anon=true';
    clearcursor();
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: 
            function(result)
            {
                returnFunction(result.responseText);
            }
        
        });
}


function openWallLogin(id,div,title)
{
    loginCallerId = id;
    ajaxwin=dhtmlwindow.open("ajaxbox", "div", div, title, "width=415px,height=250px,left=350px,top=100px,resize=1,scrolling=0")
    ajaxwin.isResize(false);
}

function SetEditRecommended(id,yesorno)
{
    //this function does not use the "checkthis" because checkthis does not take a parameter for the type
    if(yesorno == 'yes'){
        document.getElementById(id + 'No').className = 'no';
        $(id + 'Yes').className = 'yes active'
    }
    else{
        document.getElementById(id + 'Yes').className = 'yes';
        $(id + 'No').className = 'no active'
    }
    eval(id + 'Recommended' + '="' + yesorno + '"');
}

//switches the panel (comment, review or question) shown in the stand alone post section
// YH 5/12/10   
function ShowStandAlonePostPanel(panel)
{
    if(panel == "standAloneCommentPanel")
    {
        $("standAloneCommentPanel").show();
        $("standAloneQuestionPanel").hide();
        $("standAloneReviewPanel").hide();
        if($("standAloneUploadPanel") != null) {$("standAloneUploadPanel").hide(); };
        postType = "commentPost";
    }   
    else if(panel == "standAloneReviewPanel")
    {
        //if showing the question panel, put any non default text entered in the comment panel in the bottom line text panel YH 6/4/10
        var commentTextInput = $("standAloneComment");        
        if(commentTextInput.value != commentTextInput.attributes["defaultText"].value && commentTextInput.value != "")
        {
            $("standAloneBottomLine").value = commentTextInput.value;
        }        
    
        $("standAloneCommentPanel").hide();
        $("standAloneQuestionPanel").hide();
        $("standAloneReviewPanel").show();
        if($("standAloneUploadPanel") != null) {$("standAloneUploadPanel").hide(); };
        postType = "review";
    }
    else if(panel == "standAloneQuestionPanel")
    {
        //if showing the question panel, put any non default text entered in the comment panel in the question text panel YH 6/4/10
        var commentTextInput = $("standAloneComment");        
        if(commentTextInput.value != commentTextInput.attributes["defaultText"].value && commentTextInput.value != "")
        {
            $("standAloneQuestionText").value = commentTextInput.value;
        }
        $("standAloneCommentPanel").hide();
        $("standAloneQuestionPanel").show();
        $("standAloneReviewPanel").hide();
        if($("standAloneUploadPanel") != null) {$("standAloneUploadPanel").hide(); };
        postType = "question";
    }    
    else if(panel == "standAloneUploadPanel")
    {
        var commentTextInput = $("standAloneComment");        

        $("standAloneCommentPanel").hide();
        $("standAloneQuestionPanel").hide();
        $("standAloneReviewPanel").hide();
        if($("standAloneUploadPanel") != null) {$("standAloneUploadPanel").show(); };
        postType = "upload";
    }  
    SetPostButtons('standAlone',postType,loggedIn);
    if(ImagePath != '')
    {
        $("typeIcon").src = ImagePath + "/wall/" + postType + ".gif";
    }
}     

//displays the login box for facebook
//id is the id of the panel that called the function
// YH 5/12/10       
function FacebookLogin(id,permissions)
{               
    var result;
    FB.login(function(response) {
      if (response.authResponse) {
        LoginFacebookUser(response.authResponse.accessToken,response.authResponse.userID,
            function(response)
            {
                if(response != "error")
                {
                    var result = response.parseJSON();                                 
                    $$('div[id $= WallLoginPanel]').each(function(element) {element.hide();});
                    //$$('div[id $= PostToFacebookPanel]').each(function(element) {element.show();});
                    //$$('input[id $= PostToFacebook]').each(function(element) {element.checked = "checked";});
                    displayName = result.DisplayName;
                    loggedIn = true;
                    //if the preview is showing, update the names shows
                    if($(id + "PreviewPanel").visible())
                    {
                        //ShowPreview(id);
                        //now we actually do the post YH 6/7/10
                        $(id + "PreviewPostButton").onclick();
                    }
                    else
                    {
                        //show the welcome box
                        if($(id + "LoggedInDisplayName") != undefined)
                        {
                            $(id + "LoggedInDisplayName").update(displayName);
                        }
                        if($(id + "LoggedIn") != undefined)
                        {
                            $(id + "LoggedIn").show();
                        }
                        SetPostButtons(id,postType,true);
                    }
                }
            }                        
        );                        
      } 
      else 
      {
      }
    }, {scope:permissions});                
}  

//site login from login page... sends you on to specified page or account page
//id is the id of the panel that called the function
// RT 12/15/10       
function FacebookSiteLogin(permissions)
{               
    var result;
    FB.login(function(response) {
      if (response.authResponse) {
        LoginFacebookUser(response.authResponse.accessToken,response.authResponse.userID,
            function(response)
            {
                if(response != "error")
                {
                    var sendTo = getUrlParam(document.URL, "ReturnUrl");
                    if(sendTo != "")
                    {
                        window.location = unescape(sendTo);
                    }
                    else
                    {
                        window.location = "account.html";
                    }
                }
            }                        
        );                        
      } 
      else 
      {
      }
    }, {scope:permissions});                
}  

//shows the preview panel
//id is the id of the panel used
// YH 5/17/10
function ShowPreview(id,postType)
{
    //if the type is comment and the id is standAlone then we need to get the actual type from the radio button
    if(postType == 'comment' && id == 'standAlone')
    {
        postType = $('form1').getInputs('radio', 'standAlonePostType').find(function(radio) { return radio.checked; }).value;
    }
    if(ValidatePost(id,postType))
    {
        //hide the content panel
        $(id + "Post").getElementsBySelector("[id=" + id + "ContentPanel]").invoke("hide");
        $(id + "Post").getElementsBySelector("[id=" + id + "PreviewDate]")[0].update(new Date().format("shortDateTime"));
        if(postType == "review")
        {
            $("reviewPreview").getElementsBySelector("[id=stars]")[0].src = ImagePath + "/stars/red_" + starsstandAlone + ".png";
            if($(id + "Pros") != null) 
            {
                if($(id + "Pros").value == "")
                {
                    $("reviewProsPreview").hide();
                }
                else
                {
                    $("reviewProsPreview").show();
                }
                $("reviewPreview").getElementsBySelector("[id=pros]")[0].update($(id + "Pros").value);
            }
            if($(id + "Cons") != null)
            {
                if($(id + "Cons").value == "")
                {
                    $("reviewConsPreview").hide();
                }
                else
                {
                    $("reviewConsPreview").show();
                }  
                $("reviewPreview").getElementsBySelector("[id=cons]")[0].update($(id + "Cons").value);
            }   
            if($("reviewPreview").getElementsBySelector("[id=recommended]").length > 0) 
            {
                $("reviewPreview").getElementsBySelector("[id=recommended]")[0].update(standAloneRecommended);
            }
            $("reviewPreview").getElementsBySelector("[id=bottomLine]")[0].update($(id + "BottomLine").value);
            $("commentPreview").hide();
            $("reviewPreview").show();
            $("questionPreview").hide();
        }
        else if(postType == "question")
        {
            //if the question title has not been changed from the default text, then we just show nothing for the title
            var questionTitleInput = $(id + "QuestionTitle");
            var questionTitleText = "";
            if(questionTitleInput.value != questionTitleInput.attributes["defaultText"].value)
            {
                questionTitleText = questionTitleInput.value;
            }
            $("questionPreview").getElementsBySelector("[id=questionTitle]")[0].update(questionTitleText);
            $("questionPreview").getElementsBySelector("[id=questionText]")[0].update($(id + "QuestionText").value);
            $("commentPreview").hide();
            $("reviewPreview").hide();
            $("questionPreview").show();

        }
        else if(postType == "commentPost" || postType == "product_comment")
        {
            $("commentPreview").getElementsBySelector("[id=commentText]")[0].update($(id + "Comment").value);
            $("commentPreview").show();
            $("reviewPreview").hide();
            $("questionPreview").hide();
        }  
        else if(postType == "comment")
        {
            $(id + "CommentPreview").getElementsBySelector("[id=" + id + "CommentText]")[0].update($("commentText_" + id).value);
            $(id + "CommentPreview").show();
        }  
        else if(postType == "upload")
        {            
            if (uploadType=="photo")
            {
                // upload the photo from the iframe
                window.iUploadImage.__doPostBack("ctl03_File1", "");                
                // when iframe reloads, it will call the function to show the preview or the error
            
            }
            else if (uploadType=="video")
            {
                var video_id = getUrlParam($(id + "VideoPath").value, "v");
                var video_embed = '<object width="320" height="265"><param name="wmode" value="transparent" /><param name="movie" value="http://www.youtube-nocookie.com/v/'+video_id+'&hl=en_US&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/'+video_id+'&hl=en_US&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265" wmode="transparent"></embed></object><br /><br />';
                var video_title = $(id + "VideoTitle").value;
                var video_description = $(id + "VideoDescription").value;                

                if(video_description != $(id + "VideoDescription").attributes["defaultText"].value)
                {
                    $("videoPreview").getElementsBySelector("[id=videoDescription]")[0].update(video_description);
                }
                if(video_title != $(id + "VideoTitle").attributes["defaultText"].value)
                {
                    $("videoPreview").getElementsBySelector("[id=videoTitle]")[0].update(video_title);   
                }             
                $("videoPreview").getElementsBySelector("[id=videoEmbed]")[0].update(video_embed);
                $("videoPreview").show();
            }
            
            $("commentPreview").hide();
            $("reviewPreview").hide();
            $("questionPreview").hide();            
        }              
                 
        if($(id + "OwnThis").checked)
        {
            $(id + "OwnsThisName").show();
            $(id + "OwnsThisText").update("owns this product");
        }
        else
        {        
           $(id + "OwnsThisName").hide();
           $(id + "OwnsThisText").update("");
        }
        $(id + "OwnsThisName").update(displayName);
        $(id + "PreviewName").update(displayName);                
        $(id + "PreviewPanel").show();
        $(id + "WallLoginPanel").removeClassName("login");
        $(id + "WallLoginPanel").addClassName("loginPreview");
        $(id + "WallLoginPanel").childElements()[2].update(lc_LoginAndPost);
        $(id + "WallLoginPanel").childElements()[3].childElements()[0].update(lc_LoginWithFacebookAndPost);
    } 
 } 

// function to show image preview
// RT 7/9/2010 
function ShowImagePreview(id, imgPath, imgError)
{
    if(imgError == "") 
    {
        var imageTitleInput = $(id + "ImageTitle");
        var imageTextInput = $(id + "ImageText");
        if(imageTitleInput.value != imageTitleInput.attributes["defaultText"].value)
        {
            $("imagePreview").getElementsBySelector("[id=ImageTitle]")[0].update($(id + "ImageTitle").value);
        }
        if(imageTextInput.value != imageTextInput.attributes["defaultText"].value)
        {
            $("imagePreview").getElementsBySelector("[id=ImageText]")[0].update($(id + "ImageText").value);
        }
        $("imagePhoto").src = imgPath;
        $(id+"ImagePath").value = imgPath.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/)[2]; //filename without path
        $("imagePreview").show();
     }
     else
     {
        // show error
     }
} 
 
// hides the preview panel and shows the editable fields
// YH 5/17/10 
function EditPost(id)
{
    //if the type is comment and the id is standAlone then we need to get the actual type from the radio button
    if(postType == 'comment' && id == 'standAlone')
    {
        postType = $('form1').getInputs('radio', 'standAlonePostType').find(function(radio) { return radio.checked; }).value;
    }

   $(id + "PreviewPanel").hide();   
   $(id + "Post").getElementsBySelector("[id=" + id + "ContentPanel]").invoke("show");
   $(id + "WallLoginPanel").removeClassName("loginPreview");
   $(id + "WallLoginPanel").addClassName("login");  
   $(id + "WallLoginPanel").childElements()[2].update("Login");
   $(id + "WallLoginPanel").childElements()[3].childElements()[0].update(lc_LoginWithFacebook);
}     

//sets the name and display of the post buttons
// YH 5/17/10
function SetPostButtons(id,postType,loggedIn)
{
    if(postType == "review")
    {
        $(id + 'PostButton').src = ImagePath + "/postReview.gif";
    }
    else if(postType == "commentPost")
    {
        $(id + 'PostButton').src = ImagePath + "/postComment.gif";
    }
    else if(postType == "question")
    {
        $(id + 'PostButton').src = ImagePath + "/postQuestion.gif";
    }
    else if(postType == "upload")
    {
        $(id + 'PostButton').src = ImagePath + "/uploadNow.gif";
    }

    if(loggedIn)
    {
        $$('img[id $= PreviewPostButton]').each(function(element) {element.src = ImagePath + "/postComment.gif";});
        //$$('img[id $= PostButton]').each(function(element) {element.show();}); //this shows all the post button not on the preview panel
        $$('img[id $= PreviewPostButton]').each(function(element) {element.show();});
        $$('span[id $= PreviewOrLabel]').each(function(element) {element.hide();});
    }
    else
    {        
        //$$('img[id $= PostButton]').each(function(element) {element.hide();}); //this hides all the post button not on the preview panel
        $(id + 'PostButton').hide();
        $(id + 'PreviewPostButton').src = ImagePath + "/postAnon.gif";
        $(id + 'PreviewPostButton').show(); //make sure that the post button on the preview panel is always shown
        $$('span[id $= PreviewOrLabel]').each(function(element) {element.show();}); 
    }
}   

// validates the post depending on the post type
// YH 5/17/10
function ValidatePost(id,postType)
{
    var validated = true;
    if(postType == "review")
    {
        //call the validation function on the approrpiate panel
        validated = ValidateFields(id + "ReviewPanel");
        // stars + id has to be manually checked
        if(eval("stars" + id) == 0)
        {
            $(id + "ReviewStars").addClassName("validationFailed");
            validated = false;
        }                   
        else
        {
            $(id + "ReviewStars").removeClassName("validationFailed");
        }                    
    }
    else if(postType == "commentPost" || postType == "comment")
    {
        validated = ValidateFields(id + "CommentPanel");
    }
    else if(postType == "question")
    {
        validated = ValidateFields(id + "QuestionPanel");
    }  
    else if(postType == "upload")
    {
        if(uploadType=="video") 
        {
            validated = ValidateFields(id + "VideoForm");
        } 
        else if(uploadType=="photo") 
        {
            validated = ValidateFields(id + "PhotoForm");
            if(validated && $(id + "PreviewPanel").style.display=="none") { //if the rest validates AND we are not in preview mode
                validated = ValidateFields(window.iUploadImage.document.getElementById("imageUpload"));
            }
        }
    }                  
    return validated;                
}  

//validates the fields in the panel sent in. it looks for a field with the "required" class and then uses defaultText to see if the field has been completed
// YH 5/17/10
function ValidateFields(panel)
{
    if(typeof(panel) != 'object')   // if this is a name of an object and not the object itself
        panelToCheck = $(panel);    // RT 7/13/2010
    else
        panelToCheck = panel;

    var validated = true;
    
    try {
        panelToCheck.getElementsBySelector(".required").each(
           function(element){
                var defaultText = element.attributes["defaultText"] || "";
                if(element.value.trim() == defaultText.value || element.value.trim() == "")
                {
                    element.addClassName("validationFailed");
                    validated = false;
                }
                else
                {
                    element.removeClassName("validationFailed");
                }
           });
        panelToCheck.getElementsBySelector(".validate-password").each(
           function(element){
                //allow any character other than ' " and whitespace between 6 and 15 characters
                var pattern = /^[^'"\s]{6,15}$/;
                if(!pattern.test(element.value.trim()))
                {
                    element.addClassName("validationFailed");
                    validated = false;
                }
                else
                {
                    element.removeClassName("validationFailed");
                }
           }); 
        panelToCheck.getElementsBySelector(".validate-email").each(
           function(element){
                var pattern =/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
                if(!pattern.test(element.value.trim()))
                {
                    element.addClassName("validationFailed");
                    validated = false;
                }
                else
                {
                    element.removeClassName("validationFailed");
                }
           });  
       } catch (exc) {}                                    
       return validated;            
} 

//creates the wall post - refreshes the page on success
// button is the calling button
// id is the id of the element (standAlone or a post_id)
// identifier is the sku or question_id
function SubmitWallPost(button,id,type,identifier,wallType,returnUrl,postToFacebook)
{
    //if the type is comment and the id is standAlone then we need to get the actual type from the radio button
    if(type == 'comment' && id == 'standAlone')
    {
        type = $('form1').getInputs('radio', 'standAlonePostType').find(function(radio) { return radio.checked; }).value;
    }
    if(ValidatePost(id,type))
    {
        waitcursor();
        button.hide();
        $$('img[id $= PreviewButton]').each(function(element) {element.hide();});
        $$('img[id $= PreviewEditButton]').each(function(element) {element.hide();});
        button.next().show();        
        var postId = "";
        if(type == "review")
        {
            // only set the pros, cons & recommended fields if they exist in the form - RT, 6/30/2010
            var pros = ($(id + "Pros") == null ? "" : $(id + "Pros").value);
            var cons = ($(id + "Cons") == null ? "" : $(id + "Cons").value);
            var recommended = (eval(id + "Recommended") == null ? "" : eval(id + "Recommended"));
            CreateWallReview($(id + "BottomLine").value,pros,cons,eval("stars" + id),eval(id + "Recommended"),$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                function(response)
                {
                    var result = response.parseJSON(); 
                    if(result.success == "true")
                    {
                        postId = result.pid;
                        WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                    }
                    else
                    {
                        WallPostFailure(button,postId);
                    }
                }                        
            );
        }
        else if(type == "question")
        {
            CreateWallQuestion($(id + "QuestionTitle").value,$(id + "QuestionText").value,$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                function(response)
                {
                    var result = response.parseJSON(); 
                    if(result.success == "true")
                    {
                        postId = result.pid;
                        WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                    }
                    else
                    {
                        WallPostFailure(button,postId);
                    }                    
                }                        
            );                        
        } 
        else if(type == "commentPost")
        {
            CreateWallCommentPost($(id + "Comment").value,$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                function(response)
                {
                    var result = response.parseJSON(); 
                    if(result.success == "true")
                    {
                        postId = result.pid;
                        WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                    }
                    else
                    {
                        WallPostFailure(button,postId);
                    }                    
                }                        
            );                        
        } 
        else if(type == "product_comment")
        {
            CreateWallProductComment($(id + "Comment").value,$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                function(response)
                {
                    var result = response.parseJSON(); 
                    if(result.success == "true")
                    {
                        postId = result.pid;
                        WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                    }
                    else
                    {
                        WallPostFailure(button,postId);
                    }                    
                }                        
            );                        
        }         
        else if(type == "upload")
        {
            if(uploadType == "video")
            {
                var video_title = $(id + "VideoTitle").value;
                var video_description = $(id + "VideoDescription").value;                

                if(video_description == $(id + "VideoDescription").attributes["defaultText"].value)
                {
                    video_description = "";
                }
                if(video_title == $(id + "VideoTitle").attributes["defaultText"].value)
                {
                    video_title = "";   
                } 
                
                CreateWallVideo(video_title,video_description,$F(id + "VideoPath"),$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                    function(response)
                    {
                        var result = response.parseJSON(); 
                        if(result.success == "true")
                        {
                            postId = result.pid;
                            WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                        }
                        else
                        {
                            WallPostFailure(button,postId);
                        }                    
                    }                        
                );  
            }
            else if(uploadType == "photo")
            {
                var img_title = $F(id + "ImageTitle");
                var img_text = $F(id + "ImageText");                

                if(img_text == $(id + "ImageText").attributes["defaultText"].value)
                {
                    img_text = "";
                }
                if(img_title == $(id + "ImageTitle").attributes["defaultText"].value)
                {
                    img_title = "";   
                } 
                CreateWallImage(img_title,img_text,$(id + "ImagePath").value,$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                    function(response)
                    {
                        var result = response.parseJSON(); 
                        if(result.success == "true")
                        {
                            postId = result.pid;
                            WallPostSuccess(button,id,postId,type,returnUrl,"post",postToFacebook);
                        }
                        else
                        {
                            WallPostFailure(button,postId);
                        }                    
                    }                        
                );  
            }
                              
        } 
        else if(type == "comment")
        {
            CreateWallComment($("commentText_" + id).value,id,$(id + "OwnThis").checked,identifier,wallType,returnUrl,
                function(response)
                {
                    var result = response.parseJSON(); 
                    if(result.success == "true")
                    {
                        postId = result.cid;
                        WallPostSuccess(button,id,postId,type,returnUrl,"comment",postToFacebook);                   
                    }
                    else
                    {
                        WallPostFailure(button,postId);
                    }                    
                }                        
            );                        
        }                                                    
    }
}  

//called if a wall post is successfully posted
// postCategory is eiter post or comment - this is needed for the anchor (#post_id or #comment_id
// id is standAlone or the post id if it's coming from a specific post
// postId is the id of the new post
// postType is review, question, comment of commentPost
// YH 5/26/10
function WallPostSuccess(button,id,postId,postType,returnUrl,postCategory,postToFacebook)
{   
    //this uses jump, and not and anchor (#) because the anchor does not work properly when reloading
    var linkToPost = window.location.protocol + "//" + window.location.hostname + "/" + returnUrl + "?post_type=" + postType + "&jump=" + postCategory + "_" + postId;
    if(postToFacebook)
    {
        PostToFacebook(id,postType,linkToPost);
    }       
    else
    {
        window.location.replace(linkToPost);
    }
    clearcursor();    
}

function WallPostFailure(button,id)
{
    clearcursor();
    button.next().hide();
    button.show();    
}

// Post a message to the users facebook page
// ObjectName, ObjectImagePath must be set elsewhere for this function to work
//YH 5/28/10
function PostToFacebook(id,postType,linkToPost)
{
    var message,picture,name,link,caption,description;
    picture = ObjectImagePath;
    link = linkToPost;
    caption = ObjectName;
    if(postType == "review")
    {    
        message = "Just posted a review of the " + ObjectName + " and rated it " + eval("stars" + id) + " stars." ;
        name = "";
        description = $(id + "BottomLine").value;
    }
    else if(postType == "question")
    {
        message = "Just asked a question about the " + ObjectName + "." ;
        name = $(id + "QuestionTitle").value;
        description = $(id + "QuestionText").value;                    
    } 
    else if(postType == "commentPost")
    {
        message = "Just posted a comment on the " + ObjectName + "." ;
        name = "";
        description = $(id + "Comment").value;                                          
    } 
    else if(postType == "comment")
    {        
        message = "Just commented on a post about the " + ObjectName + "." ;
        name = "";
        description = $("commentText_" + id).value;                                                               
    }
    else if(uploadType == "photo")
    {        
        message = "Just uploaded a photo of the " + ObjectName + "." ;
        name = "";
        description = $(id + "ImageText").value;                                                               
    }
    else if(uploadType == "video")
    {        
        message = "Just uploaded a video about the " + ObjectName + "." ;
        name = "";
        description = $(id + "VideoDescription").value;                                                               
    }
    
    var url = 'process.aspx';
    var params = 'type=posttofacebook&title=&message='+escape(message)+'&name='+escape(name)+'&description='+escape(description)+'&picture='+escape(picture)+'&link='+escape(link)+'&caption='+escape(caption);
    var myAjax = new Ajax.Request( url, { method: 'post', parameters: params, onComplete: window.location.replace(linkToPost)});       
}

function showSignUp() { $('loginPanel').hide(); $('signupPanel').show(); }
function showLogin() { $('signupPanel').hide(); $('loginPanel').show(); }

//id is the id of the panel containing the login
//panel is the panel to be validated
function WallLogin(id,panel,email,password,rememberMe)
{
    waitcursor(); 
    if(ValidateFields(panel))
    {
        LoginSiteUser(email,password,rememberMe,
            function(response)
            {
                if(response != "error")
                {
                    $("popupLoginError").hide();
                    var result = response.parseJSON();  
                    displayName = result.DisplayName;
                    loggedIn = true;
                    //hide all the login panels
                    $$('div[id $= WallLoginPanel]').each(function(element) {element.hide();});
                    //if the preview panel is null, then the login popup was not called from a comment area 
                    if($(id + "PreviewPanel") != null){
                        //if the preview is showing, update the names shows
                        if($(id + "PreviewPanel").visible())
                        {
                            //ShowPreview(id);
                            //now we actually do post YH 6/7/10
                            $(id + "PreviewPostButton").onclick();
                        }
                        else
                        {
                            //show the welcome box
                            if($(id + "LoggedInDisplayName") != undefined)
                            {
                                $(id + "LoggedInDisplayName").update(displayName);
                            }
                            if($(id + "LoggedIn") != undefined)
                            {
                                $(id + "LoggedIn").show();
                            }
                            SetPostButtons(id,postType,true);
                        }
                    }
                    ajaxwin.close()
                }
                else
                {
                    $("popupLoginError").show();
                }
            }                        
        );
    }
    clearcursor(); 
}

//id is the id of the panel containing the login
//panel is the panel to be validated
function WallLogout(id)
{
    waitcursor(); 
    LogoutSiteUser(
            function(response)
            {
                if(response != "error")
                {
                    loggedIn = false;
                    //hide all the logged in panels
                    $$('div[id $= LoggedIn]').each(function(element) {element.hide();});
                    //show the log in panels
                    $$('div[id $= WallLoginPanel]').each(function(element) {element.show();});                    
                    displayName = anonymousDisplayName;
                    SetPostButtons(id,postType,false);
                    //make sure that the preview is not being shown
                    EditPost(id);
                }
           }                        
        );
    clearcursor(); 
}


//id is the id of the panel containing the login
//panel is the panel to be validated
function WallCreateAccount(id,panel,signupDisplayName,email,password,rememberMe)
{
    waitcursor(); 
    if(ValidateFields(panel))
    {
        CreateWallUser(signupDisplayName,email,password,rememberMe,
            function(response)
            {
                if(response != "error")
                {
                    $("popupSignupError").hide();
                    var result = response.parseJSON();  
                    displayName = result.DisplayName;
                    loggedIn = true;
                   //hide all the login panels
                    $$('div[id $= WallLoginPanel]').each(function(element) {element.hide();});
                    //if the preview is showing, update the names shows
                    if($(id + "PreviewPanel").visible())
                    {
                        //ShowPreview(id);
                        //now we actually do the post YH 6/7/10
                        $(id + "PreviewPostButton").onclick();
                    }
                    else
                    {
                        $(id + "LoggedInDisplayName").update(displayName);
                        $(id + "LoggedIn").show();
                        SetPostButtons(id,postType,true);
                    }
                    ajaxwin.close()
                }
                else
                {
                    $("popupSignupError").show();
                }
            }                        
        );
    }
    clearcursor(); 
}               

