/**
 * Common functions to improve JavaScripts usability
 *
 * @package 	DigiCMS v3.x
 * @file		js/common.js
 * @version		3.0.0
 * @author		Digitalization
 * @email		info@digitalization.nl
 * @link		http://www.digitalization.nl/
 * @copyright	All code copyright 2009,2010 by Digitalization
 */

// *** Check if a variable has been set
function isset(varname){
	if (typeof(varname) == 'undefined' || varname === null) {
		return false;
  	} else {
     	return true;
  	}
}

// *** Check if needle exists in array haystack
function in_array(needle, haystack) {
	for (i = 0; i < haystack.length; i++) {
		if (needle == haystack[i]) return true;
	}
	return false;
}

// *** Check if a number is an integer
function is_int(number) {
    var modulus = number % 1;
    if (modulus == 0) 	return true;
    else 				return false;
}

// *** Round function for more decimals
function round(value, digits) {
	var tmp = Math.pow(10,digits);
	return Math.round(value*tmp)/tmp
}

// *** Function to generate a popup
function popup(url, width, height, params) {
	if (width)   width  = ',width='+width;
	if (height)  height = ',height='+height;
	if (!params) params = 'location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes';
	return window.open(url,'_blank',params+width+height);
}

// *** Function to maximize a window
function max_window() {
	window.moveTo(0, 1); //one pixel lower to fix bug in firefox in mac os
	window.resizeTo(screen.availWidth, screen.availHeight);
}

// *** Function to redirect a user
function redirect(url) {
	window.location.href = url;
}

// *** PHP str_replace variant
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

// *** Parse an integer amount into a monetary format
function parse_amount(amount, sign) {

	amount = amount / 100;
	var amount_str = amount + '';

	pos = amount_str.indexOf('.');
	if (pos > -1) {
		var ending = amount_str.substring(pos+1);
		if (ending.length == 1) {
			amount_str += '0';
		}
	}
	else {
		amount_str += '.00';
	}

	amount_str = amount_str.replace('.',',');

	if (sign) 	return sign + amount_str;
	else		return amount_str;
}

// *** Print a page
function print() {
	window.print();
}
