/*jslint white: true, devel: false, onevar: true, browser: true, undef: true, nomen: false, regexp: false, plusplus: true, bitwise: true, maxerr: 50, indent: 4 */
/*global stxt:false */

var gotoUrlSet = false;

function gotoURL(url) {
	if (!gotoUrlSet) {
		window.top.location = url;
		gotoUrlSet = true;
	}
	return true;
}

function urldecode (str) {
    // http://kevin.vanzonneveld.net
    // + original by: Philip Peterson
    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + input by: AJ
    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + improved 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)
    // + improved by: Lars Fischer
    // + input by: Ratheous
    // + improved by: Orlando
    // % note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // * example 1: urldecode('Kevin+van+Zonneveld%21');
    // * returns 1: 'Kevin van Zonneveld!'
    // * example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // * returns 2: 'http://kevin.vanzonneveld.net/'
    // * example 3: urldecode('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'

    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';

    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urlencode.
    hash_map["'"] = '%27';
    hash_map['('] = '%28';
    hash_map[')'] = '%29';
    hash_map['*'] = '%2A';
    hash_map['~'] = '%7E';
    hash_map['!'] = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

function PopupWindow(url, width, height) {
	this.width = width;
	this.height = height;
	this.top = screen.availHeight / 2 - height / 2; // center
	this.left = screen.availWidth / 2 - width / 2; // center
	this.name = "mypopupwin";
	this.url = url;
	this.showDelay = 2000;
	this.frequency = 2; // how many times show per renewal time period
	this.scrollbars = true;
	this.toolbar = false;
	this.statusbar = false;
	this.resizable = false;
	this.locationbar = false;
	this.menubar = false;
	this.ontop = true;

	this.Show = function () {
		var newWin, settings = "width=" + this.width + ",height=" + this.height + ",top=" + this.top + ",left=" + this.left
			+ ",scrollbars=" + (this.scrollbars ? "yes" : "no") + ',toolbar=' + (this.toolbar ? "yes" : "no")
			+ ",location=" + (this.locationbar ? "yes" : "no") + ",menubar=" + (this.menubar ? "yes" : "no")
			+ ",status=" + (this.statusbar ? "yes" : "no") + ",resizable=" + (this.resizable ? "yes" : "no");

		newWin = window.open(this.url, this.name, settings);
		if (!this.ontop) {
			newWin.focus();
		}
		if (newWin) {
			newWin.focus();
		}
	};
}

function clearDefault(el) {
	if (el.defaultValue === el.value) {
		el.value = "";
	}
}

function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && a[i] && x.oSrc; i += 1) {
		x = a[i];
		x.src = x.oSrc;
	}
}

function MM_preloadImages() { //v3.0
	var d = document, i, j;
	if (d.images) {
		if (!d.MM_p) {
			d.MM_p = [];
		}
		j = d.MM_p.length;
		for (i = 0; i < arguments.length; i += 1) {
			if (arguments[i].indexOf("#") !== 0 && typeof d.MM_p[j] != 'undefined') {
				d.MM_p[j] = new Image();
				d.MM_p[j += 1].src = arguments[i];
			}
		}
	}
}

function MM_findObj(n, d) { //v4.01
	var p, i, x;
	if (!d) {
		d = document;
	}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {
		x = d.all[n];
	}
	for (i = 0; !x && i < d.forms.length; i += 1) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i += 1) {
		x = MM_findObj(n, d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x = d.getElementById(n);
	}
	return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x;
	document.MM_sr = [];
	for (i = 0; i < (arguments.length - 2); i += 3) {
		if ((x = MM_findObj(arguments[i])) !== null) {
			document.MM_sr[j += 1] = x;
			if (!x.oSrc) {
				x.oSrc = x.src;
			}
			x.src = arguments[i + 2];
		}
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function getStxt(string) {
	return (((typeof stxt !== 'undefined') && (typeof stxt[string] !== 'undefined')) ? stxt[string] :  string);
}

