﻿if (typeof jQuery == 'undefined') {
    alert('Your browser has not loaded all the required libraries.\n ' +
          'Please ensure you are connected to the internet.\n ' +
          'If the problem persists contact support@careerhub.com.au.');
}

var ch = {
	dialog_defaults: {
		modal: true,
		resizable: false,
		zIndex: 1999,
		close: function() { $(this).dialog("destroy"); }
	},

	alert_defaults: {
		title: "Alert",
		resizable: true,
		buttons: {
			"Ok": function() { $(this).dialog("close"); }
		}
	},

	confirm_defaults: {
		title: "Please Confirm"
	},

	alert: function(html, options) {
		options = options ? $.extend({}, this.alert_defaults, options) : this.alert_defaults;
		return this.dialog(html, options);
	},

	confirm: function(html, onconfirm, options) {
		var onclick = function(scope, confirm) {
			var $d = $(scope);
			var r = onconfirm.call(scope, confirm, {
				close: function() { $d.dialog("close") }
			});
			if(r === undefined || r) $d.dialog("close");
		}

		return this.dialog(html, $.extend({}, this.confirm_defaults, {
			buttons: {
				"Ok": function() {
					onclick(this, true);
				},
				"Cancel": function() {
					onclick(this, false);
				}
			}
		}, options || {}));
	},

	dialog: function(html, options) {
		options = options ? $.extend({}, this.dialog_defaults, options) : this.dialog_defaults;
		var $dialog = $("<div></div>").append(html);
		$dialog.dialog(options);
		return $dialog;
	},

	confirm_delete: function(btnId, ondelete) {
		var $btn = $("#" + btnId);
		var $tmp = $btn.clone();
		$btn.after($tmp).hide();

		$tmp[0].onclick = function() {
			ch.confirm(typeof ondelete == "function" ? ondelete() : ondelete, function(r) {
				if(!r) return;
				$btn.click();
			});
		}
	},

	getUrlParam: function(name, url) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(url == null ? window.location.href : url);
		if(results == null) {
			return null;
		} else {
			return decodeURIComponent(results[1].replace(/\+/g, " "));
		}
	},

	hasJsonError: function(data) {
		if(!data) return false;

		if(typeof data == "string") {
			if(/^{"error":"(.*)"}$/.test(data)) {
				eval('data=' + data);
			} else if(data.toLowerCase().indexOf("an unexpected error occurred") > -1) {
				return true;
			}
		}

		if(data.error != null) {
			if($.fn.dialog) {
				var errorDialog = $("#errorDialog");
				if(errorDialog.length == 0) {
					errorDialog = $("<div id='errorDialog'>");
					$("body").append(errorDialog);
					errorDialog.dialog(
						{
							modal: true,
							resizable: true,
							autoOpen: false,
							width: 600,
							title: "An Unexpected Error Has Occurred",
							buttons: {
								"Ok": function() {
									$(this).dialog("close");
								}
							}
						}
					);
				}
				errorDialog.html(data.error);
				errorDialog.dialog("open");
			} else alert(data.error);
			return true;
		} else return false;
	}
}

// Extensions
if (typeof String.prototype.toTitleCase == "undefined") {
	String.prototype.toTitleCase = function () {
		var arr = []
		for (var i = 0; i < this.length; i++) {
			if (i === 0 || /\W/.test(this.charAt(i - 1))) {
				arr.push(this.charAt(i).toUpperCase());
			} else {
				arr.push(this.charAt(i));
			}
		}
		return arr.join('');
	}
}

if (typeof String.prototype.toSentanceCase == "undefined") {
	String.prototype.toSentanceCase = function () {
		if (!this.length) return "";
		return this.charAt(0).toUpperCase() + this.substring(1);
	}
}

if (typeof String.prototype.stripHTML == "undefined") {
	String.prototype.stripHTML = function(){
		var matchTag = /<(?:.|\s)*?>/g;
		return this.replace(matchTag, "");
	}
}

if (typeof String.prototype.summarise == "undefined") {
	String.prototype.summarise = function(maxChars){
		if(this.length > maxChars) {
			var spaceIndex = this.lastIndexOf(' ', maxChars - 4);
			return this.substr(0, spaceIndex) + " ...";
		} else return this;
	}
}

if (typeof String.prototype.camelToSentance == "undefined") {
	String.prototype.camelToSentance = function() {
		if(this == null || this.length == 0){
			return this;
		}
	
		var sentance = new String();
		for (var i = 1; i < this.Length; i++){
			var c = this.charAt(i);
			if(c == c.toLowerCase()){
				sentance += c;
			} else {
				sentance += ' ' + c.toLowerCase(c);
			}
		}
		return sentance;
	}
}
