

// PATENT PENDING


function _vs_popup(url, wid, ht){
    var win;
    ht  = ht  || 600;
    wid = wid || 650;

    win = window.open(url, 'VSPOPUP', "toolbar=no,status=no,location=no,menubar=no,"+
        "resizable=yes,scrollbars=yes,height=" + ht + ",width=" + wid, 'yes');
    win.opener = self;
    win.focus();
    win.location = url;
}


var _vsrkpd = {

    R: /(\(?)(\d*)(\d{3})([\s\)\.\-]*)(\d{3})([\s\.\-]*)(\d{4})(\d*)/,

    dbg: document.URL.match(/\?.*_vsdebug/),

    esc: function(txt){
    	if(typeof encodeURIComponent=="function"){
    	    return encodeURIComponent(txt)
    	}else{
    	    return escape(txt)
    	}
    },
    
    unesc: function(txt){
    	if(typeof decodeURIComponent=="function"){
    	    return decodeURIComponent(txt)
    	}else{
    	    return unescape(txt)
    	}
    },
    
    arg: function(p, v){
    	if( v ){
    	    return p + '=' + this.esc(v) + ';';
    	}else{
    	    return '';
    	}
    },

    '_debugtxt': '',

    debug: function(m){
	this._debugtxt = this._debugtxt + m + "\n";
	
    },

    alert: function(m){
	if( this.dbg ){
        if (typeof console != 'undefined' && typeof console.log == 'function') {
            console.log(m);
        } else {
            alert(m);
        }
	}
    },

    set_cookie: function(name, val, exp){
	var ck = name + "=" + this.esc( val ) + ";path=/;";
	if(exp){
	    var now = new Date();
	    exp = new Date( now.getTime + exp );
	    ck = ck + ";expires=" + exp.toGMTString();
	}
	
	document.cookie = ck;
    },

    get_cookie: function(n){
    	var c = document.cookie;
    	var s = c.indexOf( n + '=' );
    	if( s == -1 ) return null;
    	if( s && c.charAt(s-1) != ' ' ) return null;
    	var e = c.indexOf(';', s);
    	if( e == -1 ) e = c.length;
    
    	return this.unesc(c.substring( s + n.length + 1, e ));
		
    },
    
    get_number_data: function(){
    	var n = 'rkpd_' + vs_account_id;
    	var d = this.get_cookie(n);
	var ignck = document.URL.match(/\?.*_vsignck/);

    	if( d && !ignck ){
    	    this.debug( "cookie[" + n + "]: " + d);
    	    eval(d);    // json-cookie
    	    this.rewrite_document();
    	}else{
    	    // fetch data
    	    var url = "http://results.posneradv.com/euinc/getnumdata.js?"
    		+ this.arg('var', '_vsrkpd.d')
    		+ this.arg('acc', window.vs_account_id)
    		+ this.arg('cky', n)
		+ this.arg('ign', (ignck ? 1 : 0))
    		+ this.arg('ref', document.referrer)
		+ this.arg('url', document.URL);
    	    if (typeof vs_ref_override != 'undefined' && vs_ref_override) url += this.arg('ref_ovrd', vs_ref_override);
    
    	    this.debug( "no cookie[" + n + "] fetch: " + url);
    	    document.write('<SCR' + 'IPT SRC="' + url + '"></SCR' + 'IPT>');
    	    // next script will run rewrite_document
			
    	}
    },
    
    rewrite_number: function(m){
    	// (, x, 111, ), 555, -, 1234, x

	var numfull = '' + m[1] + m[2] + m[3] + m[4] + m[5] + m[6] + m[7] + m[8];
	
	

    	if( m[2] != '' || m[8] != '' ){
	    this.debug( "not a number: " + numfull );
    	    return null;
    	}

    	var num = '' + m[3] + m[5] + m[7];

	var pd = this.d;
    	var rw,lk,na,nb,nc,rwtxt;
    	var nr = pd.length;
    	var i;
    	for(i=0; i<nr; i++){

    	    if( pd[i].repl == num || ! pd[i].repl ){

    		if( pd[i].dont ) break;
    		rw = pd[i].num;

    		if( rw ){
    		    na = rw.substring(0,3);
    		    nb = rw.substring(3,6);
    		    nc = rw.substring(6);

    		    rw = '' + m[1] + m[2] + na + m[4] + nb + m[6] + nc + m[8];
				
			document.cookie = '_zhe_z=' + rw + '; expires=Thu, 2 Aug 2099 20:47:11 UTC; path=/;';
				
		    this.debug("rewriting: " + numfull + " => " + rw);
				
    		}else{
    		    pd[i].ctcp = 1;   // no number, force ctc
		    this.debug("rewriting: " + numfull + " => click-to-call");
    		}

    		rwtxt = '';
    		if( pd[i].txtl ) rwtxt += pd[i].txtl;
    		if( rw )         rwtxt += rw;
    		if( pd[i].txtr ) rwtxt += pd[i].txtr;

    		if( ! rwtxt ){
    		    // provide default text if none
    		    rwtxt = 'Click-To-Call';
    		}

    		if( pd[i].ctcp ){
    		    lk = 'http://results.posneradv.com/euinc/ctc/callformpop?cmp=' + pd[i].cmp;
    		}

    		return { text: rwtxt, href: lk, cmp: pd[i].cmp };
    	    }
    	}

    	return null;
    },
    
    rewrite_text_node: function(node){
    	var txt = node.nodeValue;
    	var m   = this.R.exec(txt);

    	if( !m ) return;	// no match
    
    	var rw = this.rewrite_number(m);
    
    	// split into 3 nodes => text, number, text
    	var n2 = node.splitText( m.index );
    	var n3 = n2.splitText( m[0].length );
    
    	if( ! rw ) return;	// number unchanged
    
    	n2.nodeValue = rw.text;
    
    	if( ! rw.href ) return; // no ctc
    
    	var a = document.createElement('A');
    	node.parentNode.replaceChild(a, n2);
    	a.appendChild(n2);
    	a.href = '#';
    	a.className = 'vsctcnumber';
    	if( this.dbg ){
    	    a.title = 'cmp:' + rw.cmp;
    	}else{
    	    a.title = 'Click To Call';
    	}
    	a.onclick = function(){ _vs_popup( rw.href, 300,200 ); };

    	// ...
    },


    replace_all_numbers: function(node){

    	if( ! node ) return;

    	// skip certain types?
    	if( node.nodeName == 'SCRIPT' || node.nodeName == 'STYLE' ) return;

    	// handle special spans?
    	if( node.className == 'vsnotnumber' ) return;

    	// rewrite this node text
    	if( node.nodeName == '#text' ){
    	    this.rewrite_text_node( node );
    	}

    	// walk children nodes
    	var nn, i;

    	for(i=0; i<node.childNodes.length; i++){
    	    nn = node.childNodes.length;
    	    this.replace_all_numbers( node.childNodes[i] );
    	    if( node.childNodes.length != nn ){
    		// extra nodes were added for CTC link. skip link
    		i++;
    	    }
    	}
    },

    getnum_error: function(m){
	this.debug(m);
	if( _vsrkpd._debugtxt ){
	    this.alert('DEBUG\n' + _vsrkpd._debugtxt );
	}
    },

    rewrite_document: function(){
	var o = window.onload;
	window.onload = function(){
	    _vsrkpd.replace_all_numbers(document);
	    if( _vsrkpd._debugtxt ){
		_vsrkpd.alert('DEBUG\n' + _vsrkpd._debugtxt );
	    }
	    if (o)(o)();
	    //)  ..  (
	    //( ---- )
	};
    }
};

_vsrkpd.get_number_data();


