/**
 * JavaScript Template functions. 
 *
 * @project    Clute Packaging Site
 * @module     Core
 * @author     Leszek Szamocki
 * @copyright  (c) 2009 New Concept Design - London Ontario web design studio.
 * @version    09.29a
 */

var base = '/';
var isIE = 	(navigator.appName == 'Microsoft Internet Explorer');
var isIE6 = 	isIE && (parseInt(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE') + 5, 1)) <  7);

var showMenu = [];

function autoFocus() {
	var form = document.getElementsByTagName('form')[0];
	if(form) {
		var input = form.getElementsByTagName('input')[0];
		if(input)
			input.focus();
	}
}

function bodyOnLoad(msg) {

	initMenu();
	autoFocus();
	
	if(msg.length)
		alert(msg);
}

function menuItem(name, route) {
	this.name = name;
	this.route = route;
	this.items = [];
}

function show(id) {
	var item = document.getElementById('m' + id);
	
	if(isIE6 && (item.parentNode.id == 'menu_box') && (showMenu[id] == 0)) {
		item.parentNode.className = item.parentNode.className + '_sel';
		item.parentNode.style.overflow = 'visible';
	}
		
	item.style.visibility = 'visible';
	showMenu[id] += 1;
}

function hide(id) {
	setTimeout(function() {_hide(id);}, 100);
}

function _hide(id) {
	showMenu[id] -= 1;
	
	if(showMenu[id] < 1) {
		var item = document.getElementById('m'+id);
		item.style.visibility = 'hidden';

		if(isIE6 && (item.parentNode.id == 'menu_box')) {
			item.parentNode.className = item.parentNode.className.replace( /_sel/, '');
			item.parentNode.style.overflow = 'hidden';
		}

	}
}

function initMenu() {
	var menu = document.getElementById('menu_box'); 
	
	if(!menu)
		return false;
		
	var items = menu.getElementsByTagName('div');
	
	for(var i=0; i < items.length; i++) {
		showMenu[i] = 0;
		
		if(items[i].id == 'm0')
			items[i].style.top = '43px';
		else
			items[i].style.position = 'absolute';
	}

	var items = menu.getElementsByTagName('a');
	
	for(var i=0; i < items.length; i++) {
		if(isIE)
			items[i].style.width = items[i].parentNode.offsetWidth - 12 + 'px';
			
		if(items[i].onmouseover) {
			var id = items[i].attributes['onmouseover'].value;
			id = 'm' + id.substring(5, id.indexOf(');'));
			var item = document.getElementById(id);
			item.style.left = items[i].parentNode.offsetWidth + 'px';

			if(isIE && (item.parentNode.id == 'menu_box'))
				item.style.top = items[i].parentNode.offsetTop + items[i].offsetTop + 'px';
			else
				item.style.top = items[i].offsetTop + 'px';
		}
	}
	
	if(isIE6)
		menu.style.overflow = 'hidden';
		
	return true;
}

function leasePup() {
    window.open(base + 'leaseCalc', 'lease_calc', 'width=440, height=500,menubar=no, location=no, resizable=yes, status=no, scrollbars=yes, left=10, top=10');
    return false;
}

function convPup() {
    h = screen.height;
    h = screen.height>800 ? 860 : 750;
    window.open(base + 'conversion', 'conversionSheet', 'width=720, height='+h+',menubar=no, location=no, resizable=yes, status=no, scrollbars=yes, left=10, top=10');
    return false;
}

document.observe("dom:loaded", function() {    
    function test(link, i) {
        link.href = "/leaseCalc";
        link.onclick = leasePup;
    }
    
    $$("a[href$='lease_calc']").each(test);
    $$("a[href$='leaseCalc']").each(test);
});	

function validContactForm() {
	if(document.getElementById('name').value.length < 1) {
		alert('Name is required.');
		document.getElementById('name').focus();
		return false;
	}
	
	if(document.getElementById('tel').value.length < 1) {
		alert('Phone number is required.');
		document.getElementById('tel').focus();
		return false;
	}
	
	with(document.getElementById('email')) {
		if(value.length < 1) {
			alert('E-mail address is required.');
			document.getElementById('email').focus();
			return false;
		}
		
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		
		if(apos < 1 || dotpos-apos < 2) {
		  alert('E-mail address you entered is incorrect.');
			focus();
		  return false;
		}
	}
	
	if(document.getElementById('city').value.length < 1) {
		alert('City name is required.');
		document.getElementById('city').focus();
		return false;
	}
	
	return true;
}
