function printDoc() {
	if(window.print) window.print()
    else alert (tr.pressCltP);
}

function bookmark (url, description){
	if(window.external) window.external.AddFavorite(url,description);
	else alert (tr.pressCtlD);
}

function showFrameContainer (el, container, frame, url, width, height, xOffset, yOffset, busyUrl){
    if (document.getElementById){
        var e = document.getElementById(el);
        var c = document.getElementById(container);
        var f = document.getElementById(frame);
        if (c.style.display == "block"){
            c.style.display="none";
			return false;
        }
        if (f.src != url) f.src=url;
        var l=0; var t=0;
        aTag = e;
        do {
            aTag = aTag.offsetParent;
            l += aTag.offsetLeft;
            t += aTag.offsetTop;
        } while (aTag.offsetParent && aTag.tagName != 'body');
        var left =  e.offsetLeft + l;
		if (xOffset) left += xOffset;
        var top = e.offsetTop + t
		if (yOffset) top += yOffset;
		if (!width) width = parseInt(c.style.width);
		if (width && ( (left + width) > getBrowserWidth() )) left = getBrowserWidth() - width - 40;
		if (left < 0) left = 0;
		c.style.position = "absolute";
        c.style.left = left+'px';
        c.style.top = top+'px';
        c.style.display="block";
        return false;
    } else {
        window.open(url);
    }
}

function hideFrameContainer (container, frame, busyUrl) {
    if (document.getElementById){
        var c=document.getElementById(container);
        var f=document.getElementById(frame);
        c.style.display="none";
    }
}

function showPhoto (imgSrc, imgCont){
    if (document.getElementById){
        var c = document.getElementById(imgCont);
        if (c.style.display != "block"){
            c.innerHTML = '<img src="' + imgSrc + '">';
        }
		else {
            c.innerHTML = '<img src="' + tr.icons + '/transparent.gif">';
            c.style.display="none";
        }
    }
}

function hideEl(el){
	if (document.getElementById){
        document.getElementById(el).style.display = 'none';
    }
}

function showEl(el){
    if (document.getElementById){
        document.getElementById(el).style.display = 'block';
    }
}


function tickCheckBox(el){
	if (document.getElementById) {
		if (document.getElementById(el)){
			document.getElementById(el).checked=true;
		}
	}
	return true;
}

function openCurrencyConverter(obj, amount, currency) {
    if (document.getElementById) {
        document.getElementById("b_amount").value = amount;
        document.getElementById("b_sourceCurrency").value = currency;
        document.getElementById("b_currencyResults").style.display = 'none';
        positionObj(document.getElementById(obj), 'b_currencyConverterPopup');
        document.getElementById('b_currencyConverterPopup').style.display = 'block';
    }
}

function calculateCurrency () {
    if (document.getElementById) {
        var a = document.getElementById("b_amount").value;
        var sc = currencydb[document.getElementById("b_sourceCurrency").value];
        var sd = currencydb[document.getElementById("b_destinationCurrency").value];
        var da = Math.round(100 * a * (sd.val / sc.val)) / 100;
        document.getElementById("b_displayCurrencyResults").innerHTML = sc.symbol +" "+a+" = " + sd.symbol + " " + da;
        document.getElementById('b_currencyResults').style.display = "block";
        return false;
    }
}

function closeCurrencyConverter() {
    if (document.getElementById) {
        document.getElementById('b_displayCurrencyResults').innerHTML = "";
        document.getElementById('b_currencyResults').style.display = "none";
        document.getElementById('b_currencyConverterPopup').style.display = "none";
        // return false;
    }
}


function showDiv (el, div, alignX, alignY) {
	if (document.getElementById){
    	var i = document.getElementById(el);
		var c = document.getElementById(div);
        if (c.style.display != "block"){
			var l=0; var t=0;
            aTag = i;
            do {
                aTag = aTag.offsetParent;
                l += aTag.offsetLeft;
                t += aTag.offsetTop;
            } while (aTag.offsetParent && aTag.tagName != 'body');
	        var left =  i.offsetLeft + l;
    	    var top = i.offsetTop + t + i.offsetHeight + 2;
			if (alignX == 'left' && c.style.width){
				left = left - parseInt(c.style.width);
			}
			if (alignY == 'top' && c.style.height){
				top = top - parseInt(c.style.height) -25;
			}
		    c.style.position = "absolute";
        	c.style.left = left+'px';
	        c.style.top = top+'px';
			c.style.display = "block";
		} else {
			c.style.display="none";
		}
	}
}

function hideDiv (div) {
	if (document.getElementById){
		var c=document.getElementById(div);
		c.style.display="none";
	}
}

var hide  = true;

function positionObj (p, el){
	if (document.getElementById){
		var e = document.getElementById(el);
        var bw = getBrowserWidth();

		var l=0; var t=0;
		aTag = p;
		do {
			aTag = aTag.offsetParent;
			l += aTag.offsetLeft;
			t += aTag.offsetTop;
		} while (aTag.offsetParent && aTag.tagName != 'body');
		var left =  p.offsetLeft + l;

		// this should be replaced by something generic.
		// for some reason e.style.width is emptly, so
		// i cannot read the width of this element

		var width = 300;
		if (left + width > bw){
			left = bw-width-30;
		}

		var top = p.offsetTop + t + p.offsetHeight + 2;
        e.style.position = 'absolute'
		e.style.left = left+'px';
		e.style.top = top+'px';
	}
}

function getBrowserWidth(){
  var w = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    w = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    w = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    w = document.body.clientWidth;
  }
  return w;
}

function getBrowserHeight(){
  var h = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    h = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    h = document.body.clientHeight;
  }
  return h;
}

function showBlock(el){
	if (document.getElementById) {
		document.getElementById(el).style.display = 'block';
	}
}

function fillContentIframe(b_iframeId, b_contentId) {
    if (document.getElementById(b_iframeId).contentDocument) {
        var b_content=document.getElementById(b_contentId).innerHTML;
        var iframe=document.getElementById(b_iframeId);
        iframe.contentDocument.getElementById('b_dynamicIframe').innerHTML=b_content;
    }
    else
    if (document.getElementById(b_iframeId).Document)  {
        var b_content=document.getElementById(b_contentId).innerHTML;
        var iframe=document.getElementById(b_iframeId);
        iframe.Document.getElementById('b_dynamicIframe').innerHTML=b_content;
        // iframe.allowTransparency="true";
    }
    else return false;
}

function popup(url,w,h,l,t)
{
    newwindow=window.open(url,'popup1','width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no');
    if (window.focus) {
        newwindow.focus()
    }
}

function toggleTxt(ulist, spanAll, spanTop) {
    var ulBlock = document.getElementById(ulist);
    var ulBlockDisplay = ulBlock.style.display;
    var txtAll = document.getElementById(spanAll);
    var txtTop = document.getElementById(spanTop);
    if (ulBlockDisplay == "none") {
        txtAll.style.display = "none";
        txtTop.style.display = "inline";
    }
    else {
        txtAll.style.display = "inline";
        txtTop.style.display = "none";
    }
}

function enterGuestname(firstGuestnameField, nameValue) {
    var v = nameValue;
    var e = document.getElementById(firstGuestnameField);
    if (e.value == "") {
        e.value = v;
    }
}







// CVC/Issue 
function onchange_cc( node ) {
  if(node) {
    var issue_error = document.getElementById('dc_issue_error');
    var issue_label = document.getElementById('dc_issue_label');
    var issue_field = document.getElementById('dc_issue_number');
    var yyyymm_label = document.getElementById('dc_yyyymm_label');
    var yyyymm_field = document.getElementById('dc_yyyymm_field');
    var cvc_error = document.getElementById('cc_cvc_error');
    var cvc_label = document.getElementById('cc_cvc_label');
    var cvc_field = document.getElementById('cc_cvc_field');
    if(issue_error) {
      issue_error.style.display = 'none';
    }
    if(issue_label && issue_field) {
      issue_label.style.display = 'none';
      issue_field.style.display = 'none';
    }
    if(yyyymm_label && yyyymm_field) {
      yyyymm_label.style.display = 'none';
      yyyymm_field.style.display = 'none';
    }
    if(cvc_error) {
      cvc_error.style.display = 'none';
    }
    if(cvc_label && cvc_field) {
      cvc_label.style.display = 'none';
      cvc_field.style.display = 'none';
    }
    var cardName = ((node.options[node.selectedIndex].innerHTML).toLowerCase()).trim();
    switch(cardName) {
      case 'american express':
      case 'visa':
      case 'mastercard':
        if (cvc_error) {
          cvc_error.style.display = '';
        }
        if (cvc_label) {
          cvc_label.style.display = '';
          cvc_field.style.display = '';
        }
        break;
      case 'diners club':
        break;
      case 'maestro (switch)':
        if(cvc_error) {
          cvc_error.style.display = '';
        }
        if(cvc_label && cvc_field) {
          cvc_label.style.display = '';
          cvc_field.style.display = '';
        }
        if(issue_error) {
          issue_error.style.display = '';
        }
        if(issue_label && issue_field) {
          issue_label.style.display = '';
          issue_field.style.display = '';
        }
        if(yyyymm_label && yyyymm_field) {
          yyyymm_label.style.display = '';
          yyyymm_field.style.display = '';
        }
        break;
    }
  }
}

String.prototype.trim = function() {
  return this.replace(/^\s*|\s*$/g, '');
}

window.onload = function() {
  var f = document.getElementById('b_ccType');
  // Check the field only if it’s a Select (as it is in stage 2). If we don’t check we risk passing it as the hidden input it is in stage 3, causing an error.
  if (f && (f.type == 'select.one'))
	onchange_cc(f);
}










// LK: I START copying functions from bookings2/static/js/base.js here for the new Google Maps


// Basic event handling
function addListener( elm, event, handler ) {
	if(elm.addEventListener)
		elm.addEventListener(event, handler, false);
	else if(elm.attachEvent)
		elm.attachEvent('on'+event, handler);
	else
		elm['on'+event] = handler;
}

function removeListener( elm, event, handler ) {
	if(elm.removeEventListener)
		elm.removeEventListener(event, handler, false);
	else if(elm.detachEvent)
		elm.detachEvent('on'+event, handler);
}

var DOM = {
	isParentOf: function( parentElm, contextElm) {
		while(contextElm && (contextElm != parentElm))
			contextElm = contextElm.parentNode;
		return (contextElm == parentElm);
	},
	getParentOrSelf: function( contextElm, nodeName ) {
		nodeName = nodeName.toLowerCase();
		while(contextElm.nodeName.toLowerCase() != nodeName && contextElm.parentNode)
			contextElm = contextElm.parentNode;
		return contextElm;
	},
	addClass: function( elm, className ) {
		elm.className += ' '+className;
	},
	removeClass: function( elm, className) {
		var classMatch = new RegExp('\\b'+className+'\\b', 'g');
		if(classMatch.test(elm.className))
			elm.className = elm.className.replace(classMatch, ' ');
	}	
};

var gClientIsGecko = (window.controllers) ? true : false;
var gClientIsOpera = (window.opera) ? true : false;
var gClientIsIE    = (document.all && !gClientIsOpera) ? true : false;
var gClientIsIE5   = (gClientIsIE && /MSIE 5\.0/.test(navigator.appVersion)) ? true : false;
var gClientIsIE55  = (gClientIsIE && /MSIE 5\.5/.test(navigator.appVersion)) ? true : false;
var gClientIsIE6   = (gClientIsIE && /MSIE 6\.0/.test(navigator.appVersion)) ? true : false;
var gClientIsIE7   = (gClientIsIE && /MSIE 7\.0/.test(navigator.appVersion)) ? true : false;
var gClientIsMac   = (/Mac/.test(navigator.appVersion)) ? true : false;
//var gClientIsOk    = //???
// Onload function
function _init() {
	//alert(gClientIsIE5);
  fixMail();
	if(window.initDaySelect)
		initDaySelect();
}
//window.onload = _init;
addListener(window, 'load', _init);


String.prototype.deCode = function() {
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};

function fixMail() {
  var classMatch = /\bencrypted\b/;
  var mailMatch = /^mailto:(.*)$/;
  var links = document.getElementsByTagName('a');
  for(var i = 0, j = links.length; i < j; i++) {
    if(classMatch.test(links[i].className) && mailMatch.test(links[i].href)) {
      links[i].className = links[i].className.replace(classMatch, '');
      var address = mailMatch.exec(links[i].href)[1];
      var text = links[i].innerText || links[i].textContent;
      links[i].href = 'mailto:' + address.deCode();
      if(text == address)
        links[i].innerHTML = text.deCode();
    }
  }
  var spans = document.getElementsByTagName('span');
  for(var i = 0, j = spans.length; i < j; i++) {
    if(classMatch.test(spans[i].className)) {
      spans[i].className = spans[i].className.replace(classMatch, '');
      var text = spans[i].innerText || spans[i].textContent;
      spans[i].innerHTML = text.deCode();
    }
  }
}

// LK: I STOP copying from bookings2/static/js/base.js here
