
jQuery(document).ready(function() {

    /* GENERAL UTILITIES
    ------------------------------------------------------------------------------------*/
    // HIDE BOX
    $('.hide').click(function() {
        $(this).parents('.boxFrame').animate({ height: 'toggle', opacity: 'toggle' }, 700);
        return false;
    });

    // LOGIN IN-FIELD LABELS - PLACE LABEL IN INPUT FIELDS
    $("#loginBox label").inFieldLabels();
    $("#loginBox input").attr("autocomplete", "off");

    // LOGIN IN-FIELD LABELS - PLACE LABEL IN INPUT FIELDS
    $(".loginBox label").inFieldLabels();
    $(".loginBox input").attr("autocomplete", "off");

    /* SEARCH / MAIN PAGE
    ------------------------------------------------------------------------------------*/
    // SEARCH BOX DROP SIMULATION
    $('.selectBox a').click(function() {

        // HIDE ALL DROP BOX
        $('.selectBox').find('.selectDrop').removeClass('selectDropShow');

        $(this).parent().find('.selectDrop').addClass('selectDropShow');
        return false;
    });

    // HIDE ALL DROP BOX WHEN CLICK IN PAGE (OUTSIDE SELECT BOX)
    $('body').click(function() {
        $('.selectBox').find('.selectDrop').removeClass('selectDropShow');
    });


    // SELECT THE DROP OPTION
    $('.selectDrop a').click(function() {
        var dropValue = $(this).html();
        $(this).parents('.selectDrop').find('a.selected').removeClass('selected');
        $(this).addClass('selected');
        $(this).parents('.selectBox').find('.select').html(dropValue);

        // setting proper value to hidden fields
        /*if($(this).parents('.selectBox').find('.select').hasClass('serviceline')){
        $('#serviceLine').val(dropValue);
        } 
        else if ($(this).parents('.selectBox').find('.select').hasClass('country')) {
        $('#country').val(dropValue);
        }*/
        SetHiddenVal(this, dropValue);
    });

    // SCROLL PAGE TO ADVANCED SEARCH BOX WHEN CLICK ON ADVANCED BUTTON
    //    $('#advancedSearchButton a, .searchBoxSmall a').click(function() {

    //         var positionAdvancedSearchButton = $(this).offset(); // find position of the Advanced Search Button to scroll that much

    //         $('html, body').animate({ scrollTop: positionAdvancedSearchButton.top - 15 }, 1000);
    //         $('.advanced').fadeIn(300);
    //         return false;
    //     });

    $('#advancedSearchButton a, .searchBoxSmall a').click(function() {
        //  alert('test');
        // window.location.href("home.aspx");
        setCookie("isModifySearch", "true");
        //   return false;
    });




    /* SEARCH RESULTS PAGE
    ------------------------------------------------------------------------------------*/
    // HIDE TABULAR (COMPANY) DETAILS
    $('.tabularHide').click(function() {
        $(this).parents('.tabularDetails').animate({ height: 'toggle', opacity: 'toggle' }, 350);
        return false;
    });

    // TOGGLE (SHOW/HIDE) SEARCH RESULTS DETAILS
    $('.resultRow').click(function() {
        $('div[id^=detailsFor]').hide();
        var x = $(this).attr("id");
        $('#detailsFor' + x).animate({ height: 'toggle', opacity: 'toggle' }, 350);
        return false;
    });


    /* COMPANY PROFILE PAGE
    ------------------------------------------------------------------------------------*/
    $('#tabMenu ul ul a').click(function() {
        $('.boxFrame').hide(); // hide all boxes
        var value = $(this).attr("href"); // find value
        $('#' + value).animate({ height: 'toggle', opacity: 'toggle' }, 700); // load corresponding box

        $(this).parents('#tabMenu').find('ul ul a.selected').removeClass('selected');
        $(this).addClass('selected');
        setCookie("currentPage",value);
        return false;
    });

    /* EDIT CREDENTIAL PAGE
    ------------------------------------------------------------------------------------*/
    $('#tabMenu1 ul a').click(function() {
        $('#tabMenu1Content .boxFrame1').hide(); // hide all boxes
        var value = $(this).attr("href"); // find value

        $('#' + value).animate({ height: 'toggle', opacity: 'toggle' }, 700); // load corresponding box

        $(this).parents('#tabMenu1').find('ul a.selected').removeClass('selected');
        $(this).addClass('selected');
        return false;
    });

    /* ADD CREDENTIAL PAGE
    ------------------------------------------------------------------------------------*/
    $('#tabMenu2 ul a').click(function() {
        $('#tabMenu1Content .boxFrame1').hide(); // hide all boxes
        var value = $(this).attr("href"); // find value
        $('#' + value).animate({ height: 'toggle', opacity: 'toggle' }, 700); // load corresponding box

        $(this).parents('#tabMenu1').find('ul a.selected').removeClass('selected');
        $(this).addClass('selected');
        return false;
    });


    /* added by mc to implement NEXT button functionality
    ----------------------------------------------------------------------------- */
    $('#metricsBtnNext , #contactsBtnNext , #nameBtnNext , #contractBtnNext').click(function() {
        displayNextTab($(this).attr('id'), $(this).attr('value'));
        return false;
    });

    /* ADD CREDENTIAL  PAGE
    ------------------------------------------------------------------------------------*/
    /* $('#tabMenu1 ul a').click(function() {
    $('#tabMenu1Content .boxFrame1').hide(); // hide all boxes
    var value = $(this).attr("href"); // find value
    $('#' + value).animate({ height: 'toggle', opacity: 'toggle' }, 700); // load corresponding box

        $(this).parents('#tabMenu1').find('ul a.selected').removeClass('selected');
    $(this).addClass('selected');
    return false;
    });*/

    /* POPUP
    ------------------------------------------------------------------------------------*/
    $('.edit').click(function() {

        // Please modify the popup page extension accordingly, e.g. .php, .aspx
        extension = '.aspx';

        // no need to change anything below this line
        thisId = $(this).attr('id'); 		    // find ID of edit button which correspond to popup file to load

        popupTitleId = thisId.split("-"); 		// split the id to retrieve only the title part
        popupTitle = 'Edit ' + popupTitleId[1]; // prepend word "Edit" to title

        // build popup frame
        popupFrame = '<div class="overlay"></div><div class="popup boxFrame"><div class="boxContent">';
        popupFrame += '<h2><span>' + popupTitle + '</span></h2>';
        popupFrame += '<div class="buttonOption boxButton"><a href="#" onclick="window.location.reload();" class="hide"><span id="spanid">Close</span></a></div>';
        popupFrame += '<div class="box"></div></div><div class="footer"><div></div></div></div>';

        $('#pageContainer').append(popupFrame);  // append the popup to the page
        $('.popup .box').load(thisId + extension); // dynamically load the popup page in the popup
        $('.overlay, .popup').fadeIn(700); 	 // show the popup

        return false;
    });


    $('.editReference').click(function() {

        // Please modify the popup page extension accordingly, e.g. .php, .aspx
        extension = '.aspx';

        // no need to change anything below this line
        thisId = $(this).attr('id'); 		// find ID of edit button which correspond to popup file to load
        popupTitleId = thisId.split("-"); 		// split the id to retrieve only the title part
        popupTitle = 'Edit ' + popupTitleId[1]; // prepend word "Edit" to title
        popupParameter = popupTitleId[2];

        // build popup frame
        popupFrame = '<div class="overlay"></div><div class="popup boxFrame"><div class="boxContent">';
        popupFrame += '<h2><span>' + popupTitle + '</span></h2>';
        popupFrame += '<div class="buttonOption boxButton"><a href="#" class="hide" onClick="window.location.reload()"><span>Close</span></a></div>';
        popupFrame += '<div class="box"></div></div><div class="footer"><div></div></div></div>';

        $('#pageContainer').append(popupFrame);  // append the popup to the page
        $('.popup .box').load(popupTitleId[0] + '-' + popupTitleId[1] + extension + '?' + popupParameter); // dynamically load the popup page in the popup
        $('.overlay, .popup').fadeIn(700); 	 // show the popup

        return false;
    });

    $('.new').click(function() {

        // Please modify the popup page extension accordingly, e.g. .php, .aspx
        extension = '.aspx';

        // no need to change anything below this line
        thisId = $(this).attr('id'); 		// find ID of edit button which correspond to popup file to load
        popupTitleId = thisId.split("-"); 		// split the id to retrieve only the title part
        popupTitle = 'Add ' + popupTitleId[1]; // prepend word "Edit" to title
        if (popupTitleId[2] != null) {
            popupTitle = popupTitle + ' ' + popupTitleId[2];
        }

        // build popup frame
        popupFrame = '<div class="overlay"></div><div class="popup boxFrame"><div class="boxContent">';
        popupFrame += '<h2><span>' + popupTitle + '</span></h2>';
        popupFrame += '<div class="buttonOption boxButton"><a href="#" class="hide" id="testbtn" onClick="window.location.reload()"><span>Close</span></a></div>';
        popupFrame += '<div class="box"></div></div><div class="footer"><div></div></div></div>';

        $('#pageContainer').append(popupFrame);  // append the popup to the page
        $('.popup .box').load(thisId + extension); // dynamically load the popup page in the popup
        $('.overlay, .popup').fadeIn(700); 	 // show the popup

        return false;
    });

    // close popup
    $('.popup .hide').live('click', function() { // use .live('click',function()) for content after append instead of .click(function())
        $('.overlay, .popup').fadeOut(700, function() {
            $(this).remove();
        });
        return false;
    });


});

function setCookie(c_name, value) {
    var exdate = new Date();
    var exdays = 1;
    exdate.setDate(exdate.getDate() + 1);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function displayAdvanced() {
    
    var positionAdvancedSearchButton = $(this).offset(); // find position of the Advanced Search Button to scroll that much
    $('html, body').animate({ scrollTop: 572 - 15 }, 1000);
    $('.advanced').fadeIn(300);


}

function displayNextTab(current, next) {
    
    var currentTab = current.substr(0, current.indexOf('BtnNext'));
   
    $('#tabMenu1Content .boxFrame1').hide(); // hide all boxes
    $('#'+next).animate({ height: 'toggle', opacity: 'toggle' }, 700); // load corresponding box
    $('#tab'+currentTab).removeClass('selected');
    $('#tab'+next).addClass('selected');


}
function test() {
    //var elt = document.getElementById('testbtn');
    
    //alert(elt.nodeName);
}
