/**
 * General TOSC 2, module tosc javascript file.
 *
 * Here are put functions that are globaly or many times used.
 */



/**
 * This is frame width fix fo IE
 *
 */
var isResized = false
if(self != top && document.all){
	setTimeout( "fixWidth()",10)
}
function fixWidth(){
	if(self.document.body == null || document.documentElement == null){
		setTimeout( "fixWidth()", 10)
	}else{
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
		} else if( self.document.documentElement && ( self.document.documentElement.clientWidth || self.document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = self.document.documentElement.clientWidth;
		    myHeight = self.document.documentElement.clientHeight;			    
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
		}
		if(self.document.body.clientWidth != myWidth){	
		 	self.document.body.style.width= myWidth;
		 	isResized = true
		}else{
			if(!isResized){
				if(myHeight >= self.document.body.clientHeight && self.document.body.clientHeight > 0) isResized = true
				setTimeout( "fixWidth()", 10)			
				return;
			}
		}
	}
	setTimeout( "fixWidth()", 2000)						
}	





/**
 * -------------------------------------------------------------------------
 *  Global variables
 * -------------------------------------------------------------------------
 */

var g_httpURL = "";
var g_httpsURL = "";


/**
 * -------------------------------------------------------------------------
 *  General functions
 * -------------------------------------------------------------------------
 */

/**
 * Opens a new pop-up dialog window without a name.
 * <p>
 * The window is opened with default settings for the screen position.
 * By default, the window is opened without a menubar, statusbar, locationbar
 * but allows resizing.
 *
 * @param url The URL for the new or existing named window.
 * @param height The height of the window.
 * @param width The width of the window.
 * @param scroll Flag if scrolling is turned on.
 */
function openPopupDialog(url, height, width, scroll)
{	
	var rand = Math.random();
	var name = "win_" + Math.round(rand*1000);
	if (!scroll) scroll = true;

	openNamedDialog(url, name, height, width, scroll);
}


/**
 * Opens a standard named pop-up dialog window or puts the focus on an existing one.
 * <p>
 * The window is opened with default settings for the screen position.
 * By default, the window is opened without a menubar, statusbar, locationbar
 * but allows resizing.
 *
 * @param url - The URL for the new or existing named window.
 * @param name - The name of the pop-up window.
 * @param height - The height of the window.
 * @param width - The width of the window.
 * @param scrollbars - Flag if scrolling is turned on (yes|no|auto).
 * @param location - Should the window have location bar (yes|no).
 * @param menubar - Should the window have menubar (yes|no).
 * @param resizable - Should the window be resizable (yes|no).
 * @param status - The status line.
 * @param leftpos - The left upper position of window.
 * @param toppos - The top upper position of window.
 */
function openNamedDialog(url, name, height, width, scrollbars, location, menubar, resizable, status, leftpos, toppos)
{	
	var toolbar = false;
	if (!scrollbars) scrollbars = true;
	if (!location) location = false;
	if (!menubar) menubar = false;
	if (!resizable) resizable = true;
	if (!status) status = true;
	if (menubar == true) toolbar = true;
	//unifying this for all browsers:
	//features which are supported by them all are following
	
	var features = "dependent=yes"; //window is closed with closure of parent
	
	features += ",height=" + height;
	features += ",width=" + width;
	
	if (leftpos) features += ",left=" + leftpos; //left upper position of window
	if (toppos) features += ",top=" + toppos;
	if (leftpos) features += ",screenX=" + leftpos;
	if (toppos) features += ",screenY=" + toppos;
	
	if (location == true) { features += ",location=yes" } else features += ",location=no";
	if (menubar == true) { features += ",menubar=yes" } else features += ",menubar=no";
	if (resizable == true) { features += ",resizable=yes" } else features += ",resizable=no";
	if (scrollbars == true) { features += ",scrollbars=yes" } else features += ",scrollbars=yes"; 
	//this is no hack, but per define all windows MUST have scrollbars !
	if (toolbar==true) { features += ",toolbar=yes" } else features += ",toolbar=no";

	var win = window.open(url, name, features);
	
	if (win.focus) 
		win.focus();
}


/**
 * Updates the window which opened the current window with the given URL.
 * If the window already displays the given URL then a reload is made.
 * Does nothing if the opener is null.
 *
 * @param url The URL to load.
 */
function updateOpener(url)
{
	updateWindow(window.opener, url);
}


/**
 * Updates the window with the given URL.
 *
 * Does nothing if the window is null.
 *
 * @param win The window object, maybe null.
 * @param url The URL to load, if null then the page is reloaded.
 */
function updateWindow(win, url)
{	
	if (win)
	{
		var loc = win.location;
		if (url)
			loc.replace(url);
		else
			loc.reload();	
	}
}


/**
 * Updates the frame of the current window with the given name.
 *
 * Does nothing if the name is null or the frame with the given
 * name does not exist.
 *
 * @param name The name of the frame, maybe null.
 * @param url The URL to load.
 */
function updateFrame(name, url)
{
	if (name)
	{
		var win = window.top.frames[name];
		if (win)
			updateWindow(win, url);
	}
}


/**
 * Switch the contents of a frame.
 */
function switchFrame(frameName, url)
{
	window.top.frames[frameName].location.replace(url);
}


/**
 * Updates all frames of the top-level window.
 */
function updateAllFrames()
{
	var f = window.top.frames;
	for (var i = 0; i < f.length; i++)
	{
	  f[i].location.reload();  	
	}
}


/**
 * Switch the contents of the top frame.
 */
function switchTopFrame(url)
{
	window.top.location.replace(url);
}


function isMaxLength(obj,countobj,mlength)
{
	if (obj.getAttribute && obj.value.length > mlength)
		obj.value = obj.value.substring(0, mlength)		
	else
		document.getElementById(countobj).innerHTML = mlength - obj.value.length;
}


// ---------------------------------------------------------------------------

/**
 * -------------------------------------------------------------------------
 *  Header functions
 * -------------------------------------------------------------------------
 */

function getScrollTop()
{
	var scrollY = 0;
	if (document.body && typeof document.body.scrollTop != "undefined")
	{
		scrollY += document.body.scrollTop;
		if (document.body.parentNode && typeof document.body.parentNode.scrollTop != "undefined")
		{
			scrollY += document.body.parentNode.scrollTop;
		}
	}
	else if (typeof window.pageXOffset != "undefined")
	{
		scrollY += window.pageYOffset;
	}
	return scrollY;
}


function changeLanguage(ctrl, lastUrl, change)
{
	var newLangCode = ctrl.options[ctrl.selectedIndex].value;
	var strUrl = new String(lastUrl);
	if (strUrl == "") strUrl = document.location.href;



	ind1 = strUrl.indexOf("NewLanguageCode=")
	if(ind1 >= 0 )
	{
		ind2 = strUrl.indexOf("&", ind1)
		if(ind2 < 0 ) ind2 =  strUrl.length 
		strUrl = strUrl.substring(0,ind1+16) + newLangCode + strUrl.substring(ind2)	
	}else{
		if (strUrl.indexOf("?") > -1)
			strUrl = strUrl + "&";
		else
			strUrl = strUrl + "?";
		strUrl = strUrl + "NewLanguageCode=" + newLangCode;
	}
//alert(strUrl);
	if (change == true ) 
		document.location.replace(strUrl);
	else
		return strUrl;
	return true;
}


function openDiscount(sQuery) 
{
	var url = "show_yielddiscount.asp" + sQuery;
    var win = window.open(url, 'co_yield', "height=550,width=470,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	win.focus();
}



/**
 * -------------------------------------------------------------------------
 *  Footer functions
 * -------------------------------------------------------------------------
 */

function openPrintWindow()
{
   var refFunction;
   try
   {
      refFunction = openPrintWindow_Override;
   }
   catch(e)
   {
      refFunction = null; // just to be sure...
   }

   if( refFunction != null )
      refFunction();
      
	window.self.print();
}


function openEmailWindow(emailUrl, emailStr)
{
	url = "Show_pEmailURL.asp?EmailURL=" + emailUrl;
	var win = window.open(url, "SendURLEmailWindow", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=400,height=200,left=100,top=100');
	win.focus();
}


function bookmarkThis(str) 
{
	openBookmarks("BookmarkString=" + Url.encode(str));
}


function openBookmarks(bookmarkStr)
{
	var url = "Show_pBookmarks.asp";
	if (bookmarkStr != null && bookmarkStr != "")
	{
		url = url + "?" +bookmarkStr;
	}
	var win = window.open(url, "BookmarksWindow", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=50,top=50');
	win.focus();
}	


function openAgreement(HasDestIDsFlag)
{
	var hasDestIDs = 0;
	if (HasDestIDsFlag == 1) hasDestIDs = 1;
	var url = g_httpURL + "tosc/Show_pAgreement.asp?HDID=" + hasDestIDs;
	var win = window.open(url , "AGBWindow", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=450,left=0,top=0');
	win.focus();
}


function openSecurity()
{
	var url = g_httpURL + "tosc/Show_pSecurity.asp";
	var win = window.open(url, "SecurityWindow", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=500,left=0,top=0');
	win.focus();
}



/**
 * -------------------------------------------------------------------------
 *  Other Popup Functions
 * -------------------------------------------------------------------------
 */

function openHelp(helpLevel)
{
	var url = "show_help.asp?HL=" + helpLevel;
//	window.open(url, 'HelpWindow', "height=400,width=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}


function openGalerie(queryString)
{
	var url = "Show_pGalerie.asp" + queryString;
	var win = window.open(url, 'Galerie', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=450,left=50,top=50');
	win.focus();
}


function openSendEmail(queryString)
{
	var url = 'Show_pSendEmail.asp' + queryString;
	var win = window.open(url, 'SendEmail', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=530,height=270,left=50,top=50');
	win.focus();
}

function openSendEmailDetail(queryString)
{
	var url = 'Show_pSendEmailDetailed.asp' + queryString;
	var win = window.open(url, 'SendEmail', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=580,left=50,top=50');
	win.focus();
}

function openLocation(queryString)
{
	var url = 'Show_pLocation.asp' + queryString;
	var win = window.open(url, 'Location', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=500,left=0,top=0');
	win.focus();
}

function openGLocation(queryString)
{
	var url = 'Show_pGLocation.asp' + queryString;
	var win = window.open(url, 'Location', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=500,left=0,top=0');
	win.focus();
}

function openRoutePlanner(queryString)
{
	var url = "Show_pRoutePlanner.asp" + queryString;
	var win = window.open(url, 'RoutePlanner', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=600,left=0,top=0');
	win.focus();
}
function openGRoutePlanner(queryString)
{
	var url = "Show_pGRoutePlanner.asp" + queryString;
	var win = window.open(url, 'RoutePlanner', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=700,height=720,left=0,top=0');
	win.focus();
}


function openArchives(queryString)
{
	var url = "Show_pArchives.asp" + queryString;
	var win = window.open(url, 'Links', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=250,left=50,top=50');
	win.focus();
}


function openChainsGroups(queryString)
{
	var url = "Show_pChainsGroups.asp" + queryString;
	var win = window.open(url, 'ChainsGroups', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=300,left=50,top=50');
	win.focus();
}


function openAvailability(queryString)
{
	var url = "Show_pAvailability.asp" + queryString;
	var win = window.open(url, 'Availability', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=250,left=0,top=0');
	win.focus();
}


function openArrivalDeparture(queryString)
{
	var url = "Show_pArrDepConditions.asp" + queryString;
	var win = window.open(url, 'ArrivalDeparture', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=200,left=0,top=0');
	win.focus();
}


function openRoomDetails(queryString)
{
	var url = "Show_pRoomDetail.asp" + queryString;
	var win = window.open(url, "RoomDetails", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=500,left=0,top=0');
	win.focus();
}


function openCriterias(queryString)
{
	var url = "Show_pCriterias.asp" + queryString;
	var win = window.open(url, 'Criterias', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=500,height=500,left=50,top=50');
	win.focus();
}


function openCriteriasNTH(queryString)
{
	var url = "Show_pCriteriasNTH.asp" + queryString;
	var win = window.open(url, 'CriteriasNTH', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=500,height=600,left=50,top=50');
	win.focus();
}


function openDataSecurity()
{
	var url = "Show_pDataSecurity.asp";
	var win = window.open(url, "DataSecurity", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=360,left=50,top=50');
	win.focus();
}


function openEmailConfirmation(queryString)
{
	var url = "Show_pEmailConfirmation.asp" + queryString;
	var win = window.open(url, "EmailConfirmation", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=200,left=50,top=50');
	win.focus();
}


function openForgottenPassword()
{
	var url = "Show_pForgottenPassword.asp";
	var win = window.open(url, "ForgottenPassword", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=200,left=50,top=50');
	win.focus();
}


function openSSLText()
{
	var url = g_httpURL + "tosc/Show_pSSLText.asp";
	var win = window.open(url, "SSLText", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=150,left=50,top=50');
	win.focus();
}


function openRequestForm(queryString)
{
	
	url = "Show_pRequestForm.asp"+queryString; 
	//url = "Show_pRequestPasswordForm.asp" + queryString;
	var win = window.open(url, "RequestForm", 'fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=470,left=0,top=0');
	win.focus();
}


function openService(url)
{
	var win = window.open(url, "Service", 'fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=700,left=0,top=0');
	win.focus();
}


function openServiceProvider(url)
{
	var win = window.open(url, "ServiceProvider", 'fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=700,left=0,top=0');
	win.focus();
}

function openEventList(url)
{
	var win = window.open(url, "EventList", 'fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=600,height=700,left=0,top=0');
	win.focus();
}

function openBrochure(url)
{
	var win = window.open(url, "Brochure", 'fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=300,left=0,top=0');
	win.focus();
}


function openInfoText(queryString)
{
	var url = g_httpURL + "tosc/Show_pInfoText.asp" + queryString;
	var win = window.open(url, "InfoText", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=140,left=50,top=50');
	win.focus();
}


function openTownsList()
{
	var win = window.open("Show_pTownsList.asp", "Show_pTownsList", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=0,top=0');
	win.focus();
}


function openAreasList()
{
	var win = window.open("Show_pAreasList.asp", "Show_pAreasList", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=0,top=0');
	win.focus();
}


function openChainsList(params)
{	
	if(!params) params=""
	var win = window.open("Show_pChainsList.asp?"+params, "Show_pChainsList", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=0,top=0');
	win.focus();
}


function openCategoriesList()
{
	var win = window.open("Show_pCategoriesList.asp", "Show_pCategoriesList", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=0,top=0');
	win.focus();
}

function openRubricsList()
{
	var win = window.open("Show_pRubricsList.asp", "Show_pRubricsList", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=600,height=400,left=0,top=0');
	win.focus();
}

function openCvv2()
{
	var win = window.open("Show_pCvv2.asp", "show_pCvv2", 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=500,height=270,left=50,top=50');
	win.focus();
}



function openEditRequestData(url)
{										 
	document.location.replace(url);
	return true;
}


function doCustomAction()
{
	if(document.getElementById("LiteTime")){
		document.getElementById("LiteTime").style.visibility = 'visible'
	}
	if(document.getElementById("DetailTime")){
		document.getElementById("DetailTime").style.visibility = 'visible'
	}
}
function doCustomAction2()
{
	alert("Page generated in: "+ a1 + " milisec");
	if(geoItemsDuration != "undefined" && geoItemsDuration > 0 )
	{
		alert("Last GEO items  generation took: "+ geoItemsDuration + " milisec");
	}

	
}




/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}



 var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init(); 





