arAddID = new Array();

function Calc(room_id, itype_num, ipeople, iFreePlaceId, val, id, stat, change, rid){
  if (itype_num == 1 && stat == 0){
    Warning(room_id, val);
    }
    
  bErr = false;
  val = parseInt(val);  
  setV(id,val);
  
  if (isNaN(val)){
    setV(id,'');
    val = 0; 
    }
  else if (val < 0){
    setV(id,hFreePlace);
    val = hFreePlace;    
    }
    
  if (stat == 0){
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3', 0);
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7', 0);
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12', 0);  
    }
    
	hFreePlace = getV('hCntPlace_'+room_id+'_'+itype_num);
	cntNum = getV('cntPeople_'+room_id+'['+ipeople+']');
	if (cntNum < 0 && stat == 0){
	  val = 0;
	  setV('cntNum_'+room_id, val);
	  bErr = true;
   }
   
	to3 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3') * 1);
	from4to7 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7') * 1);
	from8to12 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12') * 1);
	var cnt_child = 0;
	cnt_child = to3 + from4to7 + from8to12;
	
	if ((val < 0 || isNaN(val))&& stat == 1){
	  setV(id,0);
	  bErr = true;
    }	
    	
	if (cntNum <= cnt_child && stat==1){
		alert('Детей должно быть меньше общего кол-во человек');
	  setV(id,0);
	  to3 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3') * 1);
	  from4to7 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7') * 1);
	  from8to12 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12') * 1);	  
    }

	sw = GetCountDay();
	
	arDay = sw.split(':');
	iBudni = arDay[0];
	iVih = arDay[1];
	/*
  if (is_fix[room_id]) {
    iBudni = 1;
    iVih = 0;
    }
  */
	//alert(iBudni+'_'+iVih);
	if (hFreePlace < val && stat==0){
      if (hFreePlace == 1){
	      alert('Максимум можно разместить одного');
	      setV(id,1);
	      val = hFreePlace;  
	      }
      else {
        alert('Максимум можно разместить '+hFreePlace+'');
        setV(id,hFreePlace);
        val = hFreePlace;        
        }
	    }
	halfDay = document.getElementById('isHalfDay').value;
  if (!bErr){
    iSum = 0;
    if (stat == 0){
	    iPriceB = getV('price_'+room_id+'_'+val+'b');
	    iPriceV = getV('price_'+room_id+'_'+val+'v');
	    iPriceRB = iPriceB;
	    iPriceRV = iPriceV;
	    //alert(iPriceRB+'_'+iPriceRV);
	    sTime = getV('sStartTime');
	    eTime = getV('sEndTime');
	    
	    if (halfDay == 'Y'){
	      if (sTime == '18.00' && (eTime == '11.00' || eTime == 'до 11.00')){
          iPriceRB = 150; 
          iPriceRV = 150;	      
          }
	      else{
          iPriceRB = (iPriceRB - 400) / 2; 
          iPriceRV = (iPriceRV - 400) / 2;
          }	    
        }
	    }
	  else if(stat == 1) {
      cntPeople = getV('cntPeople_'+room_id+'['+ipeople+']');
      //alert(to3);
      val = cntPeople - to3;      
      //alert(val);
      //alert('price_'+room_id+'_'+val+'b');
      iPriceB = getV('price_'+room_id+'_'+val+'b');
      iPriceV = getV('price_'+room_id+'_'+val+'v');
      
      
      iSumma1B = iPriceB	/ val * from4to7 * 0.85;
      iSumma1V = iPriceV	/ val * from4to7 * 0.85;
    
      iSumma2B = iPriceB	/ val * from8to12 * 0.9;
      iSumma2V = iPriceV	/ val * from8to12 * 0.9;
      
      
      val2 = val - (from4to7 + from8to12);
      
      iSumma3B = iPriceB / val * val2;
      iSumma3V = iPriceV / val * val2;
      
      //alert(iSumma1V);
      //alert(iSumma2V);
      //alert(iSumma3V);
      
      if (iSumma1B == 0 && iSumma2B == 0)
        iSumma4B = getV('price_'+room_id+'_'+val+'b');
      else
        iSumma4B = iSumma1B + iSumma2B + iSumma3B;
        
      if (iSumma1V == 0 && iSumma2V == 0)
        iSumma4V = getV('price_'+room_id+'_'+val+'v');
      else
        iSumma4V = iSumma1V + iSumma2V + iSumma3V; 
      
      iPriceRB = iSumma4B;
      iPriceRV = iSumma4V;



	    if (halfDay == 'Y'){
        iPriceRB = (iPriceRB - 400) / 2; 
        iPriceRV = (iPriceRV - 400) / 2;	    
        }       
      }
      
      ResultSum = iBudni * iPriceRB + iVih * iPriceRV;
      //alert(iBudni+'*'+iPriceRB+'+'+iVih+'*'+iPriceRV);
      //alert('result = '+ResultSum);
      //if (stat == 1)
      //  ResultSum = ResultSum - (2000 * to3);
      ResultSum = Math.ceil(ResultSum.toFixed(2));
	    setV('hPriceNum_'+iFreePlaceId, ResultSum);
	    setHtml('span_PriceNum_'+iFreePlaceId, ResultSum);
	    cntFreePlace = getV('cntNum_'+room_id);

	    iPreSum = 0;
	    start_j = 0;

      iFreeCnt = getV('hFreePlace_'+room_id);
	    for (j=0;j<iFreeCnt;j++){
	      FreeID = getV('hFreePlaceID_'+room_id+'['+j+']');
	      iPreSum += getV('hPriceNum_'+FreeID)* 1;
        }
      setV('hNumSum_'+room_id, iPreSum);
      setHtml('span_NumSum_'+room_id, iPreSum);
      iPreSum = 0;
	      for (i=1;i<=arRoomId.length;i++){
	        try {
			    for (j=0;j<arRoomId[i].length;j++){
			      iPreSum += getV('hNumSum_'+arRoomId[i][j]) * 1;
			      } 
			      
			    }
			    catch (e) {}
			}
			iSum = iPreSum;
	  }
  setHtml('sRPrice',iSum);
  setV('hRPrice',iSum);	
}

function Calc2(room_id, itype_num, ipeople, iFreePlaceId, val, id, stat){
  if (itype_num == 1 && stat == 0){
    Warning(room_id, val);
    }
  arCntRoomId = new Array();
  arCntRoomId[11]=1;
  arCntRoomId[12]=1;
  arCntRoomId[13]=1;
  arCntRoomId[14]=1;
  arCntRoomId[15]=32;
  arCntRoomId[16]=8;
  arCntRoomId[17]=8;
  arCntRoomId[18]=8;
  arCntRoomId[19]=4;
  arCntRoomId[23]=4;
  
  halfDay = getV('isHalfDay');
  bErr = false;  
  val = parseInt(val);
  
  if (stat == 0){
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3', 0);
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7', 0);
    setV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12', 0);  
    }
  
  setV(id,val);
  if (isNaN(val)){
    setV(id,'');
    val = 0; 
    }
  else if (val < 0){
    setV(id,hFreePlace);
    val = hFreePlace;    
    }
  
	hFreePlace = getV('hCntPlace_'+room_id+'_'+itype_num);
	cntNum = getV('cntPeople_'+room_id+'['+ipeople+']');
	
	
	if (cntNum < 0 && stat == 0){
	  val = 0;
	  setV('cntNum_'+room_id, val);
	  bErr = true;
   }
	to3 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3') * 1);
	from4to7 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7') * 1);
	from8to12 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12') * 1);
	var cnt_child = 0;
	cnt_child = to3 + from4to7 + from8to12;
	if ((val < 0 || isNaN(val))&& stat == 1){
	  setV(id,0);
	  bErr = true;
    }		
	if (cntNum <= cnt_child && stat==1){
		alert('Детей должно быть меньше общего кол-во человек');
	  setV(id,0);
	  to3 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'to3') * 1);
	  from4to7 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from4to7') * 1);
	  from8to12 = parseInt(getV('child'+room_id+'_'+itype_num+'_'+iFreePlaceId+'from8to12') * 1);
    }

	sw = GetCountDay();
	arDay = sw.split(':');
	iBudni = arDay[0];
	iVih = arDay[1];
	
	
	if (hFreePlace < val && stat==0){
      if (hFreePlace == 1){
	      alert('Максимум можно разместить одного');
	      setV(id,1);
	      val = hFreePlace;
	      }
      else {
        alert('Максимум можно разместить '+hFreePlace+'');
        setV(id,hFreePlace);
        val = hFreePlace;
        }
	    }

  if (!bErr){
    iSum = 0;
    if (stat == 0){
	    iPriceB = getV('price_'+room_id+'_'+val+'b');
	    iPriceV = getV('price_'+room_id+'_'+val+'v');
	    iPriceRB = iPriceB;
	    iPriceRV = iPriceV;	    
	    sTime = getV('sStartTime');
	    eTime = getV('sEndTime');	    
	    if (halfDay == 'Y'){
	      if (sTime == '18.00' && (eTime == '11.00' || eTime == 'до 11.00')){
          iPriceRB = 150; 
          iPriceRV = 150;	      
          }
	      else{
          iPriceRB = (iPriceRB - 400) / 2; 
          iPriceRV = (iPriceRV - 400) / 2;
          }	    
        } 	    
	    }
	  else if(stat == 1) {
      cntPeople = getV('cntPeople_'+room_id+'['+ipeople+']');
      val = cntPeople - to3;
      
      iPriceB = getV('price_'+room_id+'_'+val+'b');
      iPriceV = getV('price_'+room_id+'_'+val+'v');
      

      iSumma1B = iPriceB	/ val * from4to7 * 0.85;
      iSumma1V = iPriceV	/ val * from4to7 * 0.85;
    
      iSumma2B = iPriceB	/ val * from8to12 * 0.9;
      iSumma2V = iPriceV	/ val * from8to12 * 0.9;
      
      val2 = val - (from4to7 + from8to12);
      
      iSumma3B = iPriceB / val * val2;
      iSumma3V = iPriceV / val * val2;
      
      if (iSumma1B == 0 && iSumma2B == 0)
        iSumma4B = getV('price_'+room_id+'_'+val+'b');
      else
        iSumma4B = iSumma1B + iSumma2B + iSumma3B;
        
      if (iSumma1V == 0 && iSumma2V == 0)
        iSumma4V = getV('price_'+room_id+'_'+val+'v');
      else
        iSumma4V = iSumma1V + iSumma2V + iSumma3V; 
      
      iPriceRB = iSumma4B;
      iPriceRV = iSumma4V;   
      
	    if (halfDay == 'Y'){
        iPriceRB = (iPriceRB - 400) / 2; 
        iPriceRV = (iPriceRV - 400) / 2;	    
        }         	  
      }
      
      ResultSum = iBudni * iPriceRB + iVih * iPriceRV;
      ResultSum = ResultSum.toFixed(2);
	    setV('hPriceNum_'+iFreePlaceId, ResultSum);
	    setHtml('span_PriceNum_'+iFreePlaceId, ResultSum);
	    
	    cntFreePlace = getV('cntNum_'+room_id);
	    cntFreePlace = arCntRoomId[room_id];
	    iPreSum = 0;
	    for (j=0;j<cntFreePlace;j++){
	      FreeID = getV('hFreePlaceID_'+room_id+'['+j+']');
	      iPreSum += getV('hPriceNum_'+FreeID)* 1;
        }
        //alert('iPreSum='+iPreSum);
      setV('hNumSum_'+room_id, iPreSum);
      setHtml('span_NumSum_'+room_id, iPreSum);
      iPreSum = 0;
	      for (i=1;i<=arRoomId.length;i++){
	        try {
			    for (j=0;j<arRoomId[i].length;j++){
			      iPreSum += getV('hNumSum_'+arRoomId[i][j]) * 1;
			      } 
			      
			    }
			    catch (e) {}
			}
			iSum = iPreSum;
	  }
  setHtml('sRPrice',iSum);
  setV('hRPrice',iSum);	
}

function getV(id){
  try {
    v = document.getElementById(id).value
  }
  catch (e) { v = 0}
  return v;
}

function getV2(id){
  try {
    v = document.getElementById(id).value
    }
  catch (e) { v = 0}
  return v;
}

function setV(id, v){
  try {
    obj = document.getElementById(id);
    obj.value = v;
    }
  catch (e) { obj.value = 0}
}

function getChBox(id){
  v = document.getElementById(id).checked
  return v;
}

function getE(id){
  return document.getElementById(id);
}

function check(v){
  if (v.length > 0) return true;
	else false;
}

function setHtml(id, v){
	document.getElementById(id).innerHTML = v;
}
function Warning(room_id, val){
  switch(room_id){
    case '11':
      if (val >= 1 && val < 8)
        alert('Расчет производится из кол-ва 8 человек');
      break;
    case '12':
      if (val >= 1 && val < 8)
        alert('Расчет производится из кол-ва 8 человек');    
      break;
    case '13':
      if (val >= 1 && val < 3)
        alert('Расчет производится из кол-ва 3 человек');    
      break;
    case '14':
      if (val == 1)
        alert('Расчет производится из кол-ва 2 человек');
      else if (val == 3)
        alert('Расчет производится из кол-ва 4 человек');            
      break;                
    }
}
