/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/
var promotion;
function Promotion(){
	var xhr=new Hijax();
	var xhrPost=new Hijax();
	var openFlag=false;
	var imgsource='pics/ajax-loader.gif';
	var closepic='pics/close.gif';
	var steppic='pics/off.png';
	var stepapic='pics/on.png';
	var plusImg='pics/plus.png';
	var img;
	var firstFocusFlag=false,referenzenFlag=false;
	var steps,currentstep,pageArray;
	this.prepare=function(){
		if(document.getElementsByTagName('form').length==0) return false;
		if(!checkJS()) return false;
		var links=document.getElementsByTagName('a');
		for(var i=0; i<links.length; i++){
			if(links[i].className.indexOf('promotion') != -1 || links[i].className.indexOf('referenzen') != -1){
				if(links[i].className.indexOf('referenzen') != -1){
					referenzenFlag=true;
				}
				links[i].onclick=function(){
					return !promotion.getForm(this);
				}
			}
		}
		return true;
	}
	this.getForm=function(element){
		if(!openFlag){
			xhr.setUrl(element.href.replace(/(Xhr){0,1}\.php/,'Xhr.php'));
			xhr.setCallback(function(request){
				showForm(request);
			});
			xhr.start();
			openFlag=true;
		}
		return true;
	}
	this.close=function(){
		document.getElementsByTagName('body')[0].removeChild(document.getElementById('jobformwrapper'));
		openFlag=false;
	}
	var showForm=function(request){
		//currentstep=1;
		var formwrapperDiv=document.createElement('div');
		formwrapperDiv.id='jobformwrapper';
		var formDiv=document.createElement('div');
		formDiv.id='jobform';
		//addClass(formDiv,'jobform');
		var headDiv=document.createElement('div');
		addClass(headDiv,'head');
		formDiv.appendChild(headDiv);
		var headleft=document.createElement('div');
		addClass(headleft,'l');
		if(referenzenFlag){
			headleft.appendChild(document.createTextNode(referenzenArray['titel']));
		}else{
			headleft.appendChild(document.createTextNode(promotionArray['titel']));
		}
		headDiv.appendChild(headleft);
		var headright=document.createElement('div');
		addClass(headright,'r');

		img=document.createElement('img');
		addClass(img,'button');
		headright.appendChild(img);
		headDiv.appendChild(headright);
		img.src=closepic;
		img.onclick=function(){ promotion.close(); }
		var clearDiv=document.createElement('div');
		addClass(clearDiv,'clear');
		headDiv.appendChild(clearDiv);
		var bodyDiv=document.createElement('div');
		addClass(bodyDiv,'body');
		bodyDiv.innerHTML=request.responseText;
		formDiv.appendChild(bodyDiv);
		formwrapperDiv.appendChild(formDiv);
		document.getElementsByTagName('body')[0].appendChild(formwrapperDiv);
		var divs=document.getElementById('jobform').getElementsByTagName('div');
		prepareSaveForm();
		//document.getElementById('jobform').scrollIntoView();
		window.scrollTo(0,0);
		return true;
	}
	var prepareSaveForm = function(){
		if(!document.getElementById('promotionform')) return false;
		if(document.getElementById('promotionform').getElementsByTagName('form').length<=0) return false;
		var xhrPost=new Hijax();
		//var url='/editPageHeaderXhr/?id='+id;
		//xhrPost.setUrl(url);
		xhrPost.setContainer(document.getElementById('promotionform').getElementsByTagName('form')[0]);
		//xhrPost.setLoading(function(){
		//	return displayLoading(document.getElementById('contentEditor'));
		//});
		xhrPost.setCallback(function(request){
			//alert(request.responseText);
			purge(document.getElementById('promotionform'));
			document.getElementById('promotionform').innerHTML=request.responseText
			//var divs=request.responseXML.getElementsByTagName('div');
			prepareSaveForm();
			return true;
		});
		document.getElementById('promotionform').getElementsByTagName('form')[0].onsubmit=function(){
			if(checkForm.validate(document.getElementById('promotionform').getElementsByTagName('form')[0])){
				xhrPost.captureData();
				document.getElementById('promotionform').getElementsByTagName('form')[0].onsubmit();
				return false;
			}else{
				return false;
			}
		}
		return true;
	}
/*	var checkButtons=function(){
		if(currentstep==steps){
			
		}else{
			addClass(document.getElementById('jobsubmit'),'hidden');
			removeClass(document.getElementById('jobweiter'),'hidden');
			document.getElementById('jobsubmit').setAttribute('disabled','disabled');
		}
		addClass(document.getElementById('jobweiter'),'hidden');
		removeClass(document.getElementById('jobsubmit'),'hidden');
		document.getElementById('jobsubmit').removeAttribute('disabled');
	}*/

	this.checkDefault=function(element){
		if(!firstFocusFlag){
			firstFocusFlag=true;
			element.value='';
		}
	}
	this.addNewOptions=function(element){
		var fieldsetElement=element.parentNode;//document.getElementById(idName);
		var copyelement=fieldsetElement.lastChild.previousSibling;
		var pattern= /(.+?)(\d+)/;
		if(copyelement.nodeType==1){
			var newOptions =copyelement.cloneNode(true);
			//alert(newOptions.innerHTML);
			if(newOptions.id){
				pattern.exec(newOptions.id);
				newOptions.id=RegExp.$1+(parseInt(RegExp.$2)+1);
			}
			var elements=newOptions.getElementsByTagName('label');
			for(var i=0; i<elements.length; i++) {
				removeClass(elements[i],'required');
				if(elements[i].htmlFor!=''){
					pattern.exec(elements[i].htmlFor);
					elements[i].htmlFor=RegExp.$1+(parseInt(RegExp.$2)+1);
				}
			}
			elements=newOptions.getElementsByTagName('input');
			for(var i=0; i<elements.length; i++) {
				removeClass(elements[i],'required');
				if(elements[i].id!=''){
					pattern.exec(elements[i].id);
					elements[i].id=RegExp.$1+(parseInt(RegExp.$2)+1);
				}
				if(elements[i].value)
					elements[i].value='';
			}
			elements=newOptions.getElementsByTagName('select');
			for(var i=0; i<elements.length; i++) {
				removeClass(elements[i],'required');
				if(elements[i].id!=''){
					pattern.exec(elements[i].id);
					elements[i].id=RegExp.$1+(parseInt(RegExp.$2)+1);
				}
				if(elements[i].value)
					elements[i].value='';
			}
			insertAfter(newOptions,copyelement);
		}
	}

	this.loading=function(){
		var loadingDiv=document.createElement('div');
		var transDiv=document.createElement('div');
		var loadingImg=document.createElement('img');
		loadingDiv.style.zIndex='60';
		loadingImg.src=img.src;
		var top=0;
		if(ie6){
			var height=document.getElementById('main').offsetHeight+59;//getWindowHeight();
			loadingDiv.style.position='absolute';
			loadingDiv.style.top='0px';
			loadingDiv.style.height=height+'px';
			loadingDiv.style.bottom='0px';
			loadingDiv.style.left='0px';
			loadingDiv.style.right='0px';
			//loadingDiv.style.backgroundColor='#000';
			//loadingDiv.style.filter='alpha(opacity=30)';
			transDiv.style.position='absolute';
			transDiv.style.backgroundColor='#000';
			//transDiv.style.opacity='0.3';
			transDiv.style.filter='alpha(opacity=30)';
			transDiv.style.height=height+'px';
			transDiv.style.top='0px';
			transDiv.style.width='100%';
			transDiv.style.bottom='0px';
			transDiv.style.left='0px';
			transDiv.style.right='0px';
			top=(height-32)/2;
			loadingImg.style.padding=top+'px auto 0 auto';
		}else{
			loadingDiv.style.position='fixed';
			loadingDiv.style.top='0px';
			loadingDiv.style.bottom='0px';
			loadingDiv.style.left='0px';
			loadingDiv.style.right='0px';
			transDiv.style.position='absolute';
			transDiv.style.backgroundColor='#000';
			transDiv.style.opacity='0.3';
			transDiv.style.filter='alpha(opacity=30)';
			transDiv.style.top='0px';
			transDiv.style.bottom='0px';
			transDiv.style.left='0px';
			transDiv.style.right='0px';
			transDiv.style.zIndex='60';
			top=Math.round((getWindowHeight()-32)/2);
			loadingImg.style.margin='0 auto '+top+'px auto';
			loadingImg.style.position='relative';
			loadingImg.style.top=top+'px';
			loadingImg.style.zIndex='50';
		}
		loadingDiv.appendChild(transDiv);
		loadingImg.style.display='block';
		loadingImg.style.width='32px';
		loadingImg.style.height='32px';
		loadingDiv.appendChild(loadingImg);
		var loadingP=document.createElement('p');
		addClass(loadingP,'waiting');
		loadingP.appendChild(document.createTextNode(formAlert['wait']));
		loadingDiv.appendChild(loadingP);
		getWindowHeight();
		document.getElementsByTagName('body')[0].appendChild(loadingDiv);
		return loadingDiv;
	}
	var getWindowHeight=function(){
		var windowHeight=0;
		if(typeof(window.innerHeight)=='number'){
			windowHeight = window.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}else if( document.body && document.body.clientHeight){
			windowHeight = document.body.clientHeight;
		}
		return windowHeight;
	}
	var checkJS=function(){
		var xmlhttp = false;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else if(window.ActiveXObject){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					xmlhttp=false;
				}
			}
		}
		return xmlhttp;
	}
}
function preparePromotion(){
	promotion=new Promotion();
	promotion.prepare();
}
addLoadEvent(preparePromotion);
