// - Send Form --------------------------------------------
function psSendForm(formData,postfix){
	postfix = (postfix)?postfix:'';
	currentWindow = formData.action;	
	var reqMain = new JsHttpRequest();
	reqMain.caching = false;
	reqMain.open('POST',formData.action,false);

	var sdata = Array();

	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() {

	var Url	=	false;
	this.FieldValidate = function(obj){

		var reqMain = new JsHttpRequest();
		reqMain.caching = false;
		reqMain.open('POST',obj.form.action,false);
		reqMain.Validator = this;
		var sdata = Array();
		sdata['cmd'] = 'field_validate';
		sdata['field_name'] = obj.name;
		sdata['field_value'] = obj.value;

		reqMain.send(sdata);
		reqMain.onreadystatechange = function(){
			if (reqMain.readyState == 4){
				if (reqMain.responseJS){
					this.Validator.SetFieldStatus(reqMain.responseJS,obj);
				}
			}
		}
	}
	
	this.SetFieldStatus = function(data,obj){
		d =	document.getElementById(obj.name+'_state')
		if (data.Error == false){
			d.className='state_validated';
			d.title='';
		}else{
			d.className='state_invalid';
			d.title = data.Error.text;
			psCreateTooltip(d)
		}
	}
}

// - Send Form --------------------------------------------
