 function trim(s) {
   return s.replace(/^\s+/, '').replace(/\s+$/, '');
 }

 function chkEditInfoForm() {
  var submit = $('btn_submit');
  var email = $('edit_user[login]');
  var errorEmail = $('error_email');

  var p1 = $('edit_user[passwd]');
  var p2 = $('edit_user[passwd2]');
  var errorPassword = $('error_password');

  errorEmail.style.display = errorPassword.style.display = 'none';
  error = false;

  var v = trim(email.value);
  if (!v == '' && !/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(v)){
   errorEmail.innerHTML = languageHash["error_email"];
   errorEmail.style.display = '';
   if (!error) { email.focus(); error = true }
  }

  var v1 = trim(p1.value);
  var v2 = trim(p2.value);
  if (v1 != v2){
    p1.value = p2.value = '';
    errorPassword.innerHTML = languageHash["error_passwd"];
    errorPassword.style.display = '';
    if (!error) { p1.focus(); error = true }
  }
  if (!error) submit.disabled = true;
  return !error
 }


 function chkFormPayOut(maxSum) {
  var submitBtn = document.getElementById('submitBtn');
  var sumEdit = document.getElementById('sumEdit');
  var sumError = document.getElementById('sumError');

  while (sumError.firstChild) sumError.removeChild(sumError.firstChild);

  var ok = true;
  var sumFloat = parseFloat(trim(sumEdit.value));

  if (isNaN(sumFloat) || sumFloat <= 0 || sumFloat > maxSum) {
   sumError.innerHTML = languageHash["error_cash"];
   if (ok) { sumEdit.focus(); ok = false }
  }

  submitBtn.disabled = ok;
  return ok
 }



 function exchange(factor,i){
  var sumEdit = $('mmailinvoice_amount'+i);

  var sumError = $('sumError'+i);
  var sumResult = $('sumResult'+i);
  var boxResult = $('boxResult'+i);

  sumError.style.display = 'none';
  boxResult.className = 'invisible';

  var ok = true;

  v = trim(sumEdit.value);
  if (v == '') {
   sumError.innerHTML = languageHash["set_cash"];
   sumError.style.display = '';
   ok = false
  }
  else if (/^\d+$/.test(v)){ 
        sumResult.innerHTML = v*factor
        boxResult.className = '';
       }
       else {
        sumError.innerHTML = languageHash["error_cash"];
        sumError.style.display = '';
        ok = false
       }
  return ok
 }

 function chkWMForm(rate) {
 	var sumEdit = $('mmailinvoice_amount2').value;
 	$('LMI_PAYMENT_AMOUNT').value = parseInt(sumEdit) * rate;

 	return true;
 }
 
 function chkFormMoneyAdd(factor, i){
  var email = $('mmailinvoice_email'+i);
  var errorEmail = $('error_email'+i);
  var submitBtn = document.getElementById('submitBtn');

  errorEmail.style.display = 'none';

  var ok = exchange(factor, i);

  v = trim(email.value);
  if (v == ''){
   errorEmail.innerHTML = languageHash["unset_email"];
   errorEmail.style.display = '';
   ok = false
  }
  else
   if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(v)){
    errorEmail.innerHTML = languageHash["error_email"];
    errorEmail.style.display = '';
    ok = false
   }

  submitBtn.disabled = ok;
  return ok
 }

function viewLenghtText(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : null
	if($('#free-description').val().length >= 350) {
	    $('#free-description').val($('#free-description').val().substr(0, 350))
	}

	$('#description-lenght').html(350 - $('#free-description').val().length);
}

function chkUserRegistrationForm(){
    $('#error_email_user').css('display', 'none');
    $('#error_password_user').css('display', 'none');
    $('#error_agree_user').css('display', 'none');

  	error = false;

	if (trim($('#reg_user_login').val()) == ''){
	    $('#error_email_user').text(languageHash["unset_email"])
							  .css('display', '');

   		if (!error) {
			$('#reg_user_login').focus();
			error = true
		}
  	}
  	else if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(trim($('#reg_user_login').val()))) {
	    $('#error_email_user').text(languageHash["error_email"])
							  .css('display', '');

   		if (!error) {
			$('#reg_user_login').focus();
			error = true
		}
   	}

	if (trim($('#reg_user_passwd').val()) != trim($('#reg_user_passwd2').val())){
    	$('#reg_user_passwd').val('');
    	$('#reg_user_passwd2').val('');
    
	    $('#error_password_user').text(languageHash["error_passwd"])
								 .css('display', '');

    	if (!error) {
			$('#reg_user_passwd').focus();
			error = true
		}
	}
  	else if(trim($('#reg_user_passwd').val()) == '') {
	    $('#error_password_user').text(languageHash["unset_passwd"])
								 .css('display', '');

    	if (!error) {
			$('#reg_user_passwd').focus();
			error = true
		}
   	}

  	if ($('#reg_user_agree').attr('checked') == false) {
	    $('#error_agree_user').text(languageHash["oferta"])
							  .css('display', '');
								 
    	if (!error) {
			$('#reg_user_agree').focus();
			error = true
		}
  	}

	if (!error) $('#btn_submit').attr('disabled', true);
  	return !error
}


function chkDesignerRegistrationForm(){

    $('#error_phone_designer').css('display', 'none');
    $('#error_email_designer').css('display', 'none');
    $('#error_password_designer').css('display', 'none');

  	error = false;

	if (trim($('#reg_designer_login').val()) == ''){
	    $('#error_email_designer').text(languageHash["unset_email"])
							  	  .css('display', '');

   		if (!error) {
			$('#reg_designer_login').focus();
			error = true
		}
  	}
  	else if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(trim($('#reg_designer_login').val()))) {
	    $('#error_email_designer').text(languageHash["error_email"])
							  	  .css('display', '');

   		if (!error) {
			$('#reg_designer_login').focus();
			error = true
		}
   	}
   	
   	if (trim($('#reg_designer_phone').val()) == ''){
	    $('#error_phone_designer').text('Заполните номер телефона')
							  	  .css('display', '');

   		if (!error) {
			$('#reg_designer_phone').focus();
			error = true
		}
  	}

	if (trim($('#reg_designer_passwd').val()) != trim($('#reg_designer_passwd2').val())){
    	$('#reg_designer_passwd').val('');
    	$('#reg_designer_passwd2').val('');

	    $('#error_password_designer').text(languageHash["error_passwd"])
								 	 .css('display', '');

    	if (!error) {
			$('#reg_designer_passwd').focus();
			error = true
		}
	}
  	else if(trim($('#reg_designer_passwd').val()) == '') {
	    $('#error_password_designer').text(languageHash["unset_passwd"])
								 	 .css('display', '');

    	if (!error) {
			$('#reg_designer_passwd').focus();
			error = true
		}
   	}


	if (!error) $('#btn_submit').attr('disabled', true);
  	return !error
}


function chkPartnerRegistrationForm(){
    $('#error_email_partner').css('display', 'none');
    $('#error_password_partner').css('display', 'none');
    $('#error_agree_partner').css('display', 'none');

  	error = false;

	if (trim($('#reg_partner_login').val()) == ''){
	    $('#error_email_partner').text(languageHash["unset_email"])
							  .css('display', '');

   		if (!error) {
			$('#reg_partner_login').focus();
			error = true
		}
  	}
  	else if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(trim($('#reg_partner_login').val()))) {
	    $('#error_email_partner').text(languageHash["error_email"])
							  .css('display', '');

   		if (!error) {
			$('#reg_partner_login').focus();
			error = true
		}
   	}

	if (trim($('#reg_partner_passwd').val()) != trim($('#reg_partner_passwd2').val())){
    	$('#reg_partner_passwd').val('');
    	$('#reg_partner_passwd2').val('');

	    $('#error_password_partner').text(languageHash["error_passwd"])
								    .css('display', '');

    	if (!error) {
			$('#reg_partner_passwd').focus();
			error = true
		}
	}
  	else if(trim($('#reg_partner_passwd').val()) == '') {
	    $('#error_password_partner').text(languageHash["unset_passwd"])
								 .css('display', '');

    	if (!error) {
			$('#reg_partner_passwd').focus();
			error = true
		}
   	}

  	if ($('#reg_partner_agree').attr('checked') == false) {
	    $('#error_agree_partner').text(languageHash["oferta"])
							  .css('display', '');

    	if (!error) {
			$('#reg_partner_agree').focus();
			error = true
		}
  	}

	if (!error) $('#btn_submit').attr('disabled', true);
  	return !error
}


function chkLoginForm(){
  	error = false;
	$('#error_login').css('display', 'none');
	$('#error_password').css('display', 'none');

	if($('#login').val() == '') {
    	$('#error_login').text(languageHash["unset_email"]);
    	$('#error_login').css('display', '');

    	if (!error) {
			$('#error_login').focus();
			error = true
		}
	}

	if($('#password').val() == '') {
    	$('#error_password').text(languageHash["unset_passwd"]);
    	$('#error_password').css('display', '');

    	if (!error) {
			$('#password').focus();
			error = true
		}
	}

	if (!error) $('#btn_submit').attr('disabled', true);

  	return !error
}


function chkPwdRemindForm() {
	$('#error_email').css('display', 'none');
  	error = false;

	if (trim($('#pwdremind_email').val()) == ''){
	    $('#error_email').text(languageHash["unset_email"])
						 .css('display', '');

   		if (!error) {
			$('#pwdremind_email').focus();
			error = true
		}
  	}
  	else if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(trim($('#pwdremind_email').val()))) {
	    $('#error_email').text(languageHash["error_email"])
						 .css('display', '');

   		if (!error) {
			$('#pwdremind_email').focus();
			error = true
		}
   	}
   	
  	if (!error) $('#btn_submit').attr('disabled', true);
  	return !error
}


function chkSendMsgForm() {
    $('#contact_error_email').css('display', 'none');
    $('#contact_error_message').css('display', 'none');
  	error = false;

	if (trim($('#contact_email').val()) == ''){
	    $('#contact_error_email').text(languageHash["unset_email"])
						 		 .css('display', '');

   		if (!error) {
			$('#contact_email').focus();
			error = true
		}
  	}
  	else if (!/^(\w+[\.\-\/])*\w+@(\w+(-\w+|)\.)+[a-z]{2,}$/i.test(trim($('#contact_email').val()))) {
	    $('#contact_error_email').text(languageHash["error_email"])
						 		 .css('display', '');

   		if (!error) {
			$('#contact_email').focus();
			error = true
		}
   	}

	if (trim($('#contact_message').val()) == ''){
	    $('#contact_error_message').text(languageHash["unset_question"])
						           .css('display', '');

   		if (!error) {
			$('#contact_message').focus();
			error = true
		}
  	}
  	
  	if (!error) $('#btn_submit').attr('disabled', true);
  	return !error
}


function chkAuthorizeForm() {

  	error = false;
	$('#error_email_1').css('display', 'none');
	$('#error_password_1').css('display', 'none');
  
	if($('#login_1').val() == '') {
    	$('#error_email_1').text(languageHash["unset_email"]);
    	$('#error_email_1').css('display', '');
    	
    	if (!error) {
			$('#error_email_1').focus();
			error = true
		}
	}

	if($('#password_1').val() == '') {
    	$('#error_password_1').text(languageHash["unset_passwd"]);
    	$('#error_password_1').css('display', '');

    	if (!error) {
			$('#password_1').focus();
			error = true
		}
	}
	
	if (!error) $('#btn_submit').attr('disabled', true);
  
  	return !error
}

function checkApproveForm () {
    $('error-repay').innerHTML = '';
    $('error-return').innerHTML = '';
    $('error-decline').innerHTML = '';

	if($('repayedDesigner').checked && $('repay-descr').value == '') {
        $('error-repay').innerHTML = languageHash["empty_text"];
        return false;
	}

	if($('sameDesigner').checked && $('return-comment').value == '') {
        $('error-return').innerHTML = languageHash["empty_text"];
        return false;
	}

	if($('otherDesigner').checked && $('decline-comment').value == '') {
        $('error-decline').innerHTML = languageHash["empty_text"];
        return false;
	}

	return true;
}

function SumAmountAdmin (obj_id, ramount, zamount) {
	
	//if($('user_mode[' + obj_id + '][R]').checked) {
	//	$("z_amount").innerHTML   = (parseFloat($("z_amount").innerHTML) + ramount * 0.04).toFixed(2);
	//	$("z_amount_2").innerHTML = (parseFloat($("z_amount_2").innerHTML) + ramount * 0.04).toFixed(2);
	//}

	if($('user_mode[' + obj_id + '][Z]') && $('user_mode[' + obj_id + '][Z]').checked) {
		$("z_amount").innerHTML = (parseFloat($("z_amount").innerHTML) + zamount).toFixed(2);
		$("z_amount_2").innerHTML = (parseFloat($("z_amount_2").innerHTML) + zamount).toFixed(2);
	}
	
	if(parseFloat($("z_amount").innerHTML) >  parseFloat($("purse_z").innerHTML)) {
		$("z_amount").style.color = "#FF0000";
		$("z_amount_2").style.color = "#FF0000";
	}	

}

function normalizeAmountAdmin (obj_id, ramount, zamount) {
	
	//if($('user_mode[' + obj_id + '][R]').checked) {
	//	$("z_amount").innerHTML = (parseFloat($("z_amount").innerHTML) - ramount * 0.04).toFixed(2);
	//	$("z_amount_2").innerHTML = (parseFloat($("z_amount_2").innerHTML) - ramount * 0.04).toFixed(2);
	//}

	if($('user_mode[' + obj_id + '][Z]') && $('user_mode[' + obj_id + '][Z]').checked) {
		$("z_amount").innerHTML = (parseFloat($("z_amount").innerHTML) - zamount).toFixed(2);
		$("z_amount_2").innerHTML = (parseFloat($("z_amount_2").innerHTML) - zamount).toFixed(2);
	}
	
	if(parseFloat($("z_amount").innerHTML) <  parseFloat($("purse_z").innerHTML)) {
		$("z_amount").style.color = "#000000";
		$("z_amount_2").style.color = "#000000";	
	}	
		
}


function checkAll(id, checked)
{
	
//	alert(document.getElementById("referer1["+ id + "]".selected));
	
		if(document.getElementById("approve" + checked + "["+ id + "]"))
			document.getElementById("approve" + checked + "["+ id + "]").checked = true;

		if(document.getElementById("approve_info" + checked + "["+ id + "]"))
			document.getElementById("approve_info" + checked + "["+ id + "]").checked = true;
		
		i = 1;
		while(document.getElementById("domen" + checked + "_"+ id + "_" + i))
		{
			document.getElementById("domen" + checked + "_"+ id + "_" + i).checked = true;
			i++;
		}
		
		i = 1;
		while(document.getElementById("referer" + checked + "_"+ id + "_" + i))
		{
			document.getElementById("referer" + checked + "_"+ id + "_" + i).checked = true;
			i++;
		}

		i = 1;
		while(document.getElementById("urlstring" + checked + "_"+ id + "_" + i))
		{
			document.getElementById("urlstring" + checked + "_"+ id + "_" + i).checked = true;
			i++;
		}

}
