/*-----------------------------------------------------------------------------------------
   $Id:form_check.js 1071 2009-05-16 03:35:29Z macsIT $

   macs-Commerce
   http://www.macs-ITcom

   Copyright (c) 2009 macs-IT
-------------------------------------------------------------------------------------------*/
/* check a field if filled correctly */
function checkFormField(tForm,tFieldName,tErrorOutput)
{

                if( trim(tForm.tFieldName.value) == "" || tForm.tFieldName.value == tFieldName)
                {
                        alert(tErrorOutput);
                        tForm.tFieldName.focus();
                        return false;
                }


        return true;
}
/*-------------------------------------------------------------------------------------------*/

/* Check newsletter fields if filled correctly */
function checkNewsletterFormInput(oForm,fName,fEmail,oErrName,oErremail)
{

        // inserting?
        if(oForm.check.value == "inp")
        {

                if( trim(oForm.name.value) == "" || oForm.name.value == fName)
                {
                        alert(oErrName);
                        oForm.name.focus();
                        return false;
                }

                if( trim(oForm.email.value) == "" || oForm.email.value == fEmail || !CheckMailStr(trim(oForm.email.value)))
                {
                        alert(oErremail);
                        oForm.email.focus();
                        return false;
                }
        }
        // deleting?
        if(oForm.check.value == "del")
        {

                if( trim(oForm.email.value) == "" || oForm.email.value == fEmail || !CheckMailStr(trim(oForm.email.value)))
                {
                        alert(oErremail);
                        oForm.email.focus();
                        return false;
                }
        }
        return true;
}


/*-------------------------------------------------------------------------------------------*/

/* newsletter helpfunction checkmail entry  */
function CheckMailStr( str )
{
        // Field must not be empty!
        if( trim(str) == '' )
        {
                return(false);
        }
        // Auf Zeichen @
        if( str.indexOf("@") == -1 )
        {
                return(false);
        }
        // Auf Zeichen -.-
        if( str.indexOf(".") == -1 )
        {
                return(false);
        }
        return(true);
}

/*-------------------------------------------------------------------------------------------*/

/* Check newsletter help function to trim possible blanks away */
function trim( str )
{
        var n;
        var i;

        // count leading blanks, i = first index
        for( i = 0; i < str.length; i ++ )
        {
                if( str.charAt( i ) != " ") { break; }
        }

        // count trailing blanks  n = 2nd index
        for( n = str.length-1; n >= i; n -- )
        {
                if( str.charAt( n ) != " " ) { break; }
        }

        // select valid substring between i and n
        str=str.substring( i, n+1 );

        // return string
        return(str);

} // trim

