/*
 * $Author: dbibbens $
 * $Revision: 1.49 $
 * $Date: 2009/03/19 19:01:12 $ 
 * $Source: /home/cvs/wheelchairshops.com/local/lib/functions.js,v $
 */

var touched = false;

function verify(f) 
{
  var msg = "";
  var empty_fields = "";
  var errors = "";
  
  touched = true;
  
  f.elements[4].value = f.elements[4].value.replace(/\D/g, '');
  f.elements[5].value = f.elements[5].value.replace(/\D/g, ''); 
  f.elements[6].value = f.elements[6].value.replace(/\D/g, '');

  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];

    if(e.type == "text") {
      if((e.value == null) || (e.value == "") || isblank(e.value)) {
	empty_fields += "\n        " + e.name;
	continue;
      }
    }

    if(e.type == "select-one" && e.selectedIndex == 0) {
      empty_fields += "\n        " + e.name;
      continue;
    }
    
    /*
    if(e.type == "radio" && (e.value != "yes" || e.value != "no")) {
    if(e.id.indexOf(".yes") > -1) {
    empty_fields += "\n        " + e.name;
    continue;
    }
    }
    */
  }
    
  if(!empty_fields && !errors && 
     validate_telephone(f.elements[4].value, 
			f.elements[5].value, 
			f.elements[6].value)) {
      return true;
  }

  msg   += "_________________________________________________________\n\n";
  msg   += "Your inquiry was not submitted because of the following "
  msg   += "error(s).\n";
  msg   += "Please correct these error(s) and re-submit.\n";
  msg   += "_________________________________________________________\n\n"

    if(empty_fields) {
      msg += "- The following required field(s) are empty:"
      + empty_fields + "\n";
      msg += errors;
      alert(msg);
    }

  return false;
}

function isblank(s)
{
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if((c != ' ') && (c != '\n') && (c != '')) {
      return false;
    }
  }
  
  return true;
}

function check_hmo() 
{
  var insurance = document.forms[0].insurance;
  var selected = insurance.options[insurance.selectedIndex].value;
  if (selected == "HMO") {
    alert('Sorry, this offer is not available to HMO patients.');

    return false;
  }
  
  if (selected == "MEDICAID") {
    alert('Sorry, this offer is not available to Medicaid patients who do not have Medicare as their primary insurance.');
    
    return false;
  }
  
  return true;
}

function validate_email(string) 
{
  if (string.indexOf('@') < 0 || 
      string.charAt(string.length - 4) != '.') {
    alert(string + ': Please provide a valid email address');
    return false;
  }
  
  return true;
}

function validate_telephone(tel_ac, tel_pre, tel_number) 
{
  var string = tel_ac + tel_pre + tel_number;
  var stripped = string.replace(/[\(\)\.\-\ ]/g, '');

  if (isNaN(parseInt(stripped)) || (!(stripped.length == 10))) {
    alert('The telephone number you entered does not seem correct. Please enter a correct telephone number.');
    return false;
  }

  return true;
}

function qualified(checkbox, uri) 
{
  if(checkbox.checked) {
    document.location = uri + '&qualified';
  } else {
    document.location = '/' + uri.substr(1, (uri.indexOf('qualified') - 2));
  }
}

function steps2(checkbox, uri) 
{
  if(checkbox.checked) {
    document.location = uri + '&steps2';
  } else {
    document.location = '/' + uri.substr(1, (uri.indexOf('steps2') - 2));
  }
}

function set_range(start, end)
{
  var select = null;

  var start_year = start.substr(0, 4);
  var start_month = start.substr(5, 2);
  var start_day = start.substr(8, 2);

  var end_year = end.substr(0, 4);
  var end_month = end.substr(5, 2);
  var end_day = end.substr(8, 2);

  select = document.getElementById("start_year");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == start_year) {
      select.options[i].selected = true;
    }
  }
  select = document.getElementById("start_month");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == start_month) {
      select.options[i].selected = true;
    }
  }

  select = document.getElementById("start_day");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == start_day) {
      select.options[i].selected = true;
    }
  }

  select = document.getElementById("end_year");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == end_year) {
      select.options[i].selected = true;
    }
  }

  select = document.getElementById("end_month");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == end_month) {
      select.options[i].selected = true;
    }
  }

  select = document.getElementById("end_day");
  for(i = 0; i < select.options.length; i++) {
    if(select.options[i].value == end_day) {
      select.options[i].selected = true;
    }
  }
}

function do_today(uri, user_id) 
{
  document.location = uri + '?user-id=' + user_id;
}

function do_range(uri, user_id) 
{
  var start_year = select = document.getElementById("start_year");
  var start_month = document.getElementById("start_month");
  var start_day = document.getElementById("start_day");

  var end_year = document.getElementById("end_year");
  var end_month = document.getElementById("end_month");
  var end_day = document.getElementById("end_day");


  var start = start_year.options[start_year.selectedIndex].value + '-' + start_month.options[start_month.selectedIndex].value + '-' + start_day.options[start_day.selectedIndex].value;

  var end = end_year.options[end_year.selectedIndex].value + '-' + end_month.options[end_month.selectedIndex].value + '-' + end_day.options[end_day.selectedIndex].value;

  document.location = uri + '?user-id='+ user_id + '&start=' + start + '&end=' + end;
}

function openWin(url, width, height) 
{
  var features = "toolbar=yes,location=yes,directories=no,status=yes," + 
    "menubar=yes,marginwidth=0,marginheight=0,resizable=yes,scrollbars=" +
    "yes,width=" + width + ",height=" + height;

  if(!touched) {  
    popup_win(url, 'results', features);
  }
}

function popup_win(url, name, features) 
{
  window.open(url, name, 'screenX=0,screenY=0,left=0top=0,' + 
			  features);
}
