
function setArrivalDate(value, type) {
    var currDay = window.document.frmQuickFind.SrchFromDay.value;
    var currMonth = window.document.frmQuickFind.SrchFromMonth.value;
    var currYear = window.document.frmQuickFind.SrchFromYear.value;

    if(currDay.length==1) {
        currDay = '0'+currDay;
    }
    if(currMonth.length==1) {
        currMonth = '0'+currMonth;
    }
    if((type==1 || type==2) && value.length==1) {
        value = '0'+value;
    }

    if(type==0) { //called if date does not change or on automatic date reset
        currDateString = new String(currYear+''+currMonth+''+currDay);
        window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
    } else {
        if(type==1) { //day to change
            currDateString = new String(currYear+''+currMonth+''+value);
        }
        if(type==2) { //month to change
            currDateString = new String(currYear+''+value+''+currDay);
        }
        if(type==3) { //year to change
            currDateString = new String(value+''+currMonth+''+currDay);
        }
    }
} //setArrivalDate

/*Function to check whether the search date is in the past*/
function checkSearchDate(searchDate) {
    var currDate = new Date();
    if(searchDate.getYear() < currDate.getYear()) {
        alert("Das Jahr liegt in der Vergangenheit. Bitte nehmen Sie eine Korrektur vor!");
        return false;
    }
    if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() < currDate.getMonth()) {
        alert("Der Monat liegt in der Vergangenheit. Bitte nehmen Sie eine Korrektur vor!");
        return false;
    }
    if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() == currDate.getMonth() &&
    searchDate.getDate() < currDate.getDate()) {
        alert("Der Tag liegt in der Vergangenheit. Bitte nehmen Sie eine Korrektur vor!");
        return false;
    }
    return true;
} //checkSearchDate

/*Helper function to check the query string passed to the system*/
function displayQueryString(form) {
    var sfDay = form.SrchFromDay.value;
    var sfMonth = form.SrchFromMonth.value;
    var sfYear = form.SrchFromYear.value;
    var qfdNights = form.qfdNights.value;
    var qfdAdults = form.qfdAdults.value;
    var qfdArrivalDate = form.qfdArrivalDate.value;
    //var qfdCategories = form.qfdCategories.value;
    var baseUrl = form.action;
    var url = baseUrl+'?'+'qfdArrivalDate='+qfdArrivalDate+
    '&SrchFromDay='+sfDay+'&SrchFromMonth='+sfMonth+'&SrchFromYear='+sfYear+
    '&qfdNights='+qfdNights+'&qfdAdults='+qfdAdults;
    alert(url);
} //displayQueryString

function displayQueryString(form) {
    var sfDay = form.SrchFromDay.value;
    var sfMonth = form.SrchFromMonth.value;
    var sfYear = form.SrchFromYear.value;
    var qfdNights = form.qfdNights.value;
    var qfdAdults = form.qfdAdults.value;
    var qfdArrivalDate = form.qfdArrivalDate.value;
    //var qfdCategories = form.qfdCategories.value;
    var baseUrl = form.action;
    var url = baseUrl+'?'+'qfdArrivalDate='+qfdArrivalDate+
    '&SrchFromDay='+sfDay+'&SrchFromMonth='+sfMonth+'&SrchFromYear='+sfYear+
    '&qfdNights='+qfdNights+'&qfdAdults='+qfdAdults;
    alert(url);
} //displayQueryString

function validateForm(form) {
    var d = parseInt(form.SrchFromDay.value);
    var m = parseInt(form.SrchFromMonth.value);
    var j = parseInt(form.SrchFromYear.value);

    if ( !checkDate(d,m,j)) {
        alert("Das gegebene Datum existiert nicht (z.B. 31.02.)! Überprüfen Sie Ihre Eingabe!") ;
        form.SrchFromDay.focus();
        return false;
    }

    if ( !checkDate2()) {
        form.SrchFromDay.focus();
        return false;
    }
} //validateForm

function checkDate(d, m, j) {
    var dt = new Date(j, m-1, d);
    if (dt.getDate() != d || dt.getMonth()+1 != m) {
        return false;
    }
    return true;
} //checkDate

function checkDate2() {
    var currDay = window.document.frmQuickFind.SrchFromDay.value;
    var currMonth = window.document.frmQuickFind.SrchFromMonth.value;
    var currYear = window.document.frmQuickFind.SrchFromYear.value;
    var searchDate = new Date(currYear, currMonth-1, currDay);
    var check = checkSearchDate(searchDate);
    
    if(typeof currDateString == 'undefined')     
    	setArrivalDate(currDay, 1);
        
    if(check == false) {
        setCurrentDay();
        return false;
    } else {
        window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
        return true;
    }
} //checkDate2



Array.prototype.contains = function(obj) {
	var i, listed = false;
	for (i=0; i<this.length; i++) {
		if (this[i] === obj) {
			listed = true;
			break;
		}
	}
	return listed;
};

KRISPL = {
	
	Layout: {
		init: function() {
			this.checkContentHeight();
			this.initMainNav();
			this.initLangNav();
			this.initQuickAnfrage();
			this.initNuggets();
		},
		
		checkContentHeight: function() {
			var sidebarHeight = jQuery('div#sidebar').height();
			var contentHeight = jQuery('div#content').height();
			
			if( contentHeight < (sidebarHeight-60) ) {
				var newContentHeight = sidebarHeight-60;
				jQuery('div#content').height(newContentHeight+'px');
			}
			else if( (sidebarHeight-60) < contentHeight ) {
				jQuery('div#sidebar').height(contentHeight+60+'px');
			}
		},
		
		initMainNav: function() {
			var mainNavWidth = 960;
			
			jQuery('a.mainNavLink', '#mainNavList').each(function() {
				
				var el = jQuery(this);
				
				try {
				
					linkPosition = el.position();								
					subNavList = el.next('ul.subNavList');
					subNavPosAdjust = mainNavWidth - subNavList.width();
					
					
					if (subNavList.width()) {
						if (subNavPosAdjust < linkPosition.left) {
							subNavList.css("left", subNavPosAdjust);
						}
						else {
							subNavList.width(mainNavWidth - linkPosition.left);
							subNavList.css("left", linkPosition.left);
						}
					}
				 
				} catch(e) { }
				
			});
			
			jQuery("ul#mainNavList").superfish({
				animation : { opacity:"show", height:"show" },
				hoverClass: 'active',
				delay: 700
			}).find('ul').bgIframe({opacity:false}); 
		},
		
		initLangNav: function() {
			var availableLangCont = jQuery('div.availableLang');
			var posLeftStart = availableLangCont.outerWidth() * -1;
			var posLeftTarget = jQuery('.currentLang').outerWidth() + 5;
			
			jQuery('.currentLang').mouseenter(function(){
				if(!availableLangCont.is(":animated"))
					availableLangCont.animate({
						left: posLeftTarget
					}, 1000, 'swing');		
			});
			
			jQuery('#containerLangNav').mouseleave(function(){	
				availableLangCont.animate({
					left: posLeftStart
				});
			});
		},
		
		initQuickAnfrage: function() {
			var quickAnfrageCont = jQuery('#containerQuickAnfrage');
			jQuery('#quickAnfrageBtn').toggle(function(){
				quickAnfrageCont.animate({left: 0}, 'slow', function(){
					jQuery('#quickAnfrageBtn').css('background-position', 'top right');
				});
			}, function(){
				quickAnfrageCont.animate({left: -210}, 'slow', function(){
					jQuery('#quickAnfrageBtn').css('background-position', 'top left');
				});
			});
		},
		
		initNuggets: function() {
			jQuery('ul#newsSwitcherNav li span').click(function() {
				jQuery('div#newsSwitcherContent div').removeClass('active');
				jQuery('div#newsSwitcherContent div#'+jQuery(this).attr('rel')).addClass('active');
				jQuery('ul#newsSwitcherNav li span:not(rel='+jQuery(this).attr('rel')+')').removeClass('active');
				jQuery(this).addClass('active');
			});
		}
	},
	Feratel: {
		fillFeratelQuickAnfrageForm: function(lang) {
			if( lang=='en' ) {
				monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
				durationSuffixSingle = 'Night';
				durationSuffixMulti = 'Nights';
			}
			else {
				monthNames = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
				durationSuffixSingle = 'Nacht';
				durationSuffixMulti = 'Nächte';
			}
		
			
			// days
			var dayOptionsHtml = '';
			for( i=1; i<=31; i++ ) {
				dayOptionsHtml = dayOptionsHtml + "\n"+'<option value="'+i+'">'+i+'</option>';
			}
			jQuery("#SrchFromDay").html(dayOptionsHtml);
			
			// month
			var monthOptionsHtml = '';
			for( i=0; i<=11; i++ ) {
				monthOptionsHtml = monthOptionsHtml + "\n"+'<option value="'+(i+1)+'">'+ monthNames[i] +'</option>';
			}
			jQuery("#SrchFromMonth").html(monthOptionsHtml);
			
			// year
			var yearOptionsHtml = '';
			var yearDateObj = new Date();
			var currYear = yearDateObj.getFullYear();
			for( i=currYear; i<=currYear+4; i++ ) {
				yearOptionsHtml = yearOptionsHtml + "\n"+'<option value="'+i+'">'+i+'</option>';
			}
			jQuery("#SrchFromYear").html(yearOptionsHtml);
			
			// duration
			var durationOptionsHtml = '';
			for( i=1; i<=21; i++ ) {
				if( i==1 ) { durationLabel = durationSuffixSingle; }
				else { durationLabel = durationSuffixMulti; }
				
				durationOptionsHtml = durationOptionsHtml + "\n"+'<option value="'+i+'">'+i+' '+durationLabel+'</option>';
			}
			jQuery("#qfdNights").html(durationOptionsHtml);
						
			/* Ausgewählte Felder wieder setzen */
			var queryParams = KRISPL.Misc.getQueryString();
			for (var strKey in queryParams){
				
				if(strKey == "Submit") 
					continue; 
					
				if(document.getElementsByName(strKey)[0]) {		
					var el = document.getElementsByName(strKey)[0];
					el.value = unescape(queryParams[ strKey ]);
				}
			}
			
			var date = new Date();
			date.setDate(date.getDate()+3);
			
			//standardauswahl setzen
			var defaults = {		
				"SrchFromDay":		date.getDate(),
				"SrchFromMonth":	(date.getMonth() + 1),
				"SrchFromYear":		date.getFullYear(),
				"qfdNights": 		5
			};
			
			for(var field in defaults) {
			
				if(!queryParams[field]) {			
					if(document.getElementById(field)) {
						var val = defaults[field];				
						document.getElementById(field).value = val;
					}
				}
			}			
			
		},
		insertFeratelFrame: function(lang) {			
			var queryStr = location.search;
			//var frameSrc = 'http://feratel-tennengau.concentrator.net/a1t2/tosc/srch_ServiceProvider_list.asp'+queryStr;
			var frameSrc = 'http://web.deskline.net/tennengaukrispl/'+ lang +'/qfind_accommodation'+ queryStr;
			document.write('<iframe name="feratelSearchFrame" id="feratelSearchFrame" width="100%" height="1000" src="'+frameSrc+'" frameborder="0"></iframe>');
		},	
		
	},
	Misc: {
		getURLParam: function(srch, strParamName) {
			var strReturn = "";
			var strHref = srch;
			if ( strHref.indexOf("?") > -1 ){
				var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
				var aQueryString = strQueryString.split("&");
				for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
					if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ) {
						var aParam = aQueryString[iParam].split("=");
						strReturn = aParam[1];
						break;
					}
				}
			}
			return unescape(strReturn);
		},
		
		getQueryString: function() {
			// Build an empty URL structure in which we will store
			// the individual query values by key.
			var objURL = new Object();
		
		
			// Use the String::replace method to iterate over each
			// name-value pair in the query string. Location.search
			// gives us the query string (if it exists).
			window.location.search.replace(
				new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
		
				// For each matched query string pair, add that
				// pair to the URL struct using the pre-equals
				// value as the key.
				function( $0, $1, $2, $3 ){
					objURL[ $1 ] = $3;
				}
			);
			
			return objURL;
		}

	}	
};

