/* Extensions */

jQuery.fn.center = function ()
{
    this.css("position","absolute");
    this.css("top", Math.floor((($(window).height() - this.outerHeight()) / 2)) + $(window).scrollTop() + "px");
    this.css("left", Math.floor((($(window).width() - this.outerWidth()) / 2)) + $(window).scrollLeft() + "px");
    return this;
}

jQuery.fn.log = function()
{
//	console.log(this);
}

/* Prefixed Functions */

jQuery.json_encode = function(inVal)
{
	var encode = function(inval, out)
	{
        out = out || new Array();
        var undef; // undefined

        switch (typeof inVal)
        {
                case 'object':
                        if (!inVal)
                        {
                                out.push('null');
                        } else {
                                if (inVal.constructor == Array)
                                {
                                        // Need to make a decision... if theres any associative elements of the array
                                        // then I will block the whole thing as an object {} otherwise, I'll block it
                                        // as a  normal array []
                                        var testVal = inVal.length;
                                        var compVal = 0;
                                        for (var key in inVal) compVal++;
                                        if (testVal != compVal)
                                        {
                                                // Associative
                                                out.push('{');
                                                i = 0;
                                                for (var key in inVal)
                                                {
                                                        if (i++ > 0) out.push(',\n');
                                                        out.push('"');
                                                        out.push(key);
                                                        out.push('":');
                                                        _json_encode(inVal[key], out);
                                                }
                                                out.push('}');
                                        } else {
                                                // Standard array...
                                                out.push('[');
                                                for (var i = 0; i < inVal.length; ++i)
                                                {
                                                        if (i > 0) out.push(',\n');
                                                        _json_encode(inVal[i], out);
                                                }
                                                out.push(']');
                                        }

                                } else if (typeof inVal.toString != 'undefined') {
                                        out.push('{');
                                        var first = true;
                                        for (var i in inVal)
                                        {
                                                var curr = out.length; // Record position to allow undo when arg[i] is undefined.
                                                if (!first) out.push(',\n');
                                                _json_encode(i, out);
                                                out.push(':');
                                                _json_encode(inVal[i], out);
                                                if (out[out.length - 1] == undef)
                                                {
                                                        out.splice(curr, out.length - curr);
                                                } else {
                                                        first = false;
                                                }
                                        }
                                        out.push('}');
                                }
                        }
                        return out;

                case 'unknown':
                case 'undefined':
                case 'function':
                        out.push(undef);
                        return out;

                case 'string':
                out.push('"');
                out.push(inVal.replace(/(["\\])/g, '\\$1').replace(/\r/g, '').replace(/\n/g, '\\n'));
                out.push('"');
                return out;

                default:
                        out.push(String(inVal));
                        return out;
        }
	}

	return encode(inval).join();
}

jQuery.validateCC = function(workStr)
{
	workStr = workStr.replace(/\.|\-| /g, '');

	// if the string is not 13..16 digits exactly it is not valid
	if (!workStr.match(/^[0-9]{13,16}$/)) { return false; }

	// Split the string into parts for analysis...
	var chars = new Array();
	var len = workStr.length;
	for (var i=len-1; i>=0; i--) { chars[(len - 1) - i] = workStr.charAt(i); }

	var checkVal = 0;
	for (var i=0; i<len; i++)
	{
		// If this is an odd position in the string then I simply add the value
		// at <that> position to the total... if it is an even position then I
		// multiply it * 2 and add it to the total - if the amount is > 10 then
		// I add the 2 digits together for example: 2*7 is 14, but the final
		// value is 5 because it's 1 + 4. Also, this is not zero indexed - I count
		// the first digit to be, literally the oneth, so even though it's index
		// is zero I consider it to be an odd position. Also, as you may have
		// noticed from the previous little routine this all takes place against
		// the input value backward. Jeebers!
		if ((i + 1) % 2) { checkVal += (chars[i] - 0); }
		else {
			// the answer is false, aka zero, so this is an even number
			var thisVal = chars[i] * 2;
			if (thisVal <= 9) { checkVal += (thisVal - 0); }
			if (thisVal >= 10) { checkVal += (thisVal - 9); }
		}
	}

	// So: if the resulting value I just created mod 10 is not zero, it's not a valid CC...
	if (checkVal % 10) { return false; }

	var chars1 = workStr.charAt(0);
	var chars2 = workStr.substr(0, 2);
	var chars3 = workStr.substr(0, 3);
	var chars4 = workStr.substr(0, 4);

	if ( (chars1 == '4') && ((len == 16) || (len == 13)) ) { return 'VI'; } // Visa
	if ( (len == 16) && (chars2 >= '51') && (chars2 <= '55')) { return 'MC'; } // MasterCard
	if ( (len == 15) && ((chars2 == '34') || (chars2 == '37')) ) { return 'AX'; } // AmEx
	if ( (chars4 == '6011') && (len == 16) ) { return 'DI'; } // Discover
	if ( (((chars3 >= '300') && (chars3 <= 305)) || (chars2 == '36')) && (len == 14) ) { return 'DC'; } // Diners Club
	if ( (chars2 == '38') && (len == 14) ) { return 'CB'; } // Carte Blanche
	if ( ((chars4 == '2014') || (chars4 == '2149')) && (len == 15) ) { return 'ER'; } // En Route
	if ( ((chars4 == '2131') || (chars4 == '1800')) && (len == 15) ) { return 'JC'; } // JCB
	if ( (chars1 == '3') && (len == 16) ) { return 'JC'; } // JCB Also
	return false;
}

jQuery.validateEmail = function(em)
{
	return (em.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) ? true : false;
}

