function enable_form_text_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).readOnly =false;
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).readOnly =false;
	}
	
}

function enable_form_radio_buttons( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			if ( coll.item(i).type=='radio')
				coll.item(i).disabled =false;
	}
}

function enable_form_check_boxes( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			if ( coll.item(i).type=='checkbox')
				coll.item(i).disabled =false;
	}
}

function disable_form_text_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).readOnly = true;
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).readOnly = true;
	}
}

function disable_form_radio_buttons( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			if ( coll.item(i).type=='radio')
				coll.item(i).disabled =true;
	}
}

function disable_form_check_boxes( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			if ( coll.item(i).type=='checkbox')
				coll.item(i).disabled =true;
	}
}

function assign_text_change_event_form_text_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++)	
			if ( coll.item(i).type=='text')
				coll.item(i).onchange =text_change;
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).onchange =text_change;
	}
}

function assign_click_event_form_check_box_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++)	
			if ( coll.item(i).type=='checkbox')
				coll.item(i).onclick =check_box_click;
	}
}

function assign_key_press_event_form_text_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++)	
			if ( coll.item(i).type=='text' || coll.item(i).type=='TEXT')
				coll.item(i).onkeypress = check_numeric_key_press;
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).onkeypress = check_numeric_key_press;
	}
}

function convert_all_to_number(form_object)
{

	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++)	
			if ( coll.item(i).type=='text')
				coll.item(i).value = convert_to_number (coll.item(i).value );
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).value = convert_to_number (coll.item(i).value );
	}
}


function remove_spaces_from_text_elements( form_object)
{
	var coll = form_object.elements.tags('INPUT');
	var i;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).value = remove_spaces (coll.item(i).value);
	}
	
	//TEXT AREA!
	coll = form_object.elements.tags('TEXTAREA');
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
			coll.item(i).value = remove_spaces (coll.item(i).value);
	}
	
}


function convert_to_short_date (date_in)
{
	var result = new Date(date_in);
	var month_num = result.getMonth() + 1;
	month_num = month_num.toString();
	var date_num = result.getDate().toString();
	if (month_num.length<2) month_num = '0' + month_num;
	if (date_num.length<2) date_num = '0' + date_num;
	return (month_num + '/' +  date_num + '/' + result.getFullYear());
}

function show_confirm(prompt_text, window_title, show_ok_button_only)
{
	if (window_title=='')	window_title = 'Confirm';
	
	window.document.body.style.cursor='default';
	

	var vParams = new Array(prompt_text, window_title, show_ok_button_only);
	var vSetting = 'dialogWidth:500px;dialogHeight=200px;center:yes;status:no;help:no';

	var return_value = window.showModalDialog('./includes/confirm.htm' , vParams , vSetting)
	
	if (!return_value) return_value=false;
	
	window.document.body.style.cursor='default';
	return return_value;
}


function check_phone_number( phone_number)
{
	var phonexp =  /^\d{10}$/;
	return is_valid(phonexp,phone_number);
}

function check_zip_code( zip_code)
{
	var zipexp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/;
	return is_valid(zipexp,zip_code);
}

// ****************************************************************** 
function check_routing_number( routing_number)
{
	var routingexp = /^\d{9}$/;
	routing_number = remove_non_digits(routing_number) ;
	if ( is_valid(routingexp,routing_number) )
	{
		if ( ! bank_check_routing_number (routing_number))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function check_email( email_address)
{
	//var emailexp = /^[a-z][a-z_0-9\.]+@[-_a-z_0-9\.]+\.[a-z]{3}$/i;
	//var emailexp = /^[a-z][a-z_0-9\.]+@[-_a-z_0-9\.]+\.[a-z]{3}$/i;
	var emailexp = /^[A-Za-z_0-9\.]+@[-_A-Za-z_0-9\.]+\.[A-Za-z]{3}$/i;

	return is_valid(emailexp,email_address);
}

function check_time_input( time_value)
{
	if ( time_value.indexOf (':')==-1) return false;
	var hours = parseInt ( time_value.substring (0,time_value.indexOf (':'))) ;
	
	if ( isNaN ( hours) ) return false;
	if (hours >24 || hours <0) return false;
	
	var mins = parseInt ( time_value.substring (time_value.indexOf (':')+1)) ;
	if (mins >59 || mins <0) return false;
	
	return true;
}

function is_valid(pattern, str) {return pattern.test(str)}

function remove_spaces(str)  {	return str.replace(/[ ]/g,'')}

function remove_non_digits(str, leave_dash)  
{	
	if (leave_dash==1)
		return str.replace(/[^0123456789-]/g,'')
	else
		return str.replace(/[^0-9]/g,'')
}

function convert_to_number(str)  
{	
	var out_str =str.replace(/[^0-9.]/g,'');
	if (out_str=='') out_str=0;
	return out_str;
}

function fix_quote_problem(str)
{
	var out_str =str.replace(/'/g,"''");
	out_str = out_str.replace(/"/g,"''");
	return out_str;
}

//****************************************************************************************
function check_numeric_key_press () 
{	
	var numbers = '0123456789.';
	numbers = numbers + String.fromCharCode(8)+String.fromCharCode(27)+String.fromCharCode(13);
	if ( numbers.indexOf (String.fromCharCode(window.event.keyCode))==-1 ) 	{window.event.returnValue=false;};
	
	//alert (window.event.keyCode + ' = ' + String.fromCharCode(window.event.keyCode) + ' ['+  window.event.srcElement.value +' ]');
	if ( window.event.srcElement.value.indexOf ('.')!=-1 && String.fromCharCode(window.event.keyCode)=='.' )
			{window.event.returnValue=false;}
}

function check_digit_only_key_press (allow_dash) 
{
	var numbers = '0123456789';
	if (allow_dash == 1){numbers = numbers +'-';}
		
	numbers = numbers + String.fromCharCode(8)+String.fromCharCode(27)+String.fromCharCode(13);
	if ( numbers.indexOf (String.fromCharCode(window.event.keyCode))==-1 ) 	{window.event.returnValue=false;}
	if ( window.event.srcElement.value.indexOf ('.')!=-1 && String.fromCharCode(window.event.keyCode)=='.' )
		{window.event.returnValue=false;}
}

//****************************************************************************************
function clear_list (list_object) {	while ( list_object.options.length)	list_object.options[0]=null;}
//
function add_to_list (from_object, to_list_object, perform_lookup_merchant, user_id, add_text_value) 
{	
	if (perform_lookup_merchant==1)
	{
		lookup_merchant(user_id);
		if (document.report_form.merchant_id.value==0) return;
	}
	
	if (from_object.selectedIndex==-1) 
	{
		alert ('Please select item to add!');
		return;
	}
	
	//check if have in the list first
	if ( to_list_object.options.length>0) 
	{
		for (var i=0; i<  to_list_object.options.length; i++)
		{
			if ( to_list_object.options(i).value== from_object.value)
			{
				alert ('Item [' +  from_object.value + '] already in the list!');
				return;
			}
		}
	}
	
	//add to the list
	var option_element = document.createElement("OPTION");
	to_list_object.options.add(option_element);
	option_element.value = from_object.value;
	if (add_text_value==1)
		option_element.innerText  = from_object.options[from_object.selectedIndex].text;
	else
		option_element.innerText  = from_object.value;
	return;
	
}

function remove_from_list (from_list_object, remove_very_last_item) 
{	
	if (remove_very_last_item==1)
	{
		//do nothing		
	}
	else
	{
		if ( from_list_object.options.length==0 || from_list_object.selectedIndex==0) return;
	}
	
	if (from_list_object.selectedIndex==-1) 
	{
		alert ('Please select item to remove!');
		return;
	}
	
	//remove from the list
	from_list_object.options.remove (from_list_object.selectedIndex);
	return;
	
}

function copy_list_values_to_text (from_list_object, to_text_object) 
{	
	to_text_object.value='';
	
	//check if have in the list first
	if ( from_list_object.options.length>0) 
	{
		for (var i=0; i<from_list_object.options.length; i++)
		{
			if ( from_list_object.options(i).value!='')
			{
				if (to_text_object.value !='') to_text_object.value=to_text_object.value+',';
				to_text_object.value=to_text_object.value+ from_list_object.options(i).value;
			}
		}
	}
	return;
	
}
//****************************************************************************************
function bank_check_routing_number( routing_number)
{
    routing_number = remove_non_digits (routing_number );
   
    if ( routing_number.length !=9) return false;
    
    //Do the modulus test
	var sum=0;
	var char_at;
	var x=0;
	var temp = new Array(2);
    for (x=0; x<8; ++x)
	{
		char_at = routing_number.charAt(x);
		y = parseInt (char_at);
		switch (x)
		{
			case 0:  sum += (y * 3); break
			case 1:  sum += (y * 7); break
			case 2:  sum += (y * 1); break
		    case 3:  sum += (y * 3); break
		    case 4:  sum += (y * 7); break
		    case 5:  sum += (y * 1); break
		    case 6:  sum += (y * 3); break
		    case 7:  sum += (y * 7); break
		}
	}
    char_at = routing_number.charAt(8);
	y = parseInt (char_at);
    var checkDigit = 0;
    //alert ('sum=' + sum + ' ' + (sum % 10) );
	if ((sum % 10) > 0)  checkDigit = 10 - (sum % 10);
	if (checkDigit != y) return false;
    return true;

}

//****************************************************************************************
function show_bank_info( routing_number)
{
	var page_url;
	page_url = '../bank_info/show_bank_info.asp?routing=' + routing_number;
	
	var features='dialogHeight: 500px; dialogWidth: 980px; dialogTop: px; dialogLeft: px;'+
					'edge: Sunken; center: Yes; resizable: Yes; status: No;'+
					'help: No;unadorned: No;';
	
	//alert(page_url);
	//window.open(page_url);
	window.showModalDialog ( page_url,null,features);
}

// ****************************************************************** 
function check_state( state)
{	
	state = state.toUpperCase();
	var states = new Array('AK'	,'AL'	,'AR'	,'AZ'	,'CA'	,'CO'	,'CT'	,'DC'	,'DE'	,'FL'
							,'GA'	,'GU'	,'HI'	,'IA'	,'ID'	,'IL'	,'IN'	,'KS'	,'KY'
							,'LA'	,'MA'	,'MD'	,'ME'	,'MI'	,'MN'	,'MO'	,'MS'	,'MT'
							,'NC'	,'ND'	,'NE'	,'NH'	,'NJ'	,'NM'	,'NV'	,'NY'	,'OH'
							,'OK'	,'OR'	,'PA'	,'PR'	,'RI'	,'SC'	,'SD'	,'TN'	,'TX'
							,'UT'	,'VA'	,'VI'	,'VT'	,'WA'	,'WI'	,'WV'	,'WY'
						);
						
	for (var i=0;i<=states.length;i++)
		if (states[i]==state){return true;}

	return false;
}


//****************************************************************************************
function verify_bank_info_with_database(user_id_value, routing_number_value, routing_number_form_element)
{
	
	//lookup!
	var lookup_bank_info;
	lookup_bank_info=RSExecute("../includes/remote_calls.asp",
								"lookup_bank_info",
								user_id_value, 
								routing_number_value	);
	//get data:
	var data_array;
	//alert (lookup_bank_info.return_value);
	data_array= lookup_bank_info.return_value;
	if (! data_array) 
	{
		alert ('Error: Routing #[' + routing_number_value + '] not found in database!');
		return false;
	}
	var ach_routing_value = data_array[0][6];
	
	if ( ach_routing_value!= routing_number_value)
	{
		if ( show_confirm('ACH Routing is different than Routing # you entered.<BR>Would you like to change to ACH Routing #?'))
		{
			routing_number_form_element.value= ach_routing_value;
		}
	}
	return true;
}