/***************************************************************/
/*  Checking country
/***************************************************************/
var validCountry = 'ar br cl co ec mx pe ve uy pr us';
var path = location.href;
var patern_home   = new RegExp("http://[^/]*.terra.(?:com\.)?([^/]*)/?(.*)","gi"); 
    path.match(patern_home);
var pais = RegExp.$1;

// Verificacao do pais pelo parametro country
var patern_country = new RegExp("(.*)country=?([^&]*)?(.*)", "gi");
    path.match(patern_country);
var country = RegExp.$2;
//alert(RegExp.$2 + ' - ' + RegExp.$3);

if (validCountry.indexOf(country) != -1){
	//alert('Country: '+country);
	pais = country;
}

if (pais == "es") {
  pais = "us";
}

var iFrames;
var tag;

// funcion fecha/hora
function convertGMTtoLocal(date,hour) {
  var newRes = new Array();

  var res_date;	  
  var res_hour;
  var dia;
  var mes;
  var year;
  var horas;
  var minutos; 
  var valid_date;
  var valid_hour;
  var hh;
  var mi;
  var refCity;

  var monthsBr = ["janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"];
  var monthsEs = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];

  var dowBr = ["domingo", "segunda-feira", "terça-feira", "quarta-feira", "quinta-feira", "sexta-feira", "sábado"];
  var dowEs = ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"];

  var months;
  var dow;

  if (pais == 'br') {
    months = monthsBr;
    dow = dowBr;
  } else {
    months = monthsEs;
    dow = dowEs;
  }

  /***************************************************************/
  /*  Checking date
  /***************************************************************/
  var req_date = /^(\d{2})\/(\d{2})\/(\d{4})?$/i;
  if (res_date = req_date.exec(date)) {		  
     dia=parseInt(eval(res_date[1])); 
     mes=parseInt(eval(res_date[2])); 
     year=res_date[3]; 		 
     valid_date = true;
  }	else {
     valid_date = false;
  }
	  
  var req_hour = /^(\d{2}):(\d{2})?$/i;	  
  if (res_hour = req_hour.exec(hour)) { 
    horas   = parseInt(eval(res_hour[1]));
    minutos = res_hour[2];		 
    valid_hour = true;
  }	else {
    valid_hour = false;
  }
      
  /***************************************************************/
  /*  Checking GMT TIME
  /***************************************************************/
  var GMTDif=0;

  if       (pais == 'ar') { GMTDif=-3; refCity = 'Buenos Aires'; refCountry = 'Hora de Argentina'; }
  else if  (pais == 'br') { GMTDif=-3; refCity = 'Brasilia'; refCountry = 'Horário de Brasília'; }
  else if  (pais == 'cl') { GMTDif=-4; refCity = 'Santiago'; refCountry = 'Hora de Chile'; }
  else if  (pais == 'co') { GMTDif=-5; refCity = 'Bogotá'; refCountry = 'Hora de Colombia'; }
  else if  (pais == 'ec') { GMTDif=-5; refCity = 'Quito'; refCountry = 'Hora de Ecuador'; }
  else if  (pais == 'mx') { GMTDif=-5; refCity = 'Ciudad de México'; refCountry = 'Hora de México'; }
  else if  (pais == 'pe') { GMTDif=-5; refCity = 'Lima'; refCountry = 'Hora de Perú'; }
  else if  (pais == 'pr') { GMTDif=-4; refCity = 'San Juan'; refCountry = 'Hora de Puerto Rico'; }
  else if  (pais == 'us') { GMTDif=-4; refCity = 'Washington'; refCountry = 'Hora de Estados Unidos'; }
  else if  (pais == 'uy') { GMTDif=-3; refCity = 'Montevideo'; refCountry = 'Hora de Uruguay'; }
  else if  (pais == 've') { GMTDif=-4; refCity = 'Caracas'; refCountry = 'Hora de Venezuela'; }
  else {GMTDif=-3; refCity = ''; refCountry = ''; }

  /***************************************************************/
  /*  Checking REAL DATE
  /***************************************************************/
  //var dias=0;
  //if (horas<0)  { horas=24+horas;  dias=-1; }
  //if (horas>23) { horas=horas-23;  dias=1;  }

  var xDate = new Date();
  if (valid_date) { 
    xDate.setDate(dia); 
    xDate.setMonth(mes-1); 
    xDate.setYear(year); 
  }	  
  if (valid_hour) {	
    xDate.setHours(horas); 
    xDate.setMinutes(minutos); 
    xDate.setSeconds(0);
  }	 

  /***************************************************************/
  /*  GMT TIME
  /***************************************************************/
  xDate.setHours(xDate.getHours() + GMTDif);
	  
  /***************************************************************/
  /*  Building Output
  /***************************************************************/
  if (valid_date) { 
    dia = xDate.getDate();
    dia = (dia.toString().length == 1)?'0':'';
    dia  += xDate.getDate(); 
    mes  = xDate.getMonth()+1; 
    year = xDate.getYear(); 
    yearstr = new String(year);
    date = dia;
    date += "/" + ((mes<10) ? "0" : "") + mes;
    date += "/" + yearstr.substr(2,2);
  }

  if (valid_hour) { 
    horas    = parseInt(xDate.getHours()); 
	  minutos  = parseInt(xDate.getMinutes()); 
    hh = ((horas<10) ? "0" : "") + horas;
    mi = ((minutos<10) ? "0" : "") + minutos;
		hour = hh + "h";

    if (mi > 0) {
      hour += mi;
    }
  }					  

  var formatedDayMonYear = xDate.getDate() + " de " + months[(mes - 1)];
  var formatedDate = xDate.getDate() + " de " + months[(mes - 1)] + " - " + dow[xDate.getDay()];
  var dateDDMM = xDate.getDate() + "/" + mes;

  newRes[0] = date;
  newRes[1] = hour;
  newRes[2] = xDate.getDay();
  newRes[3] = xDate.getDate();
  newRes[4] = xDate.getMonth();
  newRes[5] = hh;
  newRes[6] = mi;
  newRes[7] = refCity;
  newRes[8] = refCountry;
  newRes[9] = formatedDate;
  newRes[10] = formatedDayMonYear;
  newRes[11] = dateDDMM;

  return newRes;
}
   
function getPais() {
  var npais = "";

  if       (pais == 'ar') { npais="Argentina"; }
  else if  (pais == 'br') { npais="Brasil"; }
  else if  (pais == 'cl') { npais="Chile"; }
  else if  (pais == 'co') { npais="Colombia"; }
  else if  (pais == 'ec') { npais="Ecuador"; }
  else if  (pais == 'mx') { npais="México DF"; }
  else if  (pais == 'pe') { npais="Perú"; }
  else if  (pais == 'pr') { npais="Puerto Rico"; }
  else if  (pais == 'us') { npais="Estados Unidos (EST)"; }
  else if  (pais == 'uy') { npais="Uruguay"; }
  else if  (pais == 've') { npais="Venezuela"; }
             
  document.write(npais);
}

function setIFrames(idSport) {
  var iFrameHeader;
  var iFrameBarra;
  var iFrameMenu;
  var iFrameFiltroCalendario;
  var iFrameFooter;

  var urlStaticIncludes = "/include/" + pais + "/";
  var urlDynamicIncludes = "";

  if (pais == 'br') {
    iFrames["mainHeader"] = "http://www.terra.com.br/barra50.htm";
    iFrames["header"] = urlStaticIncludes + "cabecalho.htm";
    iFrames["banner"] = urlStaticIncludes + "banner.htm";
    iFrames["barra"] = urlStaticIncludes + "menu_horizontal.htm";
    iFrames["barra_cal"] = urlStaticIncludes + "menu_horizontal_cal.htm";
    iFrames["barra_res"] = urlStaticIncludes + "menu_horizontal_res.htm";
    iFrames["barra_med"] = urlStaticIncludes + "menu_horizontal_med.htm";
    iFrames["menu"] = urlStaticIncludes + "menus.htm";
    iFrames["uv"] = urlStaticIncludes + "uv.htm";
    iFrames["footer"] = "http://www.terra.com.br/rodape.htm";

    urlDynamicIncludes = "/br/includes/";
  } else { // latam
    iFrames["mainHeader"] = "http://www." + pais + ".terra.com/include/cabecera.htm";
    iFrames["header"] = "http://www." + pais + ".terra.com/include/panamericanos2007/cabecera.htm";
    iFrames["banner"] = "http://www." + pais + ".terra.com/include/panamericanos2007/banner_int728.htm";
    iFrames["barra"] = "http://www." + pais + ".terra.com/shared/panamericanos2007/incl/menu_horiz_port_01.htm";
    iFrames["menu"] = "http://www." + pais + ".terra.com/include/panamericanos2007/menu_pan.htm";
    iFrames["menu2"] = "http://www." + pais + ".terra.com/include/panamericanos2007/banner_int138.htm";
    iFrames["uv"] = "http://www." + pais + ".terra.com/include/panamericanos2007/uv.htm";
    iFrames["footer"] = "http://www." + pais + ".terra.com/include/pie_blanco.htm";

    urlDynamicIncludes = "/es/includes/";
  }

  iFrames["filtro_calendario"] = urlDynamicIncludes + "filtro_agenda.html";
  iFrames["filtro_categorias"] = urlDynamicIncludes + "filtro_categorias_" + idSport + ".html";
  iFrames["filtro_resultados"] = urlDynamicIncludes + "filtro_resultados.html";
  iFrames["filtro_cat_resultado"] = urlDynamicIncludes + "filtro_cat_resultado_" + idSport + ".html";
  iFrames["filtro_medalhas"] = urlDynamicIncludes + "filtro_medalhas.html";
  iFrames["filtro_atletas"] = urlDynamicIncludes + "filtro_atletas.html";

  return iFrames;
}

function printIFrame(id, height, width, scrolling, frameborder, marginheight, marginwidth, server_host) {
  if (!server_host){
	server_host='';
  }
  if (pais == 'br') {
    if (id == "banner") {
      width = 750;
      height = 150;
    } else if (id == "menu") {
      width = 138;
      height = 960;
    }
  } else if (pais == 'ar') {
    if (id == "mainHeader") {
      width = 781;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'co') {
    if (id == "mainHeader") {
      width = 781;
      height = 58;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'cl') {
    if (id == "mainHeader") {
      width = 781;
      height = 62;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'ec') {
    if (id == "mainHeader") {
      width = 781;
      height = 55;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'mx') {
    if (id == "mainHeader") {
      width = 781;
      height = 44;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'us') {
    if (id == "mainHeader") {
      width = 780;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 've') {
    if (id == "mainHeader") {
      width = 780;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'pe') {
    if (id == "mainHeader") {
      width = 780;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'pr') {
    if (id == "mainHeader") {
      width = 780;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  } else if (pais == 'uy') {
    if (id == "mainHeader") {
      width = 780;
      height = 51;
    } else if (id == "footer") {
      width = 780;
      height = 95;
    } else if (id == "banner") {
      width = 750;
      height = 120;
    }
  }

  if (pais != 'br' && id == 'banner') {
    marginheight = 10;
    marginwidth = 20;
  }

  document.write("<iframe src=\""+server_host + iFrames[id] + "\" height=\"" + height + "\" width=\"" + width + "\" scrolling=\"" + scrolling + "\" frameborder=\"" + frameborder + "\" marginheight=\"" + marginheight + "\" marginwidth=\"" + marginwidth + "\"></iframe>");
}

function abre(url,janela,larg,alt,scroll){
  if (!scroll) { scroll='auto' }
  window.open(url,janela,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable=no,copyhistory=no,width="+larg+",height="+alt);
}

function enquete(jan) {
  abre('',jan,290,540);
}

function abreurl(valor) {
  document.location.href=valor;
}

function abretop(valor) {
  if (valor != "none") {
    top.location.href=valor;
  }
}

function AbrePopEnviar(UrlPagE,NameE,Wi,He,scrolling) {
  if (!scrolling) { scrolling = 'auto' }
  PopEnviaNoticia = window.open(UrlPagE,NameE,'width='+Wi+',height='+He+',scrollbars='+scrolling+',toolbar=no,location=no,status=no,menubar=no,resizable=no,left=450,top=5');
}

function AbrePopImprimir(UrlPagI,NameI,Wi,He,scrolling) {
  if (!scrolling) { scrolling = 'auto' }
  PopImprimiNoticia = window.open(UrlPagI,NameI,'width='+Wi+',height='+He+',scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,resizable=no,left=450,top=5');
}

function PrintPage() {
  if (window.print)
    window.print()
  else
    alert("O script não conseguiu envira o documento diretamente para impressão,nPressione Ctrl+P ou selecione Imprimir no menu Arquivo para imprimir esta página.");
}

function clearField(field) {
  if ((field.value == 'Digite o nome do atleta') || (field.value == 'Escriba el nombre del atleta')) { 
    field.value = ''; 
  }
}

function printImage(path, alt, params) {
  var imageUrl = "";

  if (pais == 'cl') {
    imageUrl = pais;
  } else if (pais == 'us') {
    imageUrl = "com";
  } else {
    imageUrl = "com." + pais;
  }

  document.write("<img src=\"http://agendaesportiva.img.terra." + imageUrl + path + "\" alt=\"" + alt + "\" " + params + "/>");
}

function printHref(link, target) {
  var newLink = "";

  if (link.substr(0, 7) == "http://") {
    newLink = "<a href=\"" + link + "\" target=\"" + target + "\">";
  } else {
    newLink = "<a href=\"http://www." + pais + ".terra.com/" + link + "\" target=\"" + target + "\">";
  }

  document.write(newLink);
}

function printCloseHref() {
  document.write("</a>");
}

function writeCountry() {
  document.write("<input TYPE=\"hidden\" NAME=\"country\" VALUE=\"" + pais + "\" />");
}
