// Used to display popup windows...
function popup(u,n,w,h,s) {
    var l = (screen.width)  ? (screen.width  - w)/2 : 0;
    var t = (screen.height) ? (screen.height - h)/2 : 0;
    var p = window.open(u,n,'width='+w+',height='+h+',scrollbars='+s+',left='+l+',top='+t+',resizable');
    p.focus();
}

function nav(p,s) {
  h=s?"_h":"";l=s?4:6;p.src=p.src.substr(0,p.src.length-l)+h+".gif";
  }

function selectAll(fname,lnk) {

    f = document.forms[fname];
    for(i=0;i<f.elements['booking_ref[]'].length;i++) {
        f.elements['booking_ref[]'][i].checked = true;
    }

}

function set_comp_correct(t) {

    f = t.form;
    a = t.name.substr(t.name.length-1,1);
    d = t.value ? false : true;
    c = t.value ? true : false;

    for(i=0;i<f.correctanswer.length;i++) {
      if(f.correctanswer[i].value == a) { 
        if(d) f.correctanswer[i].checked = c;
        f.correctanswer[i].disabled = d;
      }
    }

}

function setBookingForm(mode,f) {

    f.elements['mode'].value = mode;

    if(mode == 'print') {
        f.target = 'print_win';
        var w = window.open('','print_win','width=650,height=400,scrollbars=1,resizable=1');
    }
    else {
        f.target = '_self';
    }

    f.submit();

}

// Used to fire the progress form on the bounce pages...
function fireBounceForm() {

    // Fire form...
    ourForm = document.getElementById("bounceform");
    ourForm.submit();

    showBounceAnim();

    return true;
}


// Used to display the progress anim on the bounce pages...
function showBounceAnim() {

    // Update animation...
    document.getElementById("progress").src = "/images/bounce_loader.gif";

    return true;
}


// Used to update the quick search form...
function updateQuickSearchForm() {

    ourForm = document.getElementById('fabquicksearchform');

    airportSelect = document.getElementById("airportselect")[document.getElementById("airportselect").selectedIndex].value;

    if (airportSelect == "") {
        document.getElementById("airportselect").style.backgroundColor = 'FFD3D3';
        alert('You must select a departure airport.');        
        return false;
    }
    else {
        hideDiv('submitdiv');
        showDiv('pleasewait');
    }

    return true;
}

// Used to handle the update the action (ie: destination) of the holiday search form...
function updateSearchFormAction() {

    ourForm = document.getElementById('fabsearchform');


    // Check that we can submit...
    canSubmit = false;
    selectedDestination = document.getElementById("locationselect")[document.getElementById("locationselect").selectedIndex].value;

    
    if (ourForm.type[0].checked == "1") {
        // Holiday...

        selectedAirport = document.getElementById("airportselect")[document.getElementById("airportselect").selectedIndex].value;

        if (selectedAirport == "" || selectedDestination == "") {
            missingelements = '';
            if (selectedAirport == "") { document.getElementById("airportselect").style.backgroundColor = 'FFD3D3'; missingelements += 'departure airport'}
            if (selectedDestination == "") {
                document.getElementById("locationselect").style.backgroundColor = 'FFD3D3';
                if (missingelements != "") missingelements += ' and a destination';
                else missingelements += 'destination';
            }
            alert('You must select a '+missingelements+'.');
            return false;      
        }
        else {
            hideDiv('submitdiv');
            showDiv('pleasewait');
        }
    }
    else {    
        // Accommodation only...
        if(selectedDestination == "") {
            document.getElementById("locationselect").style.backgroundColor = 'FFD3D3';
            alert('You must select a destination.');                    
        
            return false;
        }
    }

    if (ourForm.mode.value == "execute") {
        ourForm.target = "_parent";
        ourForm.action = "/loader/search/search.php";
    }

        
    return true;    
}


// Used to handle the update of the holiday search form...
function updateSearchType() {

    ourForm = document.getElementById('fabsearchform');
    ourForm.mode.value = "reload";
    ourForm.submit();
    
}


// Used to handle the update of the holiday search form...
function updateSearchForm(ourForm, trigger) {
    ourForm.mode.value = "reload";
    ourForm.reloadtrigger.value = trigger;
    ourForm.submit();
}




// Used to hide show the num rooms selector based on the num adults selector...
function updateAvailabilityRooms() {
    numAdults = document.getElementById("numadults")[document.getElementById("numadults").selectedIndex].value;

    if (numAdults > 2) {
        showDiv('roomtitle');
        showDiv('roomselector');
    }
    else {
        hideDiv('roomtitle');
        hideDiv('roomselector');
    }
    
    return true;
}


// Used to see if the availability form can be submitted...
function scanAvailabilityForm() {

    canSubmit = false;

    ourForm = document.getElementById('availabilitycheck');

    // Check the current itinerary selection...
    if (ourForm.itindbid.length > 1) {
        for (loop=0; loop<ourForm.itindbid.length; loop++) {
            if(ourForm.itindbid[loop].checked == "1") canSubmit = true;
        }
    }
    else canSubmit = true;
    
    // Update submit visibility...
    if (canSubmit == true) { showDiv('submitbutton'); hideDiv('nosubmit'); }
    else { hideDiv('submitbutton'); showDiv('nosubmit'); }

    //window.status = 'scanAvailabilityForm called - canSubmit: '+canSubmit;

    return;
}


// Used to update the shown divs within the availability form...
function updateAvailabilityForm() {

    // Display the appropriate flights div (hiding all others)...
    ourForm = document.getElementById('availabilitycheck');
    for (loop=0; loop<ourForm.availnights.length; loop++) {
        if (ourForm.availnights[loop].checked == "1") { showDiv('nightoptions_'+ourForm.availnights[loop].value); } 
        else { hideDiv('nightoptions_'+ourForm.availnights[loop].value);  }
    }
    
    // Reset the current itinerary selection...
    for (loop=0; loop<ourForm.itindbid.length; loop++) { ourForm.itindbid[loop].checked=false; }
    
    scanAvailabilityForm();
    
    return true;            
}


// Used to handle the update of the holiday search form...
function updateAvailabilityPartyForm() {

    numChildren = document.getElementById("numchildren")[document.getElementById("numchildren").selectedIndex].value;

    if (numChildren >= 1) showDiv('childagesdiv');
    else hideDiv('childagesdiv');
    
    for (loop=0; loop<6; loop++) {
        if (loop<numChildren) showDiv('childagecell_'+loop);
        else hideDiv('childagecell_'+loop);        
    }
 
    scanAvailabilityForm(); 
    
    return true;       
}


// Used to handle the update of the holiday search form...
function checkAvailabilityPartyForm() {

    numAdults = document.getElementById("numadults")[document.getElementById("numadults").selectedIndex].value;
    numChildren = document.getElementById("numchildren")[document.getElementById("numchildren").selectedIndex].value;
    numInfants = document.getElementById("numinfants")[document.getElementById("numinfants").selectedIndex].value;
    
    total = parseInt(numAdults, 10) + parseInt(numChildren, 10) + parseInt(numInfants, 10);
    
    if (total <= 9 ) {
        showDiv('submitted');
        hideDiv('submitbutton');
        return true;       
    }
    else {
        alert('The maximum party size for online booking is nine people.');
        return false;
    }
}


// Used to update the holiday options form...
function updateOptionsInsurance() {

    ourForm = document.getElementById('unitoptionsform');

    if(ourForm.insurer[1].checked == 1) {
        hideDiv('insure3rddetails');
        //hideDiv('noinsuranceprice_top');  hideDiv('noinsuranceprice_bottom'); 
        //showDiv('sovinsuranceprice_top');  showDiv('sovinsuranceprice_bottom'); 
        showDiv('insuranceagree'); 
    }
    else {
        showDiv('insure3rddetails');
        //hideDiv('sovinsuranceprice_top'); hideDiv('sovinsuranceprice_bottom');
        //showDiv('noinsuranceprice_top'); showDiv('noinsuranceprice_bottom');    
        hideDiv('insuranceagree'); 
    }

    checkOptionsForm(false);

    return true;
}


// Used to handle the update of the holiday options form...
function updateRoomSelector() {

    ourForm = document.getElementById('unitoptionsform');

    showDiv('roomwarning');

    updateOptionsForm();

    return true;   
}

// Used to handle the update of the holiday options form...
function updateOptionsForm() {

    ourForm = document.getElementById('unitoptionsform');

    // They need to re-check availability...
    ourForm.mode.value = "update";
    hideDiv('bookbutton');
    showDiv('gobutton');

    hideDiv('price_top');
    hideDiv('price_bottom');
    showDiv('update_top');
    showDiv('update_bottom');
    
    checkOptionsForm();

    return true;    
}


// Used to handle the submit of the holiday options form...
function checkOptionsForm(finalCheck) {

    ourForm = document.getElementById('unitoptionsform');

    canSubmit = false;
    
    if (ourForm.insurer[1].checked == 1) canSubmit = true;
    else {
        if (ourForm.insurername.value != "") canSubmit = true;
        else {
            if (finalCheck == true) {
                ourForm.insurername.focus();
                ourForm.insurername.style.backgroundColor = 'FFD3D3';
                alert('You must provide the name of your insurer.');
            }
        }
    }   

    if (document.getElementById('bookbutton').style.visibility != "hidden") { bookMode = true; }
    else { bookMode = false; }
    if(ourForm.errataagree) {
        if (bookMode && finalCheck && canSubmit && ourForm.errataagree.checked != 1) {
            canSubmit = false;    
            alert('You must agree to the any errata detailed before you can continue.');
        }
    }

    if(ourForm.insurer[1].checked) {
        if (bookMode && finalCheck && canSubmit && ourForm.insuranceagree.checked != 1) {
            canSubmit = false;    
            alert('You must agree to the rules outlined for the Sovereign insurance policy before you can continue.');
        }
    }

    if (bookMode && finalCheck && canSubmit && ourForm.tacagree.checked != 1) {
        canSubmit = false;    
        alert('You must agree to the booking terms and conditions before you can continue.');
    }

    if (canSubmit) {
        if (finalCheck == true) {
            hideDiv('submitoptions');
            showDiv('pleasewait');
        }
        return true;
    }
    else return false;
}


// Used to update the shown divs within the availability form...
function updatePaymentDetailsForm() {

    /*

    // Correspondance address...
    showAlternateCorrespondanceAddress = document.getElementById('notleadpassenger').checked;
    
    if (showAlternateCorrespondanceAddress == true) {
        //window.status = 'Need to show alternate correspondance address... ';
        hideDiv('leadpasscor');   
        showDiv('altcor'); 
    }
    else {
        //window.status = 'Need to show lead passenger details...';
        hideDiv('altcor');   
        showDiv('leadpasscor');
    }
    

    // Card address...
    showAlternateCardAddress = document.getElementById('samecardaddress').checked;
    
    if (showAlternateCardAddress == false) {
        //window.status = window.status+'Need to show alternate card address... ';
        showDiv('altcardaddress'); 
    }
    else {
        //window.status = window.status+'Don\'t need to show alternate card address... ';
        hideDiv('altcardaddress');
    }    
      */  
 
    // Card type...
    feeCards = new Array("Mastercard","Visa_Credit");       
    selectedCard = document.getElementById("card_type")[document.getElementById("card_type").selectedIndex].value;  
 
    feeApplies = false;
    for (loop=0; loop<feeCards.length; loop++) {
        if (feeCards[loop] == selectedCard) feeApplies = true;
    }
        
    if (feeApplies) {
        showDiv('cardchargewarning');
        showDiv('feeheading');
        showDiv('totheading');
        showDiv('feefull');
        showDiv('totfull');
        showDiv('feedep');                
        showDiv('totdep');
    }    
    else {
        hideDiv('cardchargewarning');
        hideDiv('feeheading');
        hideDiv('totheading');
        hideDiv('feefull');
        hideDiv('totfull');
        hideDiv('feedep');                
        hideDiv('totdep');
    }    
        
    return true;            
}


// Used to update the special requirements form...
function updateRequirementsForm() {

    if (document.getElementById("reqbox").checked == true) { showDiv('reqtext'); }
    else { hideDiv('reqtext'); }

    return true;
}

function setBoardHidden(op,val) {

    opArray = new Array("dummy_option","extras_code_flexidine3nts","extras_code_flexidine5nts","extras_code_flexidine7nts","extras_code_fullboard","extras_code_halfboard");

    for(i=0;i<opArray.length;i++) {
        if(document.forms['unitoptionsform'].elements[opArray[i]]) {
            document.forms['unitoptionsform'].elements[opArray[i]].value = opArray[i] == op ? val : 0;
        }
    }

}


// Used to manage display of tabbed divs in the CMS...
currentTab = 0;
function showTab(tabNo) {
    if (tabNo != currentTab) {
        // Debug ...
        //window.status = 'showTab - Want to show '+tabNo+' and disable '+currentTab;
        
        // Update tab content...
        hideDiv('tabcontent_'+currentTab);      // Hide current div content...
        showDiv('tabcontent_'+tabNo);           // Show tab content...

        // Update tabs...
        document.getElementById('tab_'+tabNo).style.backgroundColor='#454545';
        document.getElementById('tab_'+currentTab).style.backgroundColor='#8A8A8A';

        currentTab = tabNo;                     // Update global...
    }
}

// Used to toggle the visibility of a div..
function toggleDivVisibility(divname) {
    if (document.getElementById(divname).style.visibility == "hidden") showDiv(divname);
    else hideDiv(divname);
}


// Used to toggle the visibility of a div via an image (which also gets toggled)..
function imageToggleDivVisibility(divname, imagediv, baseimage) {
    if (document.getElementById(divname).style.visibility == "hidden") {
        showDiv(divname);
        document.getElementById(imagediv).src=baseimage+"_contract.gif";
    }
    else {
        hideDiv(divname);
        document.getElementById(imagediv).src=baseimage+"_expand.gif";
    }
}

function showDiv(divname) {
    document.getElementById(divname).style.visibility="visible";
    document.getElementById(divname).style.display="inline";
}

function hideDiv(divname) {
    document.getElementById(divname).style.visibility="hidden";
    document.getElementById(divname).style.display="none";
}