var dtCh= "/";
var minYear=parseInt(new Date().getFullYear(),10);
var maxYear=minYear+3;

var InvalidDateFormat;
var InvalidMonth;
var InvalidDay;
var InvalidYear;
var InvalidDate;
var MissingDestination;
	
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}


function maximizeCurrentWin(){
  try {
    if (window.screen) {
        var aw = screen.availWidth;
        var ah = screen.availHeight;
        window.moveTo(0, 0);
        window.resizeTo(aw, ah);
    }
  }catch (e) {	}	
}

function openMaximized(url) {

  try {
	  var full=window.open(url,"","top=0,left=0,width="+screen.availWidth+",height="+screen.availHeight+",scrollbars=1,toolbar=yes,location=yes,directories=no,status=yes,resizable=yes,menubar=yes");
	  if (document.documentElement.filters) {
		full.resizeTo(w,h);
	  }
  }catch (e) {}     
}



function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth,10)
	day=parseInt(strDay,10)
	year=parseInt(strYr,10)
	if (pos1==-1 || pos2==-1){
		//alert(InvalidDateFormat);
		errMsg="home.htlform.invaliddateformat";
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert(InvalidMonth);
		errMsg="home.htlform.invalidmonth";		
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert(InvalidDay);
		errMsg="home.htlform.invalidday";		
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert(InvalidYear);
		errMsg="home.htlform.invalidyear";				
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert(InvalidDate)
		errMsg="home.htlform.invaliddate";						
		return false
	}

	return true
}





function checkForm(winName){
    var destination = "" + document.hotelsearch.destination[document.hotelsearch.destination.selectedIndex].value;
	
    if (trim(destination)=="noValue") {
		errMsg="home.htlform.missingdestination";
        document.hotelsearch.destination.focus()
        return false
    }

	var sDate=document.hotelsearch.dummy;
	if (isDate(sDate.value)==false){
		sDate.focus()
		return false
	}


    var dd   = parseInt((sDate.value).substring(0,2),10)
    var mm   = parseInt((sDate.value).substring(3,5),10)
    var yyyy = parseInt((sDate.value).substring(6,10),10)

    var oneDay 	= 86400000;
    var inDate = new Date(yyyy,mm-1,dd,23,59,59);

    if ( (inDate < new Date()) ) {
    	errMsg="home.htlform.invaliddate";
        document.hotelsearch.destination.focus()
        return false;

    }

    var nigths = parseInt(document.hotelsearch.nights.value,10);
    var outDate = new Date (inDate.valueOf() + ( oneDay * nigths) );

    if (mm < 10 ) { mm = "0" + mm; }
    if (dd < 10 ) { dd = "0" + dd; }

    var outdd = parseInt(outDate.getDate(),10);
    var outmm = parseInt(outDate.getMonth()+1,10);

    if (outmm < 10 ) { outmm = "0" + outmm; }
    if (outdd < 10 ) { outdd = "0" + outdd; }

    document.hotelsearch.checkIn.value=yyyy + "-" + mm + "-" + dd;
    document.hotelsearch.checkOut.value=outDate.getFullYear()  + "-" + outmm + "-" + outdd ;


    var hURL = document.hotelsearch.gtaurl.value + "?";
    hURL = hURL + "siteid=" + document.hotelsearch.siteid.value + "&";
    hURL = hURL + "country=" + document.hotelsearch.country.value + "&";
    
    if (document.hotelsearch.lang_ != null) {
    	hURL = hURL + "lang=" + document.hotelsearch.lang_.value + "&";	
    } else {
    	hURL = hURL + "language=" + document.hotelsearch.language.value + "&";
    }
    hURL = hURL + "currency=" + document.hotelsearch.currency.value + "&";
    hURL = hURL + "destination=" + document.hotelsearch.destination.value + "&";
    if ( (document.hotelsearch.location.value != null) & (document.hotelsearch.location.value!='')) {
        hURL = hURL + "location=" + document.hotelsearch.location.value + "&";
    }
    hURL = hURL + "checkIn=" + document.hotelsearch.checkIn.value + "&";
    hURL = hURL + "checkOut=" + document.hotelsearch.checkOut.value + "&";
    hURL = hURL + "roomtype=" + document.hotelsearch.roomtype.value + "&";
    hURL = hURL + "rooms=" + document.hotelsearch.rooms.value;
		
    return hURL;
 }


function openPopUp(sURL, winName, winWidth, winHeight){
    if (winWidth == null) winWidth = 790;
    if (winHeight == null) winHeight= 590;
    var new_window=window.open(sURL, winName, "toolbar=no,location=no,directories=no,status=no," +
                      "menubar=no,scrollbars=yes,resizable=no,width=" + winWidth +",height=" + winHeight);
	if (window.focus) {new_window.focus();}
}

function openPopUpResizable( winName, winWidth, winHeight){
    if (winWidth == null) winWidth = 800;
    if (winHeight == null) winHeight= 600;
    var new_window=window.open('', 'msg', "toolbar=no,location=no,directories=no,status=no," +
                      "menubar=no,scrollbars=yes,resizable=yes,width=" + winWidth +",height=" + winHeight);
	if (window.focus) {new_window.focus();}
}

function openPopUpName(sURL, winName){
    var new_window=window.open(sURL, winName);
	if (window.focus) {new_window.focus();}
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}



function execLinkEntry(sUrl) {

  var sValue = "" + sUrl;
  
  var aValue = sValue.split("|");
  
  if (trim(aValue[1])=="") {
		OpenPortalEntry(aValue[0]); 
  } else {
	  OpenWhiteLabelEntry(aValue[0]);
  }

}


function OpenPortalEntry(iso){
	document.location.href="home.do?country="+iso;
}

function OpenWhiteLabelEntry(iso){
	RemoveCookie("GalileoLeisure");
	document.location.href="home.do?country="+iso;
}


function RemoveCookie(name){
	//setting kill date and string
	var kill_time = new Date("January 1, 1970");
	var kill_string = name+"=;expires=" + kill_time.toGMTString();
	
	//overwriting cookie and reloading page
	document.cookie = kill_string;
}

function centerWin(width, height){
 
   var dimension = "";
   if (document.all){ 
   	var x = window.screenLeft; 
  	var y = window.screenTop; 
   	var w = window.document.body.offsetWidth; 
   	var h = window.document.body.offsetHeight; 
   }else{ 
   	var x = window.screenX; 
  	var y = window.screenY; 
   	var w = window.outerWidth; 
   	var h = window.outerHeight; 
   } 
  
   var cntx = x + Math.round((w - width) / 2); 
   var cnty = y + Math.round(h / 4); 
   dimension = 'left=' + cntx + ',top=' + cnty + ',width=' + width + ',height=' + height;
   return dimension;
} 


function loadComponent(sURL){

	var sessId = document.home.SESSIONID.value;
	sURL += ";jsessionid=" + sessId;

	startComponent(sURL);
	
}


function loadComponent(sURL, productPage){

	var sessId = document.home.SESSIONID.value;
	sURL += ";jsessionid=" + sessId + '?'+ productPage;
	startComponent(sURL);
}

function loadOffer(sURL){

	var sessId = document.home.SESSIONID.value;
	sURL += ";jsessionid=" + sessId;
	
	var new_window=window.open(sURL,"offer");
	if (window.focus) {new_window.focus();}

}

function loadGroup(sURL){
	
	var sessId = document.home.SESSIONID.value;
	sURL += ";jsessionid=" + sessId;
	openPopUp(sURL,'group',805,600);

}

function startComponent(sURL){

	var dim = "left=100, top=100, width=360, height=135";
		
	try{
		dim = centerWin(360,135);
	}catch(e){}
	
	var new_window=window.open(sURL, "GLC", "toolbar=no,location=no,directories=no,status=no," +
                  "menubar=no,scrollbars=no,resizable=no,"+dim);
				  
	if (window.focus) {new_window.focus();}
}

function startNewsTickerScroll(){
	var iCount = 0;
	
	try{
     	iCount = document.getElementById('newsCount').value;
	}catch(e){}
	
	if (iCount > 2){
		document.getElementById('marquee1').behavior="scroll";
		document.getElementById('marquee1').start();
	}else{
		document.getElementById('marquee1').behavior="slide";
		document.getElementById('marquee1').start();
	}
}

function execChangeLang(iso){
	document.location.href="/GalileoLeisure/home.do?country="+iso;
}

function setSelectedCountry(iso_code){	
	var select = document.getElementById('iso_country_code');
	for(index = 0; index < select.length; index++) {
	   if(select[index].value.substring(0,3) == iso_code.substring(0,3)){
	     select.selectedIndex = index; 
	   }
	}	
}

function setSelectedLanguage(iso_code){
	var select = document.getElementById('change_lang');
	for(index = 0; index < select.length; index++) {
	   if(select[index].value == iso_code){
	     select.selectedIndex = index; 
	   }
	}
}