/**
 * SiteCatalys funkkari ajax toiminnoille.
 *
 * @param pagename
 * @param searchwords
 * @param location
 */
function trackMapSearch(street, location, data) {
	
    try {
    	
    var extrainfo = "";
    var error = false;
    
    if (data == null || data.localities == null || data.localities[0] == null) {
        extrainfo = "_error";
        error = true;
    }

    var omtrVars = new Object();
    
    omtrVars.pageName = "ms_location";
    
    if (!error) {
    	
	    // prop1 = what
	    omtrVars.prop1 = street;
	    // prop2 = searchType
	    omtrVars.prop2 = "ms";
	
	    // prop4 = search term definition
	    omtrVars.prop4 = "basic";
	    omtrVars.prop9 = s.prop8 + ":" + omtrVars.pageName;
	    //    omtrVars.prop9 = s.prop8 + ":ms_result";
	    // prop 13 = where
	    
//	    if (location != null && location.length > 0) {
//		    omtrVars.prop13 = location;
//		    omtrVars.prop14 = location;
//	    } else {
//		    omtrVars.prop13 = ' ';
//		    omtrVars.prop14 = ' '; 
//	    }
	    
	    // proximity / regular
	    omtrVars.prop20 = "regular";
	
	    // events
	    omtrVars.events = "event1";
    
    } else {
    	
//	    omtrVars.prop13 = ' ';
//	    omtrVars.prop14 = ' ';    	
    	
    }

    // pagename + sivunumero
    omtrVars.prop25 = omtrVars.pageName + extrainfo;
    // where
    omtrVars.prop30 = location != null && location != '' ? "fon:" + location.toLowerCase() : "fon:none";

    // User registered status + pagename
    omtrVars.prop27 = s.prop26 + ":" + omtrVars.pageName;

    s.t(omtrVars);
    
    } catch (e) {
        alert(e);
    }
}

function trackAddStop() {	
    var s = s_gi(s_account);
    s.linkTrackVars='events,eVar25,products';
    s.linkTrackEvents='event13';
    s.eVar25='add to route';    
    s.events='event13'; 
    s.tl(this,'o');
}

function trackMapsDidYouMean() {
    var omtrVars = new Object();
    omtrVars.prop25="ms_location_did_you_mean";
    s.t(omtrVars);

}

function trackRouteDidYouMean() {

    var omtrVars = new Object();
    omtrVars.prop25="ms_directions_did_you_mean";
    s.t(omtrVars);

}
function trackRouteSearch(startStreet, endStreet, startLocation, endLocation, data) {
	
	var s = s_gi(s_account);
	s.events = '';
	
    var extrainfo = "";

    if (data == null || data.localities == null || data.localities[0] == null) {
        extrainfo = "_error";
    }

    var omtrVars = new Object();
    omtrVars.pageName = "ms_driving_directions";

    // prop1 = what
//    omtrVars.prop1 = startStreet + ", " + startLocation + ", " + endStreet + ", " + endLocation;
    // prop2 = searchType
//    omtrVars.prop2 = "ms";
    // prop4 = search term definition
//    omtrVars.prop4 = "basic";
    // prop9 s.prop8:pageName
    omtrVars.prop9 = s.prop8 + ":" + omtrVars.pageName;
    // prop 13 = where
//    omtrVars.prop13 = location;
    // proximity / regular
//    omtrVars.prop20 = "regular";
    // pagename + sivunumero
    omtrVars.prop25 = "ms_driving_directions" + extrainfo;
    // User registered status + pagename
    omtrVars.prop27 = s.prop26 + ":" + omtrVars.pageName;

    // events
//     omtrVars.events = "event1";

    
    s.t(omtrVars);
}

function trackBusinessSearch(searchword, location, data, maxResults, prop20) {
	
    var extrainfo = "";

    if (data.length == 0) {
        extrainfo = "_error";
    } else if (data.length > maxResults) {
        extrainfo = "_too_many";
    } 

    var omtrVars = new Object();
    omtrVars.pageName = "ms_result";

    if (data.length > 0) {
    
	    // prop1 = what
	    omtrVars.prop1 = searchword;
	    // prop2 = searchType
	    omtrVars.prop2 = "ms";
	    // prop3 = search result cluster
	    omtrVars.prop3 = getResultCount(data.length);
	    // prop4 = search term definition
	    omtrVars.prop4 = "basic";
	    // prop9 s.prop8:pageName
	    omtrVars.prop9 = s.prop8 + ":ms_result";
	    // prop 13 = where
	    omtrVars.prop13 = location != null && location != '' ? location.toLowerCase() : '';
	    // prop 14 = (ASK)
	    omtrVars.prop14 = location != null && location != '' ? "fon:" + location.toLowerCase() : "fon:none";	    
	    // products
	    omtrVars.products = parseProducts(data);
	    // events
	    omtrVars.events = "event1";
	    // pagename + sivunumero
	    omtrVars.prop25 = "ms_result_1"+extrainfo;
	 
    } else {
    	omtrVars.prop25 = "ms_result"+extrainfo;    	
    }

    // proximity / regular
    omtrVars.prop20 = prop20;

    // User registered status + pagename
    omtrVars.prop27 = s.prop26 + ":ms_result";

    s.t(omtrVars);
    
}

/**
 * Palauttaa tuloksien määrän
 * Search Results -  clustering of search results 0, 1-10, 11-25, 26-50, 51-100, 101-500, >500
 */
function getResultCount(count) {
    var countGroup = "0";

    if (count >= 1 && count <= 10) {
        countGroup = "1-10";
    } else if (count >= 11 && count <= 25) {
        countGroup = "11-25";
    } else if (count >= 26 && count <= 50) {
        countGroup = "26-50";
    } else if (count >= 51 && count <= 100) {
        countGroup = "51-100";
    } else if (count >= 101 && count <= 500) {
        countGroup = "101-500";
    } else if (count > 500) {
        countGroup = ">500";
    }


    return countGroup;
}

/**
 * Muodostaa määrittelyn mukaisen s.products-muuttujan arvon SiteCatalystille.
 *
 * @param businessResult business fast result
 * @return String
 */
function parseProducts(data) {
    // category;customer.id:entryid;1;0.00 ...

    var sb = "";
    //    var companyids = "";
    /**
     * s.products=?;<database type>:<listing ID>;;;event4=<listing position>(,;<database type>:<listing ID>;;;event4=<listing position>)?
     */
    if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            if (i > 0) sb = sb+',';
        	sb = sb + ';yp:';
            sb = sb + data[i].id; + ";;;";
            //            companyids = companyids + '<input type="hidden" id="companyid_'+(i+1)+'" value=";yp:'+data[i].id+'"/>'
        }

    }

    //    $('companyidsdiv').innerHTML = companyids;

    return sb;
}

// Use this to track the the click-throughs on a listing to 3rd party websites.
function track3PartyUrlClick(ref, contactId) {
    var s = s_gi(s_account);
    s.linkTrackVars = 'events, eVar27, products';
    s.linkTrackEvents = 'event17';
    s.eVar25 = 'kvk info';
    s.products = contactId;

    s.events = 'event17';
    s.tl(ref, 'e', '3rd party website');
}

// Use this to track the actions performed for a listing (not to 3rd party websites).
// This method is only to be used when the page isn't being reloaded after performing
// the action.
function trackEventClick(ref, linkName, events, linkType, contactId) {
	
	var s = null;
    s = s_gi(s_account);
    
    if (linkType == null) {
        linkType = 'o';
    }

    if (contactId != null) {
        s.products = ';yp:'+contactId.toString();
    }
    
    s.linkTrackVars = 'events,eVar25,products';
    s.linkTrackEvents = events;
    
  	if (linkName != null && linkName.length > 0) {
  		s.eVar25 = linkName;
	}
  	 	
    s.events = events;
   
    s.tl(ref, linkType, linkName);
}

/*
function trackEventClick2(ref, linkName, events, linkType, contactId) {
	
	var s = null;
    s = s_gi(s_account);
    
    if (linkType == null) {
        linkType = 'o';
    }

    if (contactId != null) {
    	var omtrVars = new Object();
    	omtrVars.products = ';yp:'+contactId;
    }
    
    s.linkTrackVars = 'events, eVar25, products';
    s.linkTrackEvents = events;
    
  	if (linkName != null && linkName.length > 0) {
  		s.eVar25 = linkName;
	}
  	 	
    s.events = events;
    s.t(omtrVars);
}
*/

function trackAddCustomLocation() {
    var s = s_gi(s_account);
    s.events="event18";       
    s.linkTrackVars='events,products';
    s.linkTrackEvents='event18';    
    s.events='event18';
    s.tl(this,'o','listing action');

}

function trackEventFlyout(companyId) {
    // trackedFlyoutEvents is defined in sitecatalyst.jsp
    if (!trackedFlyoutEvents.contains(companyId)) {
        var s = s_gi(s_account);
        s.linkTrackVars = 'prop37,products,events,eVar37';
        s.linkTrackEvents = 'event2';
        s.prop37 = 'map flyout';
        s.products = ";yp:" + companyId;
        s.events = 'event2';
        s.eVar37 = 'map flyout';
        s.tl(this, 'o', 'map flyout');
        trackedFlyoutEvents.push(companyId);
    }
}

function trackShowOnMapEvent(poitype, data) {
        
        var s = s_gi(s_account);
        s.linkTrackVars = 'prop23,prop24,products,events';
        s.linkTrackEvents = 'event12';
        s.prop23 = poitype;
        s.prop24 = s.prop23;
        var products = "";
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
            	if (i > 0) products += ",";
                products += ";yp:" + data[i].id;
            }
        }
        s.products = products;
        s.events = 'event12';
        s.tl(this, 'o', 'map options');
}

function trackActivateMapOptions() {
    var s = s_gi(s_account);
    s.tl(this,'o','activate map options');
}

function trackActivateViewPrintRoute() {
	
	var s = s_gi(s_account);
    s.linkTrackVars = 'events,eVar25';
    s.linkTrackEvents = 'event13';
    s.pageName = '';
    s.eVar25 = 'view print directions';
    s.events = 'event13';
    
	s.tl(this,'o','view print directions');

}
