/* --------------------------------------------------------
    Javascript Global Functions
    Filename: js/global.js
   -------------------------------------------------------- */

/* --------------------------------------------------------
    Function:     preload_image()
    Description:  Preloads image in cache
    Params:       @image_name - string
                  @image_src  - string
   -------------------------------------------------------- */

    function preload_image(image_name, image_src) {
        if (document.images) {
            eval(image_name + ' = new Image()');
            eval(image_name + '.src = "' + image_src + '"');
        }
    }

/* --------------------------------------------------------
    Function:     change_image()
    Description:  Swaps image for rollover states
    Params:       @image_name - string
                  @image_src  - string
   -------------------------------------------------------- */

    function change_image(image_name, image_src) {
        if (document.images) {
            document.images[image_name].src = image_src;
        }
    }

/* --------------------------------------------------------
    Function:     open_window()
    Description:  Opens window (popup) with a set of params
    Params:       @page_url        - string
                  @page_name       - string
                  @window_width    - numeric, no quotes
                  @window_height   - numeric, no quotes
                  @scrollbar_value - boolean, 'yes' or 'no'
                  @is_center       - boolean, 'yes' or 'no'
   -------------------------------------------------------- */

    function open_window(page_url, page_name, window_width, window_height, scrollbar_value, is_center) {
        var window_pos_x = 20;
        var window_pos_y = 20;
        if (is_center == 'yes') {
            window_pos_x = (screen.width / 2) - (window_width / 2);
            window_pos_y = (screen.height / 2) - (window_height / 2);
        }
        popup_window = this.open(page_url, page_name, "toolbar=no,status=no,menubar=no,location=no,scrollbars=" + scrollbar_value + ",resizable=no,width=" + window_width + ",height=" + window_height + ",screenX=" + window_pos_x + ",screenY=" + window_pos_y + ",left=" + window_pos_x + ",top=" + window_pos_y);
        popup_window.resizeTo(window_width + 6, window_height + 54);
        popup_window.focus();
    }

/* --------------------------------------------------------
    Function:     anti_spam_email()
    Description:  Protects email from spam bots
    Params:       @user     - string
                  @domain   - string
                  @linktext - string (not required)
   -------------------------------------------------------- */

    function anti_spam_email(user, domain, linktext) {
        var username = user;
        var hostname = domain;
        if (linktext == '') {
            linktext = username + "&#064;" + hostname;
        }
        document.write("<a href=" + "mail" + "to:" + username +"&#064;" + hostname + ">" + linktext + "</a>");
    }

/* --------------------------------------------------------
    Function:     show_hide_element()
    Description:  Shows and hides DOM element by ID
    Params:       @obj - string
   -------------------------------------------------------- */

    function show_hide_element(id){
        var my_element = document.getElementById(id);
        if (my_element.style.display == "none"){
            my_element.style.display = "block";
        } else {
            my_element.style.display = "none";
        }
    }
    
/* --------------------------------------------------------
    Function:	show_hide_dflt_val()
    Use for:	showing and hiding default input value
    Params:	
    		@action		focus or blur
    		@input		input ID in document
    		@default_val	default input value
   -------------------------------------------------------- */
 
function show_hide_dflt_val(action, input, default_val) {
	input_name = document.getElementById(input);
	if (action == 'focus') {
		if (input_name.value == default_val) {
			input_name.value = '';
		}
	}
	if (action == 'blur') {
		if (input_name.value == '') {
			input_name.value = default_val;
		}
	}
}

/* --------------------------------------------------------
    Function:	urlencode()
    Use for:	same behaviour as php's urlencode
    Params:		@str
   -------------------------------------------------------- */

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}
