/**
 * Utils Class
 */
function Utils() {
}

// Opens a new popup window
Utils.NewWindow = function(mypage,myname,w,h){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars=yes,';
	settings +='resizable=no';
	win=window.open(mypage,'',settings);
};

// Redirect page
Utils.goToPage = function(page) {
	window.location = page;
};


String.prototype.ucfirst = function () {
	var str = encodeURIComponent(this);
    str = str.toLowerCase().replace(/\b([a-z])/gi,function(c){return c.toUpperCase()});
	return decodeURIComponent(str);
};


/**
 * @description: Generates an unique id
 * @param: String prefix (not required)
 * @return: String
 */
Utils.getUniqueId = function getUniqueId(prefix) {
	var ret = typeof(prefix) == 'undefined'? '': prefix + '_';
	var chars = 'abcdefgjijklmnopqrstuvwkyz0123456789';
	var len = chars.length;
	for (var i = 0; i < 15; i++) {
		ret += chars[Math.floor(Math.random()*chars.length)];
	}
	return ret;
}

/**
 * @description: Pushes elements onto the end of the array
 * @params: Array, element-string
 * @Example: array_push(['kevin','van'], 'zonneveld');
 * @return: Number
 */
Utils.array_push = function(inputArr) {
    var i=0, pr = '', argv = arguments, argc = argv.length, allDigits = /^\d$/, size = 0, highestIdx = 0, len = 0;
    if (inputArr.hasOwnProperty('length')) {
        for (i=1; i < argc; i++){
            inputArr[inputArr.length] = escape(argv[i]);
        }        return inputArr.length;
    }
    // Associative (object)
    for (pr in inputArr) {
		if (inputArr.hasOwnProperty(pr)) {
            ++len;
            if (allDigits.test(pr)) {
                size = parseInt(pr, 10);
                highestIdx = size > highestIdx ? size : highestIdx;
			}
        }
    }
    for (i=1; i < argc; i++) {
        inputArr[++highestIdx] = escape(argv[i]);
	}
    return len + i - 1;
}

/**
 * @description: Removes any whitespace from the string and returns the result
 * @params: string (optional), "replacement" will be used to replace the whitespace
 * @return: String
 */
Utils.stripWhitespace = function(str, replacement) {
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g;
	if (str.search(re) != -1) {
		result = str.replace(re, replacement);
	}
	return result;
}

/**
 * @description: Checks if the param string or array is empty
 * @params: string / array
 * @return: true or false
 */
Utils.isEmpty = function(str) {
	return ((str == null) || ($.trim(str).length == 0));
}

/**
 * @description: Checks if the param string or array is empty
 * @params: string / array
 * @return: true or false
 */
Utils.isEmail = function(str) {
	return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(str);
}

/**
 * @description: Remove all extra whitespaces and remove spaces at the beginning and ending of a string
 * @params: string 
 * @return: string
 */
Utils.trimAll = function(str){
	return str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
}

/**
 * @description: at http://kevin.vanzonneveld.net
 * @params: string 
 * @return: string
 */
Utils.rawurlencode = function (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Michael Grier
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: rawurlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin%20van%20Zonneveld%21'
    // *     example 2: rawurlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: rawurlencode('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'

    str = (str+'').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A');
};

/**
 * @description: at http://kevin.vanzonneveld.net
 * @params: string 
 * @return: string
 */
Utils.rawurldecode = function (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on
    // %        note 1: pages served as UTF-8
    // *     example 1: rawurldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin+van+Zonneveld!'
    // *     example 2: rawurldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: rawurldecode('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');
    // *     returns 3: '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'
    // *     example 4: rawurldecode('-22%97bc%2Fbc');
    // *     returns 4: '-22—bc/bc'

    return decodeURIComponent(str);
};

