jx={http:false,format:'text',callback:function(data){},handler:false,error:false,opt:new Object(),
getHTTPObject:function(){var http=false;if(typeof ActiveXObject !='undefined'){try{http=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{http=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){http=false;}}
}else if(XMLHttpRequest){try{http=new XMLHttpRequest();}catch(e){http=false;}}return http;},
load:function(url,callback,format,method){this.init();if(!this.http||!url)return;
//if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');
this.callback=callback;
if(!method)var method="GET";if(!format)var format="text";this.format=format.toLowerCase();
method=method.toUpperCase();var ths=this;var now="cuid="+new Date().getTime();url+=(url.indexOf("?")+1)?"&":"?";
url+=now;var parameters=null;if(method=="POST"){var parts=url.split("\?");url=parts[0];parameters=parts[1];}
this.http.open(method,url,true);if(method=="POST"){
this.http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
this.http.setRequestHeader("Content-length",parameters.length);this.http.setRequestHeader("Connection","close");
}if(this.handler){this.http.onreadystatechange=this.handler;}else{this.http.onreadystatechange=function(){
if(!ths)return;var http=ths.http;if(http.readyState==4){if(http.status==200){var result="";
if(http.responseText)result=http.responseText;if(ths.format.charAt(0)=="j"){result=result.replace(/[\n\r]/g,"");
result=eval('('+result+')');}else if(ths.format.charAt(0)=="x"){result=http.responseXML;}
if(ths.callback)ths.callback(result);}else{
if(ths.opt.loadingIndicator)document.getElementsByTagName("body")[0].removeChild(ths.opt.loadingIndicator);
if(ths.opt.loading)document.getElementById(ths.opt.loading).style.display="none";
if(ths.error)ths.error(http.status);}}}}this.http.send(parameters);},
bind : function(user_options) {var opt = {'url':'','onSuccess':false,'onError':false,'format':"text",
'method':"GET",'update':"",'loading':"",'loadingIndicator':""}
for(var key in opt) {if(user_options[key]) {opt[key] = user_options[key];}}
this.opt = opt;if(!opt.url) return;if(opt.onError) this.error = opt.onError;var div = false;
if(opt.loadingIndicator) {div = document.createElement("div");
div.setAttribute("style","position:absolute;top:0px;left:0px;");div.setAttribute("class","loading-indicator");
div.innerHTML = opt.loadingIndicator;document.getElementsByTagName("body")[0].appendChild(div);
this.opt.loadingIndicator=div;}if(opt.loading) document.getElementById(opt.loading).style.display="block";
this.load(opt.url,function(data){if(opt.onSuccess) opt.onSuccess(data);
if(opt.update) document.getElementById(opt.update).innerHTML = data;
if(div) document.getElementsByTagName("body")[0].removeChild(div);
if(opt.loading) document.getElementById(opt.loading).style.display="none";
},opt.format,opt.method);},init : function() {this.http = this.getHTTPObject();}}


// Merge Objects: @see http://www.prototypejs.org/api/object/extend
Object.extend = function(destination, source) {
	for (var property in source)
		destination[property] = source[property];
	return destination;
};

ifcRedirect = function(data) {
	// bind function calls a callback function with data from ajax call
	// i did not see a possibility to pass over redirectUrl, it is stored in the global object IFC though
	window.location.href = IFC.conf.redirectUrl;
}

var InstituteFinderConnector = function () {

	this.conf = {
		pageId:'',
		uid:'',
		form:'',
		tmpl:'',
		ll:'',
		updateId:'',
		loading:'',
		ext:'dmc_institutefinder',
		ctype:'dmc_institutefinder_pi1',
		redirectUrl:''
	}
	
	this.formPrefix = '';
	this.formObj = {};
	this.savedParams = {};
	this.params = [];
	
	this.init = function () {

		this.formPrefix = this.conf.ctype+'['+this.conf.uid+']';
		this.formObj = document.getElementById(this.conf.form);
		this.setInitParams();
	}
	
	this.setConfig = function (conf) {
		Object.extend(this.conf, conf || { });
	}

	this.handleStep = function (obj, step, context, addargs) {
	
		switch (context) {
			case 'filterState':
			case 'filterCity':
				if (obj.value != '') {
					this.resetFormFields([obj.name]);
					this.setInitParams();
					this.setFormParams();
					if (document.getElementById(this.formPrefix+'[filter][country]').value == 'LUX') {
						this.setResultStep();
					} else {
						this.setReloadContentAndResultStep();
					}
					this.getStep(step);
				}				
				break;

			case 'filterZipsearch':
				this.resetFormFields([this.formPrefix+'[filter][zipsearch]']);
				this.setInitParams();
				this.setFormParams();
				this.setReloadContentAndResultStep();
				this.getStep(step);				
				break;

			case 'filterMap':
				document.getElementById(this.formPrefix+'[filter][zipsearch]').value = addargs;
				this.resetFormFields([this.formPrefix+'[filter][zipsearch]']);
				this.setInitParams();
				this.setFormParams();
				this.setResultStep();
				this.getStep(step);
				break;
			
			case 'filterRegion':
				if (obj.value != '') {
					var redirectCountries = ['DEU', 'CHE', 'NLD', 'BEL', 'AUT', 'LUX'];
					var redirectToCountyStep = false;
	
				    for (var i=0; i < redirectCountries.length; i++) {
						if (obj.value == redirectCountries[i]) {
							redirectToCountyStep = true;
							break;
						}
				    }
	
					document.getElementById(this.formPrefix+'[filter][country]').value = obj.value;
	
					this.resetFormFields([obj.name]);
					this.setInitParams();
					this.setFormParams();
	
				    if (redirectToCountyStep == true) {
						this.conf.updateId = 'updateContainer';
						this.getStep('country');
				    } else {
				    	this.setResultStep();
						this.getStep(step);
					}
				}
				break;
				
			case 'filterBar':
				if (obj.value == '') {
					obj.value = 1;
				} else {
					obj.value = '';
				}

				this.setInitParams();
				this.setFormParams();
				this.setResultStep();
				
				this.getStep(step);
				break;

			case 'nextResults':
				document.getElementById(this.formPrefix+'[next]').value = addargs;
				this.setInitParams();
				this.setFormParams();
				this.setResultStep();
				this.getStep(step);
				break;
			
							
			default:
				this.setInitParams();
				this.setFormParams();
				this.getStep(step);
				
				break;
		}
	}

	this.setInitParams = function () {
		this.params = [];
		this.params.push({key:'eID', value:this.conf.ext});
		this.params.push({key:'id', value:this.conf.pageId});
		this.params.push({key:'uid', value:this.conf.uid});
		this.params.push({key:'ctype', value:this.conf.ctype});
		this.params.push({key:'tmpl', value:this.conf.tmpl});
		this.params.push({key:'ll', value:this.conf.ll});
	}

	this.setResultStep = function () {
		this.params.push({key:this.formPrefix+'[substep]', value:'result'});
		this.conf.updateId = 'subUpdateContainer';
	}

	this.setReloadContentAndResultStep = function () {
		this.params.push({key:this.formPrefix+'[substep]', value:'result'});
		this.params.push({key:this.formPrefix+'[reloadcontent]', value:'1'});
		this.conf.updateId = 'updateContainer';
	}

	this.resetFormFields = function (excludeFields) {

	    for (var i=0; i < this.formObj.elements.length; i++) {
			var element = this.formObj.elements[i];
	
			if (typeof excludeFields != 'undefined') {
				for (var j=0; j < excludeFields.length; j++) {
					if (element.name.indexOf(excludeFields[j]) < 0) {
						switch (element.type) {
							case 'select-one':
								element.selectedIndex = 0;
								break;
								
							case 'text':
								element.value = '';
								break;
						}
					}
			    }
			} else {
				switch (element.type) {
					case 'select-one':
						element.selectedIndex = 0;
						break;
						
					case 'text':
						element.value = '';
						break;
				}
			}
	    }
	}

	this.setFormParams = function (allowedFields) {

	    for (var i=0; i < this.formObj.elements.length; i++) {
			var element = this.formObj.elements[i];
	
			if (typeof allowedFields != 'undefined') {
			    for (var i=0; i < allowedFields.length; i++) {
					if (element.name.indexOf(allowedFields[i]) > 0) {
						this.params.push({key:element.name, value:element.value});					
					}
			    }
			} else {
				this.params.push({key:element.name, value:element.value});				
			}
	    }
	}
	
	this.getStep = function (step) {
	    var url = '/index.php?';
	
		this.params.push({key:this.formPrefix+'[step]', value:step});
		
	    for (var i=0; i < this.params.length; i++) {
	    	url += '&' + this.params[i].key +'='+ this.params[i].value;
	    }
		
		var JXConf = {
			'url':url,
			'update':this.conf.updateId,
			'method':'post',
			'onSuccess':false
		};

		if ( this.conf.redirectUrl != '' ) {
			redirectUrl		= this.conf.redirectUrl;
			JXConf.onSuccess= ifcRedirect;
			JXConf.update	= "";
		} 
		
		if (this.conf.loading != '') {
			JXConf.loading = this.conf.loading;
		} 

		// bind data to jx ajax object    
		jx.bind(JXConf);
	}
	
	this.de = function () {
		console.dir(this);
	}
}


/**
* Function to position the address in header next to logo
*
* @param	element			html code to positioning
* @param	nextToArea		target area
* @access	public
* @return	void
*
*/
function positionEshopInHeader(element, nextToArea) {

	var selectedElement;
	var targetArea;
	
	selectedElement = document.getElementById(element);
	targetArea = document.getElementById(nextToArea);
	targetArea.appendChild(selectedElement);
}
