var uriWindow;

function showDivIfVal(obj, valCheck)
{
	showDiv = false;
	if(valCheck.indexOf("|") > 0)
	{
		if(obj.value)
		{
		valCheckArray = valCheck.split("|");
		for(i=0; i < valCheckArray.length; i++)
		{
			if(obj.value.indexOf(valCheckArray[i]) > 0)
				{
					showDiv = true;
				}
			}
		}
	}
	else
	{
		if(obj.value)
		{
			if(obj.value.indexOf(valCheck) > 0)
			{
				showDiv = true;
			}
		}
	}


	if(showDiv)
	{
		document.getElementById('widthHeightOptionWrapper').style.display = 'block';
		document.getElementById('imgOptionWrapper').style.display = 'none';

	}
	else
	{
		document.getElementById('widthHeightOptionWrapper').style.display = 'none';
		document.getElementById('imgOptionWrapper').style.display = 'block';
	}

	return true;
}

function windowURI(url, w, h,time,resizable)	// windowURI(link,width,height,time(close the new window after x seconds),resizable(1 -> true)
{
	if(time == undefined)
		time = 0;

	if(resizable == undefined)
		resizable = 1;

	uriWindow = window.open(url,"Window_print","menubar=no,scrollbars=1,left=10,top=0,width="+w +" ,height="+h +",resizable="+resizable);

	if(time != 0 && time != -1)
	{
		setTimeout('uriWindow.close();',time);
	}
}

/**
* toggles the menu
*/
function menuToggle(menuElement, clicked)
{
	var el_parent = clicked.parentNode; //get the parent of the UL to unfold
	var el_grandParent = el_parent.parentNode; //get the grandparent of the UL to unfold
	var uncles = el_grandParent.childNodes; //get alle children of the grandparent (li with possible nested ul which we want to fold)
	for(u_counter=0; u_counter<uncles.length; u_counter++)
	{
		nephews = uncles[u_counter].childNodes; //get the children of the LI (nephews of the UL to unfold)
		for(n_counter=0; n_counter<nephews.length; n_counter++) //loop over nephews
		{
			if(nephews[n_counter].tagName == 'UL') //if it's a nephew
			{
				if(nephews[n_counter].style.height != undefined) //if it has a style defined (has been toggled before)
				{
					if(nephews[n_counter].style.height == 'auto') //if the style is set to 'auto' (it's visible)
					{
						var folder = new fx.Height(nephews[n_counter].id, {duration: 600});
						folder.toggle(); //close it!
						var furtherdown = nephews[n_counter].getElementsByTagName('UL'); //get all underlying UL and close them also.
						var fd_len = furtherdown.length;
						for(fd_counter = 0; fd_counter<fd_len; fd_counter++)
						{
							if(furtherdown[fd_counter].style.height != undefined) //if it has a style defined (has been toggled before)
							{
								if(furtherdown[fd_counter].style.height == 'auto') //if the style is set to 'auto' (ie: it's visible)
								{
									var folder_f = new fx.Height(furtherdown[fd_counter].id, {duration: 600})
									folder_f.toggle(); //start the effect
									var a = furtherdown[fd_counter].getElementsByTagName('A');
									for(count_a =0; count_a<a.length; count_a++)
									{
										a[count_a].className = ''; //give the href a new style
									}
								}
							}
							var a = furtherdown[fd_counter].parentNode.getElementsByTagName('A');
							var len = a.length;
							for(count_a =0; count_a<len; count_a++)
							{
								a[count_a].className = ''; //give the href a new style
							}
						}

					}
					var a = nephews[n_counter].parentNode.getElementsByTagName('A');
					var len2 = a.length;
					for(count_a =0; count_a<len2; count_a++)
					{
						a[count_a].className = ''; //give the href a new style
					}
				}
			}
			if(nephews[n_counter].tagName == 'A'){
				if(nephews[n_counter].className != 'secondMenuItem') //change de class of a nephew without subitems.
				{
					if(nephews[n_counter].className =='subopenSM')
					{
						nephews[n_counter].className ='secondMenuItem';
					}
					else
					{
						nephews[n_counter].className ='';
					}
				}
			}
		}

	}
	var a_top = el_parent.getElementsByTagName('A')[0];
	if(a_top.className == ''){
		a_top.className = 'subopen'; //give the href a new style
	}


	if (document.getElementById(menuElement) != undefined)
	{
		var containerHeight;
		var el = document.getElementById(menuElement);
		//this part is for when the first element is automaticly opened.
		//get all Anchors of below this ndoe
		li_firstChild = el.getElementsByTagName('li');
		a_actionNode = li_firstChild[0].getElementsByTagName('a');

		//verify is an array was fetched
		if(a_actionNode.length)
		{
			//verify if autoopen is set to true
			if(a_actionNode[0].getAttribute('autoopen'))
			{
				//adjust the classnName of the Anchor
				a_actionNode[0].className = 'subopen_childless';
			}
		}

		//alert(el.style.display);
			el.style.display = 'block';
			containerHeight = new fx.Height(menuElement, {duration: 600, onComplete: function()
				{
					x = document.getElementById(menuElement);
					if(parseFloat(x.style.height))
					{
						x.style.height = 'auto';
					}


				}
			});
			containerHeight.toggle();

	}
	else //there is no submenu, change te style accordingly.
	{
		if(a_top.className == 'secondMenuItem')
		{
			a_top.className = 'subopenSM';
		}
		else
		{
			a_top.className = 'subopen_childless';
		}
	}
}

//mouseovers
function handleMouseOver(elt, type, buttontype)
{
	if(type == 1) // Mouseover
	{
		var img = '/images/'+ buttontype +'_over.png';
	}
	else // Mouseout
	{
		var img = '/images/'+ buttontype +'.png';
	}

	var arVersion = navigator.appVersion.split("MSIE");

	var version = parseFloat(arVersion[1]);

	if((version >= 5.5)&& (document.body.filters))
	{
		// Transparency in IE hack
		elt.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + img + ')';
		elt.src = '/images/blank.gif';
	}
	else
	{
		elt.src = img;
	}
}

function changePage(url, divName, pageTitle, elId)
{
	if (url != undefined)
	{
		var http = getHTTPObject();
		var divObj = document.getElementById(divName);
		var rand = randomString();

		new Ajax(url, {update: $(divName), evalScripts: true, onComplete: function()
			{
				if (pageTitle != undefined)
				{
					document.title = 'Dedato - '+pageTitle;
				}
			}
		}).request();
	}
}

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

	return xmlhttp;
}

function handleLoginSubmit(form)
{

	if(!md5_vm_test())
	{
		return true;
	}
	else
	{

		p = document.getElementById('password').value;
		c = document.getElementById('challenge').value;

		document.getElementById('passwordS').value = hex_md5(hex_md5(p) + c);
		document.getElementById('loginnameS').value = document.getElementById('loginname').value
		document.getElementById('shadowForm').submit();
		return false;
	}

}

function placefooter()
{
	document.getElementById('footer').style.top = '';
	document.getElementById('footer').style.bottom = '';

	var divh = document.getElementById('menu').offsetHeight;
	var pageh = document.body.scrollHeight;

	if(pageh == undefined)
	{
		pageh = document.body.scrollHeight;
	}

	if(divh > pageh)
	{
		document.getElementById('footer').style.bottom = '0px';
	}
	else
	{
		document.getElementById('footer').style.top = pageh+'px';
	}
}

function randomString()
{
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var randomstring = '';

	for (var i=0; i<string_length; i++)
	{
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}

	return randomstring;
}

// SWF check for editImagePage.html
// Using this function, the width and height options for Flash animations are selected
function checkFlashExtension(fld)
{
	flash_extensions = /(.swf)$/i;

	if (flash_extensions.test(fld.value))
	{
		document.getElementById('widthHeightOptionWrapper').style.display = 'block';
		document.getElementById('imgOptionWrapper').style.display = 'none';
	}
	else
	{
		document.getElementById('widthHeightOptionWrapper').style.display = 'none';
		document.getElementById('imgOptionWrapper').style.display = 'block';
	}
}


/**
* debug function, remove for live
*/
debugcounter = 0;
function debug(debugvar){
	debugdiv = document.getElementById('debugdiv');
	if(!debugdiv)
	{
		bugdiv = document.createElement('div');
		bugdiv.style.position = 'absolute';
		bugdiv.style.right = '5px';
		bugdiv.style.top = '5px';
		bugdiv.style.width = '210px';
		bugdiv.id = 'debugdiv';
		bugdiv.style.fontSize = '10px';
		bugdiv.style.backgroundColor = '#c0c0c0';
		bugdiv.style.border = 'solid 1px #0ff';
		document.body.appendChild(bugdiv);
		debugdiv = document.getElementById('debugdiv');
	}
	debugdiv.innerHTML = debugcounter+': '+debugvar+'<br />'+debugdiv.innerHTML;
	debugcounter++;
}
