window.addEvent('domready', function() {
    $$('[class=tb]').each(function(el) {
        $(el).addEvents({
            'focus': function(el) {
                $(this.get('id').replace('txt', 'inp')).setStyle('background', 'transparent url(images/inptxt1-leftg.png) no-repeat scroll 0 0');
                $(this.get('id').replace('txt', 'right')).setStyle('background', 'transparent url(images/inptxt1-rightg.png) no-repeat scroll 100% 0');
            },
            'blur': function(el) {
                $(this.get('id').replace('txt', 'inp')).setStyle('background');
                $(this.get('id').replace('txt', 'right')).setStyle('background');
            }
        });

    });
    $(document.body).getElements('textarea').addEvents({
        'focus': function(el) {
            this.getParent().setStyle('background', 'transparent url(images/textarea-bggreen.png) no-repeat scroll 0 0');
        },
        'blur': function(el) {
            this.getParent().setStyle('background');
        }
    });
});


var errorcount =0;

function checktext(form_field){
	if ($(form_field).value == ''){
	    errorcount += 1;
	    $(form_field).getParent().setStyle('background', 'transparent url(images/inptxt1-leftr.png) no-repeat scroll 0 0');
	    $(form_field).getParent().getParent().setStyle('background', 'transparent url(images/inptxt1-rightr.png) no-repeat scroll 100% 0');
		 $(form_field).removeEvents('focus');
		  $(form_field).removeEvents('blur');
        $(form_field).removeEvents('keyup');
	    $(form_field).addEvent('keyup',function(){
	        checktext(form_field);
	      });
	  }else{
	    $(form_field).getParent().setStyle('background', 'transparent url(images/inptxt1-leftg.png) no-repeat scroll 0 0');
	    $(form_field).getParent().getParent().setStyle('background', 'transparent url(images/inptxt1-rightg.png) no-repeat scroll 100% 0');
		$(form_field).addEvents({
			'focus': function(){
				$(form_field.replace('txt','inp')).setStyle('background','transparent url(images/inptxt1-leftg.png) no-repeat scroll 0 0');
				$(form_field.replace('txt','right')).setStyle('background','transparent url(images/inptxt1-rightg.png) no-repeat scroll 100% 0');
			},
			'blur': function(){
				$(form_field.replace('txt','inp')).setStyle('background','transparent url(images/inptxt1-left.png) no-repeat scroll 0 0');
				$(form_field.replace('txt','right')).setStyle('background','transparent url(images/inptxt1-right.png) no-repeat scroll 100% 0');
			}
		});
		
	}
}


function checkcomment(form_field){
	if ($(form_field).value == ''){
	    errorcount += 1;
	    $(form_field).getParent().setStyle('background', 'transparent url(images/textarea-bgred.png) no-repeat scroll 0 0');
		 $(form_field).removeEvents('focus');
		  $(form_field).removeEvents('blur');
        $(form_field).removeEvents('keyup');
	    $(form_field).addEvent('keyup',function(){
	        checkcomment(form_field);
	      });
	  }else{
	  $(form_field).getParent().setStyle('background', 'transparent url(images/textarea-bggreen.png) no-repeat scroll 0 0')
		$(form_field).addEvents({
			'focus': function(){
			    $(form_field).getParent().setStyle('background', 'transparent url(images/textarea-bggreen.png) no-repeat scroll 0 0')
			},
			'blur': function(){
			    $(form_field).getParent().setStyle('background', 'transparent url(images/textarea-bg.gif) no-repeat scroll 0 0');
			}
		});
		
	}
}

function checkselect(form_field) 
{
    var el = $(form_field);
    var val = el.options[el.selectedIndex].value;
    if (val == '')
        errorcount++;
}



function CheckForm() {

    errorcount = 0;
	
	checktext('txtFirstName');
	checktext('txtLastName');
	checktext('txtEmail');
	checktext('txtCompanyName');
	checktext('txtPhone');
	checkcomment('BusinessDetail');
	
	checkselect('txtCountry');
	checkselect('budget');

	if (errorcount==0){
		$('ContactUs').submit();
	}else{
		var ca1 = new Custom.Alert('', '<b>Errors</b> <br /><br /> <span class="little">You have ' + errorcount + ' errors</span>', {
			content: 'html',
			text: {
				confirmButtonText: '',
				closeButtonText: ''
			},
			opacify:false,
			draggable: false,
			zones: {
				box: 'box1',
				head: 'head1',
				body: 'body1',
				buttonBox: 'buttonBox1'
			},
			buttons: {
				closeButton: 'cancelButton1',
				confirmButton: 'confirmButton1'
			},
			overlay: false
	}
	);
		ca1.create();
	}
		

		
}


