// works with new ga-tracker
// Frank Neitzel 2008-03-12
// 2008-12-11 Updated with e-commerce and smart link function.
// 2010-11-01 Keep cookie deleted. Man ønsker ikke de dumme url'er.
// 2011-11-29 Updated document types and track only path. (hostname now included in GA)

    // google analytics, track docs and external links
    // works with new ga-tracker
    // Frank Neitzel 2008-03-12
    // 2008-12-11 Updated with e-commerce and smart link function.

    if (document.getElementsByTagName) {
        var hrefs = document.getElementsByTagName("a");

        for (var i = 0; i < hrefs.length; i++) {
            //if no onclick on the object
            if (!hrefs[i].onclick) {
                
                // check if link on the same website
                if (hrefs[i].hostname == location.host) {
                    
                    var path = hrefs[i].pathname;

                    // check if links to documents ending with:
                    if (path.match(/\.(exe|pdf|rar|zip|doc|xls|ppt|docx|xlsx|pptx)$/)) {
                        hrefs[i].onclick = function() {
                        //test
                        //alert("this is a document:" + "/documents/" + this.href.substring(7) + "/path/" + this.path);
                        //alert("/path/ " + this.pathname);
                        pageTracker._trackPageview(this.pathname);
                        }
                    }

                    // check if documents are opened with corporate/openpdf.aspx
                    if (path.toLowerCase().match("corporate/openpdf.aspx")) {

                        hrefs[i].onclick = function() {

                            // example reference: 
                            // <a href="corporate/openpdf.aspx?pdffile=/corporate/pdf/english/la22_eng.pdf">Data sheet 2</a>     

                            //document.location.pathname ikke nødvendig - er altid /corporate/openpdf.aspx i dette tilfælde.
                         
                            //this reg-ex searched for the part "file=" and any character after that in the example reference above.

                            var url_split = this.href.match("file=(.+)");

                            // The first element of the array (in url_split) is the matching string, 
                            // Any remaining elements in the array are the parenthesized subexpressions of the regular expression. 
                            // Thus, if match( ) returns an array a, a[0] contains the complete match, 
                            // a[1] contains the substring that matched the first parenthesized expression (.+), and so on.

                            //url_split[0]          contains: file=/corporate/pdf/english/la22_eng.pdf
                            var wt_uri = url_split[1] //contains: /corporate/pdf/english/la22_eng.pdf

                            orderNum = getOrderID();

                            //alert(orderNum)

                            pageTracker._addTrans(
    					        orderNum,   //order ID - required
						        "",         //affiliation or store name
						        10,         //total - required
						        "",         //tax
						        "",         //shipping
						        "",         //city
						        "",         //state or province
						        ""          //country
				                );

                            //alert(wt_uri)

                            pageTracker._addItem(
						        orderNum, 				// order ID - required
						        wt_uri, 			        // SKU (stock keeping unit), for example: page_of_interest1.pdf
						        "Document download link", // Product name (alternate whitepaper, document sheet etc)
						        "Download", 				// category or variation                    
						        "1", 					// unit price - required
						        "1"							// quantity - required
					            );

                            pageTracker._trackTrans();
							
							//test
							//alert("this is a openpdf.aspx: " + wt_uri);
							//alert("this is a openpdf.aspx, with this.hostname and this.search: " + this.hostname + this.search);
														
                            //normal stat in a virtual folder.
                            //pageTracker._trackPageview("/PDF/" +  wt_uri);
                            pageTracker._trackPageview("/PDF/" + this.hostname + this.search);
                        }
                    }

                } else {
                // it is not an internal link, but external
                        //track all external links
                        hrefs[i].onclick = function() {
                            //test
                            //alert("this is an external link - not a linak link");
                            pageTracker._trackPageview('/external/' + this.hostname + this.search);
                    }
                }
            }
        }
    }

    // generate a random order id between 1 and 10000 per day, which should be sufficient for most sites.
    function getOrderID() {
        var randomnumber = Math.floor(Math.random() * 10000);
        var currentTime = new Date();
        var month = currentTime.getMonth() + 1;
        var timeStamp = "" + currentTime.getFullYear() + month + currentTime.getDate() + "-" + currentTime.getHours()
                    + currentTime.getMinutes() + currentTime.getSeconds() + "-" + randomnumber;
        return (timeStamp);
    }





