/*
	egformsupp.js
    
    This file provides JavsScript methods to assist in working with
    forms.  It is provided free of charge by ecommguru.com, and may be
    used free of charge as long as this comment is not removed or altered 
    in any way.
    
    For purposes of small file size, you may remove any of the comments found
    below, unless you plan to distribute the file.  In this case, you must 
    distribute this file in the exact same format in which you obtained it.
*/

/******************************************************************************
Name:	egEnterSubmit
Descr:	Permits the submission of form data by pressing the ENTER/RETURN
   		key while in an text field.
Params:	e - event object (may be null in IE)
   		f - form to submit
Demo:	
<INPUT TYPE=TEXT onKeyPress='return egEnterSubmit(event, document.forms[0])' NAME=Login><BR>
*******************************************************************************/
function egEnterSubmit(e, f) 
{
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code==13) {
    	egDoSubmit( f );
		return false;
	}
	return true;
}

/******************************************************************************
Name:	egAlert
Descr:	Displays s in alert, if s is not blank 
Params:	s - String to display
*******************************************************************************/
function egAlert( s )
{
	if( s.length > 0 )
    	alert(s);
}

/******************************************************************************
Name:	egTextEmpty
Descr:	Validates for empty text input field, and optionally displays message
Params:	inp - Text type INPUT field to test
   		alertstr - String to display in alert if Inp is empty.  May be ''
Return:	true if inp is empty, false if not.
Demo:	
if( egTextEmpty( document.forms[0].Login, 'Login may nopt be empty' ) )
	return false;
*******************************************************************************/
function egTextEmpty( inp, alertstr )
{
	if( inp == null ) return false;
	s = inp.value;
	if( s == null )
		s = inp.text;
	s = s.replace( /\s+$/g, "" );
	if( s.length==0 )
    {
    	egAlert( alertstr );
        inp.focus();
    }

    return s.length == 0; 
}

/******************************************************************************
Name:	egSelectNoChoice
Descr:	Validates for SELECT selection, and optionally displays message.  The
		first item (zero) in the OPTION list must be the 'Make choice' selection.
Params:	inp - SELECT field to test.
   		alertstr - String to display in no selection is made.  May be ''
Return:	true if inp has no selection, false if it has a selection.
Demo:	
if( egSelectNoChoice( document.forms[0].Shipping, 'You must select a shipping method' ) )
	return false;
*******************************************************************************/
function egSelectNoChoice( inp, alertstr )
{
	if( inp==null )
		return false;
	if( inp.selectedIndex <= 0 )
    {
    	egAlert( alertstr );
        inp.focus();
        return true;
   	}
	return false;
}

/******************************************************************************
Name:	egTextBadEMail
Descr:	Validates INPUT TEXT for syntactically correct email value.  Note that
		it is impossible to perfectly identify valid email addresses, this script
        only tests to see if the address confirms to a name@domain.xxx format.
Params:	inp - INPUT field to test.
		required - true if email is required, false if blank is ok
   		alertstr - String to display text appears invalid.  May be ''
Return:	true if inp has a bad(or blank) email, false if email is acceptable.
Demo:	
if( egTextBadEMail( document.forms[0].EMail, 'You must enter a valid email address' ) )
	return false;
*******************************************************************************/
function egTextBadEMail( inp, required, alertstr )
{
	if( inp == null ) return false;
	s = inp.value;
	s = s.replace( /\s+$/g, "" );
    if( s.length == 0 && required==false )
    	return false;
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if( !filter.test( s ) || (s.length==0&&required==true))
    {
    	egAlert( alertstr );
		inp.focus();
		return true;
    }
	return false; 
}

/******************************************************************************
Name:	egTextAreaBadSize
Descr:	Validates minimum and maximum text lengths for a TEXTAREA field
Params:	inp - TEXTAREA field to test.
		minsize - Minimum text length to accept, 0 for no minimum.
        maxsize - Maximum text length to accept (can be large, like 1000000)
        minalertstr - String to display if text is < minsize.  May be ''.
   		maxertstr - String to display if text > maxsize.  May be ''
Return:	true if inp is < minsize or > maxsize.
Demo:	
if( egTextAreaBadSize( document.forms[0].Comment, 0, 255, '', 'Comments limited to 255 characters')
	return false;
*******************************************************************************/
function egTextAreaBadSize( inp, minsize, maxsize, minalertstr, maxalertstr )
{
	if( inp == null ) return false;	
	if( inp.value.length < minsize )
    {
    	egAlert( minalertstr );
        inp.focus();
        return true;
    }
    if( inp.value.length>maxsize )
    {
    	egAlert( maxalertstr );
        inp.focus();
        return true;
    }
	return false;    
}

/******************************************************************************
Name:	egDoSubmit
Descr:	Submits a form.  If the form has an onsubmit handler, it is called first.
Params:	f - Form to submit.
Return:	true if form submitted, or false if it failed validation (according to onsubmit).
Demo:	
<a href=# onClick="ecDoSubmit(document.forms[0] ); return false;">Submit</a>
*******************************************************************************/
function egDoSubmit( f )
{
	if( f.onsubmit ) {
		if( f.onsubmit() ) {
    		f.submit();
            return true;
        }
	}
    else {
        f.submit();
        return true;
    }
    return false;
}

/******************************************************************************
Name:	egCommaFormatted
Descr:	formats a numbre with commas
Params:	amount - number to format with commas
Return:	Formatted string
Demo:	
document.write( totalamt );
*******************************************************************************/
function egCommaFormatted(amount) {
   amount=Math.round(amount*100)/100;
	var str = amount+'', c=0, end='', ret='', decpos = str.indexOf('.');
	if( decpos >= 0 ) {
		end = str.substr( decpos, str.length-decpos );
		while( end.length < 3 )
			end = end + '0';
		str = str.substr( 0, decpos );
	}
	else
		end='.00';
	for( i= str.length-1; i >=0; i-- ) {
		if( c > 0 && (c%3)==0 )
			ret = ','+ret ;
		ret = str.charAt(i)+ret;
		c++;
	}	
	return( ret+end );
}

function egIsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++)
      if (ValidChars.indexOf( sText.charAt(i) ) == -1) 
         IsNumber = false;
   return IsNumber;
   
}
   
function egGetAsInt( Fld )
{
	sVal = 	Fld.value;
	if( egIsNumeric( Fld.value ) )
		return( Fld.value );
	return 0;
}

var egPopupWindow;
function egPopup( URL, Height, Width, Options )
{
	if( egPopupWindow )
		egPopupWindow.close();
	if( Height=='' )
		Height = 300;
	if( Width=='' )
		Width = 350;
	if( Options=='' )
		Options = 'resizable=1,scrollbars=1';
	egPopupWindow=window.open( URL, 'egPopupWindow','height='+Height+',width='+Width+','+Options);
	if (window.focus)
		egPopupWindow.focus();
	return false;
}
function egCloseUp()
{
	if( egPopupWindow )
		egPopupWindow.close();
}
