var image1 = new Image();
image1.src = "layout/star_active.png";


function changePic(num) {
  for (var i = 1; i <= num; i++) {
    document.getElementById("pic" + i).src = "layout/star_active.png";
  }
  return true;
}


function resetPic() {
  for (var i = 1; i <= 10; i++) {
    document.getElementById("pic" + i).src = "layout/star.png";
  }
  return true;
}


function sendmail(user) {
  var tmp = 'mailto:' + user + '@janesgallery.com';
  location.href = tmp;
}


function clearText(field){
    if (field.value == field.defaultValue) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function swapImg(pic) {
  var picsrc = pic.src;
  if (picsrc.indexOf("active") >= 0) {
    picsrc = picsrc.replace(/_active/, "");
  } else {
    picsrc = picsrc.replace(/\.png$/, "_active.png");
  }
  pic.src = picsrc;
}

function showcomments() {
  if (document.getElementById('morecomments').style.display !== "block") {
    document.getElementById('morecomments').style.display = "block";
    document.getElementById('arrow').src = "layout/arrow-down.png";
  } else {
    document.getElementById('morecomments').style.display = "none";
    document.getElementById('arrow').src = "layout/arrow-right.png";
  }
}


function ratingdetails(id) {
  ratingDetails = window.open("ratingdetails.php?id=" + id, "_blank", "height=220,width=300,location=no,menubar=no,scrollbars=no,toolbar=no,resizable=no,status=no,border=no");
}

// AJAX-Routinen
var xmlHttp;

function getXMLHttpRequest() {
  if (window.XMLHttpRequest) {
    // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      // XMLHTTP (neu) für Internet Explorer 
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        // XMLHTTP (alt) für Internet Explorer
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        return null;
      }
    }
  }
  return false;
}


function readyStateHandlerVote() {
  if (xmlHttp.readyState === 4) {
    if (xmlHttp.status === 200 || status === 304) {
      document.getElementById('ratingdiv').innerHTML = xmlHttp.responseText;
    } else {
      window.alert("Error: " + xmlHttp.status + " " + xmlHttp.statusText);
    }
  }
}


function vote(id, points) {
  xmlHttp = getXMLHttpRequest();
  if (xmlHttp === null) {
    window.alert("Sorry, your browser does not support AJAX!");
    return false;
  }
  var url = "vote.php?id=" + id;
  url = url + "&points=" + points;
  url = url + "&sid=" + Math.random();
  xmlHttp.onreadystatechange = readyStateHandlerVote;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}


function readyStateHandlerComment() {
  if (xmlHttp.readyState === 4) {
    if (xmlHttp.status === 200 || status === 304) {
      var comments = document.getElementById('comments');
      var new_comment = document.createElement('div');
      new_comment.innerHTML = xmlHttp.responseText;
      comments.insertBefore(new_comment, comments.firstChild);
      document.getElementById('commentdiv').innerHTML = "Thanks! Your comment has been saved<br>and will be visible after our approval.";
    } else {
      window.alert("Error: " + xmlHttp.status + " " + xmlHttp.statusText);
    }
  }
}


function addComment() {
  var words = document.forms['commentform'].elements['comment'].value.split(" ");
  if (words.length < 2) {
    alert("Please write a bit more than just one word. Thank you!");
    return false;
  }
  xmlHttp = getXMLHttpRequest();
  if (xmlHttp === null) {
    window.alert("Sorry, your browser does not support AJAX!");
    return false;
  }
  var url = "comment.php?id=" + document.forms['commentform'].elements['id'].value;
  url = url + "&comment=" + document.forms['commentform'].elements['comment'].value;
  url = url + "&sid=" + Math.random();
  xmlHttp.onreadystatechange = readyStateHandlerComment;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  return false;
}
