var isNN = (navigator.appName.indexOf("Netscape") != -1);
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
}

function showHideDiv(obj)
{
  var s;
  if(isIE)
  {
    s = document.all[obj].style.display;
    if(s == 'block')
    {
      s = 'none';
    }else{
      s = 'block';
    }
    document.all[obj].style.display = s;
  }else{
  	obj = document.getElementById(obj);
    s = obj.style.display;
    if(s == 'block')
    {
      s = 'none';
    }else{
      s = 'block';
    }
    obj.setAttribute('style','display:'+s);
  }
}

function deleteConfirmation(url) {
	var answer = confirm("Are you sure you want to delete this?")
	if (answer){
		window.location = url;
	}
}

function fillCal(obj)
{
  obj = document.getElementById(obj);
  obj2 = document.getElementById('calendar_entry');
  cal = obj.innerHTML;
  obj2.innerHTML = cal;
}


function subgroupFill(id,action)
{
  obj = document.getElementById('subgroup'+id);
  var title = obj.innerHTML;
  f = document.subgroups;
  f.id.value = id;
  f.title.value = title;
  f.action.value = action;
  f.actionbutton.value = "Update";
}

function subgroupConfirmation(url) {
	var answer = confirm("Are you sure you want to delete this subgroup? It will cause all events for this group to be uncategorized.")
	if (answer){
		window.location = url;
	}
}

function calendarFill(id,action)
{
  // get content
  obj = document.getElementById('id'+id);
  var calendar_id = obj.innerHTML;
  obj = document.getElementById('date'+id);
  var date = obj.innerHTML;
  obj = document.getElementById('title'+id);
  var title = obj.innerHTML;
  obj = document.getElementById('subgroup_id'+id);
  var subgroup_id = obj.innerHTML;
  obj = document.getElementById('description'+id);
  var description = obj.innerHTML;
  obj = document.getElementById('distance'+id);
  var distance = obj.innerHTML;

  // place content
  var f = document.calendar_entry;
  f.calendar_id.value = calendar_id;
  f.date.value = date;
  f.title.value = title;
  
  for (var i=0; i < f.subgroup_id.length; i++) {
    if (f.subgroup_id[i].value == subgroup_id) {
      f.subgroup_id[i].selected = true;
    }
  }
  f.description.value = description;
  f.distance.value = distance;
  
  if(action == 'edit')
  {
    f.action.value = 'update'; 
    f.actionbutton.value = "Update";
  }else{
    f.action.value = 'create'; 
    f.actionbutton.value = "Save";
  }
}



function calendarAdminFill(id,action)
{
  // get content
  obj = document.getElementById('id'+id);
  var calendar_id = obj.innerHTML;
  obj = document.getElementById('date'+id);
  var date = obj.innerHTML;
  obj = document.getElementById('title'+id);
  var title = obj.innerHTML;
  obj = document.getElementById('group_id'+id);
  var group_id = obj.innerHTML;
  //obj = document.getElementById('subgroup_id'+id);
  //var subgroup_id = obj.innerHTML;
  obj = document.getElementById('description'+id);
  var description = obj.innerHTML;
  obj = document.getElementById('coaches_notes'+id);
  var coaches_notes = obj.innerHTML;
  obj = document.getElementById('distance'+id);
  var distance = obj.innerHTML;

  obj = document.getElementById('plan_settings'+id);
  var plan_settings = obj.innerHTML;
  eval(plan_settings);

  // place content
  var f = document.calendar_entry;
  f.calendar_id.value = calendar_id;
  f.date.value = date;
  f.title.value = title;


  for(var i=0;i<ps.length;i++)
  {
  	var id = ps[i]['id'];
  	var val = ps[i]['val'];
  	var field = 'field_'+id;
  	field = (f[field]);
  	var type = field.type;
  	if(type == 'select-one')
  	{
  		for(var x=0;x<field.length;x++)
  		{
  			if(field[x].value == val)
  			{
  				field.selectedIndex = x;
  			}
  		}
  	}else{
  		field.value = val;
  	}
  }

  for (var i=0; i < f.group_id.length; i++) {
    if (f.group_id[i].value == group_id) {
      f.group_id[i].selected = true;
    }
  }
  
  //for (var i=0; i < f.subgroup_id.length; i++) {
  //  if (f.subgroup_id[i].value == subgroup_id) {
  //    f.subgroup_id[i].selected = true;
  //  }
  //}
  f.description.value = description;
  f.coaches_notes.value = coaches_notes;
  f.distance.value = distance;
  
  if(action == 'edit')
  {
    f.action.value = 'update'; 
    f.actionbutton.value = "Update";
  }else{
    f.action.value = 'create'; 
    f.actionbutton.value = "Save";
  }
}

function Cash(Curr) {
  return /^\d+(\.\d+)?$/.test(Curr);
}
  
function validateDonationForm(form)
{
  var errors = "";
  if(Cash(form.amount.value) == false)
  {
    errors += " - A valid amount\n";
  }
  if (errors.length > 0)
  {
    alert("Please enter the following information: \n\n" + errors);
    errors = "";
    return false;
  }
  return true;
}

function validateGroupFundraisingSettings(form)
{
    var errors = "";
    if(!isInt(form.goal.value))
    {
      	errors += " - Goal value must be a number greater than zero\n";
    }
    if(form.active[form.active.selectedIndex].value == 1 && form.goal.value < 1)
    {
      	errors += " - Goal value must be a number greater than zero\n";
    }

    if (errors.length > 0)
    {
      alert("Please enter the following information: \n\n" + errors);
      errors = "";
      return false;
    }
    return true;
}

