var project_filter = {indian_state: '', project_type: '', status: ''}
var DBG = false;
 
function update_choices() {
  var sel = document.getElementById('project-choices');
  var opts = sel.options;
  for(var i=0;i<opts.length;i++) {
    var opt = opts[i];
    var title = opt.title;
    var pairs = title.split(';');
    var keep = 1;
    for(var j=0;j<pairs.length;j++) {
      var pair = pairs[j].split('=');
      var expected = project_filter[pair[0]];
      if (! expected ) continue;
      if (pair[1] == expected) continue;
      if (DBG) alert("suppressing '" + opt.value + "' because " + pair[0] + " value is " + pair[1] + " not " + expected);
      keep = 0; 
      break;	
    }
    if (keep) {
      opt.style.display = 'block'; 
      opt.disabled = false;
      opt.className = 'enabled';
      opt.style.color = 'black';
      opt.style.fontStyle = 'normal';
    }
    else {
      opt.style.display = 'none';
      opt.disabled = true;
      opt.className = 'disabled';
      opt.style.color = 'gray';
      opt.style.fontStyle = 'italic';
    }
  }
}


// in IE, you have to have an explicit value attribute 
function _selected_value(sel) {
  var opt = sel.options[sel.selectedIndex];
  var v = opt.value ? opt.value : opt.firstChild.nodeValue;
  if (v == '(any)') v = '';
  return v;	
}

function only_state(sel) {
  var state_name = _selected_value(sel);
  if (DBG) alert("changed state to: '" + state_name + "'");
  project_filter["indian_state"] = state_name;
  update_choices();
}

function only_project_type(sel) {
  var proj_type = _selected_value(sel);
  if (DBG) alert("changed project type to: '" + proj_type + "'");
  project_filter["project_type"] = proj_type;
  update_choices();
}

function only_project_status(sel) {
  var proj_status = _selected_value(sel);
  if (DBG) alert("changed project status to: '" + proj_status + "'");
  project_filter["status"] = proj_status;
  update_choices();
}

function goto_project(sel) {
  var proj_url = sel.value; // sel.options[sel.selectedIndex].value
  if (DBG) alert("goto_project: " + proj_url);
  document.location.href = proj_url;
}

function getel (id) {
  return document.getElementById(id);
}
function getelv (id) {
  var el = getel(id);
  if (!el) {alert("no element with id '" + id + "'"); return "";}
  return el.value;
}

function validateDonate() {
  // mandatory fields, non-radio
  var mandatory = {
    firstName : 'First (Given) Name',
    lastName : 'Last (Family) Name',
    address : 'Street Address',
    city : 'City',
    state : 'State',
    pincode : 'PIN (Postal) Code',
    email : 'Email',
    donamt : 'Donation Amount'
  };
  for (var k in mandatory) {
    if (! getelv(k) ) {
      alert("Please enter a value for '" + mandatory[k] + "'.");
      return false;
    }
  } 
  // mandatory radio
  if (! el('resType1').checked && el('resType2').checked) {
     alert("Please specify your Residency type");
     return false; 
  }
  
  // dependent fields
  if (!getelv('telephone') && !getelv('mobile')) {
    alert("Please enter either a land line or mobile telephone number.");
    return false;
  }
  if (getelv('donamt') == 'Others' && !getelv('otherAmount')) {
    alert("Please enter the desired donation amount");
    return false;
  }
  if (getel('donprg2').checked && !getelv('specProj')) {
    alert("Please enter a specific project.");
    return false;
  }
  if (el('resType2').checked && !getelv('passportNumber')) {
    alert("Please specify your passport number.");
    return false;
  }

  // special regexp validation
  var re = /^[_\.0-9a-z-]+\@([0-9a-z][0-9a-z-]*\.)+([a-z]{2,4})+$/i;
  if (!getelv('email').match(re)) {
    alert("Please enter a valid email address");
    getel('email').focus();
    return false;
  } 

  return true;
}
