﻿var isIE = (navigator.appName.indexOf("Microsoft") > -1);
var d = document;


function init()
{
	new setNav('mainMenu', 'subMenuContainer');
	setFieldFocus();
	setFieldFocus2();

	try{
		document.getElementById('passHidden').onfocus=hidepass;
	}catch(err){
		//for page that have no pass hidden field
	}

}

function createImg(src, alt, w, h, link, t, className)
{
	var img = d.createElement('img');
	if (src)	img.setAttribute('src', src);
	if (alt)	img.setAttribute('alt', alt);
	if (w)	img.setAttribute('width', w);
	if (h)	img.setAttribute('height', h);
	if (className)	img.className = className;
	
	if (link)
	{
		img.setAttribute('border', 0);
		
		var a = d.createElement('a');
		a.setAttribute('href', link);
		if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
		a.appendChild(img);
		return a;
	}
	else
	{	
		return img;
	}
}

function createA(link, t, txt, id, className)
{		
	var a = d.createElement('a');
	if (link)	a.setAttribute('href', link);
	if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
	
	//if (txt)	a.appendChild(d.createTextNode(txt));
	if (txt)	a.innerHTML = txt;
	if (id)	a.setAttribute('id', id);
	if (className)	a.className = className;
	
	return a;
}

function createDiv(id, className, txt)
{		
	var div = d.createElement('div');
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	//if (txt)	div.appendChild(d.createTextNode(txt));
	if (txt)	div.innerHTML = txt;
	return div;
}

function createTag(tag, id, className)
{		
	var div = d.createElement(tag);
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	return div;
}

function createInput(name, type, value, className)
{		
	var input = d.createElement('input');
	if (name)
	{
		input.setAttribute('name', name);
		input.setAttribute('id', name);
	}
	if (type)	input.setAttribute('type', type);
	if (value)	input.setAttribute('value', value);
	if (className)	input.className = className;
	return input;
}

function setChildNodes(obj, tagName)
{
	var array = new Array();
	
	for (var i=0; i<obj.childNodes.length; i++)
	{
		if (tagName)
		{
			//alert(obj.childNodes[i].tagName)
			if (obj.childNodes[i].tagName != tagName)	continue;
		}
		if (obj.childNodes[i].toString().toLowerCase().indexOf('text') >= 0)	continue;
		array.push(obj.childNodes[i]);
	}
	
	return array;
}
	
function addEvent (o, t, f)
{
	removeEvent (o, t, f);
	if (o.attachEvent) o.attachEvent('on'+ t, f);
	else o.addEventListener(t, f, false);
}

function removeEvent (o, t, f)
{
	if (o.detachEvent) o.detachEvent('on'+ t, f);
	else o.removeEventListener(t, f, false);
};

function getElementsByClassName(p, c)
{
	var array = new Array();
	var tags = p.getElementsByTagName('*');
	
	for (var i=0; i<tags.length; i++)
	{
		if (!tags[i].className)	continue;
		if (tags[i].className == c)	array.push(tags[i]);
	}
	
	return array;
}

function setNav(n, m)
{
	var navContainer = d.getElementById(n);
	var menuContainer = d.getElementById(m);
	var timeOutSpeed = 20;
	
	var self = this;
	
	this.init = function()
	{
		var a = navContainer.getElementsByTagName('a');
		for (var i=0; i<a.length; i++)
		{
			a[i].num = i;
			a[i].rel = a[i].parentNode.className.replace('mn_', '');
			
			if (!d.getElementById('sub_' + a[i].rel))	continue;
			a[i].menu = d.getElementById('sub_' + a[i].rel);
			a[i].menu.btn = a[i];
			a[i].menu.style.height = '0px';
			a[i].menu.motion = this.motion;
			a[i].menu.hit = false;
			
			a[i].onmouseover = function()
			{
				this.menu.hit = true;
				self.showMenu(this, this.menu);
			}
			
			a[i].onmouseout = function()
			{
				this.menu.hit = false;
				self.hideMenu(this, this.menu);
			}
			
			a[i].menu.onmouseover = function()
			{
				this.hit = true;
				self.showMenu(this.btn, this);
			}
			
			a[i].menu.onmouseout = function()
			{
				this.hit = false;
				self.hideMenu(this.btn, this);
			}
			
			var subMenuClose = getElementsByClassName(a[i].menu, 'subMenuClose')[0];
			subMenuClose.onclick = function()
			{
				var menu = this.parentNode.parentNode.parentNode.parentNode;
				menu.hit = false;
				self.hideMenu(menu.btn, menu, true);
			}
		}
	}
	
	this.showMenu = function(btn, menu)
	{
		btn.className += ' active';
		menu.style.visibility = 'visible';
		
		menu.H = menu.scrollHeight;
		clearTimeout(menu.timeOut);
		menu.motion(menu);
	}
	
	this.hideMenu = function(btn, menu, directOut)
	{
		//btn.className = btn.className.replace('active', '');
		//menu.style.visibility = '';
		
		var time = (directOut)	?	0	:	200;
		
		setTimeout(function()
		{
			if (menu.hit)	return;
			menu.H = 0;
			clearTimeout(menu.timeOut);
			menu.motion(menu);
		}, time);
	}
	
	this.motion = function(obj)
	{
		obj.objH = obj.offsetHeight;
		obj.goStep = Math.floor((obj.H - obj.objH)/4);
		
		if ((Math.abs(obj.H - obj.objH) > 1))
		{
			obj.objH += obj.goStep;
			obj.timeOut = setTimeout(function(){obj.motion(obj);}, timeOutSpeed);
		}
		else
		{
			obj.objH = obj.H;
			clearTimeout(obj.timeOut);
		}
		
		if (obj.objH <= 10)
		{
			obj.btn.className = obj.btn.className.replace(/active/g, '');
			obj.style.visibility = '';
		}
		
		obj.style.height = obj.objH + 'px';
		//window.status = obj.H + '  :  ' + obj.objH;
	}
	
	this.init();
}


function showHideDiv(obj, num)
{
	var div = setChildNodes(d.getElementById(obj), 'DIV');
	
	for (var i=0; i<div.length; i++)
	{
		div[i].style.display = 'none';
	}
	
	div[num].style.display = 'block';
}


function setFieldFocus()
{
	var input = d.getElementsByTagName('input');
	
	for (var i=0; i<input.length; i++)
	{
		if (input[i].type != 'text')			continue;
		//if (input[i].className == 'routeField')	continue;
		
		input[i].onfocus = function()
		{
			if (this.value == this.defaultValue)	this.value = '';
		}
		
		input[i].onblur = function()
		{
			if (this.value == '')	this.value = this.defaultValue;
		}
	}
}

function setFieldFocus2()
{
	var input = d.getElementsByTagName('input');
	
	for (var i=0; i<input.length; i++)
	{
		if (input[i].type != 'password')			continue;
		//if (input[i].className == 'routeField')	continue;
		
		input[i].onfocus = function()
		{
			if (this.value == this.defaultValue)	this.value = '';
		}
		
		input[i].onblur = function()
		{
			if (this.value == '') this.value = this.defaultValue;
		}
	}
}
