function openwindow(url) {
msg=open(url,'NewWindow','width=505,height=450,toolbar=no,copyhistory=no,scrollbars=yes,resizable=no,directories=no,menubar=no,status=no,location=no');
}

function openAudioWindow(url) {
msg=open(url,'NewWindow','width=505,height=400,toolbar=no,copyhistory=no,scrollbars=yes,resizable=no,directories=no,menubar=no,status=no,location=no');
}

function MT_jumpMenu(linkElement)
{
	if(document.getElementById)
	{
		var le = document.getElementById(linkElement);
		if(le)
		{
			le.onchange = function(){
				document.location.href = this.value;
			}
		}
	}
}

function MT_handcursor(linkElement)
{
	linkElement.onmouseover = function(){
		this.style.cursor = 'pointer';
	}
	linkElement.onmouseout = function(){
		this.style.cursor = 'auto';
	}
}

function MT_attachPrintPage(linkElement)
{
	if(document.getElementById)
	{
		var le = document.getElementById(linkElement);
		if(le)
		{
			le.onclick = function(){window.print();};
			MT_handcursor(le);
		}
	}
}

function MT_attachEmailPage(linkElement)
{
	if(document.getElementById)
	{
		var le = document.getElementById(linkElement);
		if(le)
		{
			le.onclick = function(){MT_emailPage();};
			// Emulate link effect
			MT_handcursor(le);
		}
	}
}

function MT_emailPage()
{
	var mailit = "mailto:?subject=Link to Website of the Law Offices of Sideman %26 Bancroft";
	mailit += "&body=%0D%0A%0D%0ABelow is a link to the website of the Law Offices of Sideman %26 Bancroft LLP%0D%0A%0D%0A";
	mailit += "To view the page, visit: "+document.location.href;
	document.location.href = mailit;
}
function MT_attachSearch(searchButton, searchForm)
{
	/* Catch JS errors if searchForm not found*/
	if(document.getElementById &&  eval(document.forms[searchForm]))
	{
		var sb, sf;
		sb = document.getElementById(searchButton);
		//searchForm = document.getElementById(searchForm);
		searchForm = eval(document.forms[searchForm]);
		sf = searchForm.q;
		
		sf.onfocus = function() {
			sf.value = "";
		}
		sf.onblur = function() {
			if(sf.value == "")
				sf.value = "Search";
		}
		
		if(sb && sf && searchForm)
		{
			sb.onclick = function(){MT_submitSearch(sf, searchForm);};
			// Emulate link effect
			MT_handcursor(sb);
		}
	}
}

function MT_submitSearch(searchField, searchForm)
{
	var query = searchField.value;
	var noGo = "Search";
	
	/*Check for empty search field*/
	if(query.length > 0 && query != noGo)
	{	
		/* Check for a bunch of spaces */
		stringEmpty = true;
		for(i =0; i< query.length; i++)
		{
			if(query.substring(i, (i+1)) != ' '){
					stringEmpty = false;
			}
		}
		
		if(stringEmpty){
			searchField.value = noGo
		} else {
			searchForm.submit();
		}
	} else {
		searchField.value  = noGo;
	}
}

/*
rollOver function adapted from
www.onlinetools.org/articles/unobtrusivejavascript/chapter2.html
*/

function rollOver(obj, obj2){
	
	var src,ftype,newsrc;
	
	if (obj2!==undefined)
		src = obj2.src;
	else
		src = this.src;
		
	ftype = src.substring(src.lastIndexOf('.'), src.length);
	if(/_on/.test(src))
	{
		newsrc = src.replace('_on','');
	}else{
		newsrc = src.replace(ftype, '_on'+ftype);
	}
	this.src=newsrc;
	
	if (obj2!==undefined)
		obj2.src=newsrc;
	else
		this.src=newsrc;
}

function MT_menuImage(imageid)
 {
	if(document.getElementById && document.getElementsByTagName)
	{
		var menuImages;
		menuImages = document.getElementById(imageid);
		// Check menuImages here in case imageid doesn't exist on page
		if (menuImages)
		{
			menuImages = menuImages.getElementsByTagName('img');
			if(menuImages)
			{
				for (i = 0; i< menuImages.length; i++){
					// Preload image
					var img = new Image();
					img.src = menuImages[i].src;

					// Attach mouse over events to image
					menuImages[i].onmouseover=rollOver;
					menuImages[i].onmouseout=rollOver;
				}
			}
		}
	}
}   


// Create the new window
function openInNewWindow(e) {
    if (!e) var e = window.event;
        // Abort if a modifier key is pressed
        if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
            return true;
        }
        else {
            // Change "_blank" to something like "newWindow" to load all links in the same new window
            var newWindow = window.open(this.getAttribute('href'), 'newswindow', 'width=505,height=450,toolbar=no,copyhistory=no,scrollbars=yes,resizable=no,directories=no,menubar=no,status=no,location=no');
            if (newWindow) {
                if (newWindow.focus) {
                    newWindow.focus();
                    return false;
                }
            }
        return true;
    }
}

function getNewWindowLinks() {
    // Check that the browser is DOM compliant
    if (document.getElementById && document.createElement && document.appendChild) {
        // Change this to the text you want to use to alert the user that a new window will be opened
        // Find all links
        var link;
        var links = document.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {
            link = links[i];
            // Find all links with a class name of "targetblank"
            if (/\btargetblank\b/.exec(link.className)) {
                link.onclick = openInNewWindow;
            }
        }
    }
}    
window.onload= function()
{
	MT_menuImage('mainmenu');
	MT_attachSearch('submitSearch', 'searchForm');
	MT_attachEmailPage('emailPage');
	MT_attachPrintPage('printPage');
	MT_jumpMenu('attorneyDropDown');
	MT_jumpMenu('practiceDropDown');
	getNewWindowLinks();
}
