﻿/*
    Graham Colmer : 11th March 2009

    ils.js : ils specific javascript file. This file should contain functions that are ils specific

    11/3/09 - This file still contains alot of logic used on the old site. It can be removed once we are
    clear no-one is still using it.
*/

//toggle the efekta  tabs - www.ef.com/master/ils/why/efekta/
function toggle(choice) {
    if (choice == "one") {
        //image is 240 high but only 80px showing, move the background position to show the active tab
        document.getElementById('cont').style.background = "url(http://media.ef.com/_imgs/ils/09/efektaTabs.gif) no-repeat 0 0";
        jQuery('#before').show();
        jQuery('#during,#after').hide()
    }
    else if (choice == "two") {
        //image is 240 high but only 80px showing, move the background position to show the active tab
        document.getElementById('cont').style.background = "url(http://media.ef.com/_imgs/ils/09/efektaTabs.gif) no-repeat 0 -80px";
        jQuery('#during').show();
        jQuery('#before,#after').hide();
    }
    else {
        //image is 240 high but only 80px showing, move the background position to show the active tab
        document.getElementById('cont').style.background = "url(http://media.ef.com/_imgs/ils/09/efektaTabs.gif) no-repeat 0 -160px";
        jQuery('#after').show();
        jQuery('#before,#during').hide();
    }
}

//function to show/hide extras info on reserve-->options page
function expandcollapse(postid) {

    whichpost = document.getElementById(postid);

    if (whichpost.className == "postshown") {
        whichpost.className = "posthidden";
    }
    else {
        whichpost.className = "postshown";
    }
}

function selectInfomeeting(id) {
    //var w = document.getElementById("townlist").selectedIndex
    //var selected_value = document.getElementById("townlist").options[w].value;

    document.getElementById("townlist").value = id
    //alert(jQuery("#townlist option[@selected]").text());

    //focus on first form element
    jQuery("#fname").css({ background: "#ffdc1e" });
    jQuery(":text:first").focus();
}

//destination courses dropdown
function changePage(page) {
    if (page != '#') {
        document.getElementById(current).style.display = 'none';
        document.getElementById(page).style.display = 'block';
        current = page;
    }
}

//main nav - do we need this ?
function formatDestNav() {
    var x = document.getElementById('destNav').getElementsByTagName('li');
    x[0].className += "first";
    x[x.length - 1].className += "last";
}

/* for booking */
function optionSubmit(destname, enddate, weeks, brochure, fee, price) {
    document.option1.enddate.value = enddate;
    document.option1.destname.value = destname;
    document.option1.weeks.value = weeks;
    document.option1.brochure.value = brochure;
    document.option1.fee.value = fee;
    document.option1.price.value = price;
    document.option1.submit();
}

/* show hide extras rows in reserve -> options page*/
function expandcollapse(postid) {

    whichpost = document.getElementById(postid);

    if (whichpost.className == "postshown") {
        whichpost.className = "posthidden";
    }
    else {
        whichpost.className = "postshown";
    }
}

//functions for showing/hiding div for wwa language chooser div (blue strip in header)
var alphaInterval;
function showDiv() { document.getElementById('wwLinks').className = 'displayDiv'; }
function hideDiv() { clearTimeout(alphaInterval); document.getElementById('wwLinks').className = 'hideDiv'; }
function setTimeOut() { if (alphaInterval) { clearTimeout(alphaInterval); } alphaInterval = setInterval('hideDiv()', 1000); }
function clearTime() { clearTimeout(alphaInterval); }

function setURL(ctr) {
    var newURL = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);
    window.location = newURL + "?mc=" + ctr;
}



//function to place a flag beside a country name
function setFlags() {

    var x = document.getElementById('destList').getElementsByTagName('li');

    for (i = 0; i < x.length; i++) {
        if (x[i].className == "title") {
            var cur = x[i].childNodes[0].href.substring(x[i].childNodes[0].href.lastIndexOf('/') - 2);
            cur = cur.replace("/", "");

            x[i].className += " flag_" + cur;
        }
    }
}

//function to show country marker on the destination map
function displayCountries(ctr) {
    //we can find out what countries are available inside the destList div, where the list is outputted:

    //get all countries we need to check...

    //array to hold countries
    var countries = [];

    //div that contains countries
    var d = document.getElementById('overview').getElementsByTagName('div');

    for (i = 0; i < d.length; i++) {
        if (d[i].id) {
            //put the country into the array we need to check
            countries.push(d[i].id)
        }
    }

    //compare the countries array we created with the links in the destList div
    var list = document.getElementById('destList').getElementsByTagName('a');

    //check all links in div to see if they contain country codes in countries array
    for (i = 0; i < list.length; i++) {
        for (j = 0; j < countries.length; j++) {
            //only compare the last 5 chars of the url with the country code
            //mixup occuring for UD market without this

            var l = list[i].href.substring(list[i].href.length - 5);

            //if there is a match, show the div (which is hidden by default)
            if (l.indexOf(countries[j]) != -1) {
                document.getElementById(countries[j]).style.visibility = "visible";
            }
        }
    }

}

//contact form agent functions

function expandcollapse(postid) {
    whichpost = document.getElementById(postid);
    if (whichpost.className == "conshown") {
        whichpost.className = "conhidden";
    }
    else {
        whichpost.className = "conshown";
        document.getElementById("showhide").innerHTML = "";
    }
}

function changeAgent(form) {
    if (document.getElementsByTagName) {
        var ULElement = document.getElementsByTagName("li");
        for (var i = 0; i < ULElement.length; i++) {
            var tag = ULElement[i];

            if (tag.className == "agentselected") {
                tag.className = "agenthidden";
            }
        }

        var ele = document.getElementById(String(form.value));
        ele.className = "agentselected";
    }
}

function setBirthdayDropdowns() {
    //days
    var days = "<select name='dd' id='daySel'>\n";

    //put in default value for dd
    days += "\t<option selected='selected' value='--'>--</option>";

    for (i = 1; i <= 31; i++) {
        days += "\t<option value=" + i + ">" + i + "</option>\n";
    }

    days += "</select>\n\n";

    //months
    var months = "<select name='mm' id='monthSel'>\n";

    //put in default value for mm
    months += "\t<option selected='selected' value='--'>--</option>";

    for (i = 1; i <= 12; i++) {
        months += "\t<option value=" + i + ">" + i + "</option>\n";
    }

    months += "</select>\n\n";

    //years
    var years = "<select name='yyyy' id='yearSel'>\n";

    //put in default value for yyyy
    years += "\t<option selected='selected' value='----'>----</option>";

    for (i = 2000; i > 1950; i--) {
        years += "\t<option value=" + i + ">" + i + "</option>\n";
    }

    years += "</select>\n\n";

    return days + months + years;
}


function validNumber(object) {
    var emailReg = /^\d$/;
    var regex = new RegExp(emailReg);
    return regex.test(object);
}

function checkValidEmail(object) {
    var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var regex = new RegExp(emailReg);
    return regex.test(object);
}

function isValidDate(Mn, Day, Yr) {
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if (dt.getDate() != Day) {
        return "invalid";
    }
    else if (dt.getMonth() != Mn - 1) {
        //this is for the purpose JavaScript starts the month from 0
        return "invalid";
    }
    else if (dt.getFullYear() != Yr) {
        return "invalid";
    }

    return "valid";
}

function destImages(country, code) {

    //array contains dest code and amount of images for that dest

    var destCodeArray = new Array(
        ["brs", 4],
        ["per", 4],
        ["syd", 5],
        ["mon", 2],
        ["too", 4],
        ["van", 6],
        ["bej", 5],
        ["sha", 4],
        ["cos", 4],
        ["mun", 4],
        ["qui", 5],
        ["bar", 4],
        ["maa", 3],
        ["nic", 4],
        ["par", 5],
        ["dub", 4],
        ["rom", 3],
        ["msj", 6],
        ["auc", 4],
        ["cap", 5],
        ["bou", 3],
        ["bri", 4],
        ["bst", 4],
        ["cac", 4],
        ["cam", 4],
        ["has", 4],
        ["lon", 4],
        ["man", 3],
        ["oxf", 6],
        ["boi", 4],
        ["chi", 4],
        ["flf", 3],
        ["hnl", 5],
        ["lar", 4],
        ["mia", 8],
        ["nyc", 7],
        ["sba", 4],
        ["sdo", 4],
        ["sea", 4],
        ["sfb", 3],
        ["sfd", 4],
        ["sfo", 5],
        ["tpa", 4],
        ["bfl", 3]
    );

    var destCodeArrayLength = destCodeArray.length;
    var destCode = code;
    var imageLength = 3;
    var linkURL = location.href;

    for (var i = 0; i < destCodeArrayLength; i++) {
        if (destCodeArray[i][0] == destCode.toLowerCase()) {
            imageLength = destCodeArray[i][1];
            break;
        }
    }

    //using jquery, append image html into the destImages div. The cycle code in the main page will cycle
    //through the images.

    for (i = 1; i <= imageLength; i++) {
        jQuery("<img src='/_imgs/ils/destinations/09/" + country + "_" + code + "_" + i + ".jpg'>").appendTo("#destImages")
    }

    return imageLength;

}

function toggleThis(obj) {
	var theBox = obj.parentNode.getElementsByTagName("ul")[0];
	if (theBox.style.display == 'block') theBox.style.display = 'none'
	else {
		var objs = obj.parentNode.parentNode.getElementsByTagName("ul")
		for (i = 0; i < objs.length; i++) {
			objs[i].style.display = 'none'
		}
		theBox.style.display = 'block'
	}
}

function toggleOther(obj) {
    if (obj.options[obj.selectedIndex].value == "9054") {
        document.getElementById("occother").style.display = 'block';
    }
    else {
        document.getElementById("occother").style.display = 'none';
    }
}

function validateNewsletterSignup(fname, lname, email) {

    var errors = new Boolean();
    errors = false;

    //first name
    if (!$('#' + fname).val()) {
        errors = true;
        $('#fnameTD').addClass("error");
    }
    else {
        $('#fnameTD').removeClass("error")
    };

    //last name
    if (!$('#' + lname).val()) {
        errors = true;
        $('#lnameTD').addClass("error");
    }
    else {
        $('#lnameTD').removeClass("error")
    };

    //email
    if (!checkValidEmail($('#' + email).val())) {
        errors = true;
        $('#emailTD').addClass("error");
    }
    else {
        $('#emailTD').removeClass("error")
    };

    return !errors;
}

    function checkValidEmail(object) {
        var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        var regex = new RegExp(emailReg);
        return regex.test(object);
    }
    
//automatically shows right sub menu if a dest video is chosen
function autoToggle(code)
{
    var destinations = new Array(
        ["BOU","uk"],
        ["BRI","uk"],
        ["CAM","uk"],
        ["LON","uk"],
        ["MAN","uk"],
        ["OXF","uk"],
        ["BOI","us"],
        ["LAR","us"],
        ["MIA","us"],
        ["NYC","us"],
        ["SDO","us"],
        ["SFO","us"],
        ["SBA","us"],
        ["SEA","us"],
        ["TOO","ca"],
        ["VAN","ca"],
        ["CAP","sa"],
        ["BRS","au"],
        ["PER","au"],
        ["SYD","au"],
        ["AUC","nz"],
        ["MSJ","mt"],
        ["NIC","fr"],
        ["PAR","fr"],
        ["MUN","de"],
        ["BAR","es"],
        ["MAA","es"],
        ["QUI","ec"],
        ["COS","cr"],
        ["SHA","cn"]);
        
    for(i=0;i<destinations.length;i++)
    {
        if(destinations[i][0].toLowerCase() == code.toLowerCase())
        {
            var theBox = document.getElementById(destinations[i][1]).parentNode.getElementsByTagName("ul")[0];
            theBox.style.display = 'block'; 
        }
    } 
        
       
}    
