if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', ajaxform, false );

function ajaxform(){
  // Hide forms
  jQuery( 'form.ajaxform' ).hide().end();

  // Processing
  jQuery( 'form.ajaxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = "";
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  jQuery( 'form.ajaxform' ).show().end();
}

// +-----------------------------------------------------+
// | function: submitContact                             |
// +-----------------------------------------------------+
function submitContact() {
    
    var postData    = '';
    var fields      = Array(
        Array('product',                    1),
        Array('company',                    1),
        Array('salutation',                 1),
        Array('firstname',                  1),
        Array('surname',                    1),
        Array('street',                     1),
        Array('zipcode',                    1),
        Array('city',                       1),
        Array('position',                   1),
        Array('phone',                      1),
        Array('email',                      1),
        Array('datenschutz',                1)
    );

    //error_password_confirm
    for(i = 0; i < fields.length; i++) {
        if( fields[i][0] == 'datenschutz' ) {
            postData = ''+ postData + fields[i][0] + '=' + escape( jQuery("#"+ fields[i][0] +":checked").length ) + '&';
        } else {
            postData = ''+ postData + fields[i][0] + '=' + escape( jQuery("#"+ fields[i][0] +"").val() ) + '&';
        }
    }

    j = ajaxSubmit('submitContact', postData, fields);

}

// +-----------------------------------------------------+
// | function: ajaxSubmit                                |
// +-----------------------------------------------------+
function ajaxSubmit(ajax_url, postData, fields) {

    //jQuery("#loading").removeClass('content-hide');
    
    jQuery.ajax({
        type: 'POST',
        dataType: 'json',
        url: 'proc/'+ ajax_url +'.asp',
        data: postData,
        success: function(j){

            for(i = 0; i < fields.length; i++) {
                if( fields[i][1] == 1 ) {
                    checkField(j, fields[i][0]);
                }
            }
			
            if( j.error_code == 0 ) {
                jQuery('#kontaktDaten').hide();
				jQuery('#kontaktOk').show();
                
            } else if( j.error_code == 99 ) {
                jQuery('#kontaktFehler').show();
            } else {
				//jQuery('#kontaktFehler').show();
            }

            return j;
            
        },
        error: function(o,e,k){
           // jQuery("#loading").addClass('content-hide');
        }
    });
}

// +-----------------------------------------------------+
// | function: checkField                                |
// +-----------------------------------------------------+
function checkField(j,field_name) {
  
	if( eval('j.'+ field_name +'') == 1 ) {
        if(field_name == 'datenschutz') {
			jQuery("#datenschutzLabel").css({'color' : '#d31925'});
		} else if(field_name == 'salutation') {
            jQuery('#salutation').css({'border' : '#d31925 1px solid'});
			jQuery('#salutation option').css({'color' : '#d31925'});
        } else {
			jQuery("#"+ field_name +"").css({'border' : '#d31925 1px solid'});
		}
    } else {
		if(field_name == 'datenschutz') {
			jQuery("#datenschutzLabel").css({'color' : '#55565a'});
		} else if(field_name == 'salutation') {
            jQuery('#salutation').css({'border' : '#bbbbbb 1px solid'});
			jQuery('#salutation option').css({'color' : '#55565a'});
        } else {
			jQuery("#"+ field_name +"").css({'border' : '#bbbbbb 1px solid'});
		}
		
    }
}