// 24.02.2011 checkSerieFormat function added
// 28.02.2011 checkSerieFormat function updated
// 31.05.2011 setCountryCode function added
// 25.10.2011 loadcssfile & loadjsfile function added
// 30.10.2011 drawUniform added
// 03.11.2011 drawBigUniform added
// 09.11.2011 many updates
// 21.11.2011 drawUniform updated
// 23.11.2011 drawBigUniform updated
// 28.11.2011 ajaxRequest added
// 30.11.2011 drawUniform face drawing added
// 05.12.2011 drawUniform updated
// 02.01.2012 popUp updated

var bgError = '#FFB3B3';
var ie = isIE();

function isIE() {
 if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
  var ieversion=new Number(RegExp.$1);
  if (ieversion>=9) return 9;
  else if (ieversion>=8) return 8;
  else if (ieversion>=7) return 7;
  else if (ieversion>=6) return 6;
  else if (ieversion>=5) return 5;
 }else {
   return 0;
 }
}

function isdark(color){
  var colr = parseInt(color.substr(1),16);
  return (colr >>> 16) + ((colr >>> 8) & 0x00ff) + (colr & 0x0000ff) < 500;
}


function popUp(url,width,height,name) {
  if (typeof width == 'undefined' || width == 0) var w = 520; else var w = width;
  if (typeof height == 'undefined' || height == 0) var h = 410; else var h = height+10;
  if (typeof name == 'undefined') name = "popUp";
  //var f = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,menubar=no";
  var f = "scrollbars=yes,resizable=yes";
  var t = (screen.height-h)/2;
  var l = (screen.width-w)/2;
  newW  = window.open(url,name,'width='+w+',height='+h+',top='+t+',left='+l+','+f);
  newW.focus();
}


function googleMapForm(f) {
  if (f.form.geocode.value.length) {
    var l = f.form.geocode.value;
  }else {
    var l = f.form.addr.value;
  }
  var m = f.form.title.value.substr(0,1);
  var c = "0x"+f.form.color.value.substr(1);
  popUp("http://maps.google.com/maps/api/staticmap?center=" +l+ "&zoom=13&size=640x480&sensor=false&maptype=terrain&markers=color:"+c+"|label:"+m+"|" +l+ "",640,480);
}

function googleMap(l,m,c,z) {
  if (typeof m == 'undefined') {
    m = "";
  }else if (m.length>1){
    m = m.substr(0,1);
  }
  if (typeof c == 'undefined') {
    c = "0x0000ff";
  }else if (c.substr(0,1)=="#"){
    c = "0x"+c.substr(1);
  }
  if (typeof z == 'undefined' || z.length<1) {
    z = 14;
  }
  popUp("http://maps.google.com/maps/api/staticmap?center=" +l+ "&zoom="+z+"&size=640x480&sensor=false&maptype=terrain&markers=color:"+c+"|label:"+m+"|" +l+ "",640,480);
}

function googleGeoCode() {
  popUp("http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html",640,520);
}


function checkSerieFormat(f) {
  if (f.type=="select-one" || f.type=="hidden") return true;
  serie=false;
  for (i=0;i<f.length;i++){
    if (f[i].checked==true) serie=true;
  }
  //alert(f.value+", "+serie);
  if (!serie) {
    alert("Please select serie!\nValitse sarja!!");
    return false;
  }
  return true;
}

function checkDateFormat(f,e) {
  // e means that empty content is allowed
  if (typeof e == 'undefined') var e = 0;
  var filter = /^ *(\d+[\./]\d+[\./]\d+) *$/;
  if (f.value.length>0) {
    // field contains string
    if (filter.test(f.value)) return true;
    // date format was invalid
    alert("Check date format!\nTarkista päiväyksen muoto!");
    f.style.backgroundColor = bgError;
    return false;
  }else if (e) {
    // empty is allowed
    return true;
  }else {
    // empty is not allowed
    alert("Empty date field!\nPäiväyskenttä tyhjä!");
    f.style.backgroundColor = bgError;
    return false;
  }
}

function checkEmailFormat(f) {
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if (f.value.length>0) {
    if (filter.test(f.value)) return true;
  }
  alert("Check email format!\nTarkista sähköpostiosoitteen muoto!");
  f.style.backgroundColor = bgError;
  return false;
}

function checkPhoneFormat(f) {
  var filter = /^\s*(\+\d{3})*\s*(\d{1,4}\s*){1}[\s-]*[\d ]{5,9}\s*$/; 
  if (f.value.length>0) {
    if (filter.test(f.value)) return true;
  alert("Check phone format!\nTarkista puhelinnumeron muoto!");
    f.style.backgroundColor = bgError;
  return false;
  }
  return true;
}

function checkNumberFormat(f) {
  var filter = /(^\d+$)|(^\d+\.\d+$)|(^$)/;
  if (f.value.length>0) {
    if (filter.test(f.value)) return true;
    alert("Check number format!\nTarkista numerokentän muoto!!");
    f.style.backgroundColor = bgError;
    return false;
  }
  alert("Number field empty!\nNumerokenttä tyhjä!");
  f.style.backgroundColor = bgError;
  return true;
}

function checkTeamFormat(f) {
  var filter = /(\d{2,4})/;
  if (f.value.length>0) {
    if (filter.test(f.value)) {
      alert("Check team name! Please no unnecessary year!\nTarkista joukkueen nimi, turhia vuosilukuja ei tarvita!");
      return true;
    }else return true;
  }
  alert("Team name empty!\nJoukkeen nimi tyhjä!");
  f.style.backgroundColor = bgError;
  return false;
}

function checkCompanyFormat(f) {
  if (f.value.length<1) {
    alert("Company name empty!\nSeuran nimi tyhjä!");
    f.style.backgroundColor = bgError;
    return false;
  }
  return true;
}

//<FORM NAME=atEnter METHOD=POST onSubmit="return checkEnterForm(this)" >
function checkEnterForm(f) {
  f.checked.value = 'ok';
  var error = 0;
  //if (!checkDateFormat(f)) error++;
  if (!checkSerieFormat(f.serie)) error++;
  if (!checkCompanyFormat(f.company)) error++;
  if (!checkTeamFormat(f.team)) error++;
  if (!checkEmailFormat(f.email)) error++;
  if (!checkPhoneFormat(f.phone)) error++;
  if (!checkPhoneFormat(f.phone2)) error++;
  if (!checkNumberFormat(f.players)) error++;
  if (!checkNumberFormat(f.others)) error++;
  if (error) {
    alert("Not saved!\nEi talletettu!");
    return false
  }
  return true;
}



//<INPUT TYPE='text' NAME='players' onChange='countCharge(this)'>
function countCharge(f,c) {
  if (f.form.price.value.length) {
    var price=parseInt(f.form.price.value);
    if (isNaN(price)) price = 0;
    f.form.charge.value = price;
    return;
  }
  var players=parseInt(f.form.players.value);
  var others=parseInt(f.form.others.value);
  if (isNaN(players)) players = 0;
  if (isNaN(others)) others = 0;
  var rule=f.form.accom.value;
  if (f.form.serie.type=="hidden" || f.form.serie.type=="text") var serie=f.form.serie.value;
    else {
      for (i=0;i<f.form.serie.length;i++){
        if (f.form.serie[i].checked==true)
          var serie=f.form.serie[i].value;
      }
    }
//alert("type="+f.form.serie.type+"  rule="+rule+"  serie="+serie);
  if (typeof rules[rule][serie] != 'undefined') var base=parseInt(rules[rule][serie]);
    else var base=0;
  if (rules[rule]['exp'].length) {
    if (c>0 && rules[rule]['chg']) {
      //changing charge
      alert("Maksu riippuu ilmoittautumishetkestä!\nCharge is depending entering date!");
    }
    var charge = eval(rules[rule]['exp']);
    f.form.charge.value = charge.toFixed(2) ;
    //f.form.charge.value = Math.round(eval(rules[rule]['exp'])*100)/100 ;
  }else {
    f.form.charge.value = "?";
  }
}

function setCountryCode(f,x) {
  if (x) {
    //alert(f.form.country.value+":"+codes[f.form.country.value]);
    if (f.value.length<=5) {
      f.value = "+"+codes[f.form.country.value]+" ";
    }
  }else {
    if (f.form.phone.value.length>0 && f.form.phone.value.length<=5) {
      f.form.phone.value = "+"+codes[f.value]+" ";
    }
    if (f.form.phone2.value.length>0 && f.form.phone2.value.length<=5) {
      f.form.phone2.value = "+"+codes[f.value]+" ";
    }
  }
}

function loadjsfile(filename){
 var fileref=document.createElement('script')
 fileref.setAttribute('type','text/javascript')
 fileref.setAttribute('src', filename)
 if (typeof fileref!='undefined')
  document.getElementsByTagName('head')[0].appendChild(fileref)
 else alert(filename+' not loaded!');
}
function loadcssfile(filename){
 var fileref=document.createElement('link')
 fileref.setAttribute('rel', 'stylesheet')
 fileref.setAttribute('type', 'text/css')
 fileref.setAttribute('href', filename)
 if (typeof fileref!='undefined')
  document.getElementsByTagName('head')[0].appendChild(fileref)
 else alert(filename+' not loaded!');
}


function drawUniform(ctx,c1,c2,c3,c4,c5,ty,se,md,fa) {
  //alert(ie+', '+ctx+', '+c1+', '+c2+', '+c3+', '+c4+', '+c5+', '+ty+', '+se+', '+md);
  if (c1.length!=7) c1 = '#ffffff';
  if (c2.length!=7) c2 = '#000000';
  if (c3.length!=7) c3 = '#ffffff';
  if (c4.length!=7) c4 = '#ffffff';
  if (c5.length!=7) c5 = c4;
  if (ty=='x' || ty=='t') {
    // coalkeepers complement colors
    c1 = complementRGB(c1);
    c2 = complementRGB(c2);
    c3 = complementRGB(c3);
    c4 = complementRGB(c4);
    c5 = complementRGB(c5);
  }
  ctx.save(); //0
  if (fa) {
    ctx.save(); //1
    if (fa>0) {
      var male = 1;
    }else {
      fa = Math.abs(fa);
      var male = 0;
    }
    // draw face and other things /////////////////////////
    // fa is mood 1..6
    var red,gre,blu,rnd;
    rnd = -40+Math.floor(Math.random()*60);
    red = 235+rnd;
    gre = 220+rnd;
    blu = 158+rnd;
    ctx.fillStyle = "rgba("+red+","+gre+","+blu+",255)";
    ctx.scale(0.75,1);
    ctx.beginPath();
    ctx.arc(21,-6,7,0,Math.PI*2, false);
    ctx.closePath();
    ctx.fill();
    // shadow off ///////////////////////////////
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    ctx.shadowBlur = 0;
    ctx.lineWidth = 0.5;
    ctx.strokeStyle = "#988D2E";
    ctx.stroke();
    // eyes /////////////////////////////////////
    ctx.fillStyle = "Black";
    ctx.beginPath();
    ctx.arc(18,-6,1,0,Math.PI*2, false);
    ctx.closePath();
    ctx.fill();
    ctx.beginPath();
    ctx.arc(24,-6,1,0,Math.PI*2, false);
    ctx.closePath();
    ctx.fill();
    if (fa>6) fa = 6;
    var mood = 1-fa+Math.floor(Math.random()*4);
    ctx.strokeStyle = "Black";
    ctx.beginPath();
    ctx.moveTo(18,-3);
    ctx.quadraticCurveTo(21,mood,24,-3);
    if (fa<4) {
      // more smile for positions 1..3
      ctx.quadraticCurveTo(21,mood-2,18,-3);
    }
    ctx.stroke();
    // ears /////////////////////////////////////
    //ctx.fillStyle = "#E4DDA5";
    ctx.fillStyle = "rgba("+red+","+gre+","+blu+",255)";
    ctx.strokeStyle = "#988D2E";
    ctx.lineWidth = 0.5;
    ctx.scale(0.5,1);
    ctx.beginPath();
    ctx.arc(28,-6,2,0,Math.PI*2, false);
    ctx.closePath();
    ctx.fill();
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(56,-6,2,0,Math.PI*2, false);
    ctx.closePath();
    ctx.fill();
    ctx.stroke();
    ctx.restore();
    // HAIR /////////////////////////////////////
    if (!male || ty=='t'){
      ctx.save();
	  // switch shadow off
      ctx.shadowOffsetX = 0;
      ctx.shadowOffsetY = 0;
      ctx.shadowBlur = 0;
      rnd = -90+Math.floor(Math.random()*200);
      var rn2 = Math.floor(Math.random()*6);
      red = 90+rnd;
      gre = 78+rnd;
      blu = 56+rnd;
      ctx.strokeStyle = "rgba("+red+","+gre+","+blu+",255)";
      ctx.beginPath();
      if (ty=='t') {
        // trainer
        ctx.lineWidth = 3;
        ctx.moveTo(11,-7);
        ctx.bezierCurveTo(12,-14,20,-14,21,-7);
      }else {
        // female
        ctx.lineWidth = 3;
        ctx.moveTo(10,-5+rn2);
        ctx.bezierCurveTo(11,-17,21,-17,22,-5+rn2);
      }
      ctx.stroke();
      ctx.restore();
    }
  }
  if (ie) {
    // shadow parameters
    ctx.save(); //1
    ctx.translate(1,1);
    ctx.fillStyle = '#A6A6A6';
    // draw shirt shadow
    ctx.beginPath();
    ctx.moveTo(0,15);
    ctx.lineTo(8,1);
    ctx.lineTo(12,0);
    ctx.lineTo(15,3);
    ctx.lineTo(17,3);
    ctx.lineTo(20,0);
    ctx.lineTo(24,1);
    ctx.lineTo(31,15);
    ctx.lineTo(28,23);
    ctx.lineTo(24,12);
    ctx.lineTo(24,31);
    ctx.lineTo(8,31);
    ctx.lineTo(8,12);
    ctx.lineTo(2,23);
    ctx.lineTo(0,15);
    ctx.fill();
    // draw pants shadow
    ctx.beginPath();
    ctx.moveTo(8,32);
    ctx.lineTo(24,32);
    ctx.lineTo(29,46);
    ctx.lineTo(19,48);
    ctx.lineTo(16,39);
    ctx.lineTo(13,48);
    ctx.lineTo(3,46);
    ctx.fill();
    ctx.restore(); //1
  }
  // draw shirt
  ctx.fillStyle = c1;
  ctx.beginPath();
  ctx.moveTo(0,15);
  ctx.lineTo(8,1);
  ctx.lineTo(12,0);
  ctx.lineTo(15,3);
  ctx.lineTo(17,3);
  ctx.lineTo(20,0);
  ctx.lineTo(24,1);
  ctx.lineTo(31,15);
  ctx.lineTo(28,23);
  ctx.lineTo(24,12);
  ctx.lineTo(24,31);
  ctx.lineTo(8,31);
  ctx.lineTo(8,12);
  ctx.lineTo(2,23);
  ctx.lineTo(0,15);
  ctx.fill();
  // draw pants
  ctx.fillStyle = c4;
  ctx.beginPath();
  ctx.moveTo(8,32);
  ctx.lineTo(24,32);
  ctx.lineTo(29,46);
  ctx.lineTo(19,48);
  ctx.lineTo(16,39);
  ctx.lineTo(13,48);
  ctx.lineTo(3,46);
  //ctx.closePath();
  ctx.fill();
  // shadow off
  ctx.shadowOffsetX = 0;
  ctx.shadowOffsetY = 0;
  ctx.shadowBlur = 0;
  ctx.fillStyle = c3;
  if (typeof ty == 'number') ty = String.fromCharCode(48+ty);
  switch (ty){
    case '1': // vertical
      ctx.fillRect (9,1,2,30);
      ctx.fillRect (13,3,2,28);
      ctx.fillRect (17,3,2,28);
      ctx.fillRect (21,1,2,30);
      break;
    case '2': // horizontal
      ctx.fillRect (8,3,16,2);
      ctx.fillRect (8,7,16,2);
      ctx.fillRect (8,11,16,2);
      ctx.fillRect (8,15,16,2);
      ctx.fillRect (8,19,16,2);
      ctx.fillRect (8,23,16,2);
      ctx.fillRect (8,27,16,2);
      break;
    case '3': // sleeves
      ctx.beginPath();
      ctx.moveTo(0,15);
      ctx.lineTo(8,1);
      ctx.lineTo(8,12);
      ctx.lineTo(2,23);
      ctx.fill();
      ctx.beginPath();
      ctx.moveTo(24,1);
      ctx.lineTo(31,15);
      ctx.lineTo(28,23);
      ctx.lineTo(24,12);
      ctx.fill();
      break;
    case '4': // half
      ctx.beginPath();
      ctx.moveTo(24,1);
      ctx.lineTo(31,15);
      ctx.lineTo(28,23);
      ctx.lineTo(24,12);
      ctx.lineTo(24,31);
      ctx.lineTo(16,31);
      ctx.lineTo(16,4);
      ctx.lineTo(17,4);
      ctx.lineTo(21,0);
      ctx.lineTo(24,1);
      ctx.fill();
      break;
    case '5': // wide vertical
      ctx.beginPath();
      ctx.moveTo(12,1);
      ctx.lineTo(15,4);
      ctx.lineTo(17,4);
      ctx.lineTo(20,1);
      ctx.lineTo(20,31);
      ctx.lineTo(12,31);
      ctx.fill();
      break;
    case '6': // wide horizontal
      ctx.fillRect (8,11,16,13);
      break;
    case '7': // wide vertical & sleeves
      ctx.beginPath();
      ctx.moveTo(0,15);
      ctx.lineTo(8,1);
      ctx.lineTo(8,12);
      ctx.lineTo(2,23);
      ctx.fill();
      ctx.beginPath();
      ctx.moveTo(24,1);
      ctx.lineTo(31,15);
      ctx.lineTo(28,23);
      ctx.lineTo(24,12);
      ctx.fill();
      ctx.beginPath();
      ctx.moveTo(12,1);
      ctx.lineTo(15,4);
      ctx.lineTo(17,4);
      ctx.lineTo(20,1);
      ctx.lineTo(20,31);
      ctx.lineTo(12,31);
      ctx.fill();
      break;
    case '8': // wide horizontal
      ctx.fillRect (8,17,16,14);
      break;
    case '9': // other
      ctx.beginPath();
      ctx.moveTo(24,1);
      ctx.lineTo(31,15);
      ctx.lineTo(28,23);
      ctx.lineTo(24,12);
      ctx.lineTo(24,31);
      ctx.lineTo(8,31);
      ctx.fill();
      break;
    case 'x': // coalkeeper
      ctx.beginPath();
      ctx.lineWidth = 1;
      // left side
      ctx.moveTo(8,1);
      ctx.lineTo(16,9);
      ctx.moveTo(7,3);
      ctx.lineTo(16,12);
      ctx.moveTo(6,5);
      ctx.lineTo(16,15);
      ctx.moveTo(5,7);
      ctx.lineTo(16,18);
      ctx.moveTo(8,13);
      ctx.lineTo(16,21);
      ctx.moveTo(8,16);
      ctx.lineTo(16,24);
      ctx.moveTo(8,19);
      ctx.lineTo(16,27);
      ctx.moveTo(8,22);
      ctx.lineTo(16,30);
      ctx.moveTo(8,25);
      ctx.lineTo(14,31);
      ctx.moveTo(8,28);
      ctx.lineTo(11,31);
      // right side
      ctx.moveTo(24,1);
      ctx.lineTo(16,9);
      ctx.moveTo(25,3);
      ctx.lineTo(16,12);
      ctx.moveTo(26,5);
      ctx.lineTo(16,15);
      ctx.moveTo(27,7);
      ctx.lineTo(16,18);
      ctx.moveTo(24,13);
      ctx.lineTo(16,21);
      ctx.moveTo(24,16);
      ctx.lineTo(16,24);
      ctx.moveTo(24,19);
      ctx.lineTo(16,27);
      ctx.moveTo(24,22);
      ctx.lineTo(16,30);
      ctx.moveTo(24,25);
      ctx.lineTo(18,31);
      ctx.moveTo(24,28);
      ctx.lineTo(21,31);
      ctx.stroke();
      break;
    case 't': // trainer
      if (isdark(c1)) {
        ctx.fillStyle = 'white';
      }else {
        ctx.fillStyle = 'black';
      }
      ctx.beginPath();
      ctx.lineWidth = 2;
      // left side
      ctx.moveTo(0,15);
      ctx.lineTo(8,1);
      ctx.lineTo(9,3);
      ctx.lineTo(1,17);
      ctx.closePath();
      // right side
      ctx.moveTo(24,1);
      ctx.lineTo(31,15);
      ctx.lineTo(30,17);
      ctx.lineTo(23,3);
      //ctx.stroke();
      ctx.fill();
      break;
    default:
  }
  if (ty!='x'){
    // lines to pants
    ctx.fillStyle = c5;
    ctx.beginPath();
    ctx.moveTo(8,32);
    ctx.lineTo(12,32);
    ctx.lineTo(7,47);
    ctx.lineTo(3,46);
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(24,32);
    ctx.lineTo(29,46);
    ctx.lineTo(28,46);
    ctx.lineTo(23,32);
    ctx.fill();
  }else {
  }
  // last thing is text for shirt
  if (c2==c1) {
    if (ty==0){
      c2 = complementRGB(c1);
    }
  }
  ctx.save();
  if (se.length==1) {
    ctx.font = '18px Arial';
    var x = 10;
  }else {
    se = se.substr(0,2);
    ctx.font = '10px Arial';
    var x = 9;
  }
  if (md) {
    ctx.globalAlpha = 0.3;
  }else {
    if (isdark(c1)) {
      ctx.shadowColor = '#DDDDDD';
    }else {
      ctx.shadowColor = '#555555';
    }
    ctx.shadowOffsetX = 1;
    ctx.shadowOffsetY = 1;
    ctx.shadowBlur = 2;
  }
  ctx.fillStyle = c2;
  ctx.fillText(se,x,25);
  ctx.restore();
  // simple kind of vawe
  ctx.beginPath();
  ctx.moveTo(0,15);
  ctx.lineTo(8,1);
  ctx.lineTo(12,0);
  ctx.lineTo(15,3);
  ctx.lineTo(17,3);
  ctx.lineTo(20,0);
  ctx.lineTo(24,1);
  ctx.lineTo(31,15);
  ctx.lineTo(28,23);
  ctx.lineTo(24,12);
  ctx.lineTo(24,31);
  ctx.lineTo(8,31);
  ctx.lineTo(8,12);
  ctx.lineTo(2,23);
  ctx.lineTo(0,15);
  ctx.closePath();
  var grd = ctx.createLinearGradient(0,0,50,10);
  grd.addColorStop(0,'rgba(0,0,0,0)');
  grd.addColorStop(0.2,'rgba(0,0,0,0)');
  grd.addColorStop(0.3,'rgba(0,0,0,0.3)');
  grd.addColorStop(0.4,'rgba(0,0,0,0)');
  grd.addColorStop(1,'rgba(0,0,0,0)');
  ctx.fillStyle = grd;
  ctx.fill();
  ctx.restore(); //0
}


function drawBigUniform(ctx,c1,c2,c3,c4,c5,ty,se,te,ok,n1,n2,lo,co,id,pr,fa) {
  //alert(ie+','+c1+','+c2+','+c3+','+c4+','+c5+','+ty+','+se+','+te+','+ok+','+n1+','+n2+','+lo+','+co+','+id);
  if (co.lenght) var flag = 'images/flags16/'+co+'.png';
  else var flag = '';
  var sx = se.substr(0,1);
  var tx = te.split(' ');
  ctx.save(); //0
  ctx.save(); //1
  // draw uniform at first
  ctx.shadowOffsetX = 5;
  ctx.shadowOffsetY = 5;
  ctx.shadowBlur = 15;
  ctx.shadowColor = '#6E6E6E';
  ctx.scale(5,5);
  if (fa>0) {
    if (pr>0) fa = pr; else fa = 4;
  }else if (fa<0) {
    if (pr>0) fa = -pr; else fa = -4;
  }
  drawUniform(ctx,c1,c2,c3,c4,c5,ty,sx,1,fa);
  // draw status mark
  switch (ok) {
    case '0':
      ctx.fillStyle = '#FFFF00';
      break;
    case '1':
      ctx.fillStyle = '#3300FF';
      break;
    case '2':
      ctx.fillStyle = '#33CC00';
      break;
    default:
      ctx.fillStyle = '#FF3300';
  }
  ctx.beginPath();
  ctx.moveTo(30,13);
  ctx.lineTo(31,15);
  ctx.lineTo(28,22);
  ctx.lineTo(27,19);
  ctx.fill();
  ctx.restore();
  // shadow feature
  if (isdark(c1)) {
    // background is dark
    if (isdark(c2)) {
      ctx.shadowColor = 'white';
    }else {
      ctx.shadowColor = c2;
    }
  }else {
    // background is light
    if (isdark(c2)) {
      // text is dark
      ctx.shadowColor = c2;
    }else {
      // text is light
      ctx.shadowColor = 'black';
    }
  }
  ctx.shadowBlur = 3;
  ctx.shadowOffsetX = 2;
  ctx.shadowOffsetY = 2;
  ctx.font = '25px Arial';
  ctx.textAlign = 'center';
  ctx.fillStyle = c2;
  // draw serie
  ctx.fillText(se,80,40,100);
  // draw team name
  ctx.font = '20px Arial';
  if (typeof tx[1]=='undefined'){
    ctx.fillText(tx[0],80,70);
  }else {
    ctx.fillText(tx[0],80,70);
    ctx.font = '14px Arial';
    ctx.fillText(tx[1],80,90);
  };
  // draw location
  ctx.font = '14px Arial';
  ctx.fillText(lo,80,145);
  // draw serie id as rotated
  ctx.save();
  if (isdark(c5)) {
    ctx.shadowColor = 'white';
    ctx.fillStyle = 'white';
  }else {
    ctx.shadowColor = 'black';
    ctx.fillStyle = 'black';
  }
  ctx.translate(65,210);
  ctx.font = '20px Arial';
  ctx.rotate(-1.3);
  ctx.fillText(se,0,0);
  // draw serie id as rotated
  ctx.font = '16px Arial';
  ctx.rotate(1.0);
  ctx.translate(35,15);
  ctx.fillText(n1,0,0);
  if (n2) {
    ctx.fillText('+'+n2,0,15);
  }
  ctx.restore();
  // draw prize (medal)
  if (pr>0 && pr<4) {
    if (pr==1) {
      var pc = '#FFE87C';
    }else if (pr==2) {
      var pc = '#C0C0C0';
    }else {
      var pc = '#8C7853';
    }
    ctx.beginPath();
    ctx.moveTo(55,0);
    ctx.lineTo(80,75);
    ctx.lineTo(105,0);
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#DFD5AA';
    ctx.stroke();
    ctx.beginPath();
    ctx.fillStyle = pc;
    ctx.arc(80,80,12,0,2*Math.PI,false);
    ctx.fill();
    ctx.font = '20px Arial';
    //ctx.fillStyle = '#918926';
    ctx.fillText(pr,79,86);
  }
  // static counter
  //if(typeof drawBigUniform.counter == 'undefined') {
  //  drawBigUniform.counter = 0;
  //}
  //drawBigUniform.counter++;
  // draw flag image
  if (flag.length) {
    var img = new Array(); 
    img[id]=new Image(); 
    img[id].onload=function(){
      ctx.drawImage(img[id],23,38);
    }
    img[id].src = flag;
  }
  ctx.restore(); //0
}


// complement
function complementRGB(thisrgb) {
  var r = thisrgb.substr(1,2);
  var g = thisrgb.substr(3,2);
  var b = thisrgb.substr(5,2);
//alert('thisrgb:'+thisrgb);
  if (r==g && r==b) {
    // grayscale
    r = b = g = d2h(255-h2d(r));
  }else {
    var temprgb=new Object();
    temprgb.r=h2d(r);
    temprgb.g=h2d(g);
    temprgb.b=h2d(b);
    temphsv=RGB2HSV(temprgb);
    temphsv.hue=HueShift(temphsv.hue,180.0);
    temprgb=HSV2RGB(temphsv);
    r=d2h(temprgb.r);
    g=d2h(temprgb.g);
    b=d2h(temprgb.b);
  }
  if (r.length==1) r = '0'+r;
  if (g.length==1) g = '0'+g;
  if (b.length==1) b = '0'+b;
  return '#'+r+g+b;
}

function HueShift(h,s) {
  h+=s; 
  while (h>=360.0) h-=360.0;
  while (h<0.0) h+=360.0;
  return h;
}

function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}

function max3(a,b,c) {
  if (a>b){
    if (a>c) return a;
    else return c;
  }else if (b>c) return b;
  else return c;
}

function min3(a,b,c) {
  if (a<b){
    if (a<c) return a;
    else return c;
  }else if (b<c) return b;
  else return c;
}

function RGB2HSV(rgb) {
  hsv = new Object();
  var max=max3(rgb.r,rgb.g,rgb.b);
  var dif=max-min3(rgb.r,rgb.g,rgb.b);
  hsv.saturation=(max==0.0)?0:(100*dif/max);
  if (hsv.saturation==0) hsv.hue=0;
  else if (rgb.r==max) hsv.hue=60.0*(rgb.g-rgb.b)/dif;
  else if (rgb.g==max) hsv.hue=120.0+60.0*(rgb.b-rgb.r)/dif;
  else if (rgb.b==max) hsv.hue=240.0+60.0*(rgb.r-rgb.g)/dif;
  if (hsv.hue<0.0) hsv.hue+=360.0;
  hsv.value=Math.round(max*100/255);
  hsv.hue=Math.round(hsv.hue);
  hsv.saturation=Math.round(hsv.saturation);
  return hsv;
}

// RGB2HSV and HSV2RGB are based on Color Match Remix [http://color.twysted.net/]
// which is based on or copied from ColorMatch 5K [http://colormatch.dk/]
function HSV2RGB(hsv) {
  var rgb=new Object();
  if (hsv.saturation==0) {
     rgb.r=rgb.g=rgb.b=Math.round(hsv.value*2.55);
  } else {
      hsv.hue/=60;
      hsv.saturation/=100;
      hsv.value/=100;
      var i=Math.floor(hsv.hue);
      var f=hsv.hue-i;
      var p=hsv.value*(1-hsv.saturation);
      var q=hsv.value*(1-hsv.saturation*f);
      var t=hsv.value*(1-hsv.saturation*(1-f));
      switch(i) {
        case 0: rgb.r=hsv.value; rgb.g=t; rgb.b=p; break;
        case 1: rgb.r=q; rgb.g=hsv.value; rgb.b=p; break;
        case 2: rgb.r=p; rgb.g=hsv.value; rgb.b=t; break;
        case 3: rgb.r=p; rgb.g=q; rgb.b=hsv.value; break;
        case 4: rgb.r=t; rgb.g=p; rgb.b=hsv.value; break;
        default: rgb.r=hsv.value; rgb.g=p; rgb.b=q;
      }
      rgb.r=Math.round(rgb.r*255);
      rgb.g=Math.round(rgb.g*255);
      rgb.b=Math.round(rgb.b*255);
  }
  return rgb;
}


function getAbsoluteOffsetTop(obj) {
  var top = obj.offsetTop;
  var parent = obj.offsetParent;
  while (parent != document.body) {
    top += parent.offsetTop;
    parent = parent.offsetParent;
  }
  return top;
}


function getAbsoluteOffsetLeft(obj) {
  var left = obj.offsetLeft;
  var parent = obj.offsetParent;
  while (parent != document.body) {
    left += parent.offsetLeft;
    parent = parent.offsetParent;
  }
  return left;
}


function getObj(id) {
  if (ie) {return document.all[id];} 
  else {return document.getElementById(id);}
}


function getCookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
  offset = document.cookie.indexOf(search)
  if (offset != -1) {
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
    }
  }
  return returnvalue;
}

function setSlower() {
  var speed = getCookie("atSoccerRefresh")
  speed++;
  document.cookie="atSoccerRefresh="+speed;
  if (document.all) refreshtime.innerHTML=speed;
  else if (document.getElementById) document.getElementById("refreshtime").innerHTML=speed;
}

function setFaster() {
  var speed = getCookie("atSoccerRefresh")
  if (speed>4) speed--;
  document.cookie="atSoccerRefresh="+speed;
  if (document.all) refreshtime.innerHTML=speed;
  else if (document.getElementById) document.getElementById("refreshtime").innerHTML=speed;
}


function ajaxRequest(){
  //activeX versions to check for in IE
  var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
  //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  if (window.ActiveXObject){
    for (var i=0; i<activexmodes.length; i++){
      try{
        return new ActiveXObject(activexmodes[i])
      }catch(e){
        //suppress error
      }
    }
  // if Mozilla, Safari etc
  }else if (window.XMLHttpRequest)
    return new XMLHttpRequest()
  else return false
}


