/* 
 * 14.09.2003 ralf kramer <rk@belisar.de>
 * 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 solid #000';
            empty_fields = empty_fields + field_array[i];
            field.style.backgroundColor = "#ffcece";
        }
        else
            //field.style.border = '1px dotted green';
            field.style.backgroundColor = "#ccffcc";
            
    }    
    
    if( empty_fields.length > 2 )
    {
        alert( message );
        return true;
    }
    
    return false;
}

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 )
{
    element = document.getElementById( element );
    
    if( element.value.match( '^[0-9]*$' ) == null )
    {
        this.isShown = true
        alert( 'Bitte geben Sie hier nur Zahlen ein.\n Andere Zeichen oder Buchstaben können nicht verarbeitet werden' );
        
        element.style.border    = '1px dotted red';
        element.value           = '';
        element.focus();
    }
    else
    {
        if( this.isShown )
            msisdn.style.border = '1px dotted green';
    }
}

function redirect( url )
{
    window.location.href = url;
}

function confirmDelete( url, question )
{
    if( confirm( question ) )
        window.location.href = url;
}

function setSelectedOption( select_id , option_value )
{   
    options = document.getElementById( select_id ).options;
        for( j = 0 ; j < options.length ; j ++ )
        {
            options[j].selected = false;
            if( options[j].value == option_value )
                options[j].selected = true;
        }        
}

function getSelectedText( select_id )
{
    select = $(select_id);
    for( var i = 0 ; i < select.options.length ; i++ )
        if( select.options[i].value > 0 && select.options[i].value == select.value )
            return select.options[i].innerHTML;
}

function clearSelect( select_id )
{
    var select = $(select_id);
    var length = select.options.length;
    for( var i = 0 ; i < length ; i++ )
        select.remove( select.options[i] );
}

function hideElement( id )
{
    document.getElementById( id ).style.display = "none";
}

function unhideElement( id )
{
    document.getElementById( id ).style.display = "inline";
}

// Ïðîâåêà ââîäèìîãî çíà÷åíèÿ íà öèôðû. Èñïîëüçîâàòü â ñîáûòèè onkeyup="check_numeral(this)";
function check_numeral(m){
  var strTable="01234567890";
  var strRet = "";
  var cTmp, nTmp;
  for(i=0;i<m.value.length; i++){	
    cTmp = m.value.charAt(i);
	nTmp = strTable.indexOf(cTmp);
	if (cTmp==',')cTmp='.';
	if (nTmp >= 0) strRet += cTmp;		
  }
  if (m.value != strRet) m.value = strRet;
}

function check_num_cut(m,count,minval,maxval){
  var strTable="01234567890";
  var strRet = "";
  var cTmp, nTmp;
  for(i=0;i<m.value.length; i++){	
    if(i==count) break; 
    cTmp = m.value.charAt(i);
	nTmp = strTable.indexOf(cTmp);
	if (cTmp==',')cTmp='.';
	if (nTmp >= 0) strRet += cTmp;		
  }
  if (m.value != strRet) m.value = strRet;
}

function setCursor( element , value )
{
    element.style.cursor = value;
    while( element.parentElement != undefined )
    {
        element.parentElement.style.cursor = value;
        element = element.parentElement;
    }
}