function toggleAccept()
{
  if (document.getElementById('OrderApply').checked)
  {
    document.getElementById('checkout_button').disabled = false;
  }
  else
  {
    document.getElementById('checkout_button').disabled = true;
  }
}


function checkout()
{

if (document.getElementById('AccountAccountTypeId')){


		
	if(!document.getElementById('AccountAccountTypeId').value) {
		alert("Выберите тип покупателя");
		document.getElementById('AccountAccountTypeId').focus();
		return false;
	}	


	
	if(document.getElementById('AccountAccountTypeId').value==1)
	{
		if(document.getElementById('AccountOwnershipTypeId').value==1) {
			alert("Выберите тип собственности");
			document.getElementById('AccountOwnershipTypeId').focus();
			return false;
		}
		//alert(array_perform);
		for (var i=0; i<array_perform.length;i++){
			if (array_perform[i]!=-1) {
				if (_trim($('Performer'+array_perform[i]+'Name').value)=='') {alert('Введите имя исполнителя.'); $('Performer'+array_perform[i]+'Name').focus(); return false;}
				UserEmailOrg = $('Performer'+array_perform[i]+'Email').value;
				if (!check_email2(UserEmailOrg)) {$('Performer'+array_perform[i]+'Email').focus(); alert("Введен некорректный email исполнителя\n\rПример: test@test.ru"); return false;}
			}
		}
		for (var i=0; i<array_perform.length;i++){
			if (array_perform[i]!=-1) {
				UserNameOrg = _trim($('Performer'+array_perform[i]+'Name').value);
				UserEmailOrg = $('Performer'+array_perform[i]+'Email').value;
				break;
			}
		}
		if (document.getElementById('name_user')){
			if (document.getElementById('name_user').value=="")
				document.getElementById('name_user').value = UserNameOrg;
		}
		if (_trim(document.getElementById('AccountCompany').value) == '') {document.getElementById('AccountCompany').focus(); alert('Введите назвавние организации'); return false;}
		if (_trim(document.getElementById('AccountInn').value) == '') {document.getElementById('AccountInn').focus(); alert('Введите ИНН'); return false;}
		/*if (document.getElementById('AccountKpp').value.length == 0) {document.getElementById('AccountKpp').focus(); alert('Введите КПП'); return false;}*/
		if (document.getElementById('AccountInn').value.length > 0)
		{
			 if (!/^\d+$/.test(document.getElementById('AccountInn').value)) {document.getElementById('AccountInn').focus(); alert('Вы не ввели корректный "ИНН"!'); return false;}
			 if(document.getElementById('AccountInn').value.length != 10) {document.getElementById('AccountInn').focus(); alert('ИНН должно состоять из 10 знаков'); return false;}
		}

		if (document.getElementById('AccountKpp').value.length > 0)
		{
			if (!/^\d+$/.test(document.getElementById('AccountKpp').value)) {document.getElementById('AccountKpp').focus(); alert('Вы ввели некорректное значение в поле "КПП"!'); return false;}
		 	if(document.getElementById('AccountKpp').value.length != 9) {document.getElementById('AccountKpp').focus(); alert('КПП должно состоять из 9 знаков'); return false;}
		}
		
		
	}
	
	if(document.getElementById('AccountAccountTypeId').value==3)
	{
		if (_trim(document.getElementById('UserSurname').value)=='') {document.getElementById('UserSurname').focus(); alert('Введите фамилию'); return false;}
		if (_trim(document.getElementById('UserFirstname').value)=='') {document.getElementById('UserFirstname').focus(); alert('Введите имя'); return false;}
		if (_trim(document.getElementById('UserMiddlename').value)=='') {document.getElementById('UserMiddlename').focus(); alert('Введите отчество'); return false;}
	
		document.getElementById('name_user').value=document.getElementById('UserSurname').value+" "+document.getElementById('UserFirstname').value+" "+document.getElementById('UserMiddlename').value;
	
		if (_trim(document.getElementById('AccountInn').value) == '') {document.getElementById('AccountInn').focus(); alert('Введите ИНН'); return false;}
		if (document.getElementById('AccountInn').value.length > 0)
		{
			 if (!/^\d+$/.test(document.getElementById('AccountInn').value)) {document.getElementById('AccountInn').focus(); alert('Вы не ввели корректный "ИНН"!'); return false;}
			 if(document.getElementById('AccountInn').value.length != 12) {document.getElementById('AccountInn').focus(); alert('ИНН должно состоять из 12 знаков'); return false;}
		}
		
		
		//document.getElementById('email_user').value=document.getElementById('email_contact_fiz_input').value;
	
		
		var email = document.getElementById("email_contact_fiz_input").value;
		if (!check_email2(email)) {document.getElementById('email_contact_fiz_input').focus(); alert("Введен некорректный email контактного лица\n\rПример: test@test.ru"); return false;}
	}
	
	if(document.getElementById('AccountAccountTypeId').value==2)
	{
		if (_trim(document.getElementById('UserSurname').value)=='') {document.getElementById('UserSurname').focus(); alert('Введите фамилию'); return false;}
		if (_trim(document.getElementById('UserFirstname').value)=='') {document.getElementById('UserFirstname').focus(); alert('Введите имя'); return false;}
		
		document.getElementById('name_user').value=document.getElementById('UserSurname').value+" "+document.getElementById('UserFirstname').value+" "+document.getElementById('UserMiddlename').value;
		//document.getElementById('email_user').value=document.getElementById('email_contact_fiz_input').value;
		var email = document.getElementById("email_contact_fiz_input").value;
		if (_trim(email)=='') {document.getElementById('email_contact_fiz_input').focus(); alert('Введите email контактного лица'); return false;}
		if (!check_email2(email)) {document.getElementById('email_contact_fiz_input').focus(); alert("Введен некорректный email контактного лица\n\rПример: test@test.ru"); return false;}
	}
	
	/*Проверка телефона*/
	if (!isCheckPhoneCheckOut){
		if (document.getElementById('AccountPhoneCode').value.length > 0 || document.getElementById('AccountPhone').value.length > 0)
		{	
			if (document.getElementById('AccountPhoneCountryCode').value.length == 0 ) document.getElementById('AccountPhoneCountryCode').value = 7;
			if ((document.getElementById('AccountPhoneCode').value.length+document.getElementById('AccountPhone').value.length) != 10 ) {document.getElementById('AccountPhone').focus(); alert('Телефон и телефонный код должен состоять из 10 цифр!'); return false;}
			if (!/^\d+$/.test(document.getElementById('AccountPhoneCountryCode').value)) {document.getElementById('AccountPhoneCountryCode').focus(); alert('Телефонный код страны должен состоять только из цифр!'); return false;}
			if (!/^\d+$/.test(document.getElementById('AccountPhoneCode').value)) {document.getElementById('AccountPhoneCode').focus(); alert('Телефонный код должен состоять только из цифр!'); return false;}
			if (!/^\d+$/.test(document.getElementById('AccountPhone').value)) {document.getElementById('AccountPhone').focus(); alert('Телефон должен состоять только из цифр!'); return false;}
			//if (!/\d{1,3}-\d{2}-\d{2}$/.test(document.getElementById('AccountPhone').value)) {document.getElementById('AccountPhone').focus(); alert('Введите номер телефона в формате XXX-XX-XX или X-XX-XX'); return false;}
		}
	}
	
	/*Проверка факса*/
	if (document.getElementById('AccountFaxCode').value.length > 0 || document.getElementById('AccountFax').value.length > 0)
	{	
		if (document.getElementById('AccountFaxCountryCode').value.length == 0 ) document.getElementById('AccountFaxCountryCode').value = 7;
		if ((document.getElementById('AccountFaxCode').value.length+document.getElementById('AccountFax').value.length) != 10 ) {document.getElementById('AccountFax').focus(); alert('Факс и код должен состоять из 10 цифр!'); return false;}
		if (!/^\d+$/.test(document.getElementById('AccountFaxCountryCode').value)) {document.getElementById('AccountFaxCountryCode').focus(); alert('Телефонный код страны должен состоять только из цифр!'); return false;}
		if (!/^\d+$/.test(document.getElementById('AccountFaxCode').value)) {document.getElementById('AccountFaxCode').focus(); alert('Телефонный код должен состоять только из цифр!'); return false;}
		if (!/^\d+$/.test(document.getElementById('AccountFax').value)) {document.getElementById('AccountFax').focus(); alert('Телефон должен состоять только из цифр!'); return false;}
		//if (!/\d{1,3}-\d{2}-\d{2}$/.test(document.getElementById('AccountPhone').value)) {document.getElementById('AccountPhone').focus(); alert('Введите номер телефона в формате XXX-XX-XX или X-XX-XX'); return false;}
	}
	
}


if (isCheckPhoneCheckOut){
	if (document.getElementById('AccountPhoneCountryCode').value.length == 0 ) document.getElementById('AccountPhoneCountryCode').value = 7;
	if ((document.getElementById('AccountPhoneCode').value.length+document.getElementById('AccountPhone').value.length) != 10 ) {document.getElementById('AccountPhone').focus(); alert('При выбранном способе доставке необходимо ввести контактный телефон.\r\n\r\nТелефон и телефонный код должен состоять из 10 цифр!'); return false;}
	if (!/^\d+$/.test(document.getElementById('AccountPhoneCountryCode').value)) {document.getElementById('AccountPhoneCountryCode').focus(); alert('При выбранном способе доставке необходимо ввести контактный телефон.\r\n\r\nТелефонный код страны должен состоять только из цифр!'); return false;}
	if (!/^\d+$/.test(document.getElementById('AccountPhoneCode').value)) {document.getElementById('AccountPhoneCode').focus(); alert('При выбранном способе доставке необходимо ввести контактный телефон.\r\n\r\nТелефонный код должен состоять только из цифр!'); return false;}
	if (!/^\d+$/.test(document.getElementById('AccountPhone').value)) {document.getElementById('AccountPhone').focus(); alert('При выбранном способе доставке необходимо ввести контактный телефон.\r\n\r\nТелефон должен состоять только из цифр!'); return false;}
}

	if (document.getElementById('OrderShippingMethodId').value.length == 0) {document.getElementById('OrderShippingMethodId').focus(); alert('Выберите предпочтительный метод доставки'); return false;}	
	if (document.getElementById('OrderPaymentMethodId').value.length == 0) {document.getElementById('OrderPaymentMethodId').focus(); alert('Выберите предпочтительный метод оплаты'); return false;}
	
	
	if(document.getElementById('AccountAddrZipcode')!=null)
	{
		if(document.getElementById('AccountAddrZipcode').value.length > 0){
			if(document.getElementById('AccountAddrZipcode').value.length != 6) {document.getElementById('AccountAddrZipcode').focus(); alert('Введите почтовый индекс'); return false;}
		}
		// Город
		if(document.getElementById('AccountAddrType0').checked)
		{
			if (document.getElementById('AccountAddrRegionId').value.length == 0) {document.getElementById('AccountAddrRegionId').focus(); alert('Выберите регион'); return false;}
			
			// Если регион не Москва или СПБ - обязательно нужно выбрать город
			if (document.getElementById('AccountAddrRegionId').value != 77 && document.getElementById('AccountAddrRegionId').value != 78)
			{
				if (document.getElementById('AccountAddrCityId').value.length == 0) {document.getElementById('AccountAddrCityId').focus(); alert('Выберите город'); return false;}
			}
		}
		
		// Населенный пункт
		if(document.getElementById('AccountAddrType1').checked)
		{
			if (document.getElementById('AccountAddrRegionId').value.length == 0) {document.getElementById('AccountAddrRegionId').focus(); alert('Выберите регион'); return false;}
			//if (document.getElementById('AccountAddrAreaId').value.length == 0) {document.getElementById('AccountAddrAreaId').focus(); alert('Выберите район'); return false;}
			if (document.getElementById('AccountAddrCountryId').value.length == 0) {document.getElementById('AccountAddrCountryId').focus(); alert('Выберите населенный пункт'); return false;}
		}
		
		if (document.getElementById('AccountStreetManual').checked)
		{
			if (document.getElementById('AccountAddrStreetText').value.length == 0) {document.getElementById('AccountAddrStreetText').focus(); alert('Введите название улицы'); return false;}
		}
		else
		{
			if (document.getElementById('AccountAddrStreetId').value.length == 0) {document.getElementById('AccountAddrStreetId').focus(); alert('Выберите улицу'); return false;}				
		}
		
		if (document.getElementById('AccountAddrText').value.length == 0) {document.getElementById('AccountAddrText').focus(); alert('Введите оставшуюся часть адреса (дом, кв, офис.)'); return false;}
		
		
		
	}
	
document.getElementById("AccountTypeContact_").value="";
if (document.getElementById("AccountTypeContact1").checked) {
	/*Проверка мобильный телелфон*/
	var m = "+"+document.getElementById("AccountMobileCountryCode").value+document.getElementById("AccountMobileCode").value+document.getElementById("AccountMobile").value;
	if (!check_mobile(m)) {document.getElementById('AccountMobile').focus(); alert("Введен неправильный номер мобильного телефона в разделе Способ уведомления.\n\r Пример: + 7 950 1111111\n\r\n\rЕсли вы не хотите получать уведомления, то необходимо убрать галочку с пункта \"Отправлять SMS на номер\""); return false;}
	document.getElementById("AccountTypeContact_").value+="1";
}

if (document.getElementById("AccountTypeContact2").checked) {
	/*Проверка email*/
	var email = document.getElementById("AccountEmail").value;
	if (!check_email2(email)) {document.getElementById('AccountEmail').focus(); alert("Введен неправильный email\n\rПример: test@test.ru"); return false;}
	document.getElementById("AccountTypeContact_").value+="2";
}

if (document.getElementById("AccountTypeContact3").checked) {
	/*Проверка телефон*/
	if (document.getElementById('AccountPhoneCountryCodeContact').value.length == 0 ) document.getElementById('AccountPhoneCountryCodeContact').value = 7;
	if (!/^\d+$/.test(document.getElementById('AccountPhoneCountryCodeContact').value)) {document.getElementById('AccountPhoneCountryCodeContact').focus(); alert('Телефонный код страны должен состоять только из цифр!'); return false;}
	if (!/^\d+$/.test(document.getElementById('AccountPhoneCodeContact').value)) {document.getElementById('AccountPhoneCodeContact').focus(); alert('Телефонный код должен состоять только из цифр!'); return false;}
	if (!/^\d+$/.test(document.getElementById('AccountPhoneContact').value)) {document.getElementById('AccountPhoneContact').focus(); alert('Телефон должен состоять только из цифр!'); return false;}
	if ((document.getElementById('AccountPhoneCodeContact').value.length+document.getElementById('AccountPhoneContact').value.length) != 10 ) {document.getElementById('AccountPhoneContact').focus(); alert('Телефон и телефонный код должен состоять из 10 цифр!'); return false;}
	document.getElementById("AccountTypeContact_").value+="3";
}

if (document.getElementById("AccountTypeContact4").checked) {
	document.getElementById("AccountTypeContact_").value="4";
}
	
	
	
  return true;
}

function addfree()
{
  if (!checkOrderData())    return false;
  if (!checkAccountData())  return false;
  if (!checkShippingData()) return false;
  return true;
}

function specificationToggle()
{
  if (visible == true)
  {
    visible = false;
    document.getElementById('specificationToggle').innerHTML = 'Показать таблицу заказа';
    new Effect.SlideUp(document.getElementById('specification'));
  }
  else
  {
    visible = true;
    document.getElementById('specificationToggle').innerHTML = 'Скрыть таблицу заказа';
    new Effect.SlideDown(document.getElementById('specification'));
  }
  return false;
}

function removeRow(obj)
{
  var delRow = obj.parentNode.parentNode;
  delRow.parentNode.deleteRow(delRow.sectionRowIndex);
}

function addRow()
{
  var tbl = document.getElementById('order_table');
	var newRow = tbl.tBodies[0].insertRow(-1);
	var newCell0 = newRow.insertCell(0);
  newCell0.innerHTML = '<input type="text" style="width:100%" name="data[OrderProduct][title][]" />';
  var newCell1 = newRow.insertCell(1);
  newCell1.innerHTML = '<input type="text" style="width:100%" name="data[OrderProduct][quantity][]" />';
  var newCell2 = newRow.insertCell(2);
  newCell2.innerHTML = '<input type="text" style="width:100%" name="data[OrderProduct][description][]" />';
  var newCell3 = newRow.insertCell(3);
  newCell3.innerHTML = '<input type="button" class="button" onclick="removeRow(this);" value="Удалить">';
}

function checkOrderData()
{
  var titles  = document.getElementsByName('data[OrderProduct][title][]');
  var numbers = document.getElementsByName('data[OrderProduct][quantity][]');

  for (i=0; i<titles.length; i++)
  {
    if (titles[i].value.length==0)
    {
      alert('Вы не заполнили поле наименование!');
      titles[i].focus();
      return false;
    }

    if (numbers[i].value.length==0)
    {
      alert('Вы не уточнили количество!');
      numbers[i].focus();
      return false;
    }
  }
  return true;
}

