/*************** GENERIC validation helping functions ***********************************
* Do not modify without fully understanding ALL forms dependencies.

* Will display instant feedback to user when leaving each form field, to assits with valid inputs.
*
*
** Author: Yanay
*/

	 
function update_field_msg( field, msg ){
// usually display error msgs, regarding the form field's input after 'onblur'...
    
   
    var field_name      = field.name;
    var output_span_id  = field_name.substr( 0, field_name.length-6 ) + "_msg"; // remove original '_field', and '_msg'
   

	$( '#'+output_span_id ).addClass('form-err');
    $( '#'+output_span_id ).html( msg );
    
}


	 
	 
// various helper-functions
//---------------------------------------------------------------
function isString( strValue )
{
  return (typeof strValue == 'string' && strValue != '' && isNaN(strValue));
}

function isNumber( strValue )
{
  return (!isNaN(strValue) && strValue != '');
}


function isLettersOnly( strValue ){
// letters & *spaces* only!

	reg = /[^A-Za-z ]/i;
	
	if( strValue.search(reg) == -1 )
		return true;
	else
		return false;
}





// field-type main validations:
//---------------------------------------------------------------

function checkEmail( field )
//---------------------------
{
 
 
 var error = "";
 var strValue = field.value;
 var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
 
 if( strValue.length <=1 )
 {
        error = phrase("REQUIRED");
 } 
 else
 
 if(!objRE.test(strValue) )
	{
	    error = phrase("INVALID");
    }
	
	update_field_msg( field, error );
	
  return error;	 
}



function checkWebsite( field, required )
//---------------------------------------
{

 
 var error = "";
 var strValue = field.value;
 var objRE = /^www\.[A-Za-z0-9_-]+\.[A-Za-z]+/;
 
 if( !required && strValue=='')
    error = ""; // left blank, when not required. OK
 else
 if( required && strValue.length <=1 ){
 			error = phrase("REQUIRED");
 } 
 else
 if( !(strValue!='' && objRE.test(strValue)) )
	{
			 error = "Use \"www.\"...";
  }
		
  update_field_msg( field, error );
	
	return error;	 
}




function checkPhone(field, must)
//--------------------------------
{
    
    
    var error="";
		
    var stripped = field.value.replace(/[\(\)\.\-\ ]/g, '');
    //strip out acceptable non-numeric characters
		
    
    if( !must && stripped=='' )
      error = "";
    else
    {
        if ( stripped.length < 7 ) {
    	 	    error = phrase("INVALID");
        }
		    else
        if ( isNaN(parseInt(stripped))) {
			      error = phrase("INVALID");
        }
    }
		
		update_field_msg( field, error );
		
		return error;
}


function checkCC(field, must)
//--------------------------------
{
    
    
    var error="";
        
    var stripped = field.value.replace(/[\-\ ]/g, '');
    //strip out acceptable non-numeric characters
        
    
    if( !must && stripped=='' )
      error = "";
    else
    if ( stripped.length < 8 ) {
             error = phrase("INVALID");
    }
        else
    if ( isNaN(parseInt(stripped))) {
              error = phrase("INVALID");
    }
        
        update_field_msg( field, error );
        
        return error;
}



function notEmpty( field, lettersOnly, min_length, max_length )
//-------------------------------------------------
// at least 2 chars
{
        var error="";
        
        if(field===undefined) return "";
        
		var len = field.value.length;
		
		if( field.value=='' || field.value==' ')
		  error = phrase("REQUIRED");
		  
		else
		
		if( min_length && (len < min_length || len > max_length) )  
      error = "Use " + min_length + "-" + max_length + " Characters";
		
		else

		if(lettersOnly)
		{
			 var passed = isLettersOnly( field.value );
			 
			 if(!passed)
			 			error = 'Letter only, please';
		 }
         
		
		
	   update_field_msg( field, error );
		
		return error;
}



function mustSelect( field )
//-------------------------------------------------
// at least 2 chars
{
        
        error="";

		selected_index = field.selectedIndex;
		
		selected_value = field.options[selected_index].value;
		
    if( selected_value == '--' || selected_value == '' || selected_value == ' ')
		{
            error = phrase("REQUIRED");
		}
		
		update_field_msg( field, error );
		
		return error;

}

function matchOtherField( field, other_field, other_field_caption ){
  
  
  other_field = other_field + '_field';
  error = "";
  
  if( !field.value )
    error = phrase("REQUIRED");
  
  else
  
  if( field.value != other_field.value )
    error = "Must be same as " + other_field_caption;
    
  update_field_msg( field, error );
    
  return error; 

}



function checkBoxAny( fields, field_name ){
//-----------------------------------------
// will check if at least one of the boxes listed as (form.box_option1,form.box_option2,form.formbox_option3,...) is checked.

     var checked = 0;
	 error = "";
	 var i;
	 
	 for( i=0; i<fields.length; i++)
	 {
             //console.log( fields[i] );
             if( fields[i].checked )
    		 {
    				checked++;
    		 }
	 }
 
	 if(checked <1 )
	 {
	 				error = '[' + field_name + '] requires at least one selection. \n';
					if(!focus_set) { focus_set=true; fields[0].focus(); }
					fields[0].className = fields_error_class;
	 }
	 
	 return error; 
 
}




