
// Global variables

var cookiePrefix		= "v" + pageID + "_";
var cssUrl 			= "r.vertster.com/css/" + pageID + ".css";
var trackingdomain 		= 'r.vertster.com/track';

function QueryString(key)
{
    var value = null;
    for (var i=0;i<QueryString.keys.length;i++)
        {
            if (QueryString.keys[i]==key)
                {
                    value = QueryString.values[i];
                    break;
                }
            }
            return value;
        }

QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
    var query = window.location.search.substring(1);
    var pairs = query.split("&");
    
    for (var i=0;i<pairs.length;i++)
        {
            var pos = pairs[i].indexOf('=');
            if (pos >= 0)
                {
                    var argname = pairs[i].substring(0,pos);
                    var value = pairs[i].substring(pos+1);
                    QueryString.keys[QueryString.keys.length] = argname;
                    QueryString.values[QueryString.values.length] = value;		
                }
            }
            
        }

QueryString_Parse();

if (window.location.protocol=="http:")
    document.write('<link rel=stylesheet href="http://'+escape(cssUrl)+'" type="text/css">');
else if (window.location.protocol=="https:") 
    document.write('<link rel=stylesheet href="https://'+escape(cssUrl)+'" type="text/css">');

var thismode = QueryString('mode');
var recipeIDcookie = v_getCookie(cookiePrefix + 'recipeID');
var rotateUrl = null;
// preview urls
if (thismode == 'preview') {
rotateUrl = code_domain + '/recipes/preview/' + QueryString('recipe_id') + '.js';
}
if (thismode == 'setup' && rotateUrl == null) {
rotateUrl = code_domain + '/factors/setup/' + QueryString('factor_id') + '.js';
}
// Returning so build the cached url
if (recipeIDcookie != null && rotateUrl == null) {
rotateUrl = rotate_domain + '/shown/' + recipeIDcookie + '.js';
}
// New user so build a new file
if (recipeIDcookie == null && rotateUrl == null) {
rotateUrl = rotate_domain + '/' + pageID + '.js';	
}

function v_getCookie(NameOfCookie){
    
    if (document.cookie.length > 0) { 
        begin = document.cookie.indexOf(NameOfCookie+"="); 
        if (begin != -1) { 
            begin += NameOfCookie.length+1; 
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        } 
    }
    return null;
}

function v_setCookie(NameOfCookie, value, expiredays, thepath, thedomain) {

    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    document.cookie = NameOfCookie + "=" + escape(value) +
    ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + 
    "; path=/" +
    ((thedomain == null) ? "" : "; domain=" + thedomain );
    
}

function v_testMeter() {
    
    var testSeed = Math.floor((Math.random() * 100));
    if(window.percentInTest) {
        if (v_exclude == 'true') {
            percentInTest = 0;
        }
        else if (v_exclude == 'false') {
            percentInTest = 100;
        }
    }
    else {
        percentInTest = 100;
    }
    
    if (testSeed < percentInTest) {
        return true;
    }
    else {
        return false;
    }
}

/*
function gTrack() {
	if (window.thisRecipeName) {
		__utmSetVar(thisRecipeName);
		//alert(thisRecipeName);
	}
}
*/

// Variable Declarations

var checkCookie = 'nFactors' + pageID;
var cookieValue = v_getCookie(checkCookie);

var testMode = '&testMode=';
var isTest = QueryString('testMode');
var safeBrowser = true;

if (isTest != null) {
    testMode = testMode + isTest;
    v_setCookie(pageID + 'vexclude','false',365,'/','');
}

var v_exclude = v_getCookie(pageID + 'vexclude');
if (v_exclude == null) {
    v_exclude = false;
}

// Browser detection for protection from incapable browsers
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
// screen out older IE browsers
if (version < 6 && browser == "Internet Explorer")
{
	safeBrowser = false;
}
// screen out older Opera Browsers
if (version < 8.5 && browser == "Opera")
{
	safeBrowser = false;
}

// Conversion Functions
// ----------------------------------------------------

function doesitexist(cookie_Name) {
    
    var theCookie = v_getCookie(cookie_Name);
    if (theCookie == null) 
        return false;
    else
        return true;
}


function convertit(cookie_Name) {
    
	var actionType = 'conversion';
	
	var convertee = v_getCookie(pageID);
	
	// get campaignID from cookie
	var campaignID = v_getCookie(convertee + "_campaignID");
	var getcookiename = cookiePrefix + 'recipeID';
	var recipeID = v_getCookie(getcookiename);
	var cookieID = v_getCookie(pageID);
	
	var code_version = "1";
    
    if (window.location.protocol=="http:")
        document.write("<img height=1 width=1 src=" + "'http://" + trackingdomain + "/?recipe_id=" + recipeID + "&action_type=" + actionType + "&campaign_id=" + campaignID + "&visit_converted=" + convertee + "&visit_id=" + cookieID + "'>");
    else if (window.location.protocol=="https:") 
        document.write("<img height=1 width=1 src=" + "'https://" + trackingdomain + "/?recipe_id=" + recipeID + "&action_type=" + actionType + "&campaign_id=" + campaignID + "&visit_converted=" + convertee + "&visit_id=" + cookieID + "'>");
}

function convertit2(cookie_Name,conversion_Value) {
    

	var actionType = 'sale';
		
	var convertee = v_getCookie(pageID);
	
	// get campaignID from cookie
	var campaignID = v_getCookie(convertee + "_campaignID");
	var getcookiename = cookiePrefix + 'recipeID';
	var recipeID = v_getCookie(getcookiename);
	var cookieID = v_getCookie(pageID);
	
	var code_version = "1";
    
    if (window.location.protocol=="http:")
        document.write("<img height=1 width=1 src=" + "'http://" + trackingdomain + "/?recipe_id=" + recipeID + "&value=" + conversion_Value + "&action_type=" + actionType + "&campaign_id=" + campaignID + "&visit_converted=" + convertee + "&visit_id=" + cookieID + "'>");
    else if (window.location.protocol=="https:") 
        document.write("<img height=1 width=1 src=" + "'https://" + trackingdomain + "/?recipe_id=" + recipeID + "&value=" + conversion_Value + "&action_type=" + actionType + "&campaign_id=" + campaignID + "&visit_converted=" + convertee + "&visit_id=" + cookieID + "'>");
    
}

if (QueryString('mode') == 'p')
	mode = 'preview';

// Generate remote scripting
// ----------------------------------------------------

if (safeBrowser) {
    if (document.getElementById && document.createElement) {
        if (cookieValue != null && isTest == null) { //Returning, so pull cookie
            if (window.location.protocol=="http:")
                document.write("<scr"+"ipt src="+"http://" + escape(rotateUrl) +"></scr"+"ipt>");
            else if (window.location.protocol=="https:") 
                document.write("<scr"+"ipt src="+"https://" + escape(rotateUrl) + "></scr"+"ipt>");
        }
        else { //First time through
            
            if (v_testMeter()) {
                if (cookieValue == null||testMode != null) {
                    if (window.location.protocol=="http:") {
                        document.write("<scr"+"ipt src="+"http://"+ escape(rotateUrl) +"></scr"+"ipt>");}
                    else if (window.location.protocol=="https:") {
                        document.write("<scr"+"ipt src="+"https://"+ escape(rotateUrl) +"></scr"+"ipt>");}
                }
                v_setCookie(pageID + 'vexclude','false',365,'/','');
            }
            else {
                v_setCookie(pageID + 'vexclude', 'true', 365, '/', '');
            }
        }
    }
}


