//CUSTOM

//trim - удаляет пробелы с начала и конца
//trimLeading - удалить с начала
//trimTrailing - удалить с конца
//removeSpaces - удалить все пробелы в строке


var allSpacesRe = /\s+/g;
var leadingSpacesRe = /^\s+/;
var trailingSpacesRe = /\s+$/;

function removeSpaces(s) { 
if(s) return s.replace(allSpacesRe, ""); 
return s;
}

function trimLeading(s) { 
if(s) return s.replace(leadingSpacesRe, ""); 
return s;
}

function trimTrailing(s) { 
if(s) return s.replace(trailingSpacesRe, ""); 
return s;
}

function trim(s) { 
if(s) return trimLeading(trimTrailing(s));
return s;
}			

//clear value input
function fclear(id) {
	id.value="";
}

//check box
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
}
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
 }
}

function un(data) {return unescape(data);}


//скрытие/открытие блока
function toggle_block(block)
{
	if (document.getElementById)
	{
		if (document.getElementById(block).style.display == 'block')
		{
			document.getElementById(block).style.display = 'none';
			
	
		}
		else
		{
			document.getElementById(block).style.display = 'block';
						
		}
	}
	else
	alert("Ваш браузер не поддерживается!");
}


function regions(){
	regionRF=document.forms["profileForm"].regionRF.value;
	country=document.forms["profileForm"].country.value;

 	if (country == "Россия"){
 		document.forms["profileForm"].regionRF.disabled = false
 		if (regionRF == "Москва" || regionRF == "Санкт-Петербург"){
 			document.forms["profileForm"].city.disabled = true
 		}else{
 			document.forms["profileForm"].city.disabled = false
 		}
 		document.forms["profileForm"].region.disabled = true
 	}else{
 		document.forms["profileForm"].regionRF.disabled = true
 		document.forms["profileForm"].region.disabled = false
 		document.forms["profileForm"].city.disabled = false
 	}
}

function validateEmail(value,res) {
  	email = removeSpaces(value);
	if ((email.indexOf("@")==-1) || (email.indexOf(".")==-1))
	document.getElementById(res).innerHTML = "";
	else 
	document.getElementById(res).innerHTML = "Ok";

		}
		
		
function validatePwd(password,confirmPassword) {
	password = removeSpaces(password);
	confirmPassword = removeSpaces(confirmPassword);
	if ((password!==confirmPassword) || !password || !confirmPassword|| password.length<4 || confirmPassword.length<4 ) {
	document.getElementById("validatePwdRes").innerHTML = "";
	document.getElementById("validateConfRes").innerHTML = "";
	}
	else {
	document.getElementById("validatePwdRes").innerHTML = "Ok";
	document.getElementById("validateConfRes").innerHTML = "Ok";
	}
		}


function noEmpty(value,res) {
	value = trim(value);
	if (value.length<1) 	
	document.getElementById(res).innerHTML = "";
	else
	document.getElementById(res).innerHTML = "Ok";

}

function noEmptySrc(value,res) {
	value = trim(value);
	if (value.length==3) 	
	document.getElementById(res).innerHTML = "Ok";
	else
	document.getElementById(res).innerHTML = "";

}


//VALIDATE

function validateProfile() {
	email=removeSpaces(document.forms["profileForm"].email.value);
	password=removeSpaces(document.forms["profileForm"].password.value);
	lname=trim(document.forms["profileForm"].lname.value);
	fname=trim(document.forms["profileForm"].fname.value);
	mname=trim(document.forms["profileForm"].mname.value);
	src=removeSpaces(document.forms["profileForm"].src.value);
	
	if(!email || email.length>50 || email.indexOf("@")==-1 || email.indexOf(".")==-1) {
		document.forms["profileForm"].email.focus();
		window.alert("Введите email (max 50chr)!");
	} 
	else if(!password || password.length>15 || password.length<4) {
		document.forms["profileForm"].password.focus();
		window.alert("Укажите пароль (4-15chr)!");
	}
	else if(!lname || lname.length>50) {
		document.forms["profileForm"].lname.focus();
		window.alert("Введите фамилию (max 50chr)!");
	}
	else if(!fname || fname.length>50) {
		document.forms["profileForm"].fname.focus();
		window.alert("Введите имя (max 50chr)!");
	} 	
	else if(!mname || mname.length>50) {
		document.forms["profileForm"].mname.focus();
		window.alert("Введите отчество (max 50chr)!");
	}
	else if(!src || src.length!=3) {
		document.forms["profileForm"].src.focus();
		window.alert("Введите число (3chr)!");
	}
	else {
	document.forms["profileForm"].submit();	
    }
}

//ORDER PROCESS
function validateAdress() {
	country=trim(document.forms["profileForm"].country.value);
	regionRF=trim(document.forms["profileForm"].regionRF.value);
	region=trim(document.forms["profileForm"].region.value);
	city=trim(document.forms["profileForm"].city.value);
	postindex=trim(document.forms["profileForm"].postindex.value);
	street=trim(document.forms["profileForm"].street.value);
	house=trim(document.forms["profileForm"].house.value);
	korpus=trim(document.forms["profileForm"].korpus.value);
	appt=trim(document.forms["profileForm"].appt.value);
	phone=trim(document.forms["profileForm"].phone.value);
	comment=trim(document.forms["profileForm"].comment.value);
	flag=trim(document.forms["profileForm"].flag.value);
	if (document.forms["profileForm"].regionRF.disabled == true) regionRF="";
	if (document.forms["profileForm"].region.disabled == true) region="";
	if (document.forms["profileForm"].city.disabled == true) city="";

	if(!country) {
		document.forms["profileForm"].country.focus();
		window.alert("Не указана страна!");
	} else if (!regionRF && !region) {
		window.alert("Не указан регион!");
	} else if ((regionRF!="Москва" && regionRF!="Санкт-Петербург") && !city) {
		document.forms["profileForm"].city.focus();
		window.alert("Не указан город!");
	} else if (!postindex) {
		document.forms["profileForm"].postindex.focus();
		window.alert("Не указан почтовый индекс!");
	} else if (!street) {
		document.forms["profileForm"].street.focus();
		window.alert("Не указана улица!");
	} else if (!house) {
		document.forms["profileForm"].house.focus();
		window.alert("Не указан номер дома!");
	} else if (!appt) {
		document.forms["profileForm"].appt.focus();
		window.alert("Не указан номер квартиры!");
	} else if (window.confirm("Адресная информация достоверна?")) {
		document.forms["profileForm"].submit();	
	}
}


function validateLogin() {
	email=removeSpaces(document.forms["loginForm"].email.value);
	password=removeSpaces(document.forms["loginForm"].password.value);
	src=removeSpaces(document.forms["loginForm"].src.value);
	
	if(!email || !password || !src) {
	window.alert("Не заполнены обязательные поля!");
	} else {
	document.forms["loginForm"].submit();	
    }
}

// -->
