	
function createCookie(name,value,days) 
	{
		if (days) 
			{
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
		else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
	}
	
function readCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) 
			{
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
		return ca['navn'];
	}
	
function jatakk()
	{
		createCookie('vilkar', 1, 1);
	}
		
function show_pop(fil){
	if ($('box_overlay')){
		$('box_overlay').remove();
		$('box_wrapper').remove();
	}
	
	var overlay = Builder.node('div', { id: 'box_overlay',style: 'display:none' });
	var element = Builder.node('div', { id: 'box_wrapper'}, [
	  	
		Builder.node('div', { id: 'box_rep' }),
		
	  ]);
	  
	$('body').appendChild(overlay);
	$('body').appendChild(element);
	$('box_overlay').observe('click', (function() { $('box_overlay').remove();$('box_wrapper').remove() }));
	$('box_overlay').appear({ duration: 0.3, from: 0.0, to: 0.8 });
	
	new Ajax.Updater({ success: 'box_rep' }, fil, {});
}

/* Nytt popup script 31.08 */
var pop = {
 	init: function(file,type,width,title){
 		pop.destroy();
		pop.build_window(type,width,title);
		pop.update_content(file, type);
	},
	build_window: function(type,width,title){
		var overlay = Builder.node('div', { id: 'box_overlay', style: 'display:none' });
		
		/* Legg til nye type her hvis du vil bygge ut. */
		switch(type){
			case 1:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_rep', className: 'box_rep_login' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 2:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_glemt' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
		    case 3:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_video' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 4:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_tilbakemelding' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			 case 5:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_vilkar' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 6:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_passordsendt' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			break;
		}
		  
		document.body.appendChild(overlay);
		document.body.appendChild(element);
		
		$('box_overlay').observe('click', (function() { pop.destroy(); }));
		$('box_close').observe('click', (function() { pop.destroy(); }));
		$('box_overlay').appear({ duration: 0.3, from: 0.0, to: 0.5, queue: 'end' });
		$('box_wrapper').appear({ duration: 0.3, from: 0.0, to: 1.0, queue: 'end' });
	},
	update_content: function(file){
		new Ajax.Updater({ success: 'box_rep' }, file, {});
	},
	destroy: function(){
 		if ($('box_overlay')){
			$('box_overlay').remove();
			$('box_wrapper').remove();
		}
	}
};
/* ===== */
	
	
var f = {
	init: function(){
		if(!$('contact')) return false;
		$('contact').select('input').each(function(s){
			s.observe('blur', f.sjekk_element);
		});
	},
	sjekk_element: function(e) {
		var obj = Event.element(e);
		var rel = obj.getAttribute('rel');
		var valid = true;
		var color_yes = "#fff";
		var color_no = "#ff0000";
				
		if(rel == "text")
			{
				if(obj.value.length < 3)
					{
						obj.setStyle({background: color_no });
						valid = false;
					}else{
						obj.setStyle({background: color_yes });
					}
			}
			
		if(rel == "epost")
			{
				if(obj.value.indexOf("@") == -1 || obj.value.indexOf(".") == -1)
					{
						obj.setStyle({background: color_no });
						valid = false;
					}else{
						obj.setStyle({background: color_yes });
					}
			}
			
		if(rel == "nummer")
			{
				if(isNaN(obj.value) || obj.value.length < 4)
					{
						obj.setStyle({background: color_no });
						valid = false;
					}else{
						obj.setStyle({background: color_yes });
					}
			}
				
	}
}

function validate(e)
	{
		
		var valid = true;
		var errormsg = "Vennligst fyll ut følgende felter:" + "\n";
		var fornavn = $('fornavn').getValue();
		var etternavn = $('etternavn').getValue();
		var kjonn = readCookie('gender');
		var mobil = $('mobil').getValue();
		var epost = $('epost').getValue();
		var vilkar = readCookie('vilkar');
		var color_yes = "#00ff00";
		var color_no = "#ff0000";
				
		if(fornavn.length < 2)
			{
				valid = false
				$('fornavn').setStyle({background: color_no });
				errormsg = errormsg + "- Fornavn" + "\n";
			}else{
				$('fornavn').setStyle({background: color_yes });
			}
			
		if(etternavn.length < 2) 
			{
				valid = false;
				$('etternavn').setStyle({background: color_no });
				errormsg = errormsg + "- Etternavn" + "\n";
			}else{
				$('etternavn').setStyle({background: color_yes });
			}
		
		if(mobil.length < 4 || isNaN(mobil))
			{
				valid = false;
				$('mobil').setStyle({background: color_no });
				errormsg = errormsg + "- Mobil" + "\n";
			}else{
				$('mobil').setStyle({background: color_yes });
			}
		if(epost.length < 4 || epost.indexOf("@") == -1 || epost.indexOf(".") == -1)
			{
				valid = false;
				$('epost').setStyle({background: color_no });
				errormsg = errormsg + "- E-postadresse" + "\n";
			}else{
				$('epost').setStyle({background: color_yes });
			}
			
		if(vilkar != 1)
			{
				valid = false;
				errormsg = errormsg + "NB! Du må huke av for at du har lest vilkårene";
			}
			
		if(valid)
			{
			
			}else{
				alert(errormsg);
				Event.stop(e);	
			}
		
	}

document.observe('dom:loaded', function()
	{
 		f.init();
		Event.observe('contact', 'submit', validate);
	});
		

	
	



