/* - - - global APP js - - - */
//to determine if an ajax page has been called directly
var js_APP_GLOBAL = true;
//////////////////////////////

$(document).ready(function() {
    
    //menu1_top - if logged add count unread msg, user account link
    if ( $.cookie('c_user_login') ) {
        
        $('#menu1_top').html(        
            'Hi <b><a href="/user/profile.php?u=' + $.cookie('c_user_login') + '">' + $.cookie('c_user_login') + '</a></b>' +
            ' :: ' +
            '<a href="/messages/list.php?mtype=inbox">' + _jt("Messages")  + ' (<b>' +  $.cookie('c_unread_messages') + '</b>)</a>' +
            ' | ' +
            '<a href="/user/logout_process.php">' + _jt("Sign Out") + '</a>'                        
        );
        
        
        if ( $.cookie('c_admin') ) {
            $('#menu1_top').html(
                $('#menu1_top').html() + 
                ' | ' +
                '<a href="/admin/index.php">Admin</a>'
            );    
        }
                            
    } 
    

});

$(document).ready(function() {
    $('.corner').corner("keep"); 
    //alert(_("test2"));
    
});

    
function faq_toggle_item(id_show)
{
    if ( $('#' + id_show) ) {
        if ( $('#' + id_show).css('display') == 'block' ) {
            $('#' + id_show).hide('fast');
            $('#' + id_show).css('display', 'none');
            return;        
        }
    }
    
    $('#' + id_show).show('fast');
    $('#' + id_show).css('display', 'block');
}



function openWinUsernameCheck() {

	var f = document.form_user_register;
    
    //trim
    f['user_login'].value = TrimAll(f['user_login'].value);
    
	var user_login = f['user_login'].value;

    //check if blank	
    if (isBlank(user_login)) {
        alert(_jt("Please type a username and re-check."));
        return;
    }
    
    //check user_login
	var test_pattern = /^[A-Za-z0-9]{5,32}$/;
	if (!test_pattern.test(user_login)) {
		alert(_jt("Invalid Username. \n Please use 5 to 25 characters and use only english letters and/or numbers. \n Please re-enter."));
		return;
	}
    
    
    //now open the popup win
    
    var winWidth  = 400;
    var winHeight = 200;

    var winPosWidth = (screen.availWidth - winWidth) / 2;
    var winPosHeight = (screen.availHeight - winHeight) / 2;    

    
    window.open('./check_username.php?user_login=' + user_login, 'winCheckUsername', 'left=' + winPosWidth + ', top=' + winPosHeight + ', width=' + winWidth + ', height=' + winHeight + ', status=no, toolbar=no, menubar=no, resizable=no, location=no, scrollbars=no');
}


function toggleCoinPostMethodOther() {
 
    var f = document.form_offer_get;
    
    if (document.getElementById('pm_other').checked) {
        //document.getElementById('offer_shipment_method_other_LAYER').style.display = 'block';
        $('#offer_shipment_method_other_LAYER').show('fast');
    } else {
        //document.getElementById('offer_shipment_method_other_LAYER').style.display = 'none';
        $('#offer_shipment_method_other_LAYER').hide('fast');
    }

}


function toggleCoinPayMethodOther() {
 
    var f = document.form_coin_insert;
    
    if (document.getElementById('pm_other').checked) {
        document.getElementById('coin_pay_method_other_LAYER').style.display = 'block';
    } else {
        document.getElementById('coin_pay_method_other_LAYER').style.display = 'none';
    }

}


function toggleCoinAreas(show_layer, label_bold) {
 
    var f = document.form_coin_insert;

    //hidden layers
    var all_layers = new Array('coin_countries_LAYER', 'coin_continents_LAYER');

    for ( var i = 0; i < all_layers.length; i++) {
        document.getElementById(all_layers[i]).style.display = 'none';
    }

    if (show_layer != '') {
        document.getElementById(show_layer).style.display = 'block';
    }
    
    ////////////////////
        
    var all_labels = new Array('label_countries_all', 'label_countries_selected', 'label_continents_selected');
    
    //first hide all layers
    for ( var i = 0; i < all_labels.length; i++) {
        document.getElementById(all_labels[i]).style.fontWeight = 'normal';
    }    
    
    document.getElementById(label_bold).style.fontWeight = 'bold';

}




function cmMail(domain, user) { 
   document.location.href = "mailto:" + user + "@" + domain; 
}

function makeSelectedCategoryButton() {

    if ( document.getElementById('btn'+btnNumberSelected) ) {
        document.getElementById('btn'+btnNumberSelected).src = 'theme/images/btn_'+langDefault+'/'+btnNumberSelected+'_over.gif';
    }
}

function initialisePageOnLoad() {

    if ( document.getElementById('tbl_gallery1') ) {
        document.getElementById('tbl_gallery1').style.visibility = 'visible';
    }
    
    if ( document.getElementById('tbl_gallery2') ) {
        document.getElementById('tbl_gallery2').style.visibility = 'visible';
    }
    
    if ( document.getElementById('product_photos_present') ) {
        document.getElementById('product_photos_present').style.visibility = 'visible';
    }    
        
}


function limitChars(textid, limit, infodiv) {

    var text = $('#'+textid).val();    
    var textlength = text.length;
    
    if( textlength > limit ) {
        alert(_jt("Error: you have reached the character limit! Maximum characters allowed:") + ' ' + limit);
        $('#'+textid).val(text.substr(0,limit));
        $('#' + infodiv).html(_jt("Remaining characters: 0"));
        return false;
    } else {
        $('#' + infodiv).html(_jt("Remaining characters:") + ' <b>' + (limit - textlength) + '</b>');
        return true;
    }
    
}


function search_coins_toggle_methods(show_div, hide_div)
{

    document.getElementById(hide_div).style.display = 'none'; 
    document.getElementById(show_div).style.display = 'block';
    
    var f = document.form_search_coins;
    
    if (show_div == 'div-form-search-exact') {
        f.smode.value = 'search_exact';
    } else {
        f.smode.value = 'search_range';
    }
}



function offerMakeToCoinConfirm(coin, offer) {
    if ( confirm(_jt("Please confirm: \n\n Offer") + ' "' + offer + '" \n ' + _jt("to coin") + ' "' + coin + '"?') ) {
        return true;
    } else {
        return false;
    }
}


function myCoin_visibilityOffer(offer_id, cmdwhat)
{
    document.getElementById(offer_id).checked = true;
    $("#mark").val(cmdwhat);
    document.form_offers.submit();
}




function myCoins_markOffers(whatval)
{

    if (whatval == '') {
        return; 
 
    }

    var isAnyChecked = false;
    
    $('input[@type=checkbox]').each( 
        function() { 
           if (this.checked) {
                isAnyChecked = true;
           }
        } 
    );
    
    if ( isAnyChecked ) {
        document.form_offers.submit();
    } else {                
        alert(_jt("Please select at least one offer."));
        document.getElementById('mark').selectedIndex = 0;
        return;
    }


}

function checkboxesSelectGroup(whatval)
{
    if ( whatval == '' ) {
        return;
    }
    
    if ( whatval == 'all' ) {
        $('input[@type=checkbox]').attr('checked', 'checked');
    } else {
        $('input[@type=checkbox]').removeAttr('checked');
    }
}


function deal_accept_offer(coin_uniqid, offer_uniqid, offer_title) {

    if ( confirm(_jt("Please confirm: \n\n would you like to give your coin to this offer:") + ' \n\n ' + offer_uniqid + ' \n\n ' + offer_title) ) {
        window.location.href = 'http://' + window.location.host + '/coin/deal_new_process.php?coin_uniqid=' + coin_uniqid + '&offer_uniqid=' + offer_uniqid;
    }
}


function openCenteredWindow(url, name, width, height, params) {
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParams = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (params) { winParams += "," + params; }
   var win = window.open(url, name, winParams);
   win.window.focus();
   return win;
}
