// JavaScript Document

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
    var o = parseUri.options;
    var m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
    var uri = {};
    var i = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
};

parseUri.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
        name:   "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};

window.onload=fixFrames;	
function getLocQueryParam(url) {
    var urlAttribute = "loc=";
    var i = url.indexOf(urlAttribute);
    var allowedServer = "orders.summit.com";
    var loc = null;
    if(i > 0 && i + urlAttribute.length < url.length) {
        var isaUrl = url.substring(i + urlAttribute.length);
        var parsedUri = parseUri(isaUrl);
        if (parsedUri["host"] == allowedServer) {
            loc = isaUrl;
        }
    }
    return loc;
}

function fixFrames() {
    var summitUrl = self.location.href;
    var elementId = "contentProducts";

    var isaUrl = getLocQueryParam(summitUrl);
    if (isaUrl && document.getElementById(elementId)!=null) document.getElementById(elementId).src = isaUrl;
}


//Changes the CSS for ISA pages displayed on Summit
	
	function doContentProductsFrameLoadEvent() {
		// To relax browser security, the domain for Summit is set.
		document.domain = "summit.com";
		// A handle to the isa document is obtained.
		var contextDocument = window.frames['contentProducts'].document;
		var bodyTags = contextDocument.getElementsByTagName("body");
		var i;
		// The background color of the isa document is changed.
		if (bodyTags) {
			for(i=0;i<bodyTags.length;i++) {
				bodyTags[i].style.backgroundColor = '#FFFFFF';
			}
		}
		// In some circumstances, a text file is dynamically
		// inserted by ISA. Therefore it may be necessary to
		// change style attributes within that inserted file.
		var htmlTags = contextDocument.getElementsByTagName("html");
		if (htmlTags) {
			for(i=0;i<htmlTags.length;i++) {
				htmlTags[i].style.backgroundColor = '#FFFFFF';
			}
		}
		
		// ISA display a pagination ribbon at the top to allow navigation
		// to the next list of items.
		var paginationTags = contextDocument.getElementsByTagName("tr");
		if (paginationTags) {
			for(i=0;i<paginationTags.length;i++) {
					if (paginationTags[i].className == 'pagination')
						paginationTags[i].style.backgroundColor = '#F0F0F0';
						paginationTags[i].style.padding = '3px';
			}			
		}
		paginationTags = contextDocument.getElementsByTagName("span");
		if (paginationTags) {
			for(i=0;i<paginationTags.length;i++) {
					if (paginationTags[i].className == 'pagination')
						paginationTags[i].style.backgroundColor = '#F0F0F0';
						paginationTags[i].style.padding = '3px';
			}			
		}
		
		// In some circumstances, a "Only registered users may order online" box appears 
		var divTags = contextDocument.getElementsByTagName("div");
		if (divTags) {
			for(i=0;i<divTags.length;i++) {
					if (divTags[i].className == 'greyhilite')
						divTags[i].style.backgroundColor = '#F0F0F0';
			}						
		}

	}
	
