﻿function searchDefault(hasFocus) {

	search_field = document.getElementById('query');
	var q;
	q = search_field.value;

	if (hasFocus == 'focus') {

		if (q == defaultSearchText) search_field.value = '';
	}
	else {

		if (q == '') search_field.value = defaultSearchText;
	}
}

function countrySelector() {
	
		var myObject = document.getElementById('country-id');
		var countryID = myObject.value;
				
		if (countryID == 'de') {location.href = 'http://www.lamplan.de/';}
		else if (countryID == 'gb') {location.href = 'http://www.lamplan.co.uk/';}
		else if (countryID == 'it') {location.href = 'http://www.asteclamplan.it/';}
		
		else {
		
			changeCountry(countryID, 'yes');
		}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function selectedCountry() {

	var selectedCountryID = readCookie('lmplcountryid');
	changeCountry(selectedCountryID);
}

function changeCountry(countryID,doRedirect) {

	var doRedirect;
	var countryID;
	var displayData = document.getElementById('country-data');

	createCookie('lmplcountryid',countryID,100);
	if (doRedirect == 'yes') {location.href = countryData[countryID][3];}
	displayData.innerHTML = countryData[countryID][2];
}

function countryDropList() {

	var dropList;
	
	dropList = '<label>';
	dropList += '<select name="country-id" id="country-id" onChange="countrySelector();">';
	dropList += '<option value="">Choose your country</option>';

	for (key in countryData) {

		dropList += ' <option value="'+key+'">'+countryData[key][0]+'</option>'+"\n";
	}
	
	dropList += '</select>';
	dropList += '</label>';

	return dropList;
}

function homeBrands(selectedId) {

	var selectedId;
	var myObject;
	var myLinkName = 'link-';
	var myDivName = 'home-marques-';
	var activeClass = 'marque-active';

	for ( var i=1; i<=5; ++i ){

		myObject = document.getElementById(myDivName+i);
		myObject.style.display = 'none';
		
		myLink = document.getElementById(myLinkName+i);
		myLink.className = '';
	}

	myObject = document.getElementById(myDivName+selectedId);
	myObject.style.display = 'block';
	
	myLink = document.getElementById(myLinkName+selectedId);
	myLink.className = activeClass;
	
}