﻿/*
#region "History"
'20090304 - RH - Existance of SearchParameters field now triggers an AJAX search, via Website.Results.Start()
'20090309 - RH - Added basket class to TourOS object
'20090310 - RH - Altered searchbox startup check
'20090310 - RH - Added lights on/off functions
'20090311 - RH - Added accommodation class
'20090312 - RH - Error handler now redirects to error.aspx
'20090313 - RH - Print functions added.
'20090313 - RH - Added alert function
'20090315 - RH - Added accommodation search startup function
'20090324 - RH - Calendar Improvements
'20090327 - RH - Calendar fixed.
'20090327 - RH - ie6 fix, added code to hide all form elements when lights off.
'20090327 - RH - Tidy up
'20090331 - RH - Tidy up
#end region
*/

var Website=new TourOS(); 

function TourOS() 
{
    this.SearchBox=new SearchBox();
    this.Results=new Results();
    this.Basket=new Basket();
    this.AccommodationSearch=new AccommodationSearch(); 
    
    this.Startup=function() {
        if($('SearchBox_Visible')) this.SearchBox.Start(); 
        if($('SearchParameters')) this.Results.Start(); 
        if($('Basket_Visible')) this.Basket.Start();
        if($('AccommodationSearch_Visible')) this.AccommodationSearch.Start();
        
        if($('message')) this.Alert('Message', $F('message'));
        if ($('warning')) this.Alert('Warning', $F('warning'));
        if ($('newsletter')) this.Alert('Newsletter Signup', 'Thank you for signing up to our newsletter');        
    };
           
    this.Print=function() {
    
       /* if($('right_colunm')) $('right_colunm').hide();
        $('main_box').hide();
        $('header').hide();
        $('leftcolunm').hide(); 
        $('footer').hide(); */
        window.print();
    };
    
    this.ExitPrint=function() {
        if($('right_colunm')) $('right_colunm').show();
        $('main_box').show();
        $('header').show();
        $('leftcolunm').show(); 
        $('footer').show();
    };
    
    this.Alert=function(title, message) {
        this.SnapTop();
        this.LightsOff();
        
        $('alert_title').innerHTML=title;
        $('alert_message').innerHTML=message;
        $('alert').show();
    };
    
    this.Wait=function(title, message) {
        this.SnapTop();
        this.LightsOff();        
       /*$('floatwait_title').innerHTML=title;
        $('floatwait_message').innerHTML= ''; */         
        $('floatwait').show();          
    };    
    
    this.HandleError=function(e) {
        document.location.href='error.aspx?e=' + e;
    };  
    
    this.Unload=function() {
        this.Results.Unload();       
    }; 
    
    this.LightsOff=function() {
        this.HideFormElements();
        $('BlackOut').show();
        $('BlackOut').setStyle({width : this.Width(), height : this.Height(), opacity : 0.5});
        $('BlackOut').setOpacity(0.8);
    };
    
    this.LightsOn=function() {    
        $('BlackOut').hide(); 
        this.ShowFormElements();
    };
    
    this.Width=function() {
        return getPageSize()[0] + 'px';
    };
    
    this.Height=function() {
        return getPageSize()[1] + 'px';
    };
    
    this.SnapTop=function() {
        document.body.scrollIntoView(true);
    };
    
    this.HideFormElements=function() {    
        if((navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1)){
            var f = $$('select');
        	
	        for(var i=0; i<f.length; i++) {
		        f[i].style.visibility='hidden';
	        }
        	
            var f = $$('input');
        	
	        for(var i=0; i<f.length; i++) {
		        f[i].style.visibility='hidden';
	        }	
	    }
    };
    
    this.ShowFormElements=function() {
        var f = $$('select');
    	
	    for(var i=0; i<f.length; i++) {
		    f[i].style.visibility='visible';
	    }
    	
        var f = $$('input');
    	
	    for(var i=0; i<f.length; i++) {
		    f[i].style.visibility='visible';
	    }	
    };

    //displaydate, highlightdate, elementid to hold response, elementid to hold calendar
    this.DrawCalendar=function(dd,hd,container,target) {
        this.HideFormElements();
        $(container).show();
                
	    var mn=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	    var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
    	
    	var displayDate=new Date(dd);
    	var dy=displayDate.getFullYear();
    	var dm=displayDate.getMonth();
    	
    	var selectedDate=new Date(hd);
    	var sd=selectedDate.getDate();
    	var sm=selectedDate.getMonth();
    	var sy=selectedDate.getFullYear();
    	
	    var oD = new Date(dy, dm, 1); //DD replaced line to fix date bug when current day is 31st
	    oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
    	
	    var todaydate=new Date() //DD added
    	
	    dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
    	
	    var t='<table cellpadding="0" cellspacing="1" border="0" class="Calendar" cols="7">';
    	
    	dy=parseInt(dy);
    	dm=parseInt(dm);
    	
	    var nm=dm+1;
	    var pm=dm-1;
	    var ny=dy;
	    var py=dy;
    	
	    if(dm==11) {
		    nm=0;
		    pm=10;
		    ny=dy+1;
	    }
    	
	    if(dm==0) {
		    pm=11;	
		    nm=1;
		    py=dy-1;
	    }
    	   	
	    t+='<tr><th colspan="1">';
	    t+='<a href="javascript: Website.DrawCalendar(\'1 '+mn[pm]+' '+py+'\',\''+hd+'\',\'' + container + '\',\'' + target + '\');"><img src="graphics/resultset_previous.png" border="0"/></a>';
	    t+='</th><th colspan="5">' + mn[dm]+ '</th>';
	    t+='<th colspan="1">';	
	    t+='<a href="javascript: Website.DrawCalendar(\'1 '+mn[nm]+' '+ny+'\',\''+hd+'\',\'' + container + '\',\'' + target + '\');"><img src="graphics/resultset_next.png" border="0"/></a>';	
	    t+='</th></tr>';
    	
	    t+='<tr><th colspan="1">';
	    t+='<a href="javascript: Website.DrawCalendar(\'1 '+mn[dm]+' '+(dy-1)+'\',\''+hd+'\',\'' + container + '\',\'' + target + '\');"><img src="graphics/resultset_previous.png" border="0"/></a>';
	    t+='</th><th colspan="5">' + dy + '</th>';
	    t+='<th colspan="1">';	
	    t+='<a href="javascript: Website.DrawCalendar(\'1 '+mn[dm]+' '+(dy+1)+'\',\''+hd+'\',\'' + container + '\',\'' + target + '\');"><img src="graphics/resultset_next.png" border="0"/></a>';	
	    t+='</th></tr><tr>';
    		
	    for(s=0;s<7;s++) t+='<th>'+"SMTWTFS".substr(s,1)+'</th>';

	    t+='</tr><tr>';
    	
	    for(i=1;i<=42;i++) {
		    if((i-oD.od>=0)&&(i-oD.od<dim[dm])) {
			    t+='<td ';
    			
			    if((i-oD.od+1)==sd && dm==sm && dy==sy) {
				    t+=' class="SELECTED" ';
			    }
    			
			    t+=' onClick="$(\'' + target + '\').value=\'' + (i-oD.od+1) + ' ' + mn[dm] + ' ' + dy + '\';$(\'' + container + '\').hide();Website.ShowFormElements();">';
    			    	
			    t+=(i-oD.od+1);

			    t+='</td>';
		    }
		    else {
			    t+='<td>&nbsp;</td>';
		    }
    		
		    if(((i)%7==0)&&(i<36)) {
			    t+='</tr><tr>';
		    }
	    }
    	
    	t+='</tr><tr><th colspan="7"  onClick="$(\'' + container + '\').hide();Website.ShowFormElements();" style="cursor: pointer;">Close</th></tr>';
    	
	    t+='</table>';
    	
	    $(container).innerHTML=t;    
    };
};

/*USEFUL STUFF================================================================*/

function RemoveFromBasket(CacheID, What)
{
    if(CacheID.length>0)
    {
        if(confirm('Remove ' + What + '.\nAre You Sure?'))
        {
           Website.Wait();
            document.location.href='RemoveFromBasket.aspx?cid=' + CacheID;
        }
    }
}

function formatCurrency(s) {
    return '£' + parseFloat(s).toFixed(2);
}


function ticks() {
    return new Date().getTime();
}

function qsb(qs, name, value) {
    return qs + '&' + name + '=' +value;
}

function HasItem(dd, s) {
    if(dd.options.length>0) {
        for(var i=0; i<dd.options.length; i++) {
            if(dd.options[i].value==s) {
                return true;
                break;
            }
        }        
    }
    return false;
}

function showhide(elementid) {
    try {
        if($(elementid).style.display!='none') {
            $(elementid).style.display='none';
        } else {
            $(elementid).style.display='';
        }
    } catch(e) {
        alert('Could Not Find: ' + elementid);
    }
}

function show(elementid)
{
    try {  
        //alert('change to $(\'' + elementid + '\').show()');
        $(elementid).style.display='';
    } catch(e) {
        alert('Could Not Find: ' + elementid);
    }
}

function hide(elementid) {
    try {
       // alert('change to $(\'' + elementid + '\').hide()');
        $(elementid).style.display='none';
    }
    catch(e) {
        alert('Could Not Find: ' + elementid);
    }
}

function disable(elementid) {
    $(elementid).disabled=true;
}

function enable(elementid) {
    $(elementid).disabled=false;
}

function RestrictInput(evt) {	
	var charCode = (evt.which) ? evt.which : window.event.keyCode;

	if (charCode <= 13) {
		return true;
	} else {
		var keyChar = String.fromCharCode(charCode);
		var re = /[\sa-zA-Z0-9.,_@'-]/;
		return re.test(keyChar);			
	}	
}

function getPageSize() 
{        
    var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}				
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
    
    return[pageWidth,pageHeight];
}
/*POPUP STUFF================================================================*/