function getId( idString )
{
        return document.getElementById( idString );
}

function isEmpty( elementId )
{
        if( getId(elementId).value.length > 0 )
        {
                return false;
        }
        return true;
}

function ajax_add_photocomment( photo_id )
{
        var comment, rating, nickname;

        //Get COMMENT
        comment = document.forms['addphotocomment'].elements['comment'].value;

        //Get RATING
        for( i=0; i<document.forms['addphotocomment'].rating.length; i++ )
        {
                if(document.forms['addphotocomment'].rating[i].checked)
                {
                        rating = document.forms['addphotocomment'].rating[i].value;
                }
        }

        //Get NICKNAME
        nickname = document.forms['addphotocomment'].elements['nickname'].value;

        if( nickname.length > 0 && comment.length > 0 )
        {
                sajax_do_call('ajax_add_photocomment',[photo_id,comment,rating,nickname,ajax_add_photocomment_callback]);
        }
        else
        {
                alert( 'Please fill out the comment form fully.' );
        }
        return false;
}

/*
0=success
1=comment
2=nickname
3=rating
*/
function ajax_add_photocomment_callback( res )
{
        if( res[0] )
        {
                document.forms['addphotocomment'].elements['comment'].value  = '';
                document.forms['addphotocomment'].elements['nickname'].value = '';
                for( i=0; i<document.forms['addphotocomment'].rating.length; i++ ){ document.forms['addphotocomment'].rating[i].checked = false; }

                var e_comment = document.createElement( 'div' );
                e_comment.className = 'experience comment_just_added';

                var e_span = document.createElement( 'span' );
                e_span.className = 'commentname';
                e_span.appendChild( document.createTextNode( res[2]+' says : ' ) );

                var e_comment_text = document.createTextNode( res[1] );

                var e_rating_wrap = document.createElement( 'div' );
                e_rating_wrap.className = 'outoften';
                var e_rating = document.createElement( 'span' );
                e_rating.className = 'rating t_rating' + res[3];
                e_rating.appendChild( document.createTextNode( res[3] + '/10') );
                e_rating_wrap.appendChild( e_rating );


                e_comment.appendChild( e_span );
                e_comment.appendChild( e_comment_text );
                e_comment.appendChild( e_rating_wrap );
                document.getElementById( 'photo_experiences' ).appendChild( e_comment );
        }
        else
        {
                alert( 'Your comment could not be added due to a database error.\nPlease try later.' );
        }
}


function ajax_add_comment( site_id, product_id )
{
        var type, comment, rating, nickname;

        //Get TYPE
        for( i=0; i<document.forms['addcomment'].ctype.length; i++ )
        {
                if(document.forms['addcomment'].ctype[i].checked)
                {
                        type = document.forms['addcomment'].ctype[i].value;
                }
        }

        //Get COMMENT
        comment = document.forms['addcomment'].elements['comment'].value;

        //Get RATING
        for( i=0; i<document.forms['addcomment'].rating.length; i++ )
        {
                if(document.forms['addcomment'].rating[i].checked)
                {
                        rating = document.forms['addcomment'].rating[i].value;
                }
        }

        //Get NICKNAME
        nickname = document.forms['addcomment'].elements['nickname'].value;

        if( nickname.length > 0 && comment.length > 0 )
        {
                sajax_do_call('ajax_add_comment',[site_id,product_id,type,comment,rating,nickname,ajax_add_comment_callback]);
        }
        else
        {
                alert( 'Please fill out the comment form fully.' );
        }
        return false;
}

/*
  res[0] = (0/1) which is success true/false
  res[1] = (1/2) type
  res[2] = comment
  res[3] = nickname
  res[4] = rating
  res[5] = Site absolute URL
*/
function ajax_add_comment_callback( res )
{
        if( res[0] )
        {
                document.forms['addcomment'].elements['comment'].value  = '';
                document.forms['addcomment'].elements['nickname'].value = '';
                for( i=0; i<document.forms['addcomment'].rating.length; i++ ){ document.forms['addcomment'].rating[i].checked = false; }
                document.forms['addcomment'].ctype[0].checked = true;
                document.forms['addcomment'].ctype[1].checked = false;

                var action;
                if( res[1] == 2 )
                { action = 'asks'; }
                else{ action = 'says'; }

                var e_comment = document.createElement( 'div' );
                e_comment.className = 'experience comment_just_added';

                var e_span = document.createElement( 'span' );
                e_span.className = 'commentname';
                if( res[1] == 2 )
                {
                        var img_star = document.createElement( 'img' );
                        img_star.src = res[5] + '/star.gif';
                        e_span.appendChild( img_star );
                }
                e_span.appendChild( document.createTextNode( res[3]+' '+action+' : ' ) );

                var e_comment_text = document.createTextNode( res[2] );

                if( res[1] != 2 )
                {
                        var e_rating_wrap = document.createElement( 'div' );
                        e_rating_wrap.className = 'outoften';
                        var e_rating = document.createElement( 'span' );
                        e_rating.className = 'rating t_rating' + res[4];
                        e_rating.appendChild( document.createTextNode( res[4] + '/10') );
                        e_rating_wrap.appendChild( e_rating );
                }

                e_comment.appendChild( e_span );
                e_comment.appendChild( e_comment_text );

                if( res[1] != 2 )
                {
                        e_comment.appendChild( e_rating_wrap );
                }

                document.getElementById( 'product_experiences' ).appendChild( e_comment );
        }
        else
        {
                alert( 'Your comment could not be added due to a database error.\nPlease try later.' );
        }
}
