$(document).ready(function () {
	$("div.faqLine > a").click(function (event) {
		if ($(this).parents("div.faqLine").siblings(".faqAnswer").is(':visible')) {
			$(this).parents("div.faqLine").siblings(".faqAnswer").slideToggle("slow");
		} else {
			$(document).find("div.faqAnswer").each(function (i) {
				if ($(this).is(':visible')) {
					$(this).slideToggle("slow");
				}
			});
			$(this).parents("div.faqLine").siblings(".faqAnswer").slideToggle("slow");
		}
		event.preventDefault();
	});
});

notificationIsOpen = false;

btnOn = new Array();
btnOn["btnChiptuning"] = new Image(); btnOn["btnChiptuning"].src = root + imgs + 'btn.chiptuning.on.gif';
btnOn["btnDimsport"] = new Image(); btnOn["btnDimsport"].src = root + imgs + 'btn.dimsport.on.gif';
btnOn["btnDealers"] = new Image(); btnOn["btnDealers"].src = root + imgs + 'btn.dealers.on.gif';
btnOn["btnContact"] = new Image(); btnOn["btnContact"].src = root + imgs + 'btn.contact.on.gif';
btnOff = new Array();
btnOff["btnChiptuning"] = new Image(); btnOff["btnChiptuning"].src = root + imgs + 'btn.chiptuning.off.gif';
btnOff["btnDimsport"] = new Image(); btnOff["btnDimsport"].src = root + imgs + 'btn.dimsport.off.gif';
btnOff["btnDealers"] = new Image(); btnOff["btnDealers"].src = root + imgs + 'btn.dealers.off.gif';
btnOff["btnContact"] = new Image(); btnOff["btnContact"].src = root + imgs + 'btn.contact.off.gif';

function hiLite(which) {
	try {
		elementId = which.id;
		document.getElementById(elementId).src = btnOn[elementId].src;
	} catch (error) {
		// enjoy the silence
	}
}

function loLite(which) {
	try {
		elementId = which.id;
		document.getElementById(elementId).src = btnOff[elementId].src;
	} catch (error) {
		// enjoy the silence
	}
}

function updateSearchModels() {
	clearSelectOptions(document.getElementById('searchModelId'));
	clearSelectOptions(document.getElementById('searchEngineId'));
	clearSelectOptions(document.getElementById('searchSetId'));
	addSelectOption(document.getElementById('searchEngineId'),'','Alege motorizarea masinii...');
	addSelectOption(document.getElementById('searchSetId'),'','Alege setul de tuning...');
	makeid = document.getElementById('searchMakeId').value;
	myUrl = root + 'usersearchselects.php?get=models&makeid=' + makeid;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			try {
				models = myResponse.split("<!-- split -->");
				theSelect = document.getElementById('searchModelId');
				for (i in theSelect.options) {
					theSelect.remove(i);
				}
				for (k in models) {
					splat = models[k].split(":::");
					try {
						theSelect.options[k] = new Option(splat[1],splat[0],false,false);
					} catch (e) {
						var newOption = document.createElement('option');
						newOption.value = splat[0];
						newOption.text = splat[1];
						theSelect.add(newOption);
					}
				}
			} catch (e) {
				//silence verbose browsers like IE
			}
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function updateSearchEngines() {
	clearSelectOptions(document.getElementById('searchSetId'));
	addSelectOption(document.getElementById('searchSetId'),'','Alege setul de tuning...');
	makeid = document.getElementById('searchMakeId').value;
	modelid = document.getElementById('searchModelId').value;
	myUrl = root + 'usersearchselects.php?get=engines&makeid=' + makeid + '&modelid=' + modelid;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			try {
				engines = myResponse.split("<!-- split -->");
				theSelect = document.getElementById('searchEngineId');
				for (i in theSelect.options) {
					theSelect.remove(i);
				}
				for (k in engines) {
					splat = engines[k].split(":::");
					try {
						theSelect.options[k] = new Option(splat[1],splat[0],false,false);
					} catch (e) {
						var newOption = document.createElement('option');
						newOption.value = splat[0];
						newOption.text = splat[1];
						theSelect.add(newOption);
					}
				}
			} catch (e) {
				//silence verbose browsers like IE
			}
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function updateSearchSets() {
	makeid = document.getElementById('searchMakeId').value;
	modelid = document.getElementById('searchModelId').value;
	engineid = document.getElementById('searchEngineId').value;
	myUrl = root + 'usersearchselects.php?get=sets&makeid=' + makeid + '&modelid=' + modelid + '&engineid=' + engineid;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			try {
				sets = myResponse.split("<!-- split -->");
				theSelect = document.getElementById('searchSetId');
				for (i in theSelect.options) {
					theSelect.remove(i);
				}
				for (k in sets) {
					splat = sets[k].split(":::");
					try {
						theSelect.options[k] = new Option(splat[1],splat[0],false,false);
					} catch (e) {
						var newOption = document.createElement('option');
						newOption.value = splat[0];
						newOption.text = splat[1];
						theSelect.add(newOption);
					}
				}
			} catch (e) {
				//silence verbose browsers like IE
			}
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function clearSelectOptions(which) {
	try {
		for (i in which.options) {
			which.remove(i);
			which.options[i] = null;
		}
	} catch (e) {
		// do nothing
	}
}

function addSelectOption(which,theValue,theText) {
	try {
		which.options[which.length] = new Option(theText,theValue,false,false);
	} catch (e) {
		var newOption = document.createElement('option');
		newOption.value = theValue;
		newOption.text = theText;
		which.add(newOption);
	}
}

function subscribeEmail () {
	myUrl = root + 'subscribeemail.php?email=' + encodeURIComponent(document.getElementById('subscribeBox').value);
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			showNotification(myResponse);
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function showNotification(notificationText) {
	document.getElementById('notificationContainer').innerHTML = notificationText;
	if (window.innerWidth) {
		center = parseInt(window.innerWidth/2);
	} else {
		center = parseInt(document.body.offsetWidth/2);
	}
	center = center - 234;
	center = center + 'px';
	document.getElementById('darkOverlayer').style.display = 'block';
	document.getElementById('notificationOutline').style.display = 'block';
	document.getElementById('notificationOutline').style.left = center;
	window.scrollTo(0,0);
	var speed = 3;
	var timer = 0;
	for (i = 1; i <= 100; i++) {
		if (notificationIsOpen == false) {
			if (i <= 50) {
				setTimeout("changeOpacity(" + i + ",'darkOverlayer')",(timer * speed));
			}
			setTimeout("changeOpacity(" + i + ",'notificationOutline')",(timer * speed));
		}
		timer++;
	}
}

function hideNotification() {
	var speed = 3;
	var timer = 0;
	for (i = 100; i >= 0; i--) {
		if (i <= 50) {
			setTimeout("changeOpacity(" + i + ",'darkOverlayer')",(timer * speed));
		}
		setTimeout("changeOpacity(" + i + ",'notificationOutline')",(timer * speed));
		timer++;
	}
}

window.onscroll = function disableScroll() {
	if (notificationIsOpen != '') {
		window.scrollTo(0,0);
	}
}

function changeOpacity(opacity,elementId) { 
    var object = document.getElementById(elementId).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
	if (opacity == 0) {
		document.getElementById('darkOverlayer').style.display = 'none';
		document.getElementById('notificationOutline').style.display = 'none';
		projectIsOpen = false;
	}
}

function exchangePrice() {
	currentCurrency = document.getElementById('currency').innerHTML;
	if (currentCurrency == "EUR") {
		basePrice = parseInt(document.getElementById('spanPrice').innerHTML);
		exchangedPrice = basePrice * exchangeRate * 1.24;
		exchangedPrice = Math.ceil(exchangedPrice);
		document.getElementById('spanPrice').innerHTML = number_format(exchangedPrice,0,"","");
		try {
			discountPrice = parseInt(document.getElementById('fontPriceNew').innerHTML);
			exchangedPrice = discountPrice * exchangeRate * 1.24;
			exchangedPrice = Math.ceil(exchangedPrice);
			document.getElementById('fontPriceNew').innerHTML = number_format(exchangedPrice,0,"","");
		} catch (e) {
			// just enjoy the silence
		}
		document.getElementById('currency').innerHTML = "RON";
		document.getElementById('lnkExchange').innerHTML = "Afiseaza pretul fara TVA in EUR";
	} else {
		document.getElementById('currency').innerHTML = "EUR";
		document.getElementById('spanPrice').innerHTML = basePrice;
		try {
			document.getElementById('fontPriceNew').innerHTML = discountPrice;
		} catch (e) {
			// just enjoy the silence
		}
		document.getElementById('lnkExchange').innerHTML = "Afiseaza pretul cu TVA in RON";
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
	var n = number, prec = decimals;
	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
	var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
	var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
	var abs = Math.abs(n).toFixed(prec);
	var _, i;
	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;
		_[0] = s.slice(0,i + (n < 0)) +
		_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s = _.join(dec);
	} else {
		s = s.replace('.', dec);
	}
	return s;
}

function showDescription(whichSet) {
	myUrl = root + 'setdescription.php?id=' + whichSet;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			showNotification(myResponse);
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

answerContainerId = "";
function showAnswer(whichAnswer) {
	tmpContainerId = "answer" + whichAnswer;
	if (answerContainerId != "" && answerContainerId != tmpContainerId) {
		document.getElementById(answerContainerId).style.display = 'none';
	}
	answerContainerId = "answer" + whichAnswer;
	if (document.getElementById(answerContainerId).style.display != "inline") {
		document.getElementById(answerContainerId).style.display = 'inline';
	} else {
		document.getElementById(answerContainerId).style.display = 'none';
		answerContainerId = '';
	}
}

function updateDealerCities() {
	myUrl = root + 'getdealercities.php?country=' + document.getElementById("country").value;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			cities = myResponse.split("<!-- split -->");
			theSelect = document.getElementById('city');
			for (i in theSelect.options) {
				theSelect.remove(i);
			}
			try {
				theSelect.options[0] = new Option("- toate orasele -","",false,false);
			} catch (e) {
				var newOption = document.createElement('option');
				newOption.value = "";
				newOption.text = "- toate orasele -";
				theSelect.add(newOption);
			}
			if (document.getElementById("country").value != "") {
				for (k in cities) {
					idx = parseInt(k) + 1;
					splat = cities[k].split(":::");
					try {
						theSelect.options[idx] = new Option(splat[1],splat[0],false,false);
					} catch (e) {
						var newOption = document.createElement('option');
						newOption.value = splat[0];
						newOption.text = splat[1];
						theSelect.add(newOption);
					}
				}
			}
			updateDealersList();
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function updateDealersList() {
	myUrl = root + 'getdealers.php?country=' + document.getElementById("country").value + '&city=' + document.getElementById("city").value;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			myResponse = xmlHttp.responseText;
			blocks = myResponse.split("<!-- split -->");
			for (k in blocks) {
				try {
					splat = blocks[k].split(":::");
					blockID = "dealer" + splat[0];
					document.getElementById(blockID).style.display = splat[1];
				} catch(e) {
					// just enjoy the silence
				}
			}
		}
	}
	xmlHttp.open("GET",myUrl,true);
	xmlHttp.send(null);
}

function updateDealerForm() {
	accountType = document.getElementById("type_pj").checked == true ? "pj" : "pf";
	accountCountry = document.getElementById("country").value;
	if (accountCountry == "RO") {
		document.getElementById('selectCity').style.display = "inline";
		document.getElementById('selectCity').disabled = false;
		document.getElementById('textCity').style.display = "none";
		document.getElementById('textCity').disabled = true;
	} else {
		document.getElementById('selectCity').style.display = "none";
		document.getElementById('selectCity').disabled = true;
		document.getElementById('textCity').style.display = "inline";
		document.getElementById('textCity').value = "";
		document.getElementById('textCity').disabled = false;
	}
	if (accountType == "pj") {
		document.getElementById('lblCompany').style.color = '#999999';
		document.getElementById('txtCompany').disabled = false;
		document.getElementById('lblCNPCUI').innerHTML = 'Cod unic de inregistrare (C.I.F.):';
		document.getElementById('lblCIRC').innerHTML = 'Numar Registrul Comertului:';
	} else {
		document.getElementById('lblCompany').style.color = '#444444';
		document.getElementById('txtCompany').disabled = true;
		document.getElementById('lblCNPCUI').innerHTML = 'Cod numeric personal:';
		document.getElementById('lblCIRC').innerHTML = 'Carte de identitate:';
	}
}
