/*
 Written by AMAI, 2009, http://www.amai.be.
 ----------
*/

var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);  

function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function showOptions(pageID)
{	
	//show options current element
	var imagePath = 'img/wrapper/table/';
	var fileType = '.png';
	
	// Defined in Defaul PT
	//
	//var imageCollection = ['lt', 't', 'rt', 'lm', 'm', 'rm', 'lb', 'b', 'rb'] 
	//

	var rootDiv = document.getElementById(pageID);
	var Element = document.getElementById(pageID).getElementsByTagName('div')[0];
	var Links = Element.getElementsByTagName('ul')[0];
	var LinksCollection = Links.getElementsByTagName('li').length;
	var LinksCollectionALINK = Links.getElementsByTagName('a');
	var Table = Element.getElementsByTagName('table')[0];
	var TableChild = Table.getElementsByTagName('td');
	
		//show links in table + fix it's height
		Links.style.display= 'block';	
		Links.style.height =  Links.getElementsByTagName('li')[0].offsetHeight * LinksCollection + 'px';

		//alert(Links.getElementsByTagName('li')[0].offsetHeight);
		
		//position footer element
		if (pageID == 'footer_dropdown'){
			rootDiv.style.marginTop = '-' + ((18 * LinksCollection) + 5) + 'px';
			rootDiv.style.zIndex = 2000;
		}
		
		//show shadow
		for (var i = 0; i< imageCollection.length; i++){
			if (isIE6 == true){
				rootDiv.style.background = '#FFF';
				rootDiv.style.border = '1px solid #AAA2A0';
			}else{
				TableChild[i].style.backgroundImage = 'url(' + imageCollection[i] + ')' ;
			}	
		}
}

function hideOptions(pageID)
{
	//restore footer position
	
	if (pageID == 'footer_dropdown')
		document.getElementById(pageID).style.marginTop = '5px';
		
	var rootDiv = document.getElementById(pageID);
	var Element = document.getElementById(pageID).getElementsByTagName('div')[0];
	var Table = Element.getElementsByTagName('table')[0];
	var TableChild = Table.getElementsByTagName('td')

		//hide links in table
        Element.getElementsByTagName('ul')[0].style.display='none';
		rootDiv.style.border = 'none';
		
		//hide shadow
		for (var i = 0; i< 9; i++)
		{TableChild[i].style.background = 'none';}
}

function toggleList(pageID)
{
	var Element = document.getElementById(pageID).getElementsByTagName('div')[0];
	var FirstLine = document.getElementById(pageID).getElementsByTagName('p')[0];
	var imageLink = FirstLine.getElementsByTagName('a')[0];
	var Links = Element.getElementsByTagName('ul')[0];
	var LinksCollection = Links.getElementsByTagName('li').length;
		
	if (Element.style.display != 'block')
	{
		var DivHeight = 30 + (30 * LinksCollection);
		document.getElementById(pageID).style.height =  DivHeight + 'px';
		Element.style.display='block';
		Element.style.zIndex = 2000;
		imageLink.style.backgroundImage = 'url(img/selector/inputArrowUp.jpg)' ;
	} 
	else
	{
		var DivHeight = 32;
		document.getElementById(pageID).style.height =  DivHeight + 'px';
		Element.style.display='none';
		imageLink.style.backgroundImage = 'url(img/selector/inputArrow.jpg)' ;
	}
}

function toggleBox(linkID)
{
	if (linkID.getElementsByTagName('p')[0].style.display == 'none'){
		linkID.getElementsByTagName('p')[0].style.display = 'block';
		
		if (isIE6 == true){
			linkID.getElementsByTagName('a')[0].style.marginLeft = '30px';
		}
	}
	else {
		linkID.getElementsByTagName('p')[0].style.display = 'none';
		
		if (isIE6 == true){
			linkID.getElementsByTagName('a')[0].style.marginLeft = '33px';
		}
	}
}

function addHoverUL(ELEM)
{
	var lis = document.getElementById(ELEM).getElementsByTagName('ul');
	
	for (var i=0; i<lis.length; i++)
	{
		lis[i].onmouseover = function()
		{
			if (isIE6 == true)
			{	
				this.style.background = '#000';
				this.style.color = '#fff';
				this.style.paddingBottom = '2px';
				this.style.paddingTop = '2px';
				this.style.borderTop = '1px solid #222';
				this.style.borderBottom = '1px solid #222';
			}
		};
		
		lis[i].onmouseout = function()
		{
			if (isIE6 == true)
			{	
				this.style.background = 'none';
				this.style.color = '#888';
				this.style.paddingBottom = '3px';
				this.style.paddingTop = '3px';
				this.style.borderTop = 'none';
				this.style.borderBottom = 'none';
			}
		};
	}
}
function addHoverUL_cb(ELEM)
{
	var lis = document.getElementById(ELEM).getElementsByTagName('ul');
	
	
	for (var i=0; i<lis.length; i++){
		lis[i].onmouseover = function(){
			if (isIE6 == true){	
				this.style.background = '#000';
				this.style.color = '#fff';
				this.style.paddingBottom = '6px';
				this.style.paddingTop = '7px';
				this.style.borderTop = '1px solid #222';
				this.style.borderBottom = '1px solid #222';
			}
		};
		
		lis[i].onmouseout = function(){
			if (isIE6 == true){	
				this.style.background = 'none';
				this.style.color = '#888';
				this.style.paddingBottom = '7px';
				this.style.paddingTop = '8px';
				this.style.borderTop = 'none';
				this.style.borderBottom = 'none';
			}
		};
	}
}

	function getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth + yScroll;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];
	}

	function navRightOffsetRight() {
		var arrayPageSize = getPageSize();
		var navRightOffsetRight = (arrayPageSize[0] - $('content').getWidth()) / 2;
		$('navigation_right').setStyle({ right: navRightOffsetRight + 'px' });
		if (isIE6 == true) {
			var navRight = $('navigation_right');
			var collNavRight = navRight.getElementsBySelector('[class="navRight"]');
			var collNavRightWidth = 0;
			for (i = 0; i < collNavRight.length; i++) {
   				collNavRightWidth = collNavRightWidth + collNavRight[i].getWidth() + 10;  
  			}
			var navRightWidth = collNavRightWidth;
			var navLeftWidth = ($('content').getWidth() - navRightWidth) - 30;
		} else {
			var navLeftWidth = $('content').getWidth() - $('navigation_right').getWidth() - 50;
		}
		$('navigation_left').setStyle({width: navLeftWidth + 'px'});
	}

	function checkEmail(str) {
		re = /(^[A-Za-z0-9_])([A-Za-z0-9_.-]*)@((([A-Za-z0-9_-]{1,})\.){1,})(([A-Za-z0-9]{1,4})$)/;
		return re.test(allTrim(str));
	}

	function lTrim(text) {
		var pos = text.indexOf(" ");
		while(pos==0){
			text = text.substring(pos+1,text.length);
			pos  = text.indexOf(" ");
		}
		return text;
	}

	function allTrim(text) {
		text = lTrim(text);
		var pos = text.length - 1;
		while(text.charAt(pos)==' ') {
			text = text.substring(0,pos);
			pos--;
		}
		return text;
	}





