﻿//將圖片ID設入COOKIES
function setImgIdToCookies(ImgID) {
    var xmlpath = "img_select/setToCookies/"
    var MemVar = "Id=" + ImgID + "&action=addnew"
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");   
    xmlhttp.open("POST", xmlpath, true); 
    xmlhttp.setRequestHeader ("CONTENT-TYPE","application/x-www-form-urlencoded")
    xmlhttp.setRequestHeader ("Content-Length",MemVar.length)

    xmlhttp.onreadystatechange = function(){ 
       if (xmlhttp.readyState == 4) {
          if (xmlhttp.status == 200) {            
              if (xmlhttp.responseText == "ADDNEWOK") {
//alert(xmlhttp.responseText)
                  return false
              }
          }
       }; 
        
    }

    xmlhttp.send(MemVar);          
}

function ReMoveImgIdFromCookies(ImgID) {
    var xmlpath = "img_select/setToCookies/"
    var MemVar = "Id=" + ImgID + "&action=remove"
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");   
    xmlhttp.open("POST", xmlpath, true); 
    xmlhttp.setRequestHeader ("CONTENT-TYPE","application/x-www-form-urlencoded")
    xmlhttp.setRequestHeader ("Content-Length",MemVar.length)

    xmlhttp.onreadystatechange = function(){ 
       if (xmlhttp.readyState == 4) {
          if (xmlhttp.status == 200) {            
              if (xmlhttp.responseText == "REMOVEOK") {
//alert(xmlhttp.responseText)
                  return false
              }
          }
       }; 

    }

    xmlhttp.send(MemVar);          
}


function getImgIdToCookies(openerObj) {
    var xmlpath = "img_select/setToCookies/"
    var MemVar = "action=read"
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");   
    xmlhttp.open("POST", xmlpath, true); 
    xmlhttp.setRequestHeader ("CONTENT-TYPE","application/x-www-form-urlencoded")
    xmlhttp.setRequestHeader ("Content-Length",MemVar.length)

    xmlhttp.onreadystatechange = function(){ 
       if (xmlhttp.readyState == 4) {
          if (xmlhttp.status == 200) {            
              if (!(xmlhttp.responseText == "")) {
                  //alert(xmlhttp.responseText)
                  document.getElementById(openerObj).innerHTML=xmlhttp.responseText;
                  return false
              }
          }
          
       }; 
    }

    xmlhttp.send(MemVar);          
}

function ReMoveImgIdFromCookies2(ImgID,openerObj) {
    var xmlpath = "img_select/setToCookies/"
    var MemVar = "Id=" + ImgID + "&action=remove"
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");   
    xmlhttp.open("POST", xmlpath, true); 
    xmlhttp.setRequestHeader ("CONTENT-TYPE","application/x-www-form-urlencoded")
    xmlhttp.setRequestHeader ("Content-Length",MemVar.length)

    xmlhttp.onreadystatechange = function(){ 
       if (xmlhttp.readyState == 4) {
          if (xmlhttp.status == 200) {            
              if (xmlhttp.responseText == "REMOVEOK") {
                  getImgIdToCookies(openerObj)
                  return false
              }
          }
       }; 

    }

    xmlhttp.send(MemVar);   
}


function setToCookies (id ,thisObj) {
         if (thisObj.checked) {
             setImgIdToCookies(id)
         }
         else {
             ReMoveImgIdFromCookies(id)
         }
}