/**
 *	We check if the entered character is an integer, otherwise we delete the last character.
 *	@param The object that called this function
 *	@return void
 */
function verify_integer(call_object)
{
	var str_len = call_object.value.length;
	var char = call_object.value.charAt(str_len - 1);
	var regexp = /[0-9]/;

	if (false == regexp.test(char))
		call_object.value = call_object.value.substring(0, str_len - 1);
}


/**
 *	Generates a username with one character and seven numbers.
 *	@param void
 *	@return String Returns the new username
 */
function generate_username()
{
	var chars = new Array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	var username = chars[Math.floor(0 + (22 + 1) * (Math.random()))];
	username = username + Math.floor(1000000 + (9999999 - 1000000 + 1) * (Math.random()));
	
	return username;
}


/**
 *	Generates a password of 8 characters and numbers.
 *	@param void
 *	@return String Returns the password
 */
function generate_password()
{
	var capitals = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
	var characters = new Array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	
	var password = "";
	var i;
	var random;
	
	for (i = 0; i < 8; ++i)
	{
		// generate a random value between 0 and 2
		random = Math.floor(0 + 3 * (Math.random()));
		switch (random)
		{
			case 0:
				// take a capital
				password = password + capitals[Math.floor(0 + (22 + 1) * (Math.random()))];
				break;
			case 1:
				// take a character
				password = password + characters[Math.floor(0 + (22 + 1) * (Math.random()))];
				break;
			case 2:
				// take a number between 1 and 9
				password = password + Math.floor(1 + 9 * (Math.random()));
				break;
		}
	}
		
	return password;
}


/**
 *	Swaps an image.
 *	@param Object The object we refer to. This must be an <img>-tag!
 *	@param String The filepath of the object we should replace
 *	@return void
 */
function toggle_image(object, path)
{
	object.src = path;
}


/**
 *	This is a lifetime reloading method for a captcha image.
 *	@param void
 *	@return void
 */
function reload_captcha()
{
	// create random value between 100000 and 999999 as argument for the url
	var rand = 100000 + 999999 * Math.random();
	rand = Math.round(rand);
	
	var captcha = new Image();
	captcha.src = "lib/inc/captcha.inc.php?" + rand;
	document.getElementById("captcha_image").src = captcha.src;
}


/**
 *	If there is any text in a textfield we clear up.
 *	@param Object The object we want to modify
 *	@return void
 */
function clearTextfield(object)
{
    object.value="";
}


/**
 *	This can be used to rewrite any text into a textvalue, for example "searchtext" into a textfield for the search.
 *	@param Object This is the object (normally a textfield) we want to modify
 *	@param String text This is used as new inserted text
 *	@param String This is the CSS-value for the objects text color
 *	@return void
 */
function rewriteTextfield(object, text, color)
{
    if (object.value == "")
    {
        object.style.color = color;
        object.value = text;
    }
}


/**
 *	Shows the textfield for the reduced price.
 *	@param void
 *	@return void
 */
function showMarkdownPrice()
{
	// if this is already shown we hide it
	if (document.getElementById("markdown_price").style.display == "table-row")
		document.getElementById("markdown_price").style.display = "none";
	else
		document.getElementById("markdown_price").style.display = "table-row";
}


/**
 *	If this is the login file we set the focus to the username field.
 *	@param void
 *	@return void
 */
function set_focus()
{
	// extract file name, we need this to compare the file name
	var strFilename;
	var strHref = self.location.href;

	strHref = strHref.replace(/\//g,"\\");

	var iIdx = strHref.lastIndexOf("\\");
	if (-1<iIdx)
	{
		strFilename = strHref.substring(iIdx+1);
	}
	else
	{
		strFilename = strHref;
	}

	// compare file name and set focus if this is true
	if (strFilename == "admin_login.php")
	    window.document.login.username.focus();
}


/**
 *	Check or uncheck several checkboxes
 *	@param object calling_object The object the caused this action
 *	@param objects[] An array with all concerning elements
 */
function check_all(calling_object, elements)
{
	for (var i = 0; i < elements.length; ++i)
		if (elements[i].type == 'checkbox')
			elements[i].checked = calling_object.checked;
}


/**
 *	Switch between several possibilities in the options on admin_newsletter.php
 *	@param int
 *	@return void
 */
function newsletter_options(val, elements)
{
	if (val == 3)
	{
		var selectedItems = 0;
		for (var i = 0; i < elements.length; ++i)
			if (elements[i].type == 'checkbox' && elements[i].checked == true)
				++selectedItems;

		if (selectedItems > 0)
		{
			document.form.action = 'admin_newsletter_loeschen.php';
			document.form.submit();
		}
	}
	else if (val == 4)
	{
		location.href = "admin_newsletter.php?show=spam";
	}
	else if (val == 5)
	{
		location.href = "admin_newsletter.php";
	}
}


/**
 *	Select a checkbox by clicking into another place than the checkbox itself.
 *	@param object The checkbox
 */
function selectCheckbox(param)
{
    if (param.checked == false)
        param.checked = true;
    else
        param.checked = false;
}


/**
 *	Select a radio button by clicking into another place than the button itself.
 *	@param object The radio buttom
 */
function selectRadio(param)
{
    if (param.checked == false)
        param.checked = true;
}
