// controls the display/hide of the child ages according to how many children
function displayChildAgeBoxes(numChild)
{
	if (document.getElementById("book")!=null){
		// random children allowed for hotel and holiday booking
		var chdds = document.getElementById("book").getElementsByTagName("dd");
		totNum = chdds.length;
	} else {
		//fixed number for site main search box hotel booking
		totNum = 4;
	}
	document.getElementById("ChildAgeRow").style.display = numChild > 0 ? "" : "none";
	for (i=1; i<=numChild; i++)
		document.getElementById("rm1ChildAge" + i).style.display = "";
	for (; i<=totNum; i++)
		document.getElementById("rm1ChildAge" + i).style.display = "none";
	
}

function QBCarSetParams()
{
	// For car quickbook, just copy over the duration

	document.getElementById("carAuto").value="dur=" + document.getElementById("carDuration").value;
	setLocalWebBook("QBCarForm");
						
}

// Copies child ages over from the drop downs to the webbook child age params.  Clears out any unspecified child ages
function QBHotelSetParams(numChild)
{
	// For hotel quickbook, use auto to pass through rating and duration
	var autoString;
	autoString = "dur=" + document.getElementById("duration").value;
	autoString = autoString + "#rating=" + document.getElementById("rating").value;
	document.getElementById("auto").value=autoString;
									
    for (i=1; i<=numChild; i++)
	  document.getElementById("par_child_1_" + i).value = document.getElementById("rm1ChildAge" + i).value;
	for (; i<=4; i++)
	  document.getElementById("par_child_1_" + i).value = "";
	  
	setLocalWebBook("QBHotelForm"); 

}

function verify(arg_form)
{	
    var lclMsg = verifyWBCommon(arg_form);
     	
    if(lclMsg!="")
    {
    	lclMsg = "Please ensure you have...\n" + lclMsg;
    	alert(lclMsg);
    	return false;
    }

	//Detect IE5.5
	version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1)
    {
	   temp=navigator.appVersion.split("MSIE")
       version=parseFloat(temp[1])
	}
	//if browser not ie5.5
	if (version != 5.5) 
    {
		wbSupub = window.open('','wbSupub','scrollbars=yes,resizable=1,width=790,height=480');
		document.params.submit();	
	}
	
	return true;

}

function verifySameWindow(arg_form)
{
    var lclMsg = verifyWBCommon(arg_form);
    
    if(lclMsg!="")
    {
        lclMsg = "Please ensure you have...\n" + lclMsg;
        alert(lclMsg);
        return false;
    }
    
    return(true);
}


function verifyWBCommon(arg_form)
{
    var 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 (minDuration!=0)
    {
        
        selectedStay = parseInt (arg_form.duration.value); 

        
        if (selectedStay < minDuration )
        {
            lclMsg += "- The minimum number of nights allowed is " + minDuration +" \n";    
            
        }
        if (selectedStay > maxDuration)
        {
            lclMsg += "- The maximum number of nights allowed is " + maxDuration +" \n";        
        }
        
        //test if number is entered in number of nights.
        testVar = isNaN(selectedStay);
        if (testVar == true){
            lclMsg += "- Please enter a valid number for nights \n";    
        }
    }
    return(lclMsg)
}

function verifyAgweb(numChild, maxChildren, arg_form, testDuration)
{
    lclMsg = "";       

    
    totalPassengers = parseInt (arg_form["rooming.room1.childCount"].value) +  parseInt (arg_form["rooming.room1.adultCount"].value);
    
    if (testDuration == 'true' && minDuration != 0)
    {
        
        selectedStay = parseInt (arg_form.duration_ag.value); 

        
        if (selectedStay < minDuration )
        {
            lclMsg += "- The minimum number of nights allowed is " + minDuration +" \n";    
            
        }
        if (selectedStay > maxDuration)
        {
            lclMsg += "- The maximum number of nights allowed is " + maxDuration +" \n";        
        }
        
        //test if number is entered in number of nights.
        testVar = isNaN(selectedStay);
        if (testVar == true){
            lclMsg += "- Please enter a valid number for nights \n";    
        }
    }    
    
    if (( totalPassengers < minTotPax) || ( totalPassengers > maxTotPax))
    {
        lclMsg += "- Selected a minimum of " + minTotPax +" and a maximum "+ maxTotPax +" passengers (incl. children)\n";         
    }
    
    if (arg_form["loginId"].value.length == 0)
    {
        lclMsg += "- Must specify valid agency code\n";
    }
    
    if (arg_form["accessCode"].value.length == 0)
    {
        lclMsg += "- Must specify valid agency password\n";
    }
    
    if(lclMsg != "")
    {
        lclMsg = "Please ensure you have...\n" + lclMsg;
        alert(lclMsg);
        return false;
    }
    else
    {
        //Detect IE5.5
        version=0
        if (navigator.appVersion.indexOf("MSIE")!=-1)
        {
            temp=navigator.appVersion.split("MSIE")
            version=parseFloat(temp[1])
        }
        //if browser not ie5.5
        if (version != 5.5) 
        {
            wbSupub = window.open('','agwebBkg','scrollbars=yes,resizable=1,width=790,height=480');
            document.paramsAg.submit();   
        }
        
        return true;
    } 
}

function displayChildAgeBoxesAgweb(numChild)
{
    if (document.getElementById("agweb")!=null)
    {
        // random children allowed for hotel and holiday booking
        var chdds = document.getElementById("agweb").getElementsByTagName("dd");
        totNum = chdds.length;
    } 
    else 
    {
        //fixed number for site main search box hotel booking
        totNum = 4;
    }
    document.getElementById("agChildAgeRow").style.display = numChild > 0 ? "" : "none";

    for (i=1; i<=numChild; i++)
        document.getElementById("agRm1ChildAge" + i).style.display = "";
    for (; i<=totNum; i++)
          document.getElementById("agRm1ChildAge" + i).style.display = "none";
    
}

function AgWebSetParams(numChild, maxChildren)
{
    for (i=1; i<=numChild; i++)
        document.getElementById("ag_par_child_1_" + i).value = document.getElementById("agRm1ChildAge" + i).value;
    for (; i<=maxChildren; i++)
        document.getElementById("ag_par_child_1_" + i).value = "";  
}

function SupBookSetParams(numChild,maxChildren)
{
    // For supplier publisher based booking
    var autoString;
    autoString = "dur=" + document.getElementById("duration").value;
	document.getElementById("auto").value=autoString + "#" + document.getElementById("param_str").value;
    for (i=1; i<=numChild; i++)
        document.getElementById("par_child_1_" + i).value = document.getElementById("rm1ChildAge" + i).value;

    for (; i<=maxChildren; i++)
        document.getElementById("par_child_1_" + i).value = "";
}

// Override the destination city with the value contained in the element with id inDestSelectParam
function overrideDestCity(inDestSelectParam)
{
	if (document.getElementById(inDestSelectParam) && document.getElementById(inDestSelectParam).length > 0)
		document.getElementById('dep_city').value = document.getElementById(inDestSelectParam).value;
}
		
function setLocalWebBook(frmId)
{
    // changes the submit action for clienttest and www-ch originated pages (lets us test the booking process with TT)
    
    var frm=document.getElementById(frmId);
    if (frm == null)  return;
    
    /*
    thisPage=window.location.href;
    thisPage=thisPage.substring(thisPage.lastIndexOf('://')+3,thisPage.length);
    thisPage=thisPage.substring(0,thisPage.indexOf('/'));
    alert(thisPage + "a - " + thisPage.indexOf('clienttest') + " b - " + thisPage.indexOf('devhost'));
    if ((thisPage.indexOf('clienttest') != 0) && (thisPage.indexOf('devhost') != 0))
    {
         frm.action="https://calypso.net.au/apps/webbook/explore/engine";
    } */
    
}

