function addLoadEvent(func) {
  // to handle bootstrap stack
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function popUpLinks() {
  if (!document.getElementsByTagName) return false;
  var links = $$("a.external");
  for (var i=0; i < links.length; i++) {
	 	links[i].onclick = function(e) {
			if(!e)e=window.event;
			if(e.shiftKey || e.ctrlKey || e.altKey) return;
			window.open(this.href);
			return false;
			}
      }
}

function $javaConsole(msg){
/*
// this is for debugging purposes only.. it logs messages to the javaconsole only works in firefox usage: $javaConsole("hello world");
	try{
        java.lang.System.out.println(msg); 
    }catch(exception){
        return false;
    }
*/
}

var wbaslideshow = Class.create();
    /* date: 6/29/2006 project: worldsbest 2006 TL */
    wbaslideshow.prototype = {
        initialize : function(obj, count, relpath){
              (count != null) ? this.count = count-1 : this.count = 0;
            (relpath != null) ? this.relpath = relpath : this.relpath = "../../slideshow/images/"; 
            //initiating arrays to stick values into see obj.each right after it
            aImage=[];aText=[];aTitle=[];aLinktitle=[];aPhotog=[];aLinkUUID=[];aPid=[];
            obj.each(function(value, index){aImage[index]=value.image;aText[index]=value.text;aTitle[index]=value.title;aLinktitle[index]=value.linktitle;aPhotog[index]=value.photog;aLinkUUID[index]=value.linkUUID;aPid[index]=value.pid;});
                 this.aImage = aImage;
                  this.aText = aText;
                 this.aTitle = aTitle;
             this.aLinktitle = aLinktitle;
                this.aPhotog = aPhotog;
              this.aLinkUUID = aLinkUUID;
                   this.aPid = aPid;
            this.totalslides = obj.length-1;

                this.preloader();
                this.startListener();
                this.positionmarker();
                this.checkhistory();  
        },
        checkhistory: function(){
        var hrefPattern = /slidenr/;
        if(!hrefPattern.test(location.href)){
        var cookieMix = /slideposition=(\d{1,2})/;
        if(!cookieMix.test(document.cookie)){
            return false;
        } else {
            var arrayMatch = cookieMix.exec(document.cookie)
            this.count = arrayMatch[1]-1;
            this.changeattributes(0);
            }
         }
        },
        startListener : function(){
                this.controls = $$("a.slidecontroller");
                    for(var i=0; i<this.controls.length; i++){
                        var direction = this.controls[i].toString().replace(/(http:\/\/.*slide=)(.*)(&.*)/, "$2");          
                        switch(direction){
                        case "next":
                        this.controls[i].onclick = this.slidenext.bindAsEventListener(this);
                        break;
                        case "prev":
                        this.controls[i].onclick = this.slideprev.bindAsEventListener(this);
                        break;
                        case "first":
                        this.controls[i].onclick = this.slidefirst.bindAsEventListener(this);
                        break;
                        case "last":
                        this.controls[i].onclick = this.slidelast.bindAsEventListener(this);
                        break;
                        }
                    }
        },
        slidefirst : function(){
            this.count = 0;
            this.changeslide();
            this.preloader();
            return false;
        },
        slidelast : function(){
            this.count = this.totalslides;
            this.changeslide();
            this.preloader();
            return false;
        },
        slidenext : function(){
            this.count++;
            if(this.count > this.totalslides) {
                this.count = 0;
                }
            this.changeslide();
            this.preloader();
             return false;
        },
        slideprev : function(){
            this.count--;
            if(this.count < 0) {
                this.count = this.totalslides;
                }

            this.changeslide();
            this.preloader();
            return false;
        },
        preloader : function(){
             aPreloding = [this.count + 1,this.count - 1]

            if(aPreloding[0] > this.totalslides){
                aPreloding[0] = 0;
                }
            var nextImg = new Image();
                nextImg.src = this.relpath+this.aImage[aPreloding[0]];
            if(aPreloding[1] < 0) {
                aPreloding[1] = this.totalslides;
                }
            var prevImage = new Image();
                prevImage.src = this.relpath+this.aImage[aPreloding[1]];
            return false;
        },
        positionmarker : function(){
            //$javaConsole("ok");
            var currentSlidePosition = this.count+1;
            $("wbapid").onclick = function(){
            document.cookie ="slideposition="+currentSlidePosition;
            }
        },

        changeattributes : function(fade){
            newimage = this.relpath+this.aImage[this.count];
            $("image").setAttribute("alt",this.aTitle[this.count]);
            $("image").setAttribute("title",this.aTitle[this.count]);
            $("linktitle").setAttribute("href", this.aLinkUUID[this.count]);
            $("wbapid").setAttribute("href", this.aPid[this.count]);
            this.positionmarker();
            document.cookie ="slideposition="+parseInt(this.count+1);                   
            Element.update("text", this.aText[this.count]);
            Element.update("title", this.aTitle[this.count]);
            $("title").setAttribute("href", this.aPid[this.count]);
            Element.update("linktitle", this.aLinktitle[this.count]);
            Element.update("photog", this.aPhotog[this.count]);
            Element.update("slidenumber", this.count+1+" of "+parseInt(this.totalslides+1));
            if(fade == "1"){
                Effect.Appear($('image'),
                {  
                   from: 1, to: 0,
                   duration: 0.2,
                   afterFinishInternal: function(){
                       $("image").setAttribute("src",newimage);
                       Effect.Fade($('image'),{duration: 0.3, from: 0, to: 1}); }
                });
            } else {
                $("image").setAttribute("src", newimage);
            }
        },
        changeslide : function() {
            this.changeattributes(1);
            return false;
        }
}
//END wbaslideshow class

// legacy code below
/* START TL golf specific */
function pollListener(){
    if(!document.getElementById('hpPoll')) return false;
    document.getElementById('hpPoll').onsubmit = radioButtonChecker;
//      document.getElementById('hpPoll').onsubmit = testAjaxPoll;
    }

function radioButtonChecker() {
	// set var radio_choice to false
	var radio_choice = false;
	// Loop from zero to the one minus the number of radio button selections
    pollform = document.getElementById('hpPoll');
    for (var i = 0;  i < pollform.length; i++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
        if (pollform[i].checked){
		    radio_choice = true; 
            }
	}

	if (!radio_choice) {
		// If there were no selections made display an alert box 
		var hpPollBody = document.getElementById('hpPollBody');
        Element.update(hpPollBody, "<p class='error'>Please Make A Selection.</p>");
		return false;
	}
	return true;
}
    
    
function EmailPopUp(){
    var urlReplaceParameter = "../.."; //this needs to be changed depending on the location
    var aHrefURL = this.href;
    var hostname = location.hostname;  
    var myURL = aHrefURL.replace('http://'+hostname,urlReplaceParameter);
    var positionleft = window.screenLeft + document.body.clientWidth/2 - 450/2 || window.screenX + document.body.clientWidth/2 - 450/2;
    var positionTop  = 100/*window.screenTop + document.body.clientHeight/2 - 300/2 || window.screenY + document.body.clientHeight/2 - 300/2 */;
    
    //debugging
    
    if(open(myURL, 'emailwindow','width=450,height=300,scrollbars=no, location=no,left='+positionleft+',top='+positionTop+',menubar=No, alwaysRaised=No,resizable=yes,toolbar=No,status=No'))
        {
        return false;
        } 
    else
        { // if the user has popup blocked it should se the file through regular http request this needs to be tested properly
        return true;
        } 
}

    
function emailLinkHook(){    
if(document.getElementById('articleTools')){
    var articleTools = document.getElementById('articleTools')
    var articleTools = document.getElementById('articleTools');
    var linksHook = articleTools.getElementsByTagName('li');
    for(var i=0; i<linksHook.length; i++){
        //if link has the email class name
        if(linksHook[i].className == "email"){
            PopTrigger = linksHook[i].getElementsByTagName('a');
            }        
        }     
        PopTrigger[0].onclick=EmailPopUp;
    }
}
beenHere = false;

function changeFormBox(status){
formHook = document.getElementById('golfDealsSignUp');
var successCaption = "Thank you!";
var successPara    = "Look for the next issue of the Golf Deals newsletter in your mailbox soon!";
var errorMsg       = "Please enter valid email address";

switch(status){
     case "ok":
              signupBox = document.getElementById('content_padding') || document.getElementById('mbSidebar');
              H3Headings = signupBox.getElementsByTagName('h3');
              H3Headings[0].innerHTML = successCaption;
              formHook.innerHTML = "<p>"+successPara+"</p>";
              //formHook.style.display = "none";
     break;
     case "abort":
              if(!beenHere) {
              beenHere = true;
              new Insertion.Top(formHook,"<p class='error'>"+errorMsg+"</p>");
              }          
     break;
     }
}

function EmailValidator(){
var validEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi;
    if(this.e_mail.value.search(validEmail) == -1){
        setTimeout("changeFormBox('abort')", 100);
        return false;
    } else {
        setTimeout("changeFormBox('ok')", 600);
        return true;
    }

}


function signupFormAction(){
    if(!document.getElementById('golfDealsSignUp')) return false;
    formHook = document.getElementById('golfDealsSignUp');
    formHook.onsubmit = EmailValidator;
}
/* END TL golf*/

// start of _script_dtTime.js [deleted 7th July 2006]// end of _script_dtTime.js

// start of _tl_functions.js
/* NOTE: Consolidate these next three window.open() calls into one function */
function openCurrency() {
$javaConsole("openCurrency function called"+location.href);
	currencyWindow = window.open("http://www.oanda.com/converter/classic?user=travelandleisure","currencyWin","width=600,height=540","toolbar=no","location=no","Status=yes");
	currencyWindow.focus();
}

/* this is used in _display_partner_promotions.cfm */
function openWin(URL,winName,features){
$javaConsole("openWin called"+location.href);
	var aWindow=""
	aWindow=window.open(URL,winName,features);
};

/* this is used in _display_homepage_features.cfm (and maybe elsewhere too) */
function pop_me_up(pURL, features){
$javaConsole("pop_me_up called"+location.href); 
	// The following line names the Originating window "parentWindow"
	// so that the slideshow targets it
	window.name="parentWindow";
	new_window = window.open(pURL, "tlslideWindow", features);
	new_window.focus();	
}

function emailWin(brand,to) {
$javaConsole("emailWin"+location.href);
	window.open('http://www.amexpub.com/applications/email.cfm?brand='+brand+'&to='+to+'&url='+location.href, 'emailwindow', 'width=350,height=350,scrollbars=No,location=No,left=50,top=50,menubar=No,alwaysRaised=No,resizable=Yes,toolbar=No,status=No');
}

function validateEmailSyntax(str){
$javaConsole("validateEmailSyntax"+location.href);
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;
	if (str.indexOf(at,(lat+1))!=-1)
		return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;
	if (str.indexOf(dot,(lat+2))==-1)
		return false;
	if (str.indexOf(" ")!=-1)
		return false;
	return true;
}

function validateEmail(form) {
$javaConsole("validateEmail called"+location.href);
	var error = "";
	
	if (form.e_mail.value == "")
		error = "Please fill in an EMail Address";
	else if (validateEmailSyntax(form.e_mail.value) == false)
		error = "Invalid EMail Address";
			
	if (error != "") 
	{
		window.alert(error);	
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// FUNCTION: formatDate()
// HISTORY: Created 11/12/2002 [me] 
// USAGE: This function can take 0, 1 or 2 arguments and return a formatted date
///////////////////////////////////////////////////////////////////////////////
function formatDate(theDate, format) {
$javaConsole("formatDate"+location.href);
	
	// if no argument is provided to this function, recall the function using Now() as the date
	if (arguments.length == 0) {
		dtNow = new Date();
		return formatDate(dtNow);
	}
	
	// if no format is provided specify a format of Thursday October 14, 2004 
	if (arguments.length < 2) {
		format = "daymmmmdyyyy";
	}
	
	var aDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var aMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	var day = aDays[theDate.getDay()];
	var month = aMonths[theDate.getMonth()];
	var date = theDate.getDate();
	var year = theDate.getFullYear();
	var formattedDate = "";
	
	switch(format) {
		case "daymmmdd":
			formattedDate = day.substring(0,3) + ', ' + month.substring(0,3) + ' ' + date;
			break;
		case "daymmmmdyyyy":
			formattedDate = day + ' ' + month + ' ' + date + ', ' + year;
			break;
		default:
			formattedDate = "the format parameter to the formatDate function is incorrect.";
			break;
	}
	return formattedDate;
}

function REreplace(OrigString,searchStr,replaceStr) {
$javaConsole("REreplace"+location.href);
	var re = new RegExp(searchStr, "g");
	return OrigString.replace(re, replaceStr);
}


// end of _tl_functions.js


