window.onload = PageLoad;
var noMarginWidth = 875;
var normalMarginWidth = 1050;

function PageLoad() {
	resizeContent();
	if (!document.getElementsByTagName) {
		return;
	}
	fixImages();
	externalLinks();
}

function EriezSearch(searchControl, searchOptionsControl) {
	var searchText = document.getElementById(searchControl).value;
	var searchOption = 'all';
	for(var i = 0; i < 2; i++){
		var element = document.getElementById(searchOptionsControl + '_' + i.toString());
		if(element && element.checked){
			searchOption = element.value;
		}
	}

	searchText = escape(searchText);
	searchText = searchText.replace("+", "%2B");
	searchText = searchText.replace("/", "%2F");

	
	
	window.location = '/Search/?searchText=' + searchText + '&searchOption=' + searchOption; 
	return false;
}

function externalLinks() {
    var domainsControl = document.getElementById("ctl00_ValidDomainNames");
    if (domainsControl != null) {
        var anchors = document.getElementsByTagName("a");

        var CurrentHost = window.location.toString().substring(7, window.location.toString().indexOf('/', 8));
        var domains = domainsControl.value + "," + CurrentHost;

        for (var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];

            var hrefAtt = anchor.getAttribute("href")
            if (hrefAtt) {
                var href = hrefAtt.toString();
                if (href) {
                	if (!(
                		href.indexOf('/') == 0
                		|| href.indexOf('#') == 0
                		|| domains.match(href.substring(7, href.indexOf('/', 8)))
                		|| domains.match(href.substring(8, href.indexOf('/', 8)))
                		|| (!href.substring(0, 4).match('http') && href.indexOf('/') != 0)
                		)) {
                        anchor.target = "_blank";
                    }
                }
            }
        }
    }
}

      function fixImages() {
      	var images = document.getElementsByTagName('img');
      	if (images) {
      		for (var i = 0; i < images.length; i++) {
      			var width = images[i].width;
      			if (!width) {
      				width = images[i].style.width;
      			}

      			var height = images[i].height;
      			if (!height) {
      				height = images[i].style.height;
      			}
      			if (width) {
      				Resize(height, width, images[i]);
      			}
      		}
      	}

      }

      function Resize(height, width, image) {
      	var aspectRatio = new Number();
      	var imgHeight = parseFloat(height);
      	var imgWidth = parseFloat(width);

      	if (imgWidth < 551) {
      		return;
      	}
      	else {
      		if (imgWidth >= imgHeight) {
      			aspectRatio = imgWidth / imgHeight;
      			image.width = 550;
      			image.height = 550 / aspectRatio;
      		}
      		else {
      			aspectRatio = imgHeight / imgWidth;
      			image.height = 550;
      			image.width = 550 / aspectRatio;
      		}
      	}
      }

	function resizeContent() {
	    window.onresize = resizeContent;
	    var width = document.body.scrollWidth;

	    if (width < noMarginWidth)
	        document.body.className = "NoMargins";
	    else if (width > normalMarginWidth)
	        document.body.className = "NormalMargins";
	    else
	        document.body.className = "HalfMargins";
	}
	
