/*
 * 14.09.2003 Ralf Kramer <rk@apondos.net>* A collection of functions for validating html form contents
 *
 */


/**
 * Throws an alert if any of the mandatory field is empty
 *
 * The param mandatory_fields contains the id's of all mandatory fields.
 *
 * @author  Ralf Kramer
 * @param   string  mandatory_fields:  a list of mandatory form fields seperated by a whitespace
 * @return  boolean
 */
function hasEmptyFields( mandatory_fields, check_passwords, message )
{

    if( check_passwords )
        if( hasInvalidPasswords() )
            return true;

    field_array = mandatory_fields.split( " ");
    empty_fields = "";
    for( i = 0; i < field_array.length; i++ )
    {
        var field = document.getElementById( field_array[i] );
        if( field.value == "" )
        {
            field.style.border = '1px dotted red';
            empty_fields = empty_fields + field_array[i];
        }
        else
            field.style.border = '1px dotted green';
    }

    if( empty_fields.length > 2 )
    {
        alert( message );
        return true;
    }

    return false;
}

function checkEmail( message )
{
    if (document.getElementById("email").value=="")
    {
        alert(message);
        return false;
    }

    var regexp = /^[a-z][\w\-\.]*\w\@([\w\-]+\.)+[a-z]{2,7}$/i;

    if (document.getElementById("email").value!="" && document.getElementById("email").value.search(regexp) == -1)
    {
        alert(message);
        return false;
    }

    return true;
}


function checkEmailForElement(element_id, message)
{

    element = document.getElementById(element_id);

    var regexp = /^[a-z][\w\-\.]*\w\@([\w\-]+\.)+[a-z]{2,7}$/i;

    if (element.value!="" && element.value.search(regexp) == -1)
    {
        if(element.style.border != '1px dotted red')
        {
            border = element.style.border;
        }
        element.style.border = '1px dotted red';
        alert(message);
        return false;
    }
    else
    {
        element.style.border = border;

    }
    return true;
}

function hasInvalidPasswords()
{
    var password            = document.getElementById( 'password' );
    var confirm_password    = document.getElementById( 'confirm_password' );

    if( password.value != confirm_password.value  )
    {
        password.style.border           = '1px dotted red';
        confirm_password.style.border   = '1px dotted red';

        alert( 'Die Passwörter stimmen nicht überein' );
        return true;
    }

    return false;

}

function isNumber( element, warning )
{
    element = document.getElementById( element );

    if( element.value.match( '^[0-9]*$' ) == null )
    {
        this.isShown = true
        alert( warning );

        element.style.border    = '1px dotted red';
        element.value           = '';
    }
    else
    {
        if( this.isShown )
            element.style.border = '1px dotted green';
    }
}

function redirect( URL, width, height )
{
    if( width )
    {
        posx = (screen.width - width) / 2;
        posy = (screen.height - height) / 2;
        //alert( posx );
        //alert( posy );
        window.open(URL, "singlefenster", "screenX="+posx+",screenY="+posy+",width="+width+",height="+height+",status=yes,scrollbars=yes,menubar=no,directories=no,toolbar=no");
    }
    else
        window.location.href = URL;
}

function confirmDelete( url, question )
{
    if( confirm( question ) )
        window.location.href = url;
}

/**
 * Toggles the check state of a group of boxes
 *
 * Checkboxes must have an id attribute in the form cb0, cb1...
 * @param The number of box to 'check'
 * @param An alternative field name
 */
function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'cb';
  }
	var f = document.adminForm;
	var c = f.toggle.checked;
	var n2 = 0;
	for (i=0; i < n; i++)
	{
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}

	if (c)
	{
		document.adminForm.boxchecked.value = n2;
	}
	else
	{
		document.adminForm.boxchecked.value = 0;
	}
}

function isChecked(ischecked)
{
	if ( ischecked == true )
	{
		document.adminForm.boxchecked.value++;
	}
	else
	{
		document.adminForm.boxchecked.value--;
	}
}

function submitform( mode, confirmQuestion )
{
    if( confirmQuestion )
        if( !confirm( confirmQuestion ) )
            return;

	document.adminForm.mode.value=mode;
	try
	{
		document.adminForm.onsubmit();
	}
	catch(e){}
    	document.adminForm.submit();
}

/**
 * Counts how many chars are remaining to enter.
 *
 * Example of using:
 * <script language="JavaScript" type="text/javascript" src="chars_remain.js"></script>
 * Chars remaining: <span id="chars">128</span><br>
 * <textarea name="text" id="text" onKeyUp="countCharsRemaining(128, 'text', 'chars');"></textarea>
 *
 * @return  void
 * @param   int     max_chars       The maximum quantity of chars allowed to enter
 * @param   string  text_field      The id of the form field with the text
 * @param   string  counter_id      The id of the counter tag
 */
function countCharsRemaining( max_chars, text_field, counter_id )
{
    var newCounterValue = max_chars - document.getElementById(text_field).value.length;

    if (newCounterValue >= 0)
    {
        document.getElementById(counter_id).innerHTML = newCounterValue;
    }
    else
    {
        document.getElementById(text_field).value = document.getElementById(text_field).value.substr(0,max_chars);
        document.getElementById(counter_id).innerHTML = '0';
    }
}

function checkTandC( mandatory_fields, check_passwords, message, t_and_c_message )
{
    if( hasEmptyFields( mandatory_fields, check_passwords, message ) )
        return true;

    if( document.getElementById('t_and_c').checked == false )
    {
        alert( t_and_c_message );
        return true;
    }
    return false;
}

function minCharsRequired( field, numberMinChars, message )
{
    if( document.getElementById(field).value.length < numberMinChars )
        alert( message );
}

function magicCheckBox( cb_switch )
{
    var tmp_vals = cb_switch.name.split( '_' );
    var mtt_id = tmp_vals[2];

    var inputs = document.getElementsByTagName("input");

    for( i=0; i < inputs.length; i++ )
    {
        if( inputs[i].type == "checkbox" )
        {
            if( inputs[i].name == mtt_id + '[]' )
                inputs[i].checked = cb_switch.checked;
        }
    }
}


function popWindow_hp(theURL,vars,winName,htmlName,features) {
    eval(winName+"=window.open('"+theURL+'?'+vars+"','"+htmlName+"','"+features+"')");
    if (window.focus)
	eval(winName + ".focus()");
}