var Validators = {

	email: "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$",

	name:"/^([\-a-zA-Z\u0106\u0107\u010C\u010D\u0110\u0111\u0161\u0160\u017E\u017D\ \D\s]*)$/",

	postalcode:"^([0-9]{4})",

	number:"^[\-\0-9\s]*$",

	telnum:"^([0-9]{9})",

	notEmpty: function () {
		return this.value ? true : false;
	},

	test: function ( objElm ) { 

		if ( typeof objElm.vldFunc == 'string' ) { 

			if ( typeof Validators[ objElm.vldFunc ] == 'string' ) {
				var rx = new RegExp( Validators[ objElm.vldFunc ] );
				return rx.test( objElm.value );
			} if ( typeof Validators[ objElm.vldFunc ] == 'function' )
				return Validators[ objElm.vldFunc ].call( objElm );
		}
		return false;
	},

	captcha:function ( objElm ) {

		var strBool = PXC_Request.getSync( 'contact_form.php?captcha='+this.value );
		//alert(strBool);
		if (strBool == 'true')
			return true;
		getObj('cpimg').src = 'captcha/captcha.php?width=140&height=70&characters=4&rand='+Math.floor(Math.random()*11);
		return false;
	}
};

var ValidateFlds = {

	errors: [],

	set: function ( elm ) {
		this.errors.push( elm.vldMsg );
	},

	exe: function () {
		if ( this.errors.length > 0 ) {
			alert( this.errors.join( '\n' ) );
			this.errors = [];
			return false;
		}
		return true;
	}
};

PXC_Event.attach( window, 'load',
	function () {

		setInputBehavior( getObjByName('input[name]'), 'Ime / Naziv podjetja*', 'notEmpty', 'Prosimo vpišite ime ali naziv podjetja' );
		setInputBehavior( getObjByName('input[subject]'), 'Zadeva*', 'notEmpty', 'Prosimo vpišite zadevo sporočila' );
		setInputBehavior( getObjByName('input[telnum]'), 'Kontaktna številka', '', 'Prosimo vpišite kontaktno številko' );
		setInputBehavior( getObjByName('input[email]'), 'E-poštni naslov*', 'email', 'Prosimo vpišite e-poštni naslov' );
		setInputBehavior( getObjByName('input[msg]'), 'Sporočilo*', 'notEmpty', 'Prosimo vpišite sporočilo' );


		PXC_Event.attach( getObj('sbtn'), 'click',
			function () {
				var arrValidTypes = [ 'text', 'file', 'select-one', 'textarea', 'password' ];
				var theForm = getObj('theForm');
				var elm;
				for ( var i = 0; i < theForm.elements.length; i++ ) {
					elm = theForm.elements[i];
					if ( arrValidTypes.inArray( elm.type ) && ( elm.value == elm.defVal || !Validators.test( elm ) ) ) {
						if ( elm.validate )
							ValidateFlds.set( elm );
					}
				}
				if ( ValidateFlds.exe() )
					theForm.submit();
			}
		);
		
		PXC_Event.attach( getObj('theForm'), 'reset',
			function ( e ) {

				stopDef( e );
				
				var arrValidTypes = [ 'text', 'file', 'select-one', 'textarea', 'password' ];
				var elm;
				for ( var i = 0; i < this.elements.length; i++ ) {
					elm = this.elements[i];
					if ( arrValidTypes.inArray( elm.type ) ) {
						elm.value = elm.defVal;
					}
				}
			}
		);
	}
);

function setInputBehavior( objElm, defVal, vldFunc, vldMsg ) {
	PXC_Event.attach( objElm, 'keypress',
		function () {
			this.style.color = '#666666';
		}
	);
	PXC_Event.attach( objElm, 'focus',
		function () {
			if ( this.value == defVal ) {
				this.value = '';
			} else {
				this.style.color = '#666666';
			}
		}
	);
	PXC_Event.attach( objElm, 'blur',
		function () {
			if ( this.value == '' ) {
				this.style.color = '#666666';
				this.value = defVal;
			} else {
				this.style.color = '#666666';
			}
		}
	);
	if ( objElm.type == 'textarea' )
		objElm.innerHTML = defVal;
	else
		objElm.value = defVal;
	objElm.defVal = defVal;

	if ( vldFunc && vldMsg ) {
		objElm.vldFunc = vldFunc;
		objElm.vldMsg = vldMsg;
		objElm.validate = true;
	} else
		objElm.validate = false;
}

function stopDef( e ) 
{ 
	e.returnValue = false; 
	if ( e.preventDefault )
		e.preventDefault();
}


