// JavaScript Document
// initBinding() starts
function initBinding() {
	// Add class last to the last module in a module panel
	$(document).ready(function(){
		$('.module-panel div:last-child').addClass('last');
    });
    
    //Add brochure hover color on brochure selection page
    $(document).ready(function() {
        $(".brochure .promo").unbind('mouseover').bind('mouseover', function() {
            $(this).addClass('promo-hover');
        });
        $(".brochure .promo").unbind('mouseout').bind('mouseout', function() {
            $(this).removeClass('promo-hover');
        });
    });

	// hide show function for descriptions
    $(document).ready(function() {
        $(function() {
            // Limit
            var limit = 200;
            // place all p tags in an array
            var chars = $(".description p");
            // for each array item
            $.each(chars, function() {
                // place all text within variable
                var charLength = $(this).text();
                // if the text exceeds the limit then proceed
                if (charLength.length > limit) {
                    // text under 207 place in this variable
                    var visiblePart = $("<span> " + charLength.substr(0, limit - 1) + "</span>");
                    // creat variable for dots
                    var dots = $("<span class='dots'>... </span>");
                    // text over 207 place in this variable
                    var hiddenPart = $("<span class='more'>" + charLength.substr(limit - 1) + "</span>");
                    // create more and less html
                    var readMore = $("<span class='read-more'><img src='http://media.ef.com/_imgs/funnelpages2010//plus-button.gif' /></span>");
                    var readLess = $("<span class='read-less'><img src='http://media.ef.com/_imgs/funnelpages2010//close-button.gif' /></span>");
                    // create click function for read more button
                    readMore.click(function() {
                        // hide this read more button
                        $(this).hide();
                        // find dots and hide
                        $(this).parent().children(".dots").hide();
                        // slide toggle the hidden part
                        $(this).parent().children(".more").slideToggle('slow');
                        // show the read less button
                        $(this).parent().children(".read-less").show();
                    });
                    // create click function for read less button
                    readLess.click(function() {
                        // hide this read less button
                        $(this).hide();
                        // find dots and show
                        $(this).parent().children(".dots").show();
                        // slide toggle the hidden part
                        $(this).parent().children(".more").slideToggle('slow');
                        // show the read more button
                        $(this).parent().children(".read-more").show();
                    });
                    // recreate the paragraph within description with the above a values for visible text, dots, hidden text, and buttons
                    $(this).empty()
						.append(visiblePart)
						.append(dots)
						.append(readMore)
						.append(hiddenPart)
						.append(readLess)
                    $('span.more').hide();
                }
            });
        });
    });

	// function for the sliding of the program selections for program pages
	$(document).ready(function(){
		$('.program-selection-button').unbind('click').bind('click', function(){
			$('.program-selection').slideToggle(300);
		});
	});

	// start transforming form elements
	$(function() {
		//find all form with class jqtransform and apply the plugin
		$("form.jqtransform").jqTransform();
	});

	// start ui tabs for the contact section
	$(function() {
		// $('ul#main-contact-tabs').tabs();
		$('ul#sub-contact-tabs').tabs({ fx: { opacity: 'toggle' } });
		$('ul#agent-contact-tabs').tabs({ fx: { opacity: 'toggle' } });
	});

	/* fade and toggle effects for agents selection */
	$(document).ready(function(){
		$('#agent-contact-tabs').hide();
		$agentname = $('#agent-contact-tabs li:first-child').text();
		$('.agentSelectWrapper span').text($agentname);
		$('.agentSelectWrapper').click(function(){
			$('#agent-contact-tabs').slideToggle();
			return false;
		});
		$('.jqTransformSelectOpen').click(function(){
			$('#agent-contact-tabs').slideToggle();
			return false;
		});
		$('#agent-contact-tabs a').click(function(){
			$('#agent-contact-tabs').slideToggle();
			$agentname = $(this).text();
			$('.agentSelectWrapper span').text($agentname);
		});
	});

	$(document).ready(function(){
		//$('.quick-search .forms-up .jqTransformSelectWrapper').css({zIndex: 10});
	});

	$(document).ready(function(){
		$('#heading .jqTransformSelectWrapper ul').addClass('buttons');
	});

	$(document).ready(function() {
	    /*Go to URL link when you select a country from Top Panel Select dropdown */
	    $('a.others-select').click(function() {
	        window.location = $(this).attr("href");
	        return;
	    });
	});

	$(document).ready(function(){
		$('#footer ul li:first-child').addClass('first');
	});
	
	$(document).ready(function(){
		addContent = $('.home-address .info .content div').hide();
		$('.home-address .info .forms').show();
		$('.home-address .info .content div:first-child').show();
		$('.home-address .info .forms ul li a').click(function(){
			clickedAddId = $(this).attr('href');
			var index = clickedAddId.lastIndexOf("#");
			var filename = clickedAddId.substr(index);
			clickedAddId = filename.replace(/[#]/, "");
			addContentVis = $('.home-address .info .content div:visible');
			$(addContent).each(function(){
				addId = $(this).attr('id');
				if(addId == clickedAddId){
					addContentFadeIn = $(this);
					$(addContentVis).fadeOut(function(){
						$(addContentFadeIn).fadeIn();
					});
				}
			});
			return false;
		});
	});

	$(function() {
	    // if the function argument is given to overlay,
	    // it is assumed to be the onBeforeLoad event listener
	    $("a[rel]").overlay({
	        expose: '#445566',
	        onBeforeLoad: function() {
	            // grab wrapper element inside content
	            var wrap = this.getContent().find(".contentWrap");
	            classContent = (this.getTrigger().attr("class"));
	            if (classContent == 'quick-search') {
	                urlPath = '/Forms/QuickSearchBox.aspx';
	                $('.simple_overlay').addClass('form-pop-up');
	                $('.simple_overlay').removeClass('text-pop-up');
	            } else {
	                urlPath = (this.getTrigger().attr("href"));
	                $('.simple_overlay').addClass('text-pop-up');
	                $('.simple_overlay').removeClass('form-pop-up');
	            }
	            // ajax call
	            $.ajax({
	                url: urlPath,
	                success: function(data) {
	                    // Queue functions
	                    $(wrap).queue(function() {
	                        // add new data - empty container and append data
	                        $(this).html(data);
	                        $(this).empty().append(data);
	                        // fade in conent and add a class
	                        //$(this).hide().fadeIn();
	                        // rebind jquery
	                        $('form').removeClass('jqtransformdone');
	                        initBinding();
	                        $(this).dequeue();
	                    });
	                }
	            });
	        }
	    });
	});

	$(document).ready(function() {
	    if ($('#heading h1').hasClass('china')) {
	        $('#heading h1').html('<img src="http://media.ef.com/_imgs/funnelpages2010/header/china_header.gif" alt="EF Education First" width="238px" height="30px" >');
	    }
	});

	$('#target-groups a').click(function() {
	    var hash = $(this).attr('name').replace('loadContent(', '').replace(')', '');
	    sethash(hash);
	    return false;
	});
	
	if (columnLoaded == false) {
	    controlLength = $('#control').length;
	    if (controlLength == 1) {
	        $('#content .program-selection ul.buttons li a').click(function() {
	            var hash = $(this).attr('name').replace('loadContent(', '').replace(')', '');
	            columnLoaded = false;
                /*
	            hashValue = window.location.hash;
	            hashValue = hashValue.replace('right-col', 'inner-content');
	            window.location.hash = hashValue;
                */
	            sethash(hash);
	            return false;
	        });
	    }
	    
	    $('#left-col a').click(function() {
	        var hash = $(this).attr('name').replace('loadContent(', '').replace(')', '');
	        sethash(hash);
	        return false;
	    });
	}

	function sethash(hash) {
	    hash = hash.replace(/^.*#/, '');
	    firstComma = hash.indexOf(',');
        secondCommaPos = firstComma + 1;
        hash2 = hash.substr(secondCommaPos);
        secondComma = hash2.indexOf(',');
        thirdCommaPos = secondComma + 1;
        hash3 = hash2.substr(thirdCommaPos);
        thirdComma = hash3.indexOf(',');
        fourthCommaPos = thirdComma + 1;
        hash4 = hash3.substr(fourthCommaPos);
        fourthComma = hash4.indexOf(',');
        var1 = 'agegroup=' + hash.substring(0, firstComma).replace(/'/gi, "").replace(' ', '');
        var2 = '/product='+hash2.substring(0, secondComma).replace(/'/gi, "").replace(' ', '');
        var3 = '/target=' + hash3.substring(0, thirdComma).replace(/'/gi, "").replace(' ', '');        
        var4 = '/site='+hash4.substring(0).replace(/'/gi, "").replace(' ', '');
        hash = var1 + var2 + var3 + var4;
        $.history.load(hash);
        return false;
    }



    $(document).ready(function() {
         if (!$('#ctl00_fatfooter_footerlocaloffice').length>0) {
            $('#footer').show();
        }
    });

    
}
// initBinding() ends

columnLoaded = false;
quickSearchVar = false;

initBinding();


// pageload() start
function pageload(hash) {

    if (hash) {
        firstEqual = hash.indexOf('/');
        secondEqualPos = firstEqual + 1;
        hash2 = hash.substr(secondEqualPos);
        secondEqual = hash2.indexOf('/');
        thirdEqualPos = secondEqual + 1;
        hash3 = hash2.substr(thirdEqualPos);
        thirdEqual = hash3.indexOf('/');
        fourthEqualPos = thirdEqual + 1;
        hash4 = hash3.substr(fourthEqualPos);
        fourthEqual = hash4.indexOf('/');
        ageGroupVar = hash.substring(0, firstEqual).replace('agegroup=', '');
        productVar = hash2.substring(0, secondEqual).replace('product=', '');
        targetVar = hash3.substring(0, thirdEqual).replace('target=', '');
        siteVar = hash4.substring(0).replace('site=', '');
        reloaded = 'true';
        quickSearchVar = true;
        if (targetVar == 'right-col') {
            if (siteVar == 'default') {
                if ($('#inner-content').hasClass('programs')) {
                    targetVar = 'right-col';
                    reloaded = 'false';
                    columnLoaded = true;
                } else {
                    targetVar = 'inner-content';
                    reloaded = 'true';
                    columnLoaded = false;
                }
            }
            if (siteVar == 'smallmarkets') {
                if ($('#inner-content').hasClass('small-market-clicked')) {
                    targetVar = 'right-col';
                    reloaded = 'false';
                    columnLoaded = true;
                } else {
                    targetVar = 'inner-content';
                    reloaded = 'true';
                    columnLoaded = false;
                }
            }
        }
        onClickVarData = hash.replace('agegroup=', "'").replace('/product=', "','").replace('/target=', "','").replace('/site=', "','");
        onclickVar = "loadContent(";
        onclickVar = onclickVar + onClickVarData;
        onclickVar = onclickVar + "')"
        loadContent(ageGroupVar, productVar, targetVar, siteVar, reloaded, onclickVar);
        if (pageNameVar == 'smallmarkets') {
            $('.module-panel').addClass('module-gradient');
        }
    } else {
        if (pageNameVar == 'default') {
            quickSearchVar = false;
            loadContent('', '', 'inner-content', 'default');
            $('.home-panel').fadeIn(400);
            //$('.quick-search').fadeIn(400);
            $('.module-panel').fadeIn(800);
            $('.options').fadeIn(400);
            $('.options').addClass('no-heading');
            $('#footer').fadeIn(400);
            $('.home-address').fadeIn(400);
            $('#inner-content').removeClass('programs');
            columnLoaded = false;
        }
        else if (pageNameVar == 'smallmarkets') {
            loadContent('', '', 'inner-content', 'smallmarkets');
            $('.module-panel').addClass('module-gradient');
        }
    }


    
    return false;
}
// pageload() end






$(document).ready(function() {
    // top bar function start
    var api1 = $(".top-panel-wrapper").expose({ api: true });
    opened = false;
    closedText = '';
    $('#close-top').hide();
    $('#close a').click(function() {
        $('.nav').hide();
        $('.top-panel').slideToggle(function() {
            if (opened == true) {
                api1.close()
                opened = false;
                $('.nav').show();
                $('#header').css({ zIndex: 9999 });
            } else {
                api1.load()
                opened = true;
                $('#close-top').fadeIn();
                $('#header').css({ zIndex: 10000 });
                $contentTop = $('.top-panel .content').html();
                if ($contentTop == '') {
                    // ajax call
                    $.ajax({
                        url: '/TopBar.aspx',
                        success: function(data) {
                            $('.top-panel .content').queue(function() {
                                $(this).html(data);
                                $(this).empty().append(data);
                                $('.top-panel .content div').hide().fadeIn();
                                $('form').removeClass('jqtransformdone');
                                initBinding();
                                $(this).dequeue();
                            });
                        }
                    });
                    loading = '<div id="loading"><img src="http://media.ef.com/_imgs/funnelpages2010/ajax-loader.gif" /></div>';
                    $('.top-panel .content').empty().append(loading).ajaxStart(function() {
                        $(loading).show();
                    }).ajaxStop(function() {
                        $(loading).hide();
                    });
                }
            }
        });
        if (closedText == 'true') {
            $(this).children().children().text(countryText);
            $(this).children().children().prepend("<img src=" + countryImg + " /> ");
            closedText = 'false';
            $(this).children().children().removeClass();
            $(this).children().children().addClass('arrow');
            $('#close-top').hide();
            $(this).css({ paddingLeft: 8 });
        } else {
            countryText = $(this).children().children().text();
            countryImg = $(this).children().children().children().attr('src');
            $(this).children().children().text('Close');
            $(this).children().children().removeClass();
            $(this).children().children().addClass('close');
            closedText = 'true';
            $(this).css({ paddingLeft: 10 });
        }
        return false;
    });

    $('#close-top a').click(function() {
        $('#close-top').fadeOut();
        $('.top-panel').slideToggle(function() {
            if (opened == true) {
                api1.close()
                opened = false;
                $('.nav').show();
                $('#header').css({ zIndex: 9999 });
            } else {
                api1.load()
                opened = true;
                $('#header').css({ zIndex: 10000 });
            }
        });
        if (closedText == 'true') {
            $('#close a').children().children().text(countryText);
            $('#close a').children().children().prepend("<img src=" + countryImg + " /> ");
            closedText = 'false';
            $('#close a').children().children().removeClass();
            $('#close a').children().children().addClass('arrow');
            $(this).css({ paddingLeft: 8 });
        }
        return false;
    });
    // top bar function ends
    
    // Form vallidation starts
    $(".jqtransform").validate({
	    // the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else if ( element.is("select") )
				error.appendTo ( element.parent() );
			else if ( element.is("textarea") )
				error.appendTo ( element.parent().parent().parent().parent().parent().parent() );
			else
				error.appendTo( element.parent().parent().parent().parent() );
		}
    });
    // Form vallidation ends
});




// AJAX SSCRIPT STARTS
var xmlhttp;
var xmlhttp_country;
var xmlhttp_courselength;
var divid;
var homeproduct;
var hometarget;
var targetpage;
var selectedLink;
var funnelversion;
var fsb;
firstPageLoaded = false;

function loadContent(group, product, div, pagename, reloaded, onclickVar) {
    createHTTPRequest(group, product, pagename, reloaded, onclickVar);
    divid = div;
    homeproduct = product;
    hometarget = group;
    targetpage = pagename;
    selectedLink = onclickVar;
}

function createHTTPRequest(group, product, pagename, reloaded, onclickVar) {
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
        alert("Your browser does not support XMLHTTP!");
        return;
    }
    var verVal = document.location.search.substring(1);
    var verPairs = verVal.split("&");
    for (var i = 0; i < verPairs.length; i++) {
        var pos = verPairs[i].indexOf('=');
        if (pos == -1) continue;

        var argname = verPairs[i].substring(0, pos);
        var value = verPairs[i].substring(pos + 1);

        if (argname == 'version') {
            funnelversion = value;
        }
        if (argname == 'fsb') {
            fsb = value;
        }
    }
    if (funnelversion != 'defaultalt') {
        funnelversion = '';
    }
    if (fsb == 'false') {
        fsb = 'false';
    } else {
        fsb = 'true';
    }

    var url = "/AjaxCall/funnelContent.aspx?pagename=" + pagename + "&version=" + funnelversion;
    
    if (group != "" && product == "")
        url = url + "&target=" + group;
    else if (product != "" && group == "")
        url = url + "&product=" + product;
    else if (group != "" && product != "")
        url = url + "&target=" + group + "&product=" + product;
    url = url + "&reloaded=" + reloaded;
    if (group == '' && product == '') {
        $('#content').addClass('home');
        xmlhttp.onreadystatechange = stateChanged;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    } else {
        if (product == '' || reloaded == 'true') {
            $('.home-panel').fadeOut(400);
            $('.quick-search').fadeOut(400);
            $('.module-panel').fadeOut(400);
            $('#ctl00_Content_bottomlinks').fadeOut(400);
            $('.options').fadeOut(400);
            $('#footer').fadeOut(400, function() {
                $('#content').removeClass('home');
                $('#inner-content').addClass('clearfix programs');
                $('#heading').html('<h1>&nbsp;</h1>');
                $('.options').removeClass('no-heading');
                $('#qsearch').hide();
                $('#ctl00_fatfooter_footerlocaloffice').hide();
                xmlhttp.onreadystatechange = stateChanged;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
                // loading image
                progressBar = '<div id="loading"><img src="http://media.ef.com/_imgs/FunnelPages2010/ajax-loader.gif" /></div>';
                $('#inner-content').empty().append(progressBar).hide().fadeIn('slow').ajaxStart(function() {
                    $(progressBar).hide();
                }).ajaxStop(function() {
                    $(progressBar).hide();
                });
            });
        } else {
            $('#right-col').fadeOut(400, function() {
                xmlhttp.onreadystatechange = stateChanged;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
                // loading image
                progressBar = '<div id="loading"><img src="http://media.ef.com/_imgs/FunnelPages2010/ajax-loader.gif" /></div>';
                $('#right-col').empty().append(progressBar).hide().fadeIn('slow').ajaxStart(function() {
                    $(progressBar).hide();
                }).ajaxStop(function() {
                    $(progressBar).hide();
                });
            });
            if (divid != 'right-col') {
                if (targetpage != 'smallmarkets') {
                    $('#footer').fadeOut(400);
                    $('.module-panel').fadeOut(400);
                }
            }
        }
    }
}

function stateChanged() {
    if (xmlhttp.readyState == 4) {
        if (divid.length > 0) {
            if (targetpage == "smallmarkets") {
                $('#inner-content').queue(function() {
                    $('#inner-content').css({ 'height': 'auto' });
                    targetCont = '#';
                    targetCont += divid;
                    $(targetCont).removeClass('small-market-intro');
                    document.getElementById(divid).innerHTML = xmlhttp.responseText;
                    $('#footer').fadeIn(400);
                    $('#ctl00_fatfooter_footerlocaloffice').hide();
                    $('#ctl00_Content_bottomlinks').fadeIn(400);
                    $(".quick-search").css("display", "none");
                    initBinding();
                    $('#inner-content').addClass('small-market-clicked');
                    $(this).dequeue();
                    $("#left-col ul li").each(function() {
                        $(this).removeClass('selected');
                    });
                    $("#left-col ul li a").each(function() {
                        targetLink = $(this).attr('name').replace(/^\s*|\s*$/g, '').replace(/ /gi, "");
                        if (targetLink == selectedLink) {
                            $(this).parent().addClass('selected');
                        }
                    });
                });
            } else {
            $('#inner-content').queue(function() {
                // add new data - empty container and append data
                targetCont = '#';
                targetCont += divid;
                document.getElementById(divid).innerHTML = xmlhttp.responseText;
                $('#inner-content').css({ 'height': 'auto' });
                productlength = $('#left-col li').length;
                if (productlength == 1 && firstPageLoaded == false) {
                    $(targetCont).hide().fadeIn(400);
                    $('#right-col').hide();
                    var hash = $('#left-col a').attr('name').replace('loadContent(', '').replace(')', '');
                    hash = hash.replace(/^.*#/, '');
                    firstComma = hash.indexOf(',');
                    secondCommaPos = firstComma + 1;
                    hash2 = hash.substr(secondCommaPos);
                    secondComma = hash2.indexOf(',');
                    thirdCommaPos = secondComma + 1;
                    hash3 = hash2.substr(thirdCommaPos);
                    thirdComma = hash3.indexOf(',');
                    fourthCommaPos = thirdComma + 1;
                    hash4 = hash3.substr(fourthCommaPos);
                    fourthComma = hash4.indexOf(',');
                    var1 = 'agegroup=' + hash.substring(0, firstComma).replace(/'/gi, "").replace(' ', '');
                    var2 = '/product=' + hash2.substring(0, secondComma).replace(/'/gi, "").replace(' ', '');
                    var3 = '/target=' + hash3.substring(0, thirdComma).replace(/'/gi, "").replace(' ', '');
                    var4 = '/site=' + hash4.substring(0).replace(/'/gi, "").replace(' ', '');
                    hash = var1 + var2 + var3 + var4;
                    firstPageLoaded = true;
                    $.history.load(hash);
                } else {
                    $(targetCont).hide().fadeIn(400);
                }
                if (quickSearchVar == false) {
                    if (fsb == 'true') {
                        $('.quick-search').css({ 'display': 'none','top':'296px' });
                        $('.quick-search').fadeIn(400);
                    } else {
                        $('.quick-search').css('display', 'none');
                    }
                    quickSearchVar = true;
                }
                // fade in footer
                $("#ctl00_efheader_headerlinks").fadeIn(400);
                $('#footer').fadeIn(400);
                $(this).jqTransform();
                $(this).removeClass('jqtransformdone');
                initBinding();
                $(this).dequeue();
                $("#left-col ul li").each(function() {
                    $(this).removeClass('selected');
                });
                $("#left-col ul li a").each(function() {
                    targetLink = $(this).attr('name').replace(/^\s*|\s*$/g, '').replace(/ /gi, "");
                    //targetLink = targetLink
                    if (targetLink == selectedLink) {
                        $(this).parent().addClass('selected');
                    }
                });
            });
            }
        }

        if (homeproduct.length > 0 || hometarget.length > 0) {
            document.getElementById("ctl00_Content_bottomlinks").setAttribute("style", "display:none;");
            document.getElementById("ctl00_fatfooter_footerlocaloffice").setAttribute("style", "display:none;");
            document.getElementById("ctl00_efheader_headerlinks").setAttribute("style", "display:block");
            document.getElementById("ctl00_Content_qsearch").setAttribute("style", "display:none;");
            document.getElementById("inner-content");
        }
        if (targetpage == 'smallmarkets') {
            $('.module-panel').show();
        }
        //divid = '' is commented out on purpose in order to make the fade in fade out effect can work
        //divid = '';
        homeproduct = '';
        hometarget = '';
    }
}

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function AjaxDriveDropDown(target, country, courselength) {
    //alert(url + id + "," + value);
    var url = "/AjaxCall/SearchBoxAjax.aspx";
    var queryString = "?target=" + target + "&country=" + country + "&courselength=" + courselength;
    url += queryString;
    if (country == "All") {
        xmlhttp_country = GetXmlHttpObject();
        if (xmlhttp_country == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_country.onreadystatechange = CountrystateChanged;
        xmlhttp_country.open("GET", url, true);
        xmlhttp_country.send(null);
    }
    if (courselength == "All") {
        xmlhttp_courselength = GetXmlHttpObject();
        if (xmlhttp_courselength == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_courselength.onreadystatechange = CourseLengthstateChanged;
        xmlhttp_courselength.open("GET", url, true);
        xmlhttp_courselength.send(null);
    }
}

function CourseLengthstateChanged() {
    if (xmlhttp_courselength.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#courseLengthDiv').queue(function() {
            $(this).html(xmlhttp_courselength.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

function CountrystateChanged() {
    if (xmlhttp_country.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#destCountryDiv').queue(function() {
            $(this).html(xmlhttp_country.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

//Common Utility Function to hide all the div from the home page
function SetPostContentLinkClass() {
    var counter = $('.module').length;
    if (counter == 2) {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix two-modules";
    } else if (counter == 3) {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix three-modules";
    }
    else {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix";
    }
    if (targetpage == "smallmarkets") {
        $("#ctl00_Content_bottomlinks").addClass("grey-highlight");
    }
}

//Top Header Slider Function
function SlideTopHeader() {
    var imgArrow = jQuery('div#wrapHeader div.action li.language a#slideme');
    var languageDiv = jQuery('div#wrapHeader div.action li.language div');
    var languageUl = jQuery('div#wrapHeader div.action li.language ul');
    var languageUlheight = languageUl.height();
    languageUl.css({ "width": jQuery('div#wrapHeader div.action li.language').width() - 20, "height": "0px", "visibility": "visible" });
    jQuery('div#wrapHeader div.action li a#slideme').toggle(
        function() {
            imgArrow.css("background-image", "url(http://media.ef.com/_imgs/ly/2010/header/arrowUp.gif)");
            languageDiv.animate({ height: languageUlheight + 35 }, 200);
            languageUl.animate({ height: languageUlheight }, 200);
        },
        function() {
            imgArrow.css("background-image", "url(http://media.ef.com/_imgs/ly/2010/header/arrowDown.gif)");
            languageUl.animate({ height: "0px" }, 450);
            languageDiv.animate({ height: "24px" }, 450);
        }
    );
}

function PopUpMultilingualCountry() {
    // run code
    $(document).ready(function() {
        // select the overlay element - and "make it an overlay"
        $(".overlayWrapper").overlay({
            // custom top position
            top: 272,
            // some expose tweaks suitable for facebox-looking dialogs
            expose: '#445566',
            closeOnClick: true,
            // we want to use the programming API
            api: true,
            onBeforeLoad: function() {
                // grab wrapper element inside content
                var wrap = this.getContent().find(".contentWrap");
                urlPath = '/Forms/MultiLanguage.aspx';
                $('.simple_overlay').addClass('multilanguage-country-form-popup');
                $('.simple_overlay').removeClass('text-pop-up');
                // ajax call
                $.ajax({
                    url: urlPath,
                    success: function(data) {
                        // Queue functions
                        $(wrap).queue(function() {
                            // add new data - empty container and append data
                            $(this).html(data);
                            $(this).empty().append(data);
                            // fade in conent and add a class
                            $(this).hide().fadeIn();
                            // rebind jquery
                            $('form').removeClass('jqtransformdone');
                            initBinding();
                            $(this).dequeue();
                        });
                    }
                });
            }
            // load it immediately after the construction
        }).load();
    });
}

//$('.target').change(function() {
//    var str = "";
//    $("select option:selected").each(function() {
//        str += $(this).text() + " ";
//    });
//    $("div").text(str);
//})
//
// AJAX SCRIPT ENDS


// UPDATESEARCHBOX SCRIPT STARTS
var xmlhttp_updatecountry;
var xmlhttp_updatecourselength;
var xmlhttp_updatelengthByCountry;

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

//Get all selected values, and redirect to the search result page
function GetCourseLengthValue() {
    var agegroup_value, country_value, courselength_Value;
    if (document.getElementById("ctl00_Content_searchBox_AgeGroupCtl") != null) {
        agegroup_value = document.getElementById("ctl00_Content_searchBox_AgeGroupCtl").value;
    } else {
        agegroup_value = document.getElementById("AgeGroupCtl").value;
    }
    country_value = document.getElementById("destCountryID").value;
    courselength_Value = document.getElementById("CourseLength_ID").value;
    if (agegroup_value == null || agegroup_value == "") {
        //alert("Please select your age group!");
        alert(alertText);
    } else {
        window.location = "/SearchResult.aspx?target=" + agegroup_value + "&country=" + country_value + "&courselength=" + courselength_Value;
    }
}

//Update Course  length By Country
function UpdateCourseLengthByCountry(target, country, courselength) {
    var url = "/AjaxCall/SearchBoxAjax.aspx";
    var queryString = "";
    if (country == "")
        queryString = "?target=" + target + "&country=" + country + "&courselength=Update";
    else
        queryString = "?target=" + target + "&country=" + country + "&courselength=" + courselength;
    url += queryString;
    xmlhttp_updatelengthByCountry = GetXmlHttpObject();
    if (xmlhttp_updatelengthByCountry == null) {
        alert("Your browser does not support XMLHTTP!");
        return;
    }
    xmlhttp_updatelengthByCountry.onreadystatechange = UpdateCourseLengthByCountrystateChanged;
    xmlhttp_updatelengthByCountry.open("GET", url, true);
    xmlhttp_updatelengthByCountry.send(null);
}

function UpdateCourseLengthByCountrystateChanged() {
    if (xmlhttp_updatelengthByCountry.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText;
        $('#courseLengthDiv').queue(function() {
            $(this).html(xmlhttp_updatelengthByCountry.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

//Update country and course length by target group
function UpdateSearchBoxDropDown(target, country, courselength) {
    var url = "/AjaxCall/SearchBoxAjax.aspx";
    var queryString = "?target=" + target + "&country=" + country + "&courselength=" + courselength;
    url += queryString;
    if (country == "Update") {
        xmlhttp_updatecountry = GetXmlHttpObject();
        if (xmlhttp_updatecountry == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_updatecountry.onreadystatechange = UpdateCountrystateChanged;
        xmlhttp_updatecountry.open("GET", url, true);
        xmlhttp_updatecountry.send(null);
    }
    if (courselength == "Update") {
        xmlhttp_updatecourselength = GetXmlHttpObject();
        if (xmlhttp_updatecourselength == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_updatecourselength.onreadystatechange = UpdateCourseLengthstateChanged;
        xmlhttp_updatecourselength.open("GET", url, true);
        xmlhttp_updatecourselength.send(null);
    }
}

function UpdateCourseLengthstateChanged() {
    if (xmlhttp_updatecourselength.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#courseLengthDiv').queue(function() {
            $(this).html(xmlhttp_updatecourselength.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

function UpdateCountrystateChanged() {
    if (xmlhttp_updatecountry.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#destCountryDiv').queue(function() {
            $(this).html(xmlhttp_updatecountry.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}
// UPDATESEARCHBOX SCRIPT ENDS
