﻿// JScript File
function getIFrameDocument(iframe) {
    //returns the document WITHIN the iframe
    //If ever problems with this check DocType of document as this can impact
    if (iframe.contentDocument) {
        return iframe.contentDocument;
    } else if (iframe.contentWindow) {
        return iframe.contentWindow.document;
    } else if (iframe.document) {
        return iframe.document;
    } else {
        return null;
    }
}

function getFrameContentHeight(f) {
    //If ever problems with this check DocType of document as this can impact
    //in many situations, if viewport is larger than content we can only get VIEWPORT size. Only way to reliably get content size is to shrink frame first! (could restore it at end if wanted to be nice)
    f.style.height = "22px"

    var fdoc = getIFrameDocument(f)

    //ie. scrollht=actual contents ht, offsetht=viewport ht (if smaller)

    var isfirefox = navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
    //in ff scrollht returns Iframe viewport size if > content. offsetHeight returns viewport size if SMALLER than content
    if (isfirefox) { return fdoc.documentElement.scrollHeight } //'f' will be iframe.contentDocument

    //in safari/chrome scrollHeight returns viewport height if larger
    var isWebkit = (navigator.userAgent.toLowerCase().indexOf('webkit') != -1);
    if (!isWebkit) {
        return fdoc.body.scrollHeight
    }
    else {
        return fdoc.body.offsetHeight
    }
}

function getFrameContentWidth(f) {
    //If ever problems with this check DocType of document as this can impact

    //in many situations, if viewport is larger than content we can only get VIEWPORT size. Only way to reliably get content size is to shrink frame first! (could restore it at end if wanted to be nice)
    f.style.width = "22px"
    var fdoc = getIFrameDocument(f)
    //ie. scrollht=actual contents ht, offsetht=viewport ht (if smaller)

    var isfirefox = navigator.userAgent.toLowerCase().indexOf("firefox") > -1;

    if (isfirefox) { return fdoc.documentElement.scrollWidth } //'fdoc' will be iframe.contentDocument

    //in safari/chrome scrollWidth returns viewport height if larger
    var isWebkit = (navigator.userAgent.toLowerCase().indexOf('webkit') != -1);
    if (!isWebkit) {
        return fdoc.body.scrollWidth
    }
    else {
        return fdoc.body.offsetWidth
    }
}

function warnIfInFrame() {
    if (top.location != self.location) { //even though can't read top.location CAN do equality test!
        alert('This session is running within a frame from another site. PayPal will likely fail in this situation.\n\nThis situation is likely caused by a link that you used to access this site. Please inform tech@tennisbookings.com .')
    }
}
function getURLParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS); var results = regex.exec(window.location.href);
    if (results == null) return ""; else return results[1];
}
function Left(String, Length) {
    if (String == null)
        return (false);

    return String.substr(0, Length);
}
function isNumber(Expression) {
    Expression = Expression.toLowerCase();
    RefString = "0123456789.-";

    if (Expression.length < 1)
        return (false);

    for (var i = 0; i < Expression.length; i++) {
        var ch = Expression.substr(i, 1)
        var a = RefString.indexOf(ch, 0)
        if (a == -1)
            return (false);
    }
    return (true);
}

function CheckNumeric(evt) {
    //disallow alpha keys, allow TAB etc.
    var list = 'abcdefghijklmnopqrstuvwxyz';

    var charcode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    var ch = String.fromCharCode(charcode);
    ch = ch.toLowerCase();
    var a = list.indexOf(ch);
    if (a != -1) {
        if (window.event) {//IE
            window.event.returnValue = null
        } else { //firefox
            evt.preventDefault()
        }
    }
}
function LTrim(String) {
    var i = 0;
    var j = String.length - 1;

    if (String == null)
        return (false);

    for (i = 0; i < String.length; i++) {
        if (String.substr(i, 1) != ' ' &&
       String.substr(i, 1) != '\t')
            break;
    }

    if (i <= j)
        return (String.substr(i, (j + 1) - i));
    else
        return ('');
}


function RTrim(String) {
    var i = 0;
    var j = String.length - 1;

    if (String == null)
        return (false);

    for (j = String.length - 1; j >= 0; j--) {
        if (String.substr(j, 1) != ' ' &&
         String.substr(j, 1) != '\t')
            break;
    }

    if (i <= j)
        return (String.substr(i, (j + 1) - i));
    else
        return ('');
}
function Trim(String) {
    if (String == null)
        return (false);

    return RTrim(LTrim(String));
}
function isvalidemail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
        return true;
    }
    else {
        return false;
    }
}
function isDarkColor(cHex) {
    if (cHex.substring(0, 3) == 'rgb') { cHex = rgbConvert(cHex) } //firefox returns rgb(3,4,5)
    if (cHex.charAt(0) == "#") {
        cHex = cHex.substring(1, 7)
        //bgHex=cutHex(bgHex)
        //calculate luminance http://en.wikipedia.org/wiki/Luminance_(relative) shows 0.2126, 0.7152, 0.077
        //http://www.scantips.com/lumin.html shows 0.3, 0.59, 0.11
        if (0.3 * parseInt(cHex.substring(0, 2), 16) +
            0.59 * parseInt(cHex.substring(2, 4), 16) +
            0.11 * parseInt(cHex.substring(4, 6), 16) < 128) {
            return true
        } else {
            return false
        }
        //if (HexToR(bgHex)+HexToG(bgHex)+HexToB(bgHex)<500)
    }
    return false
}
function fitPic(o) {
    //for details on window size and scrollbars see http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
    if (window.opener != null) { //only do if in popup
        //iWidth = window.innerWidth?window.innerWidth:document.body.offsetWidth; 
        //If got doctype then above can return width of document, not of window, so do this  
        //iWidth=document.documentElement.offsetWidth?document.documentElement.offsetWidth:iWidth
        //Give element 0 the same gap at bottom as at top
        //        o = document.forms[0].firstChild //this doesn't work cod find viewstate fields etc!
        //        alert(o.innerHTML)
        var oTopGap
        var oLeftGap
        oTopGap = findPosY(o)
        if (oTopGap < 5) { oTopGap = 5 }
        oLeftGap = findPosX(o)
        if (oLeftGap < 5) { oLeftGap = 5 }
        //        alert(9/0)  
        //        alert(oTopGap)  
        iWidth = o.offsetWidth
        iHeight = o.offsetHeight
        //window.resizeTo(17+iWidth+2*oLeftGap, iHeight + 2*oTopGap); //allow for extraneous vertical scrollbar in IE + Body margin etc.
        window.resizeBy((17 + iWidth + 2 * oLeftGap) - vpWidth(), (iHeight + 2 * oTopGap) - vpHeight()); //allow for extraneous vertical scrollbar in IE + Body margin etc.
    }
}
function vpWidth() { //viewport width
    return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
function vpHeight() {
    return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}


//-----------------
var mypopIsOk, popuptimerID, mypop

function mypopup(add, nname, specs) { //Don't use for .pdf - doesn't work! (because new window seems to spawn another one)
    //Center it based on passed ht and wd - these should be set by the calling routine to the MAXIMUM possible ht and wd of contents
    var sss, n, z
    sss = specs.toLowerCase() + "="
    var arr = (sss.replace(/,/g, "=")).split("=")
    n = myIndexOf(arr, "height") //IE doesn't have an 'indexOf' for arrays!
    if (n != -1) {
        z = parseInt(arr[n + 1])
        if (z < screen.height) {
            specs += ",top=" + (screen.height - z) / 2
        } else {
            specs += ",top=0"
        }
    }
    n = myIndexOf(arr, "width") //IE doesn't have an 'indexOf' for arrays!
    if (n != -1) {
        z = parseInt(arr[n + 1])
        if (z < screen.width) {
            specs += ",left=" + (screen.width - z) / 2
        } else {
            specs += ",left=0"
        }
    }

    //Cant have " " or "." in name (for IE)
    nname = nname.replace(/[ .-]/g, "_")

    //In FF, if a PREVIOUS instance of the popup window has been hidden, doing 'window.focus()' (in the new window's onLoad) will do nothing to bring it to forefront
    //unless user has configured tools/options/javascript/advanced/Raise or Lower Windows).SO, what we do here is, before opening the pop-up, look to see if a previous popup
    //OF THE SAME NAME is open, we set focus to it, _before_ opening the new one(!).
    //This dance is NOT required for IE, but we do it anyway (can't hurt?)

    try {
        if (mypop && mypop.name == nname) { mypop.focus() }
    }
    catch (Error) { }
    
    mypopIsOk = false
    mypop=window.open(add, nname, specs)
    if (popuptimerID != "" && popuptimerID != null) { //clear any existing timer first
        window.clearTimeout(popuptimerID)
    }
    popuptimerID = setTimeout("checkpopup()", 4000)
}

function mypopupnocheck(add, nname, specs)//use this where opening up some EXTERNAL page for which we can't insert a call to SetOpenerPopOK()
{
    mypopup(add, nname, specs)
    setMyPopIsOk() //Immediately fake that it's ok!
}

function setMyPopIsOk() { //called from popup to signal 'not blocked'
    mypopIsOk = true
}

function checkpopup() { //if parent has a mypopupfailedfunction function then call it.
//    if (typeof mypopupfailedfunction == 'function') {
//        mypopupfailedfunction()
//    } else {
        if (mypopIsOk != true) { //see setMyPopIsOk() which is called from popup
            alert('Please set your browser to allow a pop-up window to open (the next screen runs in a pop-up window)')
        }
  //  }
}
function setOpenerPopOk() { //this is called from the onload of the popup itself
    if (window.opener) {
        if (window.opener.setMyPopIsOk) {
            window.opener.setMyPopIsOk()
        }
    }
}
//-------------------------
function myIndexOf(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
            return i;
        }
    }
    return -1;
}

function getRenderedStyle(el, styleProp) {//Rendered style may be different from DECLARED
    //pass style as e.g. "background-color"
    if (el.currentStyle) {
        // var st = el.currentStyle[styleProp];
        styleProp = styleProp.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        st = el.currentStyle[styleProp];
    }
    else if (window.getComputedStyle)
        var st = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
    return st;
}

function disableAllControls(c) {
    //c is an ARRAY, eg "[el1]"
    var elem = document.forms[0].elements;
    var el
    for (var i = 0; i < elem.length; i++) {
        el = elem[i]
        if (myIndexOf(c, el) == -1 && Left((el.id + 'xx'), 2) != '__') {//exclude eg. __EVENTARGUMENT!
            if (el.className != 'mybtn') {
                el.disabled = true
            } else {
                el.className = 'mybtndis'
            }
        }
    }
}
function m1(el) {
    if (el.className != 'mybtndis') { el.className = 'mybtnon' }
}
function m2(el) {
    if (el.className != 'mybtndis') { el.className = 'mybtn' }
}
function sizeToFillAvailableVertSpace(obj, bottomStuffHeight, minimumSize) {
    //fits to browser WINDOW
    //This assumes that there are NO "height=100%' anywhere in document. So browser will show document in MINIMUM vertical space required.
    //bottomstuffheight is required gap between bottom of obj and bottom of window (=bottom of document)
    //find the current gap between the bottom of the doc and the bottom of the window. Then increase the size of obj by that amount!
    var nnn
    var winHeight
    var objTop
    //   if(window.innerHeight !=undefined) {winHeight= window.innerHeight;} // most browsers
    //    else{ // IE varieties
    //    var D= (document.documentElement)? document.documentElement:document.body//standard modes:quirks
    //    winHeight= D.clientHeight; 
    //    }
    winHeight = vpHeight()
    objTop = findPosY(obj)

    minimumSize = (typeof minimumSize == "undefined") ? 0 : minimumSize 
    
    var gap = winHeight - obj.offsetHeight - objTop

    nnn = 0
    nnn += obj.style.borderBottomWidth == "" ? 0 : parseInt(obj.style.borderBottomWidth)
    nnn += obj.style.borderTopWidth == "" ? 0 : parseInt(obj.style.borderTopWidth)
    nnn += obj.style.paddingBottom == "" ? 0 : parseInt(obj.style.paddingBottom)
    nnn += obj.style.paddingTop == "" ? 0 : parseInt(obj.style.paddingTop)
    nnn = obj.offsetHeight + gap - nnn - bottomStuffHeight

    if (nnn <= minimumSize) { return }

    //Important to have this IF, to avoid recursive calling!
    if (obj.style.height != (nnn > 0) ? nnn + "px" : "0px") { obj.style.height = (nnn > 0) ? nnn + "px" : "0px" }
}

function sizeToFillAvailableHorizSpace(obj, rightStuffWidth, minimumSize) {
    //This assumes that there are NO "width=100%' anywhere in document. So browser will show document in MINIMUM vertical space required.
    //rightStuffWIdth is required gap between bottom of obj and bottom of window (=bottom of document)
    //find the current gap between the bottom of the doc and the bottom of the window. Then increase the size of obj by that amount!
    var nnn
    var winWidth
    var objLeft
    //   if(window.innerWidth !=undefined) {winWidth= window.innerWidth;} // most browsers
    //    else{ // IE varieties
    //    var D= (document.documentElement)? document.documentElement:document.body//standard modes:quirks
    //    winWidth= D.clientWidth; 
    //    }
    winWidth = vpWidth()
    objLeft = findPosX(obj)

    minimumSize = (typeof minimumSize == "undefined") ? 0 : minimumSize 

    var gap = winWidth - obj.offsetWidth - objLeft

    nnn = 0
    nnn += obj.style.borderRightWidth == "" ? 0 : parseInt(obj.style.borderRightWidth)
    nnn += obj.style.borderLeftWidth == "" ? 0 : parseInt(obj.style.borderLeftWidth)
    nnn += obj.style.paddingRight == "" ? 0 : parseInt(obj.style.paddingRight)
    nnn += obj.style.paddingLeft == "" ? 0 : parseInt(obj.style.paddingLeft)
    nnn = obj.offsetWidth + gap - nnn - rightStuffWidth

    if (nnn <= minimumSize) { return }

    //Important to have this IF, to avoid recursive calling!
    if (obj.style.width != (nnn > 0) ? nnn + "px" : "0px") { obj.style.width = (nnn > 0) ? nnn + "px" : "0px" }
    //obj.document.body.onresize=function(){myResize()}
}

function sizeToFillParentVert(obj, container, bottomStuffHeight, minimumSize) {
 //container can be parent, grandparent,... of obj
    //bottomstuffheight is required gap between bottom of obj and bottom of window (=bottom of document)
    //find the current gap between the bottom of the doc and the bottom of the window. Then increase the size of obj by that amount!
    var nnn
    var parent=obj.parentNode
    var containerHeight
    minimumSize = (typeof minimumSize == "undefined") ? 0 : minimumSize

    containerHeight = container.offsetHeight   

    var gap =( findPosY(container)+container.offsetHeight) - (findPosY(obj)+obj.offsetHeight)

    nnn = 0
    nnn += obj.style.borderBottomWidth == "" ? 0 : parseInt(obj.style.borderBottomWidth)
    nnn += obj.style.borderTopWidth == "" ? 0 : parseInt(obj.style.borderTopWidth)
    nnn += obj.style.paddingBottom == "" ? 0 : parseInt(obj.style.paddingBottom)
    nnn += obj.style.paddingTop == "" ? 0 : parseInt(obj.style.paddingTop)
    nnn = obj.offsetHeight + gap - nnn - bottomStuffHeight

    nnn = Math.max(nnn, minimumSize)

    //Important to have this IF, to avoid recursive calling!
    if (obj.style.height != (nnn > 0) ? nnn + "px" : "0px") { obj.style.height = (nnn > 0) ? nnn + "px" : "0px" }
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}


function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}


