function createXMLHttpRequest() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        throw new Error("XMLHttpRequest not supported");
    }
}

function preview_image(fileStr) {
    myWindow = window.open("", "Preview",
    "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
    myWindow.document.open();
    myWindow.document.write("<HTML><HEAD>");
    myWindow.document.write("<TITLE>Vorschau</TITLE>");
    myWindow.document.write("<LINK REL=StyleSheet HREF='/css/templates.css' TYPE='text/css' MEDIA=screen>");
    myWindow.document.write("</HEAD><BODY BGCOLOR=FFFFFF TEXT=000000>");
    myWindow.document.write("<FORM><CENTER><B><FONT SIZE=2></FONT></B>");
    myWindow.document.write("<TABLE>");
    myWindow.document.write("<TR><TD>");
    // myWindow.document.write("<IMG SRC='file:///" + form.dataFile.value + "'>");  // file: -> for Netscape 4.0
    myWindow.document.write("<IMG SRC='" + fileStr + "'>");  // file: -> for Netscape 4.0
    myWindow.document.write("</TD></TR></TABLE>");
    myWindow.document.write("<INPUT TYPE='button' VALUE='Close' onClick='window.close()'/>");
    myWindow.document.write("</CENTER></FORM>");
    myWindow.document.write("</BODY></HTML>");
    myWindow.document.close();
}

function display_image(imageId) {
	
	// Cross-Browser XMLHttpRequest
	var request = createXMLHttpRequest();
	
	request.open("GET", "http://www.tourfinder.net/do/spot?action=showImage&id=" + imageId, true);
  	
	// Set no-cache for IE
	if (typeof ActiveXObject != "undefined") {
		request.setRequestHeader("Pragma", "no-cache");
		request.setRequestHeader("Cache-Control", "must-revalidate");
		var ifModifiedSince = new Date(0); // January 1, 1970
		request.setRequestHeader("If-Modified-Since", ifModifiedSince);
	}	
	
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
	
			var xmlDoc = request.responseXML;
			
			var spotNodes = xmlDoc.documentElement.getElementsByTagName("spot");
		   	var spotText = spotNodes[0].getAttribute("name");			

			var html = "<div><a onclick='javascript:jQuery.facebox.close();'><img src='/tours/images/img_" + imageId + ".jpg' border='0'/></a><p>" + spotText + "</p></div>"
			
			jQuery.facebox(html);	
			
		}		    
	}
	request.send(null);
}

function display_panorama(imageId) {

	// Cross-Browser XMLHttpRequest
	var request = createXMLHttpRequest();
	
	request.open("GET", "http://www.tourfinder.net/do/spot?action=showImage&id=" + imageId, true);
	
	// Set no-cache for IE
	if (typeof ActiveXObject != "undefined") {
		request.setRequestHeader("Pragma", "no-cache");
		request.setRequestHeader("Cache-Control", "must-revalidate");
		var ifModifiedSince = new Date(0); // January 1, 1970
		request.setRequestHeader("If-Modified-Since", ifModifiedSince);
	}	
  	
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
	
			var xmlDoc = request.responseXML;
			
			var spotNodes = xmlDoc.documentElement.getElementsByTagName("spot");
		   	var imgText = spotNodes[0].getAttribute("name");
			var imgWidth = Number(spotNodes[0].getAttribute("filewidth"));
			var imgHeight = Number(spotNodes[0].getAttribute("fileheight")) + 50;
			// Calc view width
			var windowWidth = $(window).width();
			var resViewWidth = imgWidth;
			if (imgWidth > windowWidth) {
				resViewWidth = windowWidth;
				// Subtract border
				resViewWidth -= 40;
			}	
			var html = "<div style='width:" + resViewWidth + "px; height:" + imgHeight + "px; overflow:auto;'><a onclick='javascript:jQuery.facebox.close();'><img src='/tours/images/img_" + imageId + ".jpg' border='0'/></a><p>" + imgText + "</p></div>"
			
			jQuery.facebox(html);	
			
		}		    
	}
	request.send(null);
}

function display_legend() {
    myWindow = window.open("/do/application?action=showLegend", "Legende",
    "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=300,height=500,screenX=500,screenY=50,left=500,top=50");
    myWindow.focus();
}

function display_help(code) {
    myWindow = window.open("/do/application?action=showHelp&code=" + code, "Hilfe",
    "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=300,height=200,screenX=250,screenY=150,left=250,top=150");
    myWindow.focus();
}

function display_map(url) {
    myWindow = window.open(url, "Karte",
    "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=600,height=650,screenX=100,screenY=20,left=100,top=20");
    myWindow.focus();
}

function refreshRegion() {

    id = document.forms[0].id.value;
    countryId = document.forms[0].countryId.value;
    areaId = document.forms[0].parentId.value;

    window.location.href = "/do/region?action=add&id=" + id + "&countryId=" + countryId + "&areaId=" + areaId;
}

function refreshDrops(entity, drop) {

	if ((entity == "tour") || (entity == "spot") || (entity == "region") || (entity == "product")) {
		document.forms[0].sub.value = "refresh";
	}
    if (drop == "optCountry") {
	    if ((entity == "tour") || (entity == "spot")) {
        	document.forms[0].areaId.value = "";
			document.forms[0].regionId.value = "";
		} else if (entity == "product") {
        	document.forms[0].areaId.value = "0";
			document.forms[0].regionId.value = "0";
	    } else {
		  	// region
		  	document.forms[0].parentId.value = "";
	    }	   
    }
    if (drop == "optArea") {
        document.forms[0].regionId.value = "";
    }
    if (drop == "optLinkTypeMain") {
        document.forms[0].sub.value = "refreshType";
    }
    if (drop == "optLink") {
        document.forms[0].sub.value = "refresh";
    }
    if (drop == "optContentMenu") {
        document.forms[0].sub.value = "refreshMenu";
    }
    if (drop == "optContentCategory") {
        document.forms[0].sub.value = "refresh";
    }
    
    id = document.forms[0].id.value;
    nextAction = document.forms[0].action.value;
    if (nextAction == "store") {
        document.forms[0].action.value = (id == "") ? "add" : "edit";
    }    
    if (nextAction == "search") {
        document.forms[0].action.value = "searchPrompt";
    }
    if (nextAction == "delete") {
        document.forms[0].action.value = "deletePrompt";
    }
    if (entity == "tour") {
    	if ((nextAction != "search") && (document.forms[0].type.value == "2")) {
			// Multitour -> set Options (to be saved)
			setAllOptions(document.forms[0].selectedTours);
		}	
	}
    if ((entity == "link") || ((entity == "product") && (nextAction != "search"))) {
		// LinkCategories -> set Options (to be saved)
		setAllOptions(document.forms[0].selectedTourCategories);
	}
		
    document.forms[0].submit();
}

function refreshTourForm() {

    id = document.forms[0].id.value;
    action = document.forms[0].action.value;
	
    if (action == "store") {
    	document.forms[0].action.value = "add";	
    } else if (action == "storeWptFile") {
    	document.forms[0].action.value = "addWptFile";
    	document.forms[0].menutoken.value = "11610";
    } else if (action == "replaceTrack") {
    	document.forms[0].action.value = "replaceTrackPrompt";
    	document.forms[0].menutoken.value = "11620";		
    }	
	
	document.forms[0].submit();
}

function refreshFlashAppPreview() {

    flashAppId = document.forms[0].id.value;
    profileId = document.forms[0].flashAppProfileId.value;
    catId = document.forms[0].tourCategoryId.value;

    window.location.href = "/do/flashapplication?action=show&menutoken=51110&flashappprofileid=" + profileId + "&flashAppId=" + flashAppId + "&categoryId=" + catId;
}

function refreshOwnTours() {

    catId = document.forms[0].tourCategoryId.value;

    window.location.href = "/do/user?action=ownTours" + "&categoryId=" + catId;
}

function setAllOptions(objSelect) {

	for (var i = 0; i < objSelect.options.length; i++) {
		objSelect.options[i].selected = true;
	}

}

function wayTypeChanged() {

    wayTypeId = document.forms[0].wayTypeId.value;
	if ((wayTypeId == null) || (wayTypeId == '')) {
		document.forms[0].wayTypeSearchCrit.value = null;
		document.forms[0].wayTypeSearchPerc.value = '';
	} else {
		document.forms[0].wayTypeSearchCrit.value = 1;
		document.forms[0].wayTypeSearchPerc.value = 30;
	}
}

function initProClueTip() {
	if (typeof ActiveXObject != "undefined") {
		// IE
		$('a.pro-clue').cluetip({local:true, showTitle:false, cursor:'pointer', arrows:false, width:200, positionBy:'fixed', topOffset:70, leftOffset:5, cluetipClass:'jtip'});
	} else {
		$('a.pro-clue').cluetip({local:true, showTitle:false, cursor:'pointer', arrows:false, width:200, positionBy:'fixed', topOffset:-70, leftOffset:5, cluetipClass:'jtip'});
	}	
}

function update_sta(typeId, itemId) {

	// Cross-Browser XMLHttpRequest
	var request = createXMLHttpRequest();
	
	request.open("GET", "http://www.tourfinder.net/do/statistic?action=update&typeId=" + typeId + "&itemId=" + itemId, true);
	request.send(null);

}	