function addEvent(obj, evType, fn){ 
	 if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

function ajaxObject(url, callbackFunction) {  
	var that=this;        
	this.updating = false;  
	this.abort = function() {    
		if (that.updating) {      
			that.updating=false;      
			that.AJAX.abort();      
			that.AJAX=null;    
		}  
	}  
	this.update = function(passData,postMethod) {     
		if (that.updating) { 
			return false; 
		}    
		that.AJAX = null;                              
		if (window.XMLHttpRequest) {                    
			that.AJAX=new XMLHttpRequest();                  
		} else {                                        
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
		}                                                 
		if (that.AJAX==null) {                                   
			return false;                                   
		} else {      
			that.AJAX.onreadystatechange = function() {          
				if (that.AJAX.readyState==4) {                       
					that.updating=false;                          
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);                  
					that.AJAX=null;                                                 
				}                                                            
			}                                                              
			that.updating = new Date();                                    
			if (/post/i.test(postMethod)) {        
				var uri=urlCall+'?'+that.updating.getTime();        
				that.AJAX.open("POST", uri, true);        
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
				that.AJAX.send(passData);      
			} else {        
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());         
				that.AJAX.open("GET", uri, true);                                     
				that.AJAX.send(null);                                               
			}                    
			return true;                                                 
		}                                                		
	}  
	var urlCall = url;          
	this.callback = callbackFunction || function () { };
}


		
function openPopup() {
	
	if(document.getElementById('popup').style.display != "inline")
		bodyheight = document.body.style.height;
	popupHeightfixe2();
	document.getElementById('popup').style.display = "inline";
}

var scrolly ="";
var bodyheight = "";
function popupHeightfixe2(){
	var top = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
	var height = 0;
	  if( typeof( window.innerHeight ) == 'number' ) {
	    height = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    height = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    height = document.body.clientHeight;
	  }


	var popupHieght = document.getElementById('popup').offsetHeight+document.getElementById('popup').offsetTop
	var direction =""
	
	if(scrolly !=""){
	 	if(scrolly <= top)
	 	 	direction = "down";
	 	else
	 	 direction = "up";
	 	
	}
	
	if(height >= document.getElementById('popup').offsetHeight+100){
		document.getElementById('popup').style.top = top+100+"px";

	}
	else if(document.getElementById('popup').offsetHeight-height <= 100 && height >= document.getElementById('popup').offsetHeight){
		document.getElementById('popup').style.top = top+(parseInt(height)-parseInt(document.getElementById('popup').offsetHeight))+"px";
	}
	else{
		if(direction == "down"){
		 	if(popupHieght <(top+height)){
		 	 document.getElementById('popup').style.top = (top+height)-document.getElementById('popup').offsetHeight+"px";;
		 	}
		}
		else{
		 	if(top < document.getElementById('popup').offsetTop){
		 	 	document.getElementById('popup').style.top = top+"px";
		 	}
		 
		}
	}
	
	if(document.getElementById('popup').offsetHeight+100 >=document.body.offsetHeight){
		document.body.style.height = document.getElementById('popup').offsetHeight+100+'px';
	}
		
	scrolly= top;

	
}
var refresh = "no";				
function closePopup() {
	document.getElementById('popup').style.display = "none";
	document.body.style.height = bodyheight;
	if(refresh == "yes")
		window.location = window.location;
}
///courses popup

function agenda_popup(time) {		
		var agenda_popup_AJAX = new ajaxObject('/includes/courses/agenda_popup.php');    
		agenda_popup_AJAX .callback = function(responseText, responseStatus, responseXML) {
			document.getElementById("popup").innerHTML = responseText;
        	openPopup();
        };
		agenda_popup_AJAX.update('date='+time,'GET');
}		
//artikel popup				

function artikel_popup(id_webshop_artikel,refresh_page) {		
		if(refresh_page == "yes")refresh = refresh_page;
		else refresh = "no";
		
		var artikel_popup_AJAX = new ajaxObject('/includes/artikel_popup.php');    
		artikel_popup_AJAX .callback = function(responseText, responseStatus, responseXML) {
			document.getElementById("popup").innerHTML = responseText;
        	openPopup();
        };
		artikel_popup_AJAX.update('id_webshop_artikel='+id_webshop_artikel,'GET');
}

function addToCart() {		
		var post = "ajax=yes";		
		var size=document.getElementsByName("artikel_size");
		for(i = 0; i< size.length;i++){
			post += "&size["+size[i].id+"]="+size[i].options[size[i].selectedIndex].value;
		}	
		var addToCart_AJAX = new ajaxObject('/includes/updateCart.php');    
		addToCart_AJAX .callback = function(responseText, responseStatus, responseXML) {
			mini_shoppingCartAjax();
        };
		addToCart_AJAX.update(post,'POST');
}	
	
function article_popup(id_website_article) {		
		var article_popup_AJAX = new ajaxObject('/includes/articles_popup.php');    
		article_popup_AJAX .callback = function(responseText, responseStatus, responseXML) {
			document.getElementById("popup").innerHTML = responseText;
			openPopup();
        };
		article_popup_AJAX.update('id_website_article='+id_website_article,'GET');
		openPopup();
}		
		
//mini_shoppingCart
function mini_shoppingCartAjax() {		
		var mini_shoppingCar_AJAX = new ajaxObject('/includes/mini_shoppingcart.php');    
		mini_shoppingCar_AJAX .callback = function(responseText, responseStatus, responseXML) {
			document.getElementById("mini_shoppingCart").innerHTML = responseText; 
        };
		mini_shoppingCar_AJAX.update('ajax=yes','POST');
}

addEvent(window, 'scroll', function(){
	popupHeightfixe2();
})

addEvent(window, 'load', function(){
	document.getElementById("nlInput").firstChild;
	var input = document.getElementById("nlInput").firstChild;
	var nlSubmit = document.getElementById("nlSubmit");
	input.blur();
	with ({ "input": input }) {
		//onfocus
		addEvent(input, 'focus', function(){
			if(input.value == 'your E-mail address'){
				input.value= '';
				input.style.color = '#000';
			}
		});
		//onblur
		addEvent(input, 'blur', function(){
			if(input.value == ''){
				input.value= 'your E-mail address';
				input.style.color = '#7c7c7c';
				
			}
		});	
		addEvent(input, 'keypress', function(e){
			var evt = e || window.event;
			if(evt.keyCode == 13)
				mailingAdd(input);
			
		});
	}
	//onsubmit
	with ({ "nlSubmit": nlSubmit }) {
		addEvent(nlSubmit, 'click', function(){
			mailingAdd(input);
		});
	}	
});


function mailingAdd(input){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (input.value.search(filter) == -1){
		input.focus();
		alert("Please input a valid email address!");
		input.focus();
		return false
	}	
	var mailingAdd_AJAX = new ajaxObject('/includes/newsletterAdd.php');    
	mailingAdd_AJAX.update('email='+input.value,'GET');
    alert("Thank you for your subscription, you will now receive our newsletters.");	    
}


