// ComfortMarketing Inc.
// $Id: es_tag_1.js 1.0.0.6 $
// 2011.03.23


//===================================================================================
// functions
//===================================================================================

estracker_param.es_ini_ = function(ck){
    var w, i;
    if (document.cookie) {
        w = document.cookie;
        while (w.length > 7) {
            if (w.substring(0, 7) == "aqusr=") {
                if ((i = w.indexOf(";")) < 0) i = w.length;
                estracker_param.aqusr = w.substring(7, i);
                break;
            }
            if ((i = w.indexOf(";")) < 0) break;
            for (i += 1; i < w.length; ++i) if (w.charAt(i) != ' ') break;
            w = w.substring(i, w.length);
        }
    }
    if (ck) {
        estracker_param.aqusr = estracker_param.aqusr || "AWS1SV01.420258BB021DCD9B16";
        w = new Date();
        w.setFullYear(w.getFullYear() + 1);
        document.cookie = "aqusr=" + estracker_param.aqusr + "; path=/; expires=" + w;
        estracker_param.es_ini_(false);
    }
}
estracker_param.es_ext_ = function(){
    if( estracker_param.header ){
        estracker_param.set_header_();
    }
    if( estracker_param.products && estracker_param.products != ""){
        estracker_param.set_products_();
    }
    var arr = [
        "c01", "c02", "c03", "c04", "c05", "c06", "c07", "c08", "c09", "c10", 
        "e01", "e02", "e03", "e04", "e05", 
        "sword", 
        "hstatus", "hcustid", "htranid", "hamount", "hcount", 
        "pids", "pcounts", "ptagprices", "ptagname" , "ptagctg1", "ptagctg2", "ptagctg3", "ptagctg4", "ptagctg5"
    ];
    estracker_param.ext = "";
    for (var o in arr){
        var v = arr[o];
        if(estracker_param[v]!=undefined){
            if(v == "sword"){
                estracker_param[v] = "SWORD_START" + estracker_param[v] + "SWORD_END";
            }
            if(v.match(/^p/) && estracker_param[v].match(/^,/)){
                estracker_param[v] = estracker_param[v].substr(1);
            }
            estracker_param.ext += "\n" + v + "=" + encodeURIComponent(estracker_param[v]);
            if(v=="pids"){
                if (estracker_param["hstatus"] == 100) {
                    estracker_param.ext += "\nptids" + "=" + encodeURIComponent(estracker_param[v].replace(/PIDS_/g, "PTIDS_"));
                } else {
                    var resArray = estracker_param[v].split(",");
                    var ret = "";
                    ret += "PTIDS_START,";
                    for( var i=1 ; i<(resArray.length-1) ; i++ ) {
                        ret += ",";
                    }
                    ret += "PTIDS_END";
                    estracker_param.ext += "\nptids" + "=" + encodeURIComponent(ret);
                }
            }
        }
    }
    if (estracker_param.ext.replace(/\n[A-Za-z0-9_]*=/g, "") == '') estracker_param.ext = null;
}
estracker_param.set_header_ = function(){
    estracker_param.hstatus = estracker_param.get_checked_param('hstatus', estracker_param.header[0]);
    for (i=0; i<estracker_param.length ;i++) {
        alert(estracker_param.header[i]);
    }
    if(estracker_param.header[0] == 100){
        estracker_param.hcustid = estracker_param.get_checked_param('hcustid', estracker_param.header[1]);
        estracker_param.htranid = estracker_param.get_checked_param('htranid', estracker_param.header[2]);
        estracker_param.hamount = estracker_param.get_checked_param('hamount', estracker_param.header[3]);
        estracker_param.hcount = estracker_param.get_checked_param('hcount', estracker_param.header[4]);
    }
}

estracker_param.set_products_ = function(){
    var base_unshift_array = [
        "PIDS_START", "0", "0", "PTAGNAME_START",
        "PTAGCTG1_START", "PTAGCTG2_START", "PTAGCTG3_START", "PTAGCTG4_START", "PTAGCTG5_START"
    ];
    var base_push_array = [
        "PIDS_END", "0", "0", "PTAGNAME_END", "PTAGCTG1_END",
        "PTAGCTG2_END", "PTAGCTG3_END", "PTAGCTG4_END", "PTAGCTG5_END"
    ];

    var element_length = 0;
    for(var i=0; i<estracker_param.products.length; i++){
        if (element_length < estracker_param.products[i].length) {
            element_length = estracker_param.products[i].length;
        }
    }
    if (element_length < 3) element_length = 3;

    var unshift_array = base_unshift_array.slice(0, element_length);
    var push_array = base_push_array.slice(0, element_length);

    estracker_param.products.unshift( unshift_array );
    estracker_param.products.push( push_array );

    var pKeys_default = new Array();
    pKeys_default[0] = 'pids';
    pKeys_default[1] = 'pcounts';
    pKeys_default[2] = 'ptagprices';

    var pKeys = new Array();
    pKeys[0] = 'pids';
    pKeys[1] = 'pcounts';
    pKeys[2] = 'ptagprices';
    pKeys[3] = 'ptagname';
    pKeys[4] = 'ptagctg1';
    pKeys[5] = 'ptagctg2';
    pKeys[6] = 'ptagctg3';
    pKeys[7] = 'ptagctg4';
    pKeys[8] = 'ptagctg5';

    for(var i=0; i<estracker_param.products.length; i++){
        var p = estracker_param.products[i];
        if(p == undefined) continue;

        var pObj_default = new Object();
        for (var j=0; j<pKeys_default.length; j++) {
            pObj_default[pKeys_default[j]] = p[j];
        }
        var pObj = new Object();
        for (var j=0; j<element_length; j++) {
            pObj[pKeys[j]] = p[j];
        }

        if (estracker_param.prodinfoto == "tag") {
            estracker_param.set_product_(pObj);
        }
        else if (estracker_param.prodinfoto == "api")  {
            estracker_param.set_product_(pObj_default);

            if (i != 0 && i != (estracker_param.products.length - 1)) {
                estracker_param.set_meta_product(pObj);
            }
        }
        else {
            estracker_param.set_product_(pObj_default);
        }
    }
}
estracker_param.set_product_ = function(Obj){
    for (var key in Obj) {
        var value = Obj[key];

        var checked_value = estracker_param.product_param_check(key, value);
        estracker_param[key] += "," + checked_value;
    }
}
estracker_param.set_meta_product = function(Obj){
    var mObj = new Object;

    for (var key in Obj) {
        var value = Obj[key];

        switch (key) {
            case "pids":
            case "pcounts":
            case "ptagprices":
                mObj[key] = estracker_param.product_param_check(key, value);
                break;
            default:
                mObj[key] = value;
                break;
        }
    }

    var pit_param  = ""
    pit_param += "aqid=" + estracker_param.esid;
    pit_param += "&pid=" + mObj['pids'];
    if (mObj['ptagname']) {
        pit_param += "&pname=" + encodeURIComponent(mObj['ptagname']);
    }
    if (mObj['ptagctg1']) {
        pit_param += "&pctg1=" + encodeURIComponent(mObj['ptagctg1']);
    }
    if (mObj['ptagctg2']) {
        pit_param += "&pctg2=" + encodeURIComponent(mObj['ptagctg2']);
    }
    if (mObj['ptagctg3']) {
        pit_param += "&pctg3=" + encodeURIComponent(mObj['ptagctg3']);
    }
    if (mObj['ptagctg4']) {
        pit_param += "&pctg4=" + encodeURIComponent(mObj['ptagctg4']);
    }
    if (mObj['ptagctg5']) {
        pit_param += "&pctg5=" + encodeURIComponent(mObj['ptagctg5']);
    }
    var url = "//" + api_srv_url + pit_param;
    img = new Image;
    img.src = url;
}

estracker_param.product_param_check = function(key, value) {
    if (estracker_param[key] == undefined) estracker_param[key] = "";
    if (isNaN(value)) {
        if(value) var value = value.replace(/[,'"\n\\]/g, "");

        if (isNaN(value)) {
            checked_value = estracker_param.get_checked_param(key, value);
        } else {
            checked_value = estracker_param.get_checked_param(key, value);
        }
    } else {
        checked_value = estracker_param.get_checked_param(key, value);
    }
    return checked_value;
}

estracker_param.get_checked_param = function(key, value) {
    switch(key){
        case "pids":
        if(!isNaN(value)) value = String(value);
        if(!value) value = "";
            break;
        case "hstatus":
        case "hcustid":
        case "htranid":
            if (isNaN(value)) {
                if(value) var value = value.replace(/[,'"\n\\]/g, "");
            }
            break;
        case "ptagname":
        case "ptagctg1":
        case "ptagctg2":
        case "ptagctg3":
        case "ptagctg4":
        case "ptagctg5":
            if (!value) value = "";
            break;
        case "hcount":
        case "pcounts":
        if(!value || isNaN(value)){
            value = "1";
        }
            break;
        case "hamount":
        case "ptagprices":
        if(!value || isNaN(value)){
            value = "0";
        }
            break;
        default:
        break;
    }

    return value;
}

estracker_param.es_go_ = function(agv, mod, adv){
    var ctl, i, j, p, s, r;
    var esid = estracker_param.esid;

    if( ( ! estracker_param.aqusr ) || ( ! document.URL ) ){
        return;
    }
    
    ctl = ",,";
    if (window.esctl) ctl += window.esctl + ",";
    s = document.location.protocol + "//" + tag_srv_url;
    if (window.esctl_allow_cache || ctl.indexOf(",allow_cache,") >= 0) {
        s += "_";
    } else {
        i = new Date();
        s += i.getTime();
    }

    if (window.esctl_user_mnt || ctl.indexOf(",user_mnt,") >= 0) {
        s += "h";
    }

    if (mod == 30) {
        s += "v";
    }
	s += "/" + esid + "?";
    s += ( estracker_param.aqusr ) ? ( "1=" + estracker_param.aqusr ) : "";
    
    i = 0;
    if(mod == 5 && i < agv.length) { p = agv[i]; i++; }
    else p = document.URL;

    // delete query string
    if ( estracker_param.trim_query  && (j = p.indexOf("?")) > 0 ){
        p = p.substring(0, j);
    } else if ((window.aqctl_trim_query || ctl.indexOf(",trim_query,") >= 0) && (j = p.indexOf("?")) > 0) {
        p = p.substring(0, j);
    }

    if (mod == 10 && i < agv.length) { r = agv[i]; i++;
        if ((j = p.indexOf("?")) < 0) p = p + "?" + r;
        else if (j < p.length) p = p.substring(0, j+1) + r + "&" + p.substring(j+1);
    }
    else if (mod == 11 && i < agv.length) { r = agv[i]; i++;
        if ((j = p.indexOf("?")) < 0) p += "?"; else if (j < p.length - 1) p += "&";
        p += r;
    }
    s += "&2=" + escape(p);
    if (mod == 20 && i < agv.length) { s += "&3=" + escape(agv[i]); i++; }
    r = (mod <= 14)? document.referrer : p;
    if (r) s += "&4=" + escape(r);
    s_no_ext = s;
    while (i < agv.length) { s += "&101=p=" + escape(agv[i]); i++; }
    var isMSIE = /*@cc_on!@*/false;
    if(isMSIE) {
        if(s.length > estracker_param.LimitLength) {
            if(s.search("%0[aA]pids%3[dD]") != -1) {
                s = s.substring(0, s.search("%0[aA]pids%3[dD]"));
            }
            if (s.length > estracker_param.LimitLength) s = s_no_ext;
        }
        s = s.substring(0, estracker_param.LimitLength);
    }
    i = new Image(1, 1);
    i.onload = function es_cb_() { return; };
    i.src = s;
}

estracker_param.estracker = function () {
    if (estracker_param.stop) return;
    if (!estracker_param.ext) estracker_param.es_ext_();
    if (estracker_param.url) {
        if (estracker_param.ext) estracker_param.es_go_([estracker_param.url, estracker_param.ext], 5);
        else estracker_param.es_go_([estracker_param.url], 5, estracker_param.adv);
    }
    else if (estracker_param.ins) {
        if (estracker_param.ext) estracker_param.es_go_([estracker_param.ins, estracker_param.ext], 10);
        else estracker_param.es_go_([estracker_param.ins], 10, estracker_param.adv);
    }
    else if (estracker_param.cat) {
        if (estracker_param.ext) estracker_param.es_go_([estracker_param.cat, estracker_param.ext], 11);
        else estracker_param.es_go_([estracker_param.cat], 11, estracker_param.adv);
    }
    else if (estracker_param.nopv) {
        if (estracker_param.ext) estracker_param.es_go_([estracker_param.ext], 30);
        else estracker_param.es_go_([], 30, estracker_param.adv);
    }
    else {
        if (estracker_param.ext) estracker_param.es_go_([estracker_param.ext], 0);
        else estracker_param.es_go_([], 0, estracker_param.adv);
    }
}

estracker_param.estracker_event = function() {
    if (estracker_param.stop) return;
    estracker_param.es_go_(arguments, 20);
}

estracker_param.es_tag = function(){
    estracker_param.es_go_(arguments, 0);
}

estracker_param.es_tag_cat = function(){
    estracker_param.es_go_(arguments, 11);
}

estracker_param.es_tag_file = function(){
    estracker_param.es_go_(arguments, 20);
}

estracker_param.es_tag_value = function(){
    estracker_param.es_go_(arguments, 30);
}

//===================================================================================
// main fuction
//===================================================================================

// init
var estracker_param = estracker_param || new Object();
estracker_param.aqusr = "";
estracker_param.LimitLength = 2000;


// server name
var tag_srv_url = estracker_param.tag_srv_url || "t1.event-storage.com/cgi-bin/asp/tagx/";
var api_srv_url = estracker_param.api_srv_url || "u1.event-storage.com/meta/prod.php?";
var dts_srv_url = estracker_param.dts_srv_url || "u1.event-storage.com/js.php?cid=";

// Comfy beacon
if( tag_srv_url ){
    estracker_param.es_ini_(true);
    estracker_param.estracker();
}

// DTS beacon
if( dts_srv_url != false ){
    var dts_node = document.createElement('script');
    dts_node.type = 'text/javascript';
    dts_node.async = true;
    dts_node.src = document.location.protocol + "//" + dts_srv_url + estracker_param.esid;
    var dts_elm = document.getElementsByTagName('script')[0];
    dts_elm.parentNode.insertBefore(dts_node, dts_elm);
}
    

