//test if the browser is IE
regex=new RegExp(/MSIE/);
var isie=regex.test(navigator.appVersion);

//preset the mouse-coordinates
var mousex=0;
var mousey=0;

//enable tracking of mouse-events
if (!isie)
{
	document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove=getMouseXY;

/**
 * Get the mouse coordinates.
 * 
 * @param unknown e
 */
function getMouseXY(e)
{
	//code for IE
	if (isie)
	{
		//mousex=event.clientX+document.body.scrollLeft;
		//mousey=event.clientY+document.body.scrollTop;
		mousex=event.clientX+document.documentElement.scrollLeft;
		mousey=event.clientY+document.documentElement.scrollTop;
	}
	//code for other browsers
	else
	{
		mousex=e.pageX;
		mousey=e.pageY;
	}
}

/**
 * Get the value of a field.
 * 
 * @param string name
 * @return mixed
 */
function getfieldvaluebyname(name)
{
	//get the number of items with the given name
	var index=document.getElementsByName(name).length-1;
	

	//if no items have been found return false
	if (index<0)
	{
		
		return false;
	}

	//return the value of the last item with the given name
	return document.getElementsByName(name)[index].value;
}

/**
 * Set the value of a field.
 * 
 * @param string name
 * @return bool
 */
function setfieldvaluebyname(name,value)
{
	//get the number of items with the given name
	index=document.getElementsByName(name).length-1;

	//if no items have been found return false
	if (index<0)
	{
		return false;
	}

	//set the value of the last item with the given name
	//document.getElementsByName(name)[(document.getElementsByName(name).length-1)].value=value;
	document.getElementsByName(name)[index].value=value;
}

/**
 * Show the full-size image.
 * 
 * @param string image
 * @param int width
 * @param int height
 * @param int offsetx
 * @param int offsety
 */
function showfullimage(image,width,height,offsetx,offsety)
{
	//close all full-size images
	closefullimage();

	//calculate the position of the image
	x=mousex-width/2;
	y=mousey-height/2;

	//calculate the horizontal offset
	if (offsetx)
	{
		x+=offsetx;
	}

	//calculate the vertical offset
	if (offsety)
	{
		y+=offsety;
	}

	//create a new div
	var div=document.createElement('div');

	//set attributes,event-handlers and styles
	div.setAttribute('id','fullimage');
	div.onmouseout=function() { closefullimage(); };
	div.style.position='absolute';
	div.style.top=y+'px';
	div.style.left=x+'px';
	div.style.width=width+'px';
	div.style.height=height+'px';
	div.style.border='1px solid';
	//do not allow dynamic image resizing
	//div.style.backgroundImage='url('+image+')';
	//allow dynamic image resizing
	div.innerHTML='<img src="'+image+'" width="'+width+'px" />';

	//add the div to the document
	document.body.appendChild(div);
}

/**
 * Close the full-size image.
 */
function closefullimage()
{
	//get the full-image-div
	var div=document.getElementById('fullimage');

	//loop while there are full-image-divs
	while (div)
	{
		//remote the div from the document
		document.body.removeChild(div);

		//search for another full-image-div
		var div=document.getElementById('fullimage');
	}
}

/**
 * Hash the login-form to prevent passwords from being sent in clear-text.
 * 
 * @return bool
 */
function hashloginform()
{
	//by default set the return-value to true (successful)
	process=true;
	

	//get the fields of the form
	username=getfieldvaluebyname('login_username');
	password=getfieldvaluebyname('login_password');

	//check if the username is empty
	if (username=='')
	{
		process=false;

		document.getElementById('login_username').style.color='#ff0000';
		document.getElementById('login_username').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('login_username').style.color='#000000';
		document.getElementById('login_username').style.fontWeight='normal';
	}

	//check if the password is empty
	if (password=='')
	{
		process=false;

		document.getElementById('login_password').style.color='#ff0000';
		document.getElementById('login_password').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('login_password').style.color='#000000';
		document.getElementById('login_password').style.fontWeight='normal';
	}

	//if any field was empty return false
	if (process==false)
	{
		return false;
	}

	//switch the values of the fields and their hash-fields
	setfieldvaluebyname('login_username',getfieldvaluebyname('login_loginhash1'));
	setfieldvaluebyname('login_password',getfieldvaluebyname('login_loginhash2'));
	setfieldvaluebyname('login_loginhash1',username);
	setfieldvaluebyname('login_loginhash2',hex_md5(password));

	return true;
}

/**
 * Hash the registration-form to prevent passwords from being sent in clear-text.
 * 
 * @return bool
 */
function hashregistrationform()
{
	//by default set the return-value to true (successful)
	process=true;

	//get the fields of the form
	username=getfieldvaluebyname('register_username');
	password=getfieldvaluebyname('register_password');
	repeatpassword=getfieldvaluebyname('register_repeatpassword');
	email=getfieldvaluebyname('register_email');

	//check if the username is empty
	if (username=='')
	{
		process=false;

		document.getElementById('register_username').style.color='#ff0000';
		document.getElementById('register_username').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('register_username').style.color='#000000';
		document.getElementById('register_username').style.fontWeight='normal';
	}

	//check if the password is empty
	if (password=='')
	{
		process=false;

		document.getElementById('register_password').style.color='#ff0000';
		document.getElementById('register_password').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('register_password').style.color='#000000';
		document.getElementById('register_password').style.fontWeight='normal';
	}

	//check if the confirmation-password is empty
	if (repeatpassword=='')
	{
		process=false;

		document.getElementById('register_repeatpassword').style.color='#ff0000';
		document.getElementById('register_repeatpassword').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('register_repeatpassword').style.color='#000000';
		document.getElementById('register_repeatpassword').style.fontWeight='normal';
	}

	//check if the password and confirmation-password are different or the password is empty
	if (password!=repeatpassword)
	{
		process=false;

		document.getElementById('register_password').style.color='#ff0000';
		document.getElementById('register_password').style.fontWeight='bold';
		document.getElementById('register_repeatpassword').style.color='#ff0000';
		document.getElementById('register_repeatpassword').style.fontWeight='bold';
	}
	else if (password!='')
	{
		document.getElementById('register_password').style.color='#000000';
		document.getElementById('register_password').style.fontWeight='normal';
		document.getElementById('register_repeatpassword').style.color='#000000';
		document.getElementById('register_repeatpassword').style.fontWeight='normal';
	}

	//check if the email-address is empty or not a valid email-address
	if ((email=='') || (!checkmail(email)))
	{
		process=false;

		document.getElementById('register_email').style.color='#ff0000';
		document.getElementById('register_email').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('register_email').style.color='#000000';
		document.getElementById('register_email').style.fontWeight='normal';
	}

	//exit of there any field was empty/invalid
	if (process==false)
	{
		return false;
	}

	//switch values of the fields with their hash-fields
	setfieldvaluebyname('register_username',getfieldvaluebyname('register_loginhash1'));
	setfieldvaluebyname('register_password',getfieldvaluebyname('register_loginhash2'));
	setfieldvaluebyname('register_repeatpassword',getfieldvaluebyname('register_loginhash3'));
	setfieldvaluebyname('register_email',getfieldvaluebyname('register_loginhash4'));
	setfieldvaluebyname('register_loginhash1',username);
	setfieldvaluebyname('register_loginhash2',hex_md5(password));
	setfieldvaluebyname('register_loginhash3',hex_md5(repeatpassword));
	setfieldvaluebyname('register_loginhash4',email);

	return true;
}

function hashpasswordform()
{
	//by default set the return-value to true (successful)
	process=true;

	//get the fields of the form
	oldpassword=getfieldvaluebyname('password_oldpassword');
	password=getfieldvaluebyname('password_password');
	repeatpassword=getfieldvaluebyname('password_repeatpassword');

	//check if the old password is empty
	if (oldpassword=='')
	{
		process=false;

		document.getElementById('password_oldpassword').style.color='#ff0000';
		document.getElementById('password_oldpassword').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('password_oldpassword').style.color='#000000';
		document.getElementById('password_oldpassword').style.fontWeight='normal';
	}
	
	//check if the password is empty
	if (password=='')
	{
		process=false;

		document.getElementById('password_password').style.color='#ff0000';
		document.getElementById('password_password').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('password_password').style.color='#000000';
		document.getElementById('password_password').style.fontWeight='normal';
	}

	//check if the confirmation-password is empty
	if (repeatpassword=='')
	{
		process=false;

		document.getElementById('password_repeatpassword').style.color='#ff0000';
		document.getElementById('password_repeatpassword').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('password_repeatpassword').style.color='#000000';
		document.getElementById('password_repeatpassword').style.fontWeight='normal';
	}

	//check if the password and confirmation-password are different or the password is empty
	if (password!=repeatpassword)
	{
		process=false;

		document.getElementById('password_password').style.color='#ff0000';
		document.getElementById('password_password').style.fontWeight='bold';
		document.getElementById('password_repeatpassword').style.color='#ff0000';
		document.getElementById('password_repeatpassword').style.fontWeight='bold';
	}
	else if (password!='')
	{
		document.getElementById('password_password').style.color='#000000';
		document.getElementById('password_password').style.fontWeight='normal';
		document.getElementById('password_repeatpassword').style.color='#000000';
		document.getElementById('password_repeatpassword').style.fontWeight='normal';
	}

	//exit if any field was empty/invalid
	if (process==false)
	{
		return false;
	}

	//switch values of the fields with their hash-fields
	setfieldvaluebyname('password_oldpassword',getfieldvaluebyname('password_loginhash1'));
	setfieldvaluebyname('password_password',getfieldvaluebyname('password_loginhash2'));
	setfieldvaluebyname('password_repeatpassword',getfieldvaluebyname('password_loginhash3'));
	setfieldvaluebyname('password_loginhash1',hex_md5(oldpassword));
	setfieldvaluebyname('password_loginhash2',hex_md5(password));
	setfieldvaluebyname('password_loginhash3',hex_md5(repeatpassword));

	return true;
}

/**
 * Check th order-form.
 * 
 * @return bool
 */
function checkorderform()
{
	//by default set the return-value to true (successful)
	process=true;

	//get the fields of the form
	name=getfieldvaluebyname('order_name');
	address=getfieldvaluebyname('order_address');
	area=getfieldvaluebyname('order_area');

	//check if the name is empty
	if (name=='')
	{
		process=false;

		document.getElementById('order_name').style.color='#ff0000';
		document.getElementById('order_name').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('order_name').style.color='#000000';
		document.getElementById('order_name').style.fontWeight='normal';
	}

	//check if the address is empty
	if (address=='')
	{
		process=false;

		document.getElementById('order_address').style.color='#ff0000';
		document.getElementById('order_address').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('order_address').style.color='#000000';
		document.getElementById('order_address').style.fontWeight='normal';
	}

	//check if the area is empty
	if (area=='')
	{
		process=false;

		document.getElementById('order_area').style.color='#ff0000';
		document.getElementById('order_area').style.fontWeight='bold';
	}
	else
	{
		document.getElementById('order_area').style.color='#000000';
		document.getElementById('order_area').style.fontWeight='normal';
	}

	return process;
}

/**
 * Check an email-address for validity.
 * 
 * @param string mail
 * @return bool
 */
function checkmail(mail)
{
	regex=new RegExp(/.+@(.+[.])+[\w]{2,}/);
	return regex.test(mail);
}

/**
 * Show a category.
 * 
 * @param string id
 * @param array categories
 */
function showcategory(id,categories)
{
	//set the category-cookie
	document.cookie='showcategory='+id+'; path=/';

	//walk through the categories
	for (x=0;x<categories.length;x++)
	{
		//if the current category is the selected category show it, otherwise hide it
		if (categories[x]==id)
		{
			document.getElementById('category_'+categories[x]).style.display='block';
		}
		else
		{
			document.getElementById('category_'+categories[x]).style.display='none';
		}
	}
}

/**
 * Open/close a DIV.
 * 
 * @param string id
 * @return bool
 */
function openclosediv(id)
{
	//if the div is open (display=block) hide it, otherwise show it
	if (document.getElementById('div_'+id).style.display=='block')
	{
		document.getElementById('div_'+id).style.display='none';
	}
	else
	{
		document.getElementById('div_'+id).style.display='block';
	}

	return false;
}

function opendiv(id) {
	document.getElementById('div_'+id).style.display='block';
	return false;

}

/**
 * Open/close a DIV and change it's icon.
 * 
 * @param string id
 * @return bool
 */
function openclosepicdiv(id)
{
	//if the div is hidden (display=none) show it and change the image to close.gif, otherwise hide it and change the image to open.gif
	if (document.getElementById('div_'+id).style.display=='none')
	{
		document.getElementById('div_'+id).style.display='block';
		document.getElementById('img_'+id).src='images/layout/close.gif';
	}
	else
	{
		document.getElementById('div_'+id).style.display='none';
		document.getElementById('img_'+id).src='images/layout/open.gif';
	}
	return false;
}

/**
 * Logout the user.
 * 
 * @return bool
 */
function logout()
{
	//submit the logout-form
	document.getElementById('form_logout').submit();
	return false;
}

/**
 * Check the search-field.
 * 
 * @return bool
 */
function checksearchfield()
{
	//don't start the search if the search-field contains "Search..."
	if (document.getElementById('searchfield').value=='Search...')
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Prevent submission of forms through input-fields
 * 
 * @param object formobject
 * @param object keyevent
 * @param string type
 * @return bool
 */
function catchenter(formobject,keyevent,type)
{
	//check which kind of event has to be used, depends on the browser
	if (window.event)
	{
		keynum=keyevent.keyCode;
	}
	else
	{
		keynum=keyevent.which;
	}

	//if the key is #13 (enter), catch it, find the submit-button and click it
	if (keynum==13)
	{
		children=formobject.parentNode.childNodes;
		for (x=0;x<children.length;x++)
		{
			if (children[x]['type']==type)
			{
				children[x].click();
				break;
			}
		}

		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Find fields that have to be checked.
 * 
 * @param object parentobject
 * @param string prefix
 * @return array
 */
function findcheckfields(parentobject,prefix)
{
	//initialize the array
	var checkfields=new Array();

	//traverse through all children of the parent-object
	for (var x=0;x<parentobject.childNodes.length;x++)
	{
		////if the child has the right id add it to the array
		if (parentobject.childNodes[x].id)
		{
			if (parentobject.childNodes[x].id.indexOf(prefix)==0)
			{
				checkfields[checkfields.length]=parentobject.childNodes[x].id.substr(prefix.length);
			}
		}

		//if the child has children recursively call findcheckboxes with the child as new parent
		if (parentobject.childNodes[x].childNodes.length>0)
		{
			var subs=findcheckfields(parentobject.childNodes[x],prefix);
			if (subs.length>0)
			{
				checkfields=checkfields.concat(subs);
			}
		}
	}

	//return the array
	return checkfields;
}

/**
 * Check the form before submission.
 * 
 * @param object formobject
 * @return bool
 */
function checkform(formobject)
{
	//by default the form will be processed
	var processform=true;

	//find the fields that have to be checked
	var fields=findcheckfields(formobject,formobject.id+'_');

	//traverse through the fields
	for (var x=0;x<fields.length;x++)
	{
		//by default no error is assumed
		var fielderror=false;

		//if the field is empty set fielderror=true
		if (getfieldvaluebyname(formobject.id+'_'+fields[x])=='')
		{
			fielderror=true;
		}
		//if the field has the name email and is not a valid email-address set fielderror=true
		else if ((fields[x]=='email') && (!checkmail(getfieldvaluebyname(formobject.id+'_'+fields[x]))))
		{
			fielderror=true;
		}

		//if a field has caused an error set it's label to bold, red text, otherwise regular, black text
		if (fielderror==true)
		{
			processform=false;

			document.getElementById(formobject.id+'_'+fields[x]).style.color='#ff0000';
			document.getElementById(formobject.id+'_'+fields[x]).style.fontWeight='bold';
		}
		else
		{
			document.getElementById(formobject.id+'_'+fields[x]).style.color='#000000';
			document.getElementById(formobject.id+'_'+fields[x]).style.fontWeight='normal';
		}
	}

	//return if the form should be submitted
	return processform;
}

/**
 * Copy the delivery-address to the invoice-address
 */
function copyaddress(from, to)
{
	//'order_delivery_', 'order_invoice_'
	//get the order-form
	var orderform=document.getElementById('order');

	//get the fields with the delivery-address
	var fields=findcheckfields(orderform,from);

	//fill the fields with the invoice-address
	for (var x=0;x<fields.length;x++)
	{
		setfieldvaluebyname(to+fields[x],getfieldvaluebyname(from+fields[x]));
	}
}

/**
 * Show a confirmation-dialog before deleting.
 * 
 * @return bool
 */
function confirmdeleteitem()
{
	return confirm("Do you really want to delete this item?");
}

function NewWindow(url)
{
	var w = window.open(url);
	w.focus();
}
