function showLoading(type){
	var elm = null;
	if ( type=='all' ) {
		elm = $('loading_all');
		var height = 0;
		if (window.innerHeight && window.scrollMaxY) {
			height = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			height = document.body.scrollHeight;
		} else {
			height = document.body.offsetHeight;
		}
		elm.style.height = height+'px';
	} else {
		elm = $('loading');
	}
	elm.style.display='block';
}
function hideLoading(type){
	var elm = null;
	if ( type=='all' ) {
		elm = $('loading_all');
	} else {
		elm = $('loading');
	}
	elm.style.display='none';
}

function refreshCode(){
	var seccode = document.getElementById('securitycode');
	seccode.src=absPath+'code.php?refresh=true&r='+Math.random();
}

function setPartyVisit(par_id, pu_type){
	var req = new Request.HTML({url:'/ajax/setPartyVisit.html', 
		onSuccess: function(html) {
			$('party_agendabutton').set('text', '');
			$('party_agendabutton').adopt(html);
		},
		onFailure: function() {

		}
	}).post({'par_id':par_id, 'pu_type':pu_type});
}

function setComments(){
	
	ajaxBusy = 0;
}

function getComments(setid, page, type){
	if (type) {type=type;}else{type='party';}
	var req = new Request.HTML({url:'/ajax/getComments.htmla', 
		onSuccess: function(html) {
			$('comments_pages_container').set('text', '');
			$('comments_pages_container').adopt(html);
		},
		onFailure: function() {

		}
	}).post({'setid':setid, 'page':page, 'type':type});
}

function getQuote(comment_id){

	var req = new Request({url:'/actions/comment/quote/'+comment_id+'.html', 
		onSuccess: function(txt) {
			$('bericht').value = $('bericht').value+txt;
		},
		onFailure: function() {

		}
	}).send();
}

function loadSelectbox(selid, selval, seltype){
	var ajaxURL = '';

	switch ( seltype ) {
		case 'region':
			while($('select_region').hasChildNodes()){$('select_region').removeChild($('select_region').firstChild)}
			while($('select_city').hasChildNodes()){$('select_city').removeChild($('select_city').firstChild)}
			while($('select_location').hasChildNodes()){$('select_location').removeChild($('select_location').firstChild)}
			$('select_region').options[$('select_region').options.length] = new Option(lng.select_firstcountry, '');
			$('select_city').options[$('select_city').options.length] = new Option(lng.select_firstregion, '');
			$('select_location').options[$('select_location').options.length] = new Option(lng.select_firstcity, '');
			$('select_region').disabled = true;
			$('select_city').disabled = true;
			$('select_location').disabled = true;
			break;
		case 'city':
			while($('select_city').hasChildNodes()){$('select_city').removeChild($('select_city').firstChild)}
			while($('select_location').hasChildNodes()){$('select_location').removeChild($('select_location').firstChild)}
			$('select_city').options[$('select_city').options.length] = new Option(lng.select_firstregion, '');
			$('select_location').options[$('select_location').options.length] = new Option(lng.select_firstcity, '');
			$('select_city').disabled = true;
			$('select_location').disabled = true;
			break;
		case 'location':
			while($('select_location').hasChildNodes()){$('select_location').removeChild($('select_location').firstChild)}
			$('select_location').options[$('select_location').options.length] = new Option(lng.select_firstcity, '');
			$('select_location').disabled = true;
			break;
	}

	if ( selval != '' && selval != '-notinthislist-' && (seltype == 'region' || seltype == 'city' || seltype == 'location') ) {

		var req = new Request.JSON({url:'/ajax/getSelectbox/'+seltype+'/'+selval+'.html', 
			onSuccess: function(jsonObj) {
				$(selid).disabled = false;
				//$(selid).set('text', '');
				//$(selid).adopt(html);
				var ooptions = jsonObj.ooptions;
				while($(selid).hasChildNodes()){
					$(selid).removeChild($(selid).firstChild)
				}
				for ( var i=0;i<ooptions.length;i++ ) {
					$(selid).options[$(selid).options.length] = new Option(ooptions[i].text, ooptions[i].value);
				}
				
			},
			onFailure: function() {
				//$(selid).set('text', '');
			}
		}).send();
	
	}

	if ( selval == '-notinthislist-' ) {
		$('newlocation_country').value = '';
		$('newlocation_region').value = '';
		$('newlocation_city').value = '';
		if ( $('select_country').disabled==false && $('select_country').value != '' && $('select_country').value != '-notinthislist-' ) {
			$('newlocation_country').value=$('select_country').options[$('select_country').selectedIndex].text;
		}
		if ( $('select_region').disabled==false && $('select_region').value != '' && $('select_region').value != '-notinthislist-' ) {
			$('newlocation_region').value=$('select_region').options[$('select_region').selectedIndex].text;
		}
		if ( $('select_city').disabled==false && $('select_city').value != '' && $('select_city').value != '-notinthislist-' ) {
			$('newlocation_city').value=$('select_city').options[$('select_city').selectedIndex].text;
		}

		$('submit_newlocation_container').style.display='block';
	} else if ( $('select_city').value != '-notinthislist-' && $('select_location').value != '-notinthislist-' && $('select_region').value != '-notinthislist-' && $('select_country').value != '-notinthislist-' ) {
		$('submit_newlocation_container').style.display='none';
	}
}

function timeoutRedirect(page, time){
	setTimeout("location.href='"+absPath+page+"."+tpl_ext+"'", (time*1000));
}

function confirmDel(){
	return confirm('Are you sure you want to perform this action?');
}

function confirmLink(title){
	return confirm('Are you sure you want to delete "'+title+'"');
}

function smiley(code){
	var bericht = document.getElementById("bericht");
	/*bericht.value=bericht.value+code;*/
	insertAtCaret(bericht, code);
}

function insertAtCaret(obj, text) {
	if(document.selection) {
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}

		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	} else if(obj.selectionStart) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
		for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
	}
}

function validateForm(frmid){
	switch ( frmid ) {
		case 'form_submit_update':
			if ( $('up_text').value=='' ) {
				alert(lng.form_submit_update_error);
				return false;
			} else {
				return true;	
			}
		break;
	}
}

function msgRedirect(url, cur){
	if ( cur==0 ) {
		location.href=url;
	} else {
		$('redirect_counter').innerHTML = cur;
		setTimeout("msgRedirect('"+url+"', "+(cur-1)+");", 1000);	
	}
}

function ToggleDIV(divid){
	if(document.getElementById(divid).style.display=='block'){
		document.getElementById(divid).style.display='none';
	}else{
		document.getElementById(divid).style.display='block';
	}
}

function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}


function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

var hashVars = new Array();

function checkUrlVars(){
	var docloc = unescape(document.location);
	var hashloc = docloc.indexOf('#');
	if ( hashloc != -1 ) {
		var afterHash = docloc.substring(docloc.indexOf('#')+1, docloc.length);
		var andloc = afterHash.indexOf("&");
		var arrvars = new Array();
		if ( andloc != -1 ) {
			arrvars = afterHash.split("&");
		} else {
			arrvars[0] = afterHash;
		}
		for ( var i=0;i<arrvars.length;i++ ) {
			if ( arrvars[i].indexOf("=") != -1 ) {
				var varsplit = arrvars[i].split("=");
				hashVars[i] = new Array();
				hashVars[i]['k'] = varsplit[0];
				hashVars[i]['v'] = varsplit[1];
			}
		}
	}
	loadAjaxFromHashVars();
}


function loadAjaxFromHashVars(){

}

function buildHashUrl(){
	var return2 = '';
	if ( hashVars.length > 0 ) {
		for ( var i=0;i<hashVars.length;i++ ) {
			if ( return2 != '' ) {
				return2 = return2 + '&';
			}
			return2 = return2 + hashVars[i]['k'] + "=" + hashVars[i]['v'];
		}
	}
	if ( return2 != '' ) {
		return return2;
	} else {
		return false;	
	}
}

function getHashVar(key){
	var return2 = '';
	if ( hashVars.length > 0 ) {
		for ( var i=0;i<hashVars.length;i++ ) {
			if ( hashVars[i]['k'] == key ) {
				return2 = hashVars[i]['v'];
			}
		}
	}
	if ( return2 != '' ) {
		return return2;
	} else {
		return false;	
	}
}

function setHashVar(key, val){
	var found = false;
	if ( hashVars.length > 0 ) {
		for ( var i=0;i<hashVars.length;i++ ) {
			if ( hashVars[i]['k'] == key ) {
				hashVars[i]['v'] = val;
				found = true;
			}
			if ( i == hashVars.length-1 && found == false ) {
				hashVars[i+1] = new Array();
				hashVars[i+1]['k'] = key;
				hashVars[i+1]['v'] = val;
			}
		}
	} else {
		hashVars[0] = new Array();
		hashVars[0]['k'] = key;
		hashVars[0]['v'] = val;
	}
}

function goToHashURL(){
	document.location.href='#'+buildHashUrl();	
}

function pageLoader(){
	externalLinks();
}