
/* String.startsWith function */
if(!String.prototype.startsWith){
    String.prototype.startsWith = function (str) {
        return !this.indexOf(str);
    }
}

if(!String.prototype.startsWithAtIdx){
    String.prototype.startsWithAtIdx = function (str, idx, ci) {
    	if (!ci) {
    		var i = this.indexOf(str, idx);
	        return i == idx;
	    } else if ((this.length-idx) < str.length) {
	    	return false;
	    } else {
	    	var left = this.substr(idx, str.length).toLowerCase();
	    	var right = str.toLowerCase() 
	    	return left == right;
	    }
    }
}

function isIE() {
	if (navigator.userAgent.indexOf("MSIE")!=-1)
		return true;
}

function isFF() {
	if (navigator.userAgent.indexOf("Firefox")!=-1)
		return true;
}

function nl2br(str){
	return str.replace( /\n/g, '<br />\n' );
}

function trimToNull(str) {
	if (str == null) {
		return null;
	}
	var t = trim(str);
	if (t.length == 0) {
		return null;
	}
	return t;
}

function trimToEmpty(str) {
	if (str == null) {
		return '';
	}
	return trim(str);
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function replaceAll(str, searchTerm, replaceWith, ignoreCase)	{
	var regex = "/"+searchTerm+"/g";
	if(ignoreCase) regex += "i";
	return str.replace(eval(regex), replaceWith);
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

var whitespace = " \t\n\r";
function isWhitespace(s) {
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

function escapeRegex(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

function htmlEscape(text) {
	return text.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;")
}

function multiLinePre(str) {
	return str.replace( /\r?\n/g, '\n</pre><pre>' );
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function clearRadioButtons(buttonGroup){
	for (i=0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked == true) {
			buttonGroup[i].checked = false
		}
	}
} 

function getRadioGroupValue(radioObj) {
	if (!radioObj) {
		return undefined;
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined) {
		if (radioObj.checked) {
			return radioObj.value;
		} else {
			return undefined;
		}
	}
	for (var i = 0; i < radioLength; i++) {
		if (radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return undefined;
}

function setRadioGroupValue(radioObj, newValue) {
	if (!radioObj) {
		return;
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for (var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if (radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

/* Jump function for form "form". Used for selecting website language */
function jumpPath(form) { 
	var newIndex = form.fieldname.selectedIndex; 
	cururl = form.fieldname.options[ newIndex ].value; 
	window.location.assign( cururl ); 
}

/* Navigates to the supplied URL when called */
function navigateBrowser(url) {
	window.location.href = url;
}

var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
var substitutions = { // table of character substitutions
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
		};
function escapeJSON(string) {
    return escapable.test(string) ? string.replace(escapable, function (a) {
        var c = substitutions[a];
        return typeof c === 'string' ? c :
            '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
	        }) : string;
}

