/*
    JavaScript functions to store information about the first visit 
    to a site (referrer and date), and to report that information in
    a hidden form field when needed
*/

var cookieName = 'firstVisit';
var fieldName  = 'tracking';

function grabReferrer() {
// store referrer and date in a cookie

    var cookieData = getCookie();
        
    if( ! cookieData[ cookieName ] ) {
        // ensure that nothing is overwritten if previously stored
        var visitData = window.location + '|' + ( document.referrer ? document.referrer : 'No referrer' ) + '|' + getSSDate();
        setCookie( cookieName, visitData );
    }
}

function reportReferrer( sourceType, identifier, emailAddress, mailSubject, mailBody ) {
// write referrer and date into hidden form field 
    
    var cookieData = getCookie();
    
    if( cookieData[ cookieName ] ) {
        
        var visitData = cookieData[ cookieName ].split( '|' );
        
        var trackParams = new Array();        
        trackParams[ 'domain' ] = window.location.hostname;
        trackParams[ 'source' ] = sourceType;
        trackParams[ 'identifier' ] = identifier;
        trackParams[ 'firstpage' ] = visitData[ 0 ];
        trackParams[ 'referring_url' ] = visitData[ 1 ];
        trackParams[ 'date' ] = visitData[ 2 ];        
        
        var trackingUrl = 'http://secure.grapevineit.net/site_tracking.php?';
                        
        for( var param in trackParams ) {               
            trackingUrl += param + '=' + escape( trackParams[ param ] ) + '&';
        }        
        if( sourceType == 'email' ) {            
            var htm = '<a href="' + trackingUrl + 'redirect=' + escape( 'mailto:' + emailAddress + '?subject=' + mailSubject + '&body=' + mailBody ) + '" onclick="window.open(window.location)">' + emailAddress + '</a>';
        } else {
            var htm = '<img src="' + trackingUrl + '" width="1" height="1" border="0" />';
        }
        
        document.write( htm );
    }
}

function getSSDate() {
// return the current time in sophia date format

    var now = new Date();    
       
    return now.getFullYear() + '-' + 
        ( now.getMonth() + 1 ) + '-' + 
        now.getDate() + ' ' + 
        now.getHours()  + ':' + 
        now.getMinutes() + ':' + 
        now.getSeconds();   
}

function setCookie( n, v ) {
// set a cookie with given name and value

	var today = new Date();
	var expiry = new Date( today.getTime() + 1000 * 60 * 60 * 24 * 365 );
	
	if( n && v ) {
		window.document.cookie = n + '=' + escape( v ) + ';path=/;expires=' + expiry.toGMTString();
	}
}

function getCookie() {
// return an array full of cookie data

    var cookieData = new Array();
    var cookieVals = window.document.cookie.split( ';' );

    for( var n in cookieVals ) {
        var parts = cookieVals[ n ].replace( /^\s*([^\s]*)\s*$/, '$1' );
        parts = parts.split( '=' );
		cookieData[ parts[ 0 ] ] = unescape( parts[ 1 ] );
    }
    return cookieData;
}
