$(document).ready(function(){
	//document.cookie = "avastCoMLocale=cs-cz";
	
	if(document.cookie)
	{
		for (var i = 0;i<document.fDownload.download.length;i++)
		{
			if (document.fDownload.download.options[i].value == getCookie("avastComLocale"))
			{
				document.fDownload.download.options[i].selected = true;	
			}
		}
	}				
	else document.fDownload.download.options[0].selected=true;
	SelectDownload(document.fDownload.download);
});

function SelectDownload(dForm)
{
	$.ajax({
		type: "POST",
		url: RootPath+"dataweb/versions5_win.xml",
		cache: false,
		dataType: ($.browser.msie)? "text": "html",
		beforeSend: function(){
			$("#dButton").css("visibility","hidden");
		},
		success: function(data){
			var xml;
			 if ($.browser.msie) {
			   xml = new ActiveXObject("Microsoft.XMLDOM");
			   xml.async = false;
			   xml.loadXML(data);
			 } else {
			   xml = data;
			 }

			$(xml).find("product[internalId=FA]").each(function(){				
				// exclusivity for French and English, valid until 23.1.2010
				//var d = new Date("23 Jan 2010 GMT");
				//d.setHours(13);
				//if (d >= new Date()) {
					if (dForm.value.substring(0,2) == 'fr'){
						$("#dButton").attr("href", "http://logiciels.telechargement.fr/redirprod.html?N=4048");
					} else if (dForm.value.substring(0,2) == 'en'){
						$("#dButton").attr("href", "http://download.cnet.com/Avast-Free-Antivirus/3000-2239_4-10019223.html?part=dl-85737&subj=dl&tag=button");
					} else if (dForm.value.substring(0,2) == 'es'){
						$("#dButton").attr("href", "http://avast.softonic.com/?ptn=avast");
					} else if (dForm.value.substring(0,2) == 'pt'){
						$("#dButton").attr("href", "http://superdownloads.com.br/download/108/avast-home/");
					} else if (dForm.value.substring(0,2) == 'de'){
						$("#dButton").attr("href", "http://www.chip.de/downloads/avast-Antivirus_13010163.html");
					} else if (dForm.value.substring(0,2) == 'cs'){
						$("#dButton").attr("href", "http://www.stahuj.centrum.cz/utility_a_ostatni/antiviry/kompletni/avast/");
					} else if (dForm.value.substring(0,2) == 'it'){
						$("#dButton").attr("href", "http://download.html.it/software/vedi/8220/avast-free-antivirus/");
					} else if (dForm.value.substring(0,2) == 'pl'){
						$("#dButton").attr("href", "http://www.dobreprogramy.pl/avast-Free-Antivirus,Program,Windows,13266.html");
					} else if (dForm.value.substring(0,2) == 'ja'){
						$("#dButton").attr("href", "http://www.vector.co.jp/soft/dl/winnt/util/se481564.html");
					} else if (dForm.value.substring(0,2) == 'ru'){
						$("#dButton").attr("href", "http://www.softportal.com/get-2915-avast-home.html");
					} else if ($(this).find("file[language="+dForm.value+"]")){
						if (dForm.value == 'FA-0') {
					 		$("#dButton").attr("href","http://files.avast.com/iavs5x/setup_av_free.exe");
						} else {
							$("#dButton").attr("href","http://files.avast.com/" + $(this).find("file[language="+dForm.value+"]").attr('path'));	
						}
					} 
				//}
				
				$("#dButton").css("visibility","visible");
			});
		},
		error: function(){
		}
	});
}

function getCookie(name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}