﻿

/* This is the Global Application Namespace
===============================================*/
var YouFound = {};

YouFound.Controls = {};
YouFound.Forms = {};
YouFound.Forms.Admin = {};
YouFound.Forms.SiteAdmin = {};

var disabledFieldsUsa = ['companyABNField', 'companyABNAdminAreaField'];
var shortCutUrl = 'shortCutPrefix'

function manageFields(ctrl) {
  var country = $("#" + ctrl.id + " :selected").text();

  if (country == 'USA') {
    jQuery.each(disabledFieldsUsa, function () {
      $("#" + this).hide();
      $("#" + this + " input").val('');
    });
    $("#" + shortCutUrl).text('www.ufound.com/');
  } else {
    jQuery.each(disabledFieldsUsa, function () {
      $("#" + this).show();
    });
    $("#" + shortCutUrl).text('www.ufound.com.au/');
  }
}

function _StringFormatInline()
{
	var txt = this;
	for(var i=0;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i) + '\\}','gm');
		txt = txt.replace(exp,arguments[i]);
	}
	return txt;
}

function _StringFormatStatic()
{
	for(var i=1;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
		arguments[0] = arguments[0].replace(exp,arguments[i]);
	}
	return arguments[0];
}

if(!String.prototype.format)
{
	String.prototype.format = _StringFormatInline;
}

if(!String.format)
{
	String.format = _StringFormatStatic;
}

//===============================================================================
// * Namespace:   YouFound.Global
// * Description: Provides general startup functionality required by each page.
//===============================================================================
YouFound.Global = (function () {

    return {
        /* Performs common initialisation required for each page
        ==============================================================*/
        Initialise: function () {

            // Setup text counting on any field requiring it
            YouFound.TextCounter.Initialise();
        }
    };
})();


//===============================================================================
// * Namespace:   YouFound.TextCounter
// * Description: Implements text counter functionality on all controls marked
// *              as requiring support.
//===============================================================================
YouFound.TextCounter = (function () {

	//
	// This is the general purpose changed event handler
	// for both INPUT and TEXTAREA elements. It is assumed
	// that the TEXTAREA elements will have a manually added
	// attribute for MaxLength (this is done on init)
	var changed = function (maxLength) {

		// Get the max length and the ui display field
		var maxLength = $(this).attr('maxLength');
		var span = $('span.countTextSpan', $(this).parent());
		var length = $(this).val().length;

		// Make sure there is a field to update
		if (span === undefined)
			return;

		// Make sure the maxlength is valid
		if (maxLength === undefined || maxLength === null) {
			span.html('[maxlength not defined]');
			return;
		}

		// Update the UI SPAN element		
		$(this).parent()
			   .find("span.countTextSpan")
			   .html(length + ' of ' + maxLength + ' characters used');
	};

	function limitTextArea(e) {
		var maxLength = $(this).attr('maxLength');
		var length = $(this).val().length;

		if (!e) var e = window.event
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		var character = String.fromCharCode(code);

		if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40 && code != 46) {
			if (length >= maxLength) {
				return false;
			}
		}

		return true;
	}

	function limitTextBox(e) {
		var maxLength = $(this).attr('maxLength');
		var length = $(this).val().length;

		if (!e) var e = window.event
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;

		var character = String.fromCharCode(code);

		if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40 && code != 46) {
			if (length >= maxLength) {
				return false;
			}

			var inclusions = new RegExp("[a-zA-Z0-9$ %&*+@#!,]");

			if (inclusions.test(character) == false) {
				return false;
			}
		}

		return true;
	}

	return {

		//
		// Initialises the text counters on the page
		Initialise: function () {

			var areas = $('textarea.countText');
			var textboxes = $('input[type=text].countText');
			var spanHtml = '<span class="countTextSpan"></span>';

			// Setup all the INPUT elements on the form
			textboxes.keypress(limitTextBox);
			textboxes.keyup(changed);
			textboxes.after(spanHtml);
			textboxes.each(changed);

			// Setup all the TEXTAREA elements on the form
			// The length is given through the class definition
			// by specifying an extra class: len(x), where x
			// is the maxlength value.
			// The init code will parse this and store it in 
			// a manually added attribute.			
			areas.keypress(limitTextArea);
			areas.after(spanHtml);
			areas.each(function () {

				// Get the maxlength definition start
				var si = this.className.indexOf("len(");

				if (si == -1) {
					return;
				}

				si += 4;

				// Get the maxlength definition end
				var ei = this.className.indexOf(")");

				if (ei == -1) {
					return;
				}

				// Parse the maxlength
				var len = parseInt(this.className.substring(si, ei), 10);

				// Assign the element properties
				$(this).attr('maxLength', len)
				$(this)[0].onkeyup = changed;

				// Init the element by calling the event handler manually
				changed.call(this, len);
			});
		}
	};

})();
