//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, docType);
		
	/*
		 Manipulating docType String, this variable contains the value needed
		 for WT.z_DOI_Tab. docType is actually the name of the tab.
		 Need to remove spaces and replace any 	<br> or </br>
		 
		 DOI_Tab_info.indexOf("<br>") != -1 ---> This signal if the String being
		 manipulated contains <br> on it.
	*/
	var DOI_Tab_info = docType;
	if(DOI_Tab_info.indexOf("<br>") != -1){
		//Remove the <br> and replace with " "
		DOI_Tab_info = DOI_Tab_info.replace("<br>", " ");
	}
	
    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,
		'WT.z_DOI_Tab', DOI_Tab_info);

	_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_playlist_name = null;
	_tag.WT.z_player_name = null;
	_tag.WT.z_event_type = null;
	_tag.WT.z_en_title = null;
	_tag.WT.z_Tab_Cat = null;
	_tag.WT.z_DOI_Tab = null;
	_tag.WT.z_ExLink_Type = null;
	_tag.WT.z_player_id = null;
	_tag.WT.z_playlist_id = null;
	_tag.WT.z_video_id = null;
}

function collectLinks(tabName, docType) {
    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;
                    //Document type property
                    anchors[anchorsIndex].dt = docType;
                }
            	//IE browser
                else if (anchors[anchorsIndex].attachEvent) {
                    anchors[anchorsIndex].attachEvent("onclick", getCategories);
                    //category property
                    anchors[anchorsIndex].cat = category;
                    //Document type property
                    anchors[anchorsIndex].dt = docType;
                }
            }
            
            //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;
            		//Document type property
            		images[imagesIndex].dt = docType;
            	}
            	//IE browser
            	else if (images[imagesIndex].attachEvent) {
            		images[imagesIndex].attachEvent("onclick", getCategories);
            		//category property
            		images[imagesIndex].cat = category;
            		//Document type property
            		images[imagesIndex].dt = docType;
            	}
            }
        }
    }
}

function getCategories(evt) {

    var e_out;
	var ie_var = "srcElement";
	var moz_var = "currentTarget";

    var prop_var = "cat";
    var dt_out = "";
    var dt_var = "dt";
          
	// "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];	
	if(evt[ie_var] && !evt[ie_var][prop_var]){
		e_out = evt[ie_var]["parentElement"][prop_var];
	}
    _tag.WT.z_Tab_Cat = e_out;
    //
    evt[moz_var] ? dt_out = evt[moz_var][dt_var] : dt_out = evt[ie_var][dt_var];
	if(evt[ie_var] && !evt[ie_var][dt_var]){
		dt_out = evt[ie_var]["parentElement"][dt_var];
	}
    //
    _tag.WT.z_DOI_Tab = dt_out;
    _tag.WT.cg_n = "Document of Interest";
    _tag.WT.z_ExLink_Type = null;
}

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 = "currentTarget";
    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];
	if(evt[ie_var] && !evt[ie_var][prop_var]){
		e_out = evt[ie_var]["parentElement"][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) {
	
	
	if(getSelectedTab() == "Video"){
		_tag.WT.z_playlist_name = null;
		_tag.WT.z_player_name = null;
   		_tag.WT.z_event_type = null;
   		_tag.WT.z_ExLink_Type = null;
	}
	
	

	_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";
	_tag.WT.z_ExLink_Type = null;
}

function getSelectedTab(){

	var tabs = TabbedPanels1.getTabs();
	for (var i = 0; i < tabs.length; i++){
		if (tabs[i]){

			var classname = String(tabs[i].className);
			if(classname.indexOf(TabbedPanels1.tabSelectedClass) >=0){
				return tabs[i].innerHTML;
			}

		}

	}
}
