var server_base_url = "http://community.menshealth.it";//author: santosh 


// Constructor -- pass a REST request URL to the constructor
//
function PAscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + PAscriptRequest.scriptCounter++;
}

// Static script ID counter
PAscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
PAscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
PAscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
PAscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

var http_request = false;
if (typeof server_base_url =="undefined") {
  base_url1 = 'http://test.community.menshealth.it';
} else {
  base_url1 = server_base_url;
}
   function makePOSTRequest(url, parameters, call_type) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
                // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      if (call_type == 'comment') {
        http_request.onreadystatechange = comment.showContents;
      }else if (call_type == 'rating') {
        http_request.onreadystatechange = comment.showrating;
      }else if (call_type == 'pagination') {
        http_request.onreadystatechange = comment.showpaging;
      }
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

var comment = {
   showContents : function () {
    if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.forms['comment_form'].comment.value = '';
            if (result != 'Please login to comment') {
              alert('Comment posted sucessfully'); 
            }else {
              document.getElementById('comment_form_div').style.display = 'none';
            }
            document.getElementById('posted_comment').innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
      }
  },
  showrating : function () {
    if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('rating_main_div').innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
      }
  },
  showpaging : function () {
     if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result_paging = http_request.responseText;
            document.getElementById('comment_page_div').innerHTML = result_paging;
         } else {
            alert('There was a problem with the request.');
         }
      }
  }  
};
   function get(obj) {
     if (obj.comment.value == '') {
       alert('Il commento non può essere vuoto');
     } else {
      var poststr = "comment=" + escape(obj.comment.value)+
                    "&slug=" + escape(obj.slug.value)+
                    "&c_id=" + escape(obj.c_id.value)+
                    "&uid=" + escape(obj.uid.value)+
                    "&title="+escape(document.title)+
                    "&type=" + escape(obj.page_type.value)+
                    "&nick_name=" + escape(obj.nick_name.value)+
                    "&blog_url=" + escape(obj.blog_url.value)+
					          "&default_url=" + escape(obj.default_url.value)+
                    "&callback=echoComment";
                    var req = base_url1+'/WidgetsNew/Comment/post_comment_rest.php?'+poststr;
              // Create a new request object
              bObj = new PAscriptRequest(req); 
              // Build the dynamic script tag
              bObj.buildScriptTag(); 
              // Add the script tag to the page
              bObj.addScriptTag();
      //makePOSTRequest('/Widgets/Comment/post_comment.php', poststr,'comment');
    }
   }
   function redirect_login(url) {
     window.location= url+window.location;
   }
var thumbs_rating = {
  click : function (star_id, id, rating_type, max_rating,login_uid) {
    get_data = 'rating='+star_id+'&type_id='+id+'&rating_type='+rating_type+'&max_rating='+max_rating+'&login_uid='+login_uid+'&callback=echoRating';  
    var req = base_url1+'/WidgetsNew/Comment/rating_thumb_rest.php?'+get_data;
    // Create a new request object
    bObj = new PAscriptRequest(req); 
    // Build the dynamic script tag
    bObj.buildScriptTag(); 
    // Add the script tag to the page
    bObj.addScriptTag(); 
//     makePOSTRequest('/Widgets/Comment/rating_thumb.php', get_data, 'rating');
  },
  user_rating : 0
};
function ajax_pagination(obj, cid, slug) {
  query_string = obj.search;
  page_num = query_string.split('=');
  page_num = parseInt(page_num[1]);
  get_data = 'page='+page_num+'&cid='+cid+'&slug='+slug+'&callback=echoPaging';
  var req = base_url1+'/WidgetsNew/Comment/comment_pagination_rest.php?'+get_data;
  // Create a new request object
  bObj = new PAscriptRequest(req); 
  // Build the dynamic script tag
  bObj.buildScriptTag(); 
  // Add the script tag to the page
  bObj.addScriptTag();
//   makePOSTRequest('/Widgets/Comment/comment_pagination.php', get_data, 'pagination');
}

function show_hide (id) {
  if (document.getElementById(id).style.display=='' || document.getElementById(id).style.display=='none') {
    document.getElementById(id).style.display = 'block';
  } else {
    document.getElementById(id).style.display = 'none';
  }
}


function echoComment(jsonData) {
  if (jsonData.result == 'true') {//comment is posted successfully
    document.forms['comment_form'].comment.value = '';
    document.forms['comment_form'].nick_name.value = '';
    document.forms['comment_form'].blog_url.value = '';
    document.getElementById('posted_comment').innerHTML = jsonData.ResultSet;
    document.getElementById('total_comments').innerHTML = jsonData.TotalComment;
    /*if (jsonData.stat == 'true') {
      document.getElementById('comment-thanks').style.display='block'; 
    }*/
    return(false);
  }else {
    alert('There is problem with the request');
  }
}

function echoPaging(jsonData) {
  if (jsonData.result == 'true') {//comment is posted successfully
     document.getElementById('comment_page_div').innerHTML = jsonData.ResultSet;
  }else {
    alert('There is problem with the request');
  }
}
function echoRating(jsonData) {
  if (jsonData.result == 'true') {//comment is posted successfully
     document.getElementById('rating_main_div').innerHTML = jsonData.ResultSet;
  }else {
    alert('There is problem with the request');
  }
}
var current;
var toggle_stars = {
  
  mouseover : function (star_id, id) {
    for (cnt = 0; cnt < star_id; cnt++) {
      document.getElementById('star_'+id+'_'+(cnt+1)).src = base_url1+"/Themes/Beta/images/active_star.gif";
    }
    for (cnt = star_id+1; cnt <= 5; cnt++) {
      document.getElementById('star_'+id+'_'+(cnt)).src = base_url1+"/Themes/Beta/images/inactive_star.gif";
      
    }
    current = star_id;
  },
/**mouseout : function (star_id, id) {
     if (star_id == 1) {
       document.getElementById('star_'+id+'_1').src = "http://santosh.com/Themes/Gamma/images/inactive_star.gif";
     }
  },**/
  click : function (star_id, id, rating_type, max_rating,login_uid) {
     get_data = 'rating='+star_id+'&type_id='+id+'&rating_type='+rating_type+'&max_rating='+max_rating+'&login_uid='+login_uid+'&callback=echoRating';  
    var req = base_url1+'/WidgetsNew/Comment/comment_rating_rest.php?'+get_data;
    // Create a new request object
    bObj = new PAscriptRequest(req); 
    // Build the dynamic script tag
    bObj.buildScriptTag(); 
    // Add the script tag to the page
    bObj.addScriptTag(); 
  },
  click1: function (id, rating_type, max_rating, login_uid) {
    get_data = 'rating='+current+'&type_id='+id+'&rating_type='+rating_type+'&max_rating='+max_rating+'&login_uid='+login_uid+'&callback=echoRating';  
    var req = base_url1+'/WidgetsNew/Comment/comment_rating_rest.php?'+get_data;
    // Create a new request object
    bObj = new PAscriptRequest(req); 
    // Build the dynamic script tag
    bObj.buildScriptTag(); 
    // Add the script tag to the page
    bObj.addScriptTag(); 
    document.getElementById('rating-popup').style.display='none'; 
    document.getElementById('rating-thanks').style.display='block'; 
    return(false);
  }
  // , user_rating : 0
};

function get_view(page_type, slug) {

     if (document.title == '') {
       return false;
     }
     if (page_type == '' ) {
       alert("Type can't be empty");
     } else {
      loc = document.URL;
      //url = loc.substring(0,loc.indexOf('.php'));
      var poststr = "type=" + escape(page_type)+
                    "&title=" + escape(document.title)+
                    "&url=" + escape(loc)+
                    "&slug="+ escape(slug);
                  
                    
              var req = base_url1+'/WidgetsNew/ViewTracker/update_views.php?'+poststr;
              // Create a new request object
              bObj = new PAscriptRequest(req); 
              // Build the dynamic script tag
              bObj.buildScriptTag(); 
              // Add the script tag to the page
              bObj.addScriptTag();
                  
    }
}

function ajax_delete(comment_id, cid, slug) {
	get_data = 'comment_id='+escape(comment_id)+'&cid='+escape(cid)+'&slug='+escape(slug)+'&callback=echoDelete';  
    var req = base_url1+'/WidgetsNew/Comment/delete_comment_rest.php?'+get_data;
    // Create a new request object
    bObj = new PAscriptRequest(req); 
    // Build the dynamic script tag
    bObj.buildScriptTag(); 
    // Add the script tag to the page
    bObj.addScriptTag(); 
}

function echoDelete(jsonData) {
  if (jsonData.result == 'true') {//comment is posted successfully
     document.getElementById('comment_page_div').innerHTML = jsonData.ResultSet;
     document.getElementById('total_comments').innerHTML = jsonData.TotalComment;
  }else {
    alert('There is problem with the request');
  }
}
document.getElementById('pa_widget_comment').innerHTML = '<br />\n<ul class="defloater"></ul>\n<ul id="commenti-articolo">\n  <li class="commenti-titolone">\n  <span class="commenti-mh">MH</span> COSA NE PENSI?</li>\n  <!--li class="commenti-articolo-numero"><a href="$urlRitorno" title="commenti" id="total_comments">0</a></li-->\n  <li class="defloater"></li>\n</ul>\n<a href="#" onclick="document.location = \'http://community.menshealth.it/login.php?return=\' + escape(document.URL);" title="commenti" id="total_comments">\nPer commentare un articolo devi prima registrarti facendo il log-in.\n</a>\n<br />\n<div  id="comment_page_div" >\n  <div  id="posted_comment">\n     <ul><li>Non ci sono ancora commenti a questa pagina.</li></ul>\n      </div>\n  <div class="page_num_holder">\n      </div>\n</div>  \n\n					\n\n				\n';document.write('<script type="text/javascript" language="javascript">\n var page_type = "image";\n var slug = "ez_2942";\n get_view(page_type, slug);\n</script>');
