// JavaScript Document
function output_normal(imgfile, width, height,style) {
	var version = navigator.userAgent;
	if (version.indexOf('Firefox') == -1) {
		var content = '<img src=\"images/blank.gif\" border=\"0\" style=\"'+ style+ 'width: '+ width +'px; height: '+ height +'px; ' + 
		'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgfile + '\',sizingMethod=\'scale\')\" />';
		document.write(content);
	}
	else  {
		var content = '<img border=\"0\"  style=\"'+style+'\" src=\"' + imgfile + '\" width=\"'+ width +'\" height=\"'+ height +'\"></div>';
		document.write(content);
	}
}


function openWindow(url,w,h) {
	popupWin=window.open(url,'remote','width='+w+',height='+h+',top=20,left=20');
}

function fillAddress() {
	document.f1.company2.value = document.f1.company.value;
	document.f1.fname2.value = document.f1.fname.value;
	document.f1.lname2.value = document.f1.lname.value;
	document.f1.address2.value = document.f1.address.value;
	document.f1.city2.value = document.f1.city.value;
	document.f1.prov2.value = document.f1.prov.value;
	document.f1.postal2.value = document.f1.postal.value;
	document.f1.country2.value = document.f1.country.value;	
	document.f1.phone2.value = document.f1.phone.value;
}

function buildMenuList(menu, opts)
{
	for(i=0;i<opts.length;i++)
	{
		menu[i] = new Option(opts[i].title, opts[i].code); 
	}
}

function build_campaign_code(code, level){
	_ajax = new Ajax.Request(
		'emcms/includes/lib_ajax_menu.php',
		{
			method : 'post',
			parameters : 'code=' + code +'& level=' + level,
			onComplete : function(req, json)
			{
				$(level).options.length = 0;
				buildMenuList($(level), json);
				
			}
		}
	);
}

function inputRestriction(matchString, e){
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if(!String.fromCharCode(unicode).match(matchString))
		return false //disable key press
	}
}

function intOnly(e){
	var matchString = new RegExp("[0-9]");
	return inputRestriction(matchString, e);
}

function setCookie (name, value, path, domain, secure, expires)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie (name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie (name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function addLoadEvent (func)
{    
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

function menu_init ()
{
	var menu = document.getElementById('nav');
	var subs = menu.childNodes;
	
	var j = 0;
	
	for (var i=0 ; subs[i]; i++)
	{
		if (subs[i].tagName=='LI')
		{
			hs = subs[i].getElementsByTagName('A');
			heading = hs[0];
			ss = subs[i].getElementsByTagName('UL');
			submenu = ss[0];
			
			j++;
			
			heading.onclick = function () { menu_toggle(this); };

			if (getCookie('menu'+j)=='1')
				 submenu.style.display = 'block';
			else if (getCookie('menu'+j)=='0')
				submenu.style.display = 'none';
			else if (j==1)
				submenu.style.display = 'block';
			else
				submenu.style.display = 'none';
		}
	}
}

function menu_toggle (heading)
{
	var section = heading.parentNode;
	var submenus = section.getElementsByTagName('UL');
	var submenu = submenus[0];
		
	if (submenu.style.display=='none')
		submenu.style.display = 'block';
	else
		submenu.style.display = 'none';
		
	var j = 0;

	var menu = document.getElementById('nav');
	var subs = menu.childNodes;
	for (var i=0 ; subs[i]; i++)
	{
		if (subs[i].tagName=='LI')
		{
			hs = subs[i].getElementsByTagName('A');
			h = hs[0];			
			j++;
			
			if (h==heading && submenu.style.display=='none')
				setCookie('menu'+j, '0', '/');
			else if (h==heading)
				setCookie('menu'+j, '1', '/');
		}
	}
		

}
addLoadEvent(menu_init);