var browserCheck = new RegExp('Microsoft', 'i'); // fuck microsoft and their stupid rules

if(browserCheck.test(navigator.appName))
{
	var app = "ie";
}
else
{
	var app = "moz";
}

function setTarget(event)
{
	var e = event || window.event;

	currentTarget = e.target || e.srcElement;
}

AttachEvent(document, 'mousemove', setTarget, false);

var imageFirst = 1;
var imageLast = 4;

var imageFirstS = 1;
var imageLastS = 4;

function imageClick(i, imgSource)
{
	E('imageBig').src = imgSource;

	if(i == imageFirst && E('foto_'+parseInt(imageFirst - 1)))
	{
		imageSlide('up');
	}
	else if(i == imageLast && E('foto_'+parseInt(imageLast + 1)))
	{
		imageSlide('down');
	}
}

function imageSlide(imgDir)
{
	if(imgDir == 'down')
	{
		if( E('foto_'+parseInt(imageLast + 1)) )
		{
			E('foto_'+parseInt(imageFirst)).style.display = 'none';

			E('foto_'+parseInt(imageLast + 1)).style.display = 'block';

			imageFirst++;
			imageLast++;
		}
		else return false
	}
	else if(imgDir == 'up')
	{
		if( E('foto_'+parseInt(imageFirst - 1)) )
		{
			E('foto_'+parseInt(imageLast)).style.display = 'none';

			E('foto_'+parseInt(imageFirst - 1)).style.display = 'block';

			imageFirst--;
			imageLast--;
		}
		else return false
	}
}

function imageSlideStad(imgDir)
{
	if(imgDir == 'down')
	{
		if( E('fotoS_'+parseInt(imageLastS + 1)) )
		{
			E('fotoS_'+parseInt(imageFirstS)).style.display = 'none';

			E('fotoS_'+parseInt(imageLastS + 1)).style.display = 'block';

			imageFirstS++;
			imageLastS++;
		}
		else return false
	}
	else if(imgDir == 'up')
	{
		if( E('fotoS_'+parseInt(imageFirstS - 1)) )
		{
			E('fotoS_'+parseInt(imageLastS)).style.display = 'none';

			E('fotoS_'+parseInt(imageFirstS - 1)).style.display = 'block';

			imageFirstS--;
			imageLastS--;
		}
		else return false
	}
}

function generalAJAXStatus(debug)
{
	var debugMode = (debug == "yes") ? true : false;

    if(xmlHttp.readyState == 4)
	{
        if(xmlHttp.status == 200)
		{
			if(debugMode)
			{
				alert(xmlHttp.responseText);
			}
		}
	}
}

function changePassword(changeBox, passMain)
{
	if(changeBox.checked)
	{
		E(passMain+'_js').value = "";
		E(passMain+'_r_js').value = "";

		E(passMain+'_js').disabled = false;
		E(passMain+'_r_js').disabled = false;
	}
	else
	{
		E(passMain+'_js').value = "*****";
		E(passMain+'_r_js').value = "*****";

		E(passMain+'_js').disabled = true;
		E(passMain+'_r_js').disabled = true;
	}
}

function showPopup(url,name)
{
	var newwindow = '';
	var date = new Date();
	var timestamp = Date.parse(date);
	
	newwindow=window.open(url, name+timestamp, 'location='+url+',height=600,width=670,resize=false,scrollbars=false');
	
	if (window.focus) newwindow.focus();

	return false;
}

function changeWorkThing()
{
	workThing = E('workThing');

	if(workThing.selectedIndex == 0 || workThing.selectedIndex == 1)
	{
		E('brancheThing').disabled = false;
	}
	else
	{
		E('brancheThing').disabled = true;
	}
}

// use E instead of getElementById
function E(obj)
{
	var newObj;

	if (arguments.length > 1)
	{
		for (var i = 0, objects = []; i < arguments.length; i++)
		{
			if(document.getElementById(arguments[i]) !== null)
			{
				objects.push(E(arguments[i]));
			}
		}
		return objects;
	}

	if (typeof obj == 'string')
	{
		newObj = document.getElementById(obj);
	}
	return newObj;
}

function switchPassword()
{
	var passCheck = E('passChange');

	var passD = E('pass');
	var passR = E('passR');

	if(passCheck.checked)
	{
		passD.disabled = false;
		passR.disabled = false;

		passD.value = "";
		passR.value = "";
	}
	else	
	{
		passD.disabled = true;
		passR.disabled = true;

		passD.value = "*****";
		passR.value = "*****";
	}
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture)
{
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc)
{
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function MyFireEvent(obj,evt)
{
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
