// JavaScript Document

var client = new Object();

client.dispCdpChildAgeBoxes = function(numChild)
{
    // random children allowed for hotel and holiday booking
    var totNum = document.getElementsByName("cdpRm1ChildAge").length;
	
    document.getElementById("ChildAgeRow").style.display = numChild > 0 ? "" : "none";
    for (i=1; i<=numChild; i++)
        document.getElementById("cdpRm1ChildAge" + i).style.display = "";
    for (; i<=totNum; i++)
        document.getElementById("cdpRm1ChildAge" + i).style.display = "none";
    
}

client.checkAdultInfantNo = function(inAdultId, inChildId, inAgeId)
{
	var i, child_age, number_infant = 0;
	var adults = document.getElementById(inAdultId).value;
	var children = document.getElementById(inChildId).value;

	for(i = 1; i <= children; i++)
	{
		child_age = document.getElementById(inAgeId + i).value;
		if (child_age == 1)
			number_infant++;
	}
	
	if (adults < number_infant)
		return(false);
	return(true);
}


client.setErrorMsg = function(inDivId, inErrorMsg)
{
    var errorDiv = document.getElementById(inDivId);
    errorDiv.innerHTML = inErrorMsg;
    errorDiv.style.display = "";
    return(false);
}

client.clearErrorMsg = function(inDivId)
{
    var errorDiv = document.getElementById(inDivId);
    errorDiv.innerHTML = "";
    errorDiv.style.display = "none";
}

// Image library support
client.showImage = function(inNumber)
{
  if (inNumber < 0 || (inNumber >= imageCount)) return; // Shouldnt happen

  var elImage = document.getElementById('image');
  if (elImage == null) return;
  
  var elText = document.getElementById('imageLabel');
  if (elText == null) return;
  
  document.getElementById('hotelImages').style.display="";
  
  elImage.src = images[inNumber];
  elImage.alt = imageLabels[inNumber];
//  elText.firstChild.nodeValue = imageLabels[inNumber];
  
  currentImage=inNumber;  
  if (inNumber > 0 )
  {
     document.getElementById('previous').style.display="";
  } else
  {
    document.getElementById('previous').style.display="none";
  }
    if ((inNumber + 1) < imageCount)
  {
     document.getElementById('next').style.display="";
  } else
  {
    document.getElementById('next').style.display="none";
  }
   
}

client.verifySupubCdp = function(arg_form, inDepDate, inRetDate)
{	
	var minDuration = 1;
	var maxDuration = 14;
	var minTotPax = 1;
	var maxTotPax = 6;

	if(client.verifySimpleCdp(arg_form, minTotPax, maxTotPax) && client.calculateAndVerifyDuration(inDepDate, inRetDate, minDuration, maxDuration))
		return(true);

	return(false);

}

client.verifySimpleCdp = function(arg_form, minTotPax, maxTotPax)
{
    lclMsg = "";
    totalPassengers =parseInt (arg_form.child_1.value) +  parseInt (arg_form.adult_1.value);
    
    if (( totalPassengers < minTotPax) || ( totalPassengers > maxTotPax))
    {
       lclMsg += "- Selected a minimum of " + minTotPax + " and a maximum " + maxTotPax + " passengers (incl. children)\n";         
    }
    
    if(lclMsg!="")
    {
        lclMsg = "Please ensure you have...\n" + lclMsg;
        client.setErrorMsg("roomMixError", lclMsg);
        return(false);
    }

    var adult_count = arg_form.adult_1.value;
    var child_count = arg_form.child_1.value;
    var room_mixes = adult_count + "A";
    if (child_count > 0)
    {
        room_mixes += child_count + "C";
        for (i = 1; i <= child_count; i++)
            room_mixes += "-" + document.getElementById("cdpRm1ChildAge" + i).value;
    }          

    document.getElementById("room_mixes").value = room_mixes;   

    return(true);
}

client.calculateAndVerifyDuration = function(inDepDate, inRetDate, inMinDuration, inMaxDuration)
{
	var retDateValue = document.getElementById(inRetDate).value;
	
	if (retDateValue == '' || retDateValue.length == 0)
    {
		client.setErrorMsg("returningDateError", "Enter a returning date");
        return(false);
    }
	
	var depDate = new Date(convertDate(document.getElementById(inDepDate).value)); 
	var retDate = new Date(convertDate(retDateValue));
	
    var duration = depDate.getDaysTo(retDate);
    
    if (duration < 1)
    {
    	client.setErrorMsg("returningDateError", "Return date must be after the departing date");
        return(false);
    }
    else if (duration < inMinDuration)
    {
    	client.setErrorMsg("returningDateError", "Return date cannot be less than the minimum duration of " + inMinDuration);
        return(false);
    }
    else if (duration > inMaxDuration)
    {
    	client.setErrorMsg("returningDateError", "Return date cannot be more than the maximum duration of " + inMaxDuration);
        return(false);
    }
    
    document.getElementById("duration").value = duration;
    
    return(true);
}
