window.onload = applyEvents;
displayLoader = function (loaderParentId) {
	var parentTop=$(loaderParentId).cumulativeOffset().top;
	var parentLeft=$(loaderParentId).cumulativeOffset().left;
	var parentWidth=$(loaderParentId).getWidth();
	var parentHeight=$(loaderParentId).getHeight();
	//alert(loaderParentId + parentTop + parentLeft + parentWidth + parentHeight);
	$('loader').setStyle({position: 'absolute', top: parentTop+'px', left: parentLeft+'px', width: parentWidth+'px', height: parentHeight+'px'});
	$('loader').removeClassName('display-none');	
}

hideLoader = function () {
	$('loader').addClassName('display-none');	
}

function hideImage() {
if($('imageareaempty')){
	$('imagearea').addClassName('display-none');
	$('imageareaempty').removeClassName('display-none');
}	
}

function showImage() {
if($('imageareaempty')){
	$('imagearea').removeClassName('display-none');
	$('imageareaempty').addClassName('display-none');
}	
}

function applyStylesheet(title) {  
  $$('link[type=text/css][title]').each(function(stylesheet) {
    stylesheet.disabled = true;
    if (stylesheet.title == title) {
      stylesheet.disabled = false;
    }
  });
}

function Validate(formName){

	var form = $(formName);
	var inputs = new Array();
	inputs=form.getInputs();
	
	$$('input.validate').each(function(theInput) {
		fName=form[theInput.readAttribute('name')];
		alert('Innehåll saknas i fältet '+theInput.readAttribute('name'));
		theInput.activate();
		return false;
	});
}

function ValidateApplyForm(formName){
	
	var form = $(formName);
	
	if ($F(form['Name'])=='') {
		alert('Ange ditt namn!')
		form.Name.focus();
		return false;
	}
	
	if ($F(form['Address'])=='') {
		alert('Ange din adress!')
		form.Address.focus();
		return false;
	}

	if ($F(form['ZipCode'])=='') {
		alert('Ange ditt postnummer!')
		form.ZipCode.focus();
		return false;
	}
	
	if ($F(form['City'])=='') {
		alert('Ange vilken stad du bor i!')
		form.City.focus();
		return false;
	}
	
	if ($F(form['Phone'])=='') {
		alert('Ange ditt telefonnummer!')
		form.Phone.focus();
		return false;
	}
	
	return true;

}


//Validates the content of the form fields and  submits the form.
function AjaxSubmit(formName) {
	
	var form = $(formName);
	var inputs = new Array();
	var pars=new Hash();
	
	inputs=form.getInputs();
	
	inputs.each(function(theInput) {
		
		fName=form[theInput.readAttribute('name')];
		pars.set(theInput.readAttribute('name'),$F(fName));

	});
	
	/*$(form).request({
		parameters: { 'Ajax':'True', 'AjaxReadMore':'True' },
		onComplete: function(){
		alert(form.readAttribute('action'));
		new Ajax.Updater('popup_textwrap', form.readAttribute('action')+'&Ajax=True&AjaxReadMore=True', {evalScripts: true, onSuccess: hideLoader});
		}
	})*/
	new Ajax.Updater('popup_textwrap', form.readAttribute('action'), {method: 'post', parameters: pars.toQueryString()+'&Ajax=True&AjaxReadMore=True', evalScripts: true, onSuccess: hideLoader});
	this.blur();
}


function applyEvents(){
	if(document.getElementById && document.getElementsByTagName){
		
		var popup_close;
		popup_close=document.getElementById("popup_close");
		
		if (popup_close){
			popup_close.onclick = function (oEvent){
				var oEvent = (typeof oEvent != "undefined")? oEvent : event;
				oEvent.returnValue = false;
				if(oEvent.preventDefault){
					oEvent.preventDefault();
				}
				showImage();
				document.getElementById("popup_frame").className = "display-none";
				document.getElementById("popup_inner").className = "display-none";
				applyStylesheet('print');
			}
		}
		
		var arrAllLinks = $$('a');
		//var arrDivs = document.getElementsByTagName("div");
		//var oDiv;
		var oLink;
		for(var i=0; i<arrAllLinks.length; i++){
	    	oLink = arrAllLinks[i];
			//Handle click on thumb images on Art Project Pages. The Id of the link represents the Image to show.
			if(oLink.className.search(/servicemenuitem/) != -1){
				oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					document.getElementById("popup_frame").className = "popup display-block";
					document.getElementById("popup_inner").className = "popupctn display-block";
					hideImage();
					displayLoader('popup_wrap');
					new Ajax.Updater('popup_wrap', this.href+'/Ajax/True', {evalScripts: true, onSuccess: hideLoader});
					applyStylesheet('print_ajax');
					
					this.blur();
				}
			}
			
			if(oLink.className.search(/popupmenuitem/) != -1){
				oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					displayLoader('popup_wrap');
					new Ajax.Updater('popup_wrap', this.href+'/Ajax/True', {evalScripts: true, onSuccess: hideLoader});
					this.blur();
				}
			}
			
			if(oLink.className.search(/ajax/) != -1){
				if(oLink.className.search(/readmore/) != -1){
					oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					displayLoader('popup_wrap');
					new Ajax.Updater('popup_textwrap', this.href+'/Ajax/True/AjaxReadMore/True', {evalScripts: true, onSuccess: hideLoader});
					this.blur();
					}
				}
				
				if(oLink.className.search(/apply/) != -1){
					oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					displayLoader('popup_wrap');
					new Ajax.Updater('popup_textwrap', this.href+'/Ajax/True/AjaxReadMore/True', {evalScripts: true, onSuccess: hideLoader});
					this.blur();
					}
				}
						
				if(oLink.className.search(/goback/) != -1){
					oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					displayLoader('popup_wrap');
							new Ajax.Updater('popup_wrap', this.href+'/Ajax/True', {evalScripts: true, onSuccess: hideLoader});
							this.blur();
					}
				}
				
				if(oLink.className.search(/yearlink/) != -1){
					oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
					displayLoader('popup_wrap');
							new Ajax.Updater('popup_wrap', this.href+'/Ajax/True', {evalScripts: true, onSuccess: hideLoader});
							this.blur();
					}
				}
						
				if(oLink.className.search(/sendapply/) != -1){
					oLink.onclick = function (oEvent){
					var oEvent = (typeof oEvent != "undefined")? oEvent : event;
					oEvent.returnValue = false;
					if(oEvent.preventDefault){
						oEvent.preventDefault();
					}
						if (ValidateApplyForm('LedigBefattning')){
							displayLoader('popup_wrap');
							AjaxSubmit('LedigBefattning');
						}
					}
				}
			}
			
		}
		
	}
}
