bName = navigator.appName;
bVer = parseInt(navigator.appVersion);
if ((bName == "Netscape" && bVer >= 3) ||
(bName == "Microsoft Internet Explorer" && bVer >= 4)) br = "n3";
else br = "n2";

function unhide(x){
// parameter - id of element to unhide
	var xx = document.getElementById(x)
	if(xx){
		xx.style.display = '';
	}
}
function hide(x){
// parameter - id of element to hide
	var xx = document.getElementById(x)
	if(xx){
		xx.style.display = 'hidden';
	}
}

function check_selectbox_value (x,v) {
  var i;
  for (i = 0; i < x.options.length; i++) {
  	if (x.options[i].value == v)
		return true;
  }
  return false;
}

function get_selectbox_index (x) {
  var i;
  for (i = 0; i < x.options.length; i++) {
  	if (x.options[i].selected)
		return i;
  }

}
function get_selectbox_value (x) {
  var i;
  for (i = 0; i < x.options.length; i++) {
  	if (x.options[i].selected)
		return x.options[i].value;
  }

}

function set_selectbox_value (x, val) {
  var i
  for (i = 0; i < x.options.length; i++) {
  	if (x.options[i].value == val)
		x.options[i].selected = true;
  }

}


function val_url(url,set_focus) {
// parameter url - object being validated
	if (url.className == 'error') {
		url.className = '';
	}

  if (isblank(url.value))
    return true;
	
  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';
	
	
var urlVal = url.value
	
	//Remove Leading whitespace
	while(urlVal.charAt(0) == ' ')
		{
		// alert('URL' + urlVal)
		 urlVal = urlVal.substring(1, urlVal.length)
		 url.value = urlVal;
		}

	var url_start = urlVal.substring(0, 4).toUpperCase()
	
	if(url_start=="WWW.")
		{
		var urlVal = "http://" + url.value
		url.value = "http://" + url.value
		}	



	
	var u = urlVal
	var w_error = false;

	var x = u.indexOf('http:')
	if (x != 0)
		{
		var x = u.indexOf('https:');
		
		if(x != 0)
			{
				w_error = true;
				
			}
		u = u.substr(1, u.length-1)
		}
	
	
 		var y = u.substr(5,u.length - 6);
		
 		var b = y.indexOf('/')
		
		if (b != 0)
			{
			w_error = true;	
			}
		
		y = u.substr(6, u.length - 7)
		b = y.indexOf('/')
		
		if(b != 0)
			{
			w_error = true;
			}	
		
		var z = y.indexOf('.');
 		if (z <= 1 || z == y.length - 1)
 			w_error = true;	

	if (w_error){
		display_error(url,'Invalid url address. Use http:' + '/' + '/' + '*.* or https:' + '/' + '/' + '*.* format',w_set_focus);
		return false;
	}
	else
		return true;
}

 function dollarformat (x) {
  if (x == 0) return '';
  var y = Math.round((x - 0) * 100);
  var z = y + '';
  if (z.length == 1){
  	z = '0' + z;
  }
  if (z.length == 2 && y < 0){
  	z = '-0' + z.substr(1,1)
  }
  return z.substr(0,z.length-2) + '.' + z.substr(z.length-2,2)

}

function nothing() {
	return;
}

function return_value(return_fieldname,desc_fieldname,object_value,desc_value,reload,close_window) {
 	var f = parent.window.opener.document.forms[0];
	for (var i = 0; i < f.length; i++) {
  		var e = f.elements[i];
		if (e.name == return_fieldname && !(e.disabled)) {
			e.value = object_value;
			if (reload != 'yes') {
				if (e.onchange) {
					var oc = e.onchange.toString();
					oc = oc.toUpperCase();
					if (oc.indexOf('REFRESH()') < 0){
						e.onblur = e.onchange;
					}
				}
				e.focus();
			}
		}
		
		if (e.name == desc_fieldname && !(e.disabled)) {
			e.value = desc_value;
		}
	} 


	if (reload == 'yes') {
		var x = parent.window.opener.document.getElementById('wait');
		if (x) {
			x.style.display = '';
		}
		parent.window.opener.document.forms[0].submit();
	}
	if (! (close_window) || close_window =='yes'){
		parent.self.close();
	}
	else {
	parent.window.opener.focus();
	}

}
 



function dolkupwindow(windowurl,reload,lookup_field,windowname,source,close_window) { 
// parameters: windowurl: url of page to execute in new window
//             reload: Yes if you want calling page submitted when return value is selected
//             lookup_field: name of field on calling page to set with selected value
//             windowname: name of new window opened
//             source: name of page to execute when value is selected. - use this if multiple data fields need to be returned.

// Note: pass in p_1st_time=n  in the windowurl to have the lookup window load detail immediately

	lookup_field.onblur = 'nothing();';
	if (windowurl.indexOf('p_1st_time=n') < 0){ 
		var p_1st_time = 'p_1st_time=y';
	}
	else {
		windowurl = windowurl.replace('p_1st_time=n','');
		var p_1st_time = '';
	}	
	var x = /\?/;
	if (x.test(windowurl)) {
	    var w = windowurl + '\&' + p_1st_time + '\&' + 'p_return_fieldname=' + lookup_field.name + '\&' +'p_reload=' + reload;
	}
	else {
	    var w = windowurl + '?' + p_1st_time + '\&' + 'p_return_fieldname=' + lookup_field.name + '\&' +'p_reload=' + reload;
	}
	if (source && source != null) {
	  w = w  + '\&' +'p_redirect=' + source; 
	}
	if (close_window && close_window != null)
		w = w +  '\&' + 'p_close_window=' + close_window;		
    lkup_window = window.open(w,windowname,'channelmode,width=800,height=600,toolbar=0,location=0,directories=0,menuBar=1,scrollBars=1,resizable=1,status=yes' ) ; 
	return;
} 


/*function doaddwindow(windowurl,add_field,windowname) { 
	add_field.onblur = 'nothing();';
	var x = /\?/;
	if (x.test(windowurl)) {
	    var w = windowurl + '\&' + 'p_1st_time=y' + '\&' + 'p_return_fieldname=' + add_field.name;
	}
	else {
	    var w = windowurl + '?p_1st_time=y' + '\&' + 'p_return_fieldname=' + add_field.name
	}
    newwindow = window.open(w,windowname,'channelmode,width=800,height=600,toolbar=0,location=0,directories=0,menuBar=1,scrollBars=0,resizable=1,status=yes' ) ; 
	return;
} 
*/


function doopenwindow(ref,win) {
	if(win){
		var w_window_name = win;
	}
	else{
		var w_window_name = ref;
		w_window_name = w_window_name.substr(0,4);
	}
    var w = escape(ref);	
    newwindow = window.open(ref,w_window_name,'width=1100, height=750, left=10, top=10, status=yes,resizable=1, scrollBars=1') ; 
	return;
}
function dosendwindow(ref) {
	var w_window_name = ref;
	w_window_name = w_window_name.substr(0,4);
	
    newwindow = window.open(ref,w_window_name) ; 
	return;
}

function isblank(s) {
  if (s == null) return true;
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
	if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
	}


function validate_form(f) {
for (var i = 0; i < f.length; i++) {
  var e = f.elements[i];
  var cls = e.className;
  if (e.onclick){
    if (((e.type == "text") || (e.type == "password") || (e.type == "textarea") || (e.type == "file")  || (e.type == "select-one"))) { 
	  var x = e.onclick.toString();
	  x = x.substring(x.indexOf('{') + 1,x.indexOf('}'))
	  y = x.toUpperCase();
	  if (y.indexOf('REQUIRED(') > -1){
		var msg = x.substring(x.indexOf('(') + 2,x.indexOf(')') - 1);		
		if (e.type == 'select-one'){
			if (isblank(get_selectbox_value(e))) {
			  if (msg != '()')
			    alert(msg);
		   	  else
			  	alert('This field is required')
		      e.focus();
			  return false; 
			}
		}
		else {
			if (isblank(e.value)) {
			  if (msg != '()')
			    alert(msg);
		   	  else
			  	alert('This field is required')
		      e.focus();
			  return false; 
		  	}
		}
	  }
	}
  }
  else{
	    var cls = e.className;
	    if (cls.toUpperCase() == 'REQUIRED' || cls.toUpperCase() == 'REQUIREDFIELD' || cls.toUpperCase() == 'SMALLREQUIREDFIELD' || cls.toUpperCase() == 'SMALL9REQUIREDFIELD'){
	      if (((e.type == "text") || (e.type == "password") || (e.type == "textarea") || (e.type == "file")  || (e.type == "select-one"))) { 
			if (e.type == 'select-one'){
				if (isblank(get_selectbox_value(e))) {
		  	      alert(e.name.replace(/_/g,' ') + " must be selected.");
			      e.focus();
				  return false; 
				}
			}
			else {
				if (isblank(e.value)) {
			      alert(e.name.replace(/_/g,' ') + " is required");
			      e.focus();
				  return false; 
			  	}
			}
		  }
	    }
	}  
}
if (!(additional_edits(f)))
  return false;
return true;

}
	
function required(){
}
	

function refresh(name) {
  unhide('wait');
  if (name) {
		var s = 'document.' + name + '.myaction';
		if(eval(s)) {
			s = s + '.value = ' + "'" + "'";
			eval(s);		
		}
			
		s = 'document.' + name + '.submit()';
		eval(s);
  }
  else {
		if (document.forms[0].myaction){
			document.forms[0].myaction.value = '';
		}	
		document.forms[0].submit();
	}
}

function calendar(fieldname)
   {
      var newWind=window.open('/calendar.cfm?name=' + escape(fieldname),'remote','width=350,height=250');
      if (newWind.opener == null)
         { newWind.opener = window; }
}
function display_error(x,msg,set_focus) {
// x is object with the error, msg is the error message to display

  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';

  if (w_set_focus == 'Y'){
	x.focus();
	if (x.type != 'select-one'){
 		x.select();
	}
  }
  x.className = 'error';
  alert(msg);
}
 
function val_email(email,set_focus) {
// parameter email - object being validated
 	if (email.className == 'error') {
		email.className = '';
	}

  if (isblank(email.value))
    return true;

  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';
	
	var e = email.value;
	var w_error = false;

	var x = e.indexOf('@');
	if (x <= 0)
		w_error = true;
	else {
 		var y = e.substr(x+1,e.length - x);
 		var z = y.indexOf('.');
 		if (z <= 0 || z == y.length - 1)
 			w_error = true;
	}

	if (w_error){
		alert('Invalid email address. Use *@*.* format');
		email.select();
		if(w_set_focus == 'Y'){
			email.focus();
		}
		return false;
	}
	else
		return true;
}

function val_dollaramt(x,set_focus,positive_flag,min_value,max_value) {
  if (x.className == 'error') {
  	x.className = '';
  }
  var w_amt = x.value;
  x.value = w_amt.replace(/,/g,"");
  w_amt = x.value;
  x.value = w_amt.replace(/\$/g,"");
  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';

  if (positive_flag)
  	var w_positive = positive_flag;
  else
    var w_positive = 'Y'	
	
  if (min_value)
  	var w_min_value = min_value;
  else
  	var w_min_value = null;

  if (max_value)
  	var w_max_value = max_value;
  else
  	var w_max_value = null;
	
   if (val_number(x,'N',w_positive,null,w_min_value,w_max_value,2,w_set_focus)){
   		if (!(isblank(x.value)))
	   		x.value = dollarformat(x.value);
		return true;
   }
   else
      return false;

}


function val_number(x,integer_flag,positive_flag,desc,min_value,max_value,decimals,set_focus){

// integer flag  Y/N defaults to Y
// positive flag Y/N default to Y
// decimals - the number of digits after the decimal point allowed
  if (x.className == 'error') {
  	x.className = '';
  }

  if (isblank(x.value))
    return true;

  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';

  var w_string = x.value;
  w_string = w_string.replace(/ /g,''); 
  x.value = w_string;
  
  w_string = w_string.replace(/,/g,''); 

  if (integer_flag && integer_flag != null)
	  var w_integer_flag = integer_flag;
  else
  	var w_integer_flag = 'Y';

  if (positive_flag && positive_flag != null)
	  var w_positive_flag = positive_flag;
  else
	  var w_positive_flag = 'Y';
  
  var w_valid_digits = '0123456789';
  if (w_integer_flag.toUpperCase() != 'Y')
	 w_valid_digits += '.' ;
  if (w_positive_flag.toUpperCase() != 'Y')
	 w_valid_digits += '-' ;

  if (w_positive_flag.toUpperCase() == 'Y' && x.value < 0){
		display_error(x,'Enter a positive number',w_set_focus);
		return false;
  }

	  
  if (desc && desc != null)
    var w_desc = desc;
  else if (w_integer_flag.toUpperCase() == 'Y') {
    var w_desc = 'integer';
	}
  else {
    var w_desc = 'number';
	}
	
  for (i = 0; i<w_string.length; i++) {
  	w_digit = w_string.substr(i,1)
	if (w_valid_digits.indexOf(w_digit) == -1){
		display_error(x,'Enter a valid' + ' ' + w_desc,w_set_focus);
		return false;
	}
  }

  if (isNaN(x.value - 0)) {
		display_error(x,'Enter a valid number',w_set_focus);
		return false;
  }
  
  var temp_x = (x.value - 0)
 
  if (min_value && min_value != null && temp_x < min_value){
		display_error(x,'Enter a value greater than or equal to ' + min_value,w_set_focus);
		return false;
  }
  
  if (max_value && max_value != null && temp_x > max_value){
		display_error(x,'Enter a value less than or equal to ' + max_value,w_set_focus);
		return false;
  }
  if (decimals && decimals != null && w_integer_flag != 'Y'){
  		if (w_string.indexOf('.') != -1 &&(w_string.length - w_string.indexOf('.') - 1) > decimals){
		var w_decimal_msg = 'Invalid entry. Only';
		w_decimal_msg = w_decimal_msg + ' ' + decimals + ' decimals_allowed';
		display_error(x,w_decimal_msg,w_set_focus);
		return false;
		} 
 }
  
 return true;
}

function val_ssn(x) {
  if (x.className == 'error') {
  	x.className = '';
  }

  if (isblank(x.value))
    return true;

  var w_string = x.value;
  if (w_string.length == 11){
  	if (w_string.substr(3,1) != '-' || w_string.substr(6,1) != '-') {
		display_error(x,'Invalid SSN. Use xxx-xx-xxxx format');
		return false;
	}
	w_string = w_string.replace(/-/g,''); 
    var w_valid_digits = '0123456789';

  }
  else if (w_string.length == 9) {
	  var w_valid_digits = '0123456789';
  }
  else {
	display_error(x,'Invalid SSN. Use xxx-xx-xxxx format');
	return false;
  }
  for (i = 0; i<w_string.length; i++) {
  	w_digit = w_string.substr(i,1)
	if (w_valid_digits.indexOf(w_digit) == -1){
		display_error(x,'Invalid SSN. Use xxx-xx-xxxx format');
		return false;
	}
  }
  x.value = w_string.substr(0,3) + '-' + w_string.substr(3,2) + '-' + w_string.substr(5,4);
 
  return true;
}



function val_zip_code(x) {
  if (x.className == 'error') {
  	x.className = '';
  }

  if (isblank(x.value))
  	return true;
	
  var w_string = x.value;
  
  if (w_string.length != 5 && w_string.length != 9 && w_string.length != 10){
	display_error(x,'Enter a 5 digit zip code or 9 digit zip code');
	return false;
  }
  var w_valid_digits = '0123456789';
  for (var i = 0; i < w_string.length; i++) {
  	var w_digit = w_string.substr(i,1)
	if (w_valid_digits.indexOf(w_digit) == -1){
		if(w_digit != '-' || i != 5 || w_string.length != 10){
			display_error(x,'Invalid zip code.');
			return false;
		}
	}
  }
   if (w_string.length == 10 && w_string.substr(5,1) != '-'){
		display_error(x,'Invalid zip code.');
		return false;
   }

  if(w_string.length == 9){
 	x.value = w_string.substr(0,5) + '-' + w_string.substr(5,4);
  }
  return true;
}


function compare_date(begin_date, end_date, msg, set_focus, curr_date_begin, curr_date_end, equal_allowed) {
// begin_date, end_date objects are required
// msg - will override default error message
// set_focus = 'Y' or null or not passed will set focus to end_date for error
// curr_date_begin - 'Y' or not passed then begin date must be Greater than current date
// curr_date_end - 'Y' or not passed then end date must be Greater than current date
// equal_allowed - if begin date cannot equal end date the pass 'N'

  if (!(val_date(begin_date,curr_date_begin,null,set_focus)))
 	return false;
  if (!(val_date(end_date,curr_date_end,null,set_focus)))
  	return false;

  if (isblank(begin_date.value))
    return true;
  if (isblank(end_date.value)){
  	return true;
  }
  
  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'N'; 

  var w_begin_date_text = begin_date.value;
  var w_end_date_text = end_date.value;
	
  w_begin_date = new Date(w_begin_date_text.substr(6,2),w_begin_date_text.substr(0,2),w_begin_date_text.substr(3,2));
  w_end_date = new Date(w_end_date_text.substr(6,2),w_end_date_text.substr(0,2),w_end_date_text.substr(3,2));

  if (equal_allowed && equal_allowed == 'N') {
	  if (w_begin_date >= w_end_date) {
	    if (msg && msg != null)
			display_error(end_date,msg,w_set_focus);
	    else
	      display_error(end_date,'Date must be after ' + w_begin_date_text,w_set_focus);
		
		return false;
	  }
  }
  else {
	  if (w_begin_date > w_end_date) {
	    if (msg && msg != null)
			display_error(end_date,msg,w_set_focus);
	    else
	      display_error(end_date,'Date must be equal or after ' + w_begin_date_text,w_set_focus);
		
		return false;
	  }
  }
  return true;
}



function val_date(dd,curr_date_flag,dft_value,set_focus) {
// pass in dft_value to default field to when . entered, if not passed current date is used
 	if (dd.className == 'error') {
		dd.className = '';
	}

  if (isblank(dd.value))
    return true;

  if (set_focus && set_focus != null)
  	var w_set_focus = set_focus;
  else
    var w_set_focus = 'Y';

   now = new Date() ;
   var d = dd.value;
   var w_year = now.getYear();
   w_year = w_year.toString();
   if (w_year.length == 4)	
	  w_year = w_year.substring(2,4);

	var w_pivot = 50;
		
	    if (d.substr(0,1) == ".") {
		if (dft_value && dft_value != null) {
		  d = dft_value.substr(4,2) + '-' + dft_value.substr(6,2) + '-' + dft_value.substr(2,2);
		}
		else {
		 var w_days_away = d.substr(1,d.length-1)
		 var w_day = now.getDate();
		 w_day = w_day + w_days_away;
	 	 if ((w_day * 1) <10)
	   		w_day = '0' + w_day;
	 	 var w_mon = now.getMonth() + 1;
	 	 if ((w_mon * 1) <10)
	   		w_mon = '0' + w_mon;
    	 d = w_mon + '-' +  w_day + '-'  + w_year;
	   	}	
		
// end new		
		
	}
	else if (d.toUpperCase() == "M") {
	 var w_mon = now.getMonth() + 1;
	 if ((w_mon * 1) <10)
	   w_mon = '0' + w_mon;
	 d = w_mon + '-01-'  + w_year;
	}

    if (d.length == 0)
        return true;
	if (d.length < 5) {
		display_error(dd,'invalid date',w_set_focus);
		return;
	}
	if (d.length == 5) {
	  d = '0' + d;
	}
	if (d.length == 6 && d.indexOf('-') == -1 && d.indexOf('/') == -1) {
		l_month = d.substr(0,2);
		l_day = d.substr(2,2);
		l_year = d.substr(4,2);
	}
	else {
		isplit = d.indexOf('-');
		if (isplit == -1 || isplit == d.length) {
			isplit = d.indexOf('/');
	    	if (isplit == -1 || isplit == d.length) {
				display_error(dd,'invalid date',w_set_focus);
				return;
			}
			else {
				l_delim = '/';
			}
	    }
		else {
			l_delim = '-';}
	
	    l_month = d.substring(0, isplit);
		isplit = d.indexOf(l_delim, isplit + 1);
	
		if (isplit == -1 || (isplit + 1 ) == d.length) {
			display_error(dd,'invalid date',w_set_focus);
			return;
		}
	
	    l_day = d.substring((l_month.length + 1), isplit);
		l_year = d.substring(isplit + 1);
	}
	if (l_month.length == 1)
	  l_month = '0' + l_month;
	if (l_day.length == 1)
	  l_day = '0' + l_day;
	
	if (!wayne_checkinteger(l_month)) {//check month
		display_error(dd,'invalid month',w_set_focus);
		return;
	}
	else
	if (!wayne_checkrange(l_month, 1, 12)) { //check month
		display_error(dd,'invalid month',w_set_focus);
		return;}
	else
	if (l_year.length == 3) { //check year
		display_error(dd,'invalid year',w_set_focus);
		return;}
	else
	if (!wayne_checkinteger(l_year)) { //check year
		display_error(dd,'invalid year',w_set_focus);
		return;}
	else
	if (!wayne_checkrange(l_year, 0, 9999)) { //check year
		display_error(dd,'invalid year',w_set_focus);
		return;}
	else
	if (!wayne_checkinteger(l_day)) { //check day
		display_error(dd,'invalid day',w_set_focus);
		return;}
	else
	if (!wayne_checkday(l_year, l_month, l_day)) { // check day
		display_error(dd,'invalid day',w_set_focus);
		return;}
	else
	    d = l_month + '-' + l_day + '-' + l_year;

	if (curr_date_flag) {
	  if (curr_date_flag == 'Y') {
		if ((l_year * 1) < (w_pivot * 1))
		  w_century = '20';
		else
		  w_century = '19';
		  
	    entered_date = new Date(w_century + l_year,l_month - 1,l_day,23,59,59,999);
        if (entered_date < now) {
		  display_error(dd,'Date cannot be less than todays date - ' + now.toLocaleString(),w_set_focus);
		  return;
		}
	  }	  
    	
	}
	
	dd.value =  l_month + '-' + l_day + '-' + l_year;
	return true;
}

function wayne_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }


function wayne_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!wayne_checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (wayne_numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
    }


	
function wayne_checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return wayne_checkrange(checkDay, 1, maxDay); //check day
    }


function wayne_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return wayne_checknumber(object_value);
    else
	return false;
    }

function wayne_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }

function val_phone_nbr(x)
    {
 	if (x.className == 'error') {
		x.className = '';
	}

  if (isblank(x.value))
    return true;

	var object_value = x.value;
		
    if (object_value.length == 0)
        return true;

// prefix international numbers with I and allow anything after the I
	if (object_value.substring(0,2).toUpperCase() == 'I-'){
	    x.value = object_value.toUpperCase();
		return true
	}
	if (object_value.substring(0,1).toUpperCase() == 'I'){
	    x.value = 'I-' + object_value.substring(1);
		return true
	}
	
	object_value = object_value.replace(/\(/g,''); 
	object_value = object_value.replace(/\)/g,''); 
	object_value = object_value.replace(/-/g,''); 
	object_value = object_value.replace(/ /g,''); 
	
	object_value = object_value.toUpperCase();
	
	var p = object_value.indexOf('X')+1;
	var e = "";
	
	if (object_value.indexOf('X')+1 > 0)	{
		e = object_value.substring(p);
		object_value = object_value.substring(0,p-1);
	}		
			
    if (object_value.length != 10) {
		display_error(x,'Invalid phone number. Use 999-999-9999 x9999 format or enter international numbers prefixed with an I (I-9999999)');
        return false;
	}

	// check if first 3 characters represent a valid area code
    if (!wayne_checknumber(object_value.substring(0,3))){
		display_error(x,'Invalid phone nbr (area code)');
		return false;
	}
	if (!wayne_numberrange((eval(object_value.substring(0,3))), 100, 1000)){
		display_error(x,'Invalid phone number (area code)');
		return false;
	}

	// check if  characters 5 - 7 represent a valid exchange
    if (!wayne_checknumber(object_value.substring(3,6))){
		display_error(x,'Invalid phone number - (exchange)');
		return false;
    }
	if (!wayne_numberrange((eval(object_value.substring(3,6))), 100, 1000)){
		display_error(x,'Invalid phone number (exchange)');
		return false;
	}
	if (object_value.substring(3,6) == '555') {
		display_error(x,'Invalid phone number (exchange 555 not allowed)');
		return false;
	}
	
	if (!wayne_checkinteger(object_value.substring(6,10))){
		display_error(x,'Invalid phone number');
		return false;
	}
	
	object_value = '(' + object_value.substr(0,3) + ') ' + object_value.substr(3,3) + '-' + object_value.substr(6,4);
	if (e)
		object_value = object_value + ' x' + e;

	x.value = object_value;		
	return true;
}

function phone_format(phone){
	if (phone.length == 10)
		return '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substr(6,4);
	else
		return phone;
}



