function select10Card(){
  cardSelect = new Array();
  forecastCard = 10;
}
function select5Card(){
  cardSelect = new Array();
  forecastCard = 5;
}

function select3Card(){
  cardSelect = new Array();
  forecastCard = 3;
}
function select1Card(){
  cardSelect = new Array();
  forecastCard = 1;
}

function forecastbyMaster(){
   
    if(forecastCard == 10){
        showCard10ForecastByMaster();
        sendCardMode1(10);
    }else if(forecastCard == 5){
        showCard5ForecastByMaster();
        sendCardMode1(5);
    }else if(forecastCard == 3){
        showCard3ForecastByMaster();
        sendCardMode1(3);
    }else if(forecastCard == 1){
        showCard1ForecastByMaster();
        sendCardMode1(1);
    }
}


var cur_question_name = '';
function keepCardHistory(){


    var spanTab = document.createElement('span');
    spanTab.className = 'forecasttabhistory';
    
    spanTab.setAttribute('historyIndex',cardForecastedList.length);
    cardForecastedList[cardForecastedList.length] =  cardSelect;//Store the cards are selected by customer
    spanTab.onclick = function(){
      var objForecastTable = byId('ForecastTable');
      var divList = objForecastTable.getElementsByTagName('div');
      while( divList.length>0){
          var objDiv = divList[0];
          objDiv.parentNode.removeChild(objDiv);
      }

      var spanList = objForecastTable.getElementsByTagName('span');
      for(var i = 0; i < spanList.length; i++){
          var objSpan = spanList[i];
          objSpan.className = 'forecasttabhistory';
      }
      this.className = 'forecasttabhistory_select';




      var index = this.getAttribute('historyIndex');
      var list = cardForecastedList[index];
      for(i = 0; i < list.length; i++){
          var card = list[i];
          objForecastTable.appendChild(card);
      }
      if(list.length >= 10){
        objForecastTable.style.height = '770px';
      }else{
        objForecastTable.style.height = '';
      }      
    }
    cur_question_name = 'Set '+cardForecastedList.length;
    spanTab.appendChild(document.createTextNode(cur_question_name))
    var objForecastTable = byId('ForecastTable');
    objForecastTable.appendChild(spanTab);

    var spanList = objForecastTable.getElementsByTagName('span');
    for(var i = 0; i < spanList.length; i++){
        var objSpan = spanList[i];
        objSpan.className = 'forecasttabhistory';
    }
   spanTab.className = 'forecasttabhistory_select';

//Delete any cards from display
    var divList = objForecastTable.getElementsByTagName('div');
    while( divList.length>0){
        var objDiv = divList[0];
        objDiv.parentNode.removeChild(objDiv);
    }


}
function showView200CardInMoz(x,y,objCard){
    if(card_image_path_type.cardprop[imagePathCardset]['path200']==null){
        return;
    }
    //var cardContainer = document.getElementsByTagName('body')[0];
    var cardContainer = byId('PageBody');
    var divCard = document.createElement('div');
    cardContainer.appendChild(divCard);
    divCard.style.position ='absolute';
    divCard.style.left = (x-100)+'px';
    divCard.style.top  = (y-130)+'px';
    divCard.style.width = '200px';
    divCard.style.zIndex = 10;
    divCard.onmouseout= function(){
        this.parentNode.removeChild(this);
    }
    var imgCard = document.createElement('img');
    divCard.appendChild(imgCard);


    imgCard.src = card_image_path_type.cardprop[imagePathCardset]['path200']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
    imgCard.style.width = '200px';
}

function showView200CardInIE(x,y,objCard){
    if(card_image_path_type.cardprop[imagePathCardset]['path200']==null){
        return;
    }
    var cardContainer = byId('ContentContainer');
    var divCard = document.createElement('div');
    cardContainer.appendChild(divCard);
    divCard.style.position ='absolute';
    divCard.style.left = (x-100)+'px';
    divCard.style.top  = (y-130)+'px';
    divCard.style.width = '200px';
    divCard.style.zIndex = 10;
    divCard.onmouseout= function(){
        this.parentNode.removeChild(this);
    }
    var imgCard = document.createElement('img');
    divCard.appendChild(imgCard);


    imgCard.src = card_image_path_type.cardprop[imagePathCardset]['path200']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
    imgCard.style.width = '200px';


}

function showCard10ForecastByMaster(){
  if(cardSelect.length < 10){
      alert('You have to choose other '+(10-cardSelect.length ) +' cards before getting forecast.');
      return;
  }
  keepCardHistory();
  byId('txtFocus').focus();
  var objForecastTable = byId('ForecastTable');
  var cardClassName = 'select10card';
  for(var i = 0; i < cardSelect.length; i++){
      var objCard = cardSelect[i];
      objCard.parentNode.removeChild(objCard);
      var cardClass =  cardClassName + i;
      objCard.setAttribute('SelectPosi',i+1);
      objCard.style.left ='';
      objCard.style.top ='';
      objCard.className = cardClass;

      removeNode(objCard);
      var divLabel = document.createElement('div');
      divLabel.appendChild(document.createTextNode(i+1));
      divLabel.style.height = '10px';
      divLabel.style.textAlign = 'center';
      divLabel.style.marginTop = '-14px';
      divLabel.style.width ='78px';
      divLabel.style.paddingLeft ='4px';
      divLabel.style.color = '#000000';
      objCard.appendChild(divLabel);

      var card_img = document.createElement('img');
      //var img_name = imagePathCardset+objCard.getAttribute('CardName')+'.jpg';
      var img_name = card_image_path_type.cardprop[imagePathCardset]['path']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
      card_img.src = img_name;
      //removeNode(objCard);
      objCard.appendChild(card_img);
      if(isIE){
        objCard.onclick = function(){
            //alert(window.event.x +'  '+window.event.y +'  '+ window.event.offsetY);
            //alert(Event);

            showView200CardInIE(window.event.x,window.event.y,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }else{
        objCard.onclick = function(Event){
            //alert(Event);
            //alert(Event.pageX +'  '+Event.pageY);
            showView200CardInMoz(Event.pageX,Event.pageY,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }
      objForecastTable.appendChild(objCard);
  }
  objForecastTable.style.height = '770px';
    
}

function showCard5ForecastByMaster(){

  if(cardSelect.length < 5){
      alert('You have to choose other '+(5-cardSelect.length ) +' cards before getting forecast.');
      return;
  }
  keepCardHistory();
  byId('txtFocus').focus();
  var objForecastTable = byId('ForecastTable');
  var cardClassName = 'select5card';

  for(var i = 0; i < cardSelect.length; i++){
      var objCard = cardSelect[i];
      objCard.parentNode.removeChild(objCard);
      var cardClass =  cardClassName + i;
      objCard.setAttribute('SelectPosi',i+1);
      objCard.style.left ='';
      objCard.style.top ='';
      objCard.className = cardClass;
      
      removeNode(objCard);
      var divLabel = document.createElement('div');
      divLabel.appendChild(document.createTextNode(i+1));
      divLabel.style.height = '10px';
      divLabel.style.textAlign = 'center';
      divLabel.style.marginTop = '-14px';
      divLabel.style.width ='78px';
      divLabel.style.paddingLeft ='4px';
      divLabel.style.color = '#000000';
      objCard.appendChild(divLabel);

      var card_img = document.createElement('img');
      //var img_name = imagePathCardset+objCard.getAttribute('CardName')+'.jpg';
      var img_name = card_image_path_type.cardprop[imagePathCardset]['path']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
      card_img.src = img_name;
      //removeNode(objCard);
      objCard.appendChild(card_img);

      if(isIE){
        objCard.onclick = function(){
            //alert(window.event.x +'  '+window.event.y +'  '+ window.event.offsetY);
            //alert(Event);

            showView200CardInIE(window.event.x,window.event.y,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }else{
        objCard.onclick = function(Event){
            //alert(Event);
            //alert(Event.pageX +'  '+Event.pageY);
            showView200CardInMoz(Event.pageX,Event.pageY,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }
      objForecastTable.appendChild(objCard);
  }
  objForecastTable.style.height = '';
  
}


function showCard3ForecastByMaster(){

  if(cardSelect.length < 3){
      alert('You have to choose other '+(3-cardSelect.length ) +' cards before getting forecast.');
      return;
  }
  keepCardHistory();
  byId('txtFocus').focus();
  var objForecastTable = byId('ForecastTable');
  var cardClassName = 'select3card';
  for(var i = 0; i < cardSelect.length; i++){
      var objCard = cardSelect[i];
      objCard.parentNode.removeChild(objCard);
      var cardClass =  cardClassName + i;
      objCard.setAttribute('SelectPosi',i+1);
      objCard.style.left ='';
      objCard.style.top ='';
      objCard.className = cardClass;
      
      removeNode(objCard);
      var divLabel = document.createElement('div');
      divLabel.appendChild(document.createTextNode(i+1));
      divLabel.style.height = '10px';
      divLabel.style.textAlign = 'center';
      divLabel.style.marginTop = '-14px';
      divLabel.style.width ='78px';
      divLabel.style.paddingLeft ='4px';
      divLabel.style.color = '#000000';
      objCard.appendChild(divLabel);

      var card_img = document.createElement('img');
      //alert(imagePathCardset);
     // var img_name = imagePathCardset+objCard.getAttribute('CardName')+'.jpg';
      var img_name = card_image_path_type.cardprop[imagePathCardset]['path']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
      card_img.src = img_name;
      //removeNode(objCard);
      objCard.appendChild(card_img);

      if(isIE){
        objCard.onclick = function(){
            //alert(window.event.x +'  '+window.event.y +'  '+ window.event.offsetY);
            //alert(Event);

            showView200CardInIE(window.event.x,window.event.y,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }else{
        objCard.onclick = function(Event){
            //alert(Event);
            //alert(Event.pageX +'  '+Event.pageY);
            showView200CardInMoz(Event.pageX,Event.pageY,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }
      objForecastTable.appendChild(objCard);
  }
  objForecastTable.style.height = '';
  
}

function showCard1ForecastByMaster(){

  if(cardSelect.length < 1){
      alert('You have to choose other '+(1-cardSelect.length ) +' cards before getting forecast.');
      return;
  }
  keepCardHistory();
  byId('txtFocus').focus();
  var objForecastTable = byId('ForecastTable');
  var cardClassName = 'select3card';
  for(var i = 0; i < cardSelect.length; i++){
      var objCard = cardSelect[i];
      objCard.parentNode.removeChild(objCard);
      var cardClass =  cardClassName + '1';
      objCard.setAttribute('SelectPosi',i+1);
      objCard.style.left ='';
      objCard.style.top ='';
      objCard.className = cardClass;

      var card_img = document.createElement('img');
      //var img_name = imagePathCardset+objCard.getAttribute('CardName')+'.jpg';
      var img_name = card_image_path_type.cardprop[imagePathCardset]['path']+ objCard.getAttribute('CardName')+ card_image_path_type.cardprop[imagePathCardset]['type'];
      card_img.src = img_name;
      removeNode(objCard);
      objCard.appendChild(card_img);

      if(isIE){
        objCard.onclick = function(){
            //alert(window.event.x +'  '+window.event.y +'  '+ window.event.offsetY);
            //alert(Event);

            showView200CardInIE(window.event.x,window.event.y,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }else{
        objCard.onclick = function(Event){
            //alert(Event);
            //alert(Event.pageX +'  '+Event.pageY);
            showView200CardInMoz(Event.pageX,Event.pageY,this);
            //alert(this.id+ '      '+ this.getAttribute('CardID')+'  '+ this.getAttribute('CardName') +'  '+ this.getAttribute('SelectPosi'));
        }
      }
      objForecastTable.appendChild(objCard);
  }
  
}

function sendCardMode1(cNum){
//  var loadImage =  showOnLoadPage();
//  loadImage.style.top='2px';
//  loadImage.style.left='710px';
//  byId('MyCardTable').appendChild(loadImage);

  var urlAction = 'storeQuestionCard.php?qName='+cur_question_name;
  urlAction += '&cardNum='+cNum;
  for(var j = 0; j < cardSelect.length; j++){
    var objCard1 = cardSelect[j];
    urlAction += '&c'+(j+1)+'=' + objCard1.getAttribute('CardID');
  }
  


  dojo.io.bind({
	   url: urlAction,
	   handler: callbackSendCardMode1,
	   mimetype: 'text/plain' ,
	   encoding:'utf-8'
	});
}

function callbackSendCardMode1(p1,p2,p3){
    if(p1== 'error'){
      location.replace('Sorry! System can\'t sent data to server&pageback=memberafterlogin.php&msg_type=System Information');
    }else{
      //closeOnloadPage();
    }
}

function connectMaster(objButt){
  var urlAction = 'connectMaster.php';
  //alert(objButt.getAttribute('msvalue'));
  urlAction += '?msid='+objButt.getAttribute('msvalue')+'&msname='+objButt.getAttribute('msname') +'&d='+new Date();
  var loadImage =  showOnLoadPage();//Show Image for
  objButt.parentNode.appendChild(loadImage);
  //alert(objButt.getAttribute('msvalue'));
  dojo.io.bind({
	   url: urlAction,
	   handler: callbackConectMaster,
	   mimetype: 'text/plain' ,
	   encoding:'utf-8'
	});
}

function callbackConectMaster(p1,p2,p3){
    if(p1 == 'error'){
      location.replace('ShowErrorMsg.php?msg=Server Busy Please Try Again Later&pageback=memberafterlogin.php&msg_type=System Information');
    }else{
      callbackConectMaster_(p2);
      closeOnloadPage();
    }
}

function callbackConectMaster_(JSONdata){
    eval(JSONdata);
    if(roomInfo.length == 0){
      location.replace('ShowErrorMsg.php?msg=ขณะนี้ยังไม่มีการตอบรับจากหมอดูที่ท่านเลือก กรุณาโทรติดต่อกับหมอดูก่อน หรืออ่านวิธีการดูดวงสด &pageback=memberafterlogin.php&msg_type=System Information');
      return;
    }
    if(roomInfo[1] == '1'){
//      window.open('forecastbymasterMode1.php','_blank','width=1000,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
        location.replace('forecastbymasterMode1.php');
    }else if(roomInfo[1] == '2'){

    }




}


function receiveCardFromCustomer(){
    var urlAction = 'getCardFromCust.php?d='+new Date();
    dojo.io.bind({
	   url: urlAction,
	   handler: callbackReceiveCardFromCustomer,
	   mimetype: 'text/plain' ,
	   encoding:'utf-8'
	});
}
function callbackReceiveCardFromCustomer(p1,p2,p3){
    if(p1=='error'){
        //location.replace('ShowErrorMsg.php?msg=Try Again Please.&pageback=memberafterlogin.php&msg_type=System Information');
        alert('Connection has problem');
    }else{
        callbackReceiveCardFromCustomer_(p2);
    }
}
function callbackReceiveCardFromCustomer_(JSONdata){
    eval(JSONdata);
}


function closeServiceCustomer(){
  location.replace('closeService.php');
}

function sendRatingCommentToServer(inRate,inComment,inProgress){
    //salert(inComment);
  //return;
  var urlAction = 'receiveRatingComment.php?rating='+inRate+'&comment='+inComment;
  var loadImage =  showOnLoadPage();//Show Image for
   alert(urlAction);
  inProgress.appendChild(loadImage);
  dojo.io.bind({
	   url: urlAction,
	   handler: callbackSendRatingCommentToServer,
	   mimetype: 'text/plain' ,
	   encoding:'utf-8'
	});
}
function callbackSendRatingCommentToServer(p1,p2,p3){
    if(p1=='error'){
        alert('ขออภัย! ไม่สามารถส่งข้อมูลได้ในขณะนี้');
    }else{
      eval(p2);
    }
}


function randomCardMaster(objCardTable,cardAmount){
  try{
    for(var j =0; j< 10; j++){
        var divDesc = byId('cadeP'+j);
        divDesc.style.display = 'none';
    }
  }catch(errMsg){

  }
//  if(cardAmount >0){
//    alert('ทำใจให้สงบ เลือกไพ่มา '+cardAmount +' ใบ หลังจากระบบเรียงไพ่เรียบร้อยแล้ว');
//  }

  byId('ForecastTable').style.height = '';


  cur_cardTotal = 78;
  cardset = new Array();
  cardSelect = new Array();
  cardForecastedList = new Array();
  forecastCard = cardAmount;
  timer = 0;
  var cardIDRandomSet = randomDeal(78,78);

  var marginleft = 5;
  var marginTop = 300;
  removeNode(objCardTable);
  removeNode(byId('ForecastTable'));

  for(var i = 1; i <= 78; i++){
     var objCard = document.createElement('div');// Create Card
     objCard.id='P'+i;
     objCard.className = 'backcard';//Set backcard

     objCard.onclick = function(){  //When card is clicked.
         if(forecastCard == 0){
             //alert('กรุณากดปุ่มเพื่อเลือกจำนวนไพ่ที่ต้องการเลือก');
             showDialog(250,388,'กรุณากดปุ่มเพื่อเลือกจำนวนไพ่ที่ต้องการเลือก');
             return;
         }else if(cardSelect.length < forecastCard){
             var isDup = false;
             for(var j = 0; j < cardSelect.length; j++){
                 var objCardTemp = cardSelect[j];
                 if(objCardTemp.getAttribute('CardID') == this.getAttribute('CardID')){
                     //alert('ไพ่ใบนี้ได้ถูกเลือกแล้ว');
                     showDialog(250,388,'ไพ่ใบนี้ได้ถูกเลือกแล้ว');
                     isDup = true;
                     break;
                 }
             }
             if(!isDup)
              cardSelect[cardSelect.length] = this;//store the card that is selected by customer.

             if(cardSelect.length ==forecastCard) {
                 //alert('คุณเลือกไพ่ครบ '+forecastCard+' แล้ว\nให้กดอ่านคำทำนายจากเมนูขวาสุด');
                 showDialog(250,388,'คุณเลือกไพ่ครบ '+forecastCard+' แล้ว\nให้กด \"ส่งไพ่ให้หมอดู\" จากเมนูขวาสุด');
             }


         }else {
             //alert('จำนวนไพ่ครบ '+forecastCard +' ใบแล้ว');
             showDialog(250,388,'จำนวนไพ่ครบ '+forecastCard +' ใบแล้ว');
             return;
         }

         this.className = 'selectbackcard';
         var x = this.getAttribute('posiX');
         this.style.left = x+'px';
         var y = this.getAttribute('posiY');
         this.style.top = (parseInt(y)-20)+'px';
         this.style.border = 'solid 1px #FFFFFF';

         this.onmouseover = function(){}
         this.onmouseout = function(){}
     }
     objCard.onmouseover = function(){
      var x = this.getAttribute('posiX');
      var y = this.getAttribute('posiY');
          x = x-3;
          y = y-3;
      this.style.left = x+'px';
      this.style.top = y+'px';
      this.style.border = 'solid 1px #d2eb5e';
     }
     objCard.onmouseout = function(){
      var x = this.getAttribute('posiX');
      var y = this.getAttribute('posiY');
      this.style.left = x+'px';
      this.style.top = y+'px';
      this.style.border = 'solid 1px #FFFFFF';
     }

     var cardId = cardIDRandomSet[i-1];

     objCard.setAttribute('posiX',marginleft);
     objCard.setAttribute('posiY',marginTop);
     objCard.setAttribute('CardID',parseInt(cardId)-1 );
     objCard.setAttribute('CardName',CardList[parseInt(cardId)-1]);

     objCard.style.left ='669px';//Set Default position of the card

     objCardTable.appendChild(objCard);

     if(i<=5){
         marginTop -= 15;
         marginleft += 8;
         objCard.style.top= '300px';
     }else if(i>5 && i<=13){
         marginTop -= 10;
         marginleft += 8;
         objCard.style.top= '302px';
     }else if(i>13 && i<=26 ){
         marginTop -= 5;
         marginleft += 8;
         objCard.style.top= '304px';
     }else if(i>26 && i<29){//
         marginTop -= 3;
         marginleft += 10;
         objCard.style.top= '306px';
     }else if(i>=29 && i<39){
         marginTop -= 0;
         marginleft += 10;
         objCard.style.top= '308px';
     }else if(i>=39 && i<49){
         marginTop -= 0;
         marginleft += 10;
         objCard.style.top= '310px';
     }else if(i>=49 && i<=52){
         marginTop += 3;
         marginleft += 10;
         objCard.style.top= '312px';
     }else if(i>52 && i<65){
         marginTop += 5;
         marginleft += 8;
         objCard.style.top= '314px';
     }else if(i >= 65 && i<=73){
         marginTop += 10;
         marginleft += 8;
         objCard.style.top= '316px';
     }else if(i>=73){
         marginTop += 16;
         marginleft += 8;
         objCard.style.top= '318px';
     }
     cardset[cardset.length] = objCard;
     objCardTable.appendChild(objCard);

   }


   iTimeoutId = setTimeout(cardSilde,150);

}


