// - Send Form --------------------------------------------
function psSendForm(formData,postfix){
	postfix = (postfix)?postfix:'';
	currentWindow = formData.action;
	reqMain = new JsHttpRequest();
	reqMain.caching = false;
	reqMain.open('POST',formData.action,false);

	var sdata = {ajax:1};

	for (var i=0; i<formData.elements.length; i++)
		sdata[formData.elements[i].name] = formData.elements[i].value;

	reqMain.send(sdata);
	reqMain.onreadystatechange = function(){
		if (reqMain.readyState == 4){
			if (reqMain.responseJS){
				psShowFormResult(reqMain.responseJS,postfix);
				return false;
			}
		}
	}
	return false;
}

function psSendComplaint(mod,xid){
	psOpenInPopUp(null,'/complaint?ajax=1&mod='+mod+'&xid='+xid,true);
//	document.psComplaintForm.doc_mod.value = mod;
//	document.psComplaintForm.doc_pid.value = xid;
}

function psShowFormResult(data,postfix){
	psFormErrorsId = document.getElementById('psFormErrors'+postfix);
	psFormMessegesId = document.getElementById('psFormMesseges'+postfix);
	psFormFieldsId = document.getElementById('psFormFields'+postfix);

	psFormMessegesId.style.display='none';
	psFormErrorsId.style.display='none';
	psFormErrorsId.innerHTML = '';

	if (data.Error){
		psFormErrorsId.style.display='';
		psFormErrorsId.innerHTML=data.Error;
		psReloadCaptcha(data,postfix);
	}else if (data.Messeges){
		psFormMessegesId.style.display='';
		psFormFieldsId.style.display='none';
		psFormMessegesId.innerHTML=data.Messeges;
	}else{
		psFormErrorsId.style.display='';

		psFormErrorsId.innerHTML='Ошибка при передаче данных';
	}
}

function psReloadCaptcha(data,postfix){
	psFormCaptchaId = document.getElementById('psFormCaptcha'+postfix);
	psFormCaptchaId.src = currentWindow+'?cmd=get_captcha&'+data.sid+'&'+data.rand
}

function psCloseAndReload(postfix)
{
	if (document.getElementById('psFormErrors'+postfix).style.display != '')
		return false;

	reloadForm = function()
	{
		var psFormMessegesId = document.getElementById('psFormMesseges'+postfix);
		var psFormFieldsId = document.getElementById('psFormFields'+postfix);
		psFormMessegesId.style.display='none';
		psFormFieldsId.style.display='';
	}

	setTimeout("psClosePopUp(); reloadForm(); ", 1300);

	return false;
}

//--------- Field Validator -------------------------------------------------------
function _psValidator() {
	this.FieldValidate = function(obj){
		var r,s;
		r = new JsHttpRequest();
		r.caching = false;
		r.open('POST',obj.form.action,false);
		r.Validator = this;
		s = {ajax:1, cmd:'field_validate', field_name:obj.name, field_value:obj.value};
		r.send(s);
		r.onreadystatechange = function(){
			if (this.readyState == 4){
				if (this.responseJS){
					this.Validator.SetFieldStatus(this.responseJS,obj);
				}
			}
		}
	}

	this.SetFieldStatus = function(data,obj){
		d =	document.getElementById(obj.name+'_state')
		if (data.cmd_errors && data.cmd_errors.length > 0){
			d.className='state_invalid';
			d.title = data.cmd_errors.join(' ');
			psCreateTooltip(d)
		}else{
			d.className='state_validated';
			d.title='';
		}
	}
}

// - Send Form --------------------------------------------
