﻿
/*
    Base Functions of the website
    (c)digital-ecom, 2011
    Author: UG
    Ver.: 1.0.0
    Date: 02/2011
*/


/*
    After page loaded, adjust the col height of column which shows the vertival border. We were not authorized to do that but thought it might
    be in the interest of designer having a general rule being applied setting this borders height. The rule is: 70% of viewports height (minus 
    the header part of the website).  
*/
function reduceColHeight() {
    /*
    var obj = document.getElementById('Footer');
    var y = findPos(obj)[1];
    y = y - 495;

    var yNew = y * 0.7;
    var POS = yNew.toString().indexOf('.');

    if (POS >= 0) {
        yNew = yNew.toString().substring(0, POS);        
    }
    
    obj = document.getElementById('Column_Middle_Border');
    obj.style.padding = '0px';
    obj.style.margin = '0px';
    obj.style.height = yNew + 'px';
    */
}

/*
    A general purpose fct for getting an elements position.
*/
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
        if (obj.id == 'oEdit1' || obj.id == 'oEdit2') { break; }
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

/*
    We now want to hover menu and change btn
*/
function hoverBtnChange(sIDLeft, sIDMid, sIDRight) {
    document.getElementById(sIDLeft).style.backgroundImage = 'url(\'/images/page/menu/hoverBtn_left.jpg\')';
    document.getElementById(sIDMid).style.backgroundImage = 'url(\'/images/page/menu/hoverBtn_middle.jpg\')';
    document.getElementById(sIDRight).style.backgroundImage = 'url(\'/images/page/menu/hoverBtn_right.jpg\')';
}

function unhoverBtnChange(sIDLeft, sIDMid, sIDRight) {
    if (sIDMid == 'HeadMenuItem_Active') {
        document.getElementById(sIDLeft).style.backgroundImage = 'url(\'/images/page/menu/activeBtn_left.jpg\')';
        document.getElementById(sIDMid).style.backgroundImage = 'url(\'/images/page/menu/activeBtn_middle.jpg\')';
        document.getElementById(sIDRight).style.backgroundImage = 'url(\'/images/page/menu/activeBtn_right.jpg\')';
    }
    else {
        document.getElementById(sIDLeft).style.backgroundImage = 'url(\'/images/page/menu/inactiveBtn_left.jpg\')';
        document.getElementById(sIDMid).style.backgroundImage = 'url(\'/images/page/menu/inactiveBtn_middle.jpg\')';
        document.getElementById(sIDRight).style.backgroundImage = 'url(\'/images/page/menu/inactiveBtn_right.jpg\')';
    }
}

/*
When (un)hovering text set icon
*/
function hoverIcon(sID) {
    document.getElementById(sID).style.backgroundPosition = 'left bottom';
}

function unhoverIcon(sID) {
    document.getElementById(sID).style.backgroundPosition = 'left top';
}


/* 
    Show/Hide (default) Popup
    By default, popups will be shown by  this fct using its default IDs. Certain popups might require their own fct, i.e.
    because they are the 2nd popup on page (the IDs you know). Rooms.js does this for example.
    When closing, see if we have elements like 'popupImgContainer' which might have child elements - these have to be destroyed.
    Destroy the overlay, too - otherwise we cannot assign a different onclose fct to it in case we have several popups on 1 page.
*/
function showPopUp() {    
    adjustPopup('popUpContainer');
    OverlayPopup_Get('');
    document.getElementById('popUpContainer').style.display = 'block';
}

function closePopUp() {
    if (document.getElementById('popupImgContainer')) {
        destroyArrowObjects();
    }

    document.getElementById('popUpContainer').style.display = 'none';
    var objPopUpOverlay = document.getElementById('overlay');
    if (objPopUpOverlay != null) {
        var objBody = document.getElementsByTagName('body').item(0);
        objBody.removeChild(objPopUpOverlay);
    }
}

// Validate Email Addr
function isEmail_Correct(sEmail) {

    var proto = "(mailto:)?";
    var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
    var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
    var regex = "^" + proto + "?" + usr + "\@" + domain + "$";

    var rgx = new RegExp(regex);
    return rgx.exec(sEmail) ? true : false;
}

/*
    Validate content of a field - accept integers only
*/
function ValidateNumbers(obj) {
    var isValid = false;
    var n = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var v = obj.value;
    var vNew = '';

    for (var j = 0; j < v.length; j++) {
        for (var i = 0; i < n.length; i++) {
            if (Mid(v, j, 1) == n[i]) {
                isValid = true;
                break;
            }
            else {
                isValid = false;
            }
        }
        if (isValid == true) {
            vNew = vNew + Mid(v, j, 1);
        }
    }
    obj.value = vNew;
}

/***
IN: str - the string we are LEFTing
start - our string's starting position (0 based!!)
len - how many characters from start we want to get

RETVAL: The substring from start to start+len
***/
function Mid(str, start, len)
{
    // Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";

    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
        iEnd = iLen;
    else
        iEnd = start + len;

    return String(str).substring(start, iEnd);
}

function isBrowserIE() {
    var n = window.navigator.userAgent;
    if (n.search(/MSIE/i) > -1) {
        return true;
    }
    else {
        return false;
    }
}
