function uutiset() {
  var e = document.getElementById('ajankohtaista');
  if(e) $.getJSON("http://194.79.19.86/autosofta/portals/news36.php?format=json&callback=?",
  {
	c: "AUTOAREE",
  },
  function(response) {
	var l = response.length;
	
	if(l > 1) {
		e.innerHTML = '<span class="ajankohtaista iso-fontti">Ajankohtaista</span>' + e.innerHTML;
	}
	
	for(var i = 0; i < l; i++) {
		
		var Parent = document.getElementById("ajankohtaiset");
		var NewLI = document.createElement("li");
		var content = response[i];
		
		//NewLI.setAttribute("class", "sliderImage");
		//NewLI.setAttribute("style", "display: none;");
		//NewLI.setAttribute("onclick", "location.href = 'index.php?p=2&v=" + response[i]["id"] + "'");
		if(content != "") {
			NewLI.innerHTML = content;
			
			Parent.insertBefore(NewLI, Parent.firstChild);
		}
	}
  });
}

function mainos() {
  var e = document.getElementById('mainos');  
  if(e) $.getJSON("http://194.79.19.86/autosofta/portals/mainos36.php?format=json&callback=?",
  {
	c: "AUTOAREE",
  },
  function(response) {
	e.innerHTML=muuta(response);
	
	$('.horizontal_scroller').SetScroller({	velocity: 	 70,
											direction: 	 'horizontal',
											startfrom: 	 'right',
											loop:		 'infinite',
											movetype: 	 'linear',
											onmouseover: 'pause',
											onmouseout:  'play',
											onstartup: 	 'play',
											cursor: 	 'pointer'
										});
  });
}

function maarat() {
  var e = document.getElementById('nav_motot');  
  $.getJSON("http://194.79.19.86/autosofta/portals/ajoneuvojen_maarat36.php?format=json&callback=?",
  {
	c: "AUTOAREE",
  },
  function(response) {
	if(response['motot'] > 0) e.style.display = "block";
  });
}

function lataaSlider() {
  var e = document.getElementById('slider');  

if(e) $.getJSON("http://194.79.19.86/autosofta/portals/ajoneuvontiedot.php?format=json&callback=?",
  {
	c: "AUTOAREE",
  },
  function(response) {
	var l = response.length;
	var position = "top";
	
	for(var i = 0; i < l; i++) {
		
		var Parent = document.getElementById("sliderContent");
		var NewLI = document.createElement("li");
		/*
		if((i % 2) == 0) position = "top";
		else position = "bottom";
		*/
		/*
		var content = '<img src="' + response[i]["kuva"] + '">'
					+ '<span class="' + position + '" style="display: none;"><strong>' + response[i]["merkki"] + '</strong> ' + response[i]["malli"] + ' ' + response[i]["mallitarkenne"] + '<br>' + response[i]["vuosimalli"] + ', ' + response[i]["km"] + 'km</span>';
		*/
		
		
		var content = '<img src="' + response[i]["kuva"] + '">'
					+ '<span class="' + position + '" style="display: none;"><table cellspacing="0" cellpadding="0" width="100%"><tr><td width="70%"><strong>' + response[i]["merkki"] + '</strong> ' + response[i]["malli"] + ' ' + response[i]["mallitarkenne"] + '</td><td rowspan="2" class="s-hinta">' + response[i]["pyyntihinta"] + '&euro;</td></tr><tr><td>' + response[i]["vuosimalli"] + ', ' + response[i]["km"] + 'km</td></tr></table></span>';
		
		var page = 2;
		
		if(response[i]["kaytetty"] == 1) page = 3;
		
		NewLI.setAttribute("class", "sliderImage");
		NewLI.setAttribute("style", "display: none;");
		NewLI.setAttribute("onclick", "location.href = 'index.php?p=" + page + "&ty=" + response[i]["ty"] + "&v=" + response[i]["id"] + "'");
		NewLI.innerHTML = content;
		
		Parent.insertBefore(NewLI, Parent.firstChild);
	}
	
	/* Lisätään yrityksen kuva listan ensimmäiseksi */
	var content = '<img src="img/julkisivu.jpg">'
				+ '<span class="' + position + '" style="display: none;"><strong>AutoAreena</strong><br>Moukarinkuja 6, 04300 Tuusula</span>';
	
	NewLI.setAttribute("class", "sliderImage");
	NewLI.setAttribute("style", "display: none;");
	NewLI.innerHTML = content;
	
	Parent.insertBefore(NewLI, Parent.firstChild);
	
	
	$('#slider').s3Slider({
		timeOut: 4000 
	});
  });
  
}

function muuta(string) {
	var etsi = new Array("\u00a4");
	var korvaa = new Array("&euro;");
	
	for (var i=0; i < etsi.length; i++) {
		var lista = new Array();
		lista = string.split(etsi[i]);
		string = lista.join(korvaa[i]);
	}

	return string;
} 

function esittely() {
	var e = document.getElementById('esittely');
	
	e.style.visibility = "visible";
	
	$("#esittely-teksti").typewriter();
	
	setTimeout("lataa(); $('#esittely').fadeOut(400);", 6000);
}

function lataa() {
	if(document.getElementById('mainos').innerHTML == "" || document.getElementById('ajankohtaista').innerHTML == "") {
		maarat();
		uutiset();
		mainos();
		lataaSlider();
	}
}

function tarkista() {
	var e1 = document.getElementById('merkki');
	var e2 = document.getElementById('malli');
	var e3 = document.getElementById('vuosimalli');
	var e4 = document.getElementById('kilometrit');
	var e5 = document.getElementById('hintaarvio');
	var e6 = document.getElementById('nimi');
	var e7 = document.getElementById('puhelin');
	var e8 = document.getElementById('email');
	var ok = true;
	
	if(e1.value == "") {
		ok = false;
		alert('Lisää autosi merkki');
	} else {
		if(e2.value == "") {
			ok = false;
			alert('Lisää autosi malli');
		} else {
			if(!IsNumeric(e3.value)) {
				ok = false;
				alert('Lisää autosi vuosimalli');
			} else {
				if(!IsNumeric(e4.value)) {
					ok = false;
					alert('Lisää autosi kilometrit');
				} else {
					if(!IsNumeric(e5.value)) {
						ok = false;
						alert('Lisää oma hinta-arviosi');
					} else {
						if(e6.value == "") {
							ok = false;
							alert('Lisää nimesi');
						} else {
							if(e7.value == "" && e8.value == "") {
								ok = false;
								alert('Lisää puhelinnumerosi tai sähköpostiosoitteesi');
							}
						}
					}
				}
			}
		}
	}
	
	return ok;
}

function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
