var cookies_age = 1;
//
// add onclick to A tag
//
if (   ( is_valid_url_to_insert_onclick() ) 
    && ( is_valid_cookies_to_insert_onclick() )  
   ) {
    //Return true;
	insert_onclick();
}

function is_valid_cookies_to_insert_onclick() {
    var affiliates_value = GetCookie("affiliates");
    
    //No affiliates cookie OR affiliates cookie value is not open
    if( (affiliates_value == null) || ( !(affiliates_value) ) ) {   
        return true; 
    }else{
        return false; 
    }
}

function is_valid_url_to_insert_onclick() {

    return true;
    
    var valid_url = new RegExp("^((http(s?))\://)?(www.)?maps-guide.org/(africa|asia|europe|middle-east|pacific|thailand)", "i");
    
    if ( valid_url.test(window.top.location.href) ) {
        return true;
    }
    else {
        return false;
    }
}

function insert_onclick(){
    
    var xaHref = document.getElementsByTagName("a");
    var aClick = 'undefined';
    
    for(i=0;i<xaHref.length;i++){
    
        var aHref = xaHref[i].href;
        for(j=0;j<xaHref[i].attributes.length;j++){
            var newAttName = xaHref[i].attributes[j].name;
            var newAttValue = xaHref[i].attributes[j].value;
            if( ( ((newAttName =='onclick') || (newAttName=='onClick') ) && (newAttValue !='null') ) || ( (newAttName =='target') && (newAttValue.length > 0) ) )  {
                aClick = 'defined';
                break;
            } else {
                aClick = 'undefined';
            }
        }
    
        var aInnerHtml = xaHref[i].innerHTML;
    
        if(aClick == 'defined') {
    
        } else {
            //FF trick;
            xaHref[i].setAttribute("onClick", "createPopUnder();");
    
            //IE Trick
            xaHref[i].onclick= function(){ createPopUnder(); }
        }
    }  
} 

function createPopUnder(){

    var affiliates_value = GetCookie("affiliates");

    if( affiliates_value == null ) {         
        SetCookie("affiliates", "open" , cookies_age);
        display_popunder();
    }
    else {
        if( affiliates_value == 'open' ){
            //alert('Nothing');
        }
        else {
            SetCookie("affiliates", "open" , cookies_age);
            display_popunder();
        }
    }
}

function display_popunder() {
    var winfeatures="resizable=1,status=0,scrollbars=0,toolbar=0,location=0,menubar=0,height=430,width=810";
    var win3 = new Object;
    
    //Redirect url
    var popunder_url = '/cgi/partner/forward.cgi?';
    var base_url = window.top.location.protocol + '//' + window.top.location.hostname + window.top.location.pathname;
    
    win3=window.open(popunder_url+'url='+escape(base_url),"affiliates",winfeatures);
    win3.blur();
    win3.opener.focus();
    window.focus();
}

function GetCookie (name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    
    while (i < clen) 
    {
        var j = i + alen;
        
        if (document.cookie.substring(i, j) == arg)            
            return getCookieVal (j);
        
        i = document.cookie.indexOf(" ", i) + 1;
    
        if (i == 0) break; 
    }
    
    return null;
}

function getCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    
    return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie(name, value, expiredays) {
    var expdate = new Date(); 
    expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * expiredays));      
    WriteCookie(name, value, expdate);  
}

function WriteCookie (name, value, expires) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        
        var cookie = name + "=" + escape(value) +
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? ("; path=" + "/") : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");

        document.cookie = cookie;
}



