function item_loading(){
    document.getElementById('progress').style.display='inline';
}
function item_complete(){
    document.getElementById('progress').style.display='none';    
}
function item_loading2(){
    document.getElementById('progress2').style.display='inline';
}
function item_complete2(){
    document.getElementById('progress2').style.display='none';    
}

function vote(product_id,v,vote_v){
    var query_string = 'action=cast_vote';
    query_string += '&product_id='+product_id;
    query_string += '&vote='+v;
    query_string += '&vote_v='+vote_v;
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading()},onComplete:function(request){item_complete()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});        
    
    var score = parseInt(document.getElementById('score-'+product_id).innerHTML);
    if(v==1){
        score = score+1;
        document.getElementById('up-votes-'+product_id).innerHTML=1+parseInt(document.getElementById('up-votes-'+product_id).innerHTML);
    }else{
        score = score-1;
        document.getElementById('down-votes-'+product_id).innerHTML=1+parseInt(document.getElementById('down-votes-'+product_id).innerHTML);
    }
    if(score > 0) score = '+'+score;
    document.getElementById('score-'+product_id).innerHTML=score;
    
    document.getElementById('vote-image-up-'+product_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />&nbsp;&nbsp;';
    document.getElementById('vote-image-down-'+product_id).innerHTML='<img src="/images/thumb_down_dis.gif" width="14" heigth="14" />';
    if(document.getElementById('vote-image-up-2-'+product_id))
        document.getElementById('vote-image-up-2-'+product_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />';
    if(document.getElementById('vote-image-down-2-'+product_id))
        document.getElementById('vote-image-down-2-'+product_id).innerHTML='<img src="/images/thumb_down_dis.gif" width="14" heigth="14" />';
    
    
    document.getElementById('vote-request').innerHTML='Your vote has been added.';
    
    if(trim(document.getElementById('comment-text-form').value)){
        document.getElementById('comment-submit-button').disabled=false;
    }
    document.getElementById('comment-text-form').setAttribute('onkeyup',"enableSaveButton('comment-text-form','comment-submit-button')");
    return false;    
}

function incrementTotalCommentVotes(comm_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('number-of-total-votes-'+rep+comm_id).innerHTML=parseInt(document.getElementById('number-of-total-votes-'+rep+comm_id).innerHTML)+1;
}
function incrementUpCommentVotes(comm_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('number-of-up-votes-'+rep+comm_id).innerHTML=parseInt(document.getElementById('number-of-up-votes-'+rep+comm_id).innerHTML)+1;    
}
function decrementUpCommentVotes(comm_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('number-of-up-votes-'+rep+comm_id).innerHTML=parseInt(document.getElementById('number-of-up-votes-'+rep+comm_id).innerHTML)-1;    
}


function saveComment(vote_id, comment, prop_id){
    document.getElementById('ajax-response').innerHTML='';
    document.getElementById('comment-form').innerHTML='';
    var query_string = 'action=save_comment';
    query_string += '&vote_id='+vote_id;
    query_string += '&comment='+comment;
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading2()},onComplete:function(request){item_complete2()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});        

    if(document.getElementById('comments-number-'+prop_id)){
        var current_comments_number = parseInt(document.getElementById('comments-number-'+prop_id).innerHTML);
    }else{
        var current_comments_number = 0;
    }
    
    if(current_comments_number==0){
        document.getElementById('comments-link-'+prop_id).innerHTML = '<a href="#comments">1 Comment</a>';
    }else{
        current_comments_number++;
        document.getElementById('comments-link-'+prop_id).innerHTML = '<a href="#comments">'+current_comments_number+' Comments</a>';
    }    
    return false;    
}

function skipComment(comm_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('ajax-response-'+rep+comm_id).innerHTML='';
}

function saveCommentComment(comment_id, vote_id,comment,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('ajax-response-'+rep+comment_id).innerHTML='';
    var query_string = 'action=save_comment_comment';
    query_string += '&vote_id='+vote_id;
    query_string += '&comment_id='+comment_id;
    query_string += '&comment='+comment;
    query_string += '&type='+type;
    new Ajax.Updater('ajax-response-'+rep+comment_id,'/ajax.php',{onLoading:function(request){item_loading2()},onComplete:function(request){item_complete2()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});        
    return false;    
}


function previewComment(author,comment){
        
    comment = comment.replace(/\n/g,'<br />'); 
    comment = comment.replace(/(<([^>]+)>)/ig,"");    
    
    document.getElementById('productComments').innerHTML=document.getElementById('productComments').innerHTML + '<a name="comment-preview"></a><div class="rating_author"><div class="right_votes">Comment rating: <strong>+1</strong></div><strong>'+author+'</strong></div><div class="rating_body">'+comment+'</div><div class="vote">your comment has been added</div>';

    location.href='#comment-preview';
}                                                                                                                                                                                                                             


function updateVoteArea(product_id, text){
    document.getElementById('vote-area-'+product_id).innerHTML=text;
}


function updateVoteAreaComment(comm_id,text,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('vote-area-'+rep+comm_id).innerHTML=text;
}

function vote_form2(frm){
    if(document.getElementById('vote-id'))
        document.getElementById('vote-id-form').value=parseInt(document.getElementById('vote-id').innerHTML);
        

    var comment = document.getElementById('comment-text-form').value;
    comment = comment.replace(/\n/g,'<br />');
    comment = comment.replace(/(<([^>]+)>)/ig,"");           
    document.getElementById('productComments').innerHTML = document.getElementById('productComments').innerHTML + '<div class="rating_author"><div class="right_votes">Comment rating: +1</div><strong>'+document.getElementById('auhtor-name').value+'</strong></div><div class="rating_body">'+comment+'</div><div class="vote">your comment has been added</div>';
    
    saveComment(document.getElementById('vote-id-form').value, document.getElementById('comment-text-form').value, frm.prop_id_form.value);
    
    return false;
}

function saveReply(vote_id, comment){
    document.getElementById('ajax-response').innerHTML='';
    var query_string = 'action=save_reply';
    query_string += '&vote_id='+vote_id;
    query_string += '&comment='+comment;
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading()},onComplete:function(request){item_complete()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});        
    
    document.getElementById('number-of-comments').innerHTML=parseInt(document.getElementById('number-of-comments').innerHTML)+1;
    
    return false;    
}

var opened_comment_box = '';
function vote_comment(comment_id,v,type, is_l){
    
    if(opened_comment_box != ''){
        document.getElementById(opened_comment_box).innerHTML='';
        opened_comment_box = '';
    }
    
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    
    if(!is_l){      
        popMembershipBox(); return;                       
    }
    
    var query_string = 'action=cast_vote_comment';
    query_string += '&comment_id='+comment_id;
    query_string += '&vote='+v;
    query_string += '&type='+type;

    new Ajax.Updater('ajax-response-'+rep+comment_id,'/ajax.php',{onLoading:function(request){item_loading2()},onComplete:function(request){item_complete2()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});        
    
    //document.getElementById('vote-image-comment-up-'+rep+comment_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />';
    //document.getElementById('vote-image-comment-down-'+rep+comment_id).innerHTML='<img src="/images/thumb_down_dis.gif" width="14" heigth="14" />';
 
    opened_comment_box='ajax-response-'+rep+comment_id;
       
    document.getElementById('vote-bar-c-'+rep+comment_id).innerHTML='';
    if(v==1){
        var ud = 'up';
    }else{
        var ud = 'down';
    }
    
    document.getElementById('vote-bar-c-'+rep+comment_id).innerHTML += 'You voted&nbsp;<img id="voted-how-'+rep+comment_id+'" src="/images/thumb_'+ud+'.gif" width="14" height="14" />&nbsp;';
    document.getElementById('vote-bar-c-'+rep+comment_id).innerHTML += '<a href="javascript:void(0)" onClick="toogleCommentVote('+comment_id+','+type+')">(change)</a>';
    
    
    var score = parseInt(document.getElementById('score-comment-'+rep+comment_id).innerHTML);    
    if(v==0){
        score = score-1;
        //document.getElementById('down-votes-comment-'+rep+comment_id).innerHTML= 1 + parseInt(document.getElementById('down-votes-comment-'+rep+comment_id).innerHTML);
    }else{
        score = score+1;
        //document.getElementById('up-votes-comment-'+rep+comment_id).innerHTML= 1 + parseInt(document.getElementById('up-votes-comment-'+rep+comment_id).innerHTML);
    }
    if(score > 0) score = '+'+score;
    document.getElementById('score-comment-'+rep+comment_id).innerHTML=score;
                   
    return false;    
}

function toogleVote(prop_id){
    var query_string = 'action=toogle_vote';
    query_string += '&product_id='+prop_id;
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading()},onComplete:function(request){item_complete()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});                
    
    var cur_image = document.getElementById('voted-how-p-'+prop_id).src.split('/').pop();    
    if(cur_image=='thumb_up.gif'){
        var old_vote = 1;        
        document.getElementById('voted-how-p-'+prop_id).src='/images/thumb_down.gif';
        document.getElementById('up-votes-'+prop_id).innerHTML = -1 + parseInt(document.getElementById('up-votes-'+prop_id).innerHTML);
        document.getElementById('down-votes-'+prop_id).innerHTML = 1 + parseInt(document.getElementById('down-votes-'+prop_id).innerHTML);
    }
    else{
        var old_vote = 0;
        document.getElementById('voted-how-p-'+prop_id).src='/images/thumb_up.gif';
        document.getElementById('up-votes-'+prop_id).innerHTML = 1 + parseInt(document.getElementById('up-votes-'+prop_id).innerHTML);
        document.getElementById('down-votes-'+prop_id).innerHTML = -1 + parseInt(document.getElementById('down-votes-'+prop_id).innerHTML);        
    }
        
    
    var score = parseInt(document.getElementById('score-'+prop_id).innerHTML);
    if(old_vote==1){
        score = score-2;
    }else{
        score = score+2;
    }
    if(score > 0) score = '+'+score;
    document.getElementById('score-'+prop_id).innerHTML=score;
}

function toogleCommentVote(comm_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    var query_string = 'action=toogle_comment_vote';
    query_string += '&comment_id='+comm_id;
    query_string += '&type='+type;    
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading2()},onComplete:function(request){item_complete2()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});            
    
    
    var cur_image = document.getElementById('voted-how-'+rep+comm_id).src.split('/').pop();    
    if(cur_image=='thumb_up.gif'){
        var old_vote = 1;        
        document.getElementById('voted-how-'+rep+comm_id).src='/images/thumb_down.gif';
    }
    else{
        var old_vote = 0;
        document.getElementById('voted-how-'+rep+comm_id).src='/images/thumb_up.gif';
    }
        
    var score = parseInt(document.getElementById('score-comment-'+rep+comm_id).innerHTML);    
    
    if(old_vote==1){
        score = score-2;
        //document.getElementById('up-votes-comment-'+rep+comm_id).innerHTML= -1 + parseInt(document.getElementById('up-votes-comment-'+rep+comm_id).innerHTML);
        //document.getElementById('down-votes-comment-'+rep+comm_id).innerHTML= 1 + parseInt(document.getElementById('down-votes-comment-'+rep+comm_id).innerHTML);
    }else{
        score = score+2;
        //document.getElementById('up-votes-comment-'+rep+comm_id).innerHTML= 1 + parseInt(document.getElementById('up-votes-comment-'+rep+comm_id).innerHTML);
        //document.getElementById('down-votes-comment-'+rep+comm_id).innerHTML= -1 + parseInt(document.getElementById('down-votes-comment-'+rep+comm_id).innerHTML);
    }
    if(score > 0) score = '+'+score;
    document.getElementById('score-comment-'+rep+comm_id).innerHTML=score;
    
}

function showCommentReplies(comment_id, type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    var query_string = 'action=show_comment_replies';
    query_string += '&comment_id='+comment_id;
    query_string += '&type='+type;
    new Ajax.Updater('comment-replies-place-'+rep+comment_id,'/ajax.php',{onLoading:function(request){item_loading2()},onComplete:function(request){item_complete2()},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});            
    document.getElementById('reply-show-'+rep+comment_id).style.display='none';
    document.getElementById('reply-hide-'+rep+comment_id).style.display='inline';
}
function hideCommentReplies(comment_id,type){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    document.getElementById('reply-show-'+rep+comment_id).style.display='inline';
    document.getElementById('reply-hide-'+rep+comment_id).style.display='none';
    document.getElementById('comment-replies-place-'+rep+comment_id).innerHTML='';
}

function enableSaveButton(tarea, idbut){    
    if(trim(document.getElementById(tarea).value).length > 0){
        document.getElementById(idbut).disabled=false;
    }else{
        document.getElementById(idbut).disabled=true;
    }
}

function vote_prop_direct(prop_id, vote, is_l, preview){    

    if(document.getElementById('pop_opened').value == 'true') 
	{
	return;
	}
    if(!is_l){
        popMembershipBox(); return;             
    }
        
    if(vote==0){
        var ud='down';
        var ivote = -1;
    }else{
        var ud='up';
        var ivote = 1;
    }

    document.getElementById('vote-image-up-'+prop_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />&nbsp;&nbsp;';
    document.getElementById('vote-image-down-'+prop_id).innerHTML='<img src="/images/thumb_down_dis.gif" width="14" heigth="14" />';
    if(document.getElementById('vote-bar-'+prop_id)){
        document.getElementById('vote-bar-'+prop_id).innerHTML = 'You voted&nbsp;<img id="voted-how-p-'+prop_id+'" src="/images/thumb_'+ud+'.gif" width="14" heigth="14" />&nbsp;<a href="javascript:void(0)" onClick="toogleVote('+prop_id+')">change</a>';
    }    
    
    var score = ivote+parseInt(document.getElementById('score-'+prop_id).innerHTML);
    if(score > 0){
        score = '+'+score;
        document.getElementById('up-votes-'+prop_id).innerHTML = 1 + parseInt(document.getElementById('up-votes-'+prop_id).innerHTML);
    }else{
        document.getElementById('down-votes-'+prop_id).innerHTML = 1 + parseInt(document.getElementById('down-votes-'+prop_id).innerHTML);
    }
    document.getElementById('score-'+prop_id).innerHTML=score;
    
    if(document.getElementById('comment-form'))
    document.getElementById('comment-form').innerHTML='';
    
    showCommentPop(prop_id,vote,preview);
    
    var query_string = 'action=vote_product_direct';
    query_string += '&product_id='+prop_id;
    query_string += '&vote='+vote;    
    new Ajax.Updater('ajax-response','/ajax.php',{onLoading:function(request){item_loading()},onComplete:function(request){item_complete();},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});                
}


/* POP - START */

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
if(IE){
    window.onload = function(){
        if(document.getElementById('pop_opened'))
        document.getElementById('pop_opened').value = 'false';
    }
}

document.onmousemove = getMouseXY;
document.onclick = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

var scrOfX = 0, scrOfY = 0;
function getScrollXY() {
  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
}

function getMouseXY(e) {
  getScrollXY();  
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + scrOfX
    tempY = event.clientY + scrOfY
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  //alert(tempX+','+tempY);
  return true
}

function showCommentPop(prop_id,vote,preview){    
    document.getElementById('pop_opened').value = 'true';
    document.getElementById('show_preview').value = preview;
    document.getElementById('glob_prop_id').value = prop_id;
    document.getElementById('comment-box-no').disabled=false;    
    document.getElementById('comment-box-ta').value='';
    document.getElementById('comment-box-ar').innerHTML='';
    document.getElementById('comment-box').style.display='block';
    
    if(navigator.userAgent.indexOf('MSIE 6') > 0 )
    document.getElementById('ifrim').style.visibility='hidden';
    
    //test margins
    if(tempX + 410 > document.body.clientWidth) tempX = tempX-410;
    if(tempY + 160 > document.body.clientHeight) tempY = tempY-160;
    
    document.getElementById('comment-box').style.left = tempX+'px';
    document.getElementById('comment-box').style.top = tempY+'px';    
}
function cancelCommentDirect(){
    if(navigator.userAgent.indexOf('MSIE 6') > 0)
    document.getElementById('ifrim').style.visibility='visible';

    document.getElementById('comment-box').style.display='none';    
    document.getElementById('comment-box-ta').value='';
    document.getElementById('comment-box-save').disabled=true;    
    document.getElementById('pop_opened').value = 'false';
}

/* POP - START */

function item_loading3(){
    document.getElementById('comment-box-progress').style.display='inline';
}
function item_complete3(author,comment){        
    document.getElementById('comment-box-progress').style.display='none';    
    if(document.getElementById('productComments') && parseInt(document.getElementById('show_preview').value))
        previewComment(author,comment);    
    setTimeout('cancelCommentDirect()',500);            
}
function saveCommentDirect(comment,author){    
    prop_id = document.getElementById('glob_prop_id').value;
    vote_id = parseInt(document.getElementById('s_vote_id_'+prop_id).innerHTML);
    if(document.getElementById('comments-number-'+prop_id)){
        var current_comments_number = parseInt(document.getElementById('comments-number-'+prop_id).innerHTML);
    }else{
        var current_comments_number = 0;
    }
    if(current_comments_number==0){
        document.getElementById('comments-link-'+prop_id).innerHTML = 'comment added';
    }else{
        document.getElementById('comments-number-'+prop_id).innerHTML = 1 + parseInt(document.getElementById('comments-number-'+prop_id).innerHTML);
    }
    document.getElementById('comment-box-no').disabled=true;
    document.getElementById('comment-box-save').disabled=true;
    var query_string = 'action=save_comment_direct';
    query_string += '&comment='+comment;
    query_string += '&vote_id='+vote_id;    
    new Ajax.Updater('comment-box-ar','/ajax.php',{onLoading:function(request){item_loading3()},onComplete:function(request){item_complete3(author,comment)},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});                    
}

function item_loading_4(comment_id, rep){
    document.getElementById('progress-fetch-comment-'+rep+comment_id).style.display='inline';
}
function item_complete4(comment_id, rep){
    document.getElementById('progress-fetch-comment-'+rep+comment_id).style.display='none';
}
function show_comment_below_treshold(comment_id,type,current_vote,is_l){
    var rep='';
    if(type==2){
        rep = 'r-';
    }
    
    if(document.getElementById('show_comment_below_th_'+rep+comment_id).innerHTML){
        document.getElementById('show_comment_below_th_'+rep+comment_id).innerHTML='';
        document.getElementById('reply-show-'+rep+comment_id).style.display='none';        
        document.getElementById('reply-hide-'+rep+comment_id).style.display='none';
        document.getElementById('show_comment_below_treshold-link-'+rep+comment_id).innerHTML=document.getElementById('show_comment_below_treshold-link-'+rep+comment_id).innerHTML.replace(/hide comment/,'show comment');        
        if(current_vote==-1){
            document.getElementById('vote-image-comment-up-'+rep+comment_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />';
            document.getElementById('vote-image-comment-down-'+rep+comment_id).innerHTML='<img src="/images/thumb_up_dis.gif" width="14" heigth="14" />';        
        }
        return ;
    }
    
    document.getElementById('reply-show-'+rep+comment_id).style.display='inline';    
    document.getElementById('show_comment_below_treshold-link-'+rep+comment_id).innerHTML=document.getElementById('show_comment_below_treshold-link-'+rep+comment_id).innerHTML.replace(/show comment/,'hide comment');
    if(current_vote==-1){        
        document.getElementById('vote-image-comment-up-'+rep+comment_id).innerHTML='<a href="javascript:void(0)"><img src="/images/thumb_up.gif" onClick="vote_comment('+comment_id+',1,'+type+','+is_l+');" width="14" heigth="14"/></a>';
        document.getElementById('vote-image-comment-down-'+rep+comment_id).innerHTML='<a href="javascript:void(0)"><img src="/images/thumb_down.gif" onClick="vote_comment('+comment_id+',0,'+type+','+is_l+');" width="14" heigth="14"/></a>';
    }
    
    
    
    var query_string = 'action=show_comment_below_treshold';
    query_string += '&comment_id='+comment_id;
    query_string += '&type='+type;
    new Ajax.Updater('show_comment_below_th_'+rep+comment_id,'/ajax.php',{onLoading:function(request){item_loading_4(comment_id, rep)},onComplete:function(request){item_complete4(comment_id, rep)},parameters:query_string, insertion:Insertion.Bottom, asynchronous:true});                        
}

function popMembershipBox(){
    //test margins
    if(tempX + 350 > document.body.clientWidth) tempX = tempX-350;
    if(tempY + 60 > document.body.clientHeight) tempY = tempY-60;

    box = document.getElementById('membership-prompt-box');
    box.style.left = tempX+'px';
    box.style.top = tempY+'px';    
    box.style.display='block';
}

function trim(sString){
    while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '\n'){
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ' || sString.substring(sString.length-1, sString.length) == '\n'){
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
