
var is_debug = 0;
if(is_debug){ alert("nachalo na scripta");}

var agent = navigator.userAgent.toLowerCase();
var ns = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
var ie = (agent.indexOf("msie") != -1);
var pc = (agent.indexOf("win") != -1);



function phone_entry_add(){
	if(document.edit_form.phone_count.value < max_phones){
		//if(ie){
		document.edit_form.phone_count.value ++;
		n = document.edit_form.phone_count.value;
		var phone_add_string = '<br>тел.<input name="phone'+n+'" type="checkbox" id="phone'+n+'" value="1"> '+
		'факс <input name="fax'+n+'" type="checkbox" id="fax'+n+'" value="2">: '+
		'++<input name="country_code'+n+'" type="text" id="country_code'+n+'" value="359" size="3" maxlength="3" class="edit" onKeyDown="only_numbers(this)" onKeyUp="only_numbers(this)" onKeyPress="only_numbers(this)" onChange="only_numbers(this)" onfocus="only_numbers(this)" onblur="only_numbers(this)"> '+
		' &nbsp;<input name="city_code'+n+'" type="text" id="city_code'+n+'" value="2" size="4" maxlength="6" class="edit" onKeyDown="only_numbers(this)" onKeyUp="only_numbers(this)" onKeyPress="only_numbers(this)" onChange="only_numbers(this)" onfocus="only_numbers(this)" onblur="only_numbers(this)">&nbsp; '+
		'<input name="phone_number'+n+'" type="text" id="phone_number'+n+'" size="10" maxlength="10" class="edit" onKeyDown="only_numbers(this)" onKeyUp="only_numbers(this)" onKeyPress="only_numbers(this)" onChange="only_numbers(this)" onfocus="only_numbers(this)" onblur="only_numbers(this)">';
		if(ie){document.all.phones_div.innerHTML = document.all.phones_div.innerHTML + phone_add_string;}
		else{document.getElementById("phones_div").innerHTML = document.getElementById("phones_div").innerHTML + phone_add_string;}
		//}else{alert("Za da dobaviate telefoni triabva da izpolzvate Internet Explorer 5\n ili po-nov poddarjasht DHTML!\n");}
	}else{alert("Не може да добавите повече от " + max_phones + " телефона!");}
}

overchar_count = 0;

function char_check( field, field_left, max_char){
	var left = max_char - field.value.length;
	//alert(field_left.value + " " + left + " " + max_char + " " + field.value.length);

	if(left != field_left.value){
		if(left < 0){
			if(field_left.value >= 0){
				field_left.style.color = "#CC0000";
				overchar_count ++;
				if(overchar_count == 1){
					alert("Вие въведохте повече от позволените символи.\nБроят им се изобразява като червено отрицателно число!\nВ need услуги може да се запознаете с видовете абонамент и съответните допустими дължини.");// + overchar_count + field_left.name);
				}
			}
		}else{
			if(field_left.value < 0){field_left.style.color = "#336699";}
		}

		//if(field.value.length > max_char){ field.value = field.value.substring( 0, max_char);}
		field_left.value = left;
	}
}



function r(e, fv, v){if(e.test(fv)){fv=fv.replace(e,v);}}

function check_punctuation_blank(f){
	r( /[\s]+/g, f.value, " ");
	r( /^ /g, f.value, "");
	r( / $/g, f.value, "");
}

function check_punctuation_new(f){
	check_punctuation_blank(f);
	if(pc){
		f.value = f.value.replace(/ ([\.\,\;\:\?\!]+)/g, "$1");
		f.value = f.value.replace(/([\.\,\;\:\?\!]+)\s+([\.\,\;\:\?\!]+)/g, "$1$2");
		f.value = f.value.replace(/([\,\;\:\?\!]+)([^\ ])/g, "$1 $2");

		re = /(([а-яА-Я]+[a-zA-Z]+)|([a-zA-Z]+[а-яА-Я]+))/;
		if(re.test(f.value)){
			alert("Има дума съставена едновремено и от латински и от български букви!");
		}

		//re = /(([а-яА-Я]+[a-zA-Z]+)|([a-zA-Z]+[а-яА-Я]+))/;
		//if(re.test(f.value)){
		//	asd = f.value.match(re);
		//	alert("Има дума съставена едновремено и от латински и от български букви: \"..." + asd[1] + '..."');

			//re = /\b(([а-яА-Я][a-zA-Z])|([a-zA-Z][а-яА-Я]))+\b/g;
			//asd = f.value.match(/(([а-яА-Я]+[a-zA-Z]+)|([a-zA-Z]+[а-яА-Я]+))/);
			//alert("asl: " + asd.length + ", as0: " + asd[0] + ", as1: " + asd[1] + ", as2: " + asd[2] + ", as3: " + asd[3] + ", asi: " + asd.input);
		//}


	}
}


function check_punctuation(f){
	if(pc){
		f.value = f.value.replace(/[\s]+/g, " ");
		//f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
		f.value = f.value.replace(/ ([\.\,\;\:\?\!]+)/g, "$1");
		f.value = f.value.replace(/([\.\,\;\:\?\!]+)\s+([\.\,\;\:\?\!]+)/g, "$1$2");
		f.value = f.value.replace(/([\,\;\:\?\!]+)([^\ ])/g, "$1 $2");


		//re = /[a-zA-Z][а-яА-Я0-9]/g;
		re = /[a-zA-Z][а-яА-Я]/g;
		if(pc && re.test(f.value)){
			alert("Ima smesena duma ot latinski i kirilski bukvi!!!!");
		}

		//re = /[а-яА-Я][a-zA-Z0-9]/g;
		re = /[а-яА-Я][a-zA-Z]/g;
		if(pc && re.test(f.value)){
			alert("Ima smesena duma ot kirilski i latinski bukvi!!!!");
		}

	}
}

function only_numbers(f){
	re = /[^\d]/g;
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
	}

	re = /^0/;
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
		alert("Цифрово поле не може да започва с '0'.");
	}
}

function only_numbers_en(f){
	re = /[^\d]/g;
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
	}

	re = /^0/;
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
		alert("The digital field can not begin with zero!");
	}
}


function only_firm_letter_bg(f){
	re = /[^0-9a-zA-Zа-яА-Я \-\+\&\.\']/g; //' stupid remark
	if(pc && re.test(f.value)){
		f.value = f.value.replace(re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}

function only_firm_letter_en(f){
	re = /[^0-9a-zA-Z \-\+\&\.\']/g; //' stupid remark
	if(pc && re.test(f.value)){
		f.value = f.value.replace(re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}

function only_letter_bg(f){
	re = /[^а-яА-Я ]/g;
	if(pc && re.test(f.value)){
		f.value = f.value.replace( re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}

function only_address_bg(f){
	re = /[^0-9а-яА-Я IVX\-\.\']/g;
	if(pc && re.test(f.value)){
		f.value = f.value.replace( re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}

function only_address_en(f){
	re = /[^0-9a-zA-Z IVX\-\.]/g;
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}

function only_description_en(f){
	re = /[^0-9a-zA-Z \.\,\:\;\-\+\@\$\%\&\/\']/g; //' stupid remark
	if(re.test(f.value)){
		f.value = f.value.replace( re, "");
		f.value = f.value.replace(/ +/g, " ");
		f.value = f.value.replace(/^ /g, "");
		f.value = f.value.replace(/ $/g, "");
	}
}


function check_phone(f, phone_id){
	//alert("begin check form3");
	//alert("asd" + phone_id + ":" + f["country_code" + phone_id].value);
	//return "";

	cp = f["phone" + phone_id].checked;
	cf = f["fax" + phone_id].checked;

	coc = f["country_code" + phone_id].value;
	cic = f["city_code" + phone_id].value;
	pn = f["phone_number" + phone_id].value;

	//alert("begin check form4 cp:" + cp + "cf" + cf + coc + cic + pn);
	error_text = "";

	if((cp || cf) && coc && cic && pn){
		if(!(coc > 0 && coc < 999)){error_text += "Моля, въведете коректен код за държава.\nPlease, enter correct telephone code for country.\n";}
		else{
			switch (coc) {
				case "359" :
					if(!(cic > 1 && cic < 99999)){error_text += "Моля, въведете коректен код за населено място или оператор.\nPlease, enter correct telephone code.\n";}
					else{
						switch (cic) {
							case "2" :
								if(!(pn.length == 5 || pn.length == 6 || pn.length == 7 || pn.length == 8)){error_text += "Моля, въведете коректен телефонен номер за град София.\nPlease, enter correct telephone number for Sofia city.\n";}
								break;
							case "48" :
								if(!(pn.length == 6)){error_text += "Моля, въведете коректен номер за мобилен оператор mobikom.\nPlease, enter correct telephone number for mobile operator mobikom.\n";}
								break;
							case "87" :
								if(!(pn.length == 7)){error_text += "Моля, въведете коректен номер за мобилен оператор Vivatel.\n";}
								break;
							case "877" :
								error_text += "Моля, въведете коректен код за мобилен оператор Vivatel.\n";
								break;
							case "876" :
								error_text += "Моля, въведете коректен код за мобилен оператор Vivatel.\n";
								break;
							case "878" :
								error_text += "Моля, въведете коректен код за мобилен оператор Vivatel.\n";
								break;
							case "88" :
								if(!(pn.length == 7)){error_text += "Моля, въведете коректен номер за мобилен оператор Mtel.\n";}
								break;
							case "887" :
								error_text += "Моля, въведете коректен код за мобилен оператор Mtel.\n";
								break;
							case "888" :
								error_text += "Моля, въведете коректен код за мобилен оператор Mtel.\n";
								break;
							case "889" :
								error_text += "Моля, въведете коректен код за мобилен оператор Mtel.\n";
								break;
							case "89" :
								if(!(pn.length == 7)){error_text += "Моля, въведете коректен номер за мобилен оператор GloBul.\n";}
								break;
							case "898" :
								error_text += "Моля, въведете коректен код за мобилен оператор GloBul.\n";
								break;
							case "899" :
								error_text += "Моля, въведете коректен код за мобилен оператор GloBul.\n";
								break;
							case "98" :
								error_text += "Моля, въведете коректен код за мобилен оператор GloBul.\n";
								break;
							case "99" :
								error_text += "Моля, въведете коректен код за мобилен оператор GloBul.\n";
								break;
							default :
						}
					}
					break;
				case "1" :
					if(!(cic.length == 3 && pn.length == 7)){error_text += "Моля, въведете коректен телефонен код и номер за номер в USA.\nPlease, enter correct telephone code and number in USA.\n";}
					break;
				default :
			}
		}
	}else{return "a";}
	return error_text;
}

