// JavaScript Document

/*
 * getPuCookie
 * -----------
 * If the cookie is set it returns the username, in this case 
 * if it does, it returns the value of user, else it returns an empty string.
 * 
 */
function getPuCookie(c_name){
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
	   		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	}
  	}
	return "";
}

/*
 * setPuCookie
 * -----------
 * Set the cookie with the given information in checkPuCookie
 * in this case: 
 * - Set the cookie name: user
 * - Set the value: PuShown
 * - Set the expiration date
 */
function setPuCookie(c_name,value,expiredays,domain){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	path = "/";

	document.cookie=c_name+"="+escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + 
	((path) ? ";path=" + path : "" ) + 
	((domain) ? ";domain=" + domain : "" );
}

/*
 * checkPuCookie
 * -------------
 * Check if cookie is set, if not, document write the popunder tag (NOTICE!: the placement ID in the tag only needs to be changed)
 * then create a cookie, in this case the cookie is set for 1 dag ('ligatusPU',ligatusPU,1)
 * To set the cookie for 2 or more days, just change the 1 into a 2.
 * 
 * NOTICE:
 * - Don't forget to set the placement ID in the tag! 
 * - Don't forget to set the correct domain name without trailing www but with the dot!
 * 
 */
function checkPuCookie(){
	ligatusPU=getPuCookie('ligatusPU');
	
	//CHANGE DOMAIN NAME
	if (document.domain.match(/(demo|staging)\.gay\.eu$/)){
		a_match = document.domain.match(/(demo|staging)\.gay\.eu$/);
		domain = '.gay.eu';
	} else {
		a_match = document.domain.match(/\.?(gaygirls|gay)\.(nl|eu)/);
		domain = '.' + a_match[1] + '.' + a_match[2];
	}

	if (ligatusPU==null || ligatusPU==""){
		var i_num = Math.floor(Math.random()*11);
		if (i_num%2==0){
			window.open('http://www.gay2day.nl/?utm_source=gnl&utm_medium=popup&utm_campaign=pu', 'gay2day', 'width=1024,height=768,scrollbars=yes,toolbar=no,status=no,location=no,menubar=no,resizeable=yes');
		} else {
			//CHANGE POPUNDER PLACEMENT ID
			document.write('<script type="text\/javascript" src="http://d.ligatus.com/?ids=28403&t=js"><\/script>');
		}
		
		ligatusPU="PuShown";
		if (ligatusPU!=null && ligatusPU!=""){
			setPuCookie('ligatusPU',ligatusPU,1,domain);
		}
	}
}
