function ftdc_emailstory() {

	var url = parent.location.href
	var email = prompt("Enter e-mail address:", "");
	var subject = "Forbes Traveler Article";

	if( email != null ) {
		parent.location.href = 'mailto:' + email +
		'?subject=' + subject + '&body=' + url;
	}
}
function docdowrite(s) {
   document.write(s);
}

function ftdc_printurlparam(getname){

	document.write(ftdc_geturlparam(getname));

}

function ftdc_geturlparam(getname){

        var queries = window.location.search.split('&');

        for (i = 0; i < queries.length; i++) {

                var pair = queries[i].split('=');
                var name = pair[0];
                var value = pair[1];

		if((name == getname) || (name == "?" + getname)){
			return value;
		}
        }

//	alert("not found");
	return "";
}




// functions for slides

function ftdc_next_inPlaylist()
{
	var params = nextSlide.split('?');
	var queries = params[1].split('&');
	for(i=0; i<queries.length; i++)
	{
		var pair = queries[i].split('=');
		if ( (pair[0] != null) && (pair[0].indexOf('partner') > -1) && (pair[1] != null))
		{
			inPlaylist=true;
			nextSlide=params[0];
		}
	}
}


// var thisspeed is declared in slide.jsp
function ftdc_slideshow_initialize()
{
	var speed = defaultSpeed;
	var params = window.location.search.split('?');
	var queries = "";
	var advertise = false;
	if(params[1] !=null)
	{
		queries=params[1].split('&');
	}

	for (i = 0; i < queries.length; i++)
	{
		var pair = queries[i].split('=');
		if ( (pair[0] != null) && (pair[0].indexOf('thisSpeed') > -1) && (pair[1] != null))
		{
			var tmp = parseInt(pair[1]);
			if (! isNaN(tmp))
			{
				speed=tmp;
			}
			else
			{
				speed=defaultSpeed;
			}

		}
		if ( (pair[0] != null) && (pair[0].indexOf('advertise') > -1) && (pair[1] != null))
                {
                        //alert('this is an advertisement video2');
			window.clearTimeout(myCancel);
			advertise = true;
			//ftdc_slideshow_stopThis();
                }

		if ( (pair[0] != null) && (pair[0].indexOf('debug') > -1) && (pair[1] != null))
		{
			debug = true;
		}
		if ( (pair[0] != null) && (pair[0].indexOf('partner') > -1) && (pair[1] != null))
		{
			inPlaylist = true;
		}
	}

	thisSpeed = speed;

	if(pagenum < numpages){
		if(debug==true){ alert("setting timeout:" + thisSpeed); }
		myCancel = window.setTimeout('ftdc_slideshow_moveSlides(1)',thisSpeed);
	}
	else{
		if(debug==true){ alert("last slide: no timeout2");}
		myCancel = window.setTimeout('ftdc_slideshow_moveSlides(1)',thisSpeed);
		ftdc_next_inPlaylist();
	}
	if(advertise==true)
	{
		window.clearTimeout(myCancel);
		//alert('hello');
	}

		return;


}

function ftdc_slideshow_moveSlides(direction) {

	var next;
	var pfxThisSpeed = "?";
	var next = nextSlide;
	if(direction<1)
	{
		next = prevSlide;
	}

	if(inPlaylist)
	{
		pfxThisSpeed = "&";
		next = next + "?partner=playlist";
	}
	next = next + pfxThisSpeed+"thisSpeed=" + thisSpeed;


	if(debug==true){
		next = next + "&debug=true";
		alert("next: " + next);
	}
	document.location.href = next;

}
function ftdc_slideshow_stopThis() {
	if(debug==true){ alert("clear timeout"); }
	window.clearTimeout(myCancel);
}

function ftdc_slideshow_changeSpeed(factor) {
	thisSpeed = thisSpeed * factor;
	if(debug==true){ alert("factor:" + factor + " thisSpeed:" + thisSpeed); }
}

function ftdc_map_popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=yes,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=0,width=960,height=800,left = 120,top = 120');");
}

function ftdc_slideshow_popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=yes,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=yes,width=1024,height=778,left = 220,top = 192');");
}
function windowOpen(url, width, height)
{
	style='width='+width+',height='+height+',status=0,toolbar=0,location=1,menubar=0,directories=1,resizable=1,scrollbars=1,location=0';
	window.open( url, 'TellObj',style );
}

function PopUp(ref)
{
var strFeatures="toolbar=no,status=no,menubar=no,location=no"
strFeatures=strFeatures+",scrollbars=no,resizable=yes,height=600,width=600"

newWin = window.open(ref,"TellObj",strFeatures);

newWin.opener = top;
}


// Functions for Accomodations Search Form

// global variable - holds the value of the last destination clicked

var ftdc_accommodationsearch_destination_id = -1;
var ftdc_headesrearch_destination_id = -1;

function ftdc_accommodationsearch_clickmap(flag,thisref,nextid,nextnextid){

	var parent_id = thisref.options[thisref.selectedIndex].value;

	if (parent_id == -1){ return; }

	var children = ftdc_accommodationsearch_getChildren(parent_id);

	if(children.length == 0){

		if(flag == "accommodations"){
			ftdc_accommodationsearch_destination_id = parent_id;
		}
                 else{
                        ftdc_headersearch_destination_id = parent_id;
                         document.location.href = "/guide/overview/" + ftdc_headersearch_destination_id + "/";
                }
		if(typeof nextid != "undefined"){
			var selectref = document.getElementById(nextid);
			selectref.style.display = "none";
		}

		if(typeof nextnextid != "undefined"){
			var selectref2 = document.getElementById(nextnextid);
			selectref2.style.display = "none";
		}
	}
	else{

		if(flag == "accommodations"){
			ftdc_accommodationsearch_destination_id = -1;
		}
		else{
			ftdc_headersearch_destination_id = -1;
		}

		if(typeof nextid != "undefined"){
			var selectref = document.getElementById(nextid);
			var children = ftdc_accommodationsearch_getChildren(parent_id);
			ftdc_accommodationsearch_populate(selectref,children,"Select a subregion...");
			selectref.style.display = "inline";
		}

		if(typeof nextnextid != "undefined"){
			var selectref2 = document.getElementById(nextnextid);
			selectref2.options.length=0;
			selectref2.style.display = "none";
		}
	}

	if(flag == "accommodations"){
		ftdc_accommodationsearch_setPriceform();
	}
}

function ftdc_accommodationsearch_setPriceform(){

	var selectref = document.getElementById("select0");
	var divref = document.getElementById("priceform");

	var index = selectref.selectedIndex;

	if(index == 1){

		divref.innerHTML =
			"<INPUT TYPE=RADIO NAME=range VALUE=0 CHECKED><b class=\"util\">ALL PRICES</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=1><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><b class=\"util\"> Low</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=2><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><b class=\"util\"> Medium</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=3><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><b class=\"util\"> High</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=4><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><b class=\"util\"> Very High</b><br>";

	}
	else{

		divref.innerHTML = "<INPUT TYPE=RADIO NAME=range VALUE=0 CHECKED><b class=\"util\">ALL PRICES</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=P><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><b class=\"util\"> Expensive</b><br>"
			+ "<INPUT TYPE=RADIO NAME=range VALUE=V><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_on.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"><img src=\"/media/img/price_guide_off.gif\" border=0 alt=\"\" style=\"margin-right:1px;\"> <b class=\"util\"> Very Expensive</b><br>";

	}

}

function ftdc_accommodationsearch_getName(id){

	for(var i=0; i < Brady.length; i++ ){

		if(Brady[i][0] == id){
			return Brady [i][2];
		}
	}


	return "ID " + id + " not found";
}

function ftdc_accommodationsearch_getNumChildren(parent_id){


	var count = 0;

	for(var i=0; i < Brady.length; i++ ){
		if(Brady[i][1] == parent_id){
			count++;
		}
	}

	return count;
}
function ftdc_accommodationsearch_getChildren(parent_id){

	var tmp = new Array();

	for(var i=0; i < Brady.length; i++ ){

		//var numchildren = ftdc_accommodationsearch_getNumChildren(Brady[i][0]);

		var title = Brady[i][2];

		//if (numchildren > 0){
		//	title = title + " (" + numchildren + ")"
		//}

		if(Brady[i][1] == parent_id){
			tmp[tmp.length] = new Option(title,Brady[i][0]);
		}
	}

	return tmp;
}

function ftdc_accommodationsearch_initialize(selectid1,selectid2,selectid3){

	selectref1 = document.getElementById(selectid1);
	selectref2 = document.getElementById(selectid2);
	selectref3 = document.getElementById(selectid3);

	var children = ftdc_accommodationsearch_getChildren(0);

	ftdc_accommodationsearch_populate(selectref1,children,"Select a region...");

	selectref2.style.display="none";
	selectref3.style.display="none";
}

function ftdc_accommodationsearch_populate(selectref,children,title){


	selectref.options.length = 0;
	selectref.options[0] =  new Option(title);

	for(var i=0; i < children.length; i++ ){
		selectref.options[i+1] = children[i];
	}

	selectref.style.visibility="visible";

}

function ftdc_accommodationsearch_submit(){

        var formref = document.getElementById('searchform');
        var radioref = formref.range;

	if(typeof radioref == "undefined"){

		alert("Please select a region to continue...");
		return;

	}

	if(ftdc_accommodationsearch_destination_id == -1){

		alert("The destination you selected includes many subregions. Please pick one to continue...");
		return;

	}



        var value = -1;

        for(var i=0; i < radioref.length; i++ ){

                if (radioref[i].checked == true){

                        value = radioref[i].value;
                }
        }

        document.location.href = "/accommodations/search/" + ftdc_accommodationsearch_destination_id + "/" + value + "/";
}

function ftdc_viewBox(boxid){
document.getElementById(boxid).style.visibility = "visible";
}

function ftdc_hideBox(boxid){
document.getElementById(boxid).style.visibility = "hidden";
}

// function to grab the "vertical=" parameter from the URL and set a global variable
var ftdc_sidestep_vertical = "air";

function ftdc_reservations_sidestep_init(query){
        var queries = query.split('&');
        for (i = 0; i < queries.length; i++) {

                var pair = queries[i].split('=');
                var name = pair[0];
                var value = pair[1];

                if(name == null){ continue; }
                if(value == null){ continue; }
                if(name.indexOf('vertical') < 0){ continue; }

                if((value == "air") || (value == "hotel") || (value == "car")){
                        ftdc_sidestep_vertical = value;
                }
                else{
                        ftdc_sidestep_vertical = "air";
             }

             break;
 }
}


/**
*
*  UTF-8 data encode / decode
*
*
**/

var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

