function preloadImages(images)
{
	var preloadedImages = new Array();
    for (var i=0;i<images.length;i++)
    {
        preloadedImages[i] = new Image(490,210);
        preloadedImages[i].src = images[i];
    }
}

function replaceLinkBlockWithInternalImage(container)
{
    var parent = document.getElementById(container);
    var links = parent.getElementsByTagName("a");
    
    for (var i=0;i<links.length;i++)
    {
        var img = links[i].getElementsByTagName("img");
        if (img.length >=1)
        {
            var image = new image();
            image.src = img[0].src;
            links[i].style.background = "transparent url('" + image.src + "' no-repeat";
            links[i].style.display = "block";
            links[i].style.height = image.height;
            img[0].display = "none";
        }
    }
}

function getSender(rootEventSender)
{
    if (rootEventSender.event){return rootEventSender.event.srcElement;}
    else{return rootEventSender;}
}

var eventHandler = {
    add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }
}

function disableTextSelect(element)
{
    if (typeof element.onselectstart != "undefined") {element.onselectstart=function(){return false;};}
    else {element.onmousedown=function(){return false;};}
}

function enableTextSelect()
{
    if (typeof document.onselectstart != "undefined"){document.onselectstart = properSelectStart;}
    else {document.onmousedown=properSelectStart;}
}

function func_targetblank()
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}

var cssClass = {
	set:function(obj,name)
		{obj.className = name;},
	append:function(obj,name)
		{obj.className = obj.className + ' ' + name;},
	clear:function(obj)
		{obj.className = '';},
	get:function(obj)
		{return obj.className;}
}

var cssStyle = {
	set:function(obj,tag,value)
		{
			if (typeof obj.style.setAttribute != "undefined"){obj.style.setAttribute(tag, value);}
			else {obj.style.setProperty(tag, value, null);}
		},
	get:function(obj,tag)
		{
			if (typeof obj.style.getAttribute != "undefined"){return obj.style.getAttribute(tag);}
			else {return obj.style.getPropertyValue(tag);}
		}
		
}

function changeOpacity (element, opacity)

{
	element.style.opacity = parseFloat(opacity / 100);
    element.style.MozOpacity = parseFloat(opacity / 100);
    element.style.KhtmlOpacity = (opacity / 100);
    element.style.filter = "alpha(opacity=" + opacity + ")";
}

function setElementStyle(object, tag, value)
	{cssStyle.set(object,tag,value);}
function getElementStyle(object,tag)
	{return cssStyle.get(object,tag);}
	
var inDrag = false;
var startDragDateValue=new Date();
var endDragDateValue=new Date();

var earliestStayDate = new Date();
var latestStayDate = new Date();
var planner;

var badDays= new Array() 

var fromBox
var toBox

var properSelectStart;

function dateAdd(scale,value,refDate)
{
    var rtDate = new Date();
    if (scale=="d"){rtDate.setTime(refDate.valueOf() + value*1000*60*60*24);}
    if (scale=="h"){rtDate.setTime(refDate.valueOf() + value*1000*60*60);}
    if (scale=="m"){rtDate.setTime(refDate.valueOf() + value*1000*60);}
    if (scale=="s"){rtDate.setTime(refDate.valueOf() + value*1000);}
    if (scale=="ms"){rtDate.setTime(refDate.valueOf() + value);}
    return rtDate;
}

var dateDragger = {
    start:function()
    {var sender = getSender(this);
        if (inDrag == false)
        {
            inDrag = true;
            startDragDateValue.setTime(dateValueFromObjectId(sender));
//          if(sender.cellIndex){startCell = sender;}
//          else{startCell = sender.parentNode;}
            earliestStayDate = dateAdd("d",-13,startDragDateValue);
            latestStayDate = dateAdd("d",13,startDragDateValue);
            if (badDays.length > 0)
            {
                for (var i=0;i<badDays.length;i++)
                {
                    if (badDays[i][0] > startDragDateValue)
                    {
                        if(i>0){earliestStayDate = dateAdd('d',1,badDays[i-1][0]);}
                        else {earliestStayDate.setFullYear(startDragDateValue.getFullYear(),1,1);}
                    }
                    if (badDays[i][0] > startDragDateValue)
                    {
                        latestStayDate = dateAdd('d',-1,badDays[i][0]);
                        i=badDays.length;
                    }
                }
            }
        }
    },

    over:function()
    {
        if (inDrag)
        {
            dateDragger.getDragDates(this);
            plannerUpdate();
        }
    },

    stop: function ()
    {
        if (inDrag)
        {
            dateDragger.getDragDates(this);
            inDrag=false;
            plannerUpdate();
        }
        
    },
    getDragDates: function(senderObj)
    {
        var sender = getSender(senderObj);
        endDragDateValue.setTime(dateValueFromObjectId(sender));

        if (startDragDateValue < endDragDateValue)
        {
            if (endDragDateValue > latestStayDate)
            {
                endDragDateValue.setTime(latestStayDate.valueOf());
                inDrag = false;
            }
        }
        else  
        {
            if (endDragDateValue < earliestStayDate)
            {
                endDragDateValue.setTime(earliestStayDate.valueOf());
                inDrag = false;
            }
        }
    }
}

function plannerDisplay(newClass)
{
    planner.className = newClass;
}

function loadPlanner(tbl,frDateBx,toDateBx)
{
    planner = document.getElementById(tbl);
    
    fromBox = document.getElementById(frDateBx);
    toBox = document.getElementById(toDateBx);
    
    disableTextSelect(planner);
   
    for(var i=1;i<planner.rows.length;i++)
    {
        for (j=1;j<planner.rows[i].cells.length;j++)
        {
            var curCell = planner.rows[i].cells[j];
            if(curCell.className.search('goodday') >= 0)
            {
				eventHandler.add(planner.rows[i].cells[j],"mousedown",dateDragger.start,true);
			}
			
			eventHandler.add(planner.rows[i].cells[j],"mouseover",dateDragger.over,true);
			eventHandler.add(planner.rows[i].cells[j],"mouseup",dateDragger.stop,true);
				
			if (curCell.className.search('badday') >= 0)
			{
			    var dv = new Date;
			    dv.setTime(dateValueFromObjectId(curCell));
			    badDays[badDays.length] = [dv,curCell.id]
			}
        }
    }
    
}

function dateValueFromObjectId(obj)
{
    var refObj;
    if (obj.id){refObj = obj}
    else{refObj = obj.parentNode;}
    return parseDateStringFromId(refObj)
}

function parseDateStringFromId(obj)
{
    return Date.parse(StrReplace(obj.id.substr(obj.id.indexOf('date')+4),'-',' '))
}

function StrReplace(str1,str2,str3)
{
    var rv;
    rv = str1;
    while(rv.indexOf(str2) != -1)
    {rv = rv.replace(str2,str3);}
    return rv;
}


function plannerUpdate()
{
    var startHighlightDate;
    var endHighlightDate;
    
    if (endDragDateValue > startDragDateValue)
    {
        endHighlightDate = endDragDateValue;
        startHighlightDate = startDragDateValue;
    }
    else
    {
        startHighlightDate = endDragDateValue;
        endHighlightDate = startDragDateValue;
    }
   
    fromBox.value = startHighlightDate.toLocaleDateString();
    toBox.value = dateAdd('d',1,endHighlightDate).toLocaleDateString();
    
    $('hotel_fromDate').value=startHighlightDate.toLocaleDateString();
    $('hotel_nights').value=(Math.round(Date.parse(toBox.value)/86400000)-Math.round(Date.parse(fromBox.value)/86400000)).toString();
    
    
//    var loopStartRow = startRow;
//    var loopEndRow = endRow;
//    if(oldStartRow < startRow){loopStartRow = oldStartRow;}
//    if(oldEndRow > endRow){loopEndRow = oldEndRow;}    
    
    for(var i=1;i<planner.rows.length;i++)
    {
        for (j=1;j<planner.rows[i].cells.length;j++)
        {
            if(planner.rows[i].cells[j].id)
            {
                var idDate = new Date();
                var classNames;
                if (planner.rows[i].cells[j].className && planner.rows[i].cells[j].className.search('goodday') > -1)
                {
                    classNames = planner.rows[i].cells[j].className;
                
                    idDate.setTime(parseDateStringFromId(planner.rows[i].cells[j]));

                    if (idDate >=startHighlightDate && idDate <= endHighlightDate && classNames.search('highlighted') < 0)
                    {planner.rows[i].cells[j].className = classNames + ' highlighted';}
                
                    if ((idDate <startHighlightDate || idDate > endHighlightDate) && classNames.search('highlighted') > -1)
                    {planner.rows[i].cells[j].className = classNames.replace('highlighted','');}
                }
            }
        }
    }
//    oldEndRow = endRow;
//    oldStartRow = startRow;
    
}


function func_submit_login(arg_form){
			    func_disableallbuttons();
			    document.getElementById("id_frm_login_action").value = "login";
			    return true;
		    }

            function getexpirydate( nodays){
	            var UTCstring;
	            Today = new Date();
	            nomilli=Date.parse(Today);
	            Today.setTime(nomilli+nodays*24*60*60*1000);
	            UTCstring = Today.toUTCString();
	            return UTCstring;
	            }
            function getcookie(cookiename) {
	            var cookiestring=""+document.cookie;
	            var index1=cookiestring.indexOf(cookiename);
	            if (index1==-1 || cookiename=="") return "";
	            var index2=cookiestring.indexOf(';',index1);
	            if (index2==-1) index2=cookiestring.length;
	            return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
	            }

            function setcookie(name,value,duration){
	            cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
	            document.cookie=cookiestring;
	            if(!getcookie(name)){
	            return false;
	            }
	            else{
	            return true;
	            }
	            }

	function func_disableallbuttons(){
		var arr_buttons = document.getElementsByTagName("input");
		for(var var_counter=0; var_counter<arr_buttons.length; var_counter++){
			if (arr_buttons[var_counter].type == "button" || arr_buttons[var_counter].type == "submit") {
				arr_buttons[var_counter].disabled = true;
			}
		}
	}
	
	var win='';
function func_popupcenter(URLtoOpen, windowName, popupwidth, popupheight, windowFeatures) {
	if (!win.closed && win.location)
	{
		//windows already open, switch focus
		win.focus();
	} else {
		var winl = (screen.width-popupwidth)/2;
		var wint = (screen.height-popupheight)/2;
		win=window.open(URLtoOpen,windowName,'width=' + popupwidth + ',height='+ popupheight +',top=' + wint + ',left=' + winl + ',' + windowFeatures);
		if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
	}
}

function updateTownbyLocation()
                {
                document.getElementById("quickSearchTownID").options.length=1;
                document.getElementById("quickSearchTownID").disabled=true;
                for(i=0;i<towns[document.getElementById("quickSearchLocationID").value].length;i++)
                   {
                     document.getElementById("quickSearchTownID").options[i+1]=new Option(towns[document.getElementById("quickSearchLocationID").value][i].split('|')[1],towns[document.getElementById("quickSearchLocationID").value][i].split('|')[0]);
                    document.getElementById("quickSearchTownID").disabled=false;
                    };
                };
                
            function updateHotelbyLocation()
                {
                document.getElementById("quickSearchHotelID").options.length=1;
                document.getElementById("quickSearchHotelID").disabled=true;
                for(i=0;i<hotels[document.getElementById("quickSearchLocationID").value].length;i++)
                    {
                    document.getElementById("quickSearchHotelID").options[i+1]=new Option(hotels[document.getElementById("quickSearchLocationID").value][i].split('|')[1],hotels[document.getElementById("quickSearchLocationID").value][i].split('|')[0]);
                    document.getElementById("quickSearchHotelID").disabled=false;
                    };
                };
                


            function updateHotelbyTown()
                {
                document.getElementById("quickSearchHotelID").options.length=1;
                document.getElementById("quickSearchHotelID").disabled=true;
                if(document.getElementById("quickSearchTownID").value!='0'){
                    for(i=0;i<hotelsByTown[document.getElementById("quickSearchTownID").value].length;i++)
                        {
                        document.getElementById("quickSearchHotelID").options[i+1]=new Option(hotelsByTown[document.getElementById("quickSearchTownID").value][i].split('|')[1],hotelsByTown[document.getElementById("quickSearchTownID").value][i].split('|')[0]);
                        document.getElementById("quickSearchHotelID").disabled=false;
                        };
                    }
                    else
                    {
                    updateHotelbyLocation();
                    };
                };

function updateNewSearchTownbyLocation()
                {
                document.getElementById("newSearchTownID").options.length=1;
                document.getElementById("newSearchTownID").disabled=true;
                for(i=0;i<towns[document.getElementById("newSearchLocationID").value].length;i++)
                   {
                     document.getElementById("newSearchTownID").options[i+1]=new Option(towns[document.getElementById("newSearchLocationID").value][i].split('|')[1],towns[document.getElementById("newSearchLocationID").value][i].split('|')[0]);
                    document.getElementById("newSearchTownID").disabled=false;
                    };
                };
                
                       