var oldCol;
var oldA;

function CHMenu(tr, trName4Change, mode) {
	if (mode == 0) {
		oldCol = tr.className;
		tr.className = trName4Change;
	} else {
		tr.className = oldCol;
	}
}

function selectDeselectAllBoxes (ident, check) {
	var e = document.getElementsByName(ident);
	
	for (var c = 0; c < document.forms[ident].elements.length; c++){
		if (document.forms[ident].elements[c].type == 'checkbox'){
			document.forms[ident].elements[c].checked = check;
		}
	}
}

function checkForm(ident,ln) {
	var e = document.getElementsByName(ident);
	
	for (var c = 0; c < document.forms.typesForm.elements.length; c++){
		if (document.forms.typesForm.elements[c].checked){
			return true;
		}
	}
	
	if(ln=='1') alert("Bitte markieren Sie ein Element.");
	else  alert("Пожалуйста, выбирите минимум одно транспортное средство!");
	
	return false;
}

function addRetypeActivateCode(zahl)
{
	var code = zahl;
	var code_result = MD5(code);
	document.insertForm.code.value=code_result;
}

function checkRetypeActivateCode(form,language)
{
	var valide = false;
	
	var retype_zahl = form.code_retype.value;
	var code = form.code.value;
	
	var temp = MD5(retype_zahl);
	var code_result = MD5(retype_zahl);
	
	if(form.code.value==code_result){
		valide = true;
	}
	else{
		valide = false;
		if(language=='0') alert("Не правильный код активации!");
		else alert("Aktivierungscode ist nicht korrekt!");
	}
	
	return valide;
}

function showPOPUPWindowBySettings(site,width,height,top,left,scrollen){
	window.open(site, "_blank", "location=no,directories=no,scrollbars="+scrollen+",status=no,resizable=0,copyhistory=no,width="+width+",height="+height+",left="+left+",top="+top+"");
}

function showPOPUPWindowBySize(site,width,height,scrollen){
	window.open(site, "_blank", "location=no,directories=no,scrollbars="+scrollen+",status=no,resizable=0,copyhistory=no,width="+width+",height="+height+",left=150,top=150");
}

function showPOPUPWindow4Image(site,width,height,scrollen,resizable){
	window.open(site, "_blank", "location=no,directories=no,scrollbars="+scrollen+",status=no,resizable="+resizable+",copyhistory=no,width="+width+",height="+height+",left=150,top=150");
}

function openRegistrationWindowForDealer(seite){
	window.open(seite, "_blank");
}

function viewPics(prefix,aid,pn,preis,marke,waehrung){
	window.open(prefix + "srch/displayAddPics.jsp?aid="+aid+"&picsNumber="+pn+"&preis="+preis+"&marke="+marke+"&waehrung="+waehrung, "_blank", "location=no,directories=no,scrollbars=no,status=no,resizable=0,copyhistory=no,width=340,height=530,left=150,top=150");
}

function showHelpWindow(prefix,s,anker){
	window.open(prefix+"help/help.jsp?rk="+s+"#"+anker, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=778,height=510,left=50,top=50");
}

function showPaymentWindow(s){
	window.open("payment/payment.jsp", "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=530,height=510,left=50,top=50");
}

function showHelpCodeWindow(code){
	window.open("payment/help.jsp?code="+code, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=430,height=210,left=50,top=50");
}

function showPayWindow(){
	window.open("payment/pfrwrd.jsp?AMOUNT=1940.0", "_blank", "location=no,directories=no,scrollbars=no,status=no,resizable=0,copyhistory=no,width=540,height=630,left=50,top=50");
}

function showWindowForAveragePreis(code){
	window.open("averagePreis.jsp?code="+code, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=600,height=300,left=50,top=50");
}

function showWindowForPartner(code){
	window.open("banner/partner.jsp?code="+code, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=500,height=400,left=50,top=50");
}

function showWindowForPrint(prefix,aid,pn,preis,marke,waehrung){
	window.open(prefix+"srch/print.jsp?aid="+aid+"&picsNumber="+pn+"&preis="+preis+"&marke="+marke+"&waehrung="+waehrung+"&pg=print", "_blank", "location=no,directories=no,scrollbars=no,status=no,resizable=0,copyhistory=no,width=620,height=640,left=150,top=150");
}

function showPOPUPindow(site,marke){
	window.open(site+"?#"+marke, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=778,height=510,left=150,top=150");
}

function showPOPUPindow2(site){
	window.open(site, "_blank", "location=no,directories=no,scrollbars=yes,status=no,resizable=0,copyhistory=no,width=550,height=450,left=150,top=150");
}

function setCookie(name, wert, expires){
	var cook = name +"=" + wert;
	if(expires!='null') cook += ";expires="+expires;
	document.cookie = cook;
	//alert(name + ", " + wert + ", " + expires);
}

function setCookie4Vehicle(name,wert,expires,vehicle){
	var cook = name +"=" + wert;
	if(expires!='null') cook += ";expires="+expires;
	document.cookie = cook;
	
	alert("Транспортное средство \""+vehicle+"\" было успешно добавлено в блокнот!\n\nСохранённые в блокнот транспортные средства Вы можете посмотреть\nв меню \"Горячие ссылки / Транспорт\".");
	
	return false;
}

function eraseCookie(name){
   var cook= name +"=; expires=Thu, 01-Jan-70 00:00:01 GMT";
   document.cookie = cook;
}

function CheckInputDigit (event) {
	if ((event.keyCode < 48) || (event.keyCode > 57)) event.returnValue = false; // old implementation
}

function CheckInputDigit222 (event) {
	if (!event)
		event = window.event;
	if (event.which) {
		keycode = event.which;
	} else if (event.keyCode) {
		keycode = event.keyCode;
	}
	
	if((keycode == 8) || (keycode == 37) || (keycode == 39) || (keycode == 46)) return true;
	if ((keycode < 48) || (keycode > 57)) return false;
	
	//if ((event.keyCode < 48) || (event.keyCode > 57)) event.returnValue = false; // old implementation
}

function CheckInputDealerLogin (event) {
	var zeichen = "";
	var result = false;
	
	if (window.Event){
		zeichen = String.fromCharCode(event.which);
	}
	else{
		zeichen = String.fromCharCode(event.keyCode);
	}
	
	//alert(zeichen);
	
	Buchstaben = new Array("-","_","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	
	for(var i = 0; i < Buchstaben.length; i++){
		if(zeichen==Buchstaben[i]){
			event.returnValue = true;
			break;
		}
		else event.returnValue = false;
	}
	
	return result;
}

function describeCount4Description(form,description,size,counter) {
	describetext = document.forms[form].elements[description].value
	ist = describetext.length
	rest = size - ist - 0;
	if (rest < 0) {
		document.forms[form].elements[description].value = describetext.substr(0,size - 0);
		rest = 0;
	}
	document.forms[form].elements[counter].value = rest;
}

function describeCount() {
	describetext = document.insertForm.description.value
	ist = describetext.length
	rest = 1000 - ist - 0;
	if (rest < 0) {
		document.insertForm.description.value = describetext.substr(0,1000 - 0);
		rest = 0;
	}
	document.insertForm.counter.value = rest;
}

function enableFormFillLevelCheck(form){
	return true;
}

function enableFormFillLevelCheckKK(form){
	var valide = false;
	var formLaenge = document.forms.searchForm.length;
	
	var string_1 = "";
	
	var checkBoxCount = 0;
	var selectCount   = 0;
	
	for(i=0;i<formLaenge;++i){
		string_1 = string_1 + "\n" + document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + ", " + document.forms[0].elements[i].type;
		
		
		if(document.forms[0].elements[i].type=='select-one'){
			if(document.forms[0].elements[i].value!='-2' && document.forms[0].elements[i].value!='0' && document.forms[0].elements[i].value!=''){
				selectCount++;
			}
		}
		
		if(document.forms[0].elements[i].type=='checkbox'){
			if(document.forms[0].elements[i].checked){
				checkBoxCount++;
			}
		}
		
		//document.forms[0].elements[i].name
	}
	
	if(selectCount>0 || checkBoxCount>0){
		valide = true;
	}
	
	if(valide == false){
		//alert("Выберите минимум один критерий поиска!");
	}
	
	valide = true;
	
	return valide;
}

function CheckInputMail(){
	//if ((event.keyCode < 48) || (event.keyCode > 57)) event.returnValue = false;
}

function checkFillForm4InfoAndPreis(){
	
	var modell = document.insertForm.main_modell.value;
	
	if(document.insertForm.main_marke.value=='-2'){
		document.insertForm.step.value='vehicleParameters';
	}
	else if(document.insertForm.main_modell.value=='-1' || document.insertForm.main_modell.value.length==0){
		document.insertForm.step.value='vehicleParameters';
	}
	else if(document.insertForm.main_category.value=='-2'){
		document.insertForm.step.value='vehicleParameters';
	}
	else if(document.insertForm.main_price.value.length==0){
		document.insertForm.step.value='vehicleParameters';
	}
	else{
		document.insertForm.isValide.value=true;
	}
}


function checkFillForm4InfoAndPreis2(){
	
	//alert(document.insertForm.main_price.value.length);
	//alert(document.insertForm.ad_i_category.value);
	
	if(document.insertForm.main_price.value.length>0 && document.insertForm.main_category.value!='-2'){
		document.insertForm.save.value = 'true';
		document.insertForm.isFormValide.value = 'true';
	}
}

function isEnteredMailValid(dealerMail){
	var isValid = true;
	
	var count = 0;
	var endOfMail = "";
		
	if(dealerMail.length>0){
		var indexOfAt = dealerMail.indexOf('@');
		var lastIndexOfAt = dealerMail.lastIndexOf('@');
		var indexOfPoint = dealerMail.indexOf('.');
		var lastIndexOfPoint = dealerMail.lastIndexOf('.');
		
		if(lastIndexOfPoint!=-1){
			endOfMail = dealerMail.substring(lastIndexOfPoint, dealerMail.length);
		}
		
		if(indexOfAt==-1 || lastIndexOfAt==-1) isValid = false;
		if(indexOfAt!=lastIndexOfAt) isValid = false;
		if(indexOfPoint==-1 || lastIndexOfPoint==-1) isValid = false;
		if(indexOfAt>lastIndexOfPoint) isValid = false;
		if(lastIndexOfPoint==(dealerMail.length-1)) isValid = false;
		if(lastIndexOfAt==(dealerMail.length-1) || indexOfAt==0) isValid = false;
		if(endOfMail.length<=2 || endOfMail.length>4) isValid = false;
	}
	else isValid = false;
	
	return isValid;
}

// Validate email address
function isValidEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
   	}

	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	
	//validate domain part of the email against all known TLDs as of May 2001 
	var r3 = /\.(a[c-gil-oq-uwz]|b[a-bd-jm-or-tvwyz]|c[acdf-ik-orsuvx-z]|d[ejkmoz]|e[ceghr-u]|f[i-kmorx]|g[abd-ilmnp-uwy]|h[kmnrtu]|i[delm-oq-t]|j[emop]|k[eg-imnprwyz]|l[a-cikr-vy]|m[acdghk-z]|n[ace-giloprtuz]|om|p[ae-hk-nrtwy]|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[admrw]|com|edu|net|org|mil|gov|biz|pro|aero|coop|info|name|museum)$/i;
	
	return (!r1.test(str) && r2.test(str) && r3.test(str));
}
	
function checkFillForm4Saler(){

	//alert(document.insertForm.AGB_JA.checked);
	//alert(document.insertForm.sr_city.value);
	
	//alert(document.insertForm.ad_i_marke.value);
	
	var isMaiValid = isEnteredMailValid(document.insertForm.ad_s_email.value);
	
	//alert(document.insertForm.ad_s_email.value + ", " + isMaiValid);
	
	if(!isMaiValid){//document.insertForm.ad_s_email.value.length==0
		document.insertForm.step.value='salerInfo';
	}
	if(document.insertForm.ad_s_city.value=='-2'){
		document.insertForm.step.value='salerInfo';
	}
	if(document.insertForm.AGB_JA.checked==false){
		document.insertForm.step.value='salerInfo';
	}
	
	//alert(document.insertForm.step.value);
}

function checkFillForm4Saler2(){

	if(document.insertForm.ad_s_email.value.length>0 && document.insertForm.ad_s_city.value!='-2'){
		document.insertForm.save.value = 'true';
		document.insertForm.isFormValide.value = 'true';
	}
	
	//alert(document.insertForm.save.value + ", " + document.insertForm.isValide.value);
}
