/**
 *	Mitsubishi Distributors
 *	-------------------------
 */

if(!window.name) {
	window.name = '_opener';
}

window.onload = function() {
	initNavigationBar();
}

/**
 *	Forms
 *	--------------------------
 */

var FormExpressions = {
	email:'^[0-9a-z._-]*[@][0-9a-z._-]*[.][a-z]{2,4}$'
}

function submit(formName) {
	var frm = document.forms[formName];
	var elements = frm.elements, l = elements.length, name;
	for(var i=0; i<l; i++) {
		name = elements[i].name;
		if(!name || !FormExpressions[name]) continue;
		if(!validateField(FormExpressions[name], elements[i].value)) {
			alert('Please enter a valid E-mail address.');
			return;
		}
	}	frm.submit();
}

function submitSearch(formName) {
	var frm = document.forms[formName];
	var elements = frm.elements, l = elements.length;
	for(var i=0; i<l; i++) {
		if(elements[i].value == '') {
			alert('Bitte geben Sie ein Suchwort ein, um ein Suchergebnis zu erhalten.');
			return;
		}
	}	
	frm.submit();
}

function submitViaEnter(evt, formName) {
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var form = target.form;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {
		submitSearch(formName);
		return false;
    }
    return true;
}

function validateField(reg, value) {
	return (new RegExp(reg)).test(value);
}

/**
 *	Mitsubishi navigation
 *	-------------------------
 */

function initNavigationBar() {
	if(!document.getElementsByTagName) return;
	var bOpera = navigator.userAgent.indexOf("Opera") > -1;
	var flash = document.getElementsByTagName('object');
	
	function setHoverEvent(li, ul) {
		var css = ul.style;
		li.onmouseover = function() { 
			css.visibility = 'visible';
			if(bOpera) toggleSplashFlash(flash, false);
		}

		li.onmouseout = function() { 
			css.visibility = 'hidden';	
			if(bOpera) toggleSplashFlash(flash, true);
		}

		if((li.offsetLeft + ul.offsetWidth) > 664) {
			css.left = 'auto';
			css.right = '0px';
		}
	}

	var nav = document.getElementById('navigation');
	if(!nav) return;
	var el, li, uls = nav.getElementsByTagName('ul');
	for(var i=0; i<uls.length; i++) {
		el = uls[i].parentNode;
		if(el.nodeName == 'LI') {
			setHoverEvent(el, uls[i]);
		}
	}
}
	function toggleSplashFlash(flash, toggle) {
		for(var i=0; i<flash.length; i++) {
			flash.item(i).style.display = toggle? 'block' : 'none';
		}
	}

/**
 *	Email obfuscator
 *	--------------------------
 */

function email(name, domain, ext, label) {
	var link = "<a href='mailto:"+name+"&#64;"+domain+"&#46;"+ext+"'>" + label + "</a>";
	document.write(link);
}

/**
 *	Popups
 *	-------------------------
 */

function popupImage(path) {
	window.open('/imageviewer.jsp?image=' + path, 
		'imageViewer', 'width=320,height=240,left=100,top=100');
}

function openCentered(url, width, height, check) {
	if (screen.width < width) width=screen.width;
	if (screen.height< height) height=screen.height;
	var left = (screen.width - parseInt(width))/2;
	var top = (screen.height - parseInt(height))/2;
	var settings = 'left='+left+',top='+top+',width='+width+',height='+height+'';
	var pop = window.open(url, '', settings);
	if(check) return (!pop || !pop.window)? false:true;
}


/*
 *	Font sizer
 *	-------------------------
 */

function doFontSize(d) {
	var css = document.body.style;
	var size = parseInt(css.fontSize) || 11;
	css.fontSize = (size + d) + 'px';
}

/**
 *	Model color chooser
 *	-------------------------
 */

function setModelColor(color) {
	var img = document.getElementById('modelColor');
	img.src = color;
}
	function setColorLabel(link) {
		var title = link.title;
		var span = document.getElementById('colorTitle');
		span.innerHTML = title;
	}

function selectModelType(select) {
	var type = select[select.selectedIndex].value;
	document.location = '?type=' + type;
}

function showFlash(width, height, file) {
	if ( detectFlash() ) {
		document.write('<object width="'+width+'" height="'+height+'" align="right" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">');
		document.write('  <param NAME="movie" VALUE="'+file+'" />');
		document.write('  <param NAME="quality" VALUE="high" />');
		document.write('  <param NAME="wmode" VALUE="transparent" />');
		document.write('  <param NAME="allowScriptAccess" VALUE="always" />');
		document.write('  <embed src="'+file+'" quality="high" wmode="transparent" width="'+width+'" height="'+height+'" align="right" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
		document.write('</object>');
	} else {
		document.write('<div width="'+width+'" height="50" style="padding: 5px;border: 1px red solid">');
		document.write('  Sollten Sie diese Animation nicht sehen k&ouml;nnen, installieren Sie bitte den aktuellen Acrobat Flash Player.<br/>');
		document.write('  Laden Sie das Programm jetzt kostenlos herunter.<br/><br/>');
		document.write('  &gt;&gt; <a href="http://www.adobe.com/products/flashplayer/" target="_blank">Flash herunterladen</a>');
		document.write('</div>');
	}
}

function showPop(id)
{
   document.getElementById(id).style.visibility = "visible";
}
function hidePop(id)
{
   document.getElementById(id).style.visibility = "hidden";
}