    /* --------------------------------------------------------
    Global javascript functions
    Website: Dirgio.com
    Created by: Armin Busatlic
    Filename: common.js
   -------------------------------------------------------- */
    document.onmouseup = mouseUp;
    
    var intX;
    var intY;
    var blnPosition;

    /*
    *   Function is fired when mouse is moved. It calls the calculation of mouse coordinates
    *   Return: none, sets intX and intY global mouse coordinate variables
    */
    function mouseUp(e){
    	e           = e || window.event;

        intX = mousePosX(e);
        intY = mousePosY(e);
    }

    function mousePosX(e) {
        var intXPosition=0;

        if(!e)
            var e=window.event;
        if(e.pageX)
            intXPosition=e.pageX;
        else if(e.clientX&&document.body.scrollLeft)
            intXPosition=e.clientX+document.body.scrollLeft;
        else if(e.clientX&&document.documentElement.scrollLeft)
            intXPosition=e.clientX+document.documentElement.scrollLeft;
        else if(e.clientX)
            intXPosition=e.clientX;
        return intXPosition;
    }
    
    function mousePosY(e) {
        var intYPosition=0;
        
        if(!e)
            var e=window.event;
        if(e.pageY)
            intYPosition=e.pageY;
        else if(e.clientY&&document.body.scrollTop)
            intYPosition=e.clientY+document.body.scrollTop;
        else if(e.clientY&&document.documentElement.scrollTop)
            intYPosition=e.clientY+document.documentElement.scrollTop;
        else if(e.clientY)
            intYPosition=e.clientY;
        return intYPosition;
    }
    
    function getWindowWidth() {
        if (parseInt(navigator.appVersion)>3) {
            if (navigator.appName=="Netscape") {
                intWidth = window.innerWidth;
            }
            if (navigator.appName.indexOf("Microsoft")!=-1) {
                intWidth = document.body.offsetWidth;
            }
        }
        return intWidth;
    }
    
    function getWindowHeight() {
        if (parseInt(navigator.appVersion)>3) {
            if (navigator.appName=="Netscape") {
                intHeight = window.innerHeight;
            }
            if (navigator.appName.indexOf("Microsoft")!=-1) {
                intHeight = document.body.offsetHeight;
            }
        }
        return intHeight;
    }
    
    /************* Start of Shadow POPUP Window **************/

    /*
    *   Desc: Function pops up a custom js / css window that is used for alerts, confirmations,
    *   editing and general information to be displayed. It adjusts the popup so that it fits on the screen
    *   no matter what.
    *   Return: none
    */
    function showPopup( intWidth, intHeight, blnPosition ) {

        if( !blnPosition ) {
            intWindowWidth    = getWindowWidth();
            intWindowHeight   = getWindowHeight();

            intX = intWindowWidth / 2 - intWidth / 2;
            intY = intWindowHeight / 2 - ( intHeight / 2 - 10);
        }

        objPopup = document.getElementById('popwin');
        intPageWidth = document.body.clientWidth;
        intBoxWidth = ( intWidth == 0 ) ? 250 : intWidth;
    
        if( intPageWidth - intX < intBoxWidth )
            objPopup.style.left = ( intPageWidth - intBoxWidth - 40 ) + 'px';
        else
            objPopup.style.left = ( intX ) + 'px';
    
        if( intWidth != 0)
            objPopup.style.width = intWidth + 'px';

        objPopup.style.top = ( intY ) + 'px';

        objPopup.innerHTML = '<div id="popwinContent"></div>';
        objPopup.style.display='block';
    }
    
    /*
    *   Desc: Function pops up a custom js / css window that is used for alerts, confirmations, editing and general information to be displayed.
    *   Return: none
    */
    function hidePopup() {
        objPopup.style.display = 'none';
    }

    /************* End of Shadow POPUP Window **************/
    
    
    

    /************* Start of Info POPUP Window **************/
    
    /*
    *   Desc: Function pops up a custom js / css window that is used for alerts, confirmations,
    *   editing and general information to be displayed. It adjusts the popup so that it fits on the screen
    *   no matter what.
    *   Return: none
    */
    function showInfo( objInfo, strElementId, intHeight ) {
        intTop = objInfo.offsetTop;
        intLeft = objInfo.offsetLeft;

        objInfoDiv = document.getElementById(strElementId);
        objInfoDiv.style.display = 'block';
        objInfoDiv.style.left = (intLeft-75) + 'px';
        objInfoDiv.style.height = intHeight + 'px';
    }
    
    function hideInfo( strElementId ) {
        objInfoDiv = document.getElementById(strElementId);
        objInfoDiv.style.display = 'none';
    }
    
    /************* End of Info POPUP Window **************/



    /*
    *   Desc: Function checks all required fields are filled out on registration and makes sure agreement is checked
    */
    function register() {
        objRegistration = document.frmRegistration;

        if( objRegistration.strAgreement.checked ){
            objRegistration.submit();
        } else {
            alert('You must accept our Privacy Policy to register');
        }
    }

    /*
    *   Desc: Function searches the tag variation on the site and updates tag count
    *   Return: none
    */
    function searchTag() {
        strTag = document.getElementById('search').value;
        AjaxRequest( 'click.php?strTag='+strTag, null, null, null, true );

        location.href = 'search.php?strTag='+strTag;
    }

    /*
    *   Desc: Function updates the count of an estate in the background
    *   Return: none
    */
    function updateEstateCount( intEstateId ) {
        AjaxRequest( 'click.php?intEstateId='+intEstateId, null, null, null, true );
    }
    
    /*
    *   Desc: Function updates the count of a tag variation in the background
    *   Return: none
    */
    function updateTagCount( strTag ) {
        AjaxRequest( 'click.php?strTag='+strTag, null, null, null, true );
    }

    /*
    *   Desc: Function opens the window to display the stats of an estate / tag
    *   Return: none
    */
    function viewStats( intEstateId, intTagId, strPage ) {
        showPopup( 610, 600, false );
        AjaxRequest( strPage+'.php?intEstateId='+intEstateId+'&intTagId='+intTagId, 'popwinContent' );
    }

    /*
    *   Desc: Function clears the update log record
    *   Return: none
    */
    function clearUpdateLog( intLogId ) {
        showPopup( 230, 70, true );
        var objContentDiv = document.getElementById('popwinContent');

        objContentDiv.innerHTML =
            '<div class="aC">Are you sure you want to remove log #'+intLogId+' from your update log ?<hr /></div>'
        +   '<div class="clL m4tb aC">'
        +   '<input type="button" class="fBo ccSG" value="Yes" onclick="javascript: loadURL( \'myaccount_updatelog_process.php?strAction=delete&intLogId='+intLogId+'\' );" /> '
        +   '<input type="button" class="fBo ccSR" value="Cancel" onclick="javascript: hidePopup();" />'
        +   '</div>';
    }

    /*
    *   Desc: Function clears the transfer log record
    *   Return: none
    */
    function clearTransferLog( strType, intLogId ) {
        showPopup( 230, 70, true );
        var objContentDiv = document.getElementById('popwinContent');

        objContentDiv.innerHTML =
            '<div class="aC">Are you sure you want to remove log #'+intLogId+' from your transfer log ?<hr /></div>'
        +   '<div class="clL m4tb aC">'
        +   '<input type="button" class="fBo ccSG" value="Yes" onclick="javascript: loadURL( \'myaccount_transferlog_process.php?strAction=delete&strType='+strType+'&intLogId='+intLogId+'\' );" /> '
        +   '<input type="button" class="fBo ccSR" value="Cancel" onclick="javascript: hidePopup();" />'
        +   '</div>';
    }
    

    function removeFromWatchlist( intEstateId ) {
        showPopup( 230, 70, true );
        var objContentDiv = document.getElementById('popwinContent');

        objContentDiv.innerHTML =
            '<div class="aC">Are you sure you want to remove Estate #'+intEstateId+' from your watch list ?<hr /></div>'
        +   '<div class="clL m4tb aC">'
        +   '<input type="button" class="fBo ccSG" value="Yes" onclick="javascript: loadURL( \'myaccount_watchlist_process.php?strAction=delete&intEstateId='+intEstateId+'\' );" /> '
        +   '<input type="button" class="fBo ccSR" value="Cancel" onclick="javascript: hidePopup();" />'
        +   '</div>';
    }
    
    function unlockEstate( intEstateId ) {
        showPopup( 230, 70, true );
        var objContentDiv = document.getElementById('popwinContent');

        objContentDiv.innerHTML =
            '<div class="aC">Are you sure you want to cancel the contract of this Estate ?\n You will lose any points you spent into the contract.<hr /></div>'
        +   '<div class="clL m4tb aC">'
        +   '<input type="button" class="fBo ccSG" value="Yes" onclick="javascript: loadURL( \'estate_process.php?strAction=Unlock&intEstateId='+intEstateId+'\' );" /> '
        +   '<input type="button" class="fBo ccSR" value="Cancel" onclick="javascript: hidePopup();" />'
        +   '</div>';
    }

    function analyzeRaiseLock( intEstateId, dblValue, intCurrentLockDays, intRaiseMultiplier, intLockDivider ) {

        dblRaiseValue = parseFloat(document.getElementById('dblRaiseValue').value);
        intLockDays = parseInt(document.getElementById('intLockDays').value);
        objAnalysis = document.getElementById('analysis');

        dblNewValue = dblValue + dblRaiseValue;
        intTotalLockDays = intCurrentLockDays + intLockDays;

        dblRaiseCost = Math.round( (dblRaiseValue * intRaiseMultiplier) * 100) / 100;

        if( dblRaiseValue == 0 && intLockDays == 0 ) {
            dblLockCostPerDay = 0;
            dblLockCost = 0;
            dblTotalCost = 0;
        } else {
            if( intTotalLockDays ) dblLockCostPerDay = Math.round( ( dblNewValue / intLockDivider )* 100 ) / 100;
            else dblLockCostPerDay = 0;
            dblLockCost = Math.round( ( ( dblNewValue / intLockDivider ) * intTotalLockDays ) * 100 ) / 100;
            dblTotalCost = Math.round( ( dblRaiseCost + dblLockCost ) * 100 ) / 100;
        }

        objAnalysis.innerHTML =
            '<table class="hS">'
        +   '<tr><td>Raise Cost:</td><td><b>'+dblRaiseCost+'</b> dp</td></tr>'
        +   '<tr><td class="wM">Value after Raise:</td><td><b>'+dblNewValue+'</b> dp</td></tr>'
        +   '<tr><td>Contract Extension:</td><td>'+intLockDays+' days</td></tr>'
        +   '<tr><td>Contract Cost / Day:</td><td><b>'+dblLockCostPerDay+'</b> dp / day</td></tr>'
        +   '<tr><td>Total Contract Cost:</td><td><b>'+dblLockCost+'</b> dp ('+ intTotalLockDays +' days)</td></tr>'
        +   '<tr><td colspan="2"><hr /></td></tr>'
        +   '<tr class="fs3"><td>Total Cost:</td><td>'+dblTotalCost+' dp</td></tr>'
        +   '<tr><td><br /></td></tr>'
        +   '</table>';
    }

    /*
    *   Desc: Opens the raise / extend window with current estate value and current lock days
    *   Return: none
    */
    function raiseLockEstate( intEstateId, dblValue, intLockDays ) {
        showPopup( 400, 400, false );
        AjaxRequest( 'raiselock.php?intEstateId='+intEstateId+'&dblValue='+dblValue+'&intLockDays='+intLockDays, 'popwinContent' );
    }

    /*
    *   Desc: Proceeds the raise / lock
    *   Return: none
    */
    function raiseLockConfirm( intEstateId ) {
        dblRaiseValue = parseFloat(document.getElementById('dblRaiseValue').value);
        intLockDays = parseInt(document.getElementById('intLockDays').value);

        loadURL( 'estate_process.php?strAction=RaiseLock&intEstateId='+intEstateId+'&dblRaiseValue='+dblRaiseValue+'&intLockDays='+intLockDays );
    }
    
    /*
    *   Desc: Function redirects to the page to purchase estate on
    *   Return: none
    */
    function buyEstate( intEstateId ) {
        location.href = "buy.php?intEstateId="+intEstateId;
    }
    
    /*
    *   Desc: Function redirects to the page to process an estate purchase
    *   Return: none
    */
    function buyEstateProcess( intEstateId, dblPrice ) {
        
        showPopup( 230, 70, true );
        var objContentDiv = document.getElementById('popwinContent');

        objContentDiv.innerHTML =
            '<div class="aC">Are you sure you want to purchase Estate #'+intEstateId+' for <b>'+dblPrice+'</b> dp ?<hr /></div>'
        +   '<div class="clL m4tb aC">'
        +   '<input type="button" class="fBo ccSG" value="Yes" onclick="javascript: loadURL( \'buy_process.php?intEstateId='+intEstateId+'\' );" /> '
        +   '<input type="button" class="fBo ccSR" value="Cancel" onclick="javascript: hidePopup();" />'
        +   '</div>';
    }

    /*
    *   Desc: Function checks for the agreement checkbox and verifies if all fields are filled out, then submits the form
    *   Return: none
    */
    function saveEstate( strSaveValue ) {
        objForm = document.frmEstate;
        if( objForm.strTitle.value != '' && objForm.strLink.value != '' && objForm.intCategoryId.selectedIndex != 0 && objForm.strDescription.value) {
            blnSubmit = false;

            if( strSaveValue == "Save" || objForm.strAgreement.checked )
                objForm.submit();
            else alert('You must acknowledge and accept our terms in the Agreement to add an estate!');
        }
        else alert('Please fill out all the fields!');
    }


    function typeDescription( objField, objCountField, intMaxChars ) {
        
        if (objField.value.length > intMaxChars)
            objField.value = objField.value.substring(0, intMaxChars);

        else objCountField.value = intMaxChars - objField.value.length;
    }
    
    /*
    *   Desc: Function highlights all of the text in the search results
    *   Return: none
    */
    function highlightResultText( strTag ) {

        if( strTag != '' ) {

            var arrTagWords = strTag.split(' ');
            var intTagWordCount = arrTagWords.length;
            var strTagWords = '';
            var strTagSeperator = '';

            for(i=0;i<intTagWordCount;i++) {
                strTagWords += strTagSeperator+''+arrTagWords[i];
                strTagSeperator = '|';
            }

            highlightElementsByClass( strTagWords, 'resultTitles', 'a');
            highlightElementsByClass( strTagWords, 'resultDescriptions', 'span');
        }
    }
    
    /*
    *   Desc: Function highlights all keyterms of titles, descriptions and URLs
    *   Return: none
    */
    function highlightElementsByClass( strTagWords, strClassName, strTagName ) {

        var arrElements = document.getElementsByTagName( strTagName );
        var intElementsLength = arrElements.length;
        var strPattern = new RegExp('\\b'+strClassName+'\\b');

        for ( var i = 0; i < intElementsLength; i++ ) {
            if ( strPattern.test( arrElements[i].className ) ) {
                var strWordPattern = new RegExp('\\b('+strTagWords+')\\b',['gi']);
                arrElements[i].innerHTML = arrElements[i].innerHTML.replace( strWordPattern, '<b>$1</b>' );
            }
        }
    }
    
    /*
    *   Desc: Function toggles password change / no change
    *   Return: none
    */
    function togglePassword( blnToggle ) {

        if( blnToggle ) {
            for( i=1;i<=3;i++ )
                document.getElementById('password_'+i).className = '';
        } else {
            for( i=1;i<=3;i++ )
                document.getElementById('password_'+i).className = 'vNo';
        }
    }
    
    /*
    *   Desc: Function analyzes current tag / keyword and informs the user on details
    *   Return: none
    */
    function analyze( objInput ) {
        
        strMessage = '';
        strKeyword = objInput.value.toLowerCase();
        strSingularKeyword = strKeyword;

        arrWords = strKeyword.split(' ');
        intWordCount = arrWords.length;

        objAnalysisDiv = document.getElementById('analysis');

        if( intWordCount == 1 ) {
            strSingularKeyword = convertToSingular( strKeyword );

            strMessage = '<img src=\'images/good.png\' alt=\'broad\' /> - Broad Term! Displayed <u>ONLY</u> when singular or plural term of \'<i>'+strSingularKeyword+'\'</i> is searched.';
        } else if( intWordCount == 2 ) {
            strFirstWord = convertToSingular( arrWords[0] );
            strSecondWord = convertToSingular( arrWords[1] );

            strMessage = '<img src=\'images/good.png\' alt=\'specific\' /> - Specific Term! Displayed when any combination of \'<i>'+strFirstWord+'\'</i> <u>AND</u> \'<i>'+strSecondWord+'\'</i> is searched.';
        } else if( intWordCount == 3 ) {
            strFirstWord = convertToSingular( arrWords[0] );
            strSecondWord = convertToSingular( arrWords[1] );
            strThirdWord = convertToSingular( arrWords[2] );

            strMessage = '<img src=\'images/warning.png\'  alt=\'very specific\' /> - Very Specific Term! Displayed when any combination of \'<i>'+strFirstWord+'\'</i> <u>AND</u> \'<i>'+strSecondWord+'\' <u>AND</u> \'<i>'+strThirdWord+'\'</i> is searched.';
        } else {

            strMessage = '<img src=\'images/bad.png\' alt=\'too specific!\' /> - Too Specific! Too many keywords in the phrase!';
        }

        objAnalysisDiv.innerHTML = strMessage;
    }
    
    /*
    *   Desc: Converts a word to singular
    *   Return: none
    */
    function convertToSingular( strConvertKeyword ) {
        strVowels = 'aeiou';
        strReturnKeyword = strConvertKeyword;

        if( strConvertKeyword.charAt( strConvertKeyword.length - 1 ) == 's' && strVowels.indexOf( strConvertKeyword[ strConvertKeyword.length - 2 ] ) == -1 ) {
            strReturnKeyword = strConvertKeyword.substring( 0, strConvertKeyword.length - 1 );
        }
        return strReturnKeyword;
    }
    
    /*
    *   Desc: Function fills all the specified selection fields with specified values on the page
    *   Return: none
    */
    function fillSelections( strForm, objSelects ) {

        var objForm = document.getElementById( strForm );
    
        for (var i in objSelects) {
            objForm.elements[i].value = objSelects[i];
        }
    }
    
    /*
    *   Desc: Function opens a new javascript window
    *   Return: none
    */
    function openWindow( strURL, intWidth, intHeight, intTop, intLeft ) {
        if( !intTop ) intTop = 0;
        if( !intLeft ) intLeft = 0;

        window.open( strURL, '', 'scrollbars=no,menubar=no,height='+intHeight+',width='+intWidth+',top='+intTop+',left='+intLeft+',resizable=no,toolbar=no,location=no,status=no');
    }

    /*
    *   Desc: Function redirects to the page to strURL
    *   Return: none
    */
    function loadURL( strURL ) {
        location.href = strURL;
    }