﻿var acePopupWindow=null;
function insurance(){
    if (document.all['INSURANCE'].checked==true){
       document.getElementById('ensurancediv').style.visibility='visible';
       document.getElementById('noensurancediv').style.visibility='hidden';
    } else {
       document.getElementById('ensurancediv').style.visibility='hidden';
       document.getElementById('noensurancediv').style.visibility='visible';
    }
}
function acePopup(mypage,myname,w,h,pos,infocus)
{
        if(pos=='random')
                {
                        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
                        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
                }
        if(pos=='center')
                {
                        LeftPosition=(screen.width)?(screen.width-w)/2:100;
                        TopPosition=(screen.height)?(screen.height-h)/2:100;
                }
        else
                if((pos!='center' && pos!='random') || pos==null){LeftPosition=100;TopPosition=100;}
                        settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent=no';
        acePopupWindow=window.open('',myname,settings);
        if(infocus=='back')
        {
                acePopupWindow.blur();
                acePopupWindow.location=mypage;
                acePopupWindow.blur();
        }
        else
                acePopupWindow.location=mypage;
 return false;
}



function openMess(user)
{
        myname='mess_window'+user;
        LeftPosition=20;
        TopPosition=50;
        w=300;
        h=400;
        settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,dependent=no';
        PopupWindow=window.open('',myname,settings);
        PopupWindow.location="messenger.php?action=runmess&mode=app&id="+user;
}


function addBookmark()
{
        window.external.addFavorite('http://stuzim.com.il/', 'stuzim.com.il');
        return false;
}

function openInfo(what, id, wi, he)
{
        myname='Preview_'+ what;
        LeftPosition=(screen.width)?(screen.width-wi)/2:100-wi;
        TopPosition=(screen.height)?(screen.height-he)/2:100-he;
        w=wi+100;
        h=he+200;
        settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,dependent=no';
        PopupWindow=window.open('',myname,settings);
        PopupWindow.location="info.php?action=" + what + "&id=" + id;
}

function cleaner(frm) {
        for(i=0;i<(frm.elements.length-1);i++){
                if( (frm.elements[i].type == "radio") || (frm.elements[i].type == "checkbox") ) frm.elements[i].checked=0;
                if( (frm.elements[i].type == "select") ) frm.elements[i].selected=0;
                if( (frm.elements[i].type == "text") ) frm.elements[i].value="";
        }
}

function setvis(id){
        if (id=='fluss'){
                document.getElementById('fluss').style.visibility='visible';
                document.getElementById('ship_fluss').style.visibility='visible';
                document.getElementById('all').style.visibility='hidden';
                document.getElementById('all').selectedIndex=0;
                document.getElementById('hochsee').style.visibility='hidden';
                document.getElementById('hochsee').selectedIndex=0;
                document.getElementById('ship_hochsee').style.visibility='hidden';
                document.getElementById('ship_hochsee').selectedIndex=0;
                } else {
                document.getElementById('fluss').style.visibility='hidden';
                document.getElementById('fluss').selectedIndex=0;
                document.getElementById('ship_fluss').style.visibility='hidden';
                document.getElementById('ship_fluss').selectedIndex=0;
                document.getElementById('all').style.visibility='hidden';
                document.getElementById('all').selectedIndex=0;
                document.getElementById('hochsee').style.visibility='visible';
                document.getElementById('ship_hochsee').style.visibility='visible';
                }

        }

function setvis_admin(id){
        if (id=='fluss'){
                document.getElementById('fluss').style.visibility='visible';
                //document.getElementById('ship_fluss').style.visibility='visible';
                document.getElementById('all').style.visibility='hidden';
                //document.getElementById('all').selectedIndex=0;
                document.getElementById('hochsee').style.visibility='hidden';
                document.getElementById('hochsee').selectedIndex=0;
                //document.getElementById('ship_hochsee').style.visibility='hidden';
                //document.getElementById('ship_hochsee').selectedIndex=0;
                } else {
                document.getElementById('fluss').style.visibility='hidden';
                document.getElementById('fluss').selectedIndex=0;
                //document.getElementById('ship_fluss').style.visibility='hidden';
                //document.getElementById('ship_fluss').selectedIndex=0;
                document.getElementById('all').style.visibility='hidden';
                //document.getElementById('all').selectedIndex=0;
                document.getElementById('hochsee').style.visibility='visible';
                //document.getElementById('ship_hochsee').style.visibility='visible';
                }

        }

function changePassagier(value,id,where)
{
         document.getElementById(where).value=value;
}

// show text block
function si(id){
	var objImg = document.getElementById(id);
	if(objImg){
		objImg.title = "Da es bislang weltweit kein objektives, offizielles oder einheitliches Klassifizierungssystem für Kreuzfahrtschiffe gibt, setzt sich die Bewertung aus den Angaben der Reedereien, Kundenmeinungen und den Einschätzungen der Kreuzfahrtexperten zusammen und ist damit subjektiv.";
	}
}

function loadTrips(id, URL, dataId, showCurent){
	if(dataId && dataId != undefined){
		try{
			var dataObj = document.getElementById('trip_' + dataId);
			if(dataObj && dataObj != undefined){
				var dataValue =dataObj.value;
				var curId = 'trip_' + dataId + '_row';
				
				var link = URL+"ajax.php?action=trips_cabins";
				var strParams = "&tripData="+dataValue;
				document.getElementById(id).innerHTML = '<div id="left-45"><p class="f10" align="center"><table width="100%"><tr><td align="right"><img src="./img/loading.gif" border="0" width="16" height="16"/></td><td align="left"><b>&nbsp; Verf&uuml;gbarkeitsabfrage - Bitte warten </b></td></tr></table></p></div>';
				new Ajax.Updater(id, link + strParams,
					{ onComplete: function () {
							
							all_rows = document.getElementById('trips_main_tbl');
							if(all_rows && all_rows != undefined){
								cntRows = all_rows.rows.length;
								for(i = 0; i < cntRows; i++){
									
									if(i && all_rows.rows[i].id != curId && showCurent != undefined && showCurent){
										all_rows.deleteRow(i);
										cntRows = all_rows.rows.length;
										i = 0;
									}else if(i && all_rows.rows[i].id == curId){
										all_rows.rows[i].style.backgroundColor = '#B5D7B0';
									}else if(i){
										//var curColorObj = document.getElementById(all_rows.rows[i].id + '_color');
										//all_rows.rows[i].style.backgroundColor = curColorObj.value;
									}
								}
							}
						
					  },
					  onCreate: function () {
												document.getElementById(id).style.display = "block";
											}
					}
				);
			}
		}catch(e){
			alert(e.toString());
		}
	}else{
		return false;
	}
}

function prepareBooking(URL,bType,routeId,tripId,shipId){
	var persons = document.getElementById('persons').value;
	var cabins = document.getElementsByName('cabins');
	var priceId = '';
	for(var i=0;i<cabins.length;i++){
		if(cabins[i].checked){
			priceId = cabins[i].value;
		}
	}

	var redirectUrl = URL + 'booking.php?action=booking&route='+routeId+'&termin='+tripId+'&shipid='+shipId+'&step=1&anzahlpass='+persons+'&price='+priceId;
	switch(bType){
		case "request":
			redirectUrl += '&event=getBookingRequest';
		break;
		case "booking":
			redirectUrl += '&event=setTripAndCabin';	
		break;
	}
	
	window.location.href = redirectUrl;
	return false;
}

	function doCruiseBooking(type,fromDate,toDate,postfix,tripCabinValue){
		if(postfix == undefined){
			postfix = "";
		}

		try{
			var persons = document.getElementById('routeCabinPersons'+postfix).value;
			var child1ageObj = document.getElementById('child_1'+postfix);
			var child2ageObj = document.getElementById('child_2'+postfix);
			var child1age;
			var child2age;
			
			var child1ageStr = '';
			if(child1ageObj && child1ageObj != undefined){
				child1age = parseInt(child1ageObj.value);
				if(child1age && child1age != NaN){
					child1ageStr = "&child1age="+child1age;
				}
			}

			var child2ageStr = '';
			if(child2ageObj && child2ageObj != undefined){
				child2age = parseInt(child2ageObj.value);
				if(child2age && child2age != NaN){
					child2ageStr = "&child2age="+child2age;
				}
			}

			var locationUrl = lang["booking"]+ fromDate+"&departureDate=" + toDate +
				"&persons=" + persons + "&j_language=" + lang['lang'] +"&type="+type+tripCabinValue+lang['type']+child1ageStr+child2ageStr;
				
			var daSize = getDocumentAreaSize();
			var wHieght = 1220;
			if(daSize && daSize[1] && daSize[1] != undefined){
				wHieght = daSize[1] - 40;
			}

			var params = {'type':'url','url':locationUrl,'h':wHieght,'w':880,'ifr_type':'popup'};
			if(postfix){
				params['postfix'] = postfix;
			}
			
			if(detectFirefox()){
				acePopup(locationUrl,'Booking',900,600);
			}else{
				acePopup(locationUrl,'Booking',900,600);
				//openNetzPopup(locationUrl,'cruise_routes');
			}

			if(_gaq && _gaq != undefined) {
				//_gaq.push(['_trackPageview','/booking-form-route']);
			}

			return true;
		}catch(e){
			return false;
		}
	}

	function getInternetExplorerVersion() {
		var rv = -1; // Return value assumes failure.
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var ua = navigator.userAgent;
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				rv = parseFloat(RegExp.$1);
		}
		return rv;
	}

	function getDocumentAreaSize() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		} 
		return [ myWidth, myHeight ];
	}

	function detectFirefox(){
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
			return true;
		}
		return false;
	}

		// return true if the browser is the Internet Explorer 7
	function detectIE7Browser(){
		var ua = navigator.userAgent.toLowerCase();
		var check = function(r) {
			return r.test(ua);
		};

		var isOpera = check(/opera/);
		var isIE = !isOpera && check(/msie/);
		var isIE7 = isIE && check(/msie 7/);
		var isIE8 = isIE && check(/msie 8/);
		var isIE7InIE8 = isIE7 && document.documentMode == 7;
		if(isIE7 || isIE7InIE8){
			return true;
		}
		return false;
	}

	// show popup block by using js TINY library
	function loadBlock(params){
		var type,module,action,strParams="",content="",h=0,w=500,useFullHeight=0;
		if(params && params['w'] && params['w'] != undefined){
			w = params['w'];
		}
		if(params && params['h'] && params['h'] != undefined){
			h = params['h'];
		}
		if(params && params['ufh'] && params['ufh'] != undefined){
			useFullHeight = 1;
		}
		var strBntClose = "<table style='margin-top: 5px;' cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td style='text-align:right;'><a href='javascript:;' onclick='TINY.box.hide();return false;' id='tinybox_close_btn' style='margin-right: 5px;'><img src='http://" +window.location.host + "/img/close.gif' border='0' ></a></td></tr></table><br/>";
		if(params['type'] && params['type'] != undefined){
			type = params['type'];

			try{
				switch(type){
					case 'img':
						if(params['src'] && params['src'] != undefined){
							content = "<img src='" + params['src'] + "' style='width:auto;height:auto;' alt='' />";
						}
						if(detectIE7Browser()){
							w = 250;
						}
					break;
					case 'url':
						var strIFrame;
						if(params['action'] && params['action'] != undefined){
							action = params['action'];
						}

						var openUrl;
						strParams = strParams + "&iframe=1";
						if(action && action != undefined){
							for (prop in params){
								if(prop && prop != undefined && prop != "action" && prop != "src" && prop != "type"){
									strParams = strParams + "&" + prop + "="+params[prop];
								}
							}

							openUrl = "http://" + window.location.host + "/popup.php?action="+action + strParams;
						}else if(params['url'] && params['url'] != undefined){
							openUrl = params['url'];
						}
						
						
						if(openUrl != '') {
							strHeight = 'auto';
							if(h){
								strHeight = h - 42 + 'px';
							}
							if(useFullHeight){
								var documentAreaSize = getDocumentAreaSize();
								if(documentAreaSize.length > 1){
									if(documentAreaSize[1] > 400){
										 documentAreaSize[1] =  documentAreaSize[1] - 100;
									}
									strHeight = documentAreaSize[1] + 'px';
								}
							}
							strIFrame = "<iframe width='100%' style='height:" + strHeight + ";' frameborder='0' src='" + openUrl + "'></iframe>";	
							content = strIFrame;
							if(detectIE7Browser()){
								w = 600;
							}
						}
					break;
					default:
						
				}

				TINY.box.show(strBntClose + content,0,w,h,0,0);
			}catch (e){
				//alert(e.desription);
			}
		}
		return false;
	}

	/*
	 * pid = provider id
	 * sid = service id
	 * did = deck id
	 * cid = cabin id
	 */
	function loadCabins(outputId, pid, sid, did, cid){
		var link = "http://" + window.location.host + "/popup.php?";
		try{
			var params = "action=load_cabins&pid="+pid+"&sid="+sid+"&did="+did+"&cid="+cid;
			new Ajax.Updater(outputId, link + params, 
									{ evalScripts: true,
										onLoaded: function () {
											return true;
										},
										onCreate: function () {
											document.getElementById(outputId).innerHTML = "<tr class='hr_st1'><td colspan='3' style='padding:5px;font-size:16px;' class='ta_c'><img width='20' src=\"./img/loading.gif\"/>&nbsp; Verf&uuml;gbarkeitsabfrage - Bitte warten </td></tr>";
										}
									}
								);
		}catch(e){
			return e.toString();
		}
		return false;
	}

	function showDeck(pid,sid,deckId,cabinId){
		var deck;
		var decks = document.getElementsByName("decksList");

		if(decks != undefined){
			for(i=0;i<decks.length;i++){
				deck = document.getElementById(decks[i].id);
				if(decks != undefined){
					if(deck.id == deckId){
						deck.style.display = "block";
						loadCabins("cabins_"+deckId,pid,sid,deckId,cabinId);
					}else{
						deck.style.display = "none";
					}
				}
			}
		}
		return true;
	}

	function handleDeckCabins(id){
		var hrefEl = document.getElementById(id + '_a');
		if(handleRoomInfo(id)){
			if(hrefEl && hrefEl != undefined){
				hrefEl.style.backgroundImage = "url(./img/minus.gif)";
			}
		}else{
			if(hrefEl && hrefEl != undefined){
				hrefEl.style.backgroundImage = "url(./img/plus.gif)";
			}
		}
	}

	function handleRoomInfo(id,displayStyle){
		if(!displayStyle || displayStyle == undefined){
			displayStyle = 'block';
		}
		var roomInfo = document.getElementById(id);
		var showMinus = true;
		if(roomInfo){
			if(roomInfo.style.display == "none"){
				roomInfo.style.display = displayStyle;
			}else{
				roomInfo.style.display = "none";
				showMinus = false;
			}
		}
		return showMinus;
	}

	function hangleShowAllCabinsBtn(value, postfix){
		if(postfix == undefined){
			postfix = "";
		}
		var btnId = 'btnShowAllCabins' + postfix;
		var btnEl = document.getElementById(btnId);
		if(btnEl && btnEl != undefined){
			if(value){
				btnEl.style.display = "block";
			}else{
				btnEl.style.display = "none";
			}
		}
	}

	function handleChildBoxes(obj,id){
		if(obj){
			var intV = parseInt(obj.value);
			if(intV && intV != NaN){
				$(id).disabled = false;
			}else{
				$(id).options[0].selected = true; // select first before disabling
				$(id).disabled = true;
			}
		}
	}

	function searchRoutes(type, objectId, dateId, outputId, postfix, isDefault, defaultDate){
		var objIncomingDate,strIncomingDate,objectName,module;
		var link = "http://" + window.location.host + "/ajax.php?grandparent=site";
		var objPersonsId = 'routeCabinPersons';
		var objChildsAmount = 2;
		var objChildsPrefix = 'child_';
		var _curDateStr = '';
		if(type){
			try{
				switch(type){
					case "ship":
						strIncomingDate = dateId; // DEFAULT IS USING INSTEAD OF SELECT BOX	
						if(isDefault != undefined && !isDefault){
							objIncomingDate = document.getElementById(dateId);
							if(objIncomingDate != undefined){
								strIncomingDate = objIncomingDate.options[objIncomingDate.selectedIndex].value;
							}
						}

						objectName = "shipId";
						module = "cruise_ships";
					break;
					case "route":
						strIncomingDate = dateId; // DEFAULT IS USING INSTEAD OF SELECT BOX	
						if(isDefault != undefined && !isDefault){
							objIncomingDate = document.getElementById(dateId);
							if(objIncomingDate != undefined){
								strIncomingDate = objIncomingDate.options[objIncomingDate.selectedIndex].value;
								_curDateStr = strIncomingDate;
							}
						}
						
						objectName = "routeId";
						module = "route_trips";
					break;
				}

				if(_curDateStr == '' && defaultDate){
					_curDateStr = defaultDate;
				}

				if(postfix == undefined){
					postfix = "";
				}
				
				if(strIncomingDate == undefined){
					strIncomingDate = "";
				}

				if(strIncomingDate != ""){
					strIncomingDate = strIncomingDate + encodeURI("T12:00:00+00:00");
				}
				
				if(defaultDate == undefined){
					defaultDate = "";
				}

				// collect persons value
				var objPersons = document.getElementById(objPersonsId + postfix);
				var strPersons = "";
				if(objPersons && objPersons != undefined){
					var intPersons = parseInt(objPersons.value);
					if(intPersons != 'NaN' && intPersons > 0){
						strPersons = "&persons=" + intPersons;
					}
				}

				// collect chillds value
				var strChilds = "";
				var cntChilds = 0;
				for(var j = 1; j <= objChildsAmount; j++){
					var objChilds = document.getElementById(objChildsPrefix + j + postfix);
					if(objChilds && objChilds != undefined){
						var intChilds = parseInt(objChilds.value);
						if(intChilds != 'NaN' && intChilds > 0){
							strChilds += "&child" + j + "=" + intChilds;
							cntChilds++;
						}
					}
				}

				strChilds += "&children=" + cntChilds;

				var params = "&action=" + module + "&"+objectName+"="+objectId+"&startDate=" + strIncomingDate +"&postfix="+postfix+"&defaultDate="+defaultDate + strPersons + strChilds;

				new Ajax.Updater(outputId, link + params, 
										{ evalScripts: true,
											onLoaded: function () {
												//return true;
											},
											onCreate: function () {
												document.getElementById(outputId).innerHTML = '<div id="left-45"><p class="f10" align="center"><table width="100%"><tr><td align="right"><img src="./img/loading.gif" border="0" width="16" height="16"/></td><td align="left"><b>&nbsp; Verf&uuml;gbarkeitsabfrage - Bitte warten </b></td></tr></table></p></div>';
												document.getElementById('trip_cabins').style.display = 'none';
											},
											onComplete: function () {
												loadTrips('trip_cabins','http://' + window.location.host + '/',_curDateStr);
											}
										}
									);
			}catch(e){
				return e.toString();
			}
		}
		return false;
	}

	function clearParamsAndSearchRoutes(idSel, type, objectId, dateId, outputId, postfix, isDefault, defaultDate){
		var selEl = document.getElementById(idSel);
		if(selEl && selEl != undefined){
			selEl.options[0].selected = "true";
			searchRoutes(type, objectId, dateId, outputId, postfix, isDefault, defaultDate);
		}
	}

	function getOffset(elem) {
		if (elem.getBoundingClientRect) {
			// "correct" way
			return _getOffsetRect(elem)
		} else {
			//let's work somehow
			return _getOffsetSum(elem)
		}
	}

	// calculate offset by using getBoundingClientRect
	function _getOffsetRect(elem) {
		// (1)
		var box = elem.getBoundingClientRect()
		
		// (2)
		var body = document.body
		var docElem = document.documentElement
		
		// (3)
		var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
		var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
		
		// (4)
		var clientTop = docElem.clientTop || body.clientTop || 0
		var clientLeft = docElem.clientLeft || body.clientLeft || 0
		
		// (5)
		var top  = box.top +  scrollTop - clientTop
		var left = box.left + scrollLeft - clientLeft
		
		return { top: Math.round(top), left: Math.round(left) }
	}
	
	//calculate offset by investigation all parent elements
	function _getOffsetSum(elem) {
		var top=0, left=0
		while(elem) {
			top = top + parseFloat(elem.offsetTop)
			left = left + parseFloat(elem.offsetLeft)
			elem = elem.offsetParent        
		}
		
		return {top: Math.round(top), left: Math.round(left)}
	}

	function showPopup(cell,id,changeCursorStyle){
		var myCell = $(cell);

		if(changeCursorStyle && changeCursorStyle != undefined){
			myCell.setStyle({
				cursor:"help"
			});
		}

		var offset = getOffset(myCell);
		var copyTo = $('roomsPolicy');
		var copyFrom = $("cond_" + id);
		copyTo.innerHTML = copyFrom.innerHTML;

		copyTo.setStyle({
			left:offset.left - copyTo.getWidth()/2 + 50 + "px"
		});

		copyTo.setStyle({
			top:offset.top - copyTo.getHeight() - 20 + "px"
		});

		copyTo.show();
	}

	function hidePopup(cell,changeCursorStyle){
		var myCell = $(cell);
		
		if(changeCursorStyle && changeCursorStyle != undefined){
			myCell.setStyle({
				cursor:"auto"
			});
		}
		var copyTo = $('roomsPolicy');
		copyTo.innerHTML = '';
		copyTo.hide();
	}

	function showFSMap(id){
		if(getInternetExplorerVersion() == 8 ){ // show map in simple popup for IE8 only because of incorrect display gmap in IE8
			acePopup("http://" + window.location.host + "/popup.php?action=route_map&iframe=1&mode=view_map&ufh=1&w=1024&routeId="+id,"FullsizeGoogleRouteMap", 1024,768);
		}else{
			strWidth = 500;
			var documentAreaSize = getDocumentAreaSize();
			if(documentAreaSize.length > 1){
				if(documentAreaSize[0] > 400){
					documentAreaSize[0] =  documentAreaSize[0] - 100;
				}
				strWidth = documentAreaSize[0];
			}
			loadBlock({type:'url',action:'route_map',mode:'view_map',ufh:1,w:strWidth,routeId:id});
		}
	}
