//a function that will work on page load and will not crash the preview in teamsite
function addEvent(obj, evType, fn) {
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true;
	} 
	else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}	 
	else { 
		return false; 
	} 
}

//two functions to kick off on page load
addEvent(window, 'load', descriptionLinks);
addEvent(window, 'load', supplierLinks);

function tabTrack(title, id, tabName, shortname, docType) {
    //make certain variables null to reduce errors in webtrends reports
	resetValues();
    collectLinks(tabName);

    dcsMultiTrack(
		//dcsuri needs to remain the same, assigned to itself
		'DCS.dcsuri', _tag.DCS.dcsuri,
        'WT.ti', title,
        'WT.z_supplier_id', id,
        'WT.cg_n', 'Supplier Portal - ' + shortname,
        'WT.z_page_type', 'SP',
        'WT.z_page_sub_type', shortname);


	_tag.WT.cg_n = "Document of Interest";
	_tag.WT.z_page_type = "SP";
	_tag.WT.z_page_sub_type = "DOI";
	_tag.WT.z_supplier_id = id;
	_tag.WT.z_DOI_Tab = docType;
}

function resetValues() {
	_tag.WT.z_en_title = null;
	_tag.WT.z_Tab_Cat = null;
	_tag.WT.z_DOI_Tab = null;
	_tag.WT.z_ExLink_Type = null;
}

function collectLinks(tabName) {
    var element = document.getElementById(tabName);
    var category = "";

    //collect all of the tables that are in the tab
    var tables = element.getElementsByTagName("table");
    for (var tablesIndex = 0; tablesIndex < tables.length; tablesIndex++) {

        //only process the tables that have a category
    	if (tables[tablesIndex].getAttribute("cat") != null) {
            category = tables[tablesIndex].getAttribute("cat");

          //collect all of the attributes in the table and set an event and a category property
            var anchors = tables[tablesIndex].getElementsByTagName("a");
            for (var anchorsIndex = 0; anchorsIndex < anchors.length; anchorsIndex++) {

                //gecko based browser listener 
            	if (anchors[anchorsIndex].addEventListener) {
                    anchors[anchorsIndex].addEventListener("mousedown", getCategories, false);
                    //category property
                    anchors[anchorsIndex].cat = category;
                }
            	//IE browser
                else if (anchors[anchorsIndex].attachEvent) {
                    anchors[anchorsIndex].attachEvent("onclick", getCategories);
                    //category property
                    anchors[anchorsIndex].cat = category;
                }
            }
            
            //collect all of the images in the table and set an event and a category property
            var images = tables[tablesIndex].getElementsByTagName("img");
            for (var imagesIndex = 0; imagesIndex < images.length; imagesIndex++) {
            	
            	//gecko based browser listener
            	if (images[imagesIndex].addEventListener) {
            		images[imagesIndex].addEventListener("mousedown", getCategories, false);
            		//category property
            		images[imagesIndex].cat = category;
            	}
            	//IE browser
            	else if (images[imagesIndex].attachEvent) {
            		images[imagesIndex].attachEvent("onclick", getCategories);
            		//category property
            		images[imagesIndex].cat = category;
            	}
            }
        }
    }
}

function getCategories(evt) {
    var e_out;
	var ie_var = "srcElement";
	var moz_var = "target";

    var prop_var = "cat";
          
	// "target" for Mozilla, Netscape, Firefox et al. ; "srcElement" for IE
	evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    _tag.WT.z_Tab_Cat = e_out;
}

function supplierLinks() {
	var rohsLink = document.getElementById("RoHS Link");
	if (rohsLink != null) {
		supplierLinkEvent(rohsLink);
	}
	
	var certLink = document.getElementById("RoHS Certificate Link");
	if (certLink != null) {
		supplierLinkEvent(certLink);
	}
	
	var manufactureLink = document.getElementById("Manufacturers Link");
	if (manufactureLink != null) {
		supplierLinkEvent(manufactureLink);
	}
}

function supplierLinkEvent(link) {
	//gecko based browser listener 
	if (link.addEventListener) {
        link.addEventListener("mousedown", getSupplierLinkTag, false);
    }
	//IE browser
    else if (link.attachEvent) {
        link.attachEvent("onclick", getSupplierLinkTag);
    }
}

function getSupplierLinkTag(evt) {
	resetValues();
	
	var e_out;
	var ie_var = "srcElement";
	var moz_var = "target";
    var prop_var = "id";
          
	// "target" for Mozilla, Netscape, Firefox et al. ; "srcElement" for IE
	evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    _tag.WT.cg_n = e_out;  //e_out is storing the id from the event (element) that triggered the event
    
    _tag.WT.z_page_sub_type = "EL";
    
    //if the id is not Manufactures Link.  e_out currently contains the id of the element
    if (e_out != "Manufacturers Link") {
    	_tag.WT.z_ExLink_Type = e_out;
    }
    else if (e_out == "Manufacturers Link") {
    		_tag.WT.z_ExLink_Type = "Homepage";
    }
}

function descriptionLinks() {
	var link = "";
	
	//var element = document.getElementById("tableID3");
	var element = document.getElementById("supplierDescription");
	var anchors = element.getElementsByTagName("a");
	
	for (var anchorsIndex = 0; anchorsIndex < anchors.length; anchorsIndex++) {
		//Checking for the span tags that operate the javascript for the more/less links and not using them.
		if (!(anchors[anchorsIndex].innerHTML.match(/^(?:<span.*>|<SPAN.*>)$/))) {
			//gecko based browser listener 
        	if (anchors[anchorsIndex].addEventListener) {
                anchors[anchorsIndex].addEventListener("mousedown", getDescriptionTag, false);
            }
        	//IE browser
            else if (anchors[anchorsIndex].attachEvent) {
                anchors[anchorsIndex].attachEvent("onclick", getDescriptionTag);
            }
		}
	}
}

function getDescriptionTag(evt) {
	_tag.WT.cg_n = "Document of Interest";
	_tag.WT.z_page_sub_type = "DOI";
	_tag.WT.z_DOI_Tab = "Company Description";
	_tag.WT.z_Tab_Cat = "No Category";
}