// make dojo.toJson() print dates correctly (this feels a bit dirty)
Date.prototype.json = function(){ return dojo.date.stamp.toISOString(this, {selector: 'date'});}; 

function writeAge(value){
  document.write(formatAge(value));
}

function formatName(value){
  var namelist = value.split("|");
//  res = "<small>&nbsp&nbsp&nbsp&nbsp S:"+namelist[1]+"</small><br><strong>"+namelist[0]+"</strong><br><small>&nbsp&nbsp&nbsp&nbsp D:"+namelist[2]+"</small>";
  res = "<strong>"+namelist[0]+"</strong><br><small>&nbsp&nbsp&nbsp&nbsp S:"+namelist[1]+"</small><br><small>&nbsp&nbsp&nbsp&nbsp D:"+namelist[2]+"</small>";
  return res;
}

function formatAge(value){
  var dob = value.substring(8,10);
  var mob = value.substring(5,7);
  var yob = value.substring(0,4);

  var bdate = new Date(parseInt(yob,10), parseInt(mob,10)-1, parseInt(dob,10));
  var now = new Date();
  
  var diff = now-bdate;
  diff = diff/86400000;
  diff = Math.floor(diff);
  var age = getAge(yob+mob+dob, 1);
  var res = age + '<br>(' + diff + ' days)';
  return res;
}

function formatDate(value){
  var res = value.substring(8,10);
  res = res+"/"+value.substring(5,7);
  res = res+"/"+value.substring(0,4);
  return res;
}

function formatImageYN(value){
  var res;
  if(value == 1)
  {
    res="<IMG src=\"images/ok.png\" alt=\"Yes\" width=\"16\" height=\"16\" border=\"0\">";
  }
  else
  {
    res="<IMG src=\"images/cancel.png\" alt=\"No\" width=\"16\" height=\"16\" border=\"0\">";
  }
  return res;
}

function formatImageSmall(value){
  var res;
  res = (value == null) ? "No Image" : "<IMG src=\"images/cattle/"+value+"_S.jpg\" alt=\""+value+"\" width=\"150\" height=\"113\" border=\"0\">";
  return res;
}

function getAge(dateString,dateType) {
/*
   function getAge
   parameters: dateString dateType
   returns: boolean

   dateString is a date passed as a string in the following
   formats:

   type 1 : 19970529
   type 2 : 970529
   type 3 : 29/05/1997
   type 4 : 29/05/97

   dateType is a numeric integer from 1 to 4, representing
   the type of dateString passed, as defined above.

   Returns string containing the age in years, months and days
   in the format yyy years mm months dd days.
   Returns empty string if dateType is not one of the expected
   values.
*/

    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());

    var yearNow = now.getYear();
    var monthNow = now.getMonth();
    var dateNow = now.getDate();

    if (dateType == 1)
        var dob = new Date(dateString.substring(0,4),
                            dateString.substring(4,6)-1,
                            dateString.substring(6,8));
    else if (dateType == 2)
        var dob = new Date(dateString.substring(0,2),
                            dateString.substring(2,4)-1,
                            dateString.substring(4,6));
    else if (dateType == 3)
        var dob = new Date(dateString.substring(6,10),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else if (dateType == 4)
        var dob = new Date(dateString.substring(6,8),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else
        return '';

    var yearDob = dob.getYear();
    var monthDob = dob.getMonth();
    var dateDob = dob.getDate();

    yearAge = yearNow - yearDob;

    if (monthNow >= monthDob)
        var monthAge = monthNow - monthDob;
    else {
        yearAge--;
        var monthAge = 12 + monthNow -monthDob;
    }

    if (dateNow >= dateDob)
        var dateAge = dateNow - dateDob;
    else {
        monthAge--;
        var dateAge = 31 + dateNow - dateDob;

        if (monthAge < 0) {
            monthAge = 11;
            yearAge--; 
        }
    }

    return yearAge + 'y ' + monthAge + 'm ' + dateAge + 'd';
}

function displayLinks(e){
var selectedValue = maingrid.store.getValue(maingrid.getItem(e.rowIndex),"id");
//  alert("selected cell Value is "+selectedValue);
  var day= new Date();
  var id = day.getTime(); 
  var win = open("animal.php?id="+selectedValue,id,'width=870,height=900,scrollbars,resizable,location=0,menubar=0,status=0,titlebar=0,toolbar=0'); 
}

// http://userscripts.org/topics/22491
// To call post("http://example.com", {foo:"bar",nothing:120, another:"test"})
function post(URL, PARAMS) {
	var temp=document.createElement("form");
	temp.action=URL;
	temp.method="POST";
	temp.style.display="none";
	for(var x in PARAMS) {
		var opt=document.createElement("textarea");
		opt.name=x;
		opt.value=PARAMS[x];
		temp.appendChild(opt);
	}
	document.body.appendChild(temp);
	temp.submit();
	return temp;
}

function changeAnimalEdit(row) {
//  alert("selected row is "+row);
//  alert("selected row is "+dojo.attr(dojo.byId('selanimal'),'value'));
//  alert("selected row is "+document.getElementById('selanimal').selectedIndex);
//  dojo.attr(dojo.byId('name'),'value',"Fred"));
  dojo.byId('name').value = "Fred";
}

function getVar(val)
{
  return val;
}