function doc_ready ()
      {
        // отключение png-фона для ie6 и более старых
        if (detectIE6()) {
            document.getElementById('container').style.backgroundImage='none';
        }
  
        document.getElementById('shop_1_stars').onmousemove = change_rating;
        document.getElementById('shop_1_stars').onmouseout = out_rating;
        document.getElementById('shop_1_stars').onmousedown = set_rating;
        document.getElementById('shop_1_add_comment').onfocus=comment_focus;
        document.getElementById('shop_1_add_comment').onblur=comment_blur;
        document.getElementById('shop_1_add_name').onfocus=name_focus;
        document.getElementById('shop_1_add_name').onblur=name_blur;

        document.getElementById('shop_2_stars').onmousemove = change_rating;
        document.getElementById('shop_2_stars').onmouseout = out_rating;
        document.getElementById('shop_2_stars').onmousedown = set_rating;
        document.getElementById('shop_2_add_comment').onfocus=comment_focus;
        document.getElementById('shop_2_add_comment').onblur=comment_blur;
        document.getElementById('shop_2_add_name').onfocus=name_focus;
        document.getElementById('shop_2_add_name').onblur=name_blur;
		
		document.getElementById('shop_3_stars').onmousemove = change_rating;
        document.getElementById('shop_3_stars').onmouseout = out_rating;
        document.getElementById('shop_3_stars').onmousedown = set_rating;
        document.getElementById('shop_3_add_comment').onfocus=comment_focus;
        document.getElementById('shop_3_add_comment').onblur=comment_blur;
        document.getElementById('shop_3_add_name').onfocus=name_focus;
        document.getElementById('shop_3_add_name').onblur=name_blur;
		
		document.getElementById('shop_4_stars').onmousemove = change_rating;
        document.getElementById('shop_4_stars').onmouseout = out_rating;
        document.getElementById('shop_4_stars').onmousedown = set_rating;
        document.getElementById('shop_4_add_comment').onfocus=comment_focus;
        document.getElementById('shop_4_add_comment').onblur=comment_blur;
        document.getElementById('shop_4_add_name').onfocus=name_focus;
        document.getElementById('shop_4_add_name').onblur=name_blur;
		
		document.getElementById('shop_5_stars').onmousemove = change_rating;
        document.getElementById('shop_5_stars').onmouseout = out_rating;
        document.getElementById('shop_5_stars').onmousedown = set_rating;
        document.getElementById('shop_5_add_comment').onfocus=comment_focus;
        document.getElementById('shop_5_add_comment').onblur=comment_blur;
        document.getElementById('shop_5_add_name').onfocus=name_focus;
        document.getElementById('shop_5_add_name').onblur=name_blur;
      }

/* функции смены фона кнопок */
function buy_link(id)
{
    document.getElementById(id).setAttribute('src', 'images/buy_link.png');
}
function buy_hover(id)
{
    document.getElementById(id).setAttribute('src', 'images/buy_hover.png');
}
function buy_active(id)
{
    document.getElementById(id).setAttribute('src', 'images/buy_active.png');
}



function rev_link(id)
{
    document.getElementById(id).setAttribute('src', 'images/add_link.png');
}
function rev_hover(id)
{
    document.getElementById(id).setAttribute('src', 'images/add_hover.png');
}
function rev_active(id)
{
    document.getElementById(id).setAttribute('src', 'images/add_active.png');
}

/* скрытие/показ блока комментариев */

function show_reviews(shop_id)
{
    var img = document.getElementById(shop_id+'_r').style.backgroundImage;
    //alert(img);
    if (img.indexOf('images/r_down.png')!=-1 || img=="") {
        document.getElementById(shop_id+'_reviews').style.display = 'block';
        document.getElementById(shop_id+'_r').style.backgroundImage = "url(images/r_up.png)";
    } else {
        document.getElementById(shop_id+'_reviews').style.display = 'none';
        document.getElementById(shop_id+'_r').style.backgroundImage = "url(images/r_down.png)";
    }
}

/* функции задания рейтинга */
function change_rating(event)
{
    var i;
    var event = event || window.event;
    var bg = this.getBoundingClientRect();
    var items = Math.ceil((event.clientX - bg.left-1)/17);
    var images = document.getElementById(this.id).getElementsByTagName('img');
    for (i=0; i<images.length; i++) {
//        alert(images[i].getAttribute('src'))
        images[i].setAttribute('src', ((i<items) ? 'images/star.png' : 'images/star_hide.png'));
    }
}
function out_rating ()
{
    var i=0;
    var items = document.getElementById(this.id+'_rating').value;
    var images = document.getElementById(this.id).getElementsByTagName('img');
    for (i=0; i<5; i++) {
        images[i].setAttribute('src', ((i<items) ? 'images/star.png' : 'images/star_hide.png'));
    }
}
function set_rating()
{
    var i=0;
    var items=0;
    var images = document.getElementById(this.id).getElementsByTagName('img');
    for (i=0; i<images.length; i++) {
        if (images[i].getAttribute('src').indexOf('images/star.png')!=-1) {
            items++;
        }
    }
    document.getElementById(this.id+'_rating').value=items;
}

/* вывод/сокрытие подсказок в полях добавление комментария */

function name_focus()
{
    this.value = (this.value=='Your name') ? '' : this.value;
}
function name_blur()
{
    this.value = (this.value=='') ? 'Your name' : this.value;
}
function comment_focus()
{
    this.style.backgroundImage='url(images/textarea_bg_focus.png)';
    this.value = (this.value=='Your comment') ? '' : this.value;
}
function comment_blur()
{
    this.style.backgroundImage='url(images/textarea_bg.png)';  
    this.value = (this.value=='') ? 'Your comment' : this.value;
}


/* добавление комментария */
function createRequest() {
    var request;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                request = false;
            }
        }
    }
    return request;
}

var r;

function add_comment(shop_id)
{   if (document.getElementById(shop_id+'_add_name').value.match(/([\<])([^\>]{1,})*([\>])/i)==null){
    if (document.getElementById(shop_id+'_add_comment').value.match(/([\<])([^\>]{1,})*([\>])/i)==null){
    if(document.getElementById(shop_id+'_add_name').value == "Your name"){
    alert("Set your name");
    }
	else{ 
	   if(document.getElementById(shop_id+'_stars_rating').value == 0){
       alert("Set your rating");
       } 
	   else{
	      if(document.getElementById(shop_id+'_add_comment').value == "Your comment"){
          alert("Set your comment");
          } 
	      else{
    r = createRequest();
    if (r) {
        var url = "add_comment.php";
		comment = document.getElementById(shop_id+'_add_comment').value;
        comment = comment.replace(/\n/g, " ");
		comment = comment.replace(/\r/g, "");
        var data = 'shop='+shop_id+'&name='+document.getElementById(shop_id+'_add_name').value+'&comment='+comment+'&rating='+document.getElementById(shop_id+'_stars_rating').value;
		//alert(data);
        r.open("POST", url, true);
        r.onreadystatechange = update_add_comment;
        r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        r.send(data);
     }
    }
   }  
  }
  }
  else{
  alert("No HTML in comment is allowed");
  }
  }
  else{
  alert("No HTML in name is allowed");
  }
}
function update_add_comment()
{
    if ((r.readyState == 4) && (r.status == 200)) {
        var data = eval("("+r.responseText+")");
        var state = data['state'];
        if (state == '1') {
            var shop = data['shop'];

            // добавление разделителя
            var list = document.getElementById(shop+'_comments');
            var deliver = document.createElement('p');

            // добавление нового комментария
            var li = document.createElement('li');
            var html = "<p class='com_name'>" + data['name'] + "</p>" +
                "<p class='com_date'>"+data['date']+" User Rating: <span class='com_rating'>";
            for (i=0; i<5; i++) {
                html += (i<data['rating']) ? "<img src='images/star.png'>" : "<img src='images/star_hide.png'>";
            }
			
            html += "</span></p>\n" +
                "<p class='com_text'>" + data['comment'] + "</p>";
            li.innerHTML = html;
            list.appendChild(li);
			deliver.className = 'deliver';
            deliver.innerHTML = "* * *";
            list.getElementsByTagName('li')[list.getElementsByTagName('li').length-1].appendChild(deliver);
        } else {
            // error
            document.getElementById(data['shop']+'_add_comment').style.backgroundImage='url(images/textarea_bg_error.png)';
        }

   }
}



function detectIE6(){
  var browser = navigator.appName;
  if (browser == "Microsoft Internet Explorer"){
    var b_version = navigator.appVersion;
    var re = /\MSIE\s+(\d\.\d\b)/;
    var res = b_version.match(re);
    if (res[1] <= 6){
      return true;
    }
  }
  return false;
}
