// following variable is used for form submissions
var ajax_post_data = new Array();
// used for form submission
var required_fields_not_filled = false;

$(document).ready(function(){
	// apply MSIE6 PNG fix
	if (typeof(jQuery.fn.supersleight) == 'function') {
		$('div#right').supersleight({shim: '/images/blank.gif'});
	}

	// select username field if empty & we're on login page
	if ((window.location.href.indexOf('/login') > -1) && !$('#user').val()) {
		$('#user').focus();
	}

	// make external pages load in new window
	$('a[@rel=external_link]').bind('click', function() {
		window.open($(this).attr('href'));
		return false;
	});

	// function to submit form data via AJAX
	function AJAXIFY() {
		ajax_post_data = new Array();
		// traverse all form items and assebmle POST data
		$('#add_form input, #add_form select').each(function() {
			if (required_fields_not_filled) {
				return false;
			}

			e = $(this);
			// check for required field
			if (typeof(e.prev('label').find('span.required').attr('title')) != 'undefined') {
				if (typeof(document.getElementById(e.attr('id')).options) != 'undefined') {
					if ((e.css('display') != 'none') && (e.val() == '0')) {
						alert(f_lang.required_field_empty);
						required_fields_not_filled = true;
						return false;
					}
				} else if (e.attr('class') == 'ibox') {
					if ((e.css('display') != 'none') && (!e.val())) {
						alert(f_lang.required_field_empty);
						required_fields_not_filled = true;
						return false;
					}
				}
			}

			// differentiate between text fields and radios
			if ((e.attr('type') == 'radio') || (e.attr('type') == 'checkbox')) {
				if (e.attr('checked')) {
					ajax_post_data.push(e.attr('name') + '=' + e.val());
				}
			}
			else {
				ajax_post_data.push(e.attr('name') + '=' + e.val());
			}
		});

		if (!required_fields_not_filled) {
			$('#add_form').hide();
			$('#errorz_div').hide();
			$('#order_div').append('<p class="centered"><img src="/images/loading2.gif" width="65" height="13" alt="loading" /></p>');

			$.post($('#add_form').attr('action'), ajax_post_data.join('&'), function(msg){
				$('#order_div').html($(msg).find('#order_div').html());
				if (document.getElementById('errors_ul') == null) {
					window.location.hash = 'page_bottom';
				} else {
					window.location.hash = 'top';
				}
			});
		} else {
			required_fields_not_filled = false;
		}
	}

	// intercept form submits and post everything via AJAX request
	$('div#order_div').intercept('submit', '#add_form', function(e) {
		AJAXIFY();	
		return false;
	});

	// submit form with each deimos variant, template and domain suffix change
	$('div#order_div').intercept('change', '#variant', function(e) {
		AJAXIFY();
	});

	$('div#order_div').intercept('change', '#template', function(e) {
		AJAXIFY();
	});

	$('div#order_div').intercept('change', '#tld', function(e) {
		if (typeof($('#domain_option').attr('id')) != 'undefined') {
			AJAXIFY();
		}
	});
	
	// change thumbnail of e-shop with each color change
	$('div#order_div').intercept('change', '#color', function(e) {
		$('#thumbnail_image').attr('src', 'http://deimos-eshop.com/eshop-thumbnails/thumbnail-'+$('#variant').val()+'-'+$('#template').val()+'-'+$('#color').val()+'.png');
	});
});