//slagane avtomati4no na fuknciq confirmDelete na vs linkove za iztrivane
window.onload = addDeleteMethos;
function addDeleteMethos() {
	var allLinks = window.document.getElementsByTagName("a");
	var i;
	var patt=/Изтрий/;
	for (i=0; i< allLinks.length; i++) {
		
		if(patt.test(allLinks[i].title)) {
			url = allLinks[i].href;
			allLinks[i].href = "javascript:confirmDelete('"+url+"', 'Сигурни ли сте че искате да изтриете този запис!')";
			
		} 
	}
	return true; 
}
//funciq confirmDelete
function confirmDelete(url, message) {
	if (confirm(message)) {
		window.location.href = url;
	}
}

function refreshParent() {
	
	window.opener.location.href = window.opener.location.href;
	if (window.opener.progressWindow) {
		window.opener.progressWindow.close()
	}

	//window.opener.document.forms['searchForm'].submit();
	window.close();
}

function filterByPars(pars, url) {
	window.location.href='?page='+url+pars;
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);

}

function checkAll(className, val) {
	if(val) {
		$("."+className).attr('checked', true);
	} else {
		$("."+className).attr('checked', false);
	}
}

function swapMainImage(imageId, source) {
	if(imageId != "" && source != "") {
		$("#"+imageId).attr("src", source);
	}
}

function checkInquiryOrderForm() { 
	var name 		= window.document.getElementById("name");
	var email 		= window.document.getElementById("email");
	var phone 		= window.document.getElementById("phone");
	var engine 		= window.document.getElementById("engine");
	var hp 			= window.document.getElementById("hp");
	//var info	 	= window.document.getElementById("info");
	var form 		= window.document.getElementById("inquiryPart");
	
	if(name.value == "") { alert("Моля попълнете полето: Лице за контакт"); name.focus(); return false; }
	if(email.value == "") { alert("Моля попълнете полето: е-mail за контакт"); email.focus(); return false;
	} else if(!isValidEmailAddress(email.value)) { alert("Моля въведете коректен e-mail"); email.focus(); return false; }
	if(phone.value == "") { alert("Моля попълнете полето: Телефон за контакт"); phone.focus(); return false; }
	if(engine.value == "") { alert("Моля попълнете полето: Кубатура на двигател / брой клапани"); engine.focus(); return false; }
	if(hp.value == "") { alert("Моля попълнете полето: Конски сили"); hp.focus(); return false; }
	//if(info.value == "") { alert("Моля попълнете полето: Допълнително информация"); info.focus(); return false; }
	form.submit();
}

function checkInquiryForm() { 
	var name 		= window.document.getElementById("name");
	var email 		= window.document.getElementById("email");	
	var info 		= window.document.getElementById("info");
	var form 		= window.document.getElementById("inquiryForm");
	
	if(name.value == "") { alert("Моля попълнете полето: Име"); name.focus(); return false; }
	if(email.value == "") { alert("Моля попълнете полето: E-Mail адрес"); email.focus();return false;
	} else if(!isValidEmailAddress(email.value)) { alert("Моля въведете коректен E-Mail адрес"); email.focus(); return false; }
	if(info.value == "") { alert("Моля попълнете полето: Запитвaне"); info.focus(); return false; }
	form.submit();
}

function checkInsuranceForm() {
	var form 		= window.document.getElementById("insuranceForm");
	var fname 		= window.document.getElementById("fname");
	var sname 		= window.document.getElementById("sname");	
	var lname 		= window.document.getElementById("lname");
	var egn 		= window.document.getElementById("egn");
	var city 		= window.document.getElementById("city");
	var address 	= window.document.getElementById("address");
	var phone 		= window.document.getElementById("phone");
	var email 		= window.document.getElementById("email");
	var brand 		= window.document.getElementById("brand");
	var model 		= window.document.getElementById("model");
	var rama 		= window.document.getElementById("rama");
	var engineSize 	= window.document.getElementById("engineSize");
	var regNumber 	= window.document.getElementById("regNumber");
	var start 		= window.document.getElementById("start");

	if(fname.value == "") { alert("Моля попълнете полето: Име"); fname.focus(); return false; }
	if(sname.value == "") { alert("Моля попълнете полето: Презиме"); sname.focus(); return false; }
	if(lname.value == "") { alert("Моля попълнете полето: Фамилия"); lname.focus(); return false; }
	if(egn.value == "") { alert("Моля попълнете полето: ЕГН"); egn.focus(); return false; }
	if(city.value == "") { alert("Моля попълнете полето: Град"); city.focus(); return false; }
	if(address.value == "") { alert("Моля попълнете полето: Адрес"); address.focus(); return false; }
	if(phone.value == "") { alert("Моля попълнете полето: Телефон"); phone.focus(); return false; }
	if(email.value == "") { alert("Моля попълнете полето: e-mail"); email.focus(); return false;
	} else if(!isValidEmailAddress(email.value)) { alert("Моля въведете коректен e-mail"); email.focus(); return false; }
	if(brand.value == "") { alert("Моля попълнете полето: Марка на автомобила"); brand.focus(); return false; }
	if(model.value == "") { alert("Моля попълнете полето: Модел на автомобила"); model.focus(); return false; }
	if(rama.value == "") { alert("Моля попълнете полето: Номер на рамата"); rama.focus(); return false; }
	if(engineSize.value == "") { alert("Моля попълнете полето: Обем на двигателя"); engineSize.focus(); return false; }
	if(regNumber.value == "") { alert("Моля попълнете полето: Регистр. номер"); regNumber.focus(); return false; }
	if(start.value == "") { alert("Моля попълнете полето: Начална дата на застраховката"); start.focus(); return false; }
	form.submit();
}

function updateError(t, tips) {	
	tips
		.html(t)
		.css("background", "#FC0")
}
function checkEmptyField(obj, n, tips) {
	if (obj.val().length <= 0) {
		updateError("Моля попълнете полето "+n,tips);
		obj.focus();
		return false;
	} else {
		return true;
	}
}
function validPhone(phone) {
	if(phone.length <= 0) {  return false;  }
	var charsToBeReplaced = new Array("+", "-", "/", " ");
	for(i=0; i<=charsToBeReplaced.length; i++) {
		phone.split(charsToBeReplaced[i]).join("");
	}
	if(isNaN(phone)) { return false; }
	if(phone.length < 6 || phone.length > 20) { return false; }
	return true;
}
function nullPromoCodeCheck() {
	$( "#promo_code_check" ).val('0');
	$( "#promo_code_image" ).css('display', "none");
}
function checkPromoCode() {
	var promo_code 		= $( "#promo_code" ),
	promo_code_error	= $( "#promo_code_error" );
	if(promo_code.val().length > 0) {
		$.get("/ajax_get.php?promo_code="+promo_code.val()+"&checkPromoCode=true", function(data){
			if(data != "") {
				updateError("<br />"+data, promo_code_error);
				$( "#promo_code_check" ).val('0');
				$( "#promo_code_image" ).css('display', "none");
			} else {
				promo_code_error.css("background", "");
				promo_code_error.html("");
				$( "#promo_code_check" ).val('1');
				$( "#promo_code_image" ).css('display', "");
			}
		});
	}
}
function checkForAddressChange(val) {
	$("#checkForAddress").val(val);
}
function checkOrderForm() {
	var contact_name 		= $( "#contact_name" ),
		contact_email 		= $( "#contact_email" ),
		contact_phone 		= $( "#contact_phone" ),
		contact_address 	= $( "#contact_address" ),
		checkForAddress 	= $( "#checkForAddress" ),
		promo_code 			= $( "#promo_code" ),
		promo_code_check 	= $( "#promo_code_check" ),
		forFirmInvoice_checkbox = $( "#forFirmInvoice_checkbox" ),
		firmName 			= $( "#firmName" ),
		firmAddress 		= $( "#firmAddress" ),
		firmMol 			= $( "#firmMol" ),
		firmEIK 			= $( "#firmEIK" );
		
	var contact_name_error 		= $( "#contact_name_error" ),
		contact_email_error 	= $( "#contact_email_error" ),
		contact_phone_error 	= $( "#contact_phone_error" ),
		contact_address_error 	= $( "#contact_address_error" ),
		promo_code_error 		= $( "#promo_code_error" ),
		firmName_error 			= $( "#firmName_error" ),
		firmAddress_error 		= $( "#firmAddress_error" ),
		firmMol_error 			= $( "#firmMol_error" ),
		firmEIK_error 			= $( "#firmEIK_error" ),
		allFields_error 		= $([]).add(contact_name_error).add(contact_email_error).add(contact_phone_error).add(contact_address_error).add(promo_code_error).add(firmName_error).add(firmAddress_error).add(firmMol_error).add(firmEIK_error);
		
		var bValid = true;
		allFields_error.css("background", "");
		allFields_error.html("");
		
		bValid = bValid && checkEmptyField(contact_name, "'Лице за контакт'", contact_name_error);
		bValid = bValid && checkEmptyField(contact_email, "'е-mail за контакт'", contact_email_error);
		if(bValid) {
			if(!isValidEmailAddress(contact_email.val())) { 
				updateError("Моля въведете коректен e-mail", contact_email_error);
				bValid = false;
			}
		}
		
		bValid = bValid && checkEmptyField(contact_phone, "'Телефон за контакт'", contact_phone_error);
		if(bValid) {
			if(!validPhone(contact_phone.val())) { 
				updateError("Моля въведете коректен телефон", contact_phone_error); 
				bValid = false;
			}
		}
		if(checkForAddress.val() == "1") {
			bValid = bValid && checkEmptyField(contact_address, "'Адрес'", contact_address_error);
		}
		if(bValid) {
			if(promo_code.val().length > 0 && promo_code_check.val() != '1') {
					updateError("<br />Моля проверете въведения промо код", promo_code_error); 
					bValid = false;			
			}
		}
		if(forFirmInvoice_checkbox.attr('checked')) {
			bValid = bValid && checkEmptyField(firmName, "'Име на фирма'<br />", firmName_error);
			bValid = bValid && checkEmptyField(firmAddress, "'Адрес на фирмата'<br />", firmAddress_error);
			bValid = bValid && checkEmptyField(firmMol, "'МОЛ'<br />", firmMol_error);
			bValid = bValid && checkEmptyField(firmEIK, "'ЕИК/Булстат'<br />", firmEIK_error);
		}
		
		if(bValid) {
			$("#orderForm").submit();
		}
}


