﻿// IndexTools Helper
// Last modified 2009/09/07, JFA

ith = {
    constructor: function() {
        this.setDocumentname(this.trim(_ith_documentname));
        this.setDocumentgroup(this.trim(_ith_documentgroup));
        this.setActionId(_ith_action);
        this.setSegmentId(_ith_segment);
        this.setDomains(_ith_domains);
        this.setHierarchy(this.trim(_ith_hierarchy));
        this.setCustomVars(new Array(21));
        this.setCustomVar({ index: 8, value: _ith_segment });
        return this;
    },

    setDocumentname: function(p) { this.documentname = p; },
    getDocumentname: function() { return this.documentname; },

    setDocumentgroup: function(p) { this.documentgroup = p; },
    getDocumentgroup: function() { return this.documentgroup; },

    setActionId: function(p) { this.actionId = p; },
    getActionId: function() { return this.actionId; },

    setSegmentId: function(p) { this.segmentId = p; },
    getSegmentId: function() { return this.segmentId; },

    setDomains: function(p) { this.domains = p; },
    getDomains: function() { return this.domains; },

    setSKU: function(p) { this.sku = p; },
    getSKU: function() { return this.sku; },

    setCustomVars: function(p) { this.customVars = p; },
    getCustomVars: function() { return this.customVars; },

    setCustomVar: function(p) { this.customVars[p.index] = p.value; }, // {index:,value:} 
    getCustomVar: function(p) { return this.getCustomVars()[p]; }, // {index:}

    setHierarchy: function(p) {
        this.hierarchy = _ith_hierarchy.split(" > ");
        for (var i = 0; i < this.hierarchy.length; i++) this.hierarchy[i] = this.trim(this.hierarchy[i]);
        this.hierarchy.pop();
        this.hierarchy.shift();
    },
    getHierarchy: function() { return this.hierarchy; },

    setVars: function() {
        var doc = ith.constructor();
        itp.processVars(doc);
        //doc.setITVars(1);  //Leave this for debugging
        doc.setITVarsV5(1);
        return void doc; // doc.debug()
    },

    setITVars: function(p) { // {cfType:} 1 = Y! Standard Custom Fields					  
        DOCUMENTNAME = this.formatVar(this.getDocumentname());
        DOCUMENTGROUP = this.formatVar(this.getDocumentgroup());
        ACTION = this.getActionId();
        DOMAINS = this.getDomains();

        if (typeof p.cfType == "undefined") {
            var cfStart = 1;
        } else {
            var cfStart = 7;
        }
		tmpHierarchy = this.getHierarchy();
        //tmpHierarchy = clone(this.getHierarchy());
        for (var i = cfStart; i <= cfStart + 4; i++)
            if (tmpHierarchy.length > 0) {
                eval("_s_cf0" + i + "='" + this.formatVar(tmpHierarchy.shift()) + "';");
        } else {
            eval("_s_cf0" + i + "='[sub-category not applicable]';");
        }
        if (typeof this.getSKU() != "undefined") _S_SKU = getSKU();
        for (var i = 6, prefix = ""; i <= 20; i++)
            if (typeof this.getCustomVars()[i] != "undefined") {
            eval("_s_cf" + ((i > 9) ? "" : "0") + i + "='" + this.getCustomVar(i) + "';");
        }
    },


    setITVarsV5: function(p) { // {cfType:} 1 = Y! Standard Custom Fields					  

        var YWATracker = YWA.getTracker(_ith_project_id);
        var out = "IT:" + "\n";
		
        YWATracker.setDocumentName(this.getDocumentname());
        YWATracker.setDocumentGroup(this.getDocumentgroup());
        YWATracker.setAction(this.getActionId());
        YWATracker.setDomains(this.getDomains());
        var out = out + "DocumentName: " + this.getDocumentname() + "\n";
        var out = out + "DocumentGroup: " + this.getDocumentgroup() + "\n";
        var out = out + "Action: " + this.getActionId() + "\n";
        var out = out + "Domains: " + this.getDomains() + "\n";
        var out = out + "-------------------\n";

        if (typeof p.cfType == "undefined") {
            var cfStart = 1;
        } else {
            var cfStart = 7;
        }

		tmpHierarchy = this.getHierarchy();
        //tmpHierarchy = clone(this.getHierarchy());
        for (var i = cfStart; i <= cfStart + 4; i++)
            if (tmpHierarchy.length > 0) {
				out = out + "setCF #"+i+":" + tmpHierarchy[0] + "\n";
                YWATracker.setCF(i, this.formatVar(tmpHierarchy.shift()));
				//out = out + "setCF #"+i+":" + this.formatVar(tmpHierarchy[(i-1)]) + "\n";
        } else {
            YWATracker.setCF(i, "[sub-category not applicable]");	
			out = out + "setCF #"+i+": [sub-category not applicable] \n";
        }
        if (typeof this.getSKU() != "undefined") YWATracker.setSKU(getSKU());		
			out = out + "SKU:" + this.getSKU() + "\n";
        for (var i = 6, prefix = ""; i <= 20; i++)
            if (typeof this.getCustomVars()[i] != "undefined") {
            YWATracker.setCF(i, this.getCustomVar(i));
			out = out + "Custom #"+i+":" + this.getCustomVar(i) + "\n";
        }

        YWATracker.submit();
		
        if (typeof console == "object") console.info(out);
        if (typeof Debug == "object") Debug.writeln(out + "\n");
    },



    debug: function() {
        var out = "DOCUMENTNAME: " + DOCUMENTNAME + "\n";
        out = out + "DOCUMENTGROUP: " + DOCUMENTGROUP + "\n";
        if (typeof ACTION != "undefined")
            out = out + "ACTION: " + ACTION + "\n";
        else
            out = out + "NO ACTION";
        if (typeof _S_SKU != "undefined")
            out = out + "_S_SKU: " + _S_SKU + "\n";
        for (var j = 1; j <= 20; j++)
            try {
            eval("_s_cf" + ((j > 9) ? "" : "0") + j);
            out = out + "_s_cf" + ((j > 9) ? "" : "0") + j + ": " + eval("_s_cf" + ((j > 9) ? "" : "0") + j) + "\n";
        } catch (e) {
            // break;
        }
        if (typeof DOMAINS != "undefined")
            out = out + "DOMAINS: " + DOMAINS + "\n";
        if (typeof console == "object") console.info(out);
        if (typeof Debug == "object") Debug.writeln(out + "\n");
    },

    captureLead: function(p) { // {action:,sku:}		
        var YWATracker = YWA.getTracker(_ith_project_id);
        // var tracking_object = createITT();					
        if (typeof p.action != "undefined") {
            // tracking_object.ACTION = p.action;						
            YWATracker.setAction(p.action);
            // if (typeof console == "object") console.info("ACTION: " + p.action);
            // if (typeof Debug == "object") Debug.writeln("ACTION: " + p.action + "\n");                                             
        }
        if (typeof p.sku != "undefined") {
            // tracking_object._S_SKU = p.sku;
            YWATracker.setSKU(p.sku);
        }
        // tracking_object.submit_action();
        YWATracker.submit_action();
    },

    captureExitLink: function(p) {
        //var tracking_object = createITT();			
        //if (p.indexOf("http") != 0)
        //	p = document.URL.match(/^(.*)\/{1}/)[1] + ((p.substr(0,1)=="/")?"":"/") + p;
        //tracking_object.exitlink(p);
    },

    captureDownload: function(p) {
        //var tracking_object = createITT();
        //tracking_object.download(p);			
    },

    captureCfData: function(p) {  // {field:,data:,action:}
        //var tracking_object = createITT();
        var YWATracker = YWA.getTracker(_ith_project_id);
        //if (typeof p.action != "undefined") tracking_object.ACTION = p.action;
        if (typeof p.action != "undefined") YWATracker.setAction(p.action);
        //if (typeof p.field != "undefined") eval("tracking_object._s_cf" + p.field + "='" + p.data +"'");
        if (typeof p.field != "undefined") YWATracker.setCF(parseInt(p.field), p.data);
        //if (typeof p.action != "undefined") tracking_object.submit_action();			
        if (typeof p.action != "undefined") YWATracker.submit_action();
    },

    captureCfDataWithDoc: function(p) {  // {field:,data:,action:,documentname:,documentgroup:}
        // var tracking_object = createITT();		
        var YWATracker = YWA.getTracker(_ith_project_id);
        // if (typeof p.documentname != "undefined") tracking_object.DOCUMENTNAME = p.documentname;
        if (typeof p.documentname != "undefined") YWA.setDocumentName(p.documentname);
        //if (typeof p.documentgroup != "undefined") tracking_object.DOCUMENTGROUP = p.documentgroup;
        if (typeof p.documentgroup != "undefined") YWA.setDocumentGroup(p.documentgroup);
        //if (typeof p.action != "undefined") tracking_object.ACTION = p.action;			
        if (typeof p.action != "undefined") YWATracker.setAction(p.action);
        //if (typeof p.field != "undefined") eval("tracking_object._s_cf" + p.field + "='" + p.data +"'");
        if (typeof p.field != "undefined") YWATracker.setCF(parseInt(p.field), p.data);
        //if (typeof p.action != "undefined") tracking_object.submit_action();
        if (typeof p.action != "undefined") YWATracker.submit_action();
    },

    captureHomeLink: function(p) { //{link:,image:,action:}
        var YWATracker = YWA.getTracker(_ith_project_id);
        //var tracking_object = createITT();
        // tracking_object.ACTION = p.action;
        YWATracker.setAction(p.action);
        //tracking_object._s_cf14 = p.link;
        YWATracker.setCF(14, p.link);
        //tracking_object._s_cf15 = p.image;
        YWATracker.setCF(15, p.Image);
        //tracking_object.submit_action();
        YWATracker.submit_action();
    },

    formatVar: function(p) {
        if (p != null)
            return (html_entity_decode(p).substr(0, 75).replace(/[\'<>#&:;\?\-\*~`´\)\(=%!""\.]/ig, "_"));
    },

    trim: function(p) {
        return p.replace(/^\s+/, '').replace(/\s+$/, '');
    }

};

// Old class for compatibility

itHelper = {
	
		captureLead : function(p) { //{action:,sku:}
			ith.captureLead(p);						
		}
};

// Helper functions

function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
};

function html_entity_decode(str) {  
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");  
  return ta.value;
};

function clone(o) {
    return eval(uneval(o));
}

function trackFlashIT(page, action, cmdocumentgroup) {
	var out = "Page: " + page + ", Action: " + action;
	if (typeof console == "object") console.info(out);
	if (typeof Debug == "object") Debug.writeln(out+"\n");

	var YWATracker = YWA.getTracker(_ith_project_id);
	YWATracker.setDocumentName(page);
	YWATracker.setDocumentGroup(cmdocumentgroup);
	if (action != null) {
		YWATracker.setAction(action);
	};
	YWATracker.submit();
}

